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