Mercurial > public > hwos_code
comparison src/i2c.asm @ 631:185ba2f91f59
3.09 beta 1 release
author | heinrichsweikamp |
---|---|
date | Fri, 28 Feb 2020 15:45:07 +0100 |
parents | cd58f7fc86db |
children | 4050675965ea |
comparison
equal
deleted
inserted
replaced
630:4cd81bdbf15c | 631:185ba2f91f59 |
---|---|
1 ;============================================================================= | 1 ;============================================================================= |
2 ; | 2 ; |
3 ; File i2c.asm combined next generation V3.03.5 | 3 ; File i2c.asm combined next generation V3.08.8 |
4 ; | 4 ; |
5 ; I2C Interface | 5 ; I2C Interface |
6 ; | 6 ; |
7 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. | 7 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. |
8 ;============================================================================= | 8 ;============================================================================= |
47 | 47 |
48 #include "hwos.inc" ; Mandatory header | 48 #include "hwos.inc" ; Mandatory header |
49 #include "wait.inc" | 49 #include "wait.inc" |
50 #include "math.inc" | 50 #include "math.inc" |
51 #include "external_flash.inc" | 51 #include "external_flash.inc" |
52 #include "eeprom_rs232.inc" | |
52 | 53 |
53 | 54 |
54 i2c CODE | 55 i2c CODE |
55 | 56 |
56 ;============================================================================= | 57 ;============================================================================= |
567 rcall I2C_TX | 568 rcall I2C_TX |
568 bsf SSP1CON2,PEN ; stop condition | 569 bsf SSP1CON2,PEN ; stop condition |
569 rcall WaitMSSP | 570 rcall WaitMSSP |
570 | 571 |
571 ; accelerometer | 572 ; accelerometer |
572 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode | 573 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode |
573 | 574 |
574 bsf SSP1CON2,SEN ; start condition | 575 bsf SSP1CON2,SEN ; start condition |
575 rcall WaitMSSP | 576 rcall WaitMSSP |
576 movlw 0x38 ; address | 577 movlw 0x38 ; address |
577 rcall I2C_TX | 578 rcall I2C_TX |
689 bra WaitMSSP ; ... and return | 690 bra WaitMSSP ; ... and return |
690 | 691 |
691 | 692 |
692 I2C_init_compass3: | 693 I2C_init_compass3: |
693 ; magnetic | 694 ; magnetic |
694 bsf SSP1CON2,SEN ; Start condition | 695 bsf SSP1CON2,SEN ; start condition |
695 rcall WaitMSSP | 696 rcall WaitMSSP |
696 movlw 0x3C ; address | 697 movlw 0x3C ; address |
697 rcall I2C_TX | 698 rcall I2C_TX |
698 movlw 0xA0 ; 0x20 with auto-increment (MSB=1) | 699 movlw 0xA0 ; 0x20 with auto-increment (MSB=1) |
699 rcall I2C_TX | 700 rcall I2C_TX |
700 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20 | 701 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20 |
701 rcall I2C_TX | 702 rcall I2C_TX |
702 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21 | 703 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21 |
703 rcall I2C_TX | 704 rcall I2C_TX |
704 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22 | 705 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22 |
705 rcall I2C_TX | 706 rcall I2C_TX |
706 movlw b'00000000' ; CTRL_REG4_M (Z in Low-power mode) 0x23 | 707 movlw b'00000000' ; CTRL_REG4_M (Z in Low-power mode) 0x23 |
707 rcall I2C_TX | 708 rcall I2C_TX |
708 movlw b'00000000' ; CTRL_REG5_M 0x24 | 709 movlw b'00000000' ; CTRL_REG5_M 0x24 |
709 rcall I2C_TX | 710 rcall I2C_TX |
710 movlw b'00000000' ; CTRL_REG5_M 0x24 | 711 movlw b'00000000' ; CTRL_REG5_M 0x24 |
711 rcall I2C_TX | 712 rcall I2C_TX |
712 bsf SSP1CON2,PEN ; Stop condition | 713 bsf SSP1CON2,PEN ; Stop condition |
713 rcall WaitMSSP | 714 rcall WaitMSSP |
714 | 715 |
715 ;accelerometer | 716 ;accelerometer |
716 bsf SSP1CON2,SEN ; Start condition | 717 bsf SSP1CON2,SEN ; start condition |
717 rcall WaitMSSP | 718 rcall WaitMSSP |
718 movlw 0x3A ; address | 719 movlw 0x3A ; address |
719 rcall I2C_TX | 720 rcall I2C_TX |
720 movlw 0x20 | 721 movlw 0x20 |
721 rcall I2C_TX | 722 rcall I2C_TX |
722 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20 | 723 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20 |
723 rcall I2C_TX | 724 rcall I2C_TX |
724 movlw b'00000000' ; CTRL_REG2_A 0x21 | 725 movlw b'00000000' ; CTRL_REG2_A 0x21 |
725 rcall I2C_TX | 726 rcall I2C_TX |
726 movlw b'00000000' ; CTRL_REG3_A 0x22 | 727 movlw b'00000000' ; CTRL_REG3_A 0x22 |
727 rcall I2C_TX | 728 rcall I2C_TX |
728 movlw b'11001100' ; CTRL_REG4_A 0x23 | 729 movlw b'11001100' ; CTRL_REG4_A 0x23 |
729 rcall I2C_TX | 730 rcall I2C_TX |
730 bsf SSP1CON2,PEN ; Stop condition | 731 bsf SSP1CON2,PEN ; stop condition |
731 bra WaitMSSP ; (And return) | 732 bra WaitMSSP ; (and return) |
732 | 733 |
733 | 734 |
734 global I2C_sleep_compass | 735 global I2C_sleep_compass |
735 I2C_sleep_compass: | 736 I2C_sleep_compass: |
736 btfss compass_enabled ; compass active? | 737 btfss compass_enabled ; compass active? |
737 return ; NO - return | 738 return ; NO - return |
738 bcf compass_enabled | 739 bcf compass_enabled |
739 btfsc compass_type3 ; compass 3 ? | 740 btfsc compass_type3 ; compass 3 ? |
740 bra I2C_sleep_compass3 ; YES | 741 bra I2C_sleep_compass3 ; YES |
741 btfsc compass_type2 ; compass 2 ? | 742 btfsc compass_type2 ; compass 2 ? |
742 bra I2C_sleep_compass2 ; YES | 743 bra I2C_sleep_compass2 ; YES |
758 rcall I2C_TX | 759 rcall I2C_TX |
759 movlw b'00000010' ; idle mode | 760 movlw b'00000010' ; idle mode |
760 rcall I2C_TX | 761 rcall I2C_TX |
761 bsf SSP1CON2,PEN ; stop condition | 762 bsf SSP1CON2,PEN ; stop condition |
762 rcall WaitMSSP | 763 rcall WaitMSSP |
763 | 764 |
764 I2C_sleep_accelerometer0: ;(needed) | 765 I2C_sleep_accelerometer0: ;(needed) |
765 ; accelerometer | 766 ; accelerometer |
766 bsf SSP1CON2,SEN ; start condition | 767 bsf SSP1CON2,SEN ; start condition |
767 rcall WaitMSSP | 768 rcall WaitMSSP |
768 movlw 0x38 ; address | 769 movlw 0x38 ; address |
792 movlw 0x26 ; CTRL_REG7 | 793 movlw 0x26 ; CTRL_REG7 |
793 rcall I2C_TX | 794 rcall I2C_TX |
794 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode | 795 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode |
795 rcall I2C_TX | 796 rcall I2C_TX |
796 bsf SSP1CON2,PEN ; stop condition | 797 bsf SSP1CON2,PEN ; stop condition |
797 bra WaitMSSP ; (And return) - no I2C_sleep_accelerometer1 required (sleeps with magnetic sensor) | 798 bra WaitMSSP ; (And return) - no I2C_sleep_accelerometer1 required (sleeps with magnetic sensor) |
798 | 799 |
799 | 800 |
800 I2C_sleep_compass2: | 801 I2C_sleep_compass2: |
801 ; magnetic | 802 ; magnetic |
802 bsf SSP1CON2,SEN ; start condition | 803 bsf SSP1CON2,SEN ; start condition |
803 rcall WaitMSSP | 804 rcall WaitMSSP |
851 movlw 0x20 | 852 movlw 0x20 |
852 rcall I2C_TX | 853 rcall I2C_TX |
853 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20 | 854 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20 |
854 rcall I2C_TX | 855 rcall I2C_TX |
855 bsf SSP1CON2,PEN ; stop condition | 856 bsf SSP1CON2,PEN ; stop condition |
856 bra WaitMSSP ; ... and return | 857 bra WaitMSSP ; ... and return |
857 | 858 |
858 WaitMSSP: | 859 WaitMSSP: |
859 decfsz i2c_temp1,F ; check for timeout during I2C action | 860 decfsz i2c_temp1,F ; check for timeout during I2C action |
860 bra WaitMSSP2 | 861 bra WaitMSSP2 |
861 bra I2CFail ; timeout occurred | 862 bra I2CFail ; timeout occurred |
1074 call div16x16 ; xC = xA / xB with xA as remainder | 1075 call div16x16 ; xC = xA / xB with xA as remainder |
1075 movff xC+0,batt_percent | 1076 movff xC+0,batt_percent |
1076 movlw .100 ; max. value is 100 % | 1077 movlw .100 ; max. value is 100 % |
1077 cpfslt batt_percent ; batt_percent < 100 % ? | 1078 cpfslt batt_percent ; batt_percent < 100 % ? |
1078 movwf batt_percent ; NO - limit to 100 % | 1079 movwf batt_percent ; NO - limit to 100 % |
1079 return | 1080 return ; done |
1080 | 1081 |
1081 lt2942_set_to_zero_percent: | 1082 lt2942_set_to_zero_percent: |
1082 clrf i2c_temp1 | 1083 clrf i2c_temp1 |
1083 movlw 0x02 ; point to accumulated charge registers | 1084 movlw 0x02 ; point to accumulated charge registers |
1084 rcall I2C_TX_GAUGE | 1085 rcall I2C_TX_GAUGE |
1124 movwf SSP1BUF ; control byte | 1125 movwf SSP1BUF ; control byte |
1125 rcall WaitMSSP | 1126 rcall WaitMSSP |
1126 rcall I2C_WaitforACK | 1127 rcall I2C_WaitforACK |
1127 bsf SSP1CON2,RCEN ; enable receive mode | 1128 bsf SSP1CON2,RCEN ; enable receive mode |
1128 bra WaitMSSP ; ... and return | 1129 bra WaitMSSP ; ... and return |
1130 | |
1131 | |
1132 global reset_battery_gauge_and_lt2942 ; called from comm and menu tree | |
1133 reset_battery_gauge_and_lt2942: ; reset battery gauge chip and battery registers | |
1134 btfsc battery_gauge_available ; battery gauge chip available? | |
1135 call lt2942_charge_done ; YES - reset meter to 0xFFFF | |
1136 ;bra reset_battery_gauge ; continue resetting gauge registers | |
1137 | |
1138 global reset_battery_gauge | |
1139 reset_battery_gauge: ; reset gauge registers | |
1140 bsf block_battery_gauge ; suspend ISR from accessing the battery registers | |
1141 movlw .100 ; set battery level to 100% | |
1142 movwf batt_percent ; ... | |
1143 banksel battery_gauge ; select bank ISR data | |
1144 clrf battery_gauge+0 ; null the battery registers | |
1145 clrf battery_gauge+1 ; ... | |
1146 clrf battery_gauge+2 ; ... | |
1147 clrf battery_gauge+3 ; ... | |
1148 clrf battery_gauge+4 ; ... | |
1149 clrf battery_gauge+5 ; ... | |
1150 banksel common ; back to bank common | |
1151 goto eeprom_battery_gauge_write ; update battery registers in EEPROM, unblock ISR and return | |
1129 | 1152 |
1130 | 1153 |
1131 ;============================================================================= | 1154 ;============================================================================= |
1132 ; Transmitter Functions | 1155 ; Transmitter Functions |
1133 ; | 1156 ; |