Mercurial > public > hwos_code
annotate src/external_flash.asm @ 629:237931377539
3.07 stable release
author | heinrichsweikamp |
---|---|
date | Fri, 29 Nov 2019 18:48:11 +0100 |
parents | cd58f7fc86db |
children | 185ba2f91f59 |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
623 | 3 ; File external_flash.asm combined next generation V3.0.1 |
0 | 4 ; |
5 ; External flash | |
6 ; | |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
8 ;============================================================================= | |
9 ; HISTORY | |
10 ; 2011-08-12 : [mH] creation | |
11 | |
275 | 12 #include "hwos.inc" |
0 | 13 #include "wait.inc" |
14 | |
623 | 15 ext_flash CODE |
604 | 16 |
0 | 17 ;============================================================================= |
18 | |
623 | 19 ; increase flash address by one |
20 ; | |
582 | 21 global incf_ext_flash_address_p1 |
623 | 22 incf_ext_flash_address_p1: |
23 movlw .1 | |
24 ;bra incf_ext_flash_address0 | |
0 | 25 |
623 | 26 |
27 ; increase flash address by value in WREG | |
28 ; | |
0 | 29 global incf_ext_flash_address0 |
30 incf_ext_flash_address0: | |
582 | 31 addwf ext_flash_address+0,F ; increase address |
32 movlw d'0' | |
33 addwfc ext_flash_address+1,F | |
34 addwfc ext_flash_address+2,F | |
0 | 35 |
582 | 36 movlw 0x40 |
37 cpfseq ext_flash_address+2 ; at address 40FFFF? | |
623 | 38 return ; NO - return |
582 | 39 ; clrf ext_flash_address+0 |
40 ; clrf ext_flash_address+1 | |
623 | 41 clrf ext_flash_address+2 ; YES - rollover to 0x000000 |
0 | 42 return |
43 | |
582 | 44 |
623 | 45 ; increase flash address by one with roll-over at 0x200000 to 0x000000 |
46 ; | |
582 | 47 global incf_ext_flash_address0_p1_0x20 |
623 | 48 incf_ext_flash_address0_p1_0x20: ; increase by one |
582 | 49 movlw .1 |
623 | 50 ;bra incf_ext_flash_address0_0x20 |
0 | 51 |
623 | 52 |
53 ; increase flash address by value in WREG with roll-over at 0x200000 to 0x000000 | |
54 ; | |
582 | 55 global incf_ext_flash_address0_0x20 |
623 | 56 incf_ext_flash_address0_0x20: |
582 | 57 addwf ext_flash_address+0,F ; increase address |
58 movlw d'0' | |
59 addwfc ext_flash_address+1,F | |
60 addwfc ext_flash_address+2,F | |
61 movlw 0x20 | |
62 cpfseq ext_flash_address+2 ; at address 0x200000? | |
623 | 63 return ; NO - return |
582 | 64 ; clrf ext_flash_address+0 |
65 ; clrf ext_flash_address+1 | |
623 | 66 clrf ext_flash_address+2 ; YES - rollover to 0x000000 |
582 | 67 return |
0 | 68 |
69 | |
623 | 70 ; decrease flash address by value in WREG |
71 ; | |
0 | 72 global decf_ext_flash_address0 |
73 decf_ext_flash_address0: | |
582 | 74 subwf ext_flash_address+0,F ; decrease address: do a 16-8bits subtract |
75 movlw d'0' | |
76 subwfb ext_flash_address+1,F | |
77 movlw d'0' | |
78 subwfb ext_flash_address+2,F | |
623 | 79 btfss ext_flash_address+2,7 ; under-run? |
80 return ; NO - return | |
81 clrf ext_flash_address+2 ; YES - set to 0x00FFFFF | |
582 | 82 setf ext_flash_address+1 |
83 setf ext_flash_address+0 | |
0 | 84 return |
85 | |
582 | 86 |
623 | 87 global ext_flash_byte_read_plus ; return data read in WREG and SSP2BUF and |
582 | 88 ext_flash_byte_read_plus: ; increase address after read |
0 | 89 rcall ext_flash_byte_read |
582 | 90 movwf ext_flash_rw ; store received data |
91 bra incf_ext_flash_address_p1 ; +1 and return | |
0 | 92 |
582 | 93 |
623 | 94 global ext_flash_byte_read_plus_0x20 ; return data read in WREG and SSP2BUF and |
582 | 95 ext_flash_byte_read_plus_0x20: ; increase address after read with banking at 0x200000 |
0 | 96 rcall ext_flash_byte_read |
582 | 97 movwf ext_flash_rw ; store received data |
623 | 98 bra incf_ext_flash_address0_p1_0x20 ; +1 and return |
0 | 99 |
100 | |
623 | 101 global ext_flash_byte_read ; return data read in WREG |
0 | 102 ext_flash_byte_read: |
623 | 103 movlw 0x03 ; read command |
582 | 104 rcall write_spi |
623 | 105 rcall ext_flash_write_address ; write 24 bit address ext_flash_address:3 via SPI |
106 rcall write_spi ; dummy write to read data into WREG | |
582 | 107 bsf flash_ncs ; CS=1 |
108 movwf ext_flash_rw | |
623 | 109 return ; return data read in WREG and ext_flash_rw |
582 | 110 |
0 | 111 |
623 | 112 ext_flash_write_address: ; write 24 bit address ext_flash_address:3 via SPI |
113 movf ext_flash_address+2,W ; 24 bit address | |
582 | 114 rcall write_spi |
115 movf ext_flash_address+1,W | |
116 rcall write_spi | |
117 movf ext_flash_address+0,W | |
623 | 118 bra write_spi ; and return.... |
582 | 119 |
0 | 120 |
623 | 121 global ext_flash_read_block_start ; return data read in WREG |
0 | 122 ext_flash_read_block_start: |
623 | 123 movlw 0x03 ; read command |
582 | 124 rcall write_spi |
623 | 125 rcall ext_flash_write_address ; write 24 bit address ext_flash_address:3 via SPI |
126 rcall write_spi ; dummy write to read data into WREG | |
127 return ; return data read in WREG | |
0 | 128 |
582 | 129 |
623 | 130 global ext_flash_read_block ; return data read in WREG |
0 | 131 ext_flash_read_block: |
623 | 132 rcall incf_ext_flash_address_p1 ; increase address +1 |
133 bra write_spi1 ; dummy write to read data into WREG and return | |
582 | 134 |
0 | 135 |
623 | 136 global ext_flash_read_block_stop ; return data read in WREG |
0 | 137 ext_flash_read_block_stop: |
582 | 138 bsf flash_ncs ; CS=1 |
623 | 139 return ; nO data in WREG |
582 | 140 |
0 | 141 |
142 global write_byte_ext_flash_plus_header | |
623 | 143 write_byte_ext_flash_plus_header: ; write from WREG and increase address after write |
582 | 144 movwf ext_flash_rw ; store data |
0 | 145 ; test if write is done at first byte of 4kB block |
146 ; if yes -> delete 4kB block first | |
582 | 147 tstfsz ext_flash_address+0 ; at 0x00? |
623 | 148 bra write_byte_ext_flash_plus_h1 ; NO - normal write |
582 | 149 movf ext_flash_address+1,W |
623 | 150 andlw 0x0F ; mask lower nibble |
582 | 151 tstfsz WREG ; at 0x.0? |
623 | 152 bra write_byte_ext_flash_plus_h1 ; NO - normal write |
153 ; YES - at beginning of 4kB block -> erase first! | |
154 rcall ext_flash_erase4kB ; - erases 4kB sector @ext_flash_address:3 | |
582 | 155 write_byte_ext_flash_plus_h1: |
156 movf ext_flash_rw,W | |
623 | 157 rcall ext_flash_byte_write ; write the byte |
582 | 158 bra incf_ext_flash_address_p1 ; +1 and return |
0 | 159 |
160 | |
623 | 161 global write_byte_ext_flash_plus_nocnt ; no increase of ext_flash_dive_counter:3 |
279 | 162 write_byte_ext_flash_plus_nocnt: |
582 | 163 movwf ext_flash_rw ; store data |
164 bra write_byte_ext_flash_plus2 | |
165 | |
279 | 166 |
623 | 167 global write_byte_ext_flash_plus_nodel ; does NOT delete 4kB page when required |
168 write_byte_ext_flash_plus_nodel: ; write from WREG and increase address after write with banking at 0x200000 | |
582 | 169 movwf ext_flash_rw ; store data |
623 | 170 bra write_byte_ext_flash_plus1 ; ignore possible begin of 4kB page, there have been written 0xFF already |
6 | 171 |
623 | 172 global write_byte_ext_flash_plus ; write from WREG and increase address after write with banking at 0x200000 |
0 | 173 write_byte_ext_flash_plus: |
582 | 174 movwf ext_flash_rw ; store data |
175 ; First, increase dive length counter | |
176 incf ext_flash_dive_counter+0,F | |
177 movlw .0 | |
178 addwfc ext_flash_dive_counter+1,F | |
623 | 179 addwfc ext_flash_dive_counter+2,F ; 24 bit ++ |
0 | 180 |
6 | 181 write_byte_ext_flash_plus2: |
0 | 182 ; Now test if write is done at first byte of 4kB block |
183 ; if yes -> delete 4kB block first | |
582 | 184 tstfsz ext_flash_address+0 ; at 0x00? |
623 | 185 bra write_byte_ext_flash_plus1 ; NO - normal write |
582 | 186 movf ext_flash_address+1,W |
623 | 187 andlw 0x0F ; mask lower nibble |
582 | 188 tstfsz WREG ; at 0x.0? |
623 | 189 bra write_byte_ext_flash_plus1 ; NO - normal write |
190 ; YES - at beginning of 4kB block -> erase first! | |
191 rcall ext_flash_erase4kB ; - erases 4kB sector @ext_flash_address:3 | |
582 | 192 write_byte_ext_flash_plus1: |
193 movf ext_flash_rw,W | |
623 | 194 rcall ext_flash_byte_write ; write the byte |
582 | 195 bra incf_ext_flash_address0_p1_0x20 ; +1 and roll over at 0x200000 to 0x000000 and return |
0 | 196 |
197 | |
623 | 198 global ext_flash_byte_write ; write from WREG |
0 | 199 ext_flash_byte_write: |
582 | 200 movwf ext_flash_rw ; store data byte |
201 bsf flash_ncs ; CS=1 | |
202 movlw 0x06 ; WREN command | |
203 rcall write_spi | |
204 bsf flash_ncs ; CS=1 | |
623 | 205 movlw 0x02 ; write (PP, Page-Program) command |
582 | 206 rcall write_spi |
623 | 207 rcall ext_flash_write_address ; write 24 bit address ext_flash_address:3 via SPI |
582 | 208 movf ext_flash_rw,W ; load data byte |
623 | 209 rcall write_spi ; write one byte of data |
210 bra ext_flash_wait_write ; and return... | |
423
ccaaac45b61a
_another_ timing fix for firmware updates (2.07 was not published yet anyway)
heinrichsweikamp
parents:
421
diff
changeset
|
211 |
582 | 212 |
623 | 213 global ext_flash_byte_write_comms ; without wait, ~86us fixed delay due to 115200 Baud |
423
ccaaac45b61a
_another_ timing fix for firmware updates (2.07 was not published yet anyway)
heinrichsweikamp
parents:
421
diff
changeset
|
214 ext_flash_byte_write_comms: |
582 | 215 movwf ext_flash_rw ; store data byte |
216 bsf flash_ncs ; CS=1 | |
217 movlw 0x06 ; WREN command | |
218 rcall write_spi | |
219 bsf flash_ncs ; CS=1 | |
623 | 220 movlw 0x02 ; write PP (Page-Program) command |
582 | 221 rcall write_spi |
623 | 222 rcall ext_flash_write_address ; write 24 bit address ext_flash_address:3 via SPI |
582 | 223 movf ext_flash_rw,W ; load data byte |
623 | 224 rcall write_spi ; write one byte of data |
582 | 225 bsf flash_ncs ; CS=1 |
423
ccaaac45b61a
_another_ timing fix for firmware updates (2.07 was not published yet anyway)
heinrichsweikamp
parents:
421
diff
changeset
|
226 return |
582 | 227 |
0 | 228 |
623 | 229 global ext_flash_disable_protection ; disable write protection |
582 | 230 ext_flash_disable_protection: |
231 ; unlock old memory | |
232 bsf flash_ncs ; CS=1 | |
233 movlw 0x50 ; EWSR command | |
234 rcall write_spi | |
235 bsf flash_ncs ; CS=1 | |
236 movlw 0x01 ; WRSR command | |
237 rcall write_spi | |
623 | 238 movlw b'00000000' ; new status |
582 | 239 rcall write_spi |
240 bsf flash_ncs ; CS=1 | |
241 ; unlock new memory | |
242 movlw 0x06 ; WREN command | |
243 rcall write_spi | |
244 bsf flash_ncs ; CS=1 | |
245 movlw 0x98 ; ULBPR command | |
246 rcall write_spi | |
247 bsf flash_ncs ; CS=1 | |
248 movlw 0x06 ; WREN command | |
249 rcall write_spi | |
250 bsf flash_ncs ; CS=1 | |
251 movlw 0x42 ; WBPR command | |
252 rcall write_spi | |
253 movlw .18 | |
254 movwf lo | |
420 | 255 ext_flash_disable_protection2: |
582 | 256 movlw 0x00 |
257 rcall write_spi | |
258 decfsz lo,F ; 18 bytes with 0x00 | |
259 bra ext_flash_disable_protection2 | |
260 bsf flash_ncs ; CS=1 | |
0 | 261 return |
262 | |
623 | 263 |
0 | 264 global ext_flash_enable_protection |
265 ext_flash_enable_protection: | |
582 | 266 ; lock old memory |
623 | 267 bsf flash_ncs ; CS=1 |
268 movlw 0x50 ; EWSR command | |
582 | 269 rcall write_spi |
623 | 270 bsf flash_ncs ; CS=1 |
271 movlw 0x01 ; WRSR command | |
582 | 272 rcall write_spi |
623 | 273 movlw b'00011100' ; new status (write protection on) |
582 | 274 rcall write_spi |
623 | 275 bsf flash_ncs ; CS=1 |
582 | 276 ; lock new memory |
623 | 277 ; movlw 0x06 ; WREN command |
582 | 278 ; rcall write_spi |
623 | 279 ; bsf flash_ncs ; CS=1 |
280 ; movlw 0x8D ; LBPR command | |
582 | 281 ; rcall write_spi |
623 | 282 ; bsf flash_ncs ; CS=1 |
283 movlw 0x06 ; WREN command | |
582 | 284 rcall write_spi |
623 | 285 bsf flash_ncs ; CS=1 |
286 movlw 0x42 ; WBPR command | |
582 | 287 rcall write_spi |
288 movlw .18 | |
289 movwf lo | |
414 | 290 ext_flash_enable_protection2: |
582 | 291 movlw 0xFF |
292 rcall write_spi | |
623 | 293 decfsz lo,F ; 18 bytes with 0xFF |
582 | 294 bra ext_flash_enable_protection2 |
623 | 295 bsf flash_ncs ; CS=1 |
0 | 296 return |
297 | |
298 | |
623 | 299 global ext_flash_erase4kB ; erases 4kB sector |
0 | 300 ext_flash_erase4kB: |
623 | 301 bsf flash_ncs ; CS=1 |
302 movlw 0x06 ; WREN command | |
582 | 303 rcall write_spi |
623 | 304 bsf flash_ncs ; CS=1 |
305 movlw 0x20 ; sector erase command | |
582 | 306 rcall write_spi |
623 | 307 rcall ext_flash_write_address ; write 24 bit address ext_flash_address:3 via SPI |
308 ; bra ext_flash_wait_write ; wait for write... and return | |
0 | 309 ext_flash_wait_write: |
623 | 310 bsf flash_ncs ; CS=1 |
311 ; WAITMS d'1' ; TBE/TSE=25ms... | |
312 movlw 0x05 ; RDSR command | |
313 rcall write_spi ; read status | |
314 rcall write_spi ; read status into WREG | |
315 bsf flash_ncs ; CS=1 | |
316 btfsc SSP2BUF,0 ; write operation in process? | |
317 bra ext_flash_wait_write ; ES - loop waiting | |
0 | 318 return |
319 | |
582 | 320 |
623 | 321 global ext_flash_erase_logbook ; erases logbook memory (000000h -> 2FFFFFh -> 3MByte -> 3145728 Bytes) |
0 | 322 ext_flash_erase_logbook: |
623 | 323 bsf flash_ncs ; CS=1 |
582 | 324 clrf ext_flash_address+0 |
325 clrf ext_flash_address+1 | |
326 clrf ext_flash_address+2 | |
0 | 327 |
628 | 328 clrf ext_flash_rw ; 256 * 12 kB = 3145728 bytes |
0 | 329 ext_flash_erase_logbook_loop: |
623 | 330 rcall ext_flash_erase4kB ; 4kB |
331 rcall ext_flash_add_4kB ; increase ext_flash_address:3 by 4kB | |
332 rcall ext_flash_erase4kB ; 4kB | |
333 rcall ext_flash_add_4kB ; increase ext_flash_address:3 by 4kB | |
334 rcall ext_flash_erase4kB ; 4kB | |
335 rcall ext_flash_add_4kB ; increase ext_flash_address:3 by 4kB | |
336 decfsz ext_flash_rw,F ; decrement loop counter, done? | |
337 bra ext_flash_erase_logbook_loop ; NO - loop | |
338 return ; YES - done | |
0 | 339 |
414 | 340 ext_flash_add_4kB: |
582 | 341 movlw 0x10 |
342 addwf ext_flash_address+1,F | |
343 movlw d'0' | |
344 addwfc ext_flash_address+2,F | |
345 return | |
414 | 346 |
0 | 347 |
623 | 348 write_spi: ; with data in WREG... |
349 bcf flash_ncs ; CS | |
0 | 350 global write_spi1 |
623 | 351 write_spi1: ; with data in WREG... |
352 bcf SSP2STAT,WCOL ; clear flag | |
353 movwf SSP2BUF ; write to buffer | |
354 btfsc SSP2STAT,WCOL ; was buffer full? | |
355 bra write_spi1 ; YES - try again | |
356 write_spi2: ; wait for write command | |
357 btfss SSP2STAT, BF ; buffer full? | |
358 bra write_spi2 ; NO - loop waiting | |
359 movf SSP2BUF,W ; YES - copy RX data to WREG | |
360 return ; - return with RX data in WREG and SSP2BUF | |
0 | 361 |
582 | 362 END |