comparison src/external_flash.asm @ 623:c40025d8e750

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