Mercurial > public > hwos_code
annotate src/i2c.asm @ 573:cf4395595d9b
docu update
author | heinrichsweikamp |
---|---|
date | Tue, 13 Feb 2018 14:55:43 +0100 |
parents | 0ba88db66492 |
children | b8f45b57302d |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
3 ; File i2c.asm | |
4 ; | |
560 | 5 ; I2C Interface |
6 ; | |
7 ; Compass0 | |
0 | 8 ; HMC5883L's read address (8-Bit): 0x3D |
9 ; HMC5883L's write address (8-Bit): 0x3C | |
10 ; MMA8452Q's read address (8-Bit): 0x39 | |
11 ; MMA8452Q's write address (8-Bit): 0x38 | |
12 ; | |
560 | 13 ; Compass1 |
427 | 14 ; LSM303D's read address (8-Bit): 0x3D |
15 ; LSM303D's write address (8-Bit): 0x3C | |
16 ; | |
560 | 17 ; Compass2 |
18 ; LSM303AGR's Compass read address (8-Bit): 0x3D | |
19 ; LSM303AGR's Compass write address (8-Bit): 0x3C | |
20 ; LSM303AGR's Acceleration read address (8-Bit): 0x33 | |
21 ; LSM303AGR's Acceleration write address (8-Bit): 0x32 | |
22 ; | |
23 ; RX Circuity | |
24 ; RX Circuity read address (8-Bit): 0x51 | |
25 ; RX Circuity write address (8-Bit): 0x50 | |
26 ; | |
27 ; | |
0 | 28 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. |
29 ;============================================================================= | |
30 ; HISTORY | |
31 ; 2012-08-22 : [mH] Creation | |
565 | 32 ; 2018-02-18 : [mH] Sync with hwOS Sport release |
0 | 33 |
34 | |
275 | 35 #include "hwos.inc" ; Mandatory header |
0 | 36 #include "wait.inc" |
113 | 37 #include "math.inc" |
560 | 38 #include "external_flash.inc" |
113 | 39 |
0 | 40 i2c CODE |
41 | |
42 WaitMSSP: | |
43 decfsz i2c_temp,F ; check for timeout during I2C action | |
560 | 44 bra WaitMSSP2 |
45 bra I2CFail ; timeout occured | |
0 | 46 WaitMSSP2: |
560 | 47 btfss PIR1,SSP1IF |
48 bra WaitMSSP | |
0 | 49 clrf i2c_temp |
560 | 50 bcf PIR1,SSP1IF |
0 | 51 return |
52 | |
53 I2C_WaitforACK: | |
560 | 54 btfss SSP1CON2,ACKSTAT ; checks for ACK bit from slave |
55 return | |
0 | 56 I2CFail: |
57 rcall I2CReset ; I2C Reset | |
560 | 58 bcf PIR1,SSP1IF |
0 | 59 clrf i2c_temp |
560 | 60 bsf i2c_error_flag ; set error flag |
0 | 61 return |
62 | |
63 I2CReset: ; Something went wrong (Slave holds SDA low?) | |
64 clrf SSP1CON1 ; wake-up slave and reset entire module | |
65 clrf SSP1CON2 | |
66 clrf SSP1STAT | |
67 bcf TRISC,3 ; SCL OUTPUT | |
68 bsf TRISC,4 ; SDA Input | |
69 bcf PORTC,3 | |
70 movlw d'9' | |
71 movwf i2c_temp ; clock-out 9 clock cycles manually | |
72 I2CReset_1: | |
73 bsf PORTC,3 ; SCL=1 | |
74 nop | |
75 nop | |
76 nop | |
77 nop | |
78 btfsc PORTC,4 ; SDA=1? | |
79 bra I2CReset_2 ; =1, SDA has been released from slave | |
80 bcf PORTC,3 ; SCL=0 | |
81 nop | |
82 nop | |
83 bcf PORTC,3 | |
84 nop | |
85 nop | |
86 decfsz i2c_temp,F | |
87 bra I2CReset_1 ; check for nine clock cycles | |
88 I2CReset_2: | |
89 bsf TRISC,3 ; SCL Input | |
90 clrf SSP1CON1 ; setup I²C Mode | |
91 WAITMS d'10' ; Reset-Timeout for I2C devices | |
92 movlw b'00000000' ; with slew rate control | |
560 | 93 movwf SSP1STAT |
0 | 94 movlw b'00101000' |
95 movwf SSP1CON1 | |
96 movlw b'00000000' | |
97 movwf SSP1CON2 | |
98 movlw 0x27 | |
99 movwf SSP1ADD | |
100 return | |
101 | |
102 I2C_TX: | |
103 movwf SSP1BUF | |
104 rcall WaitMSSP | |
105 bra I2C_WaitforACK ; Returns... | |
106 | |
107 I2C_TwoBytesRX_div16: ; Get two bytes and devide lo:hi/16 (signed) | |
108 rcall I2C_OneByteRX ; Get one byte | |
109 movff SSP1BUF,hi ; Data Byte | |
110 rcall I2C_OneByteRX ; Get one byte | |
111 movff SSP1BUF,lo ; Data Byte | |
112 I2C_TwoBytesRX_div16_2: ; devide lo:hi/16 (signed) only | |
113 bcf STATUS,C | |
114 btfsc hi,7 ; Copy sign bit to carry | |
115 bsf STATUS,C | |
116 rrcf hi ; /2 | |
427 | 117 rrcf lo |
118 I2C_TwoBytesRX_div8_2: ; devide lo:hi/8 (signed) only | |
0 | 119 bcf STATUS,C |
120 btfsc hi,7 ; Copy sign bit to carry | |
121 bsf STATUS,C | |
122 rrcf hi ; /4 | |
427 | 123 rrcf lo |
0 | 124 bcf STATUS,C |
125 btfsc hi,7 ; Copy sign bit to carry | |
126 bsf STATUS,C | |
127 rrcf hi ; /8 | |
128 rrcf lo | |
129 bcf STATUS,C | |
130 btfsc hi,7 ; Copy sign bit to carry | |
131 bsf STATUS,C | |
132 rrcf hi ; /16 | |
133 rrcf lo | |
134 return | |
135 | |
136 global I2C_RX_accelerometer | |
137 I2C_RX_accelerometer: | |
560 | 138 btfsc compass_type2 ; compass2 |
139 bra I2C_RX_accelerometer_compass2 ; yes | |
427 | 140 btfsc compass_type ; compass1? |
141 bra I2C_RX_accelerometer_compass1 ; yes | |
560 | 142 I2C_RX_accelerometer_compass0: |
427 | 143 bsf SSP1CON2,SEN ; Start condition |
144 rcall WaitMSSP | |
145 movlw 0x38 ; address | |
0 | 146 rcall I2C_TX |
427 | 147 movlw 0x00 |
0 | 148 rcall I2C_TX |
427 | 149 bsf SSP1CON2,RSEN ; Repeated start condition (!) |
150 rcall WaitMSSP | |
151 movlw 0x39 ; address | |
0 | 152 rcall I2C_TX |
153 | |
158 | 154 rcall I2C_OneByteRX ; Get Status Byte |
155 movf SSP1BUF,W | |
156 | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
157 ; Non-flipped screen: |
0 | 158 ; Chip orientation on the PCB requires |
159 ; Original = Corrected | |
160 ; x = -x | |
161 ; y = -y | |
162 ; z = -z | |
163 | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
164 ; Flipped screen: |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
165 ; Chip orientation on the PCB requires |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
166 ; Original = Corrected |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
167 ; x = x |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
168 ; y = y |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
169 ; z = -z |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
170 |
0 | 171 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed) |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
172 btfsc flip_screen ; 180° rotation ? |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
173 bra I2C_RX_accelerometer2 ; Yes |
0 | 174 comf hi ; 16bit sign change. |
175 negf lo | |
176 btfsc STATUS,C ; Carry to propagate ? | |
177 incf hi,F ; YES: do it. | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
178 I2C_RX_accelerometer2: |
0 | 179 movff lo,accel_DX+0 |
180 movff hi,accel_DX+1 ; Copy result | |
181 | |
182 rcall I2C_TwoBytesRX_div16 ; Get two bytes and devide /16 (signed) | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
183 btfsc flip_screen ; 180° rotation ? |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
184 bra I2C_RX_accelerometer3 ; Yes |
0 | 185 comf hi ; 16bit sign change. |
186 negf lo | |
187 btfsc STATUS,C ; Carry to propagate ? | |
188 incf hi,F ; YES: do it. | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
189 I2C_RX_accelerometer3: |
0 | 190 movff lo,accel_DY+0 |
191 movff hi,accel_DY+1 ; Copy result | |
192 | |
193 rcall I2C_OneByteRX ; Get one byte | |
194 movff SSP1BUF,hi ; Data Byte | |
195 bsf SSP1CON2, RCEN ; Enable recieve mode | |
196 rcall WaitMSSP | |
197 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+0)... | |
198 movff SSP1BUF,lo ; Data Byte | |
199 | |
200 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only | |
201 comf hi ; 16bit sign change. | |
202 negf lo | |
203 btfsc STATUS,C ; Carry to propagate ? | |
204 incf hi,F ; YES: do it. | |
205 movff lo,accel_DZ+0 | |
206 movff hi,accel_DZ+1 ; Copy result | |
207 | |
208 bsf SSP1CON2,PEN ; Stop condition | |
560 | 209 bra WaitMSSP ; (And return) |
0 | 210 |
427 | 211 I2C_RX_accelerometer_compass1: |
212 bsf SSP1CON2,SEN ; Start condition | |
213 rcall WaitMSSP | |
214 movlw 0x3C ; address | |
215 rcall I2C_TX | |
216 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) | |
217 rcall I2C_TX | |
218 bsf SSP1CON2,RSEN ; Repeated start condition (!) | |
219 rcall WaitMSSP | |
220 movlw 0x3D ; address | |
560 | 221 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here... |
427 | 222 rcall I2C_TX |
223 ; Non-flipped screen: | |
224 ; Chip orientation on the PCB requires | |
225 ; Original = Corrected | |
560 | 226 ; x = -x (Compass 1) |
227 ; x = x (Compass 2) | |
427 | 228 ; y = -y |
229 ; z = -z | |
230 | |
231 ; Flipped screen: | |
232 ; Chip orientation on the PCB requires | |
233 ; Original = Corrected | |
560 | 234 ; x = x (Compass 1) |
235 ; x = -x (Compass 2) | |
427 | 236 ; y = y |
237 ; z = -z | |
238 | |
239 ; Dump the accelerator data | |
240 rcall I2C_OneByteRX | |
241 movff SSP1BUF,lo ;accel_DX+0 | |
242 rcall I2C_OneByteRX | |
243 movff SSP1BUF,hi ;accel_DX+1 | |
244 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only | |
560 | 245 btfss compass_type2 ; compass 2? |
246 bra I2C_RX_accelerometer1_c1 ; No, compass 1 | |
247 ; compass 2 | |
248 btfss flip_screen ; 180° rotation ? | |
249 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z | |
250 ; flipped compass 2, negate x | |
251 comf hi ; 16bit sign change. | |
252 negf lo | |
253 btfsc STATUS,C ; Carry to propagate ? | |
254 incf hi,F ; YES: do it. | |
255 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z | |
256 | |
257 I2C_RX_accelerometer1_c1: | |
427 | 258 btfsc flip_screen ; 180° rotation ? |
259 bra I2C_RX_accelerometer2_c1 ; Yes | |
560 | 260 ; non-flipped compass 1, negate x |
427 | 261 comf hi ; 16bit sign change. |
262 negf lo | |
263 btfsc STATUS,C ; Carry to propagate ? | |
264 incf hi,F ; YES: do it. | |
265 I2C_RX_accelerometer2_c1: | |
560 | 266 ; flipped compass 1, non-flipped compass 2 |
427 | 267 movff lo,accel_DX+0 |
268 movff hi,accel_DX+1 ; Copy result | |
269 rcall I2C_OneByteRX | |
270 movff SSP1BUF,lo ;accel_DY+0 | |
271 rcall I2C_OneByteRX | |
272 movff SSP1BUF,hi ;accel_DY+1 | |
273 | |
274 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only | |
275 btfsc flip_screen ; 180° rotation ? | |
276 bra I2C_RX_accelerometer3_c1 ; Yes | |
277 comf hi ; 16bit sign change. | |
278 negf lo | |
279 btfsc STATUS,C ; Carry to propagate ? | |
280 incf hi,F ; YES: do it. | |
281 I2C_RX_accelerometer3_c1: | |
282 movff lo,accel_DY+0 | |
283 movff hi,accel_DY+1 ; Copy result | |
284 | |
285 rcall I2C_OneByteRX | |
286 movff SSP1BUF,lo ;accel_DZ+0 | |
287 bsf SSP1CON2, RCEN ; Enable recieve mode | |
288 rcall WaitMSSP | |
289 ; According to datasheet there should be no Master Acknowlegde for the last Byte (accel_DZ+1)... | |
290 movff SSP1BUF,hi ;accel_DZ+1 | |
291 bsf SSP1CON2,PEN ; Stop condition | |
292 rcall WaitMSSP | |
293 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only | |
294 comf hi ; 16bit sign change for Z | |
295 negf lo | |
296 btfsc STATUS,C ; Carry to propagate ? | |
297 incf hi,F ; YES: do it. | |
298 movff lo,accel_DZ+0 | |
299 movff hi,accel_DZ+1 ; Copy result | |
300 return | |
301 | |
560 | 302 I2C_RX_accelerometer_compass2: |
303 bsf SSP1CON2,SEN ; Start condition | |
304 rcall WaitMSSP | |
305 movlw 0x32 ; address | |
306 rcall I2C_TX | |
307 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) | |
308 rcall I2C_TX | |
309 bsf SSP1CON2,RSEN ; Repeated start condition (!) | |
310 rcall WaitMSSP | |
311 movlw 0x33 ; address | |
312 bra I2C_RX_accelerometer_compass1_xx | |
313 | |
0 | 314 I2C_OneByteRX: |
427 | 315 bsf SSP1CON2, RCEN ; Enable recieve mode |
316 rcall WaitMSSP | |
317 bsf SSP1CON2,ACKEN ; Master acknowlegde | |
318 bra WaitMSSP ; And return! | |
0 | 319 |
320 global I2C_RX_compass | |
321 I2C_RX_compass: | |
560 | 322 btfsc compass_type2 ; compass2 |
323 bra I2C_RX_compass2 ; yes | |
427 | 324 btfsc compass_type ; compass1? |
325 bra I2C_RX_compass1 ; yes | |
560 | 326 I2C_RX_compass0: |
427 | 327 bsf SSP1CON2,SEN ; Start condition |
328 rcall WaitMSSP | |
329 movlw 0x3C ; address | |
0 | 330 rcall I2C_TX |
427 | 331 movlw 0x03 |
332 rcall I2C_TX | |
333 bsf SSP1CON2,PEN ; Stop condition | |
334 rcall WaitMSSP | |
0 | 335 |
560 | 336 bcf PIR1,SSP1IF |
427 | 337 bsf SSP1CON2,SEN ; Start condition |
338 rcall WaitMSSP | |
339 movlw 0x3D ; address | |
0 | 340 rcall I2C_TX |
341 | |
342 ; Compass IC sends data in following order: | |
343 ; x MSB | |
344 ; x LSB | |
345 ; z MSB | |
346 ; z LSB | |
347 ; y MSB | |
348 ; y LSB | |
349 | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
350 ; Non-flipped screen |
0 | 351 ; Chip orientation on the PCB requires |
352 ; Original = Corrected | |
353 ; x = -y | |
354 ; z = z | |
355 ; y = x | |
356 | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
357 ; Flipped screen |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
358 ; Chip orientation on the PCB requires |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
359 ; Original = Corrected |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
360 ; x = y |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
361 ; z = z |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
362 ; y = -x |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
363 |
0 | 364 rcall I2C_OneByteRX ; Get one byte |
560 | 365 movff SSP1BUF,compass_DY+1; Data Byte |
0 | 366 rcall I2C_OneByteRX ; Get one byte |
560 | 367 movff SSP1BUF,compass_DY+0; Data Byte |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
368 btfsc flip_screen ; 180° rotation ? |
560 | 369 bra I2C_RX_compass0_2 ; Yes |
0 | 370 banksel compass_DY |
371 comf compass_DY+1 ; 16bit sign change. | |
372 negf compass_DY+0 | |
373 btfsc STATUS,C ; Carry to propagate ? | |
374 incf compass_DY+1,F ; YES: do it. | |
560 | 375 I2C_RX_compass0_2: |
0 | 376 banksel common |
377 rcall I2C_OneByteRX ; Get one byte | |
427 | 378 movff SSP1BUF,compass_DZ+1; Data Byte |
0 | 379 rcall I2C_OneByteRX ; Get one byte |
427 | 380 movff SSP1BUF,compass_DZ+0; Data Byte |
0 | 381 rcall I2C_OneByteRX ; Get one byte |
427 | 382 movff SSP1BUF,compass_DX+1; Data Byte |
383 bsf SSP1CON2, RCEN ; Enable recieve mode | |
384 rcall WaitMSSP | |
385 movff SSP1BUF,compass_DX+0; Data Byte | |
386 bsf SSP1CON2,PEN ; Stop condition | |
387 rcall WaitMSSP | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
388 btfss flip_screen ; 180° rotation ? |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
389 return ; No, done. |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
390 ; Yes, flip X |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
391 banksel compass_DX |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
392 comf compass_DX+1 ; 16bit sign change. |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
393 negf compass_DX+0 |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
394 btfsc STATUS,C ; Carry to propagate ? |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
395 incf compass_DX+1,F ; YES: do it. |
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
396 banksel common |
0 | 397 return |
427 | 398 |
399 I2C_RX_compass1: ; New compass | |
400 bsf SSP1CON2,SEN ; Start condition | |
401 rcall WaitMSSP | |
402 movlw 0x3C ; address | |
403 rcall I2C_TX | |
404 movlw b'10001000' ; 0x08 with auto-increment (MSB=1) | |
405 rcall I2C_TX | |
406 bsf SSP1CON2,RSEN ; Repeated start condition (!) | |
407 rcall WaitMSSP | |
408 movlw 0x3D ; address | |
409 rcall I2C_TX | |
560 | 410 ;rcall WaitMSSP ; Needed? (mH) |
427 | 411 rcall I2C_OneByteRX ; Get one byte |
412 movff SSP1BUF,lo ; Data Byte | |
413 rcall I2C_OneByteRX ; Get one byte | |
414 movff SSP1BUF,hi ; Data Byte | |
415 rcall I2C_TwoBytesRX_div8_2 | |
416 movff lo,compass_DX+0 | |
417 movff hi,compass_DX+1 | |
418 btfss flip_screen ; 180° rotation ? | |
419 bra I2C_RX_compass1_1 ; Yes | |
420 ; Yes, flip X | |
421 banksel compass_DX | |
422 comf compass_DX+1 ; 16bit sign change. | |
423 negf compass_DX+0 | |
424 btfsc STATUS,C ; Carry to propagate ? | |
425 incf compass_DX+1,F ; YES: do it. | |
426 banksel common | |
427 I2C_RX_compass1_1: | |
428 rcall I2C_OneByteRX ; Get one byte | |
429 movff SSP1BUF,lo ; Data Byte | |
430 rcall I2C_OneByteRX ; Get one byte | |
431 movff SSP1BUF,hi ; Data Byte | |
432 rcall I2C_TwoBytesRX_div8_2 | |
433 movff lo,compass_DY+0 | |
434 movff hi,compass_DY+1 | |
435 btfss flip_screen ; 180° rotation ? | |
436 bra I2C_RX_compass1_2 ; Yes | |
437 ; Yes, flip Y | |
438 banksel compass_DY | |
439 comf compass_DY+1 ; 16bit sign change. | |
440 negf compass_DY+0 | |
441 btfsc STATUS,C ; Carry to propagate ? | |
442 incf compass_DY+1,F ; YES: do it. | |
443 I2C_RX_compass1_2: | |
444 banksel common | |
445 rcall I2C_OneByteRX ; Get one byte | |
446 movff SSP1BUF,lo ; Data Byte | |
447 bsf SSP1CON2, RCEN ; Enable recieve mode | |
448 rcall WaitMSSP | |
449 movff SSP1BUF,hi ; Data Byte | |
450 rcall I2C_TwoBytesRX_div8_2 | |
451 movff lo,compass_DZ+0 | |
452 movff hi,compass_DZ+1 | |
453 bsf SSP1CON2,PEN ; Stop condition | |
560 | 454 bra WaitMSSP ;(And return) |
455 | |
456 I2C_RX_compass2: ; newest compass | |
457 bsf SSP1CON2,SEN ; Start condition | |
427 | 458 rcall WaitMSSP |
560 | 459 movlw 0x3C ; address |
460 rcall I2C_TX | |
461 movlw 0xE8 ; 0x68 with auto-increment (MSB=1) | |
462 rcall I2C_TX | |
463 bsf SSP1CON2,RSEN ; Repeated start condition (!) | |
464 rcall WaitMSSP | |
465 movlw 0x3D ; address | |
466 rcall I2C_TX | |
467 ; rcall WaitMSSP | |
468 rcall I2C_OneByteRX ; Get one byte | |
469 movff SSP1BUF,lo ; Data Byte | |
470 rcall I2C_OneByteRX ; Get one byte | |
471 movff SSP1BUF,hi ; Data Byte | |
472 ; rcall I2C_TwoBytesRX_div8_2 | |
473 btfsc flip_screen ; 180° rotation ? | |
474 bra I2C_RX_compass2_1 ; Yes, do nothing with X | |
475 ; No, flip X | |
476 comf hi ; 16bit sign change. | |
477 negf lo | |
478 btfsc STATUS,C ; Carry to propagate ? | |
479 incf hi,F ; YES: do it. | |
480 I2C_RX_compass2_1: | |
481 movff lo,compass_DX+0 | |
482 movff hi,compass_DX+1 | |
483 rcall I2C_OneByteRX ; Get one byte | |
484 movff SSP1BUF,lo ; Data Byte | |
485 rcall I2C_OneByteRX ; Get one byte | |
486 movff SSP1BUF,hi ; Data Byte | |
487 ; rcall I2C_TwoBytesRX_div8_2 | |
488 btfss flip_screen ; 180° rotation ? | |
489 bra I2C_RX_compass2_2 ; No, do nothing with Y | |
490 ; Yes, flip Y | |
491 comf hi ; 16bit sign change. | |
492 negf lo | |
493 btfsc STATUS,C ; Carry to propagate ? | |
494 incf hi,F ; YES: do it. | |
495 I2C_RX_compass2_2: | |
496 movff lo,compass_DY+0 | |
497 movff hi,compass_DY+1 | |
498 rcall I2C_OneByteRX ; Get one byte | |
499 movff SSP1BUF,lo ; Data Byte | |
500 rcall I2C_OneByteRX ; Get one byte | |
501 movff SSP1BUF,hi ; Data Byte | |
502 ;rcall I2C_TwoBytesRX_div8_2 | |
503 movff lo,compass_DZ+0 | |
504 movff hi,compass_DZ+1 | |
505 bsf SSP1CON2,PEN ; Stop condition | |
506 bra WaitMSSP ;(And return) | |
507 | |
508 | |
0 | 509 global I2C_init_compass |
510 I2C_init_compass: | |
427 | 511 bsf compass_enabled |
560 | 512 bcf compass_type2 |
513 ; probe compass 2 | |
514 bsf SSP1CON2,SEN ; Start condition | |
515 rcall WaitMSSP | |
516 movlw 0x32 ; Address byte + Write bit | |
517 movwf SSP1BUF ; control byte | |
518 rcall WaitMSSP | |
519 btfss SSP1CON2,ACKSTAT ; ACK? | |
520 bsf compass_type2 ; ACK send. compass2 present | |
521 bsf SSP1CON2,PEN ; Stop condition | |
522 rcall WaitMSSP | |
523 | |
524 btfsc compass_type2 | |
525 bra I2C_init_compass2 ; Compass2 | |
526 ; Check for compass0 or compass1... | |
427 | 527 bsf compass_type ; set flag |
528 bsf SSP1CON2,SEN ; Start condition | |
529 rcall WaitMSSP | |
530 movlw 0x3C ; address | |
531 rcall I2C_TX | |
532 movlw 0x0F | |
533 rcall I2C_TX | |
534 bsf SSP1CON2,PEN ; Stop condition | |
535 rcall WaitMSSP | |
560 | 536 bcf PIR1,SSP1IF |
427 | 537 bsf SSP1CON2,SEN ; Start condition |
538 rcall WaitMSSP | |
539 movlw 0x3D ; address | |
540 rcall I2C_TX | |
541 rcall I2C_OneByteRX ; Get one byte | |
542 movlw 0x49 ; 0x49 = Compass1 | |
543 cpfseq SSP1BUF | |
544 bcf compass_type ; clear flag | |
545 bsf SSP1CON2,PEN ; Stop condition | |
546 rcall WaitMSSP | |
547 | |
548 btfsc compass_type ; compass1? | |
549 bra I2C_init_compass1 ; yes | |
550 ; init compass0 | |
551 bsf SSP1CON2,SEN ; Start condition | |
552 rcall WaitMSSP | |
553 movlw 0x3C ; address | |
0 | 554 rcall I2C_TX |
427 | 555 movlw 0x00 |
0 | 556 rcall I2C_TX |
427 | 557 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias) |
558 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged | |
0 | 559 rcall I2C_TX |
20 | 560 I2C_init_compass_common: |
427 | 561 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) |
18
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
562 swapf i2c_temp,F |
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
563 comf i2c_temp,F |
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
564 bcf STATUS,C |
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
565 rlcf i2c_temp |
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
566 movf i2c_temp,W |
4e3f133dfbf4
add new opt_compass_gain option to work with more magnetic battery types
heinrichsweikamp
parents:
0
diff
changeset
|
567 clrf i2c_temp |
0 | 568 rcall I2C_TX |
427 | 569 movlw b'00000000' ; Continous Mode |
0 | 570 rcall I2C_TX |
427 | 571 bsf SSP1CON2,PEN ; Stop condition |
560 | 572 bra WaitMSSP ; (And return) |
0 | 573 |
427 | 574 I2C_init_compass1: |
575 bsf SSP1CON2,SEN ; Start condition | |
576 rcall WaitMSSP | |
577 movlw 0x3C ; address | |
578 rcall I2C_TX | |
579 movlw 0x9F ; 1F with auto-increment (MSB=1) | |
580 rcall I2C_TX | |
581 movlw b'00000000' ; CTRL0 | |
582 rcall I2C_TX | |
583 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON) | |
584 rcall I2C_TX | |
585 movlw b'11000000' ; CTRL2 (50Hz, +/-2g, | |
586 rcall I2C_TX | |
587 movlw b'00000000' ; CTRL3 | |
588 rcall I2C_TX | |
589 movlw b'00000000' ; CTRL4 | |
590 rcall I2C_TX | |
591 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz | |
592 rcall I2C_TX | |
429 | 593 init_compass1_common: |
498 | 594 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) |
448 | 595 ;movlw b'00000000' ; CTRL6 (+/-2 Gauss) |
498 | 596 ;movlw b'00100000' ; CTRL6 (+/-4 Gauss) |
427 | 597 rcall I2C_TX |
598 movlw b'00000000' ; CTRL7 Continuous Mode | |
599 rcall I2C_TX | |
600 bsf SSP1CON2,PEN ; Stop condition | |
560 | 601 bra WaitMSSP ; (And return) |
427 | 602 |
560 | 603 I2C_init_compass2: |
604 bsf SSP1CON2,SEN ; Start condition | |
427 | 605 rcall WaitMSSP |
606 movlw 0x3C ; address | |
607 rcall I2C_TX | |
560 | 608 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) |
427 | 609 rcall I2C_TX |
560 | 610 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous) |
427 | 611 rcall I2C_TX |
560 | 612 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off) |
427 | 613 rcall I2C_TX |
560 | 614 movlw b'00000000' ; CFG_REG_C_M BDU=0 |
427 | 615 rcall I2C_TX |
560 | 616 bsf SSP1CON2,PEN ; Stop condition |
617 bra WaitMSSP ;(And return) | |
427 | 618 |
0 | 619 global I2C_sleep_compass |
620 I2C_sleep_compass: | |
427 | 621 bcf compass_enabled |
560 | 622 btfsc compass_type2 ; compass2? |
623 bra I2C_sleep_compass2 ; yes | |
624 btfsc compass_type ; compass1? | |
625 bra I2C_sleep_compass1 ; yes | |
626 I2C_sleep_compass0: | |
427 | 627 bsf SSP1CON2,SEN ; Start condition |
628 rcall WaitMSSP | |
629 movlw 0x3C ; address | |
0 | 630 rcall I2C_TX |
427 | 631 movlw 0x00 |
0 | 632 rcall I2C_TX |
427 | 633 movlw b'01101000' ; ConfigA |
0 | 634 rcall I2C_TX |
427 | 635 movlw b'00100000' ; ConfigB |
636 rcall I2C_TX | |
637 movlw b'00000010' ; Idle Mode | |
638 rcall I2C_TX | |
639 bsf SSP1CON2,PEN ; Stop condition | |
560 | 640 bra WaitMSSP ; (And return) |
0 | 641 |
427 | 642 I2C_sleep_compass1: |
643 bsf SSP1CON2,SEN ; Start condition | |
644 rcall WaitMSSP | |
645 movlw 0x3C ; address | |
646 rcall I2C_TX | |
647 movlw 0x20 ; CTRL_REG1 | |
648 rcall I2C_TX | |
649 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode | |
650 rcall I2C_TX | |
651 bsf SSP1CON2,PEN ; Stop condition | |
652 rcall WaitMSSP | |
653 bsf SSP1CON2,SEN ; Start condition | |
654 rcall WaitMSSP | |
655 movlw 0x3C ; address | |
656 rcall I2C_TX | |
657 movlw 0x26 ; CTRL_REG7 | |
658 rcall I2C_TX | |
659 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode | |
660 rcall I2C_TX | |
661 bsf SSP1CON2,PEN ; Stop condition | |
560 | 662 bra WaitMSSP ;(And return) |
663 | |
664 | |
665 I2C_sleep_compass2: | |
666 ; magnetic | |
667 bsf SSP1CON2,SEN ; Start condition | |
427 | 668 rcall WaitMSSP |
560 | 669 movlw 0x3C ; address |
670 rcall I2C_TX | |
671 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) | |
672 rcall I2C_TX | |
673 movlw b'00000010' ; CFG_REG_A_M (Idle mode) | |
674 rcall I2C_TX | |
675 bsf SSP1CON2,PEN ; Stop condition | |
676 bra WaitMSSP ; (And return) | |
427 | 677 |
560 | 678 I2C_sleep_accelerometer2: |
679 ; accelerometer | |
680 bsf SSP1CON2,SEN ; Start condition | |
681 rcall WaitMSSP | |
682 movlw 0x32 ; address | |
683 rcall I2C_TX | |
684 movlw 0x9F ; 1F with auto-increment (MSB=1) | |
685 rcall I2C_TX | |
686 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) | |
687 rcall I2C_TX | |
688 movlw b'00000000' ; CTRL_REG1_A (All off) | |
689 rcall I2C_TX | |
690 bsf SSP1CON2,PEN ; Stop condition | |
691 bra WaitMSSP ; (And return) | |
0 | 692 |
693 global I2C_init_accelerometer | |
694 I2C_init_accelerometer: | |
560 | 695 btfsc compass_type2 ; compass2? |
696 bra I2C_init_accelerometer2 ; Yes. | |
697 | |
427 | 698 btfsc compass_type ; compass1? |
699 return ; yes, ignore | |
700 | |
0 | 701 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode |
702 | |
560 | 703 bsf SSP1CON2,SEN ; Start condition |
704 rcall WaitMSSP | |
705 movlw 0x38 ; address | |
0 | 706 rcall I2C_TX |
560 | 707 movlw 0x0E ; XYZ_DATA_CFG |
0 | 708 rcall I2C_TX |
560 | 709 movlw b'00000000' ; High pass Filter=0 , +/- 2g range |
0 | 710 rcall I2C_TX |
560 | 711 bsf SSP1CON2,PEN ; Stop condition |
712 rcall WaitMSSP | |
0 | 713 |
714 | |
560 | 715 bsf SSP1CON2,SEN ; Start condition |
716 rcall WaitMSSP | |
717 movlw 0x38 ; address | |
0 | 718 rcall I2C_TX |
560 | 719 movlw 0x2A ; CTRL_REG1 |
0 | 720 rcall I2C_TX |
721 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode | |
560 | 722 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode |
0 | 723 rcall I2C_TX |
560 | 724 movlw b'00000010' ; CTRL_REG2: High Res in Active mode |
0 | 725 rcall I2C_TX |
560 | 726 bsf SSP1CON2,PEN ; Stop condition |
727 rcall WaitMSSP | |
0 | 728 |
560 | 729 bsf SSP1CON2,SEN ; Start condition |
730 rcall WaitMSSP | |
731 movlw 0x38 ; address | |
0 | 732 rcall I2C_TX |
560 | 733 movlw 0x2A ; CTRL_REG1 |
0 | 734 rcall I2C_TX |
735 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode | |
560 | 736 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode |
737 rcall I2C_TX | |
738 bsf SSP1CON2,PEN ; Stop condition | |
739 bra WaitMSSP ; (And return) | |
740 | |
741 I2C_init_accelerometer2: | |
742 bsf SSP1CON2,SEN ; Start condition | |
743 rcall WaitMSSP | |
744 movlw 0x32 ; address | |
745 rcall I2C_TX | |
746 movlw 0x9F ; 1F with auto-increment (MSB=1) | |
0 | 747 rcall I2C_TX |
560 | 748 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) |
749 rcall I2C_TX | |
750 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON) | |
751 rcall I2C_TX | |
752 movlw b'00000000' ; CTRL_REG2_A | |
753 rcall I2C_TX | |
754 movlw b'00000000' ; CTRL_REG3_A | |
755 rcall I2C_TX | |
756 movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g, | |
757 rcall I2C_TX | |
758 movlw b'00000000' ; CTRL_REG5_A | |
759 rcall I2C_TX | |
760 bsf SSP1CON2,PEN ; Stop condition | |
761 bra WaitMSSP ; (And return) | |
0 | 762 |
763 global I2C_sleep_accelerometer | |
764 I2C_sleep_accelerometer: | |
560 | 765 btfsc compass_type2 ; Compass2 |
766 bra I2C_sleep_accelerometer2 ; Yes | |
427 | 767 btfsc compass_type ; compass1? |
768 return ; yes, ignore | |
769 | |
0 | 770 bsf SSP1CON2,SEN ; Start condition |
771 rcall WaitMSSP | |
772 movlw 0x38 ; address | |
773 rcall I2C_TX | |
774 movlw 0x2A ; CTRL_REG1 | |
775 rcall I2C_TX | |
776 movlw b'00000000' ; St. By Mode | |
777 rcall I2C_TX | |
778 bsf SSP1CON2,PEN ; Stop condition | |
560 | 779 bra WaitMSSP ; (And return) |
780 | |
556
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
781 lt2942_init_again: |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
782 clrf i2c_temp |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
783 movlw 0x02 ; Point to accumulated charge registers |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
784 rcall I2C_TX_GAUGE |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
785 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
786 rcall WaitMSSP |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
787 rcall I2C_WaitforACK |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
788 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
789 rcall WaitMSSP |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
790 rcall I2C_WaitforACK |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
791 bsf SSP1CON2,PEN ; Stop condition |
560 | 792 rcall WaitMSSP |
556
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
793 movff battery_acumulated_charge+1,sub_a+1 |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
794 movff battery_acumulated_charge+0,sub_a+0 |
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
795 ; and init again... |
560 | 796 |
797 global lt2942_init | |
113 | 798 lt2942_init: ; Setup Control register B |
799 clrf i2c_temp | |
800 movlw 0x01 ; Point to control reg B | |
467 | 801 rcall I2C_TX_GAUGE |
802 movlw b'11111000' ; Automatic conversion every two seconds | |
113 | 803 movff WREG, SSP1BUF ; Data Byte |
804 rcall WaitMSSP | |
805 rcall I2C_WaitforACK | |
560 | 806 bsf SSP1CON2,PEN ; Stop condition |
467 | 807 bra WaitMSSP ; (And return) |
113 | 808 |
809 global lt2942_get_status | |
810 lt2942_get_status: ; Read status register | |
448 | 811 bcf battery_gauge_available ; Clear flag |
113 | 812 clrf i2c_temp |
813 movlw 0x00 ; Point to Status reg | |
467 | 814 rcall I2C_TX_GAUGE |
815 rcall I2C_RX_GAUGE | |
113 | 816 movff SSP1BUF,WREG |
467 | 817 btfss WREG,7 ; 2942 found? |
818 bsf battery_gauge_available ; Yes, set flag | |
819 bsf SSP1CON2,PEN ; Stop condition | |
820 bra WaitMSSP ; (And return) | |
113 | 821 |
822 | |
823 global lt2942_get_voltage | |
824 lt2942_get_voltage: ; Read battery voltage registers | |
825 clrf i2c_temp | |
826 movlw 0x08 ; Point to voltage registers | |
448 | 827 rcall I2C_TX_GAUGE |
828 rcall I2C_RX_GAUGE | |
113 | 829 bsf SSP1CON2,ACKEN ; Master acknowlegde |
830 rcall WaitMSSP | |
831 movff SSP1BUF,xA+1 | |
832 bsf SSP1CON2, RCEN ; Enable recieve mode | |
833 rcall WaitMSSP | |
834 movff SSP1BUF,xA+0 | |
835 bsf SSP1CON2,PEN ; Stop condition | |
836 rcall WaitMSSP | |
837 | |
838 ; banksel common | |
839 ; xA:2 loaded with raw values | |
840 movlw LOW .6000 | |
841 movwf xB+0 | |
842 movlw HIGH .6000 | |
843 movwf xB+1 | |
844 call mult16x16 ;xA*xB=xC | |
845 | |
846 ; devide xC (32bit)/65535 for result in mV (16bit) | |
847 movlw .16 | |
848 movwf i2c_temp | |
849 lt2942_get_voltage2: | |
850 bcf STATUS,C | |
851 rrcf xC+3,F | |
852 rrcf xC+2,F | |
853 rrcf xC+1,F | |
854 rrcf xC+0,F | |
855 decfsz i2c_temp,F | |
856 bra lt2942_get_voltage2 | |
857 | |
858 ; Update battery voltage in mV | |
859 movff xC+1,batt_voltage+1 | |
860 movff xC+0,batt_voltage+0 | |
466 | 861 |
862 tstfsz batt_voltage+1 ; <256mV? | |
863 return ; No, done. | |
864 | |
560 | 865 bra lt2942_init ;(and return) |
113 | 866 |
867 ; global lt2942_get_temperature | |
868 ;lt2942_get_temperature: ; Read temperature registers | |
869 ; clrf i2c_temp | |
870 ; movlw 0x0C ; Point to temperature registers | |
871 ; call I2C_TX_GAUGE | |
872 ; call I2C_RX | |
873 ; bsf SSP1CON2,ACKEN ; Master acknowlegde | |
874 ; rcall WaitMSSP | |
875 ; movff SSP1BUF,xA+1 | |
876 ; bsf SSP1CON2, RCEN ; Enable recieve mode | |
877 ; rcall WaitMSSP | |
878 ; movff SSP1BUF,xA+0 | |
879 ; bsf SSP1CON2,PEN ; Stop condition | |
880 ; rcall WaitMSSP | |
881 ; | |
882 ;; banksel common | |
883 ; ; xA:2 loaded with raw values | |
884 ; movlw LOW .6000 | |
885 ; movwf xB+0 | |
886 ; movlw HIGH .6000 | |
887 ; movwf xB+1 | |
888 ; call mult16x16 ;xA*xB=xC | |
889 ; | |
890 ; ; devide xC (32bit)/65535 for result in 0.1K (16bit) | |
891 ; movlw .16 | |
892 ; movwf i2c_temp | |
893 ;lt2942_get_temperature2: | |
894 ; bcf STATUS,C | |
895 ; rrcf xC+3,F | |
896 ; rrcf xC+2,F | |
897 ; rrcf xC+1,F | |
898 ; rrcf xC+0,F | |
899 ; decfsz i2c_temp,F | |
900 ; bra lt2942_get_temperature2 | |
901 ; | |
902 ; movff xC+1,sub_a+1 | |
903 ; movff xC+0,sub_a+0 | |
904 ; movlw LOW .2731 ; Kelvin to Celcius offset | |
905 ; movwf sub_b+0 | |
906 ; movlw HIGH .2731 ; Kelvin to Celcius offset | |
907 ; movwf sub_b+1 | |
908 ; call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values) | |
909 ; | |
910 ; ; Update batttery_temperature in 0.1°C | |
911 ; movff sub_c+1,battery_temperature+1 | |
912 ; movff sub_c+0,battery_temperature+0 | |
913 ; return | |
914 | |
915 global lt2942_get_accumulated_charge | |
916 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent | |
917 clrf i2c_temp | |
466 | 918 movlw 0x00 ; Point to status register |
467 | 919 rcall I2C_TX_GAUGE |
920 rcall I2C_RX_GAUGE | |
466 | 921 bsf SSP1CON2,ACKEN ; Master acknowlegde |
113 | 922 rcall WaitMSSP |
466 | 923 movff SSP1BUF,gauge_status_byte |
924 | |
925 bsf SSP1CON2, RCEN ; Enable recieve mode | |
926 rcall WaitMSSP ; Dummy read (Control byte) | |
467 | 927 movf SSP1BUF,W |
928 bsf SSP1CON2,ACKEN ; Master acknowlegde | |
929 rcall WaitMSSP | |
466 | 930 |
931 bsf SSP1CON2, RCEN ; Enable recieve mode | |
113 | 932 rcall WaitMSSP |
466 | 933 movff SSP1BUF,sub_a+1 |
467 | 934 bsf SSP1CON2,ACKEN ; Master acknowlegde |
935 rcall WaitMSSP | |
466 | 936 |
937 bsf SSP1CON2, RCEN ; Enable recieve mode | |
938 rcall WaitMSSP | |
939 movff SSP1BUF,sub_a+0 | |
940 bsf SSP1CON2,PEN ; Stop condition | |
113 | 941 rcall WaitMSSP |
466 | 942 |
943 movff gauge_status_byte,sub_b+0 ; copy into bank common | |
944 btfsc sub_b+0,0 ; =1: UVLO Event | |
556
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
945 rcall lt2942_init_again |
113 | 946 |
466 | 947 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value |
948 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value | |
949 | |
113 | 950 ; Compute batt_percent |
211 | 951 ; (charge-battery_offset)/365 |
448 | 952 movff battery_offset+0,sub_b+0 |
953 movff battery_offset+1,sub_b+1 | |
113 | 954 call subU16 ; sub_c = sub_a - sub_b (with signed values) |
955 | |
956 clrf batt_percent ; Set to zero | |
957 btfsc neg_flag ; result negative? | |
449 | 958 bra lt2942_set_to_zero_percent ; Yes, keep LT2942 at zero percent and return |
113 | 959 |
960 ; > Zero, set batt_percent properly | |
961 movff sub_c+0,xA+0 | |
962 movff sub_c+1,xA+1 | |
448 | 963 movff battery_capacity+0,xB+0 |
964 movff battery_capacity+1,xB+1 | |
113 | 965 call div16x16 ;xA/xB=xC with xA+0 as remainder, uses divB as temp variable |
966 movff xC+0,batt_percent | |
967 return | |
968 | |
449 | 969 lt2942_set_to_zero_percent: |
970 clrf i2c_temp | |
971 movlw 0x02 ; Point to accumulated charge registers | |
972 rcall I2C_TX_GAUGE | |
973 movff battery_offset+1,SSP1BUF | |
974 rcall WaitMSSP | |
975 rcall I2C_WaitforACK | |
976 movff battery_offset+0,SSP1BUF | |
977 rcall WaitMSSP | |
978 rcall I2C_WaitforACK | |
979 bsf SSP1CON2,PEN ; Stop condition | |
980 bra WaitMSSP; (and return) | |
981 | |
113 | 982 global lt2942_charge_done |
983 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF | |
984 clrf i2c_temp | |
985 movlw 0x02 ; Point to accumulated charge registers | |
448 | 986 rcall I2C_TX_GAUGE |
987 setf SSP1BUF ; Data Byte | |
113 | 988 rcall WaitMSSP |
989 rcall I2C_WaitforACK | |
448 | 990 setf SSP1BUF ; Data Byte |
113 | 991 rcall WaitMSSP |
992 rcall I2C_WaitforACK | |
448 | 993 bsf SSP1CON2,PEN ; Stop condition |
994 bra WaitMSSP; (and return) | |
113 | 995 |
996 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC | |
997 movwf i2c_temp+1 ; Data byte | |
448 | 998 bsf SSP1CON2,SEN ; Start condition |
113 | 999 rcall WaitMSSP |
1000 movlw b'11001000' ; Address byte + Write bit | |
1001 movwf SSP1BUF ; control byte | |
1002 rcall WaitMSSP | |
1003 rcall I2C_WaitforACK | |
1004 movff i2c_temp+1, SSP1BUF ; Data Byte | |
1005 rcall WaitMSSP | |
448 | 1006 bra I2C_WaitforACK ; (and return) |
113 | 1007 |
1008 I2C_RX_GAUGE: | |
1009 bsf SSP1CON2,SEN ; Start condition | |
1010 rcall WaitMSSP | |
1011 movlw b'11001001' ; Address byte + Read bit | |
1012 movwf SSP1BUF ; control byte | |
1013 rcall WaitMSSP | |
1014 rcall I2C_WaitforACK | |
467 | 1015 bsf SSP1CON2, RCEN ; Enable recieve mode |
448 | 1016 bra WaitMSSP; (and return) |
560 | 1017 |
1018 | |
1019 global I2C_probe_OSTC_rx | |
1020 I2C_probe_OSTC_rx: | |
1021 bsf SSP1CON2,SEN ; Start condition | |
1022 rcall WaitMSSP | |
1023 movlw 0x50 ; Address byte + Write bit | |
1024 movwf SSP1BUF ; control byte | |
1025 rcall WaitMSSP | |
1026 btfss SSP1CON2,ACKSTAT ; ACK? | |
1027 bsf ostc_rx_present ; ACK send. OSTC_RX present! | |
1028 bsf SSP1CON2,PEN ; Stop condition | |
1029 rcall WaitMSSP | |
1030 btfss ostc_rx_present ; Do we have the RX? | |
1031 return ; No, Done. | |
1032 WAITMS .1 | |
1033 bsf SSP1CON2,SEN ; Start condition | |
1034 rcall WaitMSSP | |
1035 movlw 0x50 ; Address byte + Write bit | |
1036 movwf SSP1BUF ; control byte | |
1037 rcall WaitMSSP | |
1038 rcall I2C_WaitforACK | |
1039 movlw 0x1B | |
1040 movwf SSP1BUF ; Data Byte (Get firmware) | |
1041 rcall WaitMSSP | |
1042 rcall I2C_WaitforACK | |
1043 bsf SSP1CON2,PEN ; Stop condition | |
1044 rcall WaitMSSP | |
1045 WAITMS .1 | |
1046 bsf SSP1CON2,SEN ; Start condition | |
1047 rcall WaitMSSP | |
1048 movlw 0x51 ; Address byte + Read bit | |
1049 movwf SSP1BUF ; control byte | |
1050 rcall WaitMSSP | |
1051 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1052 rcall WaitMSSP | |
1053 movff SSP1BUF,rx_firmware+0 | |
1054 bsf SSP1CON2,ACKEN ; Master acknowlegde | |
1055 rcall WaitMSSP | |
1056 | |
1057 ; last byte in read from RX circuity always with a NACK! | |
1058 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1059 rcall WaitMSSP | |
1060 movff SSP1BUF,rx_firmware+1 | |
1061 bsf SSP1CON2,ACKDT | |
1062 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde | |
1063 rcall WaitMSSP | |
1064 bcf SSP1CON2,ACKDT ; Reset ACKDT flag | |
1065 bsf SSP1CON2,PEN ; Stop condition | |
1066 bra WaitMSSP ;(and return) | |
1067 | |
1068 | |
1069 global I2C_get_tankdata | |
1070 I2C_get_tankdata: | |
1071 bsf SSP1CON2,SEN ; Start condition | |
1072 rcall WaitMSSP | |
1073 movlw 0x50 ; Address byte + Write bit | |
1074 movwf SSP1BUF ; control byte | |
1075 rcall WaitMSSP | |
1076 rcall I2C_WaitforACK | |
1077 movlw 0x1E ; Read buffer2 (48 Bytes) | |
1078 movwf SSP1BUF ; Data Byte | |
1079 rcall WaitMSSP | |
1080 rcall I2C_WaitforACK | |
1081 bsf SSP1CON2,PEN ; Stop condition | |
1082 rcall WaitMSSP | |
1083 WAITMS .1 | |
1084 | |
1085 ; read 48 bytes | |
1086 bsf SSP1CON2,SEN ; Start condition | |
1087 rcall WaitMSSP | |
1088 movlw 0x51 ; Address byte + read bit | |
1089 movwf SSP1BUF ; control byte | |
1090 rcall WaitMSSP | |
1091 rcall I2C_WaitforACK | |
1092 movlw .47 ; 47 with ACK + 1 w/o ACK | |
1093 movwf temp1 | |
1094 lfsr FSR2,rx_buffer+0 | |
1095 I2C_get_tankdata_loop_read: | |
1096 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1097 rcall WaitMSSP | |
1098 movff SSP1BUF,POSTINC2 | |
1099 bcf SSP1CON2,ACKDT | |
1100 bsf SSP1CON2,ACKEN ; Master acknowlegde | |
1101 rcall WaitMSSP | |
1102 decfsz temp1,F | |
1103 bra I2C_get_tankdata_loop_read | |
1104 | |
1105 ; 1 w/o ACK | |
1106 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1107 rcall WaitMSSP | |
1108 movff SSP1BUF,POSTINC2 | |
1109 bsf SSP1CON2,ACKDT | |
1110 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde | |
1111 rcall WaitMSSP | |
1112 bcf SSP1CON2,ACKDT ; Reset ACKDT flag | |
1113 | |
1114 bsf SSP1CON2,PEN ; Stop condition | |
1115 bra WaitMSSP ;(and return) | |
1116 | |
1117 | |
1118 global I2C_update_OSTC_rx | |
1119 I2C_update_OSTC_rx: ; 992*64byte master loop | |
1120 bcf i2c_error_flag ; clear error flag | |
1121 ; write 64 bytes | |
1122 bsf SSP1CON2,SEN ; Start condition | |
1123 rcall WaitMSSP | |
1124 movlw 0x50 ; Address byte + Write bit | |
1125 movwf SSP1BUF ; control byte | |
1126 rcall WaitMSSP | |
1127 rcall I2C_WaitforACK | |
1128 lfsr FSR2,buffer ; send buffer for verify | |
1129 movlw .64 | |
1130 movwf temp1 | |
1131 I2C_update_OSTC_loop: ; 64byte flash page loop | |
1132 movff up,POSTINC2 ; store for verify | |
1133 movff up,SSP1BUF | |
1134 rcall WaitMSSP | |
1135 rcall I2C_WaitforACK | |
1136 call ext_flash_read_block ; Read one byte | |
1137 movwf up ; prepare for transmit | |
1138 decfsz temp1,F | |
1139 bra I2C_update_OSTC_loop | |
1140 bsf SSP1CON2,PEN ; Stop condition | |
1141 rcall WaitMSSP | |
1142 WAITMS .1 | |
1143 | |
1144 ; read 64 bytes | |
1145 bsf SSP1CON2,SEN ; Start condition | |
1146 rcall WaitMSSP | |
1147 movlw 0x51 ; Address byte + read bit | |
1148 movwf SSP1BUF ; control byte | |
1149 rcall WaitMSSP | |
1150 rcall I2C_WaitforACK | |
1151 lfsr FSR2,buffer ; send buffer for verify | |
1152 movlw .63 ; 63 with ACK + 1 w/o ACK | |
1153 movwf temp1 | |
1154 I2C_update_OSTC_loop_read: | |
1155 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1156 rcall WaitMSSP | |
1157 movf SSP1BUF,W | |
1158 cpfseq POSTINC2 ; compare readback with original | |
1159 bsf i2c_error_flag ; Not equal, set flag | |
1160 bcf SSP1CON2,ACKDT | |
1161 bsf SSP1CON2,ACKEN ; Master acknowlegde | |
1162 rcall WaitMSSP | |
1163 decfsz temp1,F | |
1164 bra I2C_update_OSTC_loop_read | |
1165 | |
1166 ; 1 w/o ACK | |
1167 bsf SSP1CON2, RCEN ; Enable recieve mode | |
1168 rcall WaitMSSP | |
1169 movf SSP1BUF,W | |
1170 cpfseq POSTINC2 ; compare readback with original | |
1171 bsf i2c_error_flag ; Not equal, set flag | |
1172 bsf SSP1CON2,ACKDT | |
1173 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde | |
1174 rcall WaitMSSP | |
1175 bcf SSP1CON2,ACKDT ; Reset ACKDT flag | |
1176 | |
1177 bsf SSP1CON2,PEN ; Stop condition | |
1178 rcall WaitMSSP | |
1179 WAITMS .1 | |
1180 | |
1181 bsf SSP1CON2,SEN ; Start condition | |
1182 rcall WaitMSSP | |
1183 movlw 0x50 ; Address byte + Write bit | |
1184 movwf SSP1BUF ; control byte | |
1185 rcall WaitMSSP | |
1186 rcall I2C_WaitforACK | |
1187 movlw 0x1F ; Write command! | |
1188 movwf SSP1BUF ; Data Byte | |
1189 rcall WaitMSSP | |
1190 rcall I2C_WaitforACK | |
1191 bsf SSP1CON2,PEN ; Stop condition | |
1192 rcall WaitMSSP | |
1193 WAITMS .5 ; Required waiting time | |
1194 | |
1195 btfss i2c_error_flag | |
1196 retlw .0 ; All ok | |
1197 retlw .255 ; an error occured | |
448 | 1198 END |