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