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