ReprogApp.fth (6337B)
1 \ ReprogApp.fth - reprogram LPC2xxx Flash 2 3 (( 4 Copyright (c) 2004, 2008, 2009, 2010 5 MicroProcessor Engineering 6 133 Hill Lane 7 Southampton SO15 5AF 8 England 9 10 tel: +44 (0)23 8063 1441 11 fax: +44 (0)23 8033 9691 12 net: mpe@mpeforth.com 13 tech-support@mpeforth.com 14 web: www.mpeforth.com 15 16 From North America, our telephone and fax numbers are: 17 011 44 23 8063 1441 18 011 44 23 8033 9691 19 20 21 To do 22 ===== 23 At end of download, check for a partial block to program. 24 25 Change max program size to depend on CPU. 26 27 Change history 28 ============== 29 20100203 SFP006 STM32 conversion. 30 20100125 SFP005 LPC17xx conversion. 31 20090205 SFP004 LPC2388 does not erase last sector at $0007:D000 32 20081008 SFP003 Minor documentation changes. 33 20060701 SFP002 Added CopyFlash. 34 20050627 MPE002 Now uses sector tables. 35 20040212 MPE001 First release 36 )) 37 38 only forth definitions decimal 39 40 41 \ ******************* 42 \ *S Flash primitives 43 \ ******************* 44 \ *P Although some of these routines are not the most efficient 45 \ ** for the STM32 series, they are designed to be easily expanded 46 \ ** for future STM32 parts with as yet unknown sector sizes. 47 48 (( 49 FlashBase constant FlashBase 50 _FPEC constant _FPEC 51 52 : .FPEC \ -- 53 _FPEC $24 pdump ; 54 )) 55 56 : ?Unlock \ -- 57 \ *G Unlock the Flash if protected. 58 _FPEC flCR + @ bit7 and if 59 _FPEC flKEY1 over flKEYR + ! \ write keys 60 flKEY2 over flKEYR + ! 61 bit7 swap flCR + bic! \ clear LOCK bit 62 endif 63 ; 64 65 : waitFlNB \ -- status 66 \ *G Wait until the Flash is not busy and return the status. 67 \ ** The top bit is set if the operation timed out. 68 $0010:0000 _FPEC begin \ -- cnt fpec 69 swap 1- swap over \ check timeout counter 70 while 71 dup flSR + @ bit0 and 0= 72 until then 73 dup flSR + @ \ -- cnt fpec status ; get status 74 dup $0034 and rot flSR + ! \ -- cnt status ; clear h/w status 75 swap 0= 76 if $8000:0000 or endif 77 ; 78 79 : flw! \ w addr -- status 80 \ *G Write 16 bits to flash. A status value of $20 indicates 81 \ ** success. 82 ?Unlock 83 bit0 _FPEC flCR + or! \ set PG bit in control reg 84 w! \ write data 85 waitFlNB \ wait for completion/timeout 86 bit0 _FPEC flCR + bic! \ clear PG bit in control reg 87 ; 88 89 : flPageErase \ addr -- status 90 \ *G Erase the page containing addr. A status value of $20 91 \ ** indicates success. 92 FlashBase - \ relative to start of Flash 93 ?Unlock _FPEC 94 bit1 over flCR + or! \ set PER bit in control reg 95 swap over flAR + ! \ set address 96 bit6 swap flCR + or! \ set STRT bit in control reg 97 waitFlNB \ wait for completion/timeout 98 bit1 _FPEC flCR + bic! \ clear PER bit in control reg 99 ; 100 101 102 \ **************** 103 \ *S Flash handler 104 \ **************** 105 106 cell buffer: #PrgErrs \ -- addr 107 \ *G Holds error count. 108 109 : ?flError \ status -- 110 \ *G Increment error count if status is not $20. 111 $20 <> if #PrgErrs incr endif ; 112 113 : (EraseFlash) \ dest len -- 114 \ *G Erase the Flash range 115 bounds ?do 116 i flPageErase ?flError 117 /FlashPage +loop 118 ; 119 120 : EraseFlash \ -- 121 \ *G Erase the Flash from start to end. 122 Flashbase /Flash (EraseFlash) ; 123 124 : (ProgFlash) \ src dest len -- 125 \ *G Write len bytes from memory at src to Flash at *\i{dest}. 126 \ ** Note that the Flash is assumed to be erased, and that 127 \ ** no verification is performed except when each byte is 128 \ ** programmed. *\i{Dest} is forced to a 2 byte boundary and 129 \ ** *\i{len} is rounded up to the next 2 byte unit. *\i{Src} 130 \ ** must be on a 2 byte boundary. 131 rot -2 and -rot \ force src 132 swap -2 and swap \ force dest 133 1+ -2 and \ force len 134 bounds ?do \ -- src' 135 dup w@ i flw! ?flError 2+ 136 2 +loop 137 drop 138 ; 139 140 (( 141 : (VerifyFlash) \ src dest len -- 142 \ *G Verify len bytes from memory at src to Flash at dest. 143 3dup s= if 3drop exit endif 144 cr ." Verify failed" 145 0 ?do 146 over c@ over c@ <> if 147 cr 2 spaces .src/dest 148 key? ?leave 149 then 150 i $FFF and 0= if ." ." endif 151 1 + swap 1 + swap 152 1 +loop 153 2drop 154 ; 155 )) 156 157 : toFlash \ src dest -- 158 \ *G Handle 128 bytes at src to go to dest in Flash. 159 #128 (ProgFlash) 160 ; 161 162 163 \ ***************** 164 \ *S Numeric output 165 \ ***************** 166 167 : .nibble \ n -- 168 \ *G Convert a nibble to a hex ASCII digit and display it. 169 $0F and dup 9 > 170 if 7 + then 171 $30 + emit 172 ; 173 174 : .WORD \ w -- 175 \ *G Display w as a 4 digit unsigned hexadecimal number. 176 dup #12 rshift .nibble 177 dup 8 rshift .nibble 178 dup 4 rshift .nibble 179 .nibble 180 ; 181 182 (( 183 : .dword \ x -- 184 \ *G Display x as an 8 digit unsigned hexadecimal number. 185 dup #16 rshift .word .word 186 ; 187 )) 188 189 190 \ *************** 191 \ *S Reprogrammer 192 \ *************** 193 194 l: m1 195 ", Erasing " 196 l: m2 197 ", done " 198 l: m3 199 ", Start Xmodem TX " 200 [defined] REBOOT [if] 201 l: m4 202 ", Press R to reboot, other to reprogram " 203 [else] 204 l: m4 205 ", Reset or press key to reprogram" 206 [then] 207 l: m5 208 ", Errors" 209 l: m6 210 ", No " 211 212 : $. \ c-addr -- 213 \ *G Display counted string. 214 count type 215 ; 216 217 : .PrgErrs 218 \ *G Display number of errors. 219 cr #PrgErrs @ .word bl emit 220 m5 $. 221 ; 222 223 : initReprog \ -- 224 \ *G Get the setup data and calculate the requirements 225 init-ser #50 ms ; 226 227 : ReprogFlash \ -- 228 \ *G Erase and program the Flash. 229 initReprog 230 begin 231 #PrgErrs off 232 cr m1 $. #20 ms 233 EraseFlash 234 m2 $. .PrgErrs #PrgErrs off 235 cr m3 $. $01A emit \ send message and trigger 236 FlashBase /Flash Bin-Up drop 237 .PrgErrs 238 #100 ms 239 cr m4 $. 240 [defined] REBOOT [if] 241 key $DF and dup emit [char] R = 242 until 243 #100 ms 244 reboot 245 [else] 246 key drop 247 again 248 [then] 249 ; 250 251 \ SFP002... 252 l: msg7 253 ", Copy " 254 255 : (CopyFlash) \ src dest len -- 256 \ *G Copy one Flash area to another. The parameters are as for *\fo{CMOVE}. 257 initReprog 258 cr m1 $. #20 ms \ "Erase" message 259 #PrgErrs off 260 2dup (EraseFlash) .PrgErrs #10 ms \ -- src dest len 261 #PrgErrs @ 262 if 3drop exit endif 263 cr msg7 $. \ "Copy" message 264 (ProgFlash) \ -- 265 .PrgErrs #20 ms \ error count 266 ; 267 268 : CopyFlash \ src dest len -- 269 \ *G Copy the flash. The parameters are as for *\fo{CMOVE}. 270 \ ** The process is repeated until there are no errors, and 271 \ ** the system is then rebooted. 272 begin 273 3dup (CopyFlash) #PrgErrs @ 0= 274 until 275 #100 ms reboot 276 ; 277 \ ...SFP002 278 279 280 \ ====== 281 \ *> ### 282 \ ====== 283