-
Notifications
You must be signed in to change notification settings - Fork 30
/
avrprog-spi.mu4
350 lines (277 loc) · 10.2 KB
/
avrprog-spi.mu4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2024 David Frech. (Read the LICENSE for details.)
loading AVR programming over SPI
variable fuses ( fuse at byte 0; hfuse @ 1; efuse @ 2)
variable locks ( lock bits)
| comment %%
| We need to have loaded a "driver" that defines the following words:
|
| avr.Hello
| avr.Bye
|
| avr.ResetHigh
| avr.ResetLow
|
| avr.SlowClock
| avr.FastClock
|
| avr.Read ( avr-cmd addr data) ( leaves result in pad)
| avr.Write ( avr-cmd addr data)
| avr.BulkRead ( avr-cmd addr toggle buf len)
| avr.BulkWrite ( avr-cmd addr toggle buf len)
| %%
.ifdef avr.Hello
hex
| Having loaded a simple driver that talks to either a serial-connected or
| USB-connected device that can talk SPI to an AVR target, load the bulk of
| the commands for talking to the target.
: avr2 ( cmd addr data - r2) ( do command; return second-to-last byte of response)
avr.Read pad 2 + c@ ;
: avr3 ( cmd addr data - r3) ( do command; return last byte of response)
avr.Read pad 3 + c@ ;
: us #1000 * ( ns) 0 swap nanosleep ;
: ms #1000 * us ;
: +reset avr.ResetLow #100 ms ;
: -reset avr.ResetHigh #100 ms ;
: reset avr.Hello +reset -reset avr.Bye ;
| Sets slow-clock! If you want fast-clock, you have to call it *after*
| +prog.
: +prog ( enable programming)
avr.Hello avr.SlowClock
4 for +reset 0ac 53_00 0 avr2 53 = if pop drop ^ then
-reset next
." Couldn't enable serial programming" ;
: -prog ( disable programming)
-reset avr.Bye ;
: fast-clock
.ifndef spi-slow avr.FastClock
.else avr.SlowClock
.then ;
: read-sig ( - s0:s1:s2)
30 2 0 avr3
30 1 0 avr3
30 0 0 avr3
lohi> lohi> ;
| NOTE: wait polling is *not* available on the mega8515. You have to simply
| delay instead. Also, the 8515 lacks an efuse, so let's reflect that here
| as well.
.ifdef mega8515
( 8515 doesn't have wait polling, so we have to delay instead.)
: wait-fuse #4500 us ; ( 4.5ms)
: wait-flash #4500 us ;
: wait-eeprom #9000 us ; ( 9ms)
: wait-erase #9000 us ;
( efuse doesn't exist on 8515)
: read-fuses ( - fuses = fuse at byte 0; hfuse @ 1)
50 00_00 0 avr3
58 08_00 0 avr3 ( fuse hfuse)
lohi> ;
: write-fuses ( fuses = fuse at byte 0; hfuse @ 1)
>hilo push 0ac 0a0_00 pop avr.Write ( write fuse bits) wait-fuse
push 0ac 0a8_00 pop avr.Write ( write hfuse bits) wait-fuse ;
| Since the datasheet is silent on the topic of how long it takes to
| program the lock bits, we assume it's the same as writing the fuses.
: write-locks ( locks)
push 0ac 0e0_00 pop avr.Write wait-fuse ;
.else ( everything but the 8515)
( Use wait polling.)
: ?busy
begin 0f0 0 0 avr3 1 and 0= until ;
: wait-eeprom ?busy ;
: wait-flash ?busy ;
: wait-erase ?busy ;
: read-fuses ( - fuses = fuse at byte 0; hfuse @ 1; efuse @ 2)
50 00_00 0 avr3
58 08_00 0 avr3
50 08_00 0 avr3 ( fuse hfuse efuse)
lohi> lohi> ;
: write-fuses ( fuses = fuse at byte 0; hfuse @ 1; efuse @ 2)
>hilo push 0ac 0a0_00 pop avr.Write ( write fuse bits) ?busy
>hilo push 0ac 0a8_00 pop avr.Write ( write hfuse bits) ?busy
push 0ac 0a4_00 pop avr.Write ( write efuse bits) ?busy ;
: write-locks ( locks)
push 0ac 0e0_00 pop avr.Write ?busy ;
.then
: prog-fuses
fuses @ =if ( if fuses to program...)
dup read-fuses xor if ( ...and if different from chip's)
cr ." Writing fuses"
write-fuses
^ then then drop ;
: read-locks ( - locks)
58 0 0 avr3 ;
: prog-locks
locks @ =if ( if locks to program...)
dup read-locks xor if ( ...and if different from chip's)
cr ." Writing lock bits"
write-locks
^ then then drop ;
( This leaves the fuses alone but resets the lock bits.)
: erase-chip
0ac 80_00 0 avr.Write wait-erase ;
| Instead of reusing the memory dump's p pointer - the semantics of which
| are different than they were originally - let's create our own.
variable pp ( programming pointer - contains a target address)
: pp& ( b) pp @ image-c! 1 pp +! ;
: pp* ( - b) pp @ image-c@ 1 pp +! ;
: pp-byte ( - byte-addr) pp @ ; ( pp, as byte pointer)
: pp-word ( - word-addr) pp @ u2/ ; ( pp, as word pointer)
: pp-host ( - host-addr) pp @ image+ ; ( pp, as host [image] pointer)
| Read just the first few kb of the chip. This is _really_ slow, and is
| mostly here to test that the fast code does the right thing. Both
| versions print a . for every 128 bytes read.
: read-flash-slowly
flash 0ff fill-image 0 pp !
+prog fast-clock
[ 1 Ki #64 / #] for
#64 for ( 64 words, 128 bytes)
20 pp-word 0 avr3 pp& ( read flash lo byte)
28 pp-word 0 avr3 pp& ( read flash hi byte)
next char . emit
next
-prog ;
| We read the chip in chunks, but the buffer size does not need to match
| the flash page size, and in fact this would fail for the mega644, since
| the page size is 256 bytes, but my USB code will break if asked for a
| buffer this size! So let's always read 128 byte chunks instead.
: read-flash
flash 0ff fill-image 0 pp !
[ #flash #128 / #] for
20 pp-word 8 pp-host #128 avr.BulkRead
#128 pp +!
char . emit
next ;
: read-eeprom
eeprom 0 fill-image 0 pp !
[ #eeprom #128 / #] for ( read in 128 byte chunks)
0a0 pp-byte 0 pp-host #128 avr.BulkRead
#128 pp +!
char . emit
next ;
: read-eeprom-slowly
eeprom 0 fill-image 0 pp !
#eeprom for
0a0 pp-byte 0 avr3 pp&
char . emit
next ;
: read-chip
h preserve
+prog fast-clock
read-flash read-eeprom read-fuses fuses ! read-locks locks !
-prog ;
( This checks to see if the _image_, not the chip, is blank.)
: blank?
0 pp !
0ff #image for pp* and next 0ff = ;
b/page 2/ constant w/page
| lo-addr is the _word_ offset within a flash page.
| hi-addr is the _word_ address of a flash page.
|
| Both hi-addr and lo-addr are broken into bytes - big-endian! - to send
| via SPI to the chip.
: lo-addr pp-word [ w/page 1- #] and ;
: hi-addr pp-word [ w/page negate #] and ;
| NOTE: prog-region and prog-eeprom are _not_ standalone; they can only be
| called after prog mode is set and the SPI clock set properly.
: prog-region
region swap pp ! ( len)
b/page 1- + ( round up) b/page / for ( program flash pages)
cr ." page " pp-byte u.
40 lo-addr 8 pp-host b/page avr.BulkWrite
4c hi-addr 0 avr.Write wait-flash ( program page)
b/page pp +!
next ;
: prog-flash
cr ." Writing application" flash prog-region
cr ." Writing bootloader" boot prog-region ;
| NOTE: We program eeprom a byte at a time since the 8515 lacks page
| access. It's also simpler this way to determine whether to program a
| byte, rather than checking if the entire page is FF.
: prog-ee-byte
pp-byte pp* ( addr byte)
dup 0ff = if 2drop ^ then ( skip if byte is 0ff)
0c0 -rot avr.Write wait-eeprom ;
: prog-eeprom
cr ." Writing eeprom"
eeprom region swap pp ! ( len)
for ( program eeprom bytes)
prog-ee-byte
next ;
| NOTE: The application part of the flash - but *not* the bootloader! - can
| be programmed via serial chat. We have a nice word for this: prog.
|
| Since it's nice to have both tools in our toolbox at the same time, let's
| give the SPI version a slightly different name.
: spi-prog
cr warn"
You should commit your changes *before* programming the flash
if you want the signature to reference the correct commits!
" radix preserve hex
h preserve
+prog prog-fuses erase-chip -prog
+prog fast-clock prog-flash prog-eeprom prog-locks -prog ;
| In order to test that this code works with "factory fresh" chips, let's
| restore to a default state the fuses that affect the EEPROM and the
| clock: EESAVE unprogrammed, CKDIV8 programmed, and CKSEL=2 (to use the
| on-chip RC oscillator.)
: factory-fresh
+prog read-fuses 0ff bic 862 or write-fuses erase-chip -prog ;
: verify-pad
pad #128 for dup c@ pp* xor if pp-byte -1 +a u. then
1+ next drop ;
| NOTE: verify-flash and verify-eeprom are _not_ standalone; they can only
| be called after prog mode is set and the SPI clock set properly.
: spi-verify-region
region swap pp ! ( len)
#127 + #128 / for
20 pp-word 8 pad #128 avr.BulkRead
verify-pad
next ;
: spi-verify-flash
cr ." Verifying application " flash spi-verify-region
cr ." Verifying bootloader " boot spi-verify-region ;
: spi-verify-eeprom
cr ." Verifying eeprom "
eeprom region swap pp ! ( len)
#127 + #128 / for
0a0 pp-byte 0 pad #128 avr.BulkRead
verify-pad
next ;
| Since fuses and locks can't be verified over chat, they don't need "spi-"
| prefixes.
: verify-fuses
cr ." Verifying fuses "
read-fuses fuses @ 2dup = if 2drop ^ then ( ok)
swap u. u. ( chip, then saved) ;
: verify-locks
cr ." Verifying lock bits "
read-locks locks @ 2dup = if 2drop ^ then ( ok)
swap u. u. ( chip, then saved) ;
: spi-verify
radix preserve hex
h preserve
+prog verify-fuses -prog
+prog fast-clock spi-verify-flash spi-verify-eeprom verify-locks -prog ;
.then ( avr.Hello)
: save-image
h preserve
token, create-file ( fd)
( header) dup " muforth AVR img " write
( flash) dup flash 'image #image write
( eeprom) dup eeprom 'image #image write
( fuses) dup fuses 8 write
( lock bits) dup locks 8 write
close-file ;
: load-image
wipe
flash #flash \m allot ( appear to have filled the flash and eeprom)
eeprom #eeprom \m allot ( ... so prog and verify will work)
token, r/o open-file? ?abort ( fd)
( check header) dup pad #16 read #16 xor if error" no header" then
" muforth AVR img " pad #16 string= not if error" not an AVR image" then
( read flash) dup flash 'image #image read u.
( read eeprom) dup eeprom 'image #image read u.
( read fuses) dup fuses 8 read u.
( read lock bits) dup locks 8 read u.
close-file flash ;