annotate src/external_flash.asm @ 610:2ce43f09586e

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