comparison src/i2c.asm @ 604:ca4556fb60b9

bump to 2.99beta, work on 3.00 stable
author heinrichsweikamp
date Thu, 22 Nov 2018 19:47:26 +0100
parents b455b31ce022
children d866684249bd
comparison
equal deleted inserted replaced
603:00b24fb4324d 604:ca4556fb60b9
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File i2c.asm V2.98 3 ; File i2c.asm V2.99c
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 ;=============================================================================
67 bcf PIR1,SSP1IF 67 bcf PIR1,SSP1IF
68 clrf i2c_temp1 68 clrf i2c_temp1
69 bsf i2c_error_flag ; set error flag 69 bsf i2c_error_flag ; set error flag
70 return 70 return
71 71
72 I2CReset: ; Something went wrong (Slave holds SDA low?) 72 I2CReset: ; something went wrong (slave holds SDA low?)
73 clrf SSP1CON1 ; wake-up slave and reset entire module 73 clrf SSP1CON1 ; wake-up slave and reset entire module
74 clrf SSP1CON2 74 clrf SSP1CON2
75 clrf SSP1STAT 75 clrf SSP1STAT
76 bcf TRISC,3 ; SCL OUTPUT 76 bcf TRISC,3 ; SCL OUTPUT
77 bsf TRISC,4 ; SDA Input 77 bsf TRISC,4 ; SDA input
78 bcf PORTC,3 78 bcf PORTC,3
79 movlw d'9' 79 movlw d'9'
80 movwf i2c_temp1 ; clock-out 9 clock cycles manually 80 movwf i2c_temp1 ; clock-out 9 clock cycles manually
81 I2CReset_1: 81 I2CReset_1:
82 bsf PORTC,3 ; SCL=1 82 bsf PORTC,3 ; SCL = 1
83 nop 83 nop
84 nop 84 nop
85 nop 85 nop
86 nop 86 nop
87 btfsc PORTC,4 ; SDA=1? 87 btfsc PORTC,4 ; SDA = 1 ?
88 bra I2CReset_2 ; =1, SDA has been released from slave 88 bra I2CReset_2 ; YES - =1, SDA has been released from slave
89 bcf PORTC,3 ; SCL=0 89 bcf PORTC,3 ; NO - set SCL = 0
90 nop 90 nop
91 nop 91 nop
92 bcf PORTC,3 92 bcf PORTC,3
93 nop 93 nop
94 nop 94 nop
95 decfsz i2c_temp1,F 95 decfsz i2c_temp1,F
96 bra I2CReset_1 ; check for nine clock cycles 96 bra I2CReset_1 ; check for nine clock cycles
97 I2CReset_2: 97 I2CReset_2:
98 bsf TRISC,3 ; SCL Input 98 bsf TRISC,3 ; SCL Input
99 clrf SSP1CON1 ; setup I²C Mode 99 clrf SSP1CON1 ; setup I²C mode
100 WAITMS d'10' ; Reset-Timeout for I2C devices 100 WAITMS d'10' ; reset-timeout for I2C devices
101 movlw b'00000000' ; with slew rate control 101 movlw b'00000000' ; with slew rate control
102 movwf SSP1STAT 102 movwf SSP1STAT
103 movlw b'00101000' 103 movlw b'00101000'
104 movwf SSP1CON1 104 movwf SSP1CON1
105 movlw b'00000000' 105 movlw b'00000000'
109 return 109 return
110 110
111 I2C_TX: 111 I2C_TX:
112 movwf SSP1BUF 112 movwf SSP1BUF
113 rcall WaitMSSP 113 rcall WaitMSSP
114 bra I2C_WaitforACK ; Returns... 114 bra I2C_WaitforACK ; returns...
115 115
116 I2C_TwoBytesRX_div16: ; Get two bytes and divide lo:hi/16 (signed) 116 I2C_TwoBytesRX_div16: ; get two bytes and divide lo:hi/16 (signed)
117 rcall I2C_OneByteRX ; Get one byte 117 rcall I2C_OneByteRX ; get one byte
118 movff SSP1BUF,hi ; Data Byte 118 movff SSP1BUF,hi ; data byte
119 rcall I2C_OneByteRX ; Get one byte 119 rcall I2C_OneByteRX ; get one byte
120 movff SSP1BUF,lo ; Data Byte 120 movff SSP1BUF,lo ; data byte
121 I2C_TwoBytesRX_div16_2: ; divide lo:hi/16 (signed) only 121 I2C_TwoBytesRX_div16_2: ; divide lo:hi/16 (signed) only
122 bcf STATUS,C 122 bcf STATUS,C
123 btfsc hi,7 ; Copy sign bit to carry 123 btfsc hi,7 ; copy sign bit to carry
124 bsf STATUS,C 124 bsf STATUS,C
125 rrcf hi ; /2 125 rrcf hi ; /2
126 rrcf lo 126 rrcf lo
127 I2C_TwoBytesRX_div8_2: ; divide lo:hi/8 (signed) only 127 I2C_TwoBytesRX_div8_2: ; divide lo:hi/8 (signed) only
128 bcf STATUS,C 128 bcf STATUS,C
129 btfsc hi,7 ; Copy sign bit to carry 129 btfsc hi,7 ; copy sign bit to carry
130 bsf STATUS,C 130 bsf STATUS,C
131 rrcf hi ; /4 131 rrcf hi ; /4
132 rrcf lo 132 rrcf lo
133 bcf STATUS,C 133 bcf STATUS,C
134 btfsc hi,7 ; Copy sign bit to carry 134 btfsc hi,7 ; copy sign bit to carry
135 bsf STATUS,C 135 bsf STATUS,C
136 rrcf hi ; /8 136 rrcf hi ; /8
137 rrcf lo 137 rrcf lo
138 bcf STATUS,C 138 bcf STATUS,C
139 btfsc hi,7 ; Copy sign bit to carry 139 btfsc hi,7 ; copy sign bit to carry
140 bsf STATUS,C 140 bsf STATUS,C
141 rrcf hi ; /16 141 rrcf hi ; /16
142 rrcf lo 142 rrcf lo
143 return 143 return
144 144
145 global I2C_RX_accelerometer 145 global I2C_RX_accelerometer
146 I2C_RX_accelerometer: 146 I2C_RX_accelerometer:
147 btfsc compass_type2 ; compass2 147 btfsc compass_type2 ; compass2 ?
148 bra I2C_RX_accelerometer_compass2 ; yes 148 bra I2C_RX_accelerometer_compass2 ; YES
149 btfsc compass_type ; compass1? 149 btfsc compass_type ; compass1 ?
150 bra I2C_RX_accelerometer_compass1 ; yes 150 bra I2C_RX_accelerometer_compass1 ; YES
151 I2C_RX_accelerometer_compass0: 151 I2C_RX_accelerometer_compass0:
152 bsf SSP1CON2,SEN ; Start condition 152 bsf SSP1CON2,SEN ; start condition
153 rcall WaitMSSP 153 rcall WaitMSSP
154 movlw 0x38 ; address 154 movlw 0x38 ; address
155 rcall I2C_TX 155 rcall I2C_TX
156 movlw 0x00 156 movlw 0x00
157 rcall I2C_TX 157 rcall I2C_TX
158 bsf SSP1CON2,RSEN ; Repeated start condition (!) 158 bsf SSP1CON2,RSEN ; repeated start condition
159 rcall WaitMSSP 159 rcall WaitMSSP
160 movlw 0x39 ; address 160 movlw 0x39 ; address
161 rcall I2C_TX 161 rcall I2C_TX
162 162
163 rcall I2C_OneByteRX ; Get Status Byte 163 rcall I2C_OneByteRX ; get status byte
164 movf SSP1BUF,W 164 movf SSP1BUF,W
165 165
166 ; Non-flipped screen: 166 ; Non-flipped screen:
167 ; Chip orientation on the PCB requires 167 ; Chip orientation on the PCB requires
168 ; Original = Corrected 168 ; Original = corrected
169 ; x = -x 169 ; x = -x
170 ; y = -y 170 ; y = -y
171 ; z = -z 171 ; z = -z
172 172
173 ; Flipped screen: 173 ; Flipped screen:
174 ; Chip orientation on the PCB requires 174 ; Chip orientation on the PCB requires
175 ; Original = Corrected 175 ; Original = corrected
176 ; x = x 176 ; x = x
177 ; y = y 177 ; y = y
178 ; z = -z 178 ; z = -z
179 179
180 rcall I2C_TwoBytesRX_div16 ; Get two bytes and divide /16 (signed) 180 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
181 btfsc flip_screen ; 180° rotation ? 181 btfsc flip_screen ; 180° rotation ?
182 bra I2C_RX_accelerometer2 ; Yes 182 bra I2C_RX_accelerometer2 ; YES
183 comf hi ; 16bit sign change. 183 comf hi ; 16 bit sign change
184 negf lo 184 negf lo
185 btfsc STATUS,C ; Carry to propagate ? 185 btfsc STATUS,C ; carry to propagate ?
186 incf hi,F ; YES: do it. 186 incf hi,F ; YES - do it
187 I2C_RX_accelerometer2: 187 I2C_RX_accelerometer2:
188 movff lo,accel_DX+0 188 movff lo,accel_DX+0
189 movff hi,accel_DX+1 ; Copy result 189 movff hi,accel_DX+1 ; Copy result
190 190
191 rcall I2C_TwoBytesRX_div16 ; Get two bytes and divide /16 (signed) 191 rcall I2C_TwoBytesRX_div16 ; Get two bytes and divide /16 (signed)
981 ; > Zero, set batt_percent properly 981 ; > Zero, set batt_percent properly
982 movff sub_c+0,xA+0 982 movff sub_c+0,xA+0
983 movff sub_c+1,xA+1 983 movff sub_c+1,xA+1
984 movff battery_capacity+0,xB+0 984 movff battery_capacity+0,xB+0
985 movff battery_capacity+1,xB+1 985 movff battery_capacity+1,xB+1
986 call div16x16 ; xA/xB=xC with xA+0 as remainder, uses divB as temp variable 986 call div16x16 ; xC = xA / xB with xA as remainder
987 movff xC+0,batt_percent 987 movff xC+0,batt_percent
988 return 988 return
989 989
990 lt2942_set_to_zero_percent: 990 lt2942_set_to_zero_percent:
991 clrf i2c_temp1 991 clrf i2c_temp1
1034 rcall I2C_WaitforACK 1034 rcall I2C_WaitforACK
1035 bsf SSP1CON2, RCEN ; Enable receive mode 1035 bsf SSP1CON2, RCEN ; Enable receive mode
1036 bra WaitMSSP ; (and return) 1036 bra WaitMSSP ; (and return)
1037 1037
1038 1038
1039 ;-----------------------------------------------------------------------------
1040 ; Transmitter Functions
1041
1042 IFDEF _rx_functions
1043
1039 global I2C_probe_OSTC_rx 1044 global I2C_probe_OSTC_rx
1040 I2C_probe_OSTC_rx: 1045 I2C_probe_OSTC_rx:
1046 movlw .5
1047 movwf lo_temp
1048 I2C_probe_OSTC_rx_1:
1041 bsf SSP1CON2,SEN ; Start condition 1049 bsf SSP1CON2,SEN ; Start condition
1042 rcall WaitMSSP 1050 rcall WaitMSSP
1043 movlw 0x50 ; Address byte + Write bit 1051 movlw 0x50 ; Address byte + Write bit
1044 movwf SSP1BUF ; control byte 1052 movwf SSP1BUF ; control byte
1045 rcall WaitMSSP 1053 rcall WaitMSSP
1046 btfss SSP1CON2,ACKSTAT ; ACK? 1054 btfss SSP1CON2,ACKSTAT ; ACK?
1047 bsf ostc_rx_present ; ACK send. OSTC_RX present! 1055 bsf ostc_rx_present ; ACK sent - OSTC_RX present!
1048 bsf SSP1CON2,PEN ; Stop condition 1056 bsf SSP1CON2,PEN ; Stop condition
1049 rcall WaitMSSP 1057 rcall WaitMSSP
1050 btfss ostc_rx_present ; Do we have the RX? 1058 btfss ostc_rx_present ; Do we have the RX?
1051 return ; No, Done. 1059 return ; No, Done.
1052 WAITMS .1 1060 WAITMS .1
1073 movff SSP1BUF,rx_firmware+0 1081 movff SSP1BUF,rx_firmware+0
1074 bsf SSP1CON2,ACKEN ; Master acknowledge 1082 bsf SSP1CON2,ACKEN ; Master acknowledge
1075 rcall WaitMSSP 1083 rcall WaitMSSP
1076 1084
1077 ; last byte in read from RX circuity always with a NACK! 1085 ; last byte in read from RX circuity always with a NACK!
1078 bsf SSP1CON2, RCEN ; Enable receive mode 1086 bsf SSP1CON2, RCEN ; Enable receive mode
1079 rcall WaitMSSP 1087 rcall WaitMSSP
1080 movff SSP1BUF,rx_firmware+1 1088 movff SSP1BUF,rx_firmware+1
1081 bsf SSP1CON2,ACKDT 1089 bsf SSP1CON2,ACKDT
1082 bsf SSP1CON2,ACKEN ; Master NOT acknowledge 1090 bsf SSP1CON2,ACKEN ; Master NOT acknowledge
1083 rcall WaitMSSP 1091 rcall WaitMSSP
1084 bcf SSP1CON2,ACKDT ; Reset ACKDT flag 1092 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
1085 bsf SSP1CON2,PEN ; Stop condition 1093 bsf SSP1CON2,PEN ; Stop condition
1086 bra WaitMSSP ;(and return) 1094 rcall WaitMSSP
1095
1096 ; test for RX part not being ready during this read
1097 movff rx_firmware+1,i2c_temp1
1098 movlw .147
1099 cpfseq i2c_temp1
1100 bra I2C_probe_OSTC_rx_2 ; not equal
1101 movff rx_firmware+0,i2c_temp1
1102 movlw .27
1103 cpfseq i2c_temp1
1104 bra I2C_probe_OSTC_rx_2 ; not equal
1105 bsf active_reset_ostc_rx
1106 WAITMS .5
1107 bcf active_reset_ostc_rx
1108 WAITMS .250
1109 WAITMS .250
1110 WAITMS .250
1111 WAITMS .250
1112 WAITMS .250
1113 WAITMS .250
1114 WAITMS .250
1115 WAITMS .250
1116 clrf i2c_temp1
1117 decfsz lo_temp,F ; try max. 5 times
1118 bra I2C_probe_OSTC_rx_1
1119 bcf ostc_rx_present ; Clear flag. Something is wrong
1120 I2C_probe_OSTC_rx_2:
1121 clrf i2c_temp1
1122 return
1087 1123
1088 1124
1089 global I2C_get_tankdata 1125 global I2C_get_tankdata
1090 I2C_get_tankdata: 1126 I2C_get_tankdata:
1091 bsf SSP1CON2,SEN ; Start condition 1127 bsf SSP1CON2,SEN ; Start condition
1214 1250
1215 btfss i2c_error_flag 1251 btfss i2c_error_flag
1216 retlw .0 ; All ok 1252 retlw .0 ; All ok
1217 retlw .255 ; an error occurred 1253 retlw .255 ; an error occurred
1218 1254
1255 ENDIF
1256
1219 END 1257 END