Mercurial > public > hwos_code
comparison src/external_flash.asm @ 0:11d4fc797f74
init
| author | heinrichsweikamp |
|---|---|
| date | Wed, 24 Apr 2013 19:22:45 +0200 |
| parents | |
| children | 13cda523891f |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:11d4fc797f74 |
|---|---|
| 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 | |
| 32 clrf ext_flash_address+0 ; Yes, rollover to 0x000000 | |
| 33 clrf ext_flash_address+1 | |
| 34 clrf ext_flash_address+2 | |
| 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 | |
| 51 clrf ext_flash_address+0 ; Yes, rollover to 0x000000 | |
| 52 clrf ext_flash_address+1 | |
| 53 clrf ext_flash_address+2 | |
| 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 | |
| 147 global write_byte_ext_flash_plus ; Write from WREG and increase address after write with banking at 0x200000 | |
| 148 write_byte_ext_flash_plus: | |
| 149 movwf temp1 ; store data | |
| 150 | |
| 151 ; First, increase dive length counter | |
| 152 incf ext_flash_dive_counter+0,F | |
| 153 movlw .0 | |
| 154 addwfc ext_flash_dive_counter+1,F | |
| 155 addwfc ext_flash_dive_counter+2,F ; 24bit++ | |
| 156 | |
| 157 ; Now test if write is done at first byte of 4kB block | |
| 158 ; if yes -> delete 4kB block first | |
| 159 tstfsz ext_flash_address+0 ; at 0x00? | |
| 160 bra write_byte_ext_flash_plus1 ; No, normal Write | |
| 161 | |
| 162 movf ext_flash_address+1,W | |
| 163 andlw 0x0F ; Mask lower nibble | |
| 164 tstfsz WREG ; at 0x.0? | |
| 165 bra write_byte_ext_flash_plus1 ; No, normal Write | |
| 166 | |
| 167 ; At beginning of 4kB block -> erase first! | |
| 168 rcall ext_flash_erase4kB ; Erases 4kB sector @ext_flash_address:3 | |
| 169 write_byte_ext_flash_plus1: | |
| 170 movf temp1,W | |
| 171 rcall ext_flash_byte_write ; Write the byte | |
| 172 bra incf_ext_flash_address0_p1_0x20 ; +1 and roll over at 0x200000 to 0x000000 and return | |
| 173 | |
| 174 global ext_flash_byte_write ; Write from WREG | |
| 175 ext_flash_byte_write: | |
| 176 movwf temp1 ; store data byte | |
| 177 bsf flash_ncs ; CS=1 | |
| 178 movlw 0x06 ; WREN command | |
| 179 rcall write_spi | |
| 180 bsf flash_ncs ; CS=1 | |
| 181 movlw 0x02 ; Write command | |
| 182 rcall write_spi | |
| 183 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI | |
| 184 movf temp1,W ; load data byte | |
| 185 rcall write_spi ; write one byte of data! | |
| 186 bsf flash_ncs ; CS=1 | |
| 187 return | |
| 188 | |
| 189 global ext_flash_disable_protection ; Disable write protection | |
| 190 ext_flash_disable_protection: | |
| 191 bsf flash_ncs ; CS=1 | |
| 192 movlw 0x50 ; EWSR command | |
| 193 rcall write_spi | |
| 194 bsf flash_ncs ; CS=1 | |
| 195 | |
| 196 movlw 0x01 ; WRSR command | |
| 197 rcall write_spi | |
| 198 movlw b'00000000' ; New status | |
| 199 rcall write_spi | |
| 200 bsf flash_ncs ; CS=1 | |
| 201 return | |
| 202 | |
| 203 global ext_flash_enable_protection | |
| 204 ext_flash_enable_protection: | |
| 205 bsf flash_ncs ; CS=1 | |
| 206 movlw 0x50 ; EWSR command | |
| 207 rcall write_spi | |
| 208 bsf flash_ncs ; CS=1 | |
| 209 | |
| 210 movlw 0x01 ; WRSR command | |
| 211 rcall write_spi | |
| 212 movlw b'00011100' ; New status (Write protect on) | |
| 213 rcall write_spi | |
| 214 bsf flash_ncs ; CS=1 | |
| 215 return | |
| 216 | |
| 217 | |
| 218 global ext_flash_erase4kB ; Erases 4kB sector | |
| 219 ext_flash_erase4kB: | |
| 220 bsf flash_ncs ; CS=1 | |
| 221 movlw 0x06 ; WREN command | |
| 222 rcall write_spi | |
| 223 bsf flash_ncs ; CS=1 | |
| 224 movlw 0x20 ; Sector erase command | |
| 225 rcall write_spi | |
| 226 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI | |
| 227 bsf flash_ncs ; CS=1 | |
| 228 ; bra ext_flash_wait_write ; Wait for write... and return | |
| 229 ext_flash_wait_write: | |
| 230 WAITMS d'1' ; TBE=25ms... | |
| 231 movlw 0x05 ; RDSR command | |
| 232 rcall write_spi ; Read status | |
| 233 rcall write_spi ; Read status into WREG | |
| 234 bsf flash_ncs ; CS=1 | |
| 235 btfsc SSP2BUF,0 ; Write operation in process? | |
| 236 bra ext_flash_wait_write ; Yes, wait more.. | |
| 237 return | |
| 238 | |
| 239 global ext_flash_erase_logbook ; erases logbook memory (000000h -> 2FFFFFh -> 3MByte) | |
| 240 ext_flash_erase_logbook: | |
| 241 bsf flash_ncs ; CS=1 | |
| 242 clrf ext_flash_address+0 | |
| 243 clrf ext_flash_address+1 | |
| 244 clrf ext_flash_address+2 | |
| 245 | |
| 246 movlw d'48' | |
| 247 movwf temp1 ; 48*64kB=917504 Bytes | |
| 248 ext_flash_erase_logbook_loop: | |
| 249 movlw 0x06 ; WREN command | |
| 250 rcall write_spi | |
| 251 bsf flash_ncs ; CS=1 | |
| 252 movlw 0xD8 ; 64kB erase command | |
| 253 rcall write_spi | |
| 254 rcall ext_flash_write_address ; Write 24bit address ext_flash_address:3 via SPI | |
| 255 bsf flash_ncs ; CS=1 | |
| 256 rcall ext_flash_wait_write ; Wait for write... | |
| 257 | |
| 258 incf ext_flash_address+2,F ; 64kB ahead | |
| 259 decfsz temp1,F | |
| 260 bra ext_flash_erase_logbook_loop | |
| 261 return | |
| 262 | |
| 263 | |
| 264 write_spi: ; With data in WREG... | |
| 265 bcf flash_ncs ; CS | |
| 266 global write_spi1 | |
| 267 write_spi1: ; With data in WREG... | |
| 268 bcf SSP2STAT,WCOL ; Clear flag | |
| 269 movwf SSP2BUF ; Write to buffer | |
| 270 btfsc SSP2STAT,WCOL ; Was buffer full? | |
| 271 bra write_spi1 ; Yes, try again | |
| 272 | |
| 273 write_spi2: ; Wait for write command | |
| 274 btfss SSP2STAT, BF ; Buffer full? | |
| 275 bra write_spi2 ; No, wait. | |
| 276 movf SSP2BUF,W | |
| 277 return ; Returns RX data in WREG and SSP2BUF | |
| 278 | |
| 279 END |
