Mercurial > public > mk2
comparison code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 0:96a35aeda5f2
Initial setup
| author | heinrichsweikamp |
|---|---|
| date | Tue, 12 Jan 2010 15:05:59 +0100 |
| parents | |
| children | 73014f788032 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:96a35aeda5f2 |
|---|---|
| 1 | |
| 2 ; OSTC - diving computer code | |
| 3 ; Copyright (C) 2008 HeinrichsWeikamp GbR | |
| 4 ; This program is free software: you can redistribute it and/or modify | |
| 5 ; it under the terms of the GNU General Public License as published by | |
| 6 ; the Free Software Foundation, either version 3 of the License, or | |
| 7 ; (at your option) any later version. | |
| 8 ; This program is distributed in the hope that it will be useful, | |
| 9 ; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 ; GNU General Public License for more details. | |
| 12 ; You should have received a copy of the GNU General Public License | |
| 13 ; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 14 ; provides routines for external EEPROM via I2C | |
| 15 ; written by: Matthias Heinrichs, info@heinrichsweikamp.com | |
| 16 ; written: 10/30/05 | |
| 17 ; last updated: 08/21/06 | |
| 18 ; known bugs: | |
| 19 ; ToDo: use 2nd 32KB from external EEPROM for something | |
| 20 | |
| 21 incf_eeprom_address macro ext_ee_temp1 ; Will increase eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes | |
| 22 movlw ext_ee_temp1 ; care of bank switching at 0x8000 | |
| 23 call incf_eeprom_address0 | |
| 24 endm | |
| 25 | |
| 26 incf_eeprom_address0: | |
| 27 movwf ext_ee_temp1 | |
| 28 incf_eeprom_address1: | |
| 29 movlw d'1' ; increase address | |
| 30 addwf eeprom_address+0,F | |
| 31 movlw d'0' | |
| 32 addwfc eeprom_address+1,F | |
| 33 btfss eeprom_address+1,7 ; at address 8000? | |
| 34 bra incf_eeprom_address2 ; No, continue | |
| 35 | |
| 36 ; Yes, clear eeprom_address:2 | |
| 37 clrf eeprom_address+0 ; Clear eeprom address | |
| 38 clrf eeprom_address+1 | |
| 39 | |
| 40 incf_eeprom_address2: | |
| 41 decfsz ext_ee_temp1,F ; All done? | |
| 42 bra incf_eeprom_address1 ; No, continue | |
| 43 return ; Done. | |
| 44 | |
| 45 decf_eeprom_address macro ext_ee_temp1 ; Will decrease eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes | |
| 46 movlw ext_ee_temp1 ; care of bank switching at 0x8000 | |
| 47 call decf_eeprom_address0 | |
| 48 endm | |
| 49 | |
| 50 decf_eeprom_address0: | |
| 51 movwf ext_ee_temp1 | |
| 52 decf_eeprom_address1: | |
| 53 movlw d'1' ; decrease address | |
| 54 subwf eeprom_address+0,F | |
| 55 movlw d'0' | |
| 56 subwfb eeprom_address+1,F | |
| 57 | |
| 58 btfss eeprom_address+1,7 ; at address 8000? | |
| 59 bra decf_eeprom_address2 ; No, continue | |
| 60 | |
| 61 movlw b'01111111' ; yes, reset highbyte | |
| 62 movwf eeprom_address+1 | |
| 63 | |
| 64 decf_eeprom_address2: | |
| 65 decfsz ext_ee_temp1,F ; All done? | |
| 66 bra decf_eeprom_address1 ; No, continue | |
| 67 return ; Done. | |
| 68 | |
| 69 | |
| 70 write_external_eeprom: ; data in WREG | |
| 71 ; increase address eeprom_address+0:eeprom_address+1 after write | |
| 72 ; with banking after 7FFF | |
| 73 rcall I2CWRITE ; writes WREG into EEPROM@eeprom_address | |
| 74 movlw d'1' ; increase address | |
| 75 addwf eeprom_address+0,F | |
| 76 movlw d'0' | |
| 77 addwfc eeprom_address+1,F | |
| 78 bcf eeprom_overflow | |
| 79 btfss eeprom_address+1,7 ; at address 8000? | |
| 80 return ; no, return | |
| 81 | |
| 82 clrf eeprom_address+0 ; Clear eeprom address | |
| 83 clrf eeprom_address+1 | |
| 84 bsf eeprom_overflow ; and set overflow bit | |
| 85 return | |
| 86 write_external_eeprom_block: ; Writes a block of 64Byte (one page in external EEPROM without stop condition | |
| 87 btfsc eeprom_blockwrite ; Blockwrite continue? | |
| 88 rcall I2CWRITE_BLOCK2 | |
| 89 btfss eeprom_blockwrite ; Blockwrite start? | |
| 90 rcall I2CWRITE_BLOCK | |
| 91 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes! | |
| 92 | |
| 93 movlw d'0' ; increase address | |
| 94 incf eeprom_address+0,F | |
| 95 addwfc eeprom_address+1,F | |
| 96 bcf eeprom_overflow | |
| 97 | |
| 98 btfss eeprom_address+1,7 ; at address 8000 | |
| 99 return ; no, return | |
| 100 | |
| 101 clrf eeprom_address+0 ; Clear eeprom address | |
| 102 clrf eeprom_address+1 | |
| 103 bsf eeprom_overflow ; and set overflow bit | |
| 104 return | |
| 105 I2CWRITE_BLOCK: | |
| 106 movwf ext_ee_temp1 ; Data byte in WREG | |
| 107 bsf SSPCON2,SEN ; Start condition | |
| 108 rcall WaitMSSP | |
| 109 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
| 110 movwf SSPBUF ; control byte | |
| 111 rcall WaitMSSP | |
| 112 rcall I2C_WaitforACK | |
| 113 movff eeprom_address+1,SSPBUF ; High Address byte | |
| 114 rcall WaitMSSP | |
| 115 rcall I2C_WaitforACK | |
| 116 movff eeprom_address+0,SSPBUF ; Low Address byte | |
| 117 rcall WaitMSSP | |
| 118 rcall I2C_WaitforACK | |
| 119 I2CWRITE_BLOCK2: | |
| 120 movff ext_ee_temp1, SSPBUF ; Data Byte | |
| 121 rcall WaitMSSP | |
| 122 rcall I2C_WaitforACK | |
| 123 return | |
| 124 | |
| 125 | |
| 126 get_free_EEPROM_location: ; Searches 0xFD, 0xFD, 0xFE and sets Pointer to 0xFE | |
| 127 clrf ext_ee_temp1 ; low address counter | |
| 128 clrf ext_ee_temp2 ; high address counter | |
| 129 bcf second_FD ; clear flags | |
| 130 bcf first_FD | |
| 131 get_free_EEPROM_location3: | |
| 132 bsf SSPCON2, PEN ; Stop condition | |
| 133 rcall WaitMSSP | |
| 134 bsf SSPCON2,SEN ; Start condition | |
| 135 rcall WaitMSSP | |
| 136 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
| 137 movwf SSPBUF ; control byte | |
| 138 rcall WaitMSSP | |
| 139 btfsc SSPCON2,ACKSTAT | |
| 140 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry! | |
| 141 | |
| 142 movff ext_ee_temp2,SSPBUF ; High Address byte | |
| 143 rcall WaitMSSP | |
| 144 rcall I2C_WaitforACK | |
| 145 movff ext_ee_temp1,SSPBUF ; Low Address byte | |
| 146 rcall WaitMSSP | |
| 147 rcall I2C_WaitforACK | |
| 148 | |
| 149 bsf SSPCON2,RSEN ; Start condition | |
| 150 rcall WaitMSSP | |
| 151 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
| 152 movwf SSPBUF ; control byte | |
| 153 rcall WaitMSSP | |
| 154 rcall I2C_WaitforACK | |
| 155 | |
| 156 get_free_EEPROM_location2: | |
| 157 bsf SSPCON2, RCEN ; Enable recieve mode | |
| 158 rcall WaitMSSP | |
| 159 btfsc first_FD | |
| 160 bra test_2nd_FD | |
| 161 bsf first_FD ; found first 0xFD? | |
| 162 movlw 0xFD | |
| 163 cpfseq SSPBUF | |
| 164 bcf first_FD ; No | |
| 165 bra get_free_EEPROM_location2c | |
| 166 | |
| 167 test_2nd_FD: | |
| 168 btfsc second_FD | |
| 169 bra test_FE | |
| 170 bsf second_FD ; found second 0xFD? | |
| 171 movlw 0xFD | |
| 172 cpfseq SSPBUF | |
| 173 bra get_free_EEPROM_location2b ;No, clear both flags | |
| 174 bra get_free_EEPROM_location2c | |
| 175 test_FE: | |
| 176 movlw 0xFE ; found the final 0xFE? | |
| 177 cpfseq SSPBUF | |
| 178 bra get_free_EEPROM_location2b ;No, clear both flags | |
| 179 movff ext_ee_temp1,eeprom_address+0 ;Yes, copy ext_ee_temp1->eeprom_address+0 and | |
| 180 movff ext_ee_temp2,eeprom_address+1 ;ext_ee_temp2->eeprom_address+1 | |
| 181 bra get_free_EEPROM_location4 ; Done. | |
| 182 | |
| 183 get_free_EEPROM_location2b: | |
| 184 bcf second_FD ; clear both flags! | |
| 185 bcf first_FD | |
| 186 get_free_EEPROM_location2c: | |
| 187 movlw d'1' ; and increase search address | |
| 188 addwf ext_ee_temp1,F | |
| 189 movlw d'0' | |
| 190 addwfc ext_ee_temp2,F | |
| 191 | |
| 192 btfsc ext_ee_temp2,7 ; 8000 reached? | |
| 193 bra get_free_EEPROM_location3b ; yes | |
| 194 | |
| 195 bsf SSPCON2, ACKEN ; no, send Ack | |
| 196 rcall WaitMSSP | |
| 197 bra get_free_EEPROM_location2 ; and continue search | |
| 198 get_free_EEPROM_location3b: | |
| 199 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0 | |
| 200 clrf eeprom_address+1 | |
| 201 get_free_EEPROM_location4: | |
| 202 bsf SSPCON2, PEN ; Stop | |
| 203 rcall WaitMSSP | |
| 204 | |
| 205 bcf second_FD ; clear flags | |
| 206 bcf first_FD | |
| 207 return ; return | |
| 208 | |
| 209 | |
| 210 I2CREAD: | |
| 211 bsf SSPCON2, PEN ; Stop | |
| 212 rcall WaitMSSP | |
| 213 bsf SSPCON2,SEN ; Start condition | |
| 214 rcall WaitMSSP | |
| 215 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
| 216 movwf SSPBUF ; control byte | |
| 217 rcall WaitMSSP | |
| 218 btfsc SSPCON2,ACKSTAT | |
| 219 bra I2CREAD ; EEPROM NOT acknowledged, retry! | |
| 220 movff eeprom_address+1,SSPBUF ; High Address byte | |
| 221 rcall WaitMSSP | |
| 222 rcall I2C_WaitforACK | |
| 223 movff eeprom_address+0,SSPBUF ; Low Address byte | |
| 224 rcall WaitMSSP | |
| 225 rcall I2C_WaitforACK | |
| 226 | |
| 227 bsf SSPCON2,RSEN ; Start condition | |
| 228 rcall WaitMSSP | |
| 229 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ | |
| 230 movwf SSPBUF ; control byte | |
| 231 rcall WaitMSSP | |
| 232 rcall I2C_WaitforACK | |
| 233 | |
| 234 bsf SSPCON2, RCEN ; Enable recieve mode | |
| 235 rcall WaitMSSP | |
| 236 movf SSPBUF,W ; copy read byte into WREG | |
| 237 bsf SSPCON2, PEN ; Stop | |
| 238 rcall WaitMSSP | |
| 239 return | |
| 240 | |
| 241 I2CREAD2: ; same as I2CREAD but with automatic address increase | |
| 242 rcall I2CREAD | |
| 243 movlw d'1' | |
| 244 addwf eeprom_address+0,F | |
| 245 movlw d'0' | |
| 246 addwfc eeprom_address+1,F | |
| 247 bcf eeprom_overflow | |
| 248 btfss eeprom_address+1,7 ; at 0x8000? | |
| 249 return ; no, return | |
| 250 | |
| 251 clrf eeprom_address+0 ; Yes, clear address | |
| 252 clrf eeprom_address+1 | |
| 253 bsf eeprom_overflow ; and set overflow bit | |
| 254 return | |
| 255 | |
| 256 I2CWRITE: | |
| 257 movwf ext_ee_temp1 ; Data byte | |
| 258 bsf SSPCON2,SEN ; Start condition | |
| 259 rcall WaitMSSP | |
| 260 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ | |
| 261 movwf SSPBUF ; control byte | |
| 262 rcall WaitMSSP | |
| 263 rcall I2C_WaitforACK | |
| 264 movff eeprom_address+1,SSPBUF ; High Address byte | |
| 265 rcall WaitMSSP | |
| 266 rcall I2C_WaitforACK | |
| 267 movff eeprom_address+0,SSPBUF ; Low Address byte | |
| 268 rcall WaitMSSP | |
| 269 rcall I2C_WaitforACK | |
| 270 movff ext_ee_temp1, SSPBUF ; Data Byte | |
| 271 rcall WaitMSSP | |
| 272 rcall I2C_WaitforACK | |
| 273 bsf SSPCON2,PEN ; Stop condition | |
| 274 rcall WaitMSSP | |
| 275 WAITMS d'6' ; Write delay | |
| 276 return | |
| 277 | |
| 278 I2C_WaitforACK: | |
| 279 btfsc SSPCON2,ACKSTAT ; checks for ACK bit from slave | |
| 280 rcall I2CFail | |
| 281 return | |
| 282 | |
| 283 I2CFail: | |
| 284 ostc_debug 'M' ; Sends debug-information to screen if debugmode active | |
| 285 call set_LEDy | |
| 286 rcall I2CReset ; I2C Reset | |
| 287 bcf PIR1,SSPIF | |
| 288 clrf i2c_temp | |
| 289 return | |
| 290 | |
| 291 WaitMSSP: | |
| 292 decfsz i2c_temp,F ; check for timeout during I2C action | |
| 293 bra WaitMSSP2 | |
| 294 bra I2CFail ; timeout occured | |
| 295 WaitMSSP2: | |
| 296 btfss PIR1,SSPIF | |
| 297 bra WaitMSSP | |
| 298 clrf i2c_temp | |
| 299 bcf PIR1,SSPIF | |
| 300 return | |
| 301 | |
| 302 I2CReset: ; Something went wrong (Slave holds SDA low?) | |
| 303 clrf SSPCON1 ; wake-up slave and reset entire module | |
| 304 ostc_debug 'N' ; Sends debug-information to screen if debugmode active | |
| 305 clrf SSPCON2 | |
| 306 clrf SSPSTAT | |
| 307 bcf TRISC,3 ; SCL OUTPUT | |
| 308 bsf TRISC,4 ; SDA Input | |
| 309 bcf PORTC,3 | |
| 310 movlw d'9' | |
| 311 movwf i2c_temp ; clock-out 9 clock cycles manually | |
| 312 I2CReset_1: | |
| 313 bsf PORTC,3 ; SCL=1 | |
| 314 nop | |
| 315 btfsc PORTC,4 ; SDA=1? | |
| 316 bra I2CReset_2 ; =1, SDA has been released from slave | |
| 317 bcf PORTC,3 ; SCL=0 | |
| 318 bcf PORTC,3 | |
| 319 decfsz i2c_temp,F | |
| 320 bra I2CReset_1 ; check for nine clock cycles | |
| 321 I2CReset_2: | |
| 322 bsf TRISC,3 ; SCL Input | |
| 323 clrf SSPCON1 ; set I²C Mode | |
| 324 WAITMS d'10' ; Reset-Timeout for I2C devices | |
| 325 movlw b'00000000' | |
| 326 movwf SSPSTAT | |
| 327 movlw b'00101000' | |
| 328 movwf SSPCON1 | |
| 329 movlw b'00000000' | |
| 330 movwf SSPCON2 | |
| 331 movlw d'8' ; 400kHz I2C clock @ 16MHz Fcy | |
| 332 movwf SSPADD | |
| 333 call clear_LEDy | |
| 334 ostc_debug 'O' ; Sends debug-information to screen if debugmode active | |
| 335 return | |
| 336 I2C_TX: | |
| 337 movwf i2c_temp2 ; Data byte | |
| 338 bsf SSPCON2,SEN ; Start condition | |
| 339 rcall WaitMSSP | |
| 340 movlw b'10010000' ; Bit0=0: WRITE, Bit0=1: READ | |
| 341 movwf SSPBUF ; control byte | |
| 342 rcall WaitMSSP | |
| 343 rcall I2C_WaitforACK | |
| 344 movff i2c_temp2, SSPBUF ; Data Byte | |
| 345 rcall WaitMSSP | |
| 346 rcall I2C_WaitforACK | |
| 347 bsf SSPCON2,PEN ; Stop condition | |
| 348 rcall WaitMSSP | |
| 349 return | |
| 350 I2C_RX: | |
| 351 bcf PIR1,SSPIF | |
| 352 bsf SSPCON2,SEN ; Start condition | |
| 353 rcall WaitMSSP | |
| 354 movlw b'10010001' ; Bit0=0: WRITE, Bit0=1: READ | |
| 355 movwf SSPBUF ; control byte | |
| 356 rcall WaitMSSP | |
| 357 rcall I2C_WaitforACK | |
| 358 bsf SSPCON2, RCEN ; Enable recieve mode | |
| 359 rcall WaitMSSP | |
| 360 movff SSPBUF,i2c_temp2 ; Data Byte | |
| 361 bsf SSPCON2,ACKEN ; Master acknowlegde | |
| 362 rcall WaitMSSP | |
| 363 bsf SSPCON2,PEN ; Stop condition | |
| 364 rcall WaitMSSP | |
| 365 return |
