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