annotate src/external_flash.asm @ 573:cf4395595d9b

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