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