comparison src/i2c.asm @ 623:c40025d8e750

3.03 beta released
author heinrichsweikamp
date Mon, 03 Jun 2019 14:01:48 +0200
parents 1ad0531e9078
children 7bdcc591196c
comparison
equal deleted inserted replaced
622:02d1386429a6 623:c40025d8e750
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File i2c.asm V2.99c 3 ; File i2c.asm combined next generation V3.03.2
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 ;=============================================================================
42 #include "wait.inc" 42 #include "wait.inc"
43 #include "math.inc" 43 #include "math.inc"
44 #include "external_flash.inc" 44 #include "external_flash.inc"
45 45
46 46
47 i2c CODE 47 i2c CODE
48 48
49 ;============================================================================= 49 ;=============================================================================
50 50
51 I2C_TX: 51 I2C_TX:
52 movwf SSP1BUF 52 movwf SSP1BUF
116 ; x = x 116 ; x = x
117 ; y = y 117 ; y = y
118 ; z = -z 118 ; z = -z
119 119
120 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed) 120 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
121 btfsc flip_screen ; 180° rotation ? 121 btfsc flip_screen ; 180° rotation?
122 bra I2C_RX_accelerometer2 ; YES 122 bra I2C_RX_accelerometer2 ; YES
123 comf hi ; 16 bit sign change 123 comf hi ; 16 bit sign change
124 negf lo 124 negf lo
125 btfsc STATUS,C ; carry to propagate ? 125 btfsc STATUS,C ; carry to propagate?
126 incf hi,F ; YES - do it 126 incf hi,F ; YES - do it
127 I2C_RX_accelerometer2: 127 I2C_RX_accelerometer2:
128 movff lo,accel_DX+0 128 MOVII mpr,accel_DX ; copy result
129 movff hi,accel_DX+1 ; Copy result 129 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
130 130 btfsc flip_screen ; 180° rotation?
131 rcall I2C_TwoBytesRX_div16 ; Get two bytes and divide /16 (signed) 131 bra I2C_RX_accelerometer3 ; YES
132 btfsc flip_screen ; 180° rotation ? 132 comf hi ; 16 bit sign change
133 bra I2C_RX_accelerometer3 ; Yes
134 comf hi ; 16bit sign change.
135 negf lo 133 negf lo
136 btfsc STATUS,C ; Carry to propagate ? 134 btfsc STATUS,C ; carry to propagate?
137 incf hi,F ; YES: do it. 135 incf hi,F ; YES - do it
138 I2C_RX_accelerometer3: 136 I2C_RX_accelerometer3:
139 movff lo,accel_DY+0 137 MOVII mpr,accel_DY ; copy result
140 movff hi,accel_DY+1 ; Copy result 138 rcall I2C_OneByteRX ; get one byte
141 139 movff SSP1BUF,hi ; data byte
142 rcall I2C_OneByteRX ; Get one byte
143 movff SSP1BUF,hi ; Data Byte
144 bsf SSP1CON2, RCEN ; Enable receive mode 140 bsf SSP1CON2, RCEN ; Enable receive mode
145 rcall WaitMSSP 141 rcall WaitMSSP
146 ; According to data sheet there should be no Master Acknowledge for the last Byte (accel_DZ+0)... 142 ; According to data sheet there should be no master Acknowledge for the last byte (accel_DZ+0)...
147 movff SSP1BUF,lo ; Data Byte 143 movff SSP1BUF,lo ; data byte
148 144
149 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 145 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
150 comf hi ; 16bit sign change. 146 comf hi ; 16 bit sign change
151 negf lo 147 negf lo
152 btfsc STATUS,C ; Carry to propagate ? 148 btfsc STATUS,C ; carry to propagate?
153 incf hi,F ; YES: do it. 149 incf hi,F ; YES - do it
154 movff lo,accel_DZ+0 150 MOVII mpr,accel_DZ ; copy result
155 movff hi,accel_DZ+1 ; Copy result 151 bsf SSP1CON2,PEN ; stop condition
156 152 bra WaitMSSP ; ... and return
157 bsf SSP1CON2,PEN ; Stop condition
158 bra WaitMSSP ; (And return)
159 153
160 I2C_RX_accelerometer_compass1: 154 I2C_RX_accelerometer_compass1:
161 bsf SSP1CON2,SEN ; Start condition 155 bsf SSP1CON2,SEN ; start condition
162 rcall WaitMSSP 156 rcall WaitMSSP
163 movlw 0x3C ; address 157 movlw 0x3C ; address
164 rcall I2C_TX 158 rcall I2C_TX
165 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 159 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
166 rcall I2C_TX 160 rcall I2C_TX
167 bsf SSP1CON2,RSEN ; Repeated start condition (!) 161 bsf SSP1CON2,RSEN ; repeated start condition (!)
168 rcall WaitMSSP 162 rcall WaitMSSP
169 movlw 0x3D ; address 163 movlw 0x3D ; address
170 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here... 164 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here...
171 rcall I2C_TX 165 rcall I2C_TX
172 166
188 182
189 ; Dump the accelerator data 183 ; Dump the accelerator data
190 rcall I2C_OneByteRX 184 rcall I2C_OneByteRX
191 movff SSP1BUF,lo ; accel_DX+0 185 movff SSP1BUF,lo ; accel_DX+0
192 rcall I2C_OneByteRX 186 rcall I2C_OneByteRX
193 movff SSP1BUF,hi ;accel_DX+1 187 movff SSP1BUF,hi ; accel_DX+1
194 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 188 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
195 btfss compass_type2 ; compass 2? 189 btfss compass_type2 ; compass 2?
196 bra I2C_RX_accelerometer1_c1 ; No, compass 1 190 bra I2C_RX_accelerometer1_c1 ; NO - compass 1
197 ; compass 2 191 ; compass 2
198 btfss flip_screen ; 180° rotation ? 192 btfss flip_screen ; 180° rotation?
199 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z 193 bra I2C_RX_accelerometer2_c1 ; NO - continue with normal compass1 routines for Y and Z
200 ; flipped compass 2, negate x 194 ; flipped compass 2, negate x
201 comf hi ; 16bit sign change. 195 comf hi ; 16 bit sign change
202 negf lo 196 negf lo
203 btfsc STATUS,C ; Carry to propagate ? 197 btfsc STATUS,C ; carry to propagate?
204 incf hi,F ; YES: do it. 198 incf hi,F ; YES - do it
205 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z 199 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z
206 200
207 I2C_RX_accelerometer1_c1: 201 I2C_RX_accelerometer1_c1:
208 btfsc flip_screen ; 180° rotation ? 202 btfsc flip_screen ; 180° rotation?
209 bra I2C_RX_accelerometer2_c1 ; Yes 203 bra I2C_RX_accelerometer2_c1 ; YES
210 ; non-flipped compass 1, negate x 204 ; non-flipped compass 1, negate x
211 comf hi ; 16bit sign change. 205 comf hi ; 16 bit sign change
212 negf lo 206 negf lo
213 btfsc STATUS,C ; Carry to propagate ? 207 btfsc STATUS,C ; carry to propagate?
214 incf hi,F ; YES: do it. 208 incf hi,F ; YES - do it
215 I2C_RX_accelerometer2_c1: 209 I2C_RX_accelerometer2_c1:
216 ; flipped compass 1, non-flipped compass 2 210 ; flipped compass 1, non-flipped compass 2
217 movff lo,accel_DX+0 211 MOVII mpr,accel_DX ; copy result
218 movff hi,accel_DX+1 ; Copy result
219 rcall I2C_OneByteRX 212 rcall I2C_OneByteRX
220 movff SSP1BUF,lo ; accel_DY+0 213 movff SSP1BUF,lo ; accel_DY+0
221 rcall I2C_OneByteRX 214 rcall I2C_OneByteRX
222 movff SSP1BUF,hi ; accel_DY+1 215 movff SSP1BUF,hi ; accel_DY+1
223 216
224 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 217 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
225 btfsc flip_screen ; 180° rotation ? 218 btfsc flip_screen ; 180° rotation?
226 bra I2C_RX_accelerometer3_c1 ; Yes 219 bra I2C_RX_accelerometer3_c1 ; YES
227 comf hi ; 16bit sign change. 220 comf hi ; 16 bit sign change
228 negf lo 221 negf lo
229 btfsc STATUS,C ; Carry to propagate ? 222 btfsc STATUS,C ; carry to propagate?
230 incf hi,F ; YES: do it. 223 incf hi,F ; YES - do it
231 I2C_RX_accelerometer3_c1: 224 I2C_RX_accelerometer3_c1:
232 movff lo,accel_DY+0 225 MOVII mpr,accel_DY ; copy result
233 movff hi,accel_DY+1 ; Copy result
234
235 rcall I2C_OneByteRX 226 rcall I2C_OneByteRX
236 movff SSP1BUF,lo ;accel_DZ+0 227 movff SSP1BUF,lo ; accel_DZ+0
237 bsf SSP1CON2, RCEN ; Enable receive mode 228 bsf SSP1CON2, RCEN ; Enable receive mode
238 rcall WaitMSSP 229 rcall WaitMSSP
239 ; According to data sheet there should be no Master Acknowledge for the last Byte (accel_DZ+1)... 230 ; According to data sheet there should be no master Acknowledge for the last byte (accel_DZ+1)...
240 movff SSP1BUF,hi ;accel_DZ+1 231 movff SSP1BUF,hi ; accel_DZ+1
241 bsf SSP1CON2,PEN ; Stop condition 232 bsf SSP1CON2,PEN ; stop condition
242 rcall WaitMSSP 233 rcall WaitMSSP
243 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only 234 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
244 comf hi ; 16bit sign change for Z 235 comf hi ; 16 bit sign change for Z
245 negf lo 236 negf lo
246 btfsc STATUS,C ; Carry to propagate ? 237 btfsc STATUS,C ; carry to propagate?
247 incf hi,F ; YES: do it. 238 incf hi,F ; YES - do it
248 movff lo,accel_DZ+0 239 MOVII mpr,accel_DZ ; copy result
249 movff hi,accel_DZ+1 ; Copy result
250 return 240 return
251 241
252 I2C_RX_accelerometer_compass2: 242 I2C_RX_accelerometer_compass2:
253 bsf SSP1CON2,SEN ; Start condition 243 bsf SSP1CON2,SEN ; start condition
254 rcall WaitMSSP 244 rcall WaitMSSP
255 movlw 0x32 ; address 245 movlw 0x32 ; address
256 rcall I2C_TX 246 rcall I2C_TX
257 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 247 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
258 rcall I2C_TX 248 rcall I2C_TX
259 bsf SSP1CON2,RSEN ; Repeated start condition (!) 249 bsf SSP1CON2,RSEN ; repeated start condition (!)
260 rcall WaitMSSP 250 rcall WaitMSSP
261 movlw 0x33 ; address 251 movlw 0x33 ; address
262 bra I2C_RX_accelerometer_compass1_xx 252 bra I2C_RX_accelerometer_compass1_xx
263 253
264 I2C_OneByteRX: 254 I2C_OneByteRX:
265 bsf SSP1CON2,RCEN ; Enable receive mode 255 bsf SSP1CON2,RCEN ; enable receive mode
266 rcall WaitMSSP 256 rcall WaitMSSP
267 bsf SSP1CON2,ACKEN ; Master acknowledge 257 bsf SSP1CON2,ACKEN ; master acknowledge
268 bra WaitMSSP ; And return! 258 bra WaitMSSP ; ... and return
259
260
261 ;-----------------------------------------------------------------------------
262 IFDEF _compass
269 263
270 global I2C_RX_compass 264 global I2C_RX_compass
271 I2C_RX_compass: 265 I2C_RX_compass:
272 btfsc compass_type2 ; compass2 266 btfsc compass_type2 ; compass2 ?
273 bra I2C_RX_compass2 ; yes 267 bra I2C_RX_compass2 ; YES
274 btfsc compass_type ; compass1? 268 btfsc compass_type ; compass1 ?
275 bra I2C_RX_compass1 ; yes 269 bra I2C_RX_compass1 ; YES
276 I2C_RX_compass0: 270 I2C_RX_compass0:
277 bsf SSP1CON2,SEN ; Start condition 271 bsf SSP1CON2,SEN ; start condition
278 rcall WaitMSSP 272 rcall WaitMSSP
279 movlw 0x3C ; address 273 movlw 0x3C ; address
280 rcall I2C_TX 274 rcall I2C_TX
281 movlw 0x03 275 movlw 0x03
282 rcall I2C_TX 276 rcall I2C_TX
283 bsf SSP1CON2,PEN ; Stop condition 277 bsf SSP1CON2,PEN ; stop condition
284 rcall WaitMSSP 278 rcall WaitMSSP
285 279
286 bcf PIR1,SSP1IF 280 bcf PIR1,SSP1IF
287 bsf SSP1CON2,SEN ; Start condition 281 bsf SSP1CON2,SEN ; start condition
288 rcall WaitMSSP 282 rcall WaitMSSP
289 movlw 0x3D ; address 283 movlw 0x3D ; address
290 rcall I2C_TX 284 rcall I2C_TX
291 285
292 ; Compass IC sends data in following order: 286 ; Compass IC sends data in following order:
309 ; Original = Corrected 303 ; Original = Corrected
310 ; x = y 304 ; x = y
311 ; z = z 305 ; z = z
312 ; y = -x 306 ; y = -x
313 307
314 rcall I2C_OneByteRX ; Get one byte 308 rcall I2C_OneByteRX ; get one byte
315 movff SSP1BUF,compass_DY+1 ; Data Byte 309 movff SSP1BUF,compass_DY+1 ; data byte
316 rcall I2C_OneByteRX ; Get one byte 310 rcall I2C_OneByteRX ; get one byte
317 movff SSP1BUF,compass_DY+0 ; Data Byte 311 movff SSP1BUF,compass_DY+0 ; data byte
318 btfsc flip_screen ; 180° rotation ? 312 btfsc flip_screen ; 180° rotation?
319 bra I2C_RX_compass0_2 ; Yes 313 bra I2C_RX_compass0_2 ; NO
320 banksel compass_DY 314 banksel compass_DY ; YES - flip Y
321 comf compass_DY+1 ; 16bit sign change. 315 comf compass_DY+1 ; - 16 bit sign change
322 negf compass_DY+0 316 negf compass_DY+0
323 btfsc STATUS,C ; Carry to propagate ? 317 btfsc STATUS,C ; - carry to propagate?
324 incf compass_DY+1,F ; YES: do it. 318 incf compass_DY+1,F ; YES - do it
319 banksel common
325 I2C_RX_compass0_2: 320 I2C_RX_compass0_2:
326 banksel common 321 rcall I2C_OneByteRX ; get one byte
327 rcall I2C_OneByteRX ; Get one byte 322 movff SSP1BUF,compass_DZ+1 ; data byte
328 movff SSP1BUF,compass_DZ+1 ; Data Byte 323 rcall I2C_OneByteRX ; get one byte
329 rcall I2C_OneByteRX ; Get one byte 324 movff SSP1BUF,compass_DZ+0 ; data byte
330 movff SSP1BUF,compass_DZ+0 ; Data Byte 325 rcall I2C_OneByteRX ; get one byte
331 rcall I2C_OneByteRX ; Get one byte 326 movff SSP1BUF,compass_DX+1 ; data byte
332 movff SSP1BUF,compass_DX+1 ; Data Byte
333 bsf SSP1CON2, RCEN ; Enable receive mode 327 bsf SSP1CON2, RCEN ; Enable receive mode
334 rcall WaitMSSP 328 rcall WaitMSSP
335 movff SSP1BUF,compass_DX+0 ; Data Byte 329 movff SSP1BUF,compass_DX+0 ; data byte
336 bsf SSP1CON2,PEN ; Stop condition 330 bsf SSP1CON2,PEN ; stop condition
337 rcall WaitMSSP 331 rcall WaitMSSP
338 btfss flip_screen ; 180° rotation ? 332 btfss flip_screen ; 180° rotation?
339 return ; No, done. 333 return ; NO - done
340 ; Yes, flip X 334 banksel compass_DX ; YES - flip X
341 banksel compass_DX 335 comf compass_DX+1 ; - 16 bit sign change
342 comf compass_DX+1 ; 16bit sign change.
343 negf compass_DX+0 336 negf compass_DX+0
344 btfsc STATUS,C ; Carry to propagate ? 337 btfsc STATUS,C ; - carry to propagate?
345 incf compass_DX+1,F ; YES: do it. 338 incf compass_DX+1,F ; YES - do it
346 banksel common 339 banksel common
347 return 340 return
348 341
349 I2C_RX_compass1: ; New compass 342 I2C_RX_compass1: ; new compass
350 bsf SSP1CON2,SEN ; Start condition 343 bsf SSP1CON2,SEN ; start condition
351 rcall WaitMSSP 344 rcall WaitMSSP
352 movlw 0x3C ; address 345 movlw 0x3C ; address
353 rcall I2C_TX 346 rcall I2C_TX
354 movlw b'10001000' ; 0x08 with auto-increment (MSB=1) 347 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
355 rcall I2C_TX 348 rcall I2C_TX
356 bsf SSP1CON2,RSEN ; Repeated start condition (!) 349 bsf SSP1CON2,RSEN ; repeated start condition (!)
357 rcall WaitMSSP 350 rcall WaitMSSP
358 movlw 0x3D ; address 351 movlw 0x3D ; address
359 rcall I2C_TX 352 rcall I2C_TX
360 ;rcall WaitMSSP ; Needed? (mH) 353 ;rcall WaitMSSP ; TODO needed? (mH)
361 rcall I2C_OneByteRX ; Get one byte 354 rcall I2C_OneByteRX ; get one byte
362 movff SSP1BUF,lo ; Data Byte 355 movff SSP1BUF,lo ; data byte
363 rcall I2C_OneByteRX ; Get one byte 356 rcall I2C_OneByteRX ; get one byte
364 movff SSP1BUF,hi ; Data Byte 357 movff SSP1BUF,hi ; data byte
365 rcall I2C_TwoBytesRX_div8_2 358 rcall I2C_TwoBytesRX_div8_2
366 movff lo,compass_DX+0 359 MOVII mpr,compass_DX
367 movff hi,compass_DX+1 360 btfss flip_screen ; 180° rotation?
368 btfss flip_screen ; 180° rotation ? 361 bra I2C_RX_compass1_1 ; NO
369 bra I2C_RX_compass1_1 ; Yes 362 banksel compass_DX ; YES - flip X
370 ; Yes, flip X 363 comf compass_DX+1 ; - 16 bit sign change
371 banksel compass_DX
372 comf compass_DX+1 ; 16bit sign change.
373 negf compass_DX+0 364 negf compass_DX+0
374 btfsc STATUS,C ; Carry to propagate ? 365 btfsc STATUS,C ; - carry to propagate?
375 incf compass_DX+1,F ; YES: do it. 366 incf compass_DX+1,F ; YES - do it
376 banksel common 367 banksel common
377 I2C_RX_compass1_1: 368 I2C_RX_compass1_1:
378 rcall I2C_OneByteRX ; Get one byte 369 rcall I2C_OneByteRX ; get one byte
379 movff SSP1BUF,lo ; Data Byte 370 movff SSP1BUF,lo ; data byte
380 rcall I2C_OneByteRX ; Get one byte 371 rcall I2C_OneByteRX ; get one byte
381 movff SSP1BUF,hi ; Data Byte 372 movff SSP1BUF,hi ; data byte
382 rcall I2C_TwoBytesRX_div8_2 373 rcall I2C_TwoBytesRX_div8_2
383 movff lo,compass_DY+0 374 MOVII mpr,compass_DY
384 movff hi,compass_DY+1 375 btfss flip_screen ; 180° rotation?
385 btfss flip_screen ; 180° rotation ? 376 bra I2C_RX_compass1_2 ; NO
386 bra I2C_RX_compass1_2 ; Yes 377 banksel compass_DY ; YES - flip Y
387 ; Yes, flip Y 378 comf compass_DY+1 ; - 16 bit sign change
388 banksel compass_DY
389 comf compass_DY+1 ; 16bit sign change.
390 negf compass_DY+0 379 negf compass_DY+0
391 btfsc STATUS,C ; Carry to propagate ? 380 btfsc STATUS,C ; - carry to propagate?
392 incf compass_DY+1,F ; YES: do it. 381 incf compass_DY+1,F ; YES - do it
382 banksel common
393 I2C_RX_compass1_2: 383 I2C_RX_compass1_2:
394 banksel common 384 rcall I2C_OneByteRX ; get one byte
395 rcall I2C_OneByteRX ; Get one byte 385 movff SSP1BUF,lo ; data byte
396 movff SSP1BUF,lo ; Data Byte
397 bsf SSP1CON2, RCEN ; Enable receive mode 386 bsf SSP1CON2, RCEN ; Enable receive mode
398 rcall WaitMSSP 387 rcall WaitMSSP
399 movff SSP1BUF,hi ; Data Byte 388 movff SSP1BUF,hi ; data byte
400 rcall I2C_TwoBytesRX_div8_2 389 rcall I2C_TwoBytesRX_div8_2
401 movff lo,compass_DZ+0 390 MOVII mpr,compass_DZ
402 movff hi,compass_DZ+1 391 bsf SSP1CON2,PEN ; stop condition
403 bsf SSP1CON2,PEN ; Stop condition 392 bra WaitMSSP ; ... and return
404 bra WaitMSSP ;(And return)
405 393
406 I2C_RX_compass2: ; newest compass 394 I2C_RX_compass2: ; newest compass
407 bsf SSP1CON2,SEN ; Start condition 395 bsf SSP1CON2,SEN ; start condition
408 rcall WaitMSSP 396 rcall WaitMSSP
409 movlw 0x3C ; address 397 movlw 0x3C ; address
410 rcall I2C_TX 398 rcall I2C_TX
411 movlw 0xE8 ; 0x68 with auto-increment (MSB=1) 399 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
412 rcall I2C_TX 400 rcall I2C_TX
413 bsf SSP1CON2,RSEN ; Repeated start condition (!) 401 bsf SSP1CON2,RSEN ; repeated start condition (!)
414 rcall WaitMSSP 402 rcall WaitMSSP
415 movlw 0x3D ; address 403 movlw 0x3D ; address
416 rcall I2C_TX 404 rcall I2C_TX
417 ; rcall WaitMSSP 405 ; rcall WaitMSSP
418 rcall I2C_OneByteRX ; Get one byte 406 rcall I2C_OneByteRX ; get one byte
419 movff SSP1BUF,lo ; Data Byte 407 movff SSP1BUF,lo ; data byte
420 rcall I2C_OneByteRX ; Get one byte 408 rcall I2C_OneByteRX ; get one byte
421 movff SSP1BUF,hi ; Data Byte 409 movff SSP1BUF,hi ; data byte
422 ; rcall I2C_TwoBytesRX_div8_2 410 ; rcall I2C_TwoBytesRX_div8_2
423 btfsc flip_screen ; 180° rotation ? 411 btfsc flip_screen ; 180° rotation?
424 bra I2C_RX_compass2_1 ; Yes, do nothing with X 412 bra I2C_RX_compass2_1 ; YES - do nothing with X
425 ; No, flip X 413 ; NO - flip X
426 comf hi ; 16bit sign change. 414 comf hi ; - 16 bit sign change
427 negf lo 415 negf lo
428 btfsc STATUS,C ; Carry to propagate ? 416 btfsc STATUS,C ; - carry to propagate?
429 incf hi,F ; YES: do it. 417 incf hi,F ; YES - do it
430 I2C_RX_compass2_1: 418 I2C_RX_compass2_1:
431 movff lo,compass_DX+0 419 MOVII mpr,compass_DX
432 movff hi,compass_DX+1 420 rcall I2C_OneByteRX ; get one byte
433 rcall I2C_OneByteRX ; Get one byte 421 movff SSP1BUF,lo ; data byte
434 movff SSP1BUF,lo ; Data Byte 422 rcall I2C_OneByteRX ; get one byte
435 rcall I2C_OneByteRX ; Get one byte 423 movff SSP1BUF,hi ; data byte
436 movff SSP1BUF,hi ; Data Byte
437 ; rcall I2C_TwoBytesRX_div8_2 424 ; rcall I2C_TwoBytesRX_div8_2
438 btfss flip_screen ; 180° rotation ? 425 btfss flip_screen ; 180° rotation?
439 bra I2C_RX_compass2_2 ; No, do nothing with Y 426 bra I2C_RX_compass2_2 ; NO - do nothing with Y
440 ; Yes, flip Y 427 ; YES - flip Y
441 comf hi ; 16bit sign change. 428 comf hi ; - 16 bit sign change
442 negf lo 429 negf lo
443 btfsc STATUS,C ; Carry to propagate ? 430 btfsc STATUS,C ; - carry to propagate?
444 incf hi,F ; YES: do it. 431 incf hi,F ; YES - do it
445 I2C_RX_compass2_2: 432 I2C_RX_compass2_2:
446 movff lo,compass_DY+0 433 MOVII mpr,compass_DY
447 movff hi,compass_DY+1 434 rcall I2C_OneByteRX ; get one byte
448 rcall I2C_OneByteRX ; Get one byte 435 movff SSP1BUF,lo ; data byte
449 movff SSP1BUF,lo ; Data Byte 436 rcall I2C_OneByteRX ; get one byte
450 rcall I2C_OneByteRX ; Get one byte 437 movff SSP1BUF,hi ; data byte
451 movff SSP1BUF,hi ; Data Byte
452 ; rcall I2C_TwoBytesRX_div8_2 438 ; rcall I2C_TwoBytesRX_div8_2
453 movff lo,compass_DZ+0 439 MOVII mpr,compass_DZ
454 movff hi,compass_DZ+1 440 bsf SSP1CON2,PEN ; stop condition
455 bsf SSP1CON2,PEN ; Stop condition
456 bra WaitMSSP ;(And return) 441 bra WaitMSSP ;(And return)
457 442
443 ENDIF ; _compass
444
445 ;-----------------------------------------------------------------------------
458 446
459 global I2C_init_compass 447 global I2C_init_compass
460 I2C_init_compass: 448 I2C_init_compass:
461 bsf compass_enabled 449 bsf compass_enabled
462 bcf compass_type2 450 bcf compass_type2
463 ; probe compass 2 451
464 bsf SSP1CON2,SEN ; Start condition 452 ; check for compass2
465 rcall WaitMSSP 453 bsf SSP1CON2,SEN ; start condition
466 movlw 0x32 ; Address byte + Write bit 454 rcall WaitMSSP
455 movlw 0x32 ; address byte + Write bit
467 movwf SSP1BUF ; control byte 456 movwf SSP1BUF ; control byte
468 rcall WaitMSSP 457 rcall WaitMSSP
469 btfss SSP1CON2,ACKSTAT ; ACK? 458 btfss SSP1CON2,ACKSTAT ; ACK?
470 bsf compass_type2 ; ACK send. compass2 present 459 bsf compass_type2 ; YES - ACK send, compass2 present
471 bsf SSP1CON2,PEN ; Stop condition 460 bsf SSP1CON2,PEN ; NO - stop condition
472 rcall WaitMSSP 461 rcall WaitMSSP
473
474 btfsc compass_type2 462 btfsc compass_type2
475 bra I2C_init_compass2 ; Compass2 463 bra I2C_init_compass2 ; compass2
476 ; Check for compass0 or compass1... 464
465 ; check for compass0 or compass1...
477 bsf compass_type ; set flag 466 bsf compass_type ; set flag
478 bsf SSP1CON2,SEN ; Start condition 467 bsf SSP1CON2,SEN ; start condition
479 rcall WaitMSSP 468 rcall WaitMSSP
480 movlw 0x3C ; address 469 movlw 0x3C ; address
481 rcall I2C_TX 470 rcall I2C_TX
482 movlw 0x0F 471 movlw 0x0F
483 rcall I2C_TX 472 rcall I2C_TX
484 bsf SSP1CON2,PEN ; Stop condition 473 bsf SSP1CON2,PEN ; stop condition
485 rcall WaitMSSP 474 rcall WaitMSSP
486 bcf PIR1,SSP1IF 475 bcf PIR1,SSP1IF
487 bsf SSP1CON2,SEN ; Start condition 476 bsf SSP1CON2,SEN ; start condition
488 rcall WaitMSSP 477 rcall WaitMSSP
489 movlw 0x3D ; address 478 movlw 0x3D ; address
490 rcall I2C_TX 479 rcall I2C_TX
491 rcall I2C_OneByteRX ; Get one byte 480 rcall I2C_OneByteRX ; get one byte
492 movlw 0x49 ; 0x49 = Compass1 481 movlw 0x49 ; 0x49 = Compass1
493 cpfseq SSP1BUF 482 cpfseq SSP1BUF
494 bcf compass_type ; clear flag 483 bcf compass_type ; clear flag
495 bsf SSP1CON2,PEN ; Stop condition 484 bsf SSP1CON2,PEN ; stop condition
496 rcall WaitMSSP 485 rcall WaitMSSP
497 486 btfsc compass_type ; compass1 ?
498 btfsc compass_type ; compass1? 487 bra I2C_init_compass1 ; YES
499 bra I2C_init_compass1 ; yes 488
500 ; init compass0 489 ; init compass0
501 bsf SSP1CON2,SEN ; Start condition 490 bsf SSP1CON2,SEN ; start condition
502 rcall WaitMSSP 491 rcall WaitMSSP
503 movlw 0x3C ; address 492 movlw 0x3C ; address
504 rcall I2C_TX 493 rcall I2C_TX
505 movlw 0x00 494 movlw 0x00
506 rcall I2C_TX 495 rcall I2C_TX
507 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias) 496 ; movlw b'01101001' ; ConfigA: 3 Hz, 8 samples averaged, test mode (positive bias)
508 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged 497 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged
509 rcall I2C_TX 498 rcall I2C_TX
510 I2C_init_compass_common: 499 I2C_init_compass_common:
511 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) 500 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
512 swapf i2c_temp1,F 501 swapf i2c_temp1,F
513 comf i2c_temp1,F 502 comf i2c_temp1,F
514 bcf STATUS,C 503 bcf STATUS,C
515 rlcf i2c_temp1 504 rlcf i2c_temp1
516 movf i2c_temp1,W 505 movf i2c_temp1,W
517 clrf i2c_temp1 506 clrf i2c_temp1
518 rcall I2C_TX 507 rcall I2C_TX
519 movlw b'00000000' ; Continuous Mode 508 movlw b'00000000' ; continuous mode
520 rcall I2C_TX 509 rcall I2C_TX
521 bsf SSP1CON2,PEN ; Stop condition 510 bsf SSP1CON2,PEN ; stop condition
522 bra WaitMSSP ; (And return) 511 bra WaitMSSP ; ... and return
523 512
524 I2C_init_compass1: 513 I2C_init_compass1:
525 bsf SSP1CON2,SEN ; Start condition 514 bsf SSP1CON2,SEN ; start condition
526 rcall WaitMSSP 515 rcall WaitMSSP
527 movlw 0x3C ; address 516 movlw 0x3C ; address
528 rcall I2C_TX 517 rcall I2C_TX
529 movlw 0x9F ; 1F with auto-increment (MSB=1) 518 movlw 0x9F ; 1F with auto-increment (MSB=1)
530 rcall I2C_TX 519 rcall I2C_TX
531 movlw b'00000000' ; CTRL0 520 movlw b'00000000' ; CTRL0
532 rcall I2C_TX 521 rcall I2C_TX
533 movlw b'00101111' ; CTRL1 (6,25Hz, BDU=0, x,y,z = ON) 522 movlw b'00101111' ; CTRL1 (6.25 Hz, BDU=0, x,y,z = ON)
534 rcall I2C_TX 523 rcall I2C_TX
535 movlw b'11000000' ; CTRL2 (50Hz, +/-2g, 524 movlw b'11000000' ; CTRL2 (50 Hz, +/- 2g)
536 rcall I2C_TX 525 rcall I2C_TX
537 movlw b'00000000' ; CTRL3 526 movlw b'00000000' ; CTRL3
538 rcall I2C_TX 527 rcall I2C_TX
539 movlw b'00000000' ; CTRL4 528 movlw b'00000000' ; CTRL4
540 rcall I2C_TX 529 rcall I2C_TX
541 movlw b'01100100' ; CTRL5 HIGH res, 6,25Hz 530 movlw b'01100100' ; CTRL5 HIGH res, 6.25 Hz
542 rcall I2C_TX 531 rcall I2C_TX
543 init_compass1_common: 532 init_compass1_common:
544 movff opt_compass_gain,i2c_temp1 ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) +++ 533 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
545 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) 534 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
546 dcfsnz i2c_temp1,F ; = 1? 535 dcfsnz i2c_temp1,F ; = 1?
547 movlw b'01100000' ; Yes, CTRL6 Full scale (+/-12 Gauss -> 2730LSB/Gauss) 536 movlw b'01100000' ; YES - CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
548 dcfsnz i2c_temp1,F ; = 2? 537 dcfsnz i2c_temp1,F ; = 2?
549 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss) 538 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
550 dcfsnz i2c_temp1,F ; = 3? 539 dcfsnz i2c_temp1,F ; = 3?
551 movlw b'01000000' ; Yes, CTRL6 (+/-8 Gauss) 540 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
552 dcfsnz i2c_temp1,F ; = 4? 541 dcfsnz i2c_temp1,F ; = 4?
553 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss) 542 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
554 dcfsnz i2c_temp1,F ; = 5? 543 dcfsnz i2c_temp1,F ; = 5?
555 movlw b'00100000' ; Yes, CTRL6 (+/-4 Gauss) 544 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
556 dcfsnz i2c_temp1,F ; = 6? 545 dcfsnz i2c_temp1,F ; = 6?
557 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss) 546 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
558 dcfsnz i2c_temp1,F ; = 7? 547 dcfsnz i2c_temp1,F ; = 7?
559 movlw b'00000000' ; Yes, CTRL6 (+/-2 Gauss) 548 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
560 rcall I2C_TX 549 rcall I2C_TX
561 movlw b'00000000' ; CTRL7 Continuous Mode 550 movlw b'00000000' ; CTRL7 Continuous Mode
562 rcall I2C_TX 551 rcall I2C_TX
563 bsf SSP1CON2,PEN ; Stop condition 552 bsf SSP1CON2,PEN ; stop condition
564 bra WaitMSSP ; (And return) 553 bra WaitMSSP ; ... and return
565 554
566 I2C_init_compass2: 555 I2C_init_compass2:
567 bsf SSP1CON2,SEN ; Start condition 556 bsf SSP1CON2,SEN ; start condition
568 rcall WaitMSSP 557 rcall WaitMSSP
569 movlw 0x3C ; address 558 movlw 0x3C ; address
570 rcall I2C_TX 559 rcall I2C_TX
571 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 560 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
572 rcall I2C_TX 561 rcall I2C_TX
573 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 562 movlw b'00000000' ; CFG_REG_A_M 0x60 (10 Hz, continuous)
574 rcall I2C_TX 563 rcall I2C_TX
575 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off) 0x61 (set pulse is released every 63 ODR) 564 movlw b'00000000' ; CFG_REG_B_M 0x61 (low-pass filter off, set pulse is released every 63 ODR)
576 rcall I2C_TX 565 rcall I2C_TX
577 movlw b'00000000' ; CFG_REG_C_M BDU=0 0x62 566 movlw b'00000000' ; CFG_REG_C_M BDU=0 0x62
578 rcall I2C_TX 567 rcall I2C_TX
579 bsf SSP1CON2,PEN ; Stop condition 568 bsf SSP1CON2,PEN ; stop condition
580 bra WaitMSSP ;(And return) 569 bra WaitMSSP ; ... and return
581 570
582 571
583 global I2C_sleep_compass 572 global I2C_sleep_compass
584 I2C_sleep_compass: 573 I2C_sleep_compass:
585 bcf compass_enabled 574 bcf compass_enabled
586 btfsc compass_type2 ; compass2? 575 btfsc compass_type2 ; compass2 ?
587 bra I2C_sleep_compass2 ; yes 576 bra I2C_sleep_compass2 ; YES
588 btfsc compass_type ; compass1? 577 btfsc compass_type ; compass1 ?
589 bra I2C_sleep_compass1 ; yes 578 bra I2C_sleep_compass1 ; YES
590 I2C_sleep_compass0: 579 I2C_sleep_compass0:
591 bsf SSP1CON2,SEN ; Start condition 580 bsf SSP1CON2,SEN ; start condition
592 rcall WaitMSSP 581 rcall WaitMSSP
593 movlw 0x3C ; address 582 movlw 0x3C ; address
594 rcall I2C_TX 583 rcall I2C_TX
595 movlw 0x00 584 movlw 0x00
596 rcall I2C_TX 585 rcall I2C_TX
597 movlw b'01101000' ; ConfigA 586 movlw b'01101000' ; ConfigA
598 rcall I2C_TX 587 rcall I2C_TX
599 movlw b'00100000' ; ConfigB 588 movlw b'00100000' ; ConfigB
600 rcall I2C_TX 589 rcall I2C_TX
601 movlw b'00000010' ; Idle Mode 590 movlw b'00000010' ; idle mode
602 rcall I2C_TX 591 rcall I2C_TX
603 bsf SSP1CON2,PEN ; Stop condition 592 bsf SSP1CON2,PEN ; stop condition
604 bra WaitMSSP ; (And return) 593 bra WaitMSSP ; ... and return
605 594
606 I2C_sleep_compass1: 595 I2C_sleep_compass1:
607 bsf SSP1CON2,SEN ; Start condition 596 bsf SSP1CON2,SEN ; start condition
608 rcall WaitMSSP 597 rcall WaitMSSP
609 movlw 0x3C ; address 598 movlw 0x3C ; address
610 rcall I2C_TX 599 rcall I2C_TX
611 movlw 0x20 ; CTRL_REG1 600 movlw 0x20 ; CTRL_REG1
612 rcall I2C_TX 601 rcall I2C_TX
613 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor Power-down mode 602 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode
614 rcall I2C_TX 603 rcall I2C_TX
615 bsf SSP1CON2,PEN ; Stop condition 604 bsf SSP1CON2,PEN ; stop condition
616 rcall WaitMSSP 605 rcall WaitMSSP
617 bsf SSP1CON2,SEN ; Start condition 606 bsf SSP1CON2,SEN ; start condition
618 rcall WaitMSSP 607 rcall WaitMSSP
619 movlw 0x3C ; address 608 movlw 0x3C ; address
620 rcall I2C_TX 609 rcall I2C_TX
621 movlw 0x26 ; CTRL_REG7 610 movlw 0x26 ; CTRL_REG7
622 rcall I2C_TX 611 rcall I2C_TX
623 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode 612 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode
624 rcall I2C_TX 613 rcall I2C_TX
625 bsf SSP1CON2,PEN ; Stop condition 614 bsf SSP1CON2,PEN ; stop condition
626 bra WaitMSSP ;(And return) 615 bra WaitMSSP ; and return
627 616
628 I2C_sleep_compass2: 617 I2C_sleep_compass2:
629 ; magnetic 618 ; magnetic
630 bsf SSP1CON2,SEN ; Start condition 619 bsf SSP1CON2,SEN ; start condition
631 rcall WaitMSSP 620 rcall WaitMSSP
632 movlw 0x3C ; address 621 movlw 0x3C ; address
633 rcall I2C_TX 622 rcall I2C_TX
634 movlw 0xE0 ; 0x60 with auto-increment (MSB=1) 623 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
635 rcall I2C_TX 624 rcall I2C_TX
636 movlw b'00000011' ; CFG_REG_A_M (Idle mode) 0x60 625 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode))
637 rcall I2C_TX 626 rcall I2C_TX
638 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition) 627 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition)
639 rcall I2C_TX 628 rcall I2C_TX
640 movlw b'01010001' ; CFG_REG_C_M 0x62 629 movlw b'01010001' ; CFG_REG_C_M 0x62
641 rcall I2C_TX 630 rcall I2C_TX
642 movlw b'00000000' ; INT_CTRL_REG_M 0x63 631 movlw b'00000000' ; INT_CTRL_REG_M 0x63
643 rcall I2C_TX 632 rcall I2C_TX
644 633 bsf SSP1CON2,PEN ; stop condition
645 bsf SSP1CON2,PEN ; Stop condition 634 bra WaitMSSP ; ... and return
646 bra WaitMSSP ; (And return) 635
647
648 I2C_sleep_accelerometer2: 636 I2C_sleep_accelerometer2:
649 ; accelerometer 637 ; accelerometer
650 bsf SSP1CON2,SEN ; Start condition 638 bsf SSP1CON2,SEN ; start condition
651 rcall WaitMSSP 639 rcall WaitMSSP
652 movlw 0x32 ; address 640 movlw 0x32 ; address
653 rcall I2C_TX 641 rcall I2C_TX
654 movlw 0x9F ; 1F with auto-increment (MSB=1) 642 movlw 0x9F ; 1F with auto-increment (MSB=1)
655 rcall I2C_TX 643 rcall I2C_TX
656 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off) 0x1F 644 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off)
657 rcall I2C_TX 645 rcall I2C_TX
658 movlw b'00000000' ; CTRL_REG1_A (All off) 0x20 646 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off)
659 rcall I2C_TX 647 rcall I2C_TX
660 bsf SSP1CON2,PEN ; Stop condition 648 bsf SSP1CON2,PEN ; stop condition
661 bra WaitMSSP ; (And return) 649 bra WaitMSSP ; ... and return
650
662 651
663 WaitMSSP: 652 WaitMSSP:
664 decfsz i2c_temp1,F ; check for timeout during I2C action 653 decfsz i2c_temp1,F ; check for timeout during I2C action
665 bra WaitMSSP2 654 bra WaitMSSP2
666 bra I2CFail ; timeout occurred 655 bra I2CFail ; timeout occurred
717 movlw b'00000000' 706 movlw b'00000000'
718 movwf SSP1CON2 707 movwf SSP1CON2
719 movlw 0x27 708 movlw 0x27
720 movwf SSP1ADD 709 movwf SSP1ADD
721 return 710 return
722 711
712
723 global I2C_init_accelerometer 713 global I2C_init_accelerometer
724 I2C_init_accelerometer: 714 I2C_init_accelerometer:
725 btfsc compass_type2 ; compass2? 715 btfsc compass_type2 ; compass2 ?
726 bra I2C_init_accelerometer2 ; Yes. 716 bra I2C_init_accelerometer2 ; YES
727 717
728 btfsc compass_type ; compass1? 718 btfsc compass_type ; compass1 ?
729 return ; yes, ignore 719 return ; YES - ignore
730 720
731 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode 721 rcall I2C_sleep_accelerometer ; registers can only be changed in standby mode
732 722
733 bsf SSP1CON2,SEN ; Start condition 723 bsf SSP1CON2,SEN ; start condition
734 rcall WaitMSSP 724 rcall WaitMSSP
735 movlw 0x38 ; address 725 movlw 0x38 ; address
736 rcall I2C_TX 726 rcall I2C_TX
737 movlw 0x0E ; XYZ_DATA_CFG 727 movlw 0x0E ; XYZ_DATA_CFG
738 rcall I2C_TX 728 rcall I2C_TX
739 movlw b'00000000' ; High pass Filter=0 , +/- 2g range 729 movlw b'00000000' ; high pass filter = 0, +/- 2g range
740 rcall I2C_TX 730 rcall I2C_TX
741 bsf SSP1CON2,PEN ; Stop condition 731 bsf SSP1CON2,PEN ; stop condition
742 rcall WaitMSSP 732 rcall WaitMSSP
743 733
744 bsf SSP1CON2,SEN ; Start condition 734 bsf SSP1CON2,SEN ; start condition
745 rcall WaitMSSP 735 rcall WaitMSSP
746 movlw 0x38 ; address 736 movlw 0x38 ; address
747 rcall I2C_TX 737 rcall I2C_TX
748 movlw 0x2A ; CTRL_REG1 738 movlw 0x2A ; CTRL_REG1
749 rcall I2C_TX 739 rcall I2C_TX
750 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode 740 ; movlw b'00110000' ; CTRL_REG1: 160 ms data rate, standby mode
751 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode 741 movlw b'00110100' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode
752 rcall I2C_TX 742 rcall I2C_TX
753 movlw b'00000010' ; CTRL_REG2: High Res in Active mode 743 movlw b'00000010' ; CTRL_REG2: high-res in active mode
754 rcall I2C_TX 744 rcall I2C_TX
755 bsf SSP1CON2,PEN ; Stop condition 745 bsf SSP1CON2,PEN ; stop condition
756 rcall WaitMSSP 746 rcall WaitMSSP
757 747
758 bsf SSP1CON2,SEN ; Start condition 748 bsf SSP1CON2,SEN ; start condition
759 rcall WaitMSSP 749 rcall WaitMSSP
760 movlw 0x38 ; address 750 movlw 0x38 ; address
761 rcall I2C_TX 751 rcall I2C_TX
762 movlw 0x2A ; CTRL_REG1 752 movlw 0x2A ; CTRL_REG1
763 rcall I2C_TX 753 rcall I2C_TX
764 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode 754 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode
765 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode 755 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode
766 rcall I2C_TX 756 rcall I2C_TX
767 bsf SSP1CON2,PEN ; Stop condition 757 bsf SSP1CON2,PEN ; stop condition
768 bra WaitMSSP ; (And return) 758 bra WaitMSSP ; ... and return
769 759
770 I2C_init_accelerometer2: 760 I2C_init_accelerometer2:
771 bsf SSP1CON2,SEN ; Start condition 761 bsf SSP1CON2,SEN ; start condition
772 rcall WaitMSSP 762 rcall WaitMSSP
773 movlw 0x32 ; address 763 movlw 0x32 ; address
774 rcall I2C_TX 764 rcall I2C_TX
775 movlw 0x9F ; 1F with auto-increment (MSB=1) 765 movlw 0x9F ; 1F with auto-increment (MSB=1)
776 rcall I2C_TX 766 rcall I2C_TX
784 ; rcall I2C_TX 774 ; rcall I2C_TX
785 ; movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g, 775 ; movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g,
786 ; rcall I2C_TX 776 ; rcall I2C_TX
787 ; movlw b'00000000' ; CTRL_REG5_A 777 ; movlw b'00000000' ; CTRL_REG5_A
788 ; rcall I2C_TX 778 ; rcall I2C_TX
789 bsf SSP1CON2,PEN ; Stop condition 779 bsf SSP1CON2,PEN ; stop condition
790 bra WaitMSSP ; (And return) 780 bra WaitMSSP ; ... and return
781
791 782
792 global I2C_sleep_accelerometer 783 global I2C_sleep_accelerometer
793 I2C_sleep_accelerometer: 784 I2C_sleep_accelerometer:
794 btfsc compass_type2 ; Compass2 785 btfsc compass_type2 ; Compass2
795 bra I2C_sleep_accelerometer2 ; Yes 786 bra I2C_sleep_accelerometer2 ; YES
796 btfsc compass_type ; compass1? 787 btfsc compass_type ; compass1?
797 return ; yes, ignore 788 return ; YES - ignore
798 789
799 bsf SSP1CON2,SEN ; Start condition 790 bsf SSP1CON2,SEN ; start condition
800 rcall WaitMSSP 791 rcall WaitMSSP
801 movlw 0x38 ; address 792 movlw 0x38 ; address
802 rcall I2C_TX 793 rcall I2C_TX
803 movlw 0x2A ; CTRL_REG1 794 movlw 0x2A ; CTRL_REG1
804 rcall I2C_TX 795 rcall I2C_TX
805 movlw b'00000000' ; St. By Mode 796 movlw b'00000000' ; standby mode
806 rcall I2C_TX 797 rcall I2C_TX
807 bsf SSP1CON2,PEN ; Stop condition 798 bsf SSP1CON2,PEN ; stop condition
808 bra WaitMSSP ; (And return) 799 bra WaitMSSP ; ... and return
809 800
810 lt2942_init_again: 801 lt2942_init_again:
811 clrf i2c_temp1 802 clrf i2c_temp1
812 movlw 0x02 ; Point to accumulated charge registers 803 movlw 0x02 ; point to accumulated charge registers
813 rcall I2C_TX_GAUGE 804 rcall I2C_TX_GAUGE
814 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte 805 movff battery_accumulated_charge+1,SSP1BUF ; data byte
815 rcall WaitMSSP 806 rcall WaitMSSP
816 rcall I2C_WaitforACK 807 rcall I2C_WaitforACK
817 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte 808 movff battery_accumulated_charge+0,SSP1BUF ; data byte
818 rcall WaitMSSP 809 rcall WaitMSSP
819 rcall I2C_WaitforACK 810 rcall I2C_WaitforACK
820 bsf SSP1CON2,PEN ; Stop condition 811 bsf SSP1CON2,PEN ; stop condition
821 rcall WaitMSSP 812 rcall WaitMSSP
822 movff battery_acumulated_charge+1,sub_a+1 813 MOVII battery_accumulated_charge,sub_a
823 movff battery_acumulated_charge+0,sub_a+0
824 ; and init again... 814 ; and init again...
825 815
826 global lt2942_init 816 global lt2942_init
827 lt2942_init: ; Setup Control register B 817 lt2942_init: ; setup control register B
828 clrf i2c_temp1 818 clrf i2c_temp1
829 movlw 0x01 ; Point to control reg B 819 movlw 0x01 ; point to control reg B
830 rcall I2C_TX_GAUGE 820 rcall I2C_TX_GAUGE
831 movlw b'11111000' ; Automatic conversion every two seconds 821 movlw b'11111000' ; automatic conversion every two seconds
832 movff WREG, SSP1BUF ; Data Byte 822 movff WREG, SSP1BUF ; data byte
833 rcall WaitMSSP 823 rcall WaitMSSP
834 rcall I2C_WaitforACK 824 rcall I2C_WaitforACK
835 bsf SSP1CON2,PEN ; Stop condition 825 bsf SSP1CON2,PEN ; stop condition
836 bra WaitMSSP ; (And return) 826 bra WaitMSSP ; ... and return
837 827
838 global lt2942_get_status 828 global lt2942_get_status
839 lt2942_get_status: ; Read status register 829 lt2942_get_status: ; read status register
840 bcf battery_gauge_available ; Clear flag 830 bcf battery_gauge_available ; clear flag
841 clrf i2c_temp1 831 clrf i2c_temp1
842 movlw 0x00 ; Point to Status reg 832 movlw 0x00 ; point to status register
843 rcall I2C_TX_GAUGE 833 rcall I2C_TX_GAUGE
844 rcall I2C_RX_GAUGE 834 rcall I2C_RX_GAUGE
845 movff SSP1BUF,WREG 835 movff SSP1BUF,WREG
846 btfss WREG,7 ; 2942 found? 836 btfss WREG,7 ; 2942 found?
847 bsf battery_gauge_available ; Yes, set flag 837 bsf battery_gauge_available ; YES - set flag
848 bsf SSP1CON2,PEN ; Stop condition 838 bsf SSP1CON2,PEN ; stop condition
849 bra WaitMSSP ; (And return) 839 bra WaitMSSP ; ... and return
850 840
851 841
852 global lt2942_get_voltage 842 global lt2942_get_voltage
853 lt2942_get_voltage: ; Read battery voltage registers 843 lt2942_get_voltage: ; read battery voltage registers
854 clrf i2c_temp1 844 clrf i2c_temp1
855 movlw 0x08 ; Point to voltage registers 845 movlw 0x08 ; point to voltage registers
856 rcall I2C_TX_GAUGE 846 rcall I2C_TX_GAUGE
857 rcall I2C_RX_GAUGE 847 rcall I2C_RX_GAUGE
858 bsf SSP1CON2,ACKEN ; Master acknowledge 848 bsf SSP1CON2,ACKEN ; master acknowledge
859 rcall WaitMSSP 849 rcall WaitMSSP
860 movff SSP1BUF,xA+1 850 movff SSP1BUF,xA+1
861 bsf SSP1CON2, RCEN ; Enable receive mode 851 bsf SSP1CON2, RCEN ; enable receive mode
862 rcall WaitMSSP 852 rcall WaitMSSP
863 movff SSP1BUF,xA+0 853 movff SSP1BUF,xA+0
864 bsf SSP1CON2,PEN ; Stop condition 854 bsf SSP1CON2,PEN ; stop condition
865 rcall WaitMSSP 855 rcall WaitMSSP
866 856
867 ; banksel common 857 ; convert voltage from raw value to Volt
868 ; xA:2 loaded with raw values 858 MOVLI .6000,xB ; load conversion multiplicand into xB
869 movlw LOW .6000 859 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
870 movwf xB+0 860 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
871 movlw HIGH .6000 861 movff xC+2,batt_voltage+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result
872 movwf xB+1 862 movff xC+3,batt_voltage+1 ; ...
873 call mult16x16 ; xA*xB=xC 863
874 864 tstfsz batt_voltage+1 ; < 256 mV ?
875 ; xC+3:xC+2 -> Result in mV 865 return ; NO - done
876 866 bra lt2942_init ; YES - ... and return
877 ; Update battery voltage in mV 867
878 movff xC+3,batt_voltage+1
879 movff xC+2,batt_voltage+0
880
881 tstfsz batt_voltage+1 ; <256mV?
882 return ; No, done.
883 bra lt2942_init ;(and return)
884 868
885 global lt2942_get_temperature 869 global lt2942_get_temperature
886 lt2942_get_temperature: ; Read temperature registers 870 lt2942_get_temperature: ; read battery temperature
887 clrf i2c_temp1 871 clrf i2c_temp1
888 movlw 0x0C ; Point to temperature registers 872 movlw 0x0C ; point to temperature register
889 call I2C_TX_GAUGE 873 call I2C_TX_GAUGE
890 call I2C_RX_GAUGE 874 call I2C_RX_GAUGE
891 bsf SSP1CON2,ACKEN ; Master acknowlegde 875 bsf SSP1CON2,ACKEN ; master acknowledge
892 rcall WaitMSSP 876 rcall WaitMSSP
893 movff SSP1BUF,xA+1 877 movff SSP1BUF,xA+1 ; store raw temperature, high byte
894 bsf SSP1CON2, RCEN ; Enable recieve mode 878 bsf SSP1CON2, RCEN ; enable receive mode
895 rcall WaitMSSP 879 rcall WaitMSSP
896 movff SSP1BUF,xA+0 880 movff SSP1BUF,xA+0 ; store raw temperature, low byte
897 bsf SSP1CON2,PEN ; Stop condition 881 bsf SSP1CON2,PEN ; stop condition
898 rcall WaitMSSP 882 rcall WaitMSSP
899 883
900 ; banksel common 884 ; convert temperature from raw value to Kelvin
901 ; xA:2 loaded with raw values 885 MOVLI .6000,xB ; load conversion multiplicand into xB
902 movlw LOW .6000 886 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
903 movwf xB+0 887 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
904 movlw HIGH .6000 888 movff xC+2,battery_temperature+0 ; divide by 65536 can easily be done by just taking the 3rd and 4th byte of the multiplication result
905 movwf xB+1 889 movff xC+3,battery_temperature+1 ; ...
906 call mult16x16 ;xA*xB=xC 890
907 891 ; check if battery is charged right now
908 ; xC+3:xC+2 -> Result in 0.1K 892 btfss cc_active ; in CC charging mode?
909 893 return ; NO - not charging, done
910 movlw LOW max_allowed_battery_temp ; in 0.1K 894
911 movwf sub_a+0 895 ; check for over-temperature while charging
912 movlw HIGH max_allowed_battery_temp 896 MOVLI max_battery_charge_temp,sub_a
913 movwf sub_a+1 897 MOVII battery_temperature, sub_b
914 movff xC+3,sub_b+1 898 call cmpU16 ; sub_a - sub_b (with UNSIGNED values)
915 movff xC+2,sub_b+0 899 btfss neg_flag ; result negative?
916 call subU16 ; sub_c = sub_a - sub_b (with UNSIGNED values) 900 return ; NO - temperature <= threshold, ok, done
917 btfss neg_flag 901 ; YES - too hot, disable charging circuitry
918 return ; temp ok, return 902 bsf charge_disable ; - set charging-inhibit signal
919 ; too hot, disable charge if currently charging 903 bcf charge_enable ; - activate charging-inhibit signal
920 btfss cc_active 904 bsf battery_overtemp ; - flag that the battery charging over-temperature protection has tripped
921 return ; Not charging, return 905 return
922 ; charging: Disable now 906
923 bsf charge_disable
924 bcf TRISE,2
925 bsf battery_overtemp ; =1: The battery was charged and temp was too high (Only cleared on POR)
926 return
927 907
928 global lt2942_get_accumulated_charge 908 global lt2942_get_accumulated_charge
929 lt2942_get_accumulated_charge: ; Read accumulated charge and compute percent 909 lt2942_get_accumulated_charge: ; read accumulated charge and compute percent
930 clrf i2c_temp1 910 clrf i2c_temp1
931 movlw 0x00 ; Point to status register 911 movlw 0x00 ; point to status register
932 rcall I2C_TX_GAUGE 912 rcall I2C_TX_GAUGE
933 rcall I2C_RX_GAUGE 913 rcall I2C_RX_GAUGE
934 bsf SSP1CON2,ACKEN ; Master acknowledge 914 bsf SSP1CON2,ACKEN ; master acknowledge
935 rcall WaitMSSP 915 rcall WaitMSSP
936 movff SSP1BUF,gauge_status_byte 916 movff SSP1BUF,gauge_status_byte
937 917
938 bsf SSP1CON2, RCEN ; Enable receive mode 918 bsf SSP1CON2, RCEN ; enable receive mode
939 rcall WaitMSSP ; Dummy read (Control byte) 919 rcall WaitMSSP ; dummy read (control byte)
940 movf SSP1BUF,W 920 movf SSP1BUF,W
941 bsf SSP1CON2,ACKEN ; Master acknowledge 921 bsf SSP1CON2,ACKEN ; master acknowledge
942 rcall WaitMSSP 922 rcall WaitMSSP
943 923
944 bsf SSP1CON2, RCEN ; Enable receive mode 924 bsf SSP1CON2, RCEN ; enable receive mode
945 rcall WaitMSSP 925 rcall WaitMSSP
946 movff SSP1BUF,sub_a+1 926 movff SSP1BUF,sub_a+1
947 bsf SSP1CON2,ACKEN ; Master acknowledge 927 bsf SSP1CON2,ACKEN ; master acknowledge
948 rcall WaitMSSP 928 rcall WaitMSSP
949 929
950 bsf SSP1CON2, RCEN ; Enable receive mode 930 bsf SSP1CON2, RCEN ; enable receive mode
951 rcall WaitMSSP 931 rcall WaitMSSP
952 movff SSP1BUF,sub_a+0 932 movff SSP1BUF,sub_a+0
953 bsf SSP1CON2,PEN ; Stop condition 933 bsf SSP1CON2,PEN ; stop condition
954 rcall WaitMSSP 934 rcall WaitMSSP
955 935
956 movff gauge_status_byte,sub_b+0 ; copy into bank common 936 btfsc gauge_status_byte,0 ; UVLO event ?
957 btfsc sub_b+0,0 ; =1: UVLO Event 937 rcall lt2942_init_again ; YES
958 rcall lt2942_init_again 938
959 939 MOVII sub_a,battery_accumulated_charge ; save raw value
960 movff sub_a+1,battery_acumulated_charge+1 ; Save raw value
961 movff sub_a+0,battery_acumulated_charge+0 ; Save raw value
962 940
963 ; Compute batt_percent 941 ; Compute batt_percent
964 ; (charge-battery_offset)/365 942 ; (charge-battery_offset)/365
965 movff battery_offset+0,sub_b+0 943 MOVII battery_offset,sub_b
966 movff battery_offset+1,sub_b+1
967 call subU16 ; sub_c = sub_a - sub_b (with signed values) 944 call subU16 ; sub_c = sub_a - sub_b (with signed values)
968 945 clrf batt_percent ; set to zero
969 clrf batt_percent ; Set to zero
970 btfsc neg_flag ; result negative? 946 btfsc neg_flag ; result negative?
971 bra lt2942_set_to_zero_percent ; Yes, keep LT2942 at zero percent and return 947 bra lt2942_set_to_zero_percent ; YES - keep LT2942 at zero percent and return
972 948
973 ; > Zero, set batt_percent properly 949 ; > zero, set batt_percent properly
974 movff sub_c+0,xA+0 950 MOVII sub_c,xA
975 movff sub_c+1,xA+1 951 MOVII battery_capacity,xB
976 movff battery_capacity+0,xB+0
977 movff battery_capacity+1,xB+1
978 call div16x16 ; xC = xA / xB with xA as remainder 952 call div16x16 ; xC = xA / xB with xA as remainder
979 movff xC+0,batt_percent 953 movff xC+0,batt_percent
980 return 954 return
981 955
982 lt2942_set_to_zero_percent: 956 lt2942_set_to_zero_percent:
983 clrf i2c_temp1 957 clrf i2c_temp1
984 movlw 0x02 ; Point to accumulated charge registers 958 movlw 0x02 ; point to accumulated charge registers
985 rcall I2C_TX_GAUGE 959 rcall I2C_TX_GAUGE
986 movff battery_offset+1,SSP1BUF 960 movff battery_offset+1,SSP1BUF
987 rcall WaitMSSP 961 rcall WaitMSSP
988 rcall I2C_WaitforACK 962 rcall I2C_WaitforACK
989 movff battery_offset+0,SSP1BUF 963 movff battery_offset+0,SSP1BUF
990 rcall WaitMSSP 964 rcall WaitMSSP
991 rcall I2C_WaitforACK 965 rcall I2C_WaitforACK
992 bsf SSP1CON2,PEN ; Stop condition 966 bsf SSP1CON2,PEN ; stop condition
993 bra WaitMSSP ; (and return) 967 bra WaitMSSP ; ... and return
994 968
995 global lt2942_charge_done 969 global lt2942_charge_done
996 lt2942_charge_done: ; Reset accumulating registers to 0xFFFF 970 lt2942_charge_done: ; reset accumulating registers to 0xFFFF
997 clrf i2c_temp1 971 clrf i2c_temp1
998 movlw 0x02 ; Point to accumulated charge registers 972 movlw 0x02 ; point to accumulated charge registers
999 rcall I2C_TX_GAUGE 973 rcall I2C_TX_GAUGE
1000 setf SSP1BUF ; Data Byte 974 setf SSP1BUF ; data byte
1001 rcall WaitMSSP 975 rcall WaitMSSP
1002 rcall I2C_WaitforACK 976 rcall I2C_WaitforACK
1003 setf SSP1BUF ; Data Byte 977 setf SSP1BUF ; data byte
1004 rcall WaitMSSP 978 rcall WaitMSSP
1005 rcall I2C_WaitforACK 979 rcall I2C_WaitforACK
1006 bsf SSP1CON2,PEN ; Stop condition 980 bsf SSP1CON2,PEN ; stop condition
1007 bra WaitMSSP ; (and return) 981 bra WaitMSSP ; ... and return
1008 982
1009 I2C_TX_GAUGE: ; Sends a byte to the LT2942 Gauge IC 983 I2C_TX_GAUGE: ; send a byte to the LT2942 gauge IC
1010 movwf i2c_temp2 ; Data byte 984 movwf i2c_temp2 ; data byte
1011 bsf SSP1CON2,SEN ; Start condition 985 bsf SSP1CON2,SEN ; start condition
1012 rcall WaitMSSP 986 rcall WaitMSSP
1013 movlw b'11001000' ; Address byte + Write bit 987 movlw b'11001000' ; address byte + Write bit
1014 movwf SSP1BUF ; control byte 988 movwf SSP1BUF ; control byte
1015 rcall WaitMSSP 989 rcall WaitMSSP
1016 rcall I2C_WaitforACK 990 rcall I2C_WaitforACK
1017 movf i2c_temp2,W 991 movf i2c_temp2,W
1018 bra I2C_TX ; (and return) 992 bra I2C_TX ; ... and return
1019 993
1020 I2C_RX_GAUGE: 994 I2C_RX_GAUGE:
1021 bsf SSP1CON2,SEN ; Start condition 995 bsf SSP1CON2,SEN ; start condition
1022 rcall WaitMSSP 996 rcall WaitMSSP
1023 movlw b'11001001' ; Address byte + Read bit 997 movlw b'11001001' ; address byte + Read bit
1024 movwf SSP1BUF ; control byte 998 movwf SSP1BUF ; control byte
1025 rcall WaitMSSP 999 rcall WaitMSSP
1026 rcall I2C_WaitforACK 1000 rcall I2C_WaitforACK
1027 bsf SSP1CON2, RCEN ; Enable receive mode 1001 bsf SSP1CON2, RCEN ; enable receive mode
1028 bra WaitMSSP ; (and return) 1002 bra WaitMSSP ; ... and return
1029 1003
1030 1004
1031 ;----------------------------------------------------------------------------- 1005 ;=============================================================================
1032 ; Transmitter Functions 1006 ; Transmitter Functions
1033 1007 ;
1034 IFDEF _rx_functions 1008 IFDEF _rx_functions
1035 1009
1036 global I2C_probe_OSTC_rx 1010 global I2C_probe_OSTC_rx
1037 I2C_probe_OSTC_rx: 1011 I2C_probe_OSTC_rx:
1038 movlw .5 1012 bcf ostc_rx_present ; no TR module by default
1039 movwf lo_temp 1013 clrf WREG ; bank-safe set to zero of ...
1014 movff WREG,rx_firmware_cur_major ; ... current TR module firmware, major
1015 movff WREG,rx_firmware_cur_minor ; ... current TR module firmware, minor
1016 movlw .5 ; max number of tries for detecting a TR module
1017 movwf hy ; initialize counter for tries
1040 I2C_probe_OSTC_rx_1: 1018 I2C_probe_OSTC_rx_1:
1041 bsf SSP1CON2,SEN ; Start condition 1019 bsf SSP1CON2,SEN ; start condition
1042 rcall WaitMSSP 1020 rcall WaitMSSP
1043 movlw 0x50 ; Address byte + Write bit 1021 movlw 0x50 ; address byte + write bit
1044 movwf SSP1BUF ; control byte 1022 movwf SSP1BUF ; control byte
1045 rcall WaitMSSP 1023 rcall WaitMSSP
1046 btfss SSP1CON2,ACKSTAT ; ACK? 1024 btfss SSP1CON2,ACKSTAT ; ACK received?
1047 bsf ostc_rx_present ; ACK sent - OSTC_RX present! 1025 bsf ostc_rx_present ; YES - TR module detected
1048 bsf SSP1CON2,PEN ; Stop condition 1026 bsf SSP1CON2,PEN ; stop condition
1049 rcall WaitMSSP 1027 rcall WaitMSSP
1050 btfss ostc_rx_present ; Do we have the RX? 1028 btfss ostc_rx_present ; was a TR module detected?
1051 return ; No, Done. 1029 return ; NO - done
1052 WAITMS .1 1030 WAITMS .1
1053 bsf SSP1CON2,SEN ; Start condition 1031 bsf SSP1CON2,SEN ; start condition
1054 rcall WaitMSSP 1032 rcall WaitMSSP
1055 movlw 0x50 ; Address byte + Write bit 1033 movlw 0x50 ; address byte + write bit
1056 movwf SSP1BUF ; control byte 1034 movwf SSP1BUF ; control byte
1057 rcall WaitMSSP 1035 rcall WaitMSSP
1058 rcall I2C_WaitforACK 1036 rcall I2C_WaitforACK
1059 movlw 0x1B 1037 movlw 0x1B
1060 movwf SSP1BUF ; Data Byte (Get firmware) 1038 movwf SSP1BUF ; data byte (get firmware)
1061 rcall WaitMSSP 1039 rcall WaitMSSP
1062 rcall I2C_WaitforACK 1040 rcall I2C_WaitforACK
1063 bsf SSP1CON2,PEN ; Stop condition 1041 bsf SSP1CON2,PEN ; stop condition
1064 rcall WaitMSSP 1042 rcall WaitMSSP
1065 WAITMS .1 1043 WAITMS .1
1066 bsf SSP1CON2,SEN ; Start condition 1044 bsf SSP1CON2,SEN ; start condition
1067 rcall WaitMSSP 1045 rcall WaitMSSP
1068 movlw 0x51 ; Address byte + Read bit 1046 movlw 0x51 ; address byte + Read bit
1069 movwf SSP1BUF ; control byte 1047 movwf SSP1BUF ; control byte
1070 rcall WaitMSSP 1048 rcall WaitMSSP
1071 bsf SSP1CON2, RCEN ; Enable receive mode 1049 bsf SSP1CON2,RCEN ; enable receive mode
1072 rcall WaitMSSP 1050 rcall WaitMSSP
1073 movff SSP1BUF,rx_firmware+0 1051 movff SSP1BUF,rx_firmware_cur_major ; store as firmware version, major
1074 bsf SSP1CON2,ACKEN ; Master acknowledge 1052 bsf SSP1CON2,ACKEN ; master acknowledge
1075 rcall WaitMSSP 1053 rcall WaitMSSP
1076 1054
1077 ; last byte in read from RX circuity always with a NACK! 1055 ; last byte in read from RX circuity always with a NACK!
1078 bsf SSP1CON2, RCEN ; Enable receive mode 1056 bsf SSP1CON2,RCEN ; enable receive mode
1079 rcall WaitMSSP 1057 rcall WaitMSSP
1080 movff SSP1BUF,rx_firmware+1 1058 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor
1081 bsf SSP1CON2,ACKDT 1059 bsf SSP1CON2,ACKDT
1082 bsf SSP1CON2,ACKEN ; Master NOT acknowledge 1060 bsf SSP1CON2,ACKEN ; master NOT acknowledge
1083 rcall WaitMSSP 1061 rcall WaitMSSP
1084 bcf SSP1CON2,ACKDT ; Reset ACKDT flag 1062 bcf SSP1CON2,ACKDT ; reset ACKDT flag
1085 bsf SSP1CON2,PEN ; Stop condition 1063 bsf SSP1CON2,PEN ; stop condition
1086 rcall WaitMSSP 1064 rcall WaitMSSP
1087 1065
1088 ; test for RX part not being ready during this read 1066 ; wait for TR module becoming ready
1089 movff rx_firmware+1,i2c_temp1 1067 movff rx_firmware_cur_minor,i2c_temp1 ; copy firmware version to bank common, minor
1090 movlw .147 1068 movlw .147 ; code for not ready, minor
1091 cpfseq i2c_temp1 1069 cpfseq i2c_temp1 ; equal?
1092 bra I2C_probe_OSTC_rx_2 ; not equal 1070 bra I2C_probe_OSTC_rx_2 ; NO - TR module ready
1093 movff rx_firmware+0,i2c_temp1 1071 movff rx_firmware_cur_major,i2c_temp1 ; YES - copy firmware version to bank common, major
1094 movlw .27 1072 movlw .27 ; - code for not ready, major
1095 cpfseq i2c_temp1 1073 cpfseq i2c_temp1 ; - equal?
1096 bra I2C_probe_OSTC_rx_2 ; not equal 1074 bra I2C_probe_OSTC_rx_2 ; NO - TR module ready
1097 bsf active_reset_ostc_rx 1075 bsf active_reset_ostc_rx ; YES - apply reset to TR module
1098 WAITMS .5 1076 WAITMS .5 ; - wait 5 ms
1099 bcf active_reset_ostc_rx 1077 bcf active_reset_ostc_rx ; - release reset
1100 WAITMS .250 1078 WAITMS .250 ; - wait for 250 ms
1101 WAITMS .250 1079 WAITMS .250 ; - wait another 250 ms
1102 clrf i2c_temp1 1080 clrf i2c_temp1 ; - clean-up i2c_temp1
1103 decfsz lo_temp,F ; try max. 5 times 1081 decfsz hy,F ; - decrement counter for number of tries, became zero?
1104 bra I2C_probe_OSTC_rx_1 1082 bra I2C_probe_OSTC_rx_1 ; - NO - try again
1105 bcf ostc_rx_present ; Clear flag. Something is wrong 1083 bcf ostc_rx_present ; - YES - something is wrong, flag TR module as not available
1106 I2C_probe_OSTC_rx_2: 1084 I2C_probe_OSTC_rx_2:
1107 clrf i2c_temp1 1085 clrf i2c_temp1 ; clean-up i2c_temp1
1108 return 1086 return ; done
1109 1087
1110 1088
1111 global I2C_get_tankdata 1089 global I2C_get_tankdata
1112 I2C_get_tankdata: 1090 I2C_get_tankdata:
1113 bsf SSP1CON2,SEN ; Start condition 1091 bsf SSP1CON2,SEN ; start condition
1114 rcall WaitMSSP 1092 rcall WaitMSSP
1115 movlw 0x50 ; Address byte + Write bit 1093 movlw 0x50 ; address byte + write bit
1116 movwf SSP1BUF ; control byte 1094 movwf SSP1BUF ; control byte
1117 rcall WaitMSSP 1095 rcall WaitMSSP
1118 rcall I2C_WaitforACK 1096 rcall I2C_WaitforACK
1119 movlw 0x1E ; Read buffer2 (48 Bytes) 1097 movlw 0x1E ; read buffer2 (48 bytes)
1120 movwf SSP1BUF ; Data Byte 1098 movwf SSP1BUF ; data byte
1121 rcall WaitMSSP 1099 rcall WaitMSSP
1122 rcall I2C_WaitforACK 1100 rcall I2C_WaitforACK
1123 bsf SSP1CON2,PEN ; Stop condition 1101 bsf SSP1CON2,PEN ; stop condition
1124 rcall WaitMSSP 1102 rcall WaitMSSP
1125 WAITMS .1 1103 WAITMS .1
1126
1127 ; read 48 bytes 1104 ; read 48 bytes
1128 bsf SSP1CON2,SEN ; Start condition 1105 bsf SSP1CON2,SEN ; start condition
1129 rcall WaitMSSP 1106 rcall WaitMSSP
1130 movlw 0x51 ; Address byte + read bit 1107 movlw 0x51 ; address byte + read bit
1131 movwf SSP1BUF ; control byte 1108 movwf SSP1BUF ; control byte
1132 rcall WaitMSSP 1109 rcall WaitMSSP
1133 rcall I2C_WaitforACK 1110 rcall I2C_WaitforACK
1134 movlw .47 ; 47 with ACK + 1 w/o ACK 1111 movlw .47 ; 47 with ACK + 1 w/o ACK
1135 movwf i2c_temp2 1112 movwf i2c_temp2
1136 lfsr FSR2,rx_buffer+0 1113 lfsr FSR2,rx_buffer
1137 I2C_get_tankdata_loop_read: 1114 I2C_get_tankdata_loop_read:
1138 bsf SSP1CON2, RCEN ; Enable receive mode 1115 bsf SSP1CON2, RCEN ; enable receive mode
1139 rcall WaitMSSP 1116 rcall WaitMSSP
1140 movff SSP1BUF,POSTINC2 1117 movff SSP1BUF,POSTINC2
1141 bcf SSP1CON2,ACKDT 1118 bcf SSP1CON2,ACKDT
1142 bsf SSP1CON2,ACKEN ; Master acknowledge 1119 bsf SSP1CON2,ACKEN ; master acknowledge
1143 rcall WaitMSSP 1120 rcall WaitMSSP
1144 decfsz i2c_temp2,F 1121 decfsz i2c_temp2,F
1145 bra I2C_get_tankdata_loop_read 1122 bra I2C_get_tankdata_loop_read
1146
1147 ; 1 w/o ACK 1123 ; 1 w/o ACK
1148 bsf SSP1CON2, RCEN ; Enable receive mode 1124 bsf SSP1CON2, RCEN ; enable receive mode
1149 rcall WaitMSSP 1125 rcall WaitMSSP
1150 movff SSP1BUF,POSTINC2 1126 movff SSP1BUF,POSTINC2
1151 bsf SSP1CON2,ACKDT 1127 bsf SSP1CON2,ACKDT
1152 bsf SSP1CON2,ACKEN ; Master NOT acknowledge 1128 bsf SSP1CON2,ACKEN ; master NOT acknowledge
1153 rcall WaitMSSP 1129 rcall WaitMSSP
1154 bcf SSP1CON2,ACKDT ; Reset ACKDT flag 1130 bcf SSP1CON2,ACKDT ; reset ACKDT flag
1155 1131 bsf SSP1CON2,PEN ; stop condition
1156 bsf SSP1CON2,PEN ; Stop condition 1132 bra WaitMSSP ; ... and return
1157 bra WaitMSSP ;(and return)
1158 1133
1159 1134
1160 global I2C_update_OSTC_rx 1135 global I2C_update_OSTC_rx
1161 I2C_update_OSTC_rx: ; 992*64byte master loop 1136 I2C_update_OSTC_rx: ; transfer 64 byte to RX co-processor
1137 ; setup for write
1162 bcf i2c_error_flag ; clear error flag 1138 bcf i2c_error_flag ; clear error flag
1139 lfsr FSR2,buffer ; initialize pointer to send buffer used for verify
1140 movlw .64 ; initialize loop counter: 64 byte with ACK
1141 movwf i2c_temp2 ; ...
1142 ; address write
1143 bsf SSP1CON2,SEN ; start condition
1144 rcall WaitMSSP
1145 movlw 0x50 ; address byte + write bit
1146 movwf SSP1BUF ; control byte
1147 rcall WaitMSSP
1148 rcall I2C_WaitforACK
1163 ; write 64 bytes 1149 ; write 64 bytes
1164 bsf SSP1CON2,SEN ; Start condition 1150 I2C_update_OSTC_loop:
1165 rcall WaitMSSP 1151 TBLRD*+ ; read a byte from program memory
1166 movlw 0x50 ; Address byte + Write bit 1152 movff TABLAT,POSTINC2 ; copy to send buffer
1153 movff TABLAT,SSP1BUF ; copy to I2C data buffer
1154 rcall WaitMSSP
1155 rcall I2C_WaitforACK
1156 decfsz i2c_temp2,F ;decrement loop counter, became zero?
1157 bra I2C_update_OSTC_loop ; NO - loop
1158 bsf SSP1CON2,PEN ; YES - stop condition
1159 rcall WaitMSSP ; - wait for stop condition done
1160 WAITMS .1 ; - wait another 1 ms
1161 ; setup for read-back
1162 lfsr FSR2,buffer ; reset pointer to begin of send buffer
1163 movlw .63 ; initialize loop counter: 63 byte with ACK + 1 w/o ACK
1164 movwf i2c_temp2
1165 ; address read-back
1166 bsf SSP1CON2,SEN ; start condition
1167 rcall WaitMSSP
1168 movlw 0x51 ; address byte + read bit
1167 movwf SSP1BUF ; control byte 1169 movwf SSP1BUF ; control byte
1168 rcall WaitMSSP 1170 rcall WaitMSSP
1169 rcall I2C_WaitforACK 1171 rcall I2C_WaitforACK
1170 lfsr FSR2,buffer ; send buffer for verify 1172 ; read-back 64 bytes
1171 movlw .64 1173 I2C_update_OSTC_loop_read:
1172 movwf i2c_temp2 1174 bsf SSP1CON2,RCEN ; enable receive mode
1173 I2C_update_OSTC_loop: ; 64byte flash page loop 1175 rcall WaitMSSP
1174 movff up,POSTINC2 ; store for verify 1176 movf SSP1BUF,W
1175 movff up,SSP1BUF 1177 cpfseq POSTINC2 ; compare read-back byte with sent byte, equal?
1176 rcall WaitMSSP 1178 bsf i2c_error_flag ; NO - not equal, set error flag
1177 rcall I2C_WaitforACK 1179 bcf SSP1CON2,ACKDT
1178 call ext_flash_read_block ; Read one byte 1180 bsf SSP1CON2,ACKEN ; master acknowledge
1179 movwf up ; prepare for transmit 1181 rcall WaitMSSP
1180 decfsz i2c_temp2,F 1182 decfsz i2c_temp2,F ; decrement loop counter, became zero?
1181 bra I2C_update_OSTC_loop 1183 bra I2C_update_OSTC_loop_read ; NO - loop
1182 bsf SSP1CON2,PEN ; Stop condition 1184 ; 1 w/o ACK
1185 bsf SSP1CON2, RCEN ; YES - enable receive mode
1186 rcall WaitMSSP ; -
1187 movf SSP1BUF,W ; - get 64th byte
1188 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal?
1189 bsf i2c_error_flag ; NO - not equal, set error flag
1190 bsf SSP1CON2,ACKDT ; -
1191 bsf SSP1CON2,ACKEN ; - master NOT acknowledge
1192 rcall WaitMSSP ; -
1193 bcf SSP1CON2,ACKDT ; - reset ACKDT flag
1194 ; stop
1195 bsf SSP1CON2,PEN ; stop condition
1183 rcall WaitMSSP 1196 rcall WaitMSSP
1184 WAITMS .1 1197 WAITMS .1
1185 1198 ; address commit
1186 ; read 64 bytes 1199 bsf SSP1CON2,SEN ; start condition
1187 bsf SSP1CON2,SEN ; Start condition 1200 rcall WaitMSSP
1188 rcall WaitMSSP 1201 movlw 0x50 ; address byte + write bit
1189 movlw 0x51 ; Address byte + read bit
1190 movwf SSP1BUF ; control byte 1202 movwf SSP1BUF ; control byte
1191 rcall WaitMSSP 1203 rcall WaitMSSP
1192 rcall I2C_WaitforACK 1204 rcall I2C_WaitforACK
1193 lfsr FSR2,buffer ; send buffer for verify 1205 movlw 0x1F ; write command
1194 movlw .63 ; 63 with ACK + 1 w/o ACK 1206 movwf SSP1BUF ; data byte
1195 movwf i2c_temp2 1207 rcall WaitMSSP
1196 I2C_update_OSTC_loop_read: 1208 rcall I2C_WaitforACK
1197 bsf SSP1CON2, RCEN ; Enable receive mode 1209 bsf SSP1CON2,PEN ; stop condition
1198 rcall WaitMSSP 1210 rcall WaitMSSP
1199 movf SSP1BUF,W 1211 WAITMS .5 ; required waiting time
1200 cpfseq POSTINC2 ; compare read-back with original 1212 ; error check
1201 bsf i2c_error_flag ; Not equal, set flag 1213 btfss i2c_error_flag ; did an error occur?
1202 bcf SSP1CON2,ACKDT 1214 retlw .0 ; NO - data transfered successfully
1203 bsf SSP1CON2,ACKEN ; Master acknowledge 1215 retlw .255 ; YES - error in data transfer occurred
1204 rcall WaitMSSP
1205 decfsz i2c_temp2,F
1206 bra I2C_update_OSTC_loop_read
1207
1208 ; 1 w/o ACK
1209 bsf SSP1CON2, RCEN ; Enable receive mode
1210 rcall WaitMSSP
1211 movf SSP1BUF,W
1212 cpfseq POSTINC2 ; compare read-back with original
1213 bsf i2c_error_flag ; Not equal, set flag
1214 bsf SSP1CON2,ACKDT
1215 bsf SSP1CON2,ACKEN ; Master NOT acknowledge
1216 rcall WaitMSSP
1217 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
1218
1219 bsf SSP1CON2,PEN ; Stop condition
1220 rcall WaitMSSP
1221 WAITMS .1
1222
1223 bsf SSP1CON2,SEN ; Start condition
1224 rcall WaitMSSP
1225 movlw 0x50 ; Address byte + Write bit
1226 movwf SSP1BUF ; control byte
1227 rcall WaitMSSP
1228 rcall I2C_WaitforACK
1229 movlw 0x1F ; Write command!
1230 movwf SSP1BUF ; Data Byte
1231 rcall WaitMSSP
1232 rcall I2C_WaitforACK
1233 bsf SSP1CON2,PEN ; Stop condition
1234 rcall WaitMSSP
1235 WAITMS .5 ; Required waiting time
1236
1237 btfss i2c_error_flag
1238 retlw .0 ; All ok
1239 retlw .255 ; an error occurred
1240 1216
1241 ENDIF 1217 ENDIF
1242 1218
1219 ;=============================================================================
1220
1243 END 1221 END