-
Notifications
You must be signed in to change notification settings - Fork 30
/
prog-via-spi.mu4
115 lines (89 loc) · 3.62 KB
/
prog-via-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
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2024 David Frech. (Read the LICENSE for details.)
loading PIC18 programming over SPI
hex
| We need to talk to the code running on the STM32. Let's load some
| host-side code to do that.
ld target/ARM/stm32/picprog-host.mu4
| Make it easy to do different tasks in chunks without repeating all the
| "chunking" code.
variable 'each-chunk ( buf a len) ( executed for each chunk)
: do-chunk ( a len - a+len)
2dup + push ( increment address for the next chunk)
over image+ -rot ( calculate buf address from a and image)
'each-chunk @execute pop ;
| Since we know we have a 256 byte buffer available on the STM32, use that
| as our chunk size.
: chunked ( a u)
100 /mod ( r q) swap push for 100 do-chunk next
pop =if ( rem) do-chunk drop ^ then 2drop ;
| A defining word for a task that can be chunked. Takes a string with an
| action name, and a cfa that is called for each chunk.
: region-chunked ( action-string each-chunk-cfa)
2constant does> 2@ 'each-chunk ! cr count type space .region space
region chunked ;
| NOTE: The flash and user-id regions are word-wide and page-erasable. The
| config and eeprom regions are byte-wide and auto-erase-per-byte.
z" Programming" dup
-: ( buf a u) cr ." flash " over u. dup u. prog-flash-chunk ;
region-chunked prog-region
( Programming, from above)
-: ( buf a u) cr ." config/eeprom " over u. dup u. prog-ee-chunk ;
region-chunked prog-ee-region
: 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
boot prog-region
flash prog-region
user-id prog-region
config prog-ee-region
eeprom prog-ee-region
-prog ;
| For simplicity I'm going to verify byte-wise, even though most of the
| flash regions are word-wide.
: verify-pad ( buf a u)
pad m ! push over - ( -'image) swap pop ( -'image buf u)
over + swap do m* i c@ xor if i over + u. then loop drop ;
z" Verifying" dup
-: ( buf a u) 2dup pad -rot read-flash-chunk verify-pad ;
region-chunked verify-region
( Verifying, from above)
-: ( buf a u) 2dup pad -rot read-ee-chunk verify-pad ;
region-chunked verify-ee-region
( We have to distinguish between this and the chat version!)
: spi-verify
radix preserve hex h preserve +prog
boot verify-region
flash verify-region
user-id verify-region
config verify-ee-region
eeprom verify-ee-region
-prog ;
| The following is copied-over AVR code, but something like this could be
| useful to have.
.ifdef maybe-later
: 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 ;
: read-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, open-file-ro ( 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 ;
.then ( maybe-later)