@@ -17,35 +17,38 @@ That will ensure that those directories now are using the same file.
17
17
18
18
#if ICSP_PROGRAMMING
19
19
20
+ #if !USE_BIT_BANGED_SPI
21
+ #include < SPI.h>
22
+ #endif // !USE_BIT_BANGED_SPI
20
23
21
24
// programming commands to send via SPI to the chip
22
25
enum {
23
26
progamEnable = 0xAC ,
24
-
27
+
25
28
// writes are preceded by progamEnable
26
29
chipErase = 0x80 ,
27
30
writeLockByte = 0xE0 ,
28
31
writeLowFuseByte = 0xA0 ,
29
32
writeHighFuseByte = 0xA8 ,
30
33
writeExtendedFuseByte = 0xA4 ,
31
-
34
+
32
35
pollReady = 0xF0 ,
33
-
36
+
34
37
programAcknowledge = 0x53 ,
35
-
38
+
36
39
readSignatureByte = 0x30 ,
37
40
readCalibrationByte = 0x38 ,
38
-
41
+
39
42
readLowFuseByte = 0x50 , readLowFuseByteArg2 = 0x00 ,
40
43
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 ,
45
48
writeProgramMemory = 0x4C ,
46
49
loadExtendedAddressByte = 0x4D ,
47
50
loadProgramMemory = 0x40 ,
48
-
51
+
49
52
}; // end of enum
50
53
51
54
// 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
57
60
byte program (const byte b1, const byte b2, const byte b3, const byte b4)
58
61
{
59
62
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);
66
69
#else
67
70
SPI.transfer (b1);
68
71
SPI.transfer (b2);
@@ -73,7 +76,7 @@ byte program (const byte b1, const byte b2, const byte b3, const byte b4)
73
76
interrupts ();
74
77
return b;
75
78
} // end of program
76
-
79
+
77
80
// read a byte from flash memory
78
81
byte readFlash (unsigned long addr)
79
82
{
@@ -84,21 +87,21 @@ byte readFlash (unsigned long addr)
84
87
byte MSB = (addr >> 16 ) & 0xFF ;
85
88
if (MSB != lastAddressMSB)
86
89
{
87
- program (loadExtendedAddressByte, 0 , MSB);
90
+ program (loadExtendedAddressByte, 0 , MSB);
88
91
lastAddressMSB = MSB;
89
92
} // end if different MSB
90
93
91
94
return program (readProgramMemory | high, highByte (addr), lowByte (addr));
92
95
} // end of readFlash
93
-
96
+
94
97
// write a byte to the flash memory buffer (ready for committing)
95
98
void writeFlash (unsigned long addr, const byte data)
96
99
{
97
100
byte high = (addr & 1 ) ? 0x08 : 0 ; // set if high byte wanted
98
101
addr >>= 1 ; // turn into word address
99
102
program (loadProgramMemory | high, 0 , lowByte (addr), data);
100
- } // end of writeFlash
101
-
103
+ } // end of writeFlash
104
+
102
105
byte readFuse (const byte which)
103
106
{
104
107
switch (which)
@@ -108,38 +111,38 @@ byte readFuse (const byte which)
108
111
case extFuse: return program (readExtendedFuseByte, readExtendedFuseByteArg2);
109
112
case lockByte: return program (readLockByte, readLockByteArg2);
110
113
case calibrationByte: return program (readCalibrationByte);
111
- } // end of switch
112
-
114
+ } // end of switch
115
+
113
116
return 0 ;
114
117
} // end of readFuse
115
-
118
+
116
119
void readSignature (byte sig [3 ])
117
120
{
118
121
for (byte i = 0 ; i < 3 ; i++)
119
122
sig [i] = program (readSignatureByte, 0 , i);
120
-
123
+
121
124
// make sure extended address is zero to match lastAddressMSB variable
122
- program (loadExtendedAddressByte, 0 , 0 );
125
+ program (loadExtendedAddressByte, 0 , 0 );
123
126
lastAddressMSB = 0 ;
124
127
125
128
} // end of readSignature
126
-
129
+
127
130
// poll the target device until it is ready to be programmed
128
131
void pollUntilReady ()
129
132
{
130
133
if (currentSignature.timedWrites )
131
134
delay (10 ); // at least 2 x WD_FLASH which is 4.5 mS
132
135
else
133
- {
136
+ {
134
137
while ((program (pollReady) & 1 ) == 1 )
135
- {} // wait till ready
138
+ {} // wait till ready
136
139
} // end of if
137
140
} // end of pollUntilReady
138
-
141
+
139
142
// commit page to flash memory
140
143
void commitPage (unsigned long addr, bool showMessage)
141
144
{
142
-
145
+
143
146
if (showMessage)
144
147
{
145
148
Serial.print (F (" Committing page starting at 0x" ));
@@ -149,49 +152,49 @@ void commitPage (unsigned long addr, bool showMessage)
149
152
showProgress ();
150
153
151
154
addr >>= 1 ; // turn into word address
152
-
155
+
153
156
// set the extended (most significant) address byte if necessary
154
157
byte MSB = (addr >> 16 ) & 0xFF ;
155
158
if (MSB != lastAddressMSB)
156
159
{
157
- program (loadExtendedAddressByte, 0 , MSB);
160
+ program (loadExtendedAddressByte, 0 , MSB);
158
161
lastAddressMSB = MSB;
159
162
} // end if different MSB
160
-
163
+
161
164
program (writeProgramMemory, highByte (addr), lowByte (addr));
162
- pollUntilReady ();
163
-
165
+ pollUntilReady ();
166
+
164
167
clearPage (); // clear ready for next page full
165
168
} // end of commitPage
166
169
167
170
void eraseMemory ()
168
171
{
169
172
program (progamEnable, chipErase); // erase it
170
173
delay (20 ); // for Atmega8
171
- pollUntilReady ();
174
+ pollUntilReady ();
172
175
clearPage (); // clear temporary page
173
176
} // end of eraseMemory
174
-
177
+
175
178
// write specified value to specified fuse/lock byte
176
179
void writeFuse (const byte newValue, const byte whichFuse)
177
180
{
178
181
if (newValue == 0 )
179
182
return ; // ignore
180
-
183
+
181
184
program (progamEnable, fuseCommands [whichFuse], 0 , newValue);
182
- pollUntilReady ();
185
+ pollUntilReady ();
183
186
} // end of writeFuse
184
187
185
- // put chip into programming mode
188
+ // put chip into programming mode
186
189
bool startProgramming ()
187
190
{
188
-
191
+
189
192
Serial.print (F (" Attempting to enter ICSP programming mode ..." ));
190
-
193
+
191
194
byte confirm;
192
195
pinMode (RESET, OUTPUT);
193
196
194
- #if USE_BIT_BANGED_SPI
197
+ #if USE_BIT_BANGED_SPI
195
198
digitalWrite (MSPIM_SCK, LOW);
196
199
pinMode (MSPIM_SCK, OUTPUT);
197
200
pinMode (BB_MOSI, OUTPUT);
@@ -203,17 +206,17 @@ bool startProgramming ()
203
206
#endif // (not) USE_BIT_BANGED_SPI
204
207
205
208
unsigned int timeout = 0 ;
206
-
209
+
207
210
// we are in sync if we get back programAcknowledge on the third byte
208
- do
211
+ do
209
212
{
210
213
// regrouping pause
211
214
delay (100 );
212
215
213
216
// ensure SCK low
214
217
noInterrupts ();
215
-
216
- #if USE_BIT_BANGED_SPI
218
+
219
+ #if USE_BIT_BANGED_SPI
217
220
digitalWrite (MSPIM_SCK, LOW);
218
221
#else
219
222
digitalWrite (SCK, LOW);
@@ -227,11 +230,11 @@ bool startProgramming ()
227
230
228
231
delay (25 ); // wait at least 20 mS
229
232
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 );
235
238
#else
236
239
SPI.transfer (progamEnable);
237
240
SPI.transfer (programAcknowledge);
@@ -240,7 +243,7 @@ bool startProgramming ()
240
243
#endif // (not) USE_BIT_BANGED_SPI
241
244
242
245
interrupts ();
243
-
246
+
244
247
if (confirm != programAcknowledge)
245
248
{
246
249
Serial.print (" ." );
@@ -251,25 +254,25 @@ bool startProgramming ()
251
254
return false ;
252
255
} // end of too many attempts
253
256
} // end of not entered programming mode
254
-
257
+
255
258
} while (confirm != programAcknowledge);
256
-
259
+
257
260
Serial.println ();
258
261
Serial.println (F (" Entered programming mode OK." ));
259
262
return true ;
260
263
} // end of startProgramming
261
-
264
+
262
265
void stopProgramming ()
263
266
{
264
- digitalWrite (RESET, LOW);
267
+ digitalWrite (RESET, LOW);
265
268
pinMode (RESET, INPUT);
266
269
267
- #if USE_BIT_BANGED_SPI
270
+ #if USE_BIT_BANGED_SPI
268
271
// turn off pull-ups
269
272
digitalWrite (MSPIM_SCK, LOW);
270
273
digitalWrite (BB_MOSI, LOW);
271
274
digitalWrite (BB_MISO, LOW);
272
-
275
+
273
276
// set everything back to inputs
274
277
pinMode (MSPIM_SCK, INPUT);
275
278
pinMode (BB_MOSI, INPUT);
@@ -287,24 +290,24 @@ void stopProgramming ()
287
290
pinMode (MOSI, INPUT);
288
291
pinMode (MISO, INPUT);
289
292
#endif // (not) USE_BIT_BANGED_SPI
290
-
293
+
291
294
Serial.println (F (" Programming mode off." ));
292
-
295
+
293
296
} // end of stopProgramming
294
-
295
- // called from setup()
297
+
298
+ // called from setup()
296
299
void initPins ()
297
300
{
298
301
299
302
// set up 8 MHz timer on pin 9
300
- pinMode (CLOCKOUT, OUTPUT);
303
+ pinMode (CLOCKOUT, OUTPUT);
301
304
// set up Timer 1
302
305
TCCR1A = bit (COM1A0); // toggle OC1A on Compare Match
303
306
TCCR1B = bit (WGM12) | bit (CS10); // CTC, no prescaling
304
307
OCR1A = 0 ; // output every cycle
305
-
306
308
307
-
309
+
310
+
308
311
} // end of initPins
309
-
312
+
310
313
#endif // ICSP_PROGRAMMING
0 commit comments