comparison code_part1/OSTC_code_asm_part1/i2c_eeprom.asm @ 350:7250ca7c8d24 64kByte Logbook

64kByte logbook (Still incompatible to existing PC software)
author heinrichsweikamp
date Tue, 31 May 2011 19:43:29 +0200
parents dee88c962653
children a76c5a3e9e96
comparison
equal deleted inserted replaced
349:ed137d66ac65 350:7250ca7c8d24
16 ; written: 10/30/05 16 ; written: 10/30/05
17 ; last updated: 08/21/06 17 ; last updated: 08/21/06
18 ; known bugs: 18 ; known bugs:
19 ; ToDo: use 2nd 32KB from external EEPROM for something 19 ; ToDo: use 2nd 32KB from external EEPROM for something
20 20
21 incf_eeprom_address macro ext_ee_temp1 ; Will increase eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes 21 incf_eeprom_address macro ext_ee_temp1 ; Will increase eeprom_address:2 with the 8Bit value "ext_ee_temp1"
22 movlw ext_ee_temp1 ; care of bank switching at 0x8000 22 movlw ext_ee_temp1
23 call incf_eeprom_address0 23 call incf_eeprom_address0
24 endm 24 endm
25 25
26 incf_eeprom_address0: 26 incf_eeprom_address0:
27 addwf eeprom_address+0,F ; increase address 27 addwf eeprom_address+0,F ; increase address
28 movlw d'0' 28 movlw d'0'
29 addwfc eeprom_address+1,F 29 addwfc eeprom_address+1,F
30 30 return
31 btfss eeprom_address+1,7 ; at address 8000? 31 ; btfss eeprom_address+1,7 ; at address 8000?
32 return ; No, continue 32 ; return ; No, continue
33 33 ;
34 ; Yes, clear eeprom_address:2 34 ; ; Yes, clear eeprom_address:2
35 clrf eeprom_address+0 ; Clear eeprom address 35 ; clrf eeprom_address+0 ; Clear eeprom address
36 clrf eeprom_address+1 36 ; clrf eeprom_address+1
37 return ; Done. 37 ; return ; Done.
38 38 ;
39 ;============================================================================= 39 ;=============================================================================
40 ; Will decrease eeprom_address:2 with the 8Bit value "ext_ee_temp1" and takes 40 ; Will decrease eeprom_address:2 with the 8Bit value "ext_ee_temp1"
41 ; care of bank switching at 0x8000 41
42 42
43 decf_eeprom_address macro ext_ee_temp1 43 decf_eeprom_address macro ext_ee_temp1
44 movlw ext_ee_temp1 44 movlw ext_ee_temp1
45 call decf_eeprom_address0 45 call decf_eeprom_address0
46 endm 46 endm
47 47
48 decf_eeprom_address0: 48 decf_eeprom_address0:
49 subwf eeprom_address+0,F ; decrease address: do a 16-8bits substract. 49 subwf eeprom_address+0,F ; decrease address: do a 16-8bits substract.
50 movlw d'0' 50 movlw d'0'
51 subwfb eeprom_address+1,F 51 subwfb eeprom_address+1,F
52 52 return
53 btfss eeprom_address+1,7 ; at address 8000? 53 ;
54 return ; No, done. 54 ; btfss eeprom_address+1,7 ; at address 8000?
55 55 ; return ; No, done.
56 movlw b'01111111' ; yes, reset highbyte 56 ;
57 movwf eeprom_address+1 57 ; movlw b'01111111' ; yes, reset highbyte
58 return ; Done. 58 ; movwf eeprom_address+1
59 ; return ; Done.
59 60
60 ;============================================================================= 61 ;=============================================================================
61 62
62 write_external_eeprom: ; data in WREG 63 write_external_eeprom: ; data in WREG
63 ; increase address eeprom_address+0:eeprom_address+1 after write 64 ; increase address eeprom_address+0:eeprom_address+1 after write
70 rcall I2CWRITE ; writes WREG into EEPROM@eeprom_address 71 rcall I2CWRITE ; writes WREG into EEPROM@eeprom_address
71 movlw d'1' ; increase address 72 movlw d'1' ; increase address
72 addwf eeprom_address+0,F 73 addwf eeprom_address+0,F
73 movlw d'0' 74 movlw d'0'
74 addwfc eeprom_address+1,F 75 addwfc eeprom_address+1,F
75 bcf eeprom_overflow 76 return
76 btfss eeprom_address+1,7 ; at address 8000? 77 ; btfss eeprom_address+1,7 ; at address 8000?
77 return ; no, return 78 ; return ; no, return
78 79 ;
79 clrf eeprom_address+0 ; Clear eeprom address 80 ; clrf eeprom_address+0 ; Clear eeprom address
80 clrf eeprom_address+1 81 ; clrf eeprom_address+1
81 bsf eeprom_overflow ; and set overflow bit 82 ; return
82 return 83 ;
83
84 write_external_eeprom_block: ; Writes a block of 64Byte (one page in external EEPROM without stop condition 84 write_external_eeprom_block: ; Writes a block of 64Byte (one page in external EEPROM without stop condition
85 #ifdef TESTING 85 #ifdef TESTING
86 ; When Simulating with MPLabSIM, there is no way to emulate external EEPROM... 86 ; When Simulating with MPLabSIM, there is no way to emulate external EEPROM...
87 return 87 return
88 #endif 88 #endif
94 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes! 94 bsf eeprom_blockwrite ; After the start, do blockwriting for the next 63Bytes!
95 95
96 movlw d'0' ; increase address 96 movlw d'0' ; increase address
97 incf eeprom_address+0,F 97 incf eeprom_address+0,F
98 addwfc eeprom_address+1,F 98 addwfc eeprom_address+1,F
99 bcf eeprom_overflow 99 return
100 100
101 btfss eeprom_address+1,7 ; at address 8000 101 ; btfss eeprom_address+1,7 ; at address 8000
102 return ; no, return 102 ; return ; no, return
103 103 ;
104 clrf eeprom_address+0 ; Clear eeprom address 104 ; clrf eeprom_address+0 ; Clear eeprom address
105 clrf eeprom_address+1 105 ; clrf eeprom_address+1
106 bsf eeprom_overflow ; and set overflow bit 106 ; return
107 return
108 I2CWRITE_BLOCK: 107 I2CWRITE_BLOCK:
109 movwf ext_ee_temp1 ; Data byte in WREG 108 movwf ext_ee_temp1 ; Data byte in WREG
110 bsf SSPCON2,SEN ; Start condition 109 bsf SSPCON2,SEN ; Start condition
111 rcall WaitMSSP 110 rcall WaitMSSP
112 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ 111 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
112 btfss eeprom_address+1,7 ; Access Block2?
113 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
113 movwf SSPBUF ; control byte 114 movwf SSPBUF ; control byte
114 rcall WaitMSSP 115 rcall WaitMSSP
115 rcall I2C_WaitforACK 116 rcall I2C_WaitforACK
116 movff eeprom_address+1,SSPBUF ; High Address byte 117 movff eeprom_address+1,SSPBUF ; High Address byte
117 rcall WaitMSSP 118 rcall WaitMSSP
143 get_free_EEPROM_location3: 144 get_free_EEPROM_location3:
144 bsf SSPCON2, PEN ; Stop condition 145 bsf SSPCON2, PEN ; Stop condition
145 rcall WaitMSSP 146 rcall WaitMSSP
146 bsf SSPCON2,SEN ; Start condition 147 bsf SSPCON2,SEN ; Start condition
147 rcall WaitMSSP 148 rcall WaitMSSP
148 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ 149 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
150 btfss ext_ee_temp2,7 ; Access Block2?
151 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
149 movwf SSPBUF ; control byte 152 movwf SSPBUF ; control byte
150 rcall WaitMSSP 153 rcall WaitMSSP
151 btfsc SSPCON2,ACKSTAT 154 btfsc SSPCON2,ACKSTAT
152 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry! 155 bra get_free_EEPROM_location3 ; EEPROM NOT acknowledged, retry!
153 156
158 rcall WaitMSSP 161 rcall WaitMSSP
159 rcall I2C_WaitforACK 162 rcall I2C_WaitforACK
160 163
161 bsf SSPCON2,RSEN ; Start condition 164 bsf SSPCON2,RSEN ; Start condition
162 rcall WaitMSSP 165 rcall WaitMSSP
163 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ 166 movlw b'10101111' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
164 movwf SSPBUF ; control byte 167 btfss ext_ee_temp2,7 ; Access Block2?
168 movlw b'10100111' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
169 movwf SSPBUF ; control byte
165 rcall WaitMSSP 170 rcall WaitMSSP
166 rcall I2C_WaitforACK 171 rcall I2C_WaitforACK
167 172
168 get_free_EEPROM_location2: 173 get_free_EEPROM_location2:
169 bsf SSPCON2, RCEN ; Enable recieve mode 174 bsf SSPCON2, RCEN ; Enable recieve mode
198 get_free_EEPROM_location2c: 203 get_free_EEPROM_location2c:
199 movlw d'1' ; and increase search address 204 movlw d'1' ; and increase search address
200 addwf ext_ee_temp1,F 205 addwf ext_ee_temp1,F
201 movlw d'0' 206 movlw d'0'
202 addwfc ext_ee_temp2,F 207 addwfc ext_ee_temp2,F
203 208
204 btfsc ext_ee_temp2,7 ; 0x8000 reached? 209 movlw 0xFF
210 cpfseq ext_ee_temp2 ; =0xFFFF
211 bra get_free_EEPROM_location2d ; No
212 cpfseq ext_ee_temp1 ; =0xFFFF
213 bra get_free_EEPROM_location2d ; No
214
205 bra get_free_EEPROM_location3b ; yes 215 bra get_free_EEPROM_location3b ; yes
206 216
217 get_free_EEPROM_location2d:
207 bsf SSPCON2, ACKEN ; no, send Ack 218 bsf SSPCON2, ACKEN ; no, send Ack
208 rcall WaitMSSP 219 rcall WaitMSSP
209 bra get_free_EEPROM_location2 ; and continue search 220 bra get_free_EEPROM_location2 ; and continue search
210 get_free_EEPROM_location3b: 221 get_free_EEPROM_location3b:
211 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0 222 clrf eeprom_address+0 ; Not found in entire EEPROM, set to address 0
230 I2CREAD2_2: 241 I2CREAD2_2:
231 movlw d'1' 242 movlw d'1'
232 addwf eeprom_address+0,F 243 addwf eeprom_address+0,F
233 movlw d'0' 244 movlw d'0'
234 addwfc eeprom_address+1,F 245 addwfc eeprom_address+1,F
235 bcf eeprom_overflow 246 return
236 btfss eeprom_address+1,7 ; at 0x8000? 247 ; btfss eeprom_address+1,7 ; at 0x8000?
237 return ; no, return 248 ; return ; no, return
238 249 ;
239 clrf eeprom_address+0 ; Yes, clear address 250 ; clrf eeprom_address+0 ; Yes, clear address
240 clrf eeprom_address+1 251 ; clrf eeprom_address+1
241 bsf eeprom_overflow ; and set overflow bit 252 ; return
242 return 253 ;
243
244 I2CREAD3: ; block read start with automatic address increase 254 I2CREAD3: ; block read start with automatic address increase
245 rcall I2CREAD_COMMON 255 rcall I2CREAD_COMMON
246 ; no Stop condition here 256 ; no Stop condition here
247 bra I2CREAD2_2 257 bra I2CREAD2_2
248 258
259 I2CREAD_COMMON: 269 I2CREAD_COMMON:
260 bsf SSPCON2, PEN ; Stop 270 bsf SSPCON2, PEN ; Stop
261 rcall WaitMSSP 271 rcall WaitMSSP
262 bsf SSPCON2,SEN ; Start condition 272 bsf SSPCON2,SEN ; Start condition
263 rcall WaitMSSP 273 rcall WaitMSSP
264 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ 274
265 movwf SSPBUF ; control byte 275 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
276 btfss eeprom_address+1,7 ; Access Block2?
277 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
278 movwf SSPBUF ; control byte
266 rcall WaitMSSP 279 rcall WaitMSSP
267 btfsc SSPCON2,ACKSTAT 280 btfsc SSPCON2,ACKSTAT
268 bra I2CREAD ; EEPROM NOT acknowledged, retry! 281 bra I2CREAD ; EEPROM NOT acknowledged, retry!
269 movff eeprom_address+1,SSPBUF ; High Address byte 282 movff eeprom_address+1,SSPBUF ; High Address byte
270 rcall WaitMSSP 283 rcall WaitMSSP
273 rcall WaitMSSP 286 rcall WaitMSSP
274 rcall I2C_WaitforACK 287 rcall I2C_WaitforACK
275 288
276 bsf SSPCON2,RSEN ; Start condition 289 bsf SSPCON2,RSEN ; Start condition
277 rcall WaitMSSP 290 rcall WaitMSSP
278 movlw b'10100111' ; Bit0=0: WRITE, Bit0=1: READ 291
279 movwf SSPBUF ; control byte 292 movlw b'10101111' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
293 btfss eeprom_address+1,7 ; Access Block2?
294 movlw b'10100111' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
295 movwf SSPBUF ; control byte
280 rcall WaitMSSP 296 rcall WaitMSSP
281 rcall I2C_WaitforACK 297 rcall I2C_WaitforACK
282 298
283 bsf SSPCON2, RCEN ; Enable recieve mode 299 bsf SSPCON2, RCEN ; Enable recieve mode
284 rcall WaitMSSP 300 rcall WaitMSSP
288 304
289 I2CWRITE: 305 I2CWRITE:
290 movwf ext_ee_temp1 ; Data byte 306 movwf ext_ee_temp1 ; Data byte
291 bsf SSPCON2,SEN ; Start condition 307 bsf SSPCON2,SEN ; Start condition
292 rcall WaitMSSP 308 rcall WaitMSSP
293 movlw b'10100110' ; Bit0=0: WRITE, Bit0=1: READ 309 movlw b'10101110' ; Bit0=0: WRITE, Bit0=1: READ, BLOCK2
294 movwf SSPBUF ; control byte 310 btfss eeprom_address+1,7 ; Access Block2?
311 movlw b'10100110' ; No, -> Bit0=0: WRITE, Bit0=1: READ, BLOCK1
312 movwf SSPBUF ; control byte
295 rcall WaitMSSP 313 rcall WaitMSSP
296 rcall I2C_WaitforACK 314 rcall I2C_WaitforACK
297 movff eeprom_address+1,SSPBUF ; High Address byte 315 movff eeprom_address+1,SSPBUF ; High Address byte
298 rcall WaitMSSP 316 rcall WaitMSSP
299 rcall I2C_WaitforACK 317 rcall I2C_WaitforACK