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