Skip to content

Commit efd5473

Browse files
committed
Fixed include of SPI.h
1 parent f4bd928 commit efd5473

File tree

3 files changed

+144
-137
lines changed

3 files changed

+144
-137
lines changed

Atmega_Board_Detector/ICSP_Utils.ino

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,38 @@ That will ensure that those directories now are using the same file.
1717

1818
#if ICSP_PROGRAMMING
1919

20+
#if !USE_BIT_BANGED_SPI
21+
#include <SPI.h>
22+
#endif // !USE_BIT_BANGED_SPI
2023

2124
// programming commands to send via SPI to the chip
2225
enum {
2326
progamEnable = 0xAC,
24-
27+
2528
// writes are preceded by progamEnable
2629
chipErase = 0x80,
2730
writeLockByte = 0xE0,
2831
writeLowFuseByte = 0xA0,
2932
writeHighFuseByte = 0xA8,
3033
writeExtendedFuseByte = 0xA4,
31-
34+
3235
pollReady = 0xF0,
33-
36+
3437
programAcknowledge = 0x53,
35-
38+
3639
readSignatureByte = 0x30,
3740
readCalibrationByte = 0x38,
38-
41+
3942
readLowFuseByte = 0x50, readLowFuseByteArg2 = 0x00,
4043
readExtendedFuseByte = 0x50, readExtendedFuseByteArg2 = 0x08,
41-
readHighFuseByte = 0x58, readHighFuseByteArg2 = 0x08,
42-
readLockByte = 0x58, readLockByteArg2 = 0x00,
43-
44-
readProgramMemory = 0x20,
44+
readHighFuseByte = 0x58, readHighFuseByteArg2 = 0x08,
45+
readLockByte = 0x58, readLockByteArg2 = 0x00,
46+
47+
readProgramMemory = 0x20,
4548
writeProgramMemory = 0x4C,
4649
loadExtendedAddressByte = 0x4D,
4750
loadProgramMemory = 0x40,
48-
51+
4952
}; // end of enum
5053

