comparison src/i2c.asm @ 643:7d8a4c60ec1a

3.15 release
author heinrichsweikamp
date Mon, 24 May 2021 18:40:53 +0200
parents 8c1f1f334275
children 070528a88715 357341239438
comparison
equal deleted inserted replaced
642:a9a0188091e4 643:7d8a4c60ec1a
35 ; 35 ;
36 ; RX Circuity 36 ; RX Circuity
37 ; ----------- 37 ; -----------
38 ; RX Circuity read address (8-Bit): 0x51 38 ; RX Circuity read address (8-Bit): 0x51
39 ; RX Circuity write address (8-Bit): 0x50 39 ; RX Circuity write address (8-Bit): 0x50
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
40 ; 50 ;
41 ; 51 ;
42 52
43 ; HISTORY 53 ; HISTORY
44 ; 2012-08-22 : [mH] Creation 54 ; 2012-08-22 : [mH] Creation
47 57
48 #include "hwos.inc" ; Mandatory header 58 #include "hwos.inc" ; Mandatory header
49 #include "wait.inc" 59 #include "wait.inc"
50 #include "math.inc" 60 #include "math.inc"
51 #include "eeprom_rs232.inc" 61 #include "eeprom_rs232.inc"
52 62
53 63
54 ;============================================================================= 64 ;=============================================================================
55 i2c CODE 65 i2c CODE
56 ;============================================================================= 66 ;=============================================================================
57 67
58
59 ;-----------------------------------------------------------------------------
60 ; Helper Function - send 1 Byte, wait for end of transmission and check ackn
61 ;
62 I2C_TX:
63 movwf SSP1BUF ; put byte to be sent into TX buffer
64 rcall WaitMSSP ; wait for TX to complete
65 bra I2C_Check_ACK ; check for acknowledge by receiver and return
66 68
67 69
68 ;----------------------------------------------------------------------------- 70 ;-----------------------------------------------------------------------------
69 ; Helper Function - get two Bytes and divide hi:lo/16 (signed) 71 ; Helper Function - get two Bytes and divide hi:lo/16 (signed)
70 ; 72 ;
115 bra I2C_RX_accelerometer_compass2 ; YES 117 bra I2C_RX_accelerometer_compass2 ; YES
116 btfsc compass_type1 ; compass1 ? 118 btfsc compass_type1 ; compass1 ?
117 bra I2C_RX_accelerometer_compass1 ; YES 119 bra I2C_RX_accelerometer_compass1 ; YES
118 ;bra I2C_RX_accelerometer_compass0 ; NO - compass0 then 120 ;bra I2C_RX_accelerometer_compass0 ; NO - compass0 then
119 121
120 I2C_RX_accelerometer_compass0: 122 ;I2C_RX_accelerometer_compass0:
121 bsf SSP1CON2,SEN ; start condition 123 bsf SSP1CON2,SEN ; start condition
122 rcall WaitMSSP ; wait for TX to complete 124 rcall WaitMSSP ; wait for TX to complete
123 movlw 0x38 ; address 125 movlw 0x38 ; address
126 movff WREG,i2c_error_vault+0 ; Store address
124 rcall I2C_TX ; send byte 127 rcall I2C_TX ; send byte
125 movlw 0x00 ; ?? 128 movlw 0x00 ; ??
126 rcall I2C_TX ; send byte 129 rcall I2C_TX ; send byte
127 bsf SSP1CON2,RSEN ; repeated start condition 130 bsf SSP1CON2,RSEN ; repeated start condition
128 rcall WaitMSSP ; wait for TX to complete 131 rcall WaitMSSP ; wait for TX to complete
176 negf lo ; ... 179 negf lo ; ...
177 btfsc STATUS,C ; carry to propagate? 180 btfsc STATUS,C ; carry to propagate?
178 incf hi,F ; YES - do it 181 incf hi,F ; YES - do it
179 MOVII mpr,accel_DZ ; copy result to accel_DZ 182 MOVII mpr,accel_DZ ; copy result to accel_DZ
180 bsf SSP1CON2,PEN ; stop condition 183 bsf SSP1CON2,PEN ; stop condition
181 bra WaitMSSP ; wait for TX to complete and return 184 rcall WaitMSSP ; wait for TX to complete
185 return
182 186
183 187
184 I2C_RX_accelerometer_compass1: 188 I2C_RX_accelerometer_compass1:
185 bsf SSP1CON2,SEN ; start condition 189 bsf SSP1CON2,SEN ; start condition
186 rcall WaitMSSP ; wait for TX to complete 190 rcall WaitMSSP ; wait for TX to complete
187 movlw 0x3C ; address 191 movlw 0x3C ; address
192 movff WREG,i2c_error_vault+0 ; Store address
188 rcall I2C_TX ; send byte 193 rcall I2C_TX ; send byte
189 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 194 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
190 rcall I2C_TX ; send byte 195 rcall I2C_TX ; send byte
191 bsf SSP1CON2,RSEN ; repeated start condition (!) 196 bsf SSP1CON2,RSEN ; repeated start condition (!)
192 rcall WaitMSSP ; wait for TX to complete 197 rcall WaitMSSP ; wait for TX to complete
272 277
273 I2C_RX_accelerometer_compass2: 278 I2C_RX_accelerometer_compass2:
274 bsf SSP1CON2,SEN ; start condition 279 bsf SSP1CON2,SEN ; start condition
275 rcall WaitMSSP ; wait for TX to complete 280 rcall WaitMSSP ; wait for TX to complete
276 movlw 0x32 ; address 281 movlw 0x32 ; address
282 movff WREG,i2c_error_vault+0 ; Store address
277 rcall I2C_TX ; send byte 283 rcall I2C_TX ; send byte
278 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 284 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
279 rcall I2C_TX ; send byte 285 rcall I2C_TX ; send byte
280 bsf SSP1CON2,RSEN ; repeated start condition (!) 286 bsf SSP1CON2,RSEN ; repeated start condition (!)
281 rcall WaitMSSP ; wait for TX to complete 287 rcall WaitMSSP ; wait for TX to complete
284 290
285 I2C_RX_accelerometer_compass3: 291 I2C_RX_accelerometer_compass3:
286 bsf SSP1CON2,SEN ; start condition 292 bsf SSP1CON2,SEN ; start condition
287 rcall WaitMSSP ; wait for TX to complete 293 rcall WaitMSSP ; wait for TX to complete
288 movlw 0x3A ; address 294 movlw 0x3A ; address
295 movff WREG,i2c_error_vault+0 ; Store address
289 rcall I2C_TX ; send byte 296 rcall I2C_TX ; send byte
290 movlw 0x28 ; 0x28 (OUT_X_L_A) 297 movlw 0x28 ; 0x28 (OUT_X_L_A)
291 rcall I2C_TX ; send byte 298 rcall I2C_TX ; send byte
292 bsf SSP1CON2,RSEN ; repeated start condition (!) 299 bsf SSP1CON2,RSEN ; repeated start condition (!)
293 rcall WaitMSSP ; wait for TX to complete 300 rcall WaitMSSP ; wait for TX to complete
318 bra I2C_RX_compass2 ; YES 325 bra I2C_RX_compass2 ; YES
319 btfsc compass_type1 ; compass 1 ? 326 btfsc compass_type1 ; compass 1 ?
320 bra I2C_RX_compass1 ; YES 327 bra I2C_RX_compass1 ; YES
321 ;bra I2C_RX_compass0 ; NO - compass 0 then 328 ;bra I2C_RX_compass0 ; NO - compass 0 then
322 329
323 I2C_RX_compass0: 330 ;I2C_RX_compass0:
324 bsf SSP1CON2,SEN ; start condition 331 bsf SSP1CON2,SEN ; start condition
325 rcall WaitMSSP ; wait for TX to complete 332 rcall WaitMSSP ; wait for TX to complete
326 movlw 0x3C ; address 333 movlw 0x3C ; address
334 movff WREG,i2c_error_vault+0 ; Store address
327 rcall I2C_TX ; send byte 335 rcall I2C_TX ; send byte
328 movlw 0x03 ; ?? 336 movlw 0x03 ; ??
329 rcall I2C_TX ; send byte 337 rcall I2C_TX ; send byte
330 bsf SSP1CON2,PEN ; stop condition 338 bsf SSP1CON2,PEN ; stop condition
331 rcall WaitMSSP ; wait for TX to complete 339 rcall WaitMSSP ; wait for TX to complete
393 401
394 I2C_RX_compass1: ; compass type 1 402 I2C_RX_compass1: ; compass type 1
395 bsf SSP1CON2,SEN ; start condition 403 bsf SSP1CON2,SEN ; start condition
396 rcall WaitMSSP ; wait for TX to complete 404 rcall WaitMSSP ; wait for TX to complete
397 movlw 0x3C ; address 405 movlw 0x3C ; address
406 movff WREG,i2c_error_vault+0 ; Store address
398 rcall I2C_TX ; send byte 407 rcall I2C_TX ; send byte
399 movlw b'10001000' ; 0x08 with auto-increment (MSB=1) 408 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
400 rcall I2C_TX ; send byte 409 rcall I2C_TX ; send byte
401 bsf SSP1CON2,RSEN ; repeated start condition (!) 410 bsf SSP1CON2,RSEN ; repeated start condition (!)
402 rcall WaitMSSP ; wait for TX to complete 411 rcall WaitMSSP ; wait for TX to complete
438 bsf SSP1CON2, RCEN ; Enable receive mode 447 bsf SSP1CON2, RCEN ; Enable receive mode
439 rcall WaitMSSP ; wait for TX to complete 448 rcall WaitMSSP ; wait for TX to complete
440 movff SSP1BUF,hi ; data byte 449 movff SSP1BUF,hi ; data byte
441 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) 450 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
442 MOVII mpr,compass_DZ ; copy result 451 MOVII mpr,compass_DZ ; copy result
443 bsf SSP1CON2,PEN ; stop condition 452 bsf SSP1CON2,PEN ; stop condition
444 bra WaitMSSP ; ... and return 453 rcall WaitMSSP
454 return ; done
455
445 456
446 457
447 I2C_RX_compass2: ; compass type 2 458 I2C_RX_compass2: ; compass type 2
448 bsf SSP1CON2,SEN ; start condition 459 bsf SSP1CON2,SEN ; start condition
449 rcall WaitMSSP ; wait for TX to complete 460 rcall WaitMSSP ; wait for TX to complete
450 movlw 0x3C ; address 461 movlw 0x3C ; address
462 movff WREG,i2c_error_vault+0 ; Store address
451 rcall I2C_TX ; send byte 463 rcall I2C_TX ; send byte
452 movlw 0xE8 ; 0x68 with auto-increment (MSB=1) 464 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
453 rcall I2C_TX ; send byte 465 rcall I2C_TX ; send byte
454 bsf SSP1CON2,RSEN ; repeated start condition (!) 466 bsf SSP1CON2,RSEN ; repeated start condition (!)
455 rcall WaitMSSP ; wait for TX to complete 467 rcall WaitMSSP ; wait for TX to complete
490 rcall I2C_OneByteRX ; receive 1 byte with acknowledge 502 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
491 movff SSP1BUF,hi ; data byte 503 movff SSP1BUF,hi ; data byte
492 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed) 504 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
493 MOVII mpr,compass_DZ ; copy result 505 MOVII mpr,compass_DZ ; copy result
494 bsf SSP1CON2,PEN ; stop condition 506 bsf SSP1CON2,PEN ; stop condition
495 bra WaitMSSP ; ...and return 507 rcall WaitMSSP
508 return ; done
509
496 510
497 511
498 I2C_RX_compass3: ; compass type 3 512 I2C_RX_compass3: ; compass type 3
499 bsf SSP1CON2,SEN ; start condition 513 bsf SSP1CON2,SEN ; start condition
500 rcall WaitMSSP ; wait for TX to complete 514 rcall WaitMSSP ; wait for TX to complete
501 movlw 0x3C ; address 515 movlw 0x3C ; address
516 movff WREG,i2c_error_vault+0 ; Store address
502 rcall I2C_TX ; send byte 517 rcall I2C_TX ; send byte
503 movlw 0xA8 ; 0x28 with auto-increment (MSB=1) 518 movlw 0xA8 ; 0x28 with auto-increment (MSB=1)
504 rcall I2C_TX ; send byte 519 rcall I2C_TX ; send byte
505 bsf SSP1CON2,RSEN ; repeated start condition (!) 520 bsf SSP1CON2,RSEN ; repeated start condition (!)
506 rcall WaitMSSP ; wait for TX to complete 521 rcall WaitMSSP ; wait for TX to complete
522 537
523 ; probe for compass 3 538 ; probe for compass 3
524 bsf SSP1CON2,SEN ; start condition 539 bsf SSP1CON2,SEN ; start condition
525 rcall WaitMSSP ; wait for TX to complete 540 rcall WaitMSSP ; wait for TX to complete
526 movlw 0x3A ; address byte + write bit 541 movlw 0x3A ; address byte + write bit
542 movff WREG,i2c_error_vault+0 ; Store address
527 movwf SSP1BUF ; control byte 543 movwf SSP1BUF ; control byte
528 rcall WaitMSSP ; wait for TX to complete 544 rcall WaitMSSP ; wait for TX to complete
529 btfss SSP1CON2,ACKSTAT ; ACK received? 545 btfss SSP1CON2,ACKSTAT ; ACK received?
530 bsf compass_type3 ; YES - ACK was send, compass 3 found 546 bsf compass_type3 ; YES - ACK was send, compass 3 found
531 bsf SSP1CON2,PEN ; stop condition 547 bsf SSP1CON2,PEN ; stop condition
536 552
537 ; probe for compass 2 553 ; probe for compass 2
538 bsf SSP1CON2,SEN ; start condition 554 bsf SSP1CON2,SEN ; start condition
539 rcall WaitMSSP ; wait for TX to complete 555 rcall WaitMSSP ; wait for TX to complete
540 movlw 0x32 ; address byte + write bit 556 movlw 0x32 ; address byte + write bit
557 movff WREG,i2c_error_vault+0 ; Store address
541 movwf SSP1BUF ; control byte 558 movwf SSP1BUF ; control byte
542 rcall WaitMSSP ; wait for TX to complete 559 rcall WaitMSSP ; wait for TX to complete
543 btfss SSP1CON2,ACKSTAT ; ACK received? 560 btfss SSP1CON2,ACKSTAT ; ACK received?
544 bsf compass_type2 ; YES - ACK send, compass 2 found 561 bsf compass_type2 ; YES - ACK send, compass 2 found
545 bsf SSP1CON2,PEN ; stop condition 562 bsf SSP1CON2,PEN ; stop condition
551 ; probe for compass 0 or 1 568 ; probe for compass 0 or 1
552 bsf compass_type1 ; assume compass 1 by default 569 bsf compass_type1 ; assume compass 1 by default
553 bsf SSP1CON2,SEN ; start condition 570 bsf SSP1CON2,SEN ; start condition
554 rcall WaitMSSP ; wait for TX to complete 571 rcall WaitMSSP ; wait for TX to complete
555 movlw 0x3C ; address 572 movlw 0x3C ; address
573 movff WREG,i2c_error_vault+0 ; Store address
556 rcall I2C_TX ; send byte 574 rcall I2C_TX ; send byte
557 movlw 0x0F ; ?? 575 movlw 0x0F ; ??
558 rcall I2C_TX ; send byte 576 rcall I2C_TX ; send byte
559 bsf SSP1CON2,PEN ; stop condition 577 bsf SSP1CON2,PEN ; stop condition
560 rcall WaitMSSP ; wait for TX to complete 578 rcall WaitMSSP ; wait for TX to complete
577 I2C_init_compass0: 595 I2C_init_compass0:
578 ; magnetic 596 ; magnetic
579 bsf SSP1CON2,SEN ; start condition 597 bsf SSP1CON2,SEN ; start condition
580 rcall WaitMSSP ; wait for TX to complete 598 rcall WaitMSSP ; wait for TX to complete
581 movlw 0x3C ; address 599 movlw 0x3C ; address
600 movff WREG,i2c_error_vault+0 ; Store address
582 rcall I2C_TX ; send byte 601 rcall I2C_TX ; send byte
583 movlw 0x00 ; ?? 602 movlw 0x00 ; ??
584 rcall I2C_TX ; send byte 603 rcall I2C_TX ; send byte
585 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged 604 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged
586 rcall I2C_TX ; send byte 605 rcall I2C_TX ; send byte
600 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode 619 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode
601 620
602 bsf SSP1CON2,SEN ; start condition 621 bsf SSP1CON2,SEN ; start condition
603 rcall WaitMSSP ; wait for TX to complete 622 rcall WaitMSSP ; wait for TX to complete
604 movlw 0x38 ; address 623 movlw 0x38 ; address
624 movff WREG,i2c_error_vault+0 ; Store address
605 rcall I2C_TX ; send byte 625 rcall I2C_TX ; send byte
606 movlw 0x0E ; XYZ_DATA_CFG 626 movlw 0x0E ; XYZ_DATA_CFG
607 rcall I2C_TX ; send byte 627 rcall I2C_TX ; send byte
608 movlw b'00000000' ; high pass filter = 0, +/- 2 g range 628 movlw b'00000000' ; high pass filter = 0, +/- 2 g range
609 rcall I2C_TX ; send byte 629 rcall I2C_TX ; send byte
631 rcall I2C_TX ; send byte 651 rcall I2C_TX ; send byte
632 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode 652 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode
633 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode 653 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode
634 rcall I2C_TX ; send byte 654 rcall I2C_TX ; send byte
635 bsf SSP1CON2,PEN ; stop condition 655 bsf SSP1CON2,PEN ; stop condition
636 bra WaitMSSP ; wait for TX to complete and return 656 rcall WaitMSSP ; wait for TX to complete
657 return ; done
658
637 659
638 660
639 I2C_init_compass1: 661 I2C_init_compass1:
640 bsf SSP1CON2,SEN ; start condition 662 bsf SSP1CON2,SEN ; start condition
641 rcall WaitMSSP ; wait for TX to complete 663 rcall WaitMSSP ; wait for TX to complete
642 movlw 0x3C ; address 664 movlw 0x3C ; address
665 movff WREG,i2c_error_vault+0 ; Store address
643 rcall I2C_TX ; send byte 666 rcall I2C_TX ; send byte
644 movlw 0x9F ; 1F with auto-increment (MSB=1) 667 movlw 0x9F ; 1F with auto-increment (MSB=1)
645 rcall I2C_TX ; send byte 668 rcall I2C_TX ; send byte
646 movlw b'00000000' ; CTRL0 669 movlw b'00000000' ; CTRL0
647 rcall I2C_TX ; send byte 670 rcall I2C_TX ; send byte
673 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss) 696 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
674 rcall I2C_TX ; send byte 697 rcall I2C_TX ; send byte
675 movlw b'00000000' ; CTRL7 Continuous Mode 698 movlw b'00000000' ; CTRL7 Continuous Mode
676 rcall I2C_TX ; send byte 699 rcall I2C_TX ; send byte
677 bsf SSP1CON2,PEN ; stop condition 700 bsf SSP1CON2,PEN ; stop condition
678 bra WaitMSSP ; wait for TX to complete and return 701 rcall WaitMSSP ; wait for TX to complete
702 return ; done
679 703
680 ; accelerometer initializes along with magnetic sensor 704 ; accelerometer initializes along with magnetic sensor
681 705
682 706
683 I2C_init_compass2: 707 I2C_init_compass2:
684 ; magnetic 708 ; magnetic
685 bsf SSP1CON2,SEN ; start condition 709 bsf SSP1CON2,SEN ; start condition
686 rcall WaitMSSP ; wait for TX to complete 710 rcall WaitMSSP ; wait for TX to complete
687 movlw 0x3C ; address 711 movlw 0x3C ; address
712 movff WREG,i2c_error_vault+0 ; Store address
688 rcall I2C_TX ; send byte 713 rcall I2C_TX ; send byte
689 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 714 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
690 rcall I2C_TX ; send byte 715 rcall I2C_TX ; send byte
691 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 0x00 716 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 0x00
692 rcall I2C_TX ; send byte 717 rcall I2C_TX ; send byte
699 724
700 ; accelerometer 725 ; accelerometer
701 bsf SSP1CON2,SEN ; start condition 726 bsf SSP1CON2,SEN ; start condition
702 rcall WaitMSSP ; wait for TX to complete 727 rcall WaitMSSP ; wait for TX to complete
703 movlw 0x32 ; address 728 movlw 0x32 ; address
729 movff WREG,i2c_error_vault+0 ; Store address
704 rcall I2C_TX ; send byte 730 rcall I2C_TX ; send byte
705 movlw 0x9F ; 1F with auto-increment (MSB=1) 731 movlw 0x9F ; 1F with auto-increment (MSB=1)
706 rcall I2C_TX ; send byte 732 rcall I2C_TX ; send byte
707 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 733 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
708 rcall I2C_TX ; send byte 734 rcall I2C_TX ; send byte
715 movlw b'00001000' ; CTRL_REG4_A (BDU=0, +/-2g, HR=1) 741 movlw b'00001000' ; CTRL_REG4_A (BDU=0, +/-2g, HR=1)
716 rcall I2C_TX ; send byte 742 rcall I2C_TX ; send byte
717 ; movlw b'00000000' ; CTRL_REG5_A 743 ; movlw b'00000000' ; CTRL_REG5_A
718 ; rcall I2C_TX ; send byte 744 ; rcall I2C_TX ; send byte
719 bsf SSP1CON2,PEN ; stop condition 745 bsf SSP1CON2,PEN ; stop condition
720 bra WaitMSSP ; wait for TX to complete and return 746 rcall WaitMSSP ; wait for TX to complete
747 return ; done
721 748
722 749
723 I2C_init_compass3: 750 I2C_init_compass3:
724 ; magnetic 751 ; magnetic
725 bsf SSP1CON2,SEN ; start condition 752 bsf SSP1CON2,SEN ; start condition
726 rcall WaitMSSP ; wait for TX to complete 753 rcall WaitMSSP ; wait for TX to complete
727 movlw 0x3C ; address 754 movlw 0x3C ; address
755 movff WREG,i2c_error_vault+0 ; Store address
728 rcall I2C_TX ; send byte 756 rcall I2C_TX ; send byte
729 movlw 0xA0 ; 0x20 with auto-increment (MSB=1) 757 movlw 0xA0 ; 0x20 with auto-increment (MSB=1)
730 rcall I2C_TX ; send byte 758 rcall I2C_TX ; send byte
731 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20 759 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20
732 rcall I2C_TX ; send byte 760 rcall I2C_TX ; send byte
745 773
746 ;accelerometer 774 ;accelerometer
747 bsf SSP1CON2,SEN ; start condition 775 bsf SSP1CON2,SEN ; start condition
748 rcall WaitMSSP ; wait for TX to complete 776 rcall WaitMSSP ; wait for TX to complete
749 movlw 0x3A ; address 777 movlw 0x3A ; address
778 movff WREG,i2c_error_vault+0 ; Store address
750 rcall I2C_TX ; send byte 779 rcall I2C_TX ; send byte
751 movlw 0x20 780 movlw 0x20
752 rcall I2C_TX ; send byte 781 rcall I2C_TX ; send byte
753 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20 782 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20
754 rcall I2C_TX ; send byte 783 rcall I2C_TX ; send byte
757 movlw b'00000000' ; CTRL_REG3_A 0x22 786 movlw b'00000000' ; CTRL_REG3_A 0x22
758 rcall I2C_TX ; send byte 787 rcall I2C_TX ; send byte
759 movlw b'11001100' ; CTRL_REG4_A 0x23 788 movlw b'11001100' ; CTRL_REG4_A 0x23
760 rcall I2C_TX ; send byte 789 rcall I2C_TX ; send byte
761 bsf SSP1CON2,PEN ; stop condition 790 bsf SSP1CON2,PEN ; stop condition
762 bra WaitMSSP ; wait for TX to complete and return 791 rcall WaitMSSP ; wait for TX to complete
763 792 return ; done
793
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
764 801
765 ;----------------------------------------------------------------------------- 802 ;-----------------------------------------------------------------------------
766 ; Deactivate Compass / Accelerometer 803 ; Deactivate Compass / Accelerometer
767 ; 804 ;
768 global I2C_sleep_compass 805 global I2C_sleep_compass
782 I2C_sleep_compass0: 819 I2C_sleep_compass0:
783 ; magnetic 820 ; magnetic
784 bsf SSP1CON2,SEN ; start condition 821 bsf SSP1CON2,SEN ; start condition
785 rcall WaitMSSP ; wait for TX to complete 822 rcall WaitMSSP ; wait for TX to complete
786 movlw 0x3C ; address 823 movlw 0x3C ; address
824 movff WREG,i2c_error_vault+0 ; Store address
787 rcall I2C_TX ; send byte 825 rcall I2C_TX ; send byte
788 movlw 0x00 ; ?? 826 movlw 0x00 ; ??
789 rcall I2C_TX ; send byte 827 rcall I2C_TX ; send byte
790 movlw b'01101000' ; ConfigA 828 movlw b'01101000' ; ConfigA
791 rcall I2C_TX ; send byte 829 rcall I2C_TX ; send byte
799 I2C_sleep_accelerometer0: 837 I2C_sleep_accelerometer0:
800 ; accelerometer 838 ; accelerometer
801 bsf SSP1CON2,SEN ; start condition 839 bsf SSP1CON2,SEN ; start condition
802 rcall WaitMSSP ; wait for TX to complete 840 rcall WaitMSSP ; wait for TX to complete
803 movlw 0x38 ; address 841 movlw 0x38 ; address
842 movff WREG,i2c_error_vault+0 ; Store address
804 rcall I2C_TX ; send byte 843 rcall I2C_TX ; send byte
805 movlw 0x2A ; CTRL_REG1 844 movlw 0x2A ; CTRL_REG1
806 rcall I2C_TX ; send byte 845 rcall I2C_TX ; send byte
807 movlw b'00000000' ; standby mode 846 movlw b'00000000' ; standby mode
808 rcall I2C_TX ; send byte 847 rcall I2C_TX ; send byte
809 bsf SSP1CON2,PEN ; stop condition 848 bsf SSP1CON2,PEN ; stop condition
810 bra WaitMSSP ; wait for TX to complete and return 849 rcall WaitMSSP ; wait for TX to complete
850 return ; done
811 851
812 852
813 I2C_sleep_compass1: 853 I2C_sleep_compass1:
814 bsf SSP1CON2,SEN ; start condition 854 bsf SSP1CON2,SEN ; start condition
815 rcall WaitMSSP ; wait for TX to complete 855 rcall WaitMSSP ; wait for TX to complete
816 movlw 0x3C ; address 856 movlw 0x3C ; address
857 movff WREG,i2c_error_vault+0 ; Store address
817 rcall I2C_TX ; send byte 858 rcall I2C_TX ; send byte
818 movlw 0x20 ; CTRL_REG1 859 movlw 0x20 ; CTRL_REG1
819 rcall I2C_TX ; send byte 860 rcall I2C_TX ; send byte
820 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode 861 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode
821 rcall I2C_TX ; send byte 862 rcall I2C_TX ; send byte
828 movlw 0x26 ; CTRL_REG7 869 movlw 0x26 ; CTRL_REG7
829 rcall I2C_TX ; send byte 870 rcall I2C_TX ; send byte
830 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode 871 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode
831 rcall I2C_TX ; send byte 872 rcall I2C_TX ; send byte
832 bsf SSP1CON2,PEN ; stop condition 873 bsf SSP1CON2,PEN ; stop condition
833 bra WaitMSSP ; wait for TX to complete and return 874 rcall WaitMSSP ; wait for TX to complete
875 return ; done
834 876
835 ; accelerometer sleeps with magnetic sensor 877 ; accelerometer sleeps with magnetic sensor
836 878
837 879
838 I2C_sleep_compass2: 880 I2C_sleep_compass2:
839 ; magnetic 881 ; magnetic
840 bsf SSP1CON2,SEN ; start condition 882 bsf SSP1CON2,SEN ; start condition
841 rcall WaitMSSP ; wait for TX to complete 883 rcall WaitMSSP ; wait for TX to complete
842 movlw 0x3C ; address 884 movlw 0x3C ; address
885 movff WREG,i2c_error_vault+0 ; Store address
843 rcall I2C_TX ; send byte 886 rcall I2C_TX ; send byte
844 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 887 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
845 rcall I2C_TX ; send byte 888 rcall I2C_TX ; send byte
846 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode)) 889 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode))
847 rcall I2C_TX ; send byte 890 rcall I2C_TX ; send byte
856 899
857 ; accelerometer 900 ; accelerometer
858 bsf SSP1CON2,SEN ; start condition 901 bsf SSP1CON2,SEN ; start condition
859 rcall WaitMSSP ; wait for TX to complete 902 rcall WaitMSSP ; wait for TX to complete
860 movlw 0x32 ; address 903 movlw 0x32 ; address
904 movff WREG,i2c_error_vault+0 ; Store address
861 rcall I2C_TX ; send byte 905 rcall I2C_TX ; send byte
862 movlw 0x9F ; 1F with auto-increment (MSB=1) 906 movlw 0x9F ; 1F with auto-increment (MSB=1)
863 rcall I2C_TX ; send byte 907 rcall I2C_TX ; send byte
864 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off) 908 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off)
865 rcall I2C_TX ; send byte 909 rcall I2C_TX ; send byte
866 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off) 910 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off)
867 rcall I2C_TX ; send byte 911 rcall I2C_TX ; send byte
868 bsf SSP1CON2,PEN ; stop condition 912 bsf SSP1CON2,PEN ; stop condition
869 bra WaitMSSP ; wait for TX to complete and return 913 rcall WaitMSSP ; wait for TX to complete
914 return ; done
870 915
871 916
872 I2C_sleep_compass3: 917 I2C_sleep_compass3:
873 ; magnetic 918 ; magnetic
874 bsf SSP1CON2,SEN ; start condition 919 bsf SSP1CON2,SEN ; start condition
875 rcall WaitMSSP ; wait for TX to complete 920 rcall WaitMSSP ; wait for TX to complete
876 movlw 0x3C ; address 921 movlw 0x3C ; address
922 movff WREG,i2c_error_vault+0 ; Store address
877 rcall I2C_TX ; send byte 923 rcall I2C_TX ; send byte
878 movlw 0xA2 ; 0x22 with auto-increment (MSB=1) 924 movlw 0xA2 ; 0x22 with auto-increment (MSB=1)
879 rcall I2C_TX ; send byte 925 rcall I2C_TX ; send byte
880 movlw b'01000010' ; CTRL_REG3_M (power-down) 0x22 926 movlw b'01000010' ; CTRL_REG3_M (power-down) 0x22
881 rcall I2C_TX ; send byte 927 rcall I2C_TX ; send byte
884 930
885 ; accelerometer 931 ; accelerometer
886 bsf SSP1CON2,SEN ; start condition 932 bsf SSP1CON2,SEN ; start condition
887 rcall WaitMSSP ; wait for TX to complete 933 rcall WaitMSSP ; wait for TX to complete
888 movlw 0x3A ; address 934 movlw 0x3A ; address
935 movff WREG,i2c_error_vault+0 ; Store address
889 rcall I2C_TX ; send byte 936 rcall I2C_TX ; send byte
890 movlw 0x20 937 movlw 0x20
891 rcall I2C_TX ; send byte 938 rcall I2C_TX ; send byte
892 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20 939 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20
893 rcall I2C_TX ; send byte 940 rcall I2C_TX ; send byte
894 bsf SSP1CON2,PEN ; stop condition 941 bsf SSP1CON2,PEN ; stop condition
895 bra WaitMSSP ; wait for TX to complete and return 942 rcall WaitMSSP ; wait for TX to complete
943 return ; done
896 944
897 945
898 ;----------------------------------------------------------------------------- 946 ;-----------------------------------------------------------------------------
899 ; Helper Function - wait for TX to complete 947 ; Helper Function - wait for TX to complete
900 ; 948 ;
901 WaitMSSP: 949 WaitMSSP:
950 movff SSP1BUF,i2c_error_vault+1
902 clrf i2c_temp1 ; wait for max 256 loops 951 clrf i2c_temp1 ; wait for max 256 loops
903 WaitMSSP_loop: 952 WaitMSSP_loop:
904 decfsz i2c_temp1,F ; decrement loop counter, timeout? 953 decfsz i2c_temp1,F ; decrement loop counter, timeout?
905 bra WaitMSSP2 ; NO 954 bra WaitMSSP2 ; NO
906 bra I2CFail ; YES 955 bra I2CFail ; YES
927 bsf active_reset_ostc_rx ; reset RX circuitry (which may be the cause for the hang up) 976 bsf active_reset_ostc_rx ; reset RX circuitry (which may be the cause for the hang up)
928 rcall I2CReset ; reset I2C 977 rcall I2CReset ; reset I2C
929 bcf PIR1,SSP1IF ; clear TX completion flag 978 bcf PIR1,SSP1IF ; clear TX completion flag
930 bsf i2c_error_flag ; set error flag 979 bsf i2c_error_flag ; set error flag
931 bcf active_reset_ostc_rx ; release reset from RX circuitry 980 bcf active_reset_ostc_rx ; release reset from RX circuitry
981 ; bcf i2c_busy_temperature
982 ; bcf i2c_busy_pressure
932 return ; done 983 return ; done
933 984
934 985
935 ;----------------------------------------------------------------------------- 986 ;-----------------------------------------------------------------------------
936 ; Helper Function - Reset I2C Module 987 ; Helper Function - Reset I2C Module
970 movwf SSP1STAT ; ... 1021 movwf SSP1STAT ; ...
971 movlw b'00101000' ; configure I2C module 1022 movlw b'00101000' ; configure I2C module
972 movwf SSP1CON1 ; ... 1023 movwf SSP1CON1 ; ...
973 movlw b'00000000' ; ... 1024 movlw b'00000000' ; ...
974 movwf SSP1CON2 ; ... 1025 movwf SSP1CON2 ; ...
975 movlw 0x9C ; ... 1026 movlw i2c_speed_value
976 movwf SSP1ADD ; ... 1027 movwf SSP1ADD ; ...
977 return ; done 1028 return ; done
978 1029
979 1030
980 ;----------------------------------------------------------------------------- 1031 ;-----------------------------------------------------------------------------
1005 movlw b'11111000' ; automatic conversion every two seconds 1056 movlw b'11111000' ; automatic conversion every two seconds
1006 movwf SSP1BUF ; data byte 1057 movwf SSP1BUF ; data byte
1007 rcall WaitMSSP ; wait for TX to complete 1058 rcall WaitMSSP ; wait for TX to complete
1008 rcall I2C_Check_ACK ; check for acknowledge by receiver 1059 rcall I2C_Check_ACK ; check for acknowledge by receiver
1009 bsf SSP1CON2,PEN ; stop condition 1060 bsf SSP1CON2,PEN ; stop condition
1010 bra WaitMSSP ; wait for TX to complete and return 1061 rcall WaitMSSP ; wait for TX to complete
1062 return ; done
1011 1063
1012 ;----------------------------------------------------------------------------- 1064 ;-----------------------------------------------------------------------------
1013 ; Sleep Gauge IC 1065 ; Sleep Gauge IC
1014 ; 1066 ;
1015 global lt2942_sleep 1067 global lt2942_sleep
1019 movlw b'00111000' ; sleep 1071 movlw b'00111000' ; sleep
1020 movwf SSP1BUF ; data byte 1072 movwf SSP1BUF ; data byte
1021 rcall WaitMSSP ; wait for TX to complete 1073 rcall WaitMSSP ; wait for TX to complete
1022 rcall I2C_Check_ACK ; check for acknowledge by receiver 1074 rcall I2C_Check_ACK ; check for acknowledge by receiver
1023 bsf SSP1CON2,PEN ; stop condition 1075 bsf SSP1CON2,PEN ; stop condition
1024 bra WaitMSSP ; wait for TX to complete and return 1076 rcall WaitMSSP ; wait for TX to complete
1077 return ; done
1025 1078
1026 ;----------------------------------------------------------------------------- 1079 ;-----------------------------------------------------------------------------
1027 ; Read Gauge IC - Status Register 1080 ; Read Gauge IC - Status Register
1028 ; 1081 ;
1029 global lt2942_get_status 1082 global lt2942_get_status
1030 lt2942_get_status: 1083 lt2942_get_status:
1031 bcf battery_gauge_available ; clear flag 1084 bcf battery_gauge_available ; clear flag
1032 movlw 0x00 ; point to status register 1085 movlw 0x00 ; point to status register
1033 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC 1086 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
1034 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC 1087 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
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
1035 movff SSP1BUF,WREG ; copy received byte to WREG 1094 movff SSP1BUF,WREG ; copy received byte to WREG
1036 btfss WREG,7 ; 2942 found? 1095 btfss WREG,7 ; 2942 found?
1037 bsf battery_gauge_available ; YES - set flag 1096 bsf battery_gauge_available ; YES - set flag
1038 bsf SSP1CON2,PEN ; stop condition 1097 bsf SSP1CON2,PEN ; stop condition
1039 bra WaitMSSP ; wait for TX to complete and return 1098 rcall WaitMSSP ; wait for TX to complete
1099 return ; done
1040 1100
1041 1101
1042 ;----------------------------------------------------------------------------- 1102 ;-----------------------------------------------------------------------------
1043 ; Read Gauge IC - Voltage 1103 ; Read Gauge IC - Voltage
1044 ; 1104 ;
1051 rcall WaitMSSP ; wait for TX to complete 1111 rcall WaitMSSP ; wait for TX to complete
1052 movff SSP1BUF,xA+1 ; copy received byte to xA+1 1112 movff SSP1BUF,xA+1 ; copy received byte to xA+1
1053 bsf SSP1CON2,RCEN ; enable receive mode 1113 bsf SSP1CON2,RCEN ; enable receive mode
1054 rcall WaitMSSP ; wait for TX to complete 1114 rcall WaitMSSP ; wait for TX to complete
1055 movff SSP1BUF,xA+0 ; copy received byte to xA+0 1115 movff SSP1BUF,xA+0 ; copy received byte to xA+0
1056 bsf SSP1CON2,PEN ; stop condition 1116 bsf SSP1CON2,ACKDT ; set ACKDT flag
1057 rcall WaitMSSP ; wait for TX to complete 1117 bsf SSP1CON2,ACKEN ; master NOT acknowledge
1058 1118 rcall WaitMSSP ; wait for TX to complete
1119 bcf SSP1CON2,ACKDT ; reset ACKDT flag
1120
1121 bsf SSP1CON2,PEN ; stop condition
1122 rcall WaitMSSP ; wait for TX to complete
1123
1059 ; convert voltage from raw value to Volt 1124 ; convert voltage from raw value to Volt
1060 MOVLI .6000,xB ; load conversion multiplicand into xB 1125 MOVLI .6000,xB ; load conversion multiplicand into xB
1061 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand 1126 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
1062 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % 1127 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
1063 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 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
1072 ; Read Gauge IC - Temperature 1137 ; Read Gauge IC - Temperature
1073 ; 1138 ;
1074 global lt2942_get_temperature 1139 global lt2942_get_temperature
1075 lt2942_get_temperature: ; read battery temperature 1140 lt2942_get_temperature: ; read battery temperature
1076 movlw 0x0C ; point to temperature register 1141 movlw 0x0C ; point to temperature register
1077 call I2C_TX_GAUGE ; send byte to the LT2942 gauge IC 1142 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
1078 call I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC 1143 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
1079 bsf SSP1CON2,ACKEN ; master acknowledge 1144 bsf SSP1CON2,ACKEN ; master acknowledge
1080 rcall WaitMSSP ; wait for TX to complete 1145 rcall WaitMSSP ; wait for TX to complete
1081 movff SSP1BUF,xA+1 ; store raw temperature, high byte 1146 movff SSP1BUF,xA+1 ; store raw temperature, high byte
1082 bsf SSP1CON2,RCEN ; enable receive mode 1147 bsf SSP1CON2,RCEN ; enable receive mode
1083 rcall WaitMSSP ; wait for TX to complete 1148 rcall WaitMSSP ; wait for TX to complete
1084 movff SSP1BUF,xA+0 ; store raw temperature, low byte 1149 movff SSP1BUF,xA+0 ; store raw temperature, low byte
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
1085 bsf SSP1CON2,PEN ; stop condition 1155 bsf SSP1CON2,PEN ; stop condition
1086 rcall WaitMSSP ; wait for TX to complete 1156 rcall WaitMSSP ; wait for TX to complete
1087 1157
1088 ; convert temperature from raw value to Kelvin 1158 ; convert temperature from raw value to Kelvin
1089 MOVLI .6000,xB ; load conversion multiplicand into xB 1159 MOVLI .6000,xB ; load conversion multiplicand into xB
1145 rcall WaitMSSP ; wait for TX to complete 1215 rcall WaitMSSP ; wait for TX to complete
1146 1216
1147 bsf SSP1CON2,RCEN ; enable receive mode 1217 bsf SSP1CON2,RCEN ; enable receive mode
1148 rcall WaitMSSP ; wait for TX to complete 1218 rcall WaitMSSP ; wait for TX to complete
1149 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0 1219 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0
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
1150 bsf SSP1CON2,PEN ; stop condition 1225 bsf SSP1CON2,PEN ; stop condition
1151 rcall WaitMSSP ; wait for TX to complete 1226 rcall WaitMSSP ; wait for TX to complete
1152 1227
1153 btfsc gauge_status_byte,0 ; UVLO event ? 1228 btfsc gauge_status_byte,0 ; UVLO event ?
1154 rcall lt2942_init_again ; YES - do an re-initialization 1229 rcall lt2942_init_again ; YES - do an re-initialization
1155
1156 MOVII sub_a,battery_accumulated_charge ; save raw value 1230 MOVII sub_a,battery_accumulated_charge ; save raw value
1157 1231
1158 ; Compute batt_percent = (charge - battery_offset) / 365 1232 ; Compute batt_percent = (charge - battery_offset) / 365
1159 MOVII battery_offset,sub_b ; get battery offset 1233 MOVII battery_offset,sub_b ; get battery offset
1160 call subU16 ; sub_c = sub_a - sub_b (with signed values) 1234 call subU16 ; sub_c = sub_a - sub_b (with signed values)
1180 rcall I2C_Check_ACK ; check for acknowledge by receiver 1254 rcall I2C_Check_ACK ; check for acknowledge by receiver
1181 movff battery_offset+0,SSP1BUF ; send battery offset, low byte 1255 movff battery_offset+0,SSP1BUF ; send battery offset, low byte
1182 rcall WaitMSSP ; wait for TX to complete 1256 rcall WaitMSSP ; wait for TX to complete
1183 rcall I2C_Check_ACK ; check for acknowledge by receiver 1257 rcall I2C_Check_ACK ; check for acknowledge by receiver
1184 bsf SSP1CON2,PEN ; stop condition 1258 bsf SSP1CON2,PEN ; stop condition
1185 bra WaitMSSP ; wait for TX to complete and return 1259 rcall WaitMSSP ; wait for TX to complete
1260 return ; done
1186 1261
1187 1262
1188 ;----------------------------------------------------------------------------- 1263 ;-----------------------------------------------------------------------------
1189 ; Read Gauge IC - Reset Accumulating Register to 0xFFFF 1264 ; Read Gauge IC - Reset Accumulating Register to 0xFFFF
1190 ; 1265 ;
1197 rcall I2C_Check_ACK ; check for acknowledge by receiver 1272 rcall I2C_Check_ACK ; check for acknowledge by receiver
1198 setf SSP1BUF ; data byte 1273 setf SSP1BUF ; data byte
1199 rcall WaitMSSP ; wait for TX to complete 1274 rcall WaitMSSP ; wait for TX to complete
1200 rcall I2C_Check_ACK ; check for acknowledge by receiver 1275 rcall I2C_Check_ACK ; check for acknowledge by receiver
1201 bsf SSP1CON2,PEN ; stop condition 1276 bsf SSP1CON2,PEN ; stop condition
1202 bra WaitMSSP ; wait for TX to complete and return 1277 rcall WaitMSSP ; wait for TX to complete
1278 return ; done
1203 1279
1204 1280
1205 ;----------------------------------------------------------------------------- 1281 ;-----------------------------------------------------------------------------
1206 ; Helper Function - send 1 Byte to the LT2942 Gauge IC 1282 ; Helper Function - send 1 Byte to the LT2942 Gauge IC
1207 ; 1283 ;
1208 I2C_TX_GAUGE: 1284 I2C_TX_GAUGE:
1209 movwf i2c_temp2 ; save data byte to be sent 1285 movwf i2c_temp2 ; save data byte to be sent
1210 bsf SSP1CON2,SEN ; start condition 1286 bsf SSP1CON2,SEN ; start condition
1211 rcall WaitMSSP ; wait for TX to complete 1287 rcall WaitMSSP ; wait for TX to complete
1212 movlw b'11001000' ; address byte + Write bit 1288 movlw 0xC8 ; address byte + Write bit
1289 movff WREG,i2c_error_vault+0 ; Store address
1213 movwf SSP1BUF ; control byte 1290 movwf SSP1BUF ; control byte
1214 rcall WaitMSSP ; wait for TX to complete 1291 rcall WaitMSSP ; wait for TX to complete
1215 rcall I2C_Check_ACK ; check for acknowledge by receiver 1292 rcall I2C_Check_ACK ; check for acknowledge by receiver
1216 movf i2c_temp2,W ; restore data byte to be sent 1293 movf i2c_temp2,W ; restore data byte to be sent
1217 bra I2C_TX ; send byte and return 1294 bra I2C_TX ; send byte and return
1219 1296
1220 ;----------------------------------------------------------------------------- 1297 ;-----------------------------------------------------------------------------
1221 ; Helper Function - receive 1 Byte from the LT2942 Gauge IC 1298 ; Helper Function - receive 1 Byte from the LT2942 Gauge IC
1222 ; 1299 ;
1223 I2C_RX_GAUGE: 1300 I2C_RX_GAUGE:
1224 bsf SSP1CON2,SEN ; start condition 1301 bsf SSP1CON2,RSEN ; repeated start condition
1225 rcall WaitMSSP ; wait for TX to complete 1302 rcall WaitMSSP ; wait for TX to complete
1226 movlw b'11001001' ; address byte + Read bit 1303 movlw 0xC9 ; address byte + Read bit
1304 movff WREG,i2c_error_vault+0 ; Store address
1227 movwf SSP1BUF ; control byte 1305 movwf SSP1BUF ; control byte
1228 rcall WaitMSSP ; wait for TX to complete 1306 rcall WaitMSSP ; wait for TX to complete
1229 rcall I2C_Check_ACK ; check for acknowledge by receiver 1307 rcall I2C_Check_ACK ; check for acknowledge by receiver
1230 bsf SSP1CON2,RCEN ; enable receive mode 1308 bsf SSP1CON2,RCEN ; enable receive mode
1231 bra WaitMSSP ; wait for reception and return 1309 bra WaitMSSP ; wait for reception and return
1278 movwf hy ; initialize loop counter for tries 1356 movwf hy ; initialize loop counter for tries
1279 I2C_probe_OSTC_rx_1: 1357 I2C_probe_OSTC_rx_1:
1280 bsf SSP1CON2,SEN ; start condition 1358 bsf SSP1CON2,SEN ; start condition
1281 rcall WaitMSSP ; wait for TX to complete 1359 rcall WaitMSSP ; wait for TX to complete
1282 movlw 0x50 ; address byte + write bit 1360 movlw 0x50 ; address byte + write bit
1361 movff WREG,i2c_error_vault+0 ; Store address
1283 movwf SSP1BUF ; control byte 1362 movwf SSP1BUF ; control byte
1284 rcall WaitMSSP ; wait for TX to complete 1363 rcall WaitMSSP ; wait for TX to complete
1285 btfss SSP1CON2,ACKSTAT ; ACK received? 1364 btfss SSP1CON2,ACKSTAT ; ACK received?
1286 bsf ostc_rx_present ; YES - TR module detected 1365 bsf ostc_rx_present ; YES - TR module detected
1287 bsf SSP1CON2,PEN ; stop condition 1366 bsf SSP1CON2,PEN ; stop condition
1288 rcall WaitMSSP ; wait for TX to complete 1367 rcall WaitMSSP ; wait for TX to complete
1289 btfss ostc_rx_present ; was a TR module detected? 1368 btfss ostc_rx_present ; was a TR module detected?
1290 return ; NO - done 1369 return ; NO - done
1291 1370
1292 WAITMS .1 ; wait 1 ms 1371 WAITMS .1 ; wait 1 ms
1293
1294 bsf SSP1CON2,SEN ; start condition 1372 bsf SSP1CON2,SEN ; start condition
1295 rcall WaitMSSP ; wait for TX to complete 1373 rcall WaitMSSP ; wait for TX to complete
1296 movlw 0x50 ; address byte + write bit 1374 movlw 0x50 ; address byte + write bit
1297 movwf SSP1BUF ; control byte 1375 movwf SSP1BUF ; control byte
1298 rcall WaitMSSP ; wait for TX to complete 1376 rcall WaitMSSP ; wait for TX to complete
1325 bsf SSP1CON2,ACKEN ; master NOT acknowledge 1403 bsf SSP1CON2,ACKEN ; master NOT acknowledge
1326 rcall WaitMSSP ; wait for TX to complete 1404 rcall WaitMSSP ; wait for TX to complete
1327 bcf SSP1CON2,ACKDT ; reset ACKDT flag 1405 bcf SSP1CON2,ACKDT ; reset ACKDT flag
1328 bsf SSP1CON2,PEN ; stop condition 1406 bsf SSP1CON2,PEN ; stop condition
1329 rcall WaitMSSP ; wait for TX to complete 1407 rcall WaitMSSP ; wait for TX to complete
1330 1408
1331 ; wait for TR module becoming ready 1409 ; wait for TR module becoming ready
1332 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common 1410 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common
1333 movlw .147 ; code for not ready, minor 1411 movlw .147 ; code for not ready, minor
1334 cpfseq i2c_temp1 ; equal? 1412 cpfseq i2c_temp1 ; equal?
1335 return ; NO - TR module ready, done 1413 return ; NO - TR module ready, done
1354 global I2C_get_tankdata 1432 global I2C_get_tankdata
1355 I2C_get_tankdata: 1433 I2C_get_tankdata:
1356 bsf SSP1CON2,SEN ; start condition 1434 bsf SSP1CON2,SEN ; start condition
1357 rcall WaitMSSP ; wait for TX to complete 1435 rcall WaitMSSP ; wait for TX to complete
1358 movlw 0x50 ; address byte + write bit 1436 movlw 0x50 ; address byte + write bit
1437 movff WREG,i2c_error_vault+0 ; Store address
1359 movwf SSP1BUF ; control byte 1438 movwf SSP1BUF ; control byte
1360 rcall WaitMSSP ; wait for TX to complete 1439 rcall WaitMSSP ; wait for TX to complete
1361 rcall I2C_Check_ACK ; check for acknowledge by receiver 1440 rcall I2C_Check_ACK ; check for acknowledge by receiver
1362 movlw 0x1E ; read buffer2 (48 bytes) 1441 movlw 0x1E ; read buffer2 (48 bytes)
1363 movwf SSP1BUF ; data byte 1442 movwf SSP1BUF ; data byte
1394 bsf SSP1CON2,ACKDT ; set ACKDT flag 1473 bsf SSP1CON2,ACKDT ; set ACKDT flag
1395 bsf SSP1CON2,ACKEN ; master NOT acknowledge 1474 bsf SSP1CON2,ACKEN ; master NOT acknowledge
1396 rcall WaitMSSP ; wait for TX to complete 1475 rcall WaitMSSP ; wait for TX to complete
1397 bcf SSP1CON2,ACKDT ; reset ACKDT flag 1476 bcf SSP1CON2,ACKDT ; reset ACKDT flag
1398 bsf SSP1CON2,PEN ; stop condition 1477 bsf SSP1CON2,PEN ; stop condition
1399 bra WaitMSSP ; wait for TX to complete and return 1478 rcall WaitMSSP ; wait for TX to complete
1479 return ; done
1480
1400 1481
1401 1482
1402 ;----------------------------------------------------------------------------- 1483 ;-----------------------------------------------------------------------------
1403 ; OSTC TR - Firmware Update 1484 ; OSTC TR - Firmware Update
1404 ; 1485 ;
1413 movwf i2c_temp2 ; ... 1494 movwf i2c_temp2 ; ...
1414 ; address write 1495 ; address write
1415 bsf SSP1CON2,SEN ; start condition 1496 bsf SSP1CON2,SEN ; start condition
1416 rcall WaitMSSP ; wait for TX to complete 1497 rcall WaitMSSP ; wait for TX to complete
1417 movlw 0x50 ; address byte + write bit 1498 movlw 0x50 ; address byte + write bit
1499 movff WREG,i2c_error_vault+0 ; Store address
1418 movwf SSP1BUF ; control byte 1500 movwf SSP1BUF ; control byte
1419 rcall WaitMSSP ; wait for TX to complete 1501 rcall WaitMSSP ; wait for TX to complete
1420 rcall I2C_Check_ACK ; check for acknowledge by receiver 1502 rcall I2C_Check_ACK ; check for acknowledge by receiver
1421 ; write 64 bytes 1503 ; write 64 bytes
1422 I2C_update_OSTC_loop: 1504 I2C_update_OSTC_loop:
1487 retlw .255 ; YES - error in data transfer occurred 1569 retlw .255 ; YES - error in data transfer occurred
1488 1570
1489 ENDIF ; _rx_update 1571 ENDIF ; _rx_update
1490 1572
1491 ENDIF ; _rx_functions 1573 ENDIF ; _rx_functions
1492 1574
1493 ;----------------------------------------------------------------------------- 1575 ;-----------------------------------------------------------------------------
1494 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
1651
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 ;-----------------------------------------------------------------------------
1495 END 1794 END