0
|
1 ;=============================================================================
|
|
2 ;
|
|
3 ; File external_flash.asm
|
|
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
|
|
12 #include "ostc3.inc"
|
|
13 #include "wait.inc"
|
|
14
|
|
15 basic CODE
|
|
16 ;=============================================================================
|
|
17
|
|
18 global incf_ext_flash_address_p1
|
|
19 incf_ext_flash_address_p1: ; Increase by one
|
|
20 movlw .1
|
|
21
|
|
22 global incf_ext_flash_address0
|
|
23 incf_ext_flash_address0:
|
|
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
|
|
28
|
|
29 movlw 0x40
|
|
30 cpfseq ext_flash_address+2 ; at address 40FFFF?
|
|
31 return ; No, return
|
167
|
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
|
|
37 global incf_ext_flash_address0_p1_0x20
|
|
38 incf_ext_flash_address0_p1_0x20: ; Increase by one
|
|
39 movlw .1
|
|
40
|
|
41 global incf_ext_flash_address0_0x20
|
|
42 incf_ext_flash_address0_0x20: ; with roll-over at 0x200000 to 0x000000
|
|
43 addwf ext_flash_address+0,F ; increase address
|
|
44 movlw d'0'
|
|
45 addwfc ext_flash_address+1,F
|
|
46 addwfc ext_flash_address+2,F
|
|
47
|
|
48 movlw 0x20
|
|
49 cpfseq ext_flash_address+2 ; at address 0x200000?
|
|
50 return ; No, return
|
167
|
51 ; clrf ext_flash_address+0
|
|
52 ; clrf ext_flash_address+1
|
|
53 clrf ext_flash_address+2 ; Yes, rollover to 0x000000
|
0
|
54 return
|
|
55
|
|
56 global decf_ext_flash_address0
|
|
57 decf_ext_flash_address0:
|
|
58 subwf ext_flash_address+0,F ; decrease address: do a 16-8bits substract.
|
|
59 movlw d'0'
|
|
60 subwfb ext_flash_address+1,F
|
|
61 movlw d'0'
|
|
62 subwfb ext_flash_address+2,F
|
|
63
|
|
64 btfss ext_flash_address+2,7 ; Rollover to 0xFFFFFF?
|
|
65 return ; No, return
|
|
66 clrf ext_flash_address+2 ; Set to 0x00FFFFF
|
|
67 setf ext_flash_address+1
|
|
68 setf ext_flash_address+0
|
|
69 return
|
|
70
|
|
71 global ext_flash_power_down
|
|
72 ext_flash_power_down:
|
|
73 movlw 0x04 ; Write disable
|
|
74 rcall write_spi
|
|
75 bsf flash_ncs ; CS=1
|
|
76 return
|
|
77
|
|
78 global ext_flash_byte_read_plus ; Return data read in WREG and SSP2BUF and
|
|
79 ext_flash_byte_read_plus: ; increase address after read
|
|
80 rcall ext_flash_byte_read
|
|
81 movwf temp1 ; store received data
|
|
82 bra incf_ext_flash_address_p1 ; +1 and return
|
|
83
|
|
84 global ext_flash_byte_read_plus_0x20; Return data read in WREG and SSP2BUF and
|
|
85 ext_flash_byte_read_plus_0x20: ; increase address after read with banking at 0x200000
|
|
86 rcall ext_flash_byte_read
|
|
87 movwf temp1 ; store received data
|
|
88 bra incf_ext_flash_address0_p1_0x20 ;+1 and return
|
|
89
|
|
90
|
|
91 global ext_flash_byte_read ; Return data read in WREG
|
|
92 ext_flash_byte_read:
|
|
93 movlw 0x03 ; Read command
|
|
94 rcall write_spi
|
|
95 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI
|
|
96 rcall write_spi ; Dummy write to read data into WREG
|
|
97 bsf flash_ncs ; CS=1
|
|
98 movwf temp1
|
|
99 return ; Return data read in WREG and temp1
|
|
100
|
|
101 ext_flash_write_address: ; Write 24bit address ext_flash_address:3 via SPI
|
|
102 movf ext_flash_address+2,W ; 24Bit Address
|
|
103 rcall write_spi
|
|
104 movf ext_flash_address+1,W
|
|
105 rcall write_spi
|
|
106 movf ext_flash_address+0,W
|
|
107 bra write_spi ; And return....
|
|
108
|
|
109 global ext_flash_read_block_start ; Return data read in WREG
|
|
110 ext_flash_read_block_start:
|
|
111 movlw 0x03 ; Read command
|
|
112 rcall write_spi
|
|
113 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI
|
|
114 rcall write_spi ; Dummy write to read data into WREG
|
|
115 return ; Return data read in WREG
|
|
116
|
|
117 global ext_flash_read_block ; Return data read in WREG
|
|
118 ext_flash_read_block:
|
|
119 rcall incf_ext_flash_address_p1 ; Increase address +1
|
|
120 bra write_spi1 ; Dummy write to read data into WREG and return
|
|
121
|
|
122 global ext_flash_read_block_stop ; Return data read in WREG
|
|
123 ext_flash_read_block_stop:
|
|
124 bsf flash_ncs ; CS=1
|
|
125 return ; NO data in WREG
|
|
126
|
|
127 global write_byte_ext_flash_plus_header
|
|
128 write_byte_ext_flash_plus_header: ; Write from WREG and increase address after write
|
|
129 movwf temp1 ; store data
|
|
130 ; test if write is done at first byte of 4kB block
|
|
131 ; if yes -> delete 4kB block first
|
|
132 tstfsz ext_flash_address+0 ; at 0x00?
|
|
133 bra write_byte_ext_flash_plus_h1 ; No, normal Write
|
|
134
|
|
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
|
|
140 ; At beginning of 4kB block -> rease first!
|
|
141 rcall ext_flash_erase4kB ; Erases 4kB sector @ext_flash_address:3
|
|
142 write_byte_ext_flash_plus_h1:
|
|
143 movf temp1,W
|
|
144 rcall ext_flash_byte_write ; Write the byte
|
|
145 bra incf_ext_flash_address_p1 ; +1 and return
|
|
146
|
6
|
147 global write_byte_ext_flash_plus_nocnt
|
|
148 write_byte_ext_flash_plus_nocnt: ; Write from WREG and increase address after write with banking at 0x200000
|
|
149 movwf temp1 ; store data
|
|
150 bra write_byte_ext_flash_plus2
|
|
151
|
0
|
152 global write_byte_ext_flash_plus ; Write from WREG and increase address after write with banking at 0x200000
|
|
153 write_byte_ext_flash_plus:
|
|
154 movwf temp1 ; store data
|
|
155
|
|
156 ; First, increase dive length counter
|
|
157 incf ext_flash_dive_counter+0,F
|
|
158 movlw .0
|
|
159 addwfc ext_flash_dive_counter+1,F
|
|
160 addwfc ext_flash_dive_counter+2,F ; 24bit++
|
|
161
|
6
|
162 write_byte_ext_flash_plus2:
|
0
|
163 ; Now test if write is done at first byte of 4kB block
|
|
164 ; if yes -> delete 4kB block first
|
|
165 tstfsz ext_flash_address+0 ; at 0x00?
|
|
166 bra write_byte_ext_flash_plus1 ; No, normal Write
|
|
167
|
|
168 movf ext_flash_address+1,W
|
|
169 andlw 0x0F ; Mask lower nibble
|
|
170 tstfsz WREG ; at 0x.0?
|
|
171 bra write_byte_ext_flash_plus1 ; No, normal Write
|
|
172
|
|
173 ; At beginning of 4kB block -> erase first!
|
|
174 rcall ext_flash_erase4kB ; Erases 4kB sector @ext_flash_address:3
|
|
175 write_byte_ext_flash_plus1:
|
|
176 movf temp1,W
|
|
177 rcall ext_flash_byte_write ; Write the byte
|
|
178 bra incf_ext_flash_address0_p1_0x20 ; +1 and roll over at 0x200000 to 0x000000 and return
|
|
179
|
|
180 global ext_flash_byte_write ; Write from WREG
|
|
181 ext_flash_byte_write:
|
|
182 movwf temp1 ; store data byte
|
|
183 bsf flash_ncs ; CS=1
|
|
184 movlw 0x06 ; WREN command
|
|
185 rcall write_spi
|
|
186 bsf flash_ncs ; CS=1
|
|
187 movlw 0x02 ; Write command
|
|
188 rcall write_spi
|
|
189 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI
|
|
190 movf temp1,W ; load data byte
|
|
191 rcall write_spi ; write one byte of data!
|
|
192 bsf flash_ncs ; CS=1
|
|
193 return
|
|
194
|
|
195 global ext_flash_disable_protection ; Disable write protection
|
|
196 ext_flash_disable_protection:
|
|
197 bsf flash_ncs ; CS=1
|
|
198 movlw 0x50 ; EWSR command
|
|
199 rcall write_spi
|
|
200 bsf flash_ncs ; CS=1
|
|
201
|
|
202 movlw 0x01 ; WRSR command
|
|
203 rcall write_spi
|
|
204 movlw b'00000000' ; New status
|
|
205 rcall write_spi
|
|
206 bsf flash_ncs ; CS=1
|
|
207 return
|
|
208
|
|
209 global ext_flash_enable_protection
|
|
210 ext_flash_enable_protection:
|
|
211 bsf flash_ncs ; CS=1
|
|
212 movlw 0x50 ; EWSR command
|
|
213 rcall write_spi
|
|
214 bsf flash_ncs ; CS=1
|
|
215
|
|
216 movlw 0x01 ; WRSR command
|
|
217 rcall write_spi
|
|
218 movlw b'00011100' ; New status (Write protect on)
|
|
219 rcall write_spi
|
|
220 bsf flash_ncs ; CS=1
|
|
221 return
|
|
222
|
|
223
|
|
224 global ext_flash_erase4kB ; Erases 4kB sector
|
|
225 ext_flash_erase4kB:
|
|
226 bsf flash_ncs ; CS=1
|
|
227 movlw 0x06 ; WREN command
|
|
228 rcall write_spi
|
|
229 bsf flash_ncs ; CS=1
|
|
230 movlw 0x20 ; Sector erase command
|
|
231 rcall write_spi
|
|
232 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI
|
|
233 bsf flash_ncs ; CS=1
|
|
234 ; bra ext_flash_wait_write ; Wait for write... and return
|
|
235 ext_flash_wait_write:
|
|
236 WAITMS d'1' ; TBE=25ms...
|
|
237 movlw 0x05 ; RDSR command
|
|
238 rcall write_spi ; Read status
|
|
239 rcall write_spi ; Read status into WREG
|
|
240 bsf flash_ncs ; CS=1
|
|
241 btfsc SSP2BUF,0 ; Write operation in process?
|
|
242 bra ext_flash_wait_write ; Yes, wait more..
|
|
243 return
|
|
244
|
|
245 global ext_flash_erase_logbook ; erases logbook memory (000000h -> 2FFFFFh -> 3MByte)
|
|
246 ext_flash_erase_logbook:
|
|
247 bsf flash_ncs ; CS=1
|
|
248 clrf ext_flash_address+0
|
|
249 clrf ext_flash_address+1
|
|
250 clrf ext_flash_address+2
|
|
251
|
|
252 movlw d'48'
|
|
253 movwf temp1 ; 48*64kB=917504 Bytes
|
|
254 ext_flash_erase_logbook_loop:
|
|
255 movlw 0x06 ; WREN command
|
|
256 rcall write_spi
|
|
257 bsf flash_ncs ; CS=1
|
|
258 movlw 0xD8 ; 64kB erase command
|
|
259 rcall write_spi
|
|
260 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI
|
|
261 bsf flash_ncs ; CS=1
|
|
262 rcall ext_flash_wait_write ; Wait for write...
|
|
263
|
|
264 incf ext_flash_address+2,F ; 64kB ahead
|
|
265 decfsz temp1,F
|
|
266 bra ext_flash_erase_logbook_loop
|
|
267 return
|
|
268
|
|
269
|
|
270 write_spi: ; With data in WREG...
|
|
271 bcf flash_ncs ; CS
|
|
272 global write_spi1
|
|
273 write_spi1: ; With data in WREG...
|
|
274 bcf SSP2STAT,WCOL ; Clear flag
|
|
275 movwf SSP2BUF ; Write to buffer
|
|
276 btfsc SSP2STAT,WCOL ; Was buffer full?
|
|
277 bra write_spi1 ; Yes, try again
|
|
278
|
|
279 write_spi2: ; Wait for write command
|
|
280 btfss SSP2STAT, BF ; Buffer full?
|
|
281 bra write_spi2 ; No, wait.
|
|
282 movf SSP2BUF,W
|
|
283 return ; Returns RX data in WREG and SSP2BUF
|
|
284
|
|
285 END |