5154
// which program instruction writes which fuse
@@ -57,12 +60,12 @@ byte program (const byte b1, const byte b2 = 0, const byte b3 = 0, const byte b4
5760
byte program (const byte b1, const byte b2, const byte b3, const byte b4)
5861
{
5962
noInterrupts ();
60-
#if USE_BIT_BANGED_SPI
61-
62-
BB_SPITransfer (b1);
63-
BB_SPITransfer (b2);
64-
BB_SPITransfer (b3);
65-
byte b = BB_SPITransfer (b4);
63+
#if USE_BIT_BANGED_SPI
64+
65+
BB_SPITransfer (b1);
66+
BB_SPITransfer (b2);
67+
BB_SPITransfer (b3);
68+
byte b = BB_SPITransfer (b4);
6669
#else
6770
SPI.transfer (b1);
6871
SPI.transfer (b2);
@@ -73,7 +76,7 @@ byte program (const byte b1, const byte b2, const byte b3, const byte b4)
7376
interrupts ();
7477
return b;
7578
} // end of program
76-
79+
7780
// read a byte from flash memory
7881
byte readFlash (unsigned long addr)
7982
{
@@ -84,21 +87,21 @@ byte readFlash (unsigned long addr)
8487
byte MSB = (addr >> 16) & 0xFF;
8588
if (MSB != lastAddressMSB)
8689
{
87-
program (loadExtendedAddressByte, 0, MSB);
90+
program (loadExtendedAddressByte, 0, MSB);
8891
lastAddressMSB = MSB;
8992
} // end if different MSB
9093

9194
return program (readProgramMemory | high, highByte (addr), lowByte (addr));
9295
} // end of readFlash
93-
96+
9497
// write a byte to the flash memory buffer (ready for committing)
9598
void writeFlash (unsigned long addr, const byte data)
9699
{
97100
byte high = (addr & 1) ? 0x08 : 0; // set if high byte wanted
98101
addr >>= 1; // turn into word address
99102
program (loadProgramMemory | high, 0, lowByte (addr), data);
100-
} // end of writeFlash
101-
103+
} // end of writeFlash
104+
102105
byte readFuse (const byte which)
103106
{
104107
switch (which)
@@ -108,38 +111,38 @@ byte readFuse (const byte which)
108111
case extFuse: return program (readExtendedFuseByte, readExtendedFuseByteArg2);
109112
case lockByte: return program (readLockByte, readLockByteArg2);
110113
case calibrationByte: return program (readCalibrationByte);
111-
} // end of switch
112-
114+
} // end of switch
115+
113116
return 0;
114117
} // end of readFuse
115-
118+
116119
void readSignature (byte sig [3])
117120
{
118121
for (byte i = 0; i < 3; i++)
119122
sig [i] = program (readSignatureByte, 0, i);
120-
123+
121124
// make sure extended address is zero to match lastAddressMSB variable
122-
program (loadExtendedAddressByte, 0, 0);
125+
program (loadExtendedAddressByte, 0, 0);
123126
lastAddressMSB = 0;
124127

125128
} // end of readSignature
126-
129+
127130
// poll the target device until it is ready to be programmed
128131
void pollUntilReady ()
129132
{
130133
if (currentSignature.timedWrites)
131134
delay (10); // at least 2 x WD_FLASH which is 4.5 mS
132135
else
133-
{
136+
{
134137
while ((program (pollReady) & 1) == 1)
135-
{} // wait till ready
138+
{} // wait till ready
136139
} // end of if
137140
} // end of pollUntilReady
138-
141+
139142
// commit page to flash memory
140143
void commitPage (unsigned long addr, bool showMessage)
141144
{
142-
145+
143146
if (showMessage)
144147
{
145148
Serial.print (F("Committing page starting at 0x"));
@@ -149,49 +152,49 @@ void commitPage (unsigned long addr, bool showMessage)
149152
showProgress ();
150153

151154
addr >>= 1; // turn into word address
152-
155+
153156
// set the extended (most significant) address byte if necessary
154157
byte MSB = (addr >> 16) & 0xFF;
155158
if (MSB != lastAddressMSB)
156159
{
157-
program (loadExtendedAddressByte, 0, MSB);
160+
program (loadExtendedAddressByte, 0, MSB);
158161
lastAddressMSB = MSB;
159162
} // end if different MSB
160-
163+
161164
program (writeProgramMemory, highByte (addr), lowByte (addr));
162-
pollUntilReady ();
163-
165+
pollUntilReady ();
166+
164167
clearPage(); // clear ready for next page full
165168
} // end of commitPage
166169

167170
void eraseMemory ()
168171
{
169172
program (progamEnable, chipErase); // erase it
170173
delay (20); // for Atmega8
171-
pollUntilReady ();
174+
pollUntilReady ();
172175
clearPage(); // clear temporary page
173176
} // end of eraseMemory
174-
177+
175178
// write specified value to specified fuse/lock byte
176179
void writeFuse (const byte newValue, const byte whichFuse)
177180
{
178181
if (newValue == 0)
179182
return; // ignore
180-
183+
181184
program (progamEnable, fuseCommands [whichFuse], 0, newValue);
182-
pollUntilReady ();
185+
pollUntilReady ();
183186
} // end of writeFuse
184187

185-
// put chip into programming mode
188+
// put chip into programming mode
186189
bool startProgramming ()
187190
{
188-
191+
189192
Serial.print (F("Attempting to enter ICSP programming mode ..."));
190-
193+
191194
byte confirm;
192195
pinMode (RESET, OUTPUT);
193196

194-
#if USE_BIT_BANGED_SPI
197+
#if USE_BIT_BANGED_SPI
195198
digitalWrite (MSPIM_SCK, LOW);
196199
pinMode (MSPIM_SCK, OUTPUT);
197200
pinMode (BB_MOSI, OUTPUT);
@@ -203,17 +206,17 @@ bool startProgramming ()
203206
#endif // (not) USE_BIT_BANGED_SPI
204207

205208
unsigned int timeout = 0;
206-
209+
207210
// we are in sync if we get back programAcknowledge on the third byte
208-
do
211+
do
209212
{
210213
// regrouping pause
211214
delay (100);
212215

213216
// ensure SCK low
214217
noInterrupts ();
215-
216-
#if USE_BIT_BANGED_SPI
218+
219+
#if USE_BIT_BANGED_SPI
217220
digitalWrite (MSPIM_SCK, LOW);
218221
#else
219222
digitalWrite (SCK, LOW);
@@ -227,11 +230,11 @@ bool startProgramming ()
227230

228231
delay (25); // wait at least 20 mS
229232
noInterrupts ();
230-
#if USE_BIT_BANGED_SPI
231-
BB_SPITransfer (progamEnable);
232-
BB_SPITransfer (programAcknowledge);
233-
confirm = BB_SPITransfer (0);
234-
BB_SPITransfer (0);
233+
#if USE_BIT_BANGED_SPI
234+
BB_SPITransfer (progamEnable);
235+
BB_SPITransfer (programAcknowledge);
236+
confirm = BB_SPITransfer (0);
237+
BB_SPITransfer (0);
235238
#else
236239
SPI.transfer (progamEnable);
237240
SPI.transfer (programAcknowledge);
@@ -240,7 +243,7 @@ bool startProgramming ()
240243
#endif // (not) USE_BIT_BANGED_SPI
241244

242245
interrupts ();
243-
246+
244247
if (confirm != programAcknowledge)
245248
{
246249
Serial.print (".");
@@ -251,25 +254,25 @@ bool startProgramming ()
251254
return false;
252255
} // end of too many attempts
253256
} // end of not entered programming mode
254-
257+
255258
} while (confirm != programAcknowledge);
256-
259+
257260
Serial.println ();
258261
Serial.println (F("Entered programming mode OK."));
259262
return true;
260263
} // end of startProgramming
261-
264+
262265
void stopProgramming ()
263266
{
264-
digitalWrite (RESET, LOW);
267+
digitalWrite (RESET, LOW);
265268
pinMode (RESET, INPUT);
266269

267-
#if USE_BIT_BANGED_SPI
270+
#if USE_BIT_BANGED_SPI
268271
// turn off pull-ups
269272
digitalWrite (MSPIM_SCK, LOW);
270273
digitalWrite (BB_MOSI, LOW);
271274
digitalWrite (BB_MISO, LOW);
272-
275+
273276
// set everything back to inputs
274277
pinMode (MSPIM_SCK, INPUT);
275278
pinMode (BB_MOSI, INPUT);
@@ -287,24 +290,24 @@ void stopProgramming ()
287290
pinMode (MOSI, INPUT);
288291
pinMode (MISO, INPUT);
289292
#endif // (not) USE_BIT_BANGED_SPI
290-
293+
291294
Serial.println (F("Programming mode off."));
292-
295+
293296
} // end of stopProgramming
294-
295-
// called from setup()
297+
298+
// called from setup()
296299
void initPins ()
297300
{
298301

299302
// set up 8 MHz timer on pin 9
300-
pinMode (CLOCKOUT, OUTPUT);
303+
pinMode (CLOCKOUT, OUTPUT);
301304
// set up Timer 1
302305
TCCR1A = bit (COM1A0); // toggle OC1A on Compare Match
303306
TCCR1B = bit (WGM12) | bit (CS10); // CTC, no prescaling
304307
OCR1A = 0; // output every cycle
305-
306308

307-
309+
310+
308311
} // end of initPins
309-
312+
310313
#endif // ICSP_PROGRAMMING

0 commit comments

Comments
 (0)