Mercurial > public > hwos_code
annotate src/i2c.asm @ 649:ef2ed7e3a895
Merge
author | heinrichs weikamp |
---|---|
date | Fri, 04 Mar 2022 08:30:23 +0100 |
parents | 357341239438 070528a88715 |
children | bc214815deb2 |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
634 | 3 ; File i2c.asm * combined next generation V3.09.4b |
0 | 4 ; |
560 | 5 ; I2C Interface |
6 ; | |
0 | 7 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. |
8 ;============================================================================= | |
582 | 9 ; |
10 ; Compass0 | |
11 ; -------- | |
12 ; HMC5883L's read address (8-Bit): 0x3D | |
13 ; HMC5883L's write address (8-Bit): 0x3C | |
14 ; MMA8452Q's read address (8-Bit): 0x39 | |
15 ; MMA8452Q's write address (8-Bit): 0x38 | |
16 ; | |
17 ; Compass1 | |
18 ; -------- | |
19 ; LSM303D's read address (8-Bit): 0x3D | |
20 ; LSM303D's write address (8-Bit): 0x3C | |
21 ; | |
22 ; Compass2 | |
23 ; -------- | |
24 ; LSM303AGR's Compass read address (8-Bit): 0x3D | |
25 ; LSM303AGR's Compass write address (8-Bit): 0x3C | |
26 ; LSM303AGR's Acceleration read address (8-Bit): 0x33 | |
27 ; LSM303AGR's Acceleration write address (8-Bit): 0x32 | |
624 | 28 ; |
29 ; Compass3 | |
30 ; -------- | |
628 | 31 ; LSM303C's Compass read address (8-Bit): 0x3D |
32 ; LSM303C's Compass write address (8-Bit): 0x3C | |
624 | 33 ; LSM303C's Acceleration read address (8-Bit): 0x3B |
34 ; LSM303C's Acceleration write address (8-Bit): 0x3A | |
582 | 35 ; |
36 ; RX Circuity | |
37 ; ----------- | |
38 ; RX Circuity read address (8-Bit): 0x51 | |
39 ; RX Circuity write address (8-Bit): 0x50 | |
643 | 40 ; |
41 ; Battery gauge | |
42 ; ------------- | |
43 ; LTC2942 read address (8-Bit): 0xC9 | |
44 ; LTC2942 write address (8-Bit): 0xC8 | |
45 ; | |
46 ; Alternative pressure sensor | |
47 ; ----------- | |
48 ; MS5837 read address (8-Bit): 0xED | |
49 ; MS5837 write address (8-Bit): 0xEC | |
582 | 50 ; |
51 ; | |
52 | |
0 | 53 ; HISTORY |
54 ; 2012-08-22 : [mH] Creation | |
565 | 55 ; 2018-02-18 : [mH] Sync with hwOS Sport release |
0 | 56 |
57 | |
582 | 58 #include "hwos.inc" ; Mandatory header |
0 | 59 #include "wait.inc" |
113 | 60 #include "math.inc" |
631 | 61 #include "eeprom_rs232.inc" |
643 | 62 |
582 | 63 |
634 | 64 ;============================================================================= |
65 i2c CODE | |
582 | 66 ;============================================================================= |
0 | 67 |
634 | 68 |
0 | 69 |
634 | 70 ;----------------------------------------------------------------------------- |
71 ; Helper Function - get two Bytes and divide hi:lo/16 (signed) | |
72 ; | |
73 I2C_TwoBytesRX_div16: | |
74 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
75 movff SSP1BUF,hi ; copy data byte to hi | |
76 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
77 movff SSP1BUF,lo ; copy data byte to lo | |
78 I2C_TwoBytesRX_div16_2: ; divide hi:lo/16 (signed) only | |
582 | 79 bcf STATUS,C |
604 | 80 btfsc hi,7 ; copy sign bit to carry |
582 | 81 bsf STATUS,C |
82 rrcf hi ; /2 | |
83 rrcf lo | |
634 | 84 ;bra I2C_TwoBytesRX_div8 ; continue dividing hi:lo/8 (signed) |
85 | |
86 | |
87 ;----------------------------------------------------------------------------- | |
88 ; Helper Function - divide hi:lo/8 (signed) | |
89 ; | |
90 I2C_TwoBytesRX_div8: | |
582 | 91 bcf STATUS,C |
604 | 92 btfsc hi,7 ; copy sign bit to carry |
582 | 93 bsf STATUS,C |
94 rrcf hi ; /4 | |
95 rrcf lo | |
96 bcf STATUS,C | |
604 | 97 btfsc hi,7 ; copy sign bit to carry |
582 | 98 bsf STATUS,C |
99 rrcf hi ; /8 | |
100 rrcf lo | |
101 bcf STATUS,C | |
604 | 102 btfsc hi,7 ; copy sign bit to carry |
582 | 103 bsf STATUS,C |
104 rrcf hi ; /16 | |
105 rrcf lo | |
634 | 106 return ; done |
107 | |
0 | 108 |
634 | 109 ;----------------------------------------------------------------------------- |
110 ; Read Accelerometer | |
111 ; | |
582 | 112 global I2C_RX_accelerometer |
0 | 113 I2C_RX_accelerometer: |
628 | 114 btfsc compass_type3 ; compass3 ? |
115 bra I2C_RX_accelerometer_compass3 ; YES | |
604 | 116 btfsc compass_type2 ; compass2 ? |
117 bra I2C_RX_accelerometer_compass2 ; YES | |
628 | 118 btfsc compass_type1 ; compass1 ? |
604 | 119 bra I2C_RX_accelerometer_compass1 ; YES |
628 | 120 ;bra I2C_RX_accelerometer_compass0 ; NO - compass0 then |
121 | |
643 | 122 ;I2C_RX_accelerometer_compass0: |
604 | 123 bsf SSP1CON2,SEN ; start condition |
634 | 124 rcall WaitMSSP ; wait for TX to complete |
582 | 125 movlw 0x38 ; address |
643 | 126 movff WREG,i2c_error_vault+0 ; Store address |
634 | 127 rcall I2C_TX ; send byte |
128 movlw 0x00 ; ?? | |
129 rcall I2C_TX ; send byte | |
604 | 130 bsf SSP1CON2,RSEN ; repeated start condition |
634 | 131 rcall WaitMSSP ; wait for TX to complete |
582 | 132 movlw 0x39 ; address |
634 | 133 rcall I2C_TX ; send byte |
134 rcall I2C_OneByteRX ; get status byte | |
135 movf SSP1BUF,W ; copy status byte to WREG | |
158 | 136 |
582 | 137 ; Non-flipped screen: |
138 ; Chip orientation on the PCB requires | |
604 | 139 ; Original = corrected |
582 | 140 ; x = -x |
141 ; y = -y | |
142 ; z = -z | |
0 | 143 |
582 | 144 ; Flipped screen: |
145 ; Chip orientation on the PCB requires | |
604 | 146 ; Original = corrected |
582 | 147 ; x = x |
148 ; y = y | |
149 ; z = -z | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
150 |
604 | 151 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed) |
623 | 152 btfsc flip_screen ; 180° rotation? |
604 | 153 bra I2C_RX_accelerometer2 ; YES |
634 | 154 comf hi ; NO - 16 bit sign change |
155 negf lo ; - .... | |
156 btfsc STATUS,C ; - carry to propagate? | |
157 incf hi,F ; - YES - do it | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
158 I2C_RX_accelerometer2: |
634 | 159 MOVII mpr,accel_DX ; copy result to accel_DX |
160 | |
623 | 161 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed) |
162 btfsc flip_screen ; 180° rotation? | |
163 bra I2C_RX_accelerometer3 ; YES | |
634 | 164 comf hi ; NO - 16 bit sign change |
165 negf lo ; - ... | |
166 btfsc STATUS,C ; - carry to propagate? | |
167 incf hi,F ; YES - do it | |
168 I2C_RX_accelerometer3: | |
169 MOVII mpr,accel_DY ; copy result to accel_DY | |
170 | |
171 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
172 movff SSP1BUF,hi ; copy data byte to hi | |
173 bsf SSP1CON2,RCEN ; enable receive mode | |
174 rcall WaitMSSP ; wait for TX to complete | |
175 ; according to data sheet there should be no master acknowledge for the last byte (accel_DZ+0)... | |
176 movff SSP1BUF,lo ; copy data byte to lo | |
177 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed) | |
623 | 178 comf hi ; 16 bit sign change |
634 | 179 negf lo ; ... |
623 | 180 btfsc STATUS,C ; carry to propagate? |
181 incf hi,F ; YES - do it | |
634 | 182 MOVII mpr,accel_DZ ; copy result to accel_DZ |
183 bsf SSP1CON2,PEN ; stop condition | |
643 | 184 rcall WaitMSSP ; wait for TX to complete |
185 return | |
0 | 186 |
187 | |
427 | 188 I2C_RX_accelerometer_compass1: |
623 | 189 bsf SSP1CON2,SEN ; start condition |
634 | 190 rcall WaitMSSP ; wait for TX to complete |
582 | 191 movlw 0x3C ; address |
643 | 192 movff WREG,i2c_error_vault+0 ; Store address |
634 | 193 rcall I2C_TX ; send byte |
582 | 194 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) |
634 | 195 rcall I2C_TX ; send byte |
623 | 196 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 197 rcall WaitMSSP ; wait for TX to complete |
582 | 198 movlw 0x3D ; address |
634 | 199 |
200 I2C_RX_accelerometer_common: ; common part for compass 1,2 and 3 | |
201 rcall I2C_TX ; send byte | |
582 | 202 |
203 ; Non-flipped screen: | |
204 ; Chip orientation on the PCB requires | |
205 ; Original = Corrected | |
206 ; x = -x (Compass 1) | |
207 ; x = x (Compass 2) | |
208 ; y = -y | |
209 ; z = -z | |
210 | |
211 ; Flipped screen: | |
212 ; Chip orientation on the PCB requires | |
213 ; Original = Corrected | |
214 ; x = x (Compass 1) | |
215 ; x = -x (Compass 2) | |
216 ; y = y | |
217 ; z = -z | |
427 | 218 |
582 | 219 ; Dump the accelerator data |
634 | 220 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
582 | 221 movff SSP1BUF,lo ; accel_DX+0 |
634 | 222 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 223 movff SSP1BUF,hi ; accel_DX+1 |
582 | 224 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only |
225 btfss compass_type2 ; compass 2? | |
623 | 226 bra I2C_RX_accelerometer1_c1 ; NO - compass 1 |
582 | 227 ; compass 2 |
623 | 228 btfss flip_screen ; 180° rotation? |
229 bra I2C_RX_accelerometer2_c1 ; NO - continue with normal compass1 routines for Y and Z | |
582 | 230 ; flipped compass 2, negate x |
634 | 231 comf hi ; YES - 16 bit sign change |
232 negf lo ; - ... | |
233 btfsc STATUS,C ; - carry to propagate? | |
234 incf hi,F ; YES - do it | |
235 bra I2C_RX_accelerometer2_c1 ; - continue with normal compass1 routines for Y and Z | |
582 | 236 |
237 I2C_RX_accelerometer1_c1: | |
623 | 238 btfsc flip_screen ; 180° rotation? |
239 bra I2C_RX_accelerometer2_c1 ; YES | |
582 | 240 ; non-flipped compass 1, negate x |
634 | 241 comf hi ; NO - 16 bit sign change |
242 negf lo ; - ... | |
243 btfsc STATUS,C ; - carry to propagate? | |
244 incf hi,F ; YES - do it | |
427 | 245 I2C_RX_accelerometer2_c1: |
582 | 246 ; flipped compass 1, non-flipped compass 2 |
623 | 247 MOVII mpr,accel_DX ; copy result |
634 | 248 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
249 movff SSP1BUF,lo ; copy accel_DY+0 to lo | |
250 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
251 movff SSP1BUF,hi ; copy accel_DY+1 to hi | |
252 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed) only | |
623 | 253 btfsc flip_screen ; 180° rotation? |
254 bra I2C_RX_accelerometer3_c1 ; YES | |
634 | 255 comf hi ; NO - 16 bit sign change |
256 negf lo ; - ... | |
257 btfsc STATUS,C ; - carry to propagate? | |
258 incf hi,F ; YES - do it | |
427 | 259 I2C_RX_accelerometer3_c1: |
623 | 260 MOVII mpr,accel_DY ; copy result |
634 | 261 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 262 movff SSP1BUF,lo ; accel_DZ+0 |
634 | 263 bsf SSP1CON2,RCEN ; enable receive mode |
264 rcall WaitMSSP ; wait for TX to complete | |
265 ; according to data sheet there should be no master Acknowledge for the last byte (accel_DZ+1)... | |
623 | 266 movff SSP1BUF,hi ; accel_DZ+1 |
267 bsf SSP1CON2,PEN ; stop condition | |
634 | 268 rcall WaitMSSP ; wait for TX to complete |
582 | 269 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only |
623 | 270 comf hi ; 16 bit sign change for Z |
634 | 271 negf lo ; ... |
623 | 272 btfsc STATUS,C ; carry to propagate? |
273 incf hi,F ; YES - do it | |
274 MOVII mpr,accel_DZ ; copy result | |
634 | 275 return ; done |
276 | |
582 | 277 |
560 | 278 I2C_RX_accelerometer_compass2: |
623 | 279 bsf SSP1CON2,SEN ; start condition |
634 | 280 rcall WaitMSSP ; wait for TX to complete |
582 | 281 movlw 0x32 ; address |
643 | 282 movff WREG,i2c_error_vault+0 ; Store address |
634 | 283 rcall I2C_TX ; send byte |
582 | 284 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) |
634 | 285 rcall I2C_TX ; send byte |
623 | 286 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 287 rcall WaitMSSP ; wait for TX to complete |
582 | 288 movlw 0x33 ; address |
634 | 289 bra I2C_RX_accelerometer_common ; continue with common part |
624 | 290 |
291 I2C_RX_accelerometer_compass3: | |
634 | 292 bsf SSP1CON2,SEN ; start condition |
293 rcall WaitMSSP ; wait for TX to complete | |
628 | 294 movlw 0x3A ; address |
643 | 295 movff WREG,i2c_error_vault+0 ; Store address |
634 | 296 rcall I2C_TX ; send byte |
628 | 297 movlw 0x28 ; 0x28 (OUT_X_L_A) |
634 | 298 rcall I2C_TX ; send byte |
628 | 299 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 300 rcall WaitMSSP ; wait for TX to complete |
628 | 301 movlw 0x3B ; address |
634 | 302 bra I2C_RX_accelerometer_common ; continue with common part |
623 | 303 |
304 | |
305 ;----------------------------------------------------------------------------- | |
634 | 306 ; Helper Function - receive 1 Byte with Acknowledge |
307 ; | |
308 I2C_OneByteRX: | |
309 bsf SSP1CON2,RCEN ; enable receive mode | |
310 rcall WaitMSSP ; wait for TX to complete | |
311 bsf SSP1CON2,ACKEN ; send master acknowledge | |
312 bra WaitMSSP ; wait for TX to complete and return | |
313 | |
314 | |
315 ;----------------------------------------------------------------------------- | |
316 ; Read Compass | |
317 ; | |
623 | 318 IFDEF _compass |
0 | 319 |
582 | 320 global I2C_RX_compass |
0 | 321 I2C_RX_compass: |
628 | 322 btfsc compass_type3 ; compass 3 ? |
323 bra I2C_RX_compass3 ; YES | |
324 btfsc compass_type2 ; compass 2 ? | |
623 | 325 bra I2C_RX_compass2 ; YES |
628 | 326 btfsc compass_type1 ; compass 1 ? |
623 | 327 bra I2C_RX_compass1 ; YES |
628 | 328 ;bra I2C_RX_compass0 ; NO - compass 0 then |
329 | |
643 | 330 ;I2C_RX_compass0: |
623 | 331 bsf SSP1CON2,SEN ; start condition |
634 | 332 rcall WaitMSSP ; wait for TX to complete |
582 | 333 movlw 0x3C ; address |
643 | 334 movff WREG,i2c_error_vault+0 ; Store address |
634 | 335 rcall I2C_TX ; send byte |
336 movlw 0x03 ; ?? | |
337 rcall I2C_TX ; send byte | |
623 | 338 bsf SSP1CON2,PEN ; stop condition |
634 | 339 rcall WaitMSSP ; wait for TX to complete |
623 | 340 bsf SSP1CON2,SEN ; start condition |
634 | 341 rcall WaitMSSP ; wait for TX to complete |
582 | 342 movlw 0x3D ; address |
634 | 343 rcall I2C_TX ; send byte |
0 | 344 |
582 | 345 ; Compass IC sends data in following order: |
346 ; x MSB | |
347 ; x LSB | |
348 ; z MSB | |
349 ; z LSB | |
350 ; y MSB | |
351 ; y LSB | |
0 | 352 |
582 | 353 ; Non-flipped screen |
354 ; Chip orientation on the PCB requires | |
355 ; Original = Corrected | |
356 ; x = -y | |
357 ; z = z | |
358 ; y = x | |
0 | 359 |
582 | 360 ; Flipped screen |
361 ; Chip orientation on the PCB requires | |
362 ; Original = Corrected | |
363 ; x = y | |
364 ; z = z | |
365 ; y = -x | |
166
30ebaf72170d
BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents:
158
diff
changeset
|
366 |
634 | 367 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 368 movff SSP1BUF,compass_DY+1 ; data byte |
634 | 369 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 370 movff SSP1BUF,compass_DY+0 ; data byte |
371 btfsc flip_screen ; 180° rotation? | |
372 bra I2C_RX_compass0_2 ; NO | |
373 banksel compass_DY ; YES - flip Y | |
374 comf compass_DY+1 ; - 16 bit sign change | |
582 | 375 negf compass_DY+0 |
623 | 376 btfsc STATUS,C ; - carry to propagate? |
377 incf compass_DY+1,F ; YES - do it | |
582 | 378 banksel common |
623 | 379 I2C_RX_compass0_2: |
634 | 380 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 381 movff SSP1BUF,compass_DZ+1 ; data byte |
634 | 382 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 383 movff SSP1BUF,compass_DZ+0 ; data byte |
634 | 384 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 385 movff SSP1BUF,compass_DX+1 ; data byte |
628 | 386 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 387 rcall WaitMSSP ; wait for TX to complete |
623 | 388 movff SSP1BUF,compass_DX+0 ; data byte |
389 bsf SSP1CON2,PEN ; stop condition | |
634 | 390 rcall WaitMSSP ; wait for TX to complete |
623 | 391 btfss flip_screen ; 180° rotation? |
392 return ; NO - done | |
393 banksel compass_DX ; YES - flip X | |
394 comf compass_DX+1 ; - 16 bit sign change | |
582 | 395 negf compass_DX+0 |
623 | 396 btfsc STATUS,C ; - carry to propagate? |
397 incf compass_DX+1,F ; YES - do it | |
634 | 398 banksel common ; back to bank common |
399 return ; done | |
400 | |
582 | 401 |
628 | 402 I2C_RX_compass1: ; compass type 1 |
623 | 403 bsf SSP1CON2,SEN ; start condition |
634 | 404 rcall WaitMSSP ; wait for TX to complete |
582 | 405 movlw 0x3C ; address |
643 | 406 movff WREG,i2c_error_vault+0 ; Store address |
634 | 407 rcall I2C_TX ; send byte |
582 | 408 movlw b'10001000' ; 0x08 with auto-increment (MSB=1) |
634 | 409 rcall I2C_TX ; send byte |
623 | 410 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 411 rcall WaitMSSP ; wait for TX to complete |
582 | 412 movlw 0x3D ; address |
634 | 413 rcall I2C_TX ; send byte |
623 | 414 ;rcall WaitMSSP ; TODO needed? (mH) |
634 | 415 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 416 movff SSP1BUF,lo ; data byte |
634 | 417 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 418 movff SSP1BUF,hi ; data byte |
634 | 419 rcall I2C_TwoBytesRX_div8 ; divide hi,lo by 8 (signed) |
420 MOVII mpr,compass_DX ; copy result | |
623 | 421 btfss flip_screen ; 180° rotation? |
422 bra I2C_RX_compass1_1 ; NO | |
423 banksel compass_DX ; YES - flip X | |
424 comf compass_DX+1 ; - 16 bit sign change | |
582 | 425 negf compass_DX+0 |
623 | 426 btfsc STATUS,C ; - carry to propagate? |
427 incf compass_DX+1,F ; YES - do it | |
582 | 428 banksel common |
427 | 429 I2C_RX_compass1_1: |
634 | 430 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 431 movff SSP1BUF,lo ; data byte |
634 | 432 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 433 movff SSP1BUF,hi ; data byte |
634 | 434 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) |
623 | 435 MOVII mpr,compass_DY |
436 btfss flip_screen ; 180° rotation? | |
437 bra I2C_RX_compass1_2 ; NO | |
438 banksel compass_DY ; YES - flip Y | |
439 comf compass_DY+1 ; - 16 bit sign change | |
634 | 440 negf compass_DY+0 ; - ... |
623 | 441 btfsc STATUS,C ; - carry to propagate? |
442 incf compass_DY+1,F ; YES - do it | |
443 banksel common | |
427 | 444 I2C_RX_compass1_2: |
634 | 445 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 446 movff SSP1BUF,lo ; data byte |
582 | 447 bsf SSP1CON2, RCEN ; Enable receive mode |
634 | 448 rcall WaitMSSP ; wait for TX to complete |
623 | 449 movff SSP1BUF,hi ; data byte |
634 | 450 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) |
451 MOVII mpr,compass_DZ ; copy result | |
643 | 452 bsf SSP1CON2,PEN ; stop condition |
453 rcall WaitMSSP | |
454 return ; done | |
455 | |
560 | 456 |
634 | 457 |
628 | 458 I2C_RX_compass2: ; compass type 2 |
623 | 459 bsf SSP1CON2,SEN ; start condition |
634 | 460 rcall WaitMSSP ; wait for TX to complete |
582 | 461 movlw 0x3C ; address |
643 | 462 movff WREG,i2c_error_vault+0 ; Store address |
634 | 463 rcall I2C_TX ; send byte |
582 | 464 movlw 0xE8 ; 0x68 with auto-increment (MSB=1) |
634 | 465 rcall I2C_TX ; send byte |
623 | 466 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 467 rcall WaitMSSP ; wait for TX to complete |
582 | 468 movlw 0x3D ; address |
634 | 469 rcall I2C_TX ; send byte |
628 | 470 I2C_RX_compass2_xx: ; compass 3 joins in here |
634 | 471 ; rcall WaitMSSP ; wait for TX to complete (not needed, as included in I2C_TX) |
472 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
623 | 473 movff SSP1BUF,lo ; data byte |
634 | 474 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 475 movff SSP1BUF,hi ; data byte |
634 | 476 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) |
623 | 477 btfsc flip_screen ; 180° rotation? |
478 bra I2C_RX_compass2_1 ; YES - do nothing with X | |
479 ; NO - flip X | |
480 comf hi ; - 16 bit sign change | |
634 | 481 negf lo ; - ... |
623 | 482 btfsc STATUS,C ; - carry to propagate? |
483 incf hi,F ; YES - do it | |
582 | 484 I2C_RX_compass2_1: |
623 | 485 MOVII mpr,compass_DX |
634 | 486 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 487 movff SSP1BUF,lo ; data byte |
634 | 488 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 489 movff SSP1BUF,hi ; data byte |
634 | 490 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) |
623 | 491 btfss flip_screen ; 180° rotation? |
492 bra I2C_RX_compass2_2 ; NO - do nothing with Y | |
493 ; YES - flip Y | |
494 comf hi ; - 16 bit sign change | |
634 | 495 negf lo ; - ... |
623 | 496 btfsc STATUS,C ; - carry to propagate? |
497 incf hi,F ; YES - do it | |
498 I2C_RX_compass2_2: | |
499 MOVII mpr,compass_DY | |
634 | 500 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 501 movff SSP1BUF,lo ; data byte |
634 | 502 rcall I2C_OneByteRX ; receive 1 byte with acknowledge |
623 | 503 movff SSP1BUF,hi ; data byte |
634 | 504 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) |
505 MOVII mpr,compass_DZ ; copy result | |
623 | 506 bsf SSP1CON2,PEN ; stop condition |
643 | 507 rcall WaitMSSP |
508 return ; done | |
509 | |
582 | 510 |
634 | 511 |
628 | 512 I2C_RX_compass3: ; compass type 3 |
513 bsf SSP1CON2,SEN ; start condition | |
634 | 514 rcall WaitMSSP ; wait for TX to complete |
628 | 515 movlw 0x3C ; address |
643 | 516 movff WREG,i2c_error_vault+0 ; Store address |
634 | 517 rcall I2C_TX ; send byte |
628 | 518 movlw 0xA8 ; 0x28 with auto-increment (MSB=1) |
634 | 519 rcall I2C_TX ; send byte |
628 | 520 bsf SSP1CON2,RSEN ; repeated start condition (!) |
634 | 521 rcall WaitMSSP ; wait for TX to complete |
628 | 522 movlw 0x3D ; address |
634 | 523 rcall I2C_TX ; send byte |
628 | 524 bra I2C_RX_compass2_xx ; join with compass 2 code |
624 | 525 |
623 | 526 ENDIF ; _compass |
527 | |
634 | 528 |
623 | 529 ;----------------------------------------------------------------------------- |
634 | 530 ; Initialize Compass / Accelerometer Chip |
531 ; | |
582 | 532 global I2C_init_compass |
0 | 533 I2C_init_compass: |
634 | 534 bsf compass_enabled ; flag compass will be enabled |
535 bcf compass_type2 ; clear in preparation of chip detection | |
536 bcf compass_type3 ; ... | |
623 | 537 |
628 | 538 ; probe for compass 3 |
623 | 539 bsf SSP1CON2,SEN ; start condition |
634 | 540 rcall WaitMSSP ; wait for TX to complete |
628 | 541 movlw 0x3A ; address byte + write bit |
643 | 542 movff WREG,i2c_error_vault+0 ; Store address |
582 | 543 movwf SSP1BUF ; control byte |
634 | 544 rcall WaitMSSP ; wait for TX to complete |
545 btfss SSP1CON2,ACKSTAT ; ACK received? | |
546 bsf compass_type3 ; YES - ACK was send, compass 3 found | |
628 | 547 bsf SSP1CON2,PEN ; stop condition |
634 | 548 rcall WaitMSSP ; wait for TX to complete |
628 | 549 |
550 btfsc compass_type3 ; compass 3 found? | |
551 bra I2C_init_compass3 ; YES - initialize compass 3 | |
552 | |
553 ; probe for compass 2 | |
554 bsf SSP1CON2,SEN ; start condition | |
634 | 555 rcall WaitMSSP ; wait for TX to complete |
628 | 556 movlw 0x32 ; address byte + write bit |
643 | 557 movff WREG,i2c_error_vault+0 ; Store address |
628 | 558 movwf SSP1BUF ; control byte |
634 | 559 rcall WaitMSSP ; wait for TX to complete |
560 btfss SSP1CON2,ACKSTAT ; ACK received? | |
561 bsf compass_type2 ; YES - ACK send, compass 2 found | |
628 | 562 bsf SSP1CON2,PEN ; stop condition |
634 | 563 rcall WaitMSSP ; wait for TX to complete |
623 | 564 |
628 | 565 btfsc compass_type2 ; compass 2 found? |
566 bra I2C_init_compass2 ; YES - initialize compass 2 | |
567 | |
568 ; probe for compass 0 or 1 | |
634 | 569 bsf compass_type1 ; assume compass 1 by default |
623 | 570 bsf SSP1CON2,SEN ; start condition |
634 | 571 rcall WaitMSSP ; wait for TX to complete |
582 | 572 movlw 0x3C ; address |
643 | 573 movff WREG,i2c_error_vault+0 ; Store address |
634 | 574 rcall I2C_TX ; send byte |
575 movlw 0x0F ; ?? | |
576 rcall I2C_TX ; send byte | |
623 | 577 bsf SSP1CON2,PEN ; stop condition |
634 | 578 rcall WaitMSSP ; wait for TX to complete |
623 | 579 bsf SSP1CON2,SEN ; start condition |
634 | 580 rcall WaitMSSP ; wait for TX to complete |
582 | 581 movlw 0x3D ; address |
634 | 582 rcall I2C_TX ; send byte |
583 rcall I2C_OneByteRX ; receive 1 byte with acknowledge | |
628 | 584 movlw 0x49 ; 0x49 = compass 1 |
634 | 585 cpfseq SSP1BUF ; 0x49 received? |
586 bcf compass_type1 ; NO - clear flag for compass 1 | |
623 | 587 bsf SSP1CON2,PEN ; stop condition |
634 | 588 rcall WaitMSSP ; wait for TX to complete |
582 | 589 |
628 | 590 btfsc compass_type1 ; compass 1 found? |
591 bra I2C_init_compass1 ; YES - initialize compass 1 | |
592 ;bra I2C_init_compass0 ; NO - must be compass 0 then | |
593 | |
634 | 594 |
628 | 595 I2C_init_compass0: |
596 ; magnetic | |
623 | 597 bsf SSP1CON2,SEN ; start condition |
634 | 598 rcall WaitMSSP ; wait for TX to complete |
582 | 599 movlw 0x3C ; address |
643 | 600 movff WREG,i2c_error_vault+0 ; Store address |
634 | 601 rcall I2C_TX ; send byte |
602 movlw 0x00 ; ?? | |
603 rcall I2C_TX ; send byte | |
623 | 604 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged |
634 | 605 rcall I2C_TX ; send byte |
623 | 606 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss) |
634 | 607 swapf i2c_temp1,F ; |
608 comf i2c_temp1,F ; | |
609 bcf STATUS,C ; | |
610 rlcf i2c_temp1 ; | |
611 movf i2c_temp1,W ; | |
612 rcall I2C_TX ; send byte | |
613 movlw b'00000000' ; select continuous mode | |
614 rcall I2C_TX ; send byte | |
623 | 615 bsf SSP1CON2,PEN ; stop condition |
634 | 616 rcall WaitMSSP ; wait for TX to complete |
617 | |
628 | 618 ; accelerometer |
631 | 619 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode |
628 | 620 |
621 bsf SSP1CON2,SEN ; start condition | |
634 | 622 rcall WaitMSSP ; wait for TX to complete |
628 | 623 movlw 0x38 ; address |
643 | 624 movff WREG,i2c_error_vault+0 ; Store address |
634 | 625 rcall I2C_TX ; send byte |
628 | 626 movlw 0x0E ; XYZ_DATA_CFG |
634 | 627 rcall I2C_TX ; send byte |
628 | 628 movlw b'00000000' ; high pass filter = 0, +/- 2 g range |
634 | 629 rcall I2C_TX ; send byte |
628 | 630 bsf SSP1CON2,PEN ; stop condition |
634 | 631 rcall WaitMSSP ; wait for TX to complete |
628 | 632 bsf SSP1CON2,SEN ; start condition |
634 | 633 rcall WaitMSSP ; wait for TX to complete |
628 | 634 movlw 0x38 ; address |
634 | 635 rcall I2C_TX ; send byte |
628 | 636 movlw 0x2A ; CTRL_REG1 |
634 | 637 rcall I2C_TX ; send byte |
628 | 638 ; movlw b'00110000' ; CTRL_REG1: 160 ms data rate, standby mode |
639 movlw b'00110100' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode | |
634 | 640 rcall I2C_TX ; send byte |
628 | 641 movlw b'00000010' ; CTRL_REG2: high-res in active mode |
634 | 642 rcall I2C_TX ; send byte |
628 | 643 bsf SSP1CON2,PEN ; stop condition |
634 | 644 rcall WaitMSSP ; wait for TX to complete |
628 | 645 |
646 bsf SSP1CON2,SEN ; start condition | |
634 | 647 rcall WaitMSSP ; wait for TX to complete |
628 | 648 movlw 0x38 ; address |
634 | 649 rcall I2C_TX ; send byte |
628 | 650 movlw 0x2A ; CTRL_REG1 |
634 | 651 rcall I2C_TX ; send byte |
628 | 652 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode |
653 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode | |
634 | 654 rcall I2C_TX ; send byte |
628 | 655 bsf SSP1CON2,PEN ; stop condition |
643 | 656 rcall WaitMSSP ; wait for TX to complete |
657 return ; done | |
658 | |
0 | 659 |
628 | 660 |
427 | 661 I2C_init_compass1: |
623 | 662 bsf SSP1CON2,SEN ; start condition |
634 | 663 rcall WaitMSSP ; wait for TX to complete |
582 | 664 movlw 0x3C ; address |
643 | 665 movff WREG,i2c_error_vault+0 ; Store address |
634 | 666 rcall I2C_TX ; send byte |
582 | 667 movlw 0x9F ; 1F with auto-increment (MSB=1) |
634 | 668 rcall I2C_TX ; send byte |
582 | 669 movlw b'00000000' ; CTRL0 |
634 | 670 rcall I2C_TX ; send byte |
623 | 671 movlw b'00101111' ; CTRL1 (6.25 Hz, BDU=0, x,y,z = ON) |
634 | 672 rcall I2C_TX ; send byte |
623 | 673 movlw b'11000000' ; CTRL2 (50 Hz, +/- 2g) |
634 | 674 rcall I2C_TX ; send byte |
582 | 675 movlw b'00000000' ; CTRL3 |
634 | 676 rcall I2C_TX ; send byte |
582 | 677 movlw b'00000000' ; CTRL4 |
634 | 678 rcall I2C_TX ; send byte |
623 | 679 movlw b'01100100' ; CTRL5 HIGH res, 6.25 Hz |
634 | 680 rcall I2C_TX ; send byte |
623 | 681 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss) |
682 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss) | |
582 | 683 dcfsnz i2c_temp1,F ; = 1? |
623 | 684 movlw b'01100000' ; YES - CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss) |
582 | 685 dcfsnz i2c_temp1,F ; = 2? |
623 | 686 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss) |
582 | 687 dcfsnz i2c_temp1,F ; = 3? |
623 | 688 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss) |
582 | 689 dcfsnz i2c_temp1,F ; = 4? |
623 | 690 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss) |
582 | 691 dcfsnz i2c_temp1,F ; = 5? |
623 | 692 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss) |
582 | 693 dcfsnz i2c_temp1,F ; = 6? |
623 | 694 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss) |
582 | 695 dcfsnz i2c_temp1,F ; = 7? |
623 | 696 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss) |
634 | 697 rcall I2C_TX ; send byte |
582 | 698 movlw b'00000000' ; CTRL7 Continuous Mode |
634 | 699 rcall I2C_TX ; send byte |
623 | 700 bsf SSP1CON2,PEN ; stop condition |
643 | 701 rcall WaitMSSP ; wait for TX to complete |
702 return ; done | |
634 | 703 |
704 ; accelerometer initializes along with magnetic sensor | |
705 | |
582 | 706 |
560 | 707 I2C_init_compass2: |
628 | 708 ; magnetic |
634 | 709 bsf SSP1CON2,SEN ; start condition |
710 rcall WaitMSSP ; wait for TX to complete | |
711 movlw 0x3C ; address | |
643 | 712 movff WREG,i2c_error_vault+0 ; Store address |
634 | 713 rcall I2C_TX ; send byte |
714 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) | |
715 rcall I2C_TX ; send byte | |
716 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 0x00 | |
717 rcall I2C_TX ; send byte | |
718 movlw b'00000011' ; CFG_REG_B_M (low-pass filter enabled) 0x61 (set pulse is released every 63 ODR) | |
719 rcall I2C_TX ; send byte | |
720 movlw b'00010000' ; CFG_REG_C_M BDU=1 0x62 0x57 | |
721 rcall I2C_TX ; send byte | |
722 bsf SSP1CON2,PEN ; stop condition | |
723 rcall WaitMSSP ; wait for TX to complete | |
724 | |
628 | 725 ; accelerometer |
623 | 726 bsf SSP1CON2,SEN ; start condition |
634 | 727 rcall WaitMSSP ; wait for TX to complete |
628 | 728 movlw 0x32 ; address |
643 | 729 movff WREG,i2c_error_vault+0 ; Store address |
634 | 730 rcall I2C_TX ; send byte |
628 | 731 movlw 0x9F ; 1F with auto-increment (MSB=1) |
634 | 732 rcall I2C_TX ; send byte |
628 | 733 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) |
634 | 734 rcall I2C_TX ; send byte |
628 | 735 movlw b'00100111' ; CTRL_REG1_A (10Hz, x,y,z = ON) |
634 | 736 rcall I2C_TX ; send byte |
628 | 737 movlw b'00000000' ; CTRL_REG2_A |
634 | 738 rcall I2C_TX ; send byte |
628 | 739 movlw b'00000000' ; CTRL_REG3_A |
634 | 740 rcall I2C_TX ; send byte |
628 | 741 movlw b'00001000' ; CTRL_REG4_A (BDU=0, +/-2g, HR=1) |
634 | 742 rcall I2C_TX ; send byte |
628 | 743 ; movlw b'00000000' ; CTRL_REG5_A |
634 | 744 ; rcall I2C_TX ; send byte |
623 | 745 bsf SSP1CON2,PEN ; stop condition |
643 | 746 rcall WaitMSSP ; wait for TX to complete |
747 return ; done | |
582 | 748 |
628 | 749 |
624 | 750 I2C_init_compass3: |
628 | 751 ; magnetic |
631 | 752 bsf SSP1CON2,SEN ; start condition |
634 | 753 rcall WaitMSSP ; wait for TX to complete |
631 | 754 movlw 0x3C ; address |
643 | 755 movff WREG,i2c_error_vault+0 ; Store address |
634 | 756 rcall I2C_TX ; send byte |
631 | 757 movlw 0xA0 ; 0x20 with auto-increment (MSB=1) |
634 | 758 rcall I2C_TX ; send byte |
645 | 759 movlw b'01110000' ; CTRL_REG1_M (10Hz, X and Y in Ultra-high performance mode) 0x20 |
634 | 760 rcall I2C_TX ; send byte |
631 | 761 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21 |
634 | 762 rcall I2C_TX ; send byte |
631 | 763 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22 |
634 | 764 rcall I2C_TX ; send byte |
645 | 765 movlw b'00001100' ; CTRL_REG4_M (Z in Ultra-high performance mode) 0x23 |
634 | 766 rcall I2C_TX ; send byte |
631 | 767 movlw b'00000000' ; CTRL_REG5_M 0x24 |
634 | 768 rcall I2C_TX ; send byte |
631 | 769 movlw b'00000000' ; CTRL_REG5_M 0x24 |
634 | 770 rcall I2C_TX ; send byte |
631 | 771 bsf SSP1CON2,PEN ; Stop condition |
634 | 772 rcall WaitMSSP ; wait for TX to complete |
631 | 773 |
628 | 774 ;accelerometer |
631 | 775 bsf SSP1CON2,SEN ; start condition |
634 | 776 rcall WaitMSSP ; wait for TX to complete |
631 | 777 movlw 0x3A ; address |
643 | 778 movff WREG,i2c_error_vault+0 ; Store address |
634 | 779 rcall I2C_TX ; send byte |
628 | 780 movlw 0x20 |
634 | 781 rcall I2C_TX ; send byte |
631 | 782 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20 |
634 | 783 rcall I2C_TX ; send byte |
631 | 784 movlw b'00000000' ; CTRL_REG2_A 0x21 |
634 | 785 rcall I2C_TX ; send byte |
631 | 786 movlw b'00000000' ; CTRL_REG3_A 0x22 |
634 | 787 rcall I2C_TX ; send byte |
631 | 788 movlw b'11001100' ; CTRL_REG4_A 0x23 |
634 | 789 rcall I2C_TX ; send byte |
790 bsf SSP1CON2,PEN ; stop condition | |
643 | 791 rcall WaitMSSP ; wait for TX to complete |
792 return ; done | |
628 | 793 |
643 | 794 ;----------------------------------------------------------------------------- |
795 ; Helper Function - send 1 Byte, wait for end of transmission and check ackn | |
796 ; | |
797 I2C_TX: | |
798 movwf SSP1BUF ; put byte to be sent into TX buffer | |
799 rcall WaitMSSP ; wait for TX to complete | |
800 bra I2C_Check_ACK ; check for acknowledge by receiver and return | |
582 | 801 |
634 | 802 ;----------------------------------------------------------------------------- |
803 ; Deactivate Compass / Accelerometer | |
804 ; | |
582 | 805 global I2C_sleep_compass |
0 | 806 I2C_sleep_compass: |
628 | 807 btfss compass_enabled ; compass active? |
634 | 808 return ; NO - done |
623 | 809 bcf compass_enabled |
628 | 810 btfsc compass_type3 ; compass 3 ? |
624 | 811 bra I2C_sleep_compass3 ; YES |
628 | 812 btfsc compass_type2 ; compass 2 ? |
623 | 813 bra I2C_sleep_compass2 ; YES |
628 | 814 btfsc compass_type1 ; compass 1 ? |
623 | 815 bra I2C_sleep_compass1 ; YES |
628 | 816 ;bra I2C_sleep_compass0 ; NO - must be compass 0 then |
817 | |
634 | 818 |
560 | 819 I2C_sleep_compass0: |
628 | 820 ; magnetic |
623 | 821 bsf SSP1CON2,SEN ; start condition |
634 | 822 rcall WaitMSSP ; wait for TX to complete |
582 | 823 movlw 0x3C ; address |
643 | 824 movff WREG,i2c_error_vault+0 ; Store address |
634 | 825 rcall I2C_TX ; send byte |
826 movlw 0x00 ; ?? | |
827 rcall I2C_TX ; send byte | |
582 | 828 movlw b'01101000' ; ConfigA |
634 | 829 rcall I2C_TX ; send byte |
582 | 830 movlw b'00100000' ; ConfigB |
634 | 831 rcall I2C_TX ; send byte |
623 | 832 movlw b'00000010' ; idle mode |
634 | 833 rcall I2C_TX ; send byte |
623 | 834 bsf SSP1CON2,PEN ; stop condition |
634 | 835 rcall WaitMSSP ; wait for TX to complete |
631 | 836 |
634 | 837 I2C_sleep_accelerometer0: |
628 | 838 ; accelerometer |
839 bsf SSP1CON2,SEN ; start condition | |
634 | 840 rcall WaitMSSP ; wait for TX to complete |
628 | 841 movlw 0x38 ; address |
643 | 842 movff WREG,i2c_error_vault+0 ; Store address |
634 | 843 rcall I2C_TX ; send byte |
628 | 844 movlw 0x2A ; CTRL_REG1 |
634 | 845 rcall I2C_TX ; send byte |
628 | 846 movlw b'00000000' ; standby mode |
634 | 847 rcall I2C_TX ; send byte |
628 | 848 bsf SSP1CON2,PEN ; stop condition |
643 | 849 rcall WaitMSSP ; wait for TX to complete |
850 return ; done | |
634 | 851 |
0 | 852 |
427 | 853 I2C_sleep_compass1: |
623 | 854 bsf SSP1CON2,SEN ; start condition |
634 | 855 rcall WaitMSSP ; wait for TX to complete |
582 | 856 movlw 0x3C ; address |
643 | 857 movff WREG,i2c_error_vault+0 ; Store address |
634 | 858 rcall I2C_TX ; send byte |
582 | 859 movlw 0x20 ; CTRL_REG1 |
634 | 860 rcall I2C_TX ; send byte |
623 | 861 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode |
634 | 862 rcall I2C_TX ; send byte |
623 | 863 bsf SSP1CON2,PEN ; stop condition |
634 | 864 rcall WaitMSSP ; wait for TX to complete |
623 | 865 bsf SSP1CON2,SEN ; start condition |
634 | 866 rcall WaitMSSP ; wait for TX to complete |
582 | 867 movlw 0x3C ; address |
634 | 868 rcall I2C_TX ; send byte |
582 | 869 movlw 0x26 ; CTRL_REG7 |
634 | 870 rcall I2C_TX ; send byte |
623 | 871 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode |
634 | 872 rcall I2C_TX ; send byte |
623 | 873 bsf SSP1CON2,PEN ; stop condition |
643 | 874 rcall WaitMSSP ; wait for TX to complete |
875 return ; done | |
634 | 876 |
877 ; accelerometer sleeps with magnetic sensor | |
631 | 878 |
560 | 879 |
880 I2C_sleep_compass2: | |
623 | 881 ; magnetic |
882 bsf SSP1CON2,SEN ; start condition | |
634 | 883 rcall WaitMSSP ; wait for TX to complete |
623 | 884 movlw 0x3C ; address |
643 | 885 movff WREG,i2c_error_vault+0 ; Store address |
634 | 886 rcall I2C_TX ; send byte |
623 | 887 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) |
634 | 888 rcall I2C_TX ; send byte |
623 | 889 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode)) |
634 | 890 rcall I2C_TX ; send byte |
623 | 891 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition) |
634 | 892 rcall I2C_TX ; send byte |
623 | 893 movlw b'01010001' ; CFG_REG_C_M 0x62 |
634 | 894 rcall I2C_TX ; send byte |
623 | 895 movlw b'00000000' ; INT_CTRL_REG_M 0x63 |
634 | 896 rcall I2C_TX ; send byte |
623 | 897 bsf SSP1CON2,PEN ; stop condition |
634 | 898 rcall WaitMSSP ; wait for TX to complete |
582 | 899 |
623 | 900 ; accelerometer |
901 bsf SSP1CON2,SEN ; start condition | |
634 | 902 rcall WaitMSSP ; wait for TX to complete |
623 | 903 movlw 0x32 ; address |
643 | 904 movff WREG,i2c_error_vault+0 ; Store address |
634 | 905 rcall I2C_TX ; send byte |
623 | 906 movlw 0x9F ; 1F with auto-increment (MSB=1) |
634 | 907 rcall I2C_TX ; send byte |
623 | 908 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off) |
634 | 909 rcall I2C_TX ; send byte |
623 | 910 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off) |
634 | 911 rcall I2C_TX ; send byte |
623 | 912 bsf SSP1CON2,PEN ; stop condition |
643 | 913 rcall WaitMSSP ; wait for TX to complete |
914 return ; done | |
634 | 915 |
623 | 916 |
628 | 917 I2C_sleep_compass3: |
918 ; magnetic | |
919 bsf SSP1CON2,SEN ; start condition | |
634 | 920 rcall WaitMSSP ; wait for TX to complete |
628 | 921 movlw 0x3C ; address |
643 | 922 movff WREG,i2c_error_vault+0 ; Store address |
634 | 923 rcall I2C_TX ; send byte |
628 | 924 movlw 0xA2 ; 0x22 with auto-increment (MSB=1) |
634 | 925 rcall I2C_TX ; send byte |
628 | 926 movlw b'01000010' ; CTRL_REG3_M (power-down) 0x22 |
634 | 927 rcall I2C_TX ; send byte |
628 | 928 bsf SSP1CON2,PEN ; stop condition |
634 | 929 rcall WaitMSSP ; wait for TX to complete |
628 | 930 |
931 ; accelerometer | |
932 bsf SSP1CON2,SEN ; start condition | |
634 | 933 rcall WaitMSSP ; wait for TX to complete |
628 | 934 movlw 0x3A ; address |
643 | 935 movff WREG,i2c_error_vault+0 ; Store address |
634 | 936 rcall I2C_TX ; send byte |
624 | 937 movlw 0x20 |
634 | 938 rcall I2C_TX ; send byte |
628 | 939 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20 |
634 | 940 rcall I2C_TX ; send byte |
628 | 941 bsf SSP1CON2,PEN ; stop condition |
643 | 942 rcall WaitMSSP ; wait for TX to complete |
943 return ; done | |
634 | 944 |
0 | 945 |
634 | 946 ;----------------------------------------------------------------------------- |
947 ; Helper Function - wait for TX to complete | |
948 ; | |
615 | 949 WaitMSSP: |
643 | 950 movff SSP1BUF,i2c_error_vault+1 |
634 | 951 clrf i2c_temp1 ; wait for max 256 loops |
952 WaitMSSP_loop: | |
953 decfsz i2c_temp1,F ; decrement loop counter, timeout? | |
954 bra WaitMSSP2 ; NO | |
955 bra I2CFail ; YES | |
615 | 956 WaitMSSP2: |
634 | 957 btfss PIR1,SSP1IF ; TX completed? |
958 bra WaitMSSP_loop ; NO - loop | |
959 bcf PIR1,SSP1IF ; YES - clear TX completion flag | |
960 return ; - done | |
961 | |
615 | 962 |
634 | 963 ;----------------------------------------------------------------------------- |
964 ; Helper Function - check for Acknowledge by Receiver | |
965 ; | |
966 I2C_Check_ACK: | |
628 | 967 btfss SSP1CON2,ACKSTAT ; ACK received from slave? |
634 | 968 return ; YES - done |
969 ;bra I2CFail ; NO - do some clean up | |
970 | |
971 | |
972 ;----------------------------------------------------------------------------- | |
973 ; Helper Function - clean up I2C Interface after missing Acknowledge | |
974 ; | |
975 I2CFail: | |
976 bsf active_reset_ostc_rx ; reset RX circuitry (which may be the cause for the hang up) | |
977 rcall I2CReset ; reset I2C | |
978 bcf PIR1,SSP1IF ; clear TX completion flag | |
979 bsf i2c_error_flag ; set error flag | |
980 bcf active_reset_ostc_rx ; release reset from RX circuitry | |
643 | 981 ; bcf i2c_busy_temperature |
982 ; bcf i2c_busy_pressure | |
634 | 983 return ; done |
615 | 984 |
634 | 985 |
986 ;----------------------------------------------------------------------------- | |
987 ; Helper Function - Reset I2C Module | |
988 ; | |
989 ; recover in case something went wrong, i.g. slave holds SDA low | |
990 ; | |
991 I2CReset: | |
992 clrf SSP1CON1 ; reset entire module | |
993 clrf SSP1CON2 ; ... | |
994 clrf SSP1STAT ; ... | |
995 bcf TRISC,3 ; SCL as OUTPUT | |
996 bsf TRISC,4 ; SDA as input | |
997 bcf PORTC,3 ; SCL = 0 | |
998 movlw d'9' ; clock-out 9 clock cycles manually | |
999 movwf i2c_temp1 ; ... | |
615 | 1000 I2CReset_1: |
1001 bsf PORTC,3 ; SCL = 1 | |
634 | 1002 nop ; pause for 4 CPU cycles |
1003 nop ; ... | |
1004 nop ; ... | |
1005 nop ; ... | |
615 | 1006 btfsc PORTC,4 ; SDA = 1 ? |
634 | 1007 bra I2CReset_2 ; YES - SDA has been released from slave |
615 | 1008 bcf PORTC,3 ; NO - set SCL = 0 |
634 | 1009 nop ; - pause for 2 CPU cycles |
1010 nop ; - ... | |
1011 bcf PORTC,3 ; - SCL = 0 | |
1012 nop ; - pause for 2 CPU cycles | |
1013 nop ; - ... | |
1014 decfsz i2c_temp1,F ; - clock counter, all cycles done? | |
1015 bra I2CReset_1 ; NO - loop | |
615 | 1016 I2CReset_2: |
634 | 1017 bsf TRISC,3 ; SCL as input |
1018 clrf SSP1CON1 ; setup I2C mode | |
1019 WAITMS d'10' ; wait 10 ms (reset-timeout for I2C devices) | |
1020 movlw b'00000000' ; enable slew rate control | |
1021 movwf SSP1STAT ; ... | |
1022 movlw b'00101000' ; configure I2C module | |
1023 movwf SSP1CON1 ; ... | |
1024 movlw b'00000000' ; ... | |
1025 movwf SSP1CON2 ; ... | |
643 | 1026 movlw i2c_speed_value |
634 | 1027 movwf SSP1ADD ; ... |
1028 return ; done | |
623 | 1029 |
1030 | |
634 | 1031 ;----------------------------------------------------------------------------- |
1032 ; Helper Function - Initialize Gauge IC again after an UVLO Event | |
1033 ; | |
556
dd28d4efd4d2
fix a potential issue in the battery managment
heinrichsweikamp
parents:
498
diff
changeset
|
1034 lt2942_init_again: |
623 | 1035 movlw 0x02 ; point to accumulated charge registers |
634 | 1036 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
623 | 1037 movff battery_accumulated_charge+1,SSP1BUF ; data byte |
634 | 1038 rcall WaitMSSP ; wait for TX to complete |
1039 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1040 movff battery_accumulated_charge+0,SSP1BUF ; data byte |
634 | 1041 rcall WaitMSSP ; wait for TX to complete |
1042 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1043 bsf SSP1CON2,PEN ; stop condition |
634 | 1044 rcall WaitMSSP ; wait for TX to complete |
1045 MOVII battery_accumulated_charge,sub_a ; copy result to sub_a | |
1046 ;bra lt2942_init ; and initialize again... | |
560 | 1047 |
628 | 1048 |
634 | 1049 ;----------------------------------------------------------------------------- |
1050 ; Initialize Gauge IC | |
1051 ; | |
582 | 1052 global lt2942_init |
634 | 1053 lt2942_init: |
623 | 1054 movlw 0x01 ; point to control reg B |
634 | 1055 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
623 | 1056 movlw b'11111000' ; automatic conversion every two seconds |
640 | 1057 movwf SSP1BUF ; data byte |
634 | 1058 rcall WaitMSSP ; wait for TX to complete |
1059 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1060 bsf SSP1CON2,PEN ; stop condition |
643 | 1061 rcall WaitMSSP ; wait for TX to complete |
1062 return ; done | |
113 | 1063 |
640 | 1064 ;----------------------------------------------------------------------------- |
1065 ; Sleep Gauge IC | |
1066 ; | |
1067 global lt2942_sleep | |
1068 lt2942_sleep: | |
1069 movlw 0x01 ; point to control reg B | |
1070 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC | |
1071 movlw b'00111000' ; sleep | |
1072 movwf SSP1BUF ; data byte | |
1073 rcall WaitMSSP ; wait for TX to complete | |
1074 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1075 bsf SSP1CON2,PEN ; stop condition | |
643 | 1076 rcall WaitMSSP ; wait for TX to complete |
1077 return ; done | |
628 | 1078 |
634 | 1079 ;----------------------------------------------------------------------------- |
1080 ; Read Gauge IC - Status Register | |
1081 ; | |
113 | 1082 global lt2942_get_status |
634 | 1083 lt2942_get_status: |
623 | 1084 bcf battery_gauge_available ; clear flag |
1085 movlw 0x00 ; point to status register | |
634 | 1086 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
1087 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC | |
643 | 1088 |
1089 bsf SSP1CON2,ACKDT ; set ACKDT flag | |
1090 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1091 rcall WaitMSSP ; wait for TX to complete | |
1092 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1093 | |
634 | 1094 movff SSP1BUF,WREG ; copy received byte to WREG |
582 | 1095 btfss WREG,7 ; 2942 found? |
623 | 1096 bsf battery_gauge_available ; YES - set flag |
1097 bsf SSP1CON2,PEN ; stop condition | |
643 | 1098 rcall WaitMSSP ; wait for TX to complete |
1099 return ; done | |
113 | 1100 |
1101 | |
634 | 1102 ;----------------------------------------------------------------------------- |
1103 ; Read Gauge IC - Voltage | |
1104 ; | |
113 | 1105 global lt2942_get_voltage |
634 | 1106 lt2942_get_voltage: |
623 | 1107 movlw 0x08 ; point to voltage registers |
634 | 1108 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
1109 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC | |
623 | 1110 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1111 rcall WaitMSSP ; wait for TX to complete |
1112 movff SSP1BUF,xA+1 ; copy received byte to xA+1 | |
1113 bsf SSP1CON2,RCEN ; enable receive mode | |
1114 rcall WaitMSSP ; wait for TX to complete | |
1115 movff SSP1BUF,xA+0 ; copy received byte to xA+0 | |
643 | 1116 bsf SSP1CON2,ACKDT ; set ACKDT flag |
1117 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1118 rcall WaitMSSP ; wait for TX to complete | |
1119 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1120 | |
623 | 1121 bsf SSP1CON2,PEN ; stop condition |
634 | 1122 rcall WaitMSSP ; wait for TX to complete |
643 | 1123 |
623 | 1124 ; convert voltage from raw value to Volt |
1125 MOVLI .6000,xB ; load conversion multiplicand into xB | |
1126 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand | |
1127 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % | |
1128 movff xC+2,batt_voltage+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result | |
1129 movff xC+3,batt_voltage+1 ; ... | |
615 | 1130 |
623 | 1131 tstfsz batt_voltage+1 ; < 256 mV ? |
1132 return ; NO - done | |
634 | 1133 bra lt2942_init ; YES - initialize gauge and return |
623 | 1134 |
615 | 1135 |
634 | 1136 ;----------------------------------------------------------------------------- |
1137 ; Read Gauge IC - Temperature | |
1138 ; | |
623 | 1139 global lt2942_get_temperature |
1140 lt2942_get_temperature: ; read battery temperature | |
1141 movlw 0x0C ; point to temperature register | |
643 | 1142 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
1143 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC | |
623 | 1144 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1145 rcall WaitMSSP ; wait for TX to complete |
623 | 1146 movff SSP1BUF,xA+1 ; store raw temperature, high byte |
628 | 1147 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 1148 rcall WaitMSSP ; wait for TX to complete |
623 | 1149 movff SSP1BUF,xA+0 ; store raw temperature, low byte |
643 | 1150 bsf SSP1CON2,ACKDT ; set ACKDT flag |
1151 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1152 rcall WaitMSSP ; wait for TX to complete | |
1153 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1154 | |
623 | 1155 bsf SSP1CON2,PEN ; stop condition |
634 | 1156 rcall WaitMSSP ; wait for TX to complete |
623 | 1157 |
1158 ; convert temperature from raw value to Kelvin | |
1159 MOVLI .6000,xB ; load conversion multiplicand into xB | |
1160 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand | |
1161 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % | |
1162 movff xC+2,battery_temperature+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result | |
1163 movff xC+3,battery_temperature+1 ; ... | |
1164 | |
634 | 1165 ; check if battery is being charged right now |
623 | 1166 btfss cc_active ; in CC charging mode? |
1167 return ; NO - not charging, done | |
1168 | |
640 | 1169 ; ignore false readings (>125°C) |
1170 movlw LOW .3307 | |
1171 movwf sub_a+0 | |
1172 movlw HIGH .3307 | |
1173 movwf sub_a+1 | |
1174 MOVII battery_temperature, sub_b | |
1175 call cmpU16 ; sub_a - sub_b (with UNSIGNED values) | |
1176 btfsc neg_flag ; result negative? | |
1177 return ; YES - temperature > 125°C, not possible here. Skip test. | |
1178 | |
623 | 1179 ; check for over-temperature while charging |
1180 MOVLI max_battery_charge_temp,sub_a | |
1181 MOVII battery_temperature, sub_b | |
1182 call cmpU16 ; sub_a - sub_b (with UNSIGNED values) | |
1183 btfss neg_flag ; result negative? | |
1184 return ; NO - temperature <= threshold, ok, done | |
640 | 1185 return |
623 | 1186 ; YES - too hot, disable charging circuitry |
1187 bsf charge_disable ; - set charging-inhibit signal | |
1188 bcf charge_enable ; - activate charging-inhibit signal | |
1189 bsf battery_overtemp ; - flag that the battery charging over-temperature protection has tripped | |
634 | 1190 return ; - done |
623 | 1191 |
113 | 1192 |
634 | 1193 ;----------------------------------------------------------------------------- |
1194 ; Read Gauge IC - Read State of Charge | |
1195 ; | |
113 | 1196 global lt2942_get_accumulated_charge |
623 | 1197 lt2942_get_accumulated_charge: ; read accumulated charge and compute percent |
1198 movlw 0x00 ; point to status register | |
634 | 1199 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
1200 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC | |
623 | 1201 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1202 rcall WaitMSSP ; wait for TX to complete |
1203 movff SSP1BUF,gauge_status_byte ; copy received byte to gauge_status_byte | |
466 | 1204 |
634 | 1205 bsf SSP1CON2,RCEN ; enable receive mode |
1206 rcall WaitMSSP ; wait for TX to complete ; dummy read (control byte) | |
1207 movf SSP1BUF,W ; dump to WREG | |
623 | 1208 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1209 rcall WaitMSSP ; wait for TX to complete |
582 | 1210 |
634 | 1211 bsf SSP1CON2,RCEN ; enable receive mode |
1212 rcall WaitMSSP ; wait for TX to complete | |
1213 movff SSP1BUF,sub_a+1 ; copy received byte to sub_a+1 | |
623 | 1214 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1215 rcall WaitMSSP ; wait for TX to complete |
582 | 1216 |
634 | 1217 bsf SSP1CON2,RCEN ; enable receive mode |
1218 rcall WaitMSSP ; wait for TX to complete | |
1219 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0 | |
643 | 1220 bsf SSP1CON2,ACKDT ; set ACKDT flag |
1221 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1222 rcall WaitMSSP ; wait for TX to complete | |
1223 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1224 | |
623 | 1225 bsf SSP1CON2,PEN ; stop condition |
634 | 1226 rcall WaitMSSP ; wait for TX to complete |
113 | 1227 |
623 | 1228 btfsc gauge_status_byte,0 ; UVLO event ? |
634 | 1229 rcall lt2942_init_again ; YES - do an re-initialization |
623 | 1230 MOVII sub_a,battery_accumulated_charge ; save raw value |
582 | 1231 |
634 | 1232 ; Compute batt_percent = (charge - battery_offset) / 365 |
1233 MOVII battery_offset,sub_b ; get battery offset | |
582 | 1234 call subU16 ; sub_c = sub_a - sub_b (with signed values) |
634 | 1235 clrf batt_percent ; default batt_percent to zero |
582 | 1236 btfsc neg_flag ; result negative? |
623 | 1237 bra lt2942_set_to_zero_percent ; YES - keep LT2942 at zero percent and return |
113 | 1238 |
623 | 1239 ; > zero, set batt_percent properly |
634 | 1240 MOVII sub_c,xA ; copy net charge to xA |
1241 MOVII battery_capacity,xB ; get battery capacity into xB | |
604 | 1242 call div16x16 ; xC = xA / xB with xA as remainder |
634 | 1243 movff xC+0,batt_percent ; result is battery percentage |
628 | 1244 movlw .100 ; max. value is 100 % |
1245 cpfslt batt_percent ; batt_percent < 100 % ? | |
1246 movwf batt_percent ; NO - limit to 100 % | |
631 | 1247 return ; done |
113 | 1248 |
449 | 1249 lt2942_set_to_zero_percent: |
623 | 1250 movlw 0x02 ; point to accumulated charge registers |
634 | 1251 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
1252 movff battery_offset+1,SSP1BUF ; send battery offset, high byte | |
1253 rcall WaitMSSP ; wait for TX to complete | |
1254 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1255 movff battery_offset+0,SSP1BUF ; send battery offset, low byte | |
1256 rcall WaitMSSP ; wait for TX to complete | |
1257 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1258 bsf SSP1CON2,PEN ; stop condition |
643 | 1259 rcall WaitMSSP ; wait for TX to complete |
1260 return ; done | |
449 | 1261 |
628 | 1262 |
634 | 1263 ;----------------------------------------------------------------------------- |
1264 ; Read Gauge IC - Reset Accumulating Register to 0xFFFF | |
1265 ; | |
113 | 1266 global lt2942_charge_done |
634 | 1267 lt2942_charge_done: |
623 | 1268 movlw 0x02 ; point to accumulated charge registers |
634 | 1269 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC |
623 | 1270 setf SSP1BUF ; data byte |
634 | 1271 rcall WaitMSSP ; wait for TX to complete |
1272 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1273 setf SSP1BUF ; data byte |
634 | 1274 rcall WaitMSSP ; wait for TX to complete |
1275 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1276 bsf SSP1CON2,PEN ; stop condition |
643 | 1277 rcall WaitMSSP ; wait for TX to complete |
1278 return ; done | |
634 | 1279 |
113 | 1280 |
634 | 1281 ;----------------------------------------------------------------------------- |
1282 ; Helper Function - send 1 Byte to the LT2942 Gauge IC | |
1283 ; | |
1284 I2C_TX_GAUGE: | |
1285 movwf i2c_temp2 ; save data byte to be sent | |
623 | 1286 bsf SSP1CON2,SEN ; start condition |
634 | 1287 rcall WaitMSSP ; wait for TX to complete |
643 | 1288 movlw 0xC8 ; address byte + Write bit |
1289 movff WREG,i2c_error_vault+0 ; Store address | |
582 | 1290 movwf SSP1BUF ; control byte |
634 | 1291 rcall WaitMSSP ; wait for TX to complete |
1292 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1293 movf i2c_temp2,W ; restore data byte to be sent | |
1294 bra I2C_TX ; send byte and return | |
582 | 1295 |
634 | 1296 |
1297 ;----------------------------------------------------------------------------- | |
1298 ; Helper Function - receive 1 Byte from the LT2942 Gauge IC | |
1299 ; | |
113 | 1300 I2C_RX_GAUGE: |
643 | 1301 bsf SSP1CON2,RSEN ; repeated start condition |
634 | 1302 rcall WaitMSSP ; wait for TX to complete |
643 | 1303 movlw 0xC9 ; address byte + Read bit |
1304 movff WREG,i2c_error_vault+0 ; Store address | |
582 | 1305 movwf SSP1BUF ; control byte |
634 | 1306 rcall WaitMSSP ; wait for TX to complete |
1307 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
628 | 1308 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 1309 bra WaitMSSP ; wait for reception and return |
1310 | |
1311 | |
1312 ;----------------------------------------------------------------------------- | |
1313 ; Reset Hardware and Software Battery Gauge | |
1314 ; | |
1315 ; called from comm.asm and menu_tree.asm | |
1316 ; | |
1317 global reset_battery_gauge_and_lt2942 | |
1318 reset_battery_gauge_and_lt2942: | |
1319 btfsc battery_gauge_available ; battery gauge chip available? | |
1320 call lt2942_charge_done ; YES - reset meter to 0xFFFF | |
1321 ;bra reset_battery_gauge ; continue resetting gauge registers | |
582 | 1322 |
1323 | |
634 | 1324 ;----------------------------------------------------------------------------- |
1325 ; Reset Software Battery Gauge | |
1326 ; | |
631 | 1327 global reset_battery_gauge |
634 | 1328 reset_battery_gauge: |
1329 bsf block_battery_gauge ; suspend ISR from accessing the battery registers | |
1330 movlw .100 ; set battery level to 100% | |
1331 movwf batt_percent ; ... | |
1332 banksel battery_gauge ; select bank ISR data | |
1333 clrf battery_gauge+0 ; null the battery registers | |
1334 clrf battery_gauge+1 ; ... | |
1335 clrf battery_gauge+2 ; ... | |
1336 clrf battery_gauge+3 ; ... | |
1337 clrf battery_gauge+4 ; ... | |
1338 clrf battery_gauge+5 ; ... | |
1339 banksel common ; back to bank common | |
1340 goto eeprom_battery_gauge_write ; update battery registers in EEPROM, unblock ISR and return | |
631 | 1341 |
1342 | |
634 | 1343 |
604 | 1344 IFDEF _rx_functions |
1345 | |
634 | 1346 ;----------------------------------------------------------------------------- |
1347 ; OSTC TR - probe if TR Module available | |
1348 ; | |
560 | 1349 global I2C_probe_OSTC_rx |
1350 I2C_probe_OSTC_rx: | |
634 | 1351 bcf ostc_rx_present ; default to no TR module available |
623 | 1352 clrf WREG ; bank-safe set to zero of ... |
1353 movff WREG,rx_firmware_cur_major ; ... current TR module firmware, major | |
1354 movff WREG,rx_firmware_cur_minor ; ... current TR module firmware, minor | |
1355 movlw .5 ; max number of tries for detecting a TR module | |
634 | 1356 movwf hy ; initialize loop counter for tries |
604 | 1357 I2C_probe_OSTC_rx_1: |
623 | 1358 bsf SSP1CON2,SEN ; start condition |
634 | 1359 rcall WaitMSSP ; wait for TX to complete |
623 | 1360 movlw 0x50 ; address byte + write bit |
643 | 1361 movff WREG,i2c_error_vault+0 ; Store address |
582 | 1362 movwf SSP1BUF ; control byte |
634 | 1363 rcall WaitMSSP ; wait for TX to complete |
623 | 1364 btfss SSP1CON2,ACKSTAT ; ACK received? |
1365 bsf ostc_rx_present ; YES - TR module detected | |
1366 bsf SSP1CON2,PEN ; stop condition | |
634 | 1367 rcall WaitMSSP ; wait for TX to complete |
623 | 1368 btfss ostc_rx_present ; was a TR module detected? |
1369 return ; NO - done | |
634 | 1370 |
1371 WAITMS .1 ; wait 1 ms | |
623 | 1372 bsf SSP1CON2,SEN ; start condition |
634 | 1373 rcall WaitMSSP ; wait for TX to complete |
623 | 1374 movlw 0x50 ; address byte + write bit |
582 | 1375 movwf SSP1BUF ; control byte |
634 | 1376 rcall WaitMSSP ; wait for TX to complete |
1377 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1378 movlw 0x1B ; command: get firmware | |
1379 movwf SSP1BUF ; send command | |
1380 rcall WaitMSSP ; wait for TX to complete | |
1381 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1382 bsf SSP1CON2,PEN ; stop condition |
634 | 1383 rcall WaitMSSP ; wait for TX to complete |
1384 | |
1385 WAITMS .1 ; wait 1 ms | |
1386 | |
623 | 1387 bsf SSP1CON2,SEN ; start condition |
634 | 1388 rcall WaitMSSP ; wait for TX to complete |
623 | 1389 movlw 0x51 ; address byte + Read bit |
582 | 1390 movwf SSP1BUF ; control byte |
634 | 1391 rcall WaitMSSP ; wait for TX to complete |
623 | 1392 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 1393 rcall WaitMSSP ; wait for TX to complete |
623 | 1394 movff SSP1BUF,rx_firmware_cur_major ; store as firmware version, major |
1395 bsf SSP1CON2,ACKEN ; master acknowledge | |
634 | 1396 rcall WaitMSSP ; wait for TX to complete |
582 | 1397 |
560 | 1398 ; last byte in read from RX circuity always with a NACK! |
623 | 1399 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 1400 rcall WaitMSSP ; wait for TX to complete |
623 | 1401 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor |
634 | 1402 bsf SSP1CON2,ACKDT ; set ACKDT flag |
623 | 1403 bsf SSP1CON2,ACKEN ; master NOT acknowledge |
634 | 1404 rcall WaitMSSP ; wait for TX to complete |
623 | 1405 bcf SSP1CON2,ACKDT ; reset ACKDT flag |
1406 bsf SSP1CON2,PEN ; stop condition | |
634 | 1407 rcall WaitMSSP ; wait for TX to complete |
643 | 1408 |
623 | 1409 ; wait for TR module becoming ready |
634 | 1410 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common |
623 | 1411 movlw .147 ; code for not ready, minor |
1412 cpfseq i2c_temp1 ; equal? | |
634 | 1413 return ; NO - TR module ready, done |
1414 movff rx_firmware_cur_major,i2c_temp1 ; YES - copy major firmware version to bank common | |
623 | 1415 movlw .27 ; - code for not ready, major |
1416 cpfseq i2c_temp1 ; - equal? | |
634 | 1417 return ; NO - TR module ready, done |
623 | 1418 bsf active_reset_ostc_rx ; YES - apply reset to TR module |
1419 WAITMS .5 ; - wait 5 ms | |
1420 bcf active_reset_ostc_rx ; - release reset | |
1421 WAITMS .250 ; - wait for 250 ms | |
1422 WAITMS .250 ; - wait another 250 ms | |
1423 decfsz hy,F ; - decrement counter for number of tries, became zero? | |
1424 bra I2C_probe_OSTC_rx_1 ; - NO - try again | |
1425 bcf ostc_rx_present ; - YES - something is wrong, flag TR module as not available | |
634 | 1426 return ; - done |
582 | 1427 |
560 | 1428 |
634 | 1429 ;----------------------------------------------------------------------------- |
1430 ; OSTC TR - get Tank Data | |
1431 ; | |
560 | 1432 global I2C_get_tankdata |
1433 I2C_get_tankdata: | |
623 | 1434 bsf SSP1CON2,SEN ; start condition |
634 | 1435 rcall WaitMSSP ; wait for TX to complete |
623 | 1436 movlw 0x50 ; address byte + write bit |
643 | 1437 movff WREG,i2c_error_vault+0 ; Store address |
582 | 1438 movwf SSP1BUF ; control byte |
634 | 1439 rcall WaitMSSP ; wait for TX to complete |
1440 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1441 movlw 0x1E ; read buffer2 (48 bytes) |
1442 movwf SSP1BUF ; data byte | |
634 | 1443 rcall WaitMSSP ; wait for TX to complete |
1444 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1445 bsf SSP1CON2,PEN ; stop condition |
634 | 1446 rcall WaitMSSP ; wait for TX to complete |
1447 | |
1448 WAITMS .1 ; wait 1 ms | |
1449 | |
560 | 1450 ; read 48 bytes |
623 | 1451 bsf SSP1CON2,SEN ; start condition |
634 | 1452 rcall WaitMSSP ; wait for TX to complete |
623 | 1453 movlw 0x51 ; address byte + read bit |
582 | 1454 movwf SSP1BUF ; control byte |
634 | 1455 rcall WaitMSSP ; wait for TX to complete |
1456 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
582 | 1457 movlw .47 ; 47 with ACK + 1 w/o ACK |
634 | 1458 movwf i2c_temp2 ; initialize loop counter |
1459 lfsr FSR2,rx_buffer ; point to start of rx data buffer | |
560 | 1460 I2C_get_tankdata_loop_read: |
634 | 1461 bsf SSP1CON2,RCEN ; enable receive mode |
1462 rcall WaitMSSP ; wait for TX to complete | |
1463 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer | |
1464 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
623 | 1465 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1466 rcall WaitMSSP ; wait for TX to complete |
1467 decfsz i2c_temp2,F ; decrement loop counter, done? | |
1468 bra I2C_get_tankdata_loop_read ; NO - loop | |
1469 ; read last byte without ACK | |
1470 bsf SSP1CON2,RCEN ; enable receive mode | |
1471 rcall WaitMSSP ; wait for TX to complete | |
1472 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer | |
1473 bsf SSP1CON2,ACKDT ; set ACKDT flag | |
623 | 1474 bsf SSP1CON2,ACKEN ; master NOT acknowledge |
634 | 1475 rcall WaitMSSP ; wait for TX to complete |
623 | 1476 bcf SSP1CON2,ACKDT ; reset ACKDT flag |
1477 bsf SSP1CON2,PEN ; stop condition | |
643 | 1478 rcall WaitMSSP ; wait for TX to complete |
1479 return ; done | |
1480 | |
582 | 1481 |
1482 | |
634 | 1483 ;----------------------------------------------------------------------------- |
1484 ; OSTC TR - Firmware Update | |
1485 ; | |
628 | 1486 IFDEF _rx_update |
1487 | |
560 | 1488 global I2C_update_OSTC_rx |
623 | 1489 I2C_update_OSTC_rx: ; transfer 64 byte to RX co-processor |
1490 ; setup for write | |
582 | 1491 bcf i2c_error_flag ; clear error flag |
623 | 1492 lfsr FSR2,buffer ; initialize pointer to send buffer used for verify |
1493 movlw .64 ; initialize loop counter: 64 byte with ACK | |
1494 movwf i2c_temp2 ; ... | |
1495 ; address write | |
1496 bsf SSP1CON2,SEN ; start condition | |
634 | 1497 rcall WaitMSSP ; wait for TX to complete |
623 | 1498 movlw 0x50 ; address byte + write bit |
643 | 1499 movff WREG,i2c_error_vault+0 ; Store address |
582 | 1500 movwf SSP1BUF ; control byte |
634 | 1501 rcall WaitMSSP ; wait for TX to complete |
1502 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1503 ; write 64 bytes |
1504 I2C_update_OSTC_loop: | |
1505 TBLRD*+ ; read a byte from program memory | |
1506 movff TABLAT,POSTINC2 ; copy to send buffer | |
1507 movff TABLAT,SSP1BUF ; copy to I2C data buffer | |
634 | 1508 rcall WaitMSSP ; wait for TX to complete |
1509 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1510 decfsz i2c_temp2,F ;decrement loop counter, became zero? |
1511 bra I2C_update_OSTC_loop ; NO - loop | |
1512 bsf SSP1CON2,PEN ; YES - stop condition | |
634 | 1513 rcall WaitMSSP ; - wait for TX to complete |
623 | 1514 WAITMS .1 ; - wait another 1 ms |
1515 ; setup for read-back | |
1516 lfsr FSR2,buffer ; reset pointer to begin of send buffer | |
1517 movlw .63 ; initialize loop counter: 63 byte with ACK + 1 w/o ACK | |
634 | 1518 movwf i2c_temp2 ; ... |
623 | 1519 ; address read-back |
1520 bsf SSP1CON2,SEN ; start condition | |
634 | 1521 rcall WaitMSSP ; wait for TX to complete |
623 | 1522 movlw 0x51 ; address byte + read bit |
582 | 1523 movwf SSP1BUF ; control byte |
634 | 1524 rcall WaitMSSP ; wait for TX to complete |
1525 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1526 ; read-back 64 bytes |
560 | 1527 I2C_update_OSTC_loop_read: |
623 | 1528 bsf SSP1CON2,RCEN ; enable receive mode |
634 | 1529 rcall WaitMSSP ; wait for TX to complete |
1530 movf SSP1BUF,W ; copy received byte to WREG | |
623 | 1531 cpfseq POSTINC2 ; compare read-back byte with sent byte, equal? |
1532 bsf i2c_error_flag ; NO - not equal, set error flag | |
634 | 1533 bcf SSP1CON2,ACKDT ; reset ACKDT flag |
623 | 1534 bsf SSP1CON2,ACKEN ; master acknowledge |
634 | 1535 rcall WaitMSSP ; wait for TX to complete |
623 | 1536 decfsz i2c_temp2,F ; decrement loop counter, became zero? |
1537 bra I2C_update_OSTC_loop_read ; NO - loop | |
560 | 1538 ; 1 w/o ACK |
623 | 1539 bsf SSP1CON2, RCEN ; YES - enable receive mode |
634 | 1540 rcall WaitMSSP ; - wait for TX to complete |
623 | 1541 movf SSP1BUF,W ; - get 64th byte |
1542 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal? | |
1543 bsf i2c_error_flag ; NO - not equal, set error flag | |
634 | 1544 bsf SSP1CON2,ACKDT ; - set ACKDT flag |
623 | 1545 bsf SSP1CON2,ACKEN ; - master NOT acknowledge |
634 | 1546 rcall WaitMSSP ; - wait for TX to complete |
623 | 1547 bcf SSP1CON2,ACKDT ; - reset ACKDT flag |
1548 ; stop | |
1549 bsf SSP1CON2,PEN ; stop condition | |
634 | 1550 rcall WaitMSSP ; wait for TX to complete |
560 | 1551 WAITMS .1 |
623 | 1552 ; address commit |
1553 bsf SSP1CON2,SEN ; start condition | |
634 | 1554 rcall WaitMSSP ; wait for TX to complete |
623 | 1555 movlw 0x50 ; address byte + write bit |
582 | 1556 movwf SSP1BUF ; control byte |
634 | 1557 rcall WaitMSSP ; wait for TX to complete |
1558 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1559 movlw 0x1F ; write command |
1560 movwf SSP1BUF ; data byte | |
634 | 1561 rcall WaitMSSP ; wait for TX to complete |
1562 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
623 | 1563 bsf SSP1CON2,PEN ; stop condition |
634 | 1564 rcall WaitMSSP ; wait for TX to complete |
623 | 1565 WAITMS .5 ; required waiting time |
1566 ; error check | |
1567 btfss i2c_error_flag ; did an error occur? | |
1568 retlw .0 ; NO - data transfered successfully | |
1569 retlw .255 ; YES - error in data transfer occurred | |
582 | 1570 |
628 | 1571 ENDIF ; _rx_update |
1572 | |
1573 ENDIF ; _rx_functions | |
643 | 1574 |
634 | 1575 ;----------------------------------------------------------------------------- |
643 | 1576 ; Probe for MS5837 sensor |
1577 ; | |
1578 global I2C_probe_pressure_sensor ; Do not call from ISR! | |
1579 I2C_probe_pressure_sensor: ; Probe the type of sensor, set/clear press_sensor_type | |
1580 bcf press_sensor_type ; MS5541 as default | |
1581 bsf SSP1CON2,SEN ; start condition | |
1582 rcall WaitMSSP ; wait for TX to complete | |
1583 movlw 0xEC ; address byte + write bit | |
1584 movff WREG,i2c_error_vault+0 ; Store address | |
1585 movwf SSP1BUF ; control byte | |
1586 rcall WaitMSSP ; wait for TX to complete | |
1587 btfss SSP1CON2,ACKSTAT ; ACK received? | |
1588 bsf press_sensor_type ; MS5837 sensor found | |
1589 bsf SSP1CON2,PEN ; stop condition | |
1590 rcall WaitMSSP ; wait for TX to complete | |
1591 return | |
1592 | |
1593 ;-------------------------------------------------------------------- | |
1594 ; Helper Function - get the calibration parameter from # WREG address | |
1595 ; Do not call from ISR! | |
1596 I2C_get_calib_parameter: | |
1597 movwf lo ; store address | |
1598 bsf SSP1CON2,SEN ; start condition | |
1599 rcall WaitMSSP ; wait for TX to complete | |
1600 movlw 0xEC ; address byte + write bit | |
1601 movff WREG,i2c_error_vault+0 ; Store address | |
1602 rcall I2C_TX ; send byte | |
1603 movf lo,W ; Point to calibration register | |
1604 rcall I2C_TX ; send byte | |
1605 bsf SSP1CON2,PEN ; stop condition | |
1606 rcall WaitMSSP ; wait for TX to complete | |
1607 | |
1608 bsf SSP1CON2,SEN ; start condition | |
1609 rcall WaitMSSP ; wait for TX to complete | |
1610 movlw 0xED ; address byte + read bit | |
1611 movff WREG,i2c_error_vault+0 ; Store address | |
1612 movwf SSP1BUF ; control byte | |
1613 rcall WaitMSSP ; wait for TX to complete | |
1614 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1615 bsf SSP1CON2,RCEN ; enable receive mode | |
1616 rcall WaitMSSP ; wait for reception and return | |
1617 movff SSP1BUF,dMSB ; High byte | |
1618 bsf SSP1CON2,ACKEN ; master acknowledge | |
1619 rcall WaitMSSP ; wait for TX to complete | |
1620 | |
1621 bsf SSP1CON2,RCEN ; enable receive mode | |
1622 rcall WaitMSSP ; wait for reception | |
1623 movff SSP1BUF,dLSB ; Low byte | |
1624 ; bsf SSP1CON2,ACKDT ; set ACKDT flag | |
1625 bsf SSP1CON2,ACKEN ; master acknowledge | |
1626 rcall WaitMSSP ; wait for TX to complete | |
1627 ; bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1628 bsf SSP1CON2,PEN ; stop condition | |
1629 bra WaitMSSP ; wait for TX to complete (And return) | |
1630 | |
1631 | |
1632 global I2C_get_calib_MS5837 ; Do not call from ISR! | |
1633 I2C_get_calib_MS5837: | |
1634 banksel common | |
1635 ; first, send a reset | |
1636 bsf SSP1CON2,SEN ; start condition | |
1637 rcall WaitMSSP ; wait for TX to complete | |
1638 movlw 0xEC ; address byte + write bit | |
1639 movff WREG,i2c_error_vault+0 ; Store address | |
1640 rcall I2C_TX ; send byte | |
1641 movlw 0x1E | |
1642 rcall I2C_TX ; send byte | |
1643 bsf SSP1CON2,PEN ; stop condition | |
1644 rcall WaitMSSP ; wait for TX to complete | |
1645 WAITMS .5 ; 2.8ms according to datasheet | |
1646 | |
1647 movlw 0xA2 ; Point to C1 | |
1648 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1649 movff dLSB,C1+0 ; store calib | |
1650 movff dMSB,C1+1 ; store calib | |
623 | 1651 |
643 | 1652 movlw 0xA4 ; Point to C2 |
1653 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1654 movff dLSB,C2+0 ; store calib | |
1655 movff dMSB,C2+1 ; store calib | |
1656 | |
1657 movlw 0xA6 ; Point to C3 | |
1658 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1659 movff dLSB,C3+0 ; store calib | |
1660 movff dMSB,C3+1 ; store calib | |
1661 | |
1662 movlw 0xA8 ; Point to C4 | |
1663 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1664 movff dLSB,C4+0 ; store calib | |
1665 movff dMSB,C4+1 ; store calib | |
1666 | |
1667 movlw 0xAA ; Point to C5 | |
1668 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1669 movff dLSB,C5+0 ; store calib | |
1670 movff dMSB,C5+1 ; store calib | |
1671 | |
1672 movlw 0xAC ; Point to C6 | |
1673 rcall I2C_get_calib_parameter ; returns calibration value in lo and hi | |
1674 movff dLSB,C6+0 ; store calib | |
1675 movff dMSB,C6+1 ; store calib | |
1676 | |
1677 return | |
1678 | |
1679 global I2C_get_press_val_MS5837 | |
1680 I2C_get_press_val_MS5837: | |
1681 bsf i2c_busy_pressure ; reading new pressure | |
1682 | |
1683 bsf SSP1CON2,SEN ; start condition | |
1684 rcall WaitMSSP ; wait for TX to complete | |
1685 movlw 0xEC ; address byte + write bit | |
1686 movff WREG,i2c_error_vault+0 ; Store address | |
1687 rcall I2C_TX ; send byte | |
1688 movlw 0x00 ; command byte (0x00, read ADC) | |
1689 rcall I2C_TX ; send byte | |
1690 | |
1691 bsf SSP1CON2,RSEN ; repeated start condition | |
1692 rcall WaitMSSP ; wait for TX to complete | |
1693 movlw 0xED ; address byte + read bit | |
1694 movff WREG,i2c_error_vault+0 ; Store address | |
1695 movwf SSP1BUF ; control byte | |
1696 rcall WaitMSSP ; wait for TX to complete | |
1697 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1698 | |
1699 bsf SSP1CON2,RCEN ; enable receive mode | |
1700 rcall WaitMSSP ; wait for reception and return | |
1701 movff SSP1BUF,D1_buffer+2 ; Upper byte | |
1702 bsf SSP1CON2,ACKEN ; master acknowledge | |
1703 rcall WaitMSSP ; wait for TX to complete | |
1704 bsf SSP1CON2,RCEN ; enable receive mode | |
1705 rcall WaitMSSP ; wait for reception | |
1706 movff SSP1BUF,D1_buffer+1 ; high byte | |
1707 bsf SSP1CON2,ACKEN ; master acknowledge | |
1708 rcall WaitMSSP ; wait for TX to complete | |
1709 bsf SSP1CON2,RCEN ; enable receive mode | |
1710 rcall WaitMSSP ; wait for reception | |
1711 movff SSP1BUF,D1_buffer+0 ; Low byte | |
1712 bsf SSP1CON2,ACKEN ; master acknowledge | |
1713 rcall WaitMSSP ; wait for TX to complete | |
1714 bsf SSP1CON2,PEN ; stop condition | |
1715 rcall WaitMSSP ; wait for TX to complete | |
1716 | |
1717 ; Start temperature measurement | |
1718 bsf SSP1CON2,SEN ; start condition | |
1719 rcall WaitMSSP ; wait for TX to complete | |
1720 movlw 0xEC ; address byte + write bit | |
1721 rcall I2C_TX ; send byte | |
1722 movlw 0x58 ; OSR=4096, type=D2 | |
1723 rcall I2C_TX ; send byte | |
1724 bsf SSP1CON2,PEN ; stop condition | |
1725 rcall WaitMSSP ; wait for TX to complete | |
1726 bcf ms5837_state ; =0: result of temperature will be in the ADC | |
1727 bcf i2c_busy_pressure ; reading new pressure | |
1728 return | |
1729 | |
1730 global I2C_get_temp_val_MS5837 | |
1731 I2C_get_temp_val_MS5837: | |
1732 bsf i2c_busy_temperature ; reading new temperature | |
1733 | |
1734 bsf SSP1CON2,SEN ; start condition | |
1735 rcall WaitMSSP ; wait for TX to complete | |
1736 movlw 0xEC ; address byte + write bit | |
1737 movff WREG,i2c_error_vault+0 ; Store address | |
1738 rcall I2C_TX ; send byte | |
1739 movlw 0x00 ; command byte (0x00, read ADC) | |
1740 rcall I2C_TX ; send byte | |
1741 | |
1742 bsf SSP1CON2,RSEN ; repeated start condition | |
1743 rcall WaitMSSP ; wait for TX to complete | |
1744 movlw 0xED ; address byte + read bit | |
1745 movff WREG,i2c_error_vault+0 ; Store address | |
1746 movwf SSP1BUF ; control byte | |
1747 rcall WaitMSSP ; wait for TX to complete | |
1748 rcall I2C_Check_ACK ; check for acknowledge by receiver | |
1749 | |
1750 bsf SSP1CON2,RCEN ; enable receive mode | |
1751 rcall WaitMSSP ; wait for reception and return | |
1752 movff SSP1BUF,D2_buffer+2 ; Upper byte | |
1753 bsf SSP1CON2,ACKEN ; master acknowledge | |
1754 rcall WaitMSSP ; wait for TX to complete | |
1755 bsf SSP1CON2,RCEN ; enable receive mode | |
1756 rcall WaitMSSP ; wait for reception | |
1757 movff SSP1BUF,D2_buffer+1 ; high byte | |
1758 bsf SSP1CON2,ACKEN ; master acknowledge | |
1759 rcall WaitMSSP ; wait for TX to complete | |
1760 bsf SSP1CON2,RCEN ; enable receive mode | |
1761 rcall WaitMSSP ; wait for reception | |
1762 movff SSP1BUF,D2_buffer+0 ; Low byte | |
1763 bsf SSP1CON2,ACKEN ; master acknowledge | |
1764 rcall WaitMSSP ; wait for TX to complete | |
1765 bsf SSP1CON2,PEN ; stop condition | |
1766 rcall WaitMSSP ; wait for TX to complete | |
1767 | |
1768 ; Start pressure measurement | |
1769 bsf SSP1CON2,SEN ; start condition | |
1770 rcall WaitMSSP ; wait for TX to complete | |
1771 movlw 0xEC ; address byte + write bit | |
1772 rcall I2C_TX ; send byte | |
1773 movlw 0x48 ; OSR=4096, type=D1 | |
1774 rcall I2C_TX ; send byte | |
1775 bsf SSP1CON2,PEN ; stop condition | |
1776 rcall WaitMSSP ; wait for TX to complete | |
1777 bsf ms5837_state ; =0: result of pressure will be in the ADC | |
1778 bcf i2c_busy_temperature ; reading new temperature | |
1779 return | |
1780 | |
1781 | |
1782 ;----------------------------------------------------------------------------- | |
1783 ; I2C Bus error checker | |
1784 ; | |
1785 global check_i2c_error | |
1786 extern TFT_message_i2c_error | |
1787 check_i2c_error: | |
1788 btfss i2c_error_flag | |
1789 return | |
1790 incf message_counter,F ; increase message counter | |
1791 goto TFT_message_i2c_error ; show message for battery low (battery percent) and return | |
1792 | |
1793 ;----------------------------------------------------------------------------- | |
582 | 1794 END |