comparison src/external_flash.asm @ 582:b455b31ce022

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