comparison src/i2c.asm @ 560:b7eb98dbd800

bump to 2.96beta (REFACTORED VERSION)
author heinrichsweikamp
date Wed, 31 Jan 2018 19:39:37 +0100
parents dd28d4efd4d2
children 54346c651b6a
comparison
equal deleted inserted replaced
559:9cb967d844f0 560:b7eb98dbd800
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File i2c.asm 3 ; File i2c.asm
4 ; 4 ;
5 ; I2C Interface to HMC5883L and MMA8452Q 5 ; I2C Interface
6 ; 6 ;
7 ; Compass0
7 ; HMC5883L's read address (8-Bit): 0x3D 8 ; HMC5883L's read address (8-Bit): 0x3D
8 ; HMC5883L's write address (8-Bit): 0x3C 9 ; HMC5883L's write address (8-Bit): 0x3C
9 ;
10 ; MMA8452Q's read address (8-Bit): 0x39 10 ; MMA8452Q's read address (8-Bit): 0x39
11 ; MMA8452Q's write address (8-Bit): 0x38 11 ; MMA8452Q's write address (8-Bit): 0x38
12 ; 12 ;
13 ; Compass1
13 ; LSM303D's read address (8-Bit): 0x3D 14 ; LSM303D's read address (8-Bit): 0x3D
14 ; LSM303D's write address (8-Bit): 0x3C 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 ;
15 ; 27 ;
16 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved. 28 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
17 ;============================================================================= 29 ;=============================================================================
18 ; HISTORY 30 ; HISTORY
19 ; 2012-08-22 : [mH] Creation 31 ; 2012-08-22 : [mH] Creation
20 32
21 33
22 #include "hwos.inc" ; Mandatory header 34 #include "hwos.inc" ; Mandatory header
23 #include "wait.inc" 35 #include "wait.inc"
24 #include "math.inc" 36 #include "math.inc"
37 #include "external_flash.inc"
25 38
26 i2c CODE 39 i2c CODE
27 40
28 WaitMSSP: 41 WaitMSSP:
29 decfsz i2c_temp,F ; check for timeout during I2C action 42 decfsz i2c_temp,F ; check for timeout during I2C action
30 bra WaitMSSP2 43 bra WaitMSSP2
31 bra I2CFail ; timeout occured 44 bra I2CFail ; timeout occured
32 WaitMSSP2: 45 WaitMSSP2:
33 btfss PIR1,SSPIF 46 btfss PIR1,SSP1IF
34 bra WaitMSSP 47 bra WaitMSSP
35 clrf i2c_temp 48 clrf i2c_temp
36 bcf PIR1,SSPIF 49 bcf PIR1,SSP1IF
37 nop
38 return 50 return
39 51
40 I2C_WaitforACK: 52 I2C_WaitforACK:
41 btfss SSPCON2,ACKSTAT ; checks for ACK bit from slave 53 btfss SSP1CON2,ACKSTAT ; checks for ACK bit from slave
42 return 54 return
43 I2CFail: 55 I2CFail:
44 rcall I2CReset ; I2C Reset 56 rcall I2CReset ; I2C Reset
45 bcf PIR1,SSPIF 57 bcf PIR1,SSP1IF
46 clrf i2c_temp 58 clrf i2c_temp
59 bsf i2c_error_flag ; set error flag
47 return 60 return
48 61
49 I2CReset: ; Something went wrong (Slave holds SDA low?) 62 I2CReset: ; Something went wrong (Slave holds SDA low?)
50 clrf SSP1CON1 ; wake-up slave and reset entire module 63 clrf SSP1CON1 ; wake-up slave and reset entire module
51 clrf SSP1CON2 64 clrf SSP1CON2
74 I2CReset_2: 87 I2CReset_2:
75 bsf TRISC,3 ; SCL Input 88 bsf TRISC,3 ; SCL Input
76 clrf SSP1CON1 ; setup I²C Mode 89 clrf SSP1CON1 ; setup I²C Mode
77 WAITMS d'10' ; Reset-Timeout for I2C devices 90 WAITMS d'10' ; Reset-Timeout for I2C devices
78 movlw b'00000000' ; with slew rate control 91 movlw b'00000000' ; with slew rate control
79 movwf SSPSTAT 92 movwf SSP1STAT
80 movlw b'00101000' 93 movlw b'00101000'
81 movwf SSP1CON1 94 movwf SSP1CON1
82 movlw b'00000000' 95 movlw b'00000000'
83 movwf SSP1CON2 96 movwf SSP1CON2
84 movlw 0x27 97 movlw 0x27
119 rrcf lo 132 rrcf lo
120 return 133 return
121 134
122 global I2C_RX_accelerometer 135 global I2C_RX_accelerometer
123 I2C_RX_accelerometer: 136 I2C_RX_accelerometer:
137 btfsc compass_type2 ; compass2
138 bra I2C_RX_accelerometer_compass2 ; yes
124 btfsc compass_type ; compass1? 139 btfsc compass_type ; compass1?
125 bra I2C_RX_accelerometer_compass1 ; yes 140 bra I2C_RX_accelerometer_compass1 ; yes
126 ;I2C_RX_accelerometer_compass0: 141 I2C_RX_accelerometer_compass0:
127 bsf SSP1CON2,SEN ; Start condition 142 bsf SSP1CON2,SEN ; Start condition
128 rcall WaitMSSP 143 rcall WaitMSSP
129 movlw 0x38 ; address 144 movlw 0x38 ; address
130 rcall I2C_TX 145 rcall I2C_TX
131 movlw 0x00 146 movlw 0x00
188 incf hi,F ; YES: do it. 203 incf hi,F ; YES: do it.
189 movff lo,accel_DZ+0 204 movff lo,accel_DZ+0
190 movff hi,accel_DZ+1 ; Copy result 205 movff hi,accel_DZ+1 ; Copy result
191 206
192 bsf SSP1CON2,PEN ; Stop condition 207 bsf SSP1CON2,PEN ; Stop condition
193 rcall WaitMSSP 208 bra WaitMSSP ; (And return)
194 return
195 209
196 I2C_RX_accelerometer_compass1: 210 I2C_RX_accelerometer_compass1:
197 bsf SSP1CON2,SEN ; Start condition 211 bsf SSP1CON2,SEN ; Start condition
198 rcall WaitMSSP 212 rcall WaitMSSP
199 movlw 0x3C ; address 213 movlw 0x3C ; address
201 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 215 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
202 rcall I2C_TX 216 rcall I2C_TX
203 bsf SSP1CON2,RSEN ; Repeated start condition (!) 217 bsf SSP1CON2,RSEN ; Repeated start condition (!)
204 rcall WaitMSSP 218 rcall WaitMSSP
205 movlw 0x3D ; address 219 movlw 0x3D ; address
206 rcall I2C_TX 220 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here...
207 221 rcall I2C_TX
208 ; Non-flipped screen: 222 ; Non-flipped screen:
209 ; Chip orientation on the PCB requires 223 ; Chip orientation on the PCB requires
210 ; Original = Corrected 224 ; Original = Corrected
211 ; x = -x 225 ; x = -x (Compass 1)
226 ; x = x (Compass 2)
212 ; y = -y 227 ; y = -y
213 ; z = -z 228 ; z = -z
214 229
215 ; Flipped screen: 230 ; Flipped screen:
216 ; Chip orientation on the PCB requires 231 ; Chip orientation on the PCB requires
217 ; Original = Corrected 232 ; Original = Corrected
218 ; x = x 233 ; x = x (Compass 1)
234 ; x = -x (Compass 2)
219 ; y = y 235 ; y = y
220 ; z = -z 236 ; z = -z
221 237
222 ; Dump the accelerator data 238 ; Dump the accelerator data
223 rcall I2C_OneByteRX 239 rcall I2C_OneByteRX
224 movff SSP1BUF,lo ;accel_DX+0 240 movff SSP1BUF,lo ;accel_DX+0
225 rcall I2C_OneByteRX 241 rcall I2C_OneByteRX
226 movff SSP1BUF,hi ;accel_DX+1 242 movff SSP1BUF,hi ;accel_DX+1
227 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only 243 rcall I2C_TwoBytesRX_div16_2; devide lo:hi/16 (signed) only
244 btfss compass_type2 ; compass 2?
245 bra I2C_RX_accelerometer1_c1 ; No, compass 1
246 ; compass 2
247 btfss flip_screen ; 180° rotation ?
248 bra I2C_RX_accelerometer2_c1 ; No, continue with normal compass1 routines for Y and Z
249 ; flipped compass 2, negate x
250 comf hi ; 16bit sign change.
251 negf lo
252 btfsc STATUS,C ; Carry to propagate ?
253 incf hi,F ; YES: do it.
254 bra I2C_RX_accelerometer2_c1 ; continue with normal compass1 routines for Y and Z
255
256 I2C_RX_accelerometer1_c1:
228 btfsc flip_screen ; 180° rotation ? 257 btfsc flip_screen ; 180° rotation ?
229 bra I2C_RX_accelerometer2_c1 ; Yes 258 bra I2C_RX_accelerometer2_c1 ; Yes
259 ; non-flipped compass 1, negate x
230 comf hi ; 16bit sign change. 260 comf hi ; 16bit sign change.
231 negf lo 261 negf lo
232 btfsc STATUS,C ; Carry to propagate ? 262 btfsc STATUS,C ; Carry to propagate ?
233 incf hi,F ; YES: do it. 263 incf hi,F ; YES: do it.
234 I2C_RX_accelerometer2_c1: 264 I2C_RX_accelerometer2_c1:
265 ; flipped compass 1, non-flipped compass 2
235 movff lo,accel_DX+0 266 movff lo,accel_DX+0
236 movff hi,accel_DX+1 ; Copy result 267 movff hi,accel_DX+1 ; Copy result
237
238 rcall I2C_OneByteRX 268 rcall I2C_OneByteRX
239 movff SSP1BUF,lo ;accel_DY+0 269 movff SSP1BUF,lo ;accel_DY+0
240 rcall I2C_OneByteRX 270 rcall I2C_OneByteRX
241 movff SSP1BUF,hi ;accel_DY+1 271 movff SSP1BUF,hi ;accel_DY+1
242 272
266 incf hi,F ; YES: do it. 296 incf hi,F ; YES: do it.
267 movff lo,accel_DZ+0 297 movff lo,accel_DZ+0
268 movff hi,accel_DZ+1 ; Copy result 298 movff hi,accel_DZ+1 ; Copy result
269 return 299 return
270 300
301 I2C_RX_accelerometer_compass2:
302 bsf SSP1CON2,SEN ; Start condition
303 rcall WaitMSSP
304 movlw 0x32 ; address
305 rcall I2C_TX
306 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
307 rcall I2C_TX
308 bsf SSP1CON2,RSEN ; Repeated start condition (!)
309 rcall WaitMSSP
310 movlw 0x33 ; address
311 bra I2C_RX_accelerometer_compass1_xx
312
271 I2C_OneByteRX: 313 I2C_OneByteRX:
272 bsf SSP1CON2, RCEN ; Enable recieve mode 314 bsf SSP1CON2, RCEN ; Enable recieve mode
273 rcall WaitMSSP 315 rcall WaitMSSP
274 bsf SSP1CON2,ACKEN ; Master acknowlegde 316 bsf SSP1CON2,ACKEN ; Master acknowlegde
275 bra WaitMSSP ; And return! 317 bra WaitMSSP ; And return!
276 318
277 global I2C_RX_compass 319 global I2C_RX_compass
278 I2C_RX_compass: 320 I2C_RX_compass:
321 btfsc compass_type2 ; compass2
322 bra I2C_RX_compass2 ; yes
279 btfsc compass_type ; compass1? 323 btfsc compass_type ; compass1?
280 bra I2C_RX_compass1 ; yes 324 bra I2C_RX_compass1 ; yes
281 ;I2C_RX_compass0: 325 I2C_RX_compass0:
282 bsf SSP1CON2,SEN ; Start condition 326 bsf SSP1CON2,SEN ; Start condition
283 rcall WaitMSSP 327 rcall WaitMSSP
284 movlw 0x3C ; address 328 movlw 0x3C ; address
285 rcall I2C_TX 329 rcall I2C_TX
286 movlw 0x03 330 movlw 0x03
287 rcall I2C_TX 331 rcall I2C_TX
288 bsf SSP1CON2,PEN ; Stop condition 332 bsf SSP1CON2,PEN ; Stop condition
289 rcall WaitMSSP 333 rcall WaitMSSP
290 334
291 bcf PIR1,SSPIF 335 bcf PIR1,SSP1IF
292 bsf SSP1CON2,SEN ; Start condition 336 bsf SSP1CON2,SEN ; Start condition
293 rcall WaitMSSP 337 rcall WaitMSSP
294 movlw 0x3D ; address 338 movlw 0x3D ; address
295 rcall I2C_TX 339 rcall I2C_TX
296 340
315 ; x = y 359 ; x = y
316 ; z = z 360 ; z = z
317 ; y = -x 361 ; y = -x
318 362
319 rcall I2C_OneByteRX ; Get one byte 363 rcall I2C_OneByteRX ; Get one byte
320 movff SSP1BUF,compass_DY+1; Data Byte 364 movff SSP1BUF,compass_DY+1; Data Byte
321 rcall I2C_OneByteRX ; Get one byte 365 rcall I2C_OneByteRX ; Get one byte
322 movff SSP1BUF,compass_DY+0; Data Byte 366 movff SSP1BUF,compass_DY+0; Data Byte
323 btfsc flip_screen ; 180° rotation ? 367 btfsc flip_screen ; 180° rotation ?
324 bra I2C_RX_compass2 ; Yes 368 bra I2C_RX_compass0_2 ; Yes
325 banksel compass_DY 369 banksel compass_DY
326 comf compass_DY+1 ; 16bit sign change. 370 comf compass_DY+1 ; 16bit sign change.
327 negf compass_DY+0 371 negf compass_DY+0
328 btfsc STATUS,C ; Carry to propagate ? 372 btfsc STATUS,C ; Carry to propagate ?
329 incf compass_DY+1,F ; YES: do it. 373 incf compass_DY+1,F ; YES: do it.
330 I2C_RX_compass2: 374 I2C_RX_compass0_2:
331 banksel common 375 banksel common
332 rcall I2C_OneByteRX ; Get one byte 376 rcall I2C_OneByteRX ; Get one byte
333 movff SSP1BUF,compass_DZ+1; Data Byte 377 movff SSP1BUF,compass_DZ+1; Data Byte
334 rcall I2C_OneByteRX ; Get one byte 378 rcall I2C_OneByteRX ; Get one byte
335 movff SSP1BUF,compass_DZ+0; Data Byte 379 movff SSP1BUF,compass_DZ+0; Data Byte
360 rcall I2C_TX 404 rcall I2C_TX
361 bsf SSP1CON2,RSEN ; Repeated start condition (!) 405 bsf SSP1CON2,RSEN ; Repeated start condition (!)
362 rcall WaitMSSP 406 rcall WaitMSSP
363 movlw 0x3D ; address 407 movlw 0x3D ; address
364 rcall I2C_TX 408 rcall I2C_TX
365 409 ;rcall WaitMSSP ; Needed? (mH)
366 rcall I2C_OneByteRX ; Get one byte 410 rcall I2C_OneByteRX ; Get one byte
367 movff SSP1BUF,lo ; Data Byte 411 movff SSP1BUF,lo ; Data Byte
368 rcall I2C_OneByteRX ; Get one byte 412 rcall I2C_OneByteRX ; Get one byte
369 movff SSP1BUF,hi ; Data Byte 413 movff SSP1BUF,hi ; Data Byte
370 rcall I2C_TwoBytesRX_div8_2 414 rcall I2C_TwoBytesRX_div8_2
404 movff SSP1BUF,hi ; Data Byte 448 movff SSP1BUF,hi ; Data Byte
405 rcall I2C_TwoBytesRX_div8_2 449 rcall I2C_TwoBytesRX_div8_2
406 movff lo,compass_DZ+0 450 movff lo,compass_DZ+0
407 movff hi,compass_DZ+1 451 movff hi,compass_DZ+1
408 bsf SSP1CON2,PEN ; Stop condition 452 bsf SSP1CON2,PEN ; Stop condition
409 rcall WaitMSSP 453 bra WaitMSSP ;(And return)
410 return 454
411 455 I2C_RX_compass2: ; newest compass
456 bsf SSP1CON2,SEN ; Start condition
457 rcall WaitMSSP
458 movlw 0x3C ; address
459 rcall I2C_TX
460 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
461 rcall I2C_TX
462 bsf SSP1CON2,RSEN ; Repeated start condition (!)
463 rcall WaitMSSP
464 movlw 0x3D ; address
465 rcall I2C_TX
466 ; rcall WaitMSSP
467 rcall I2C_OneByteRX ; Get one byte
468 movff SSP1BUF,lo ; Data Byte
469 rcall I2C_OneByteRX ; Get one byte
470 movff SSP1BUF,hi ; Data Byte
471 ; rcall I2C_TwoBytesRX_div8_2
472 btfsc flip_screen ; 180° rotation ?
473 bra I2C_RX_compass2_1 ; Yes, do nothing with X
474 ; No, flip X
475 comf hi ; 16bit sign change.
476 negf lo
477 btfsc STATUS,C ; Carry to propagate ?
478 incf hi,F ; YES: do it.
479 I2C_RX_compass2_1:
480 movff lo,compass_DX+0
481 movff hi,compass_DX+1
482 rcall I2C_OneByteRX ; Get one byte
483 movff SSP1BUF,lo ; Data Byte
484 rcall I2C_OneByteRX ; Get one byte
485 movff SSP1BUF,hi ; Data Byte
486 ; rcall I2C_TwoBytesRX_div8_2
487 btfss flip_screen ; 180° rotation ?
488 bra I2C_RX_compass2_2 ; No, do nothing with Y
489 ; Yes, flip Y
490 comf hi ; 16bit sign change.
491 negf lo
492 btfsc STATUS,C ; Carry to propagate ?
493 incf hi,F ; YES: do it.
494 I2C_RX_compass2_2:
495 movff lo,compass_DY+0
496 movff hi,compass_DY+1
497 rcall I2C_OneByteRX ; Get one byte
498 movff SSP1BUF,lo ; Data Byte
499 rcall I2C_OneByteRX ; Get one byte
500 movff SSP1BUF,hi ; Data Byte
501 ;rcall I2C_TwoBytesRX_div8_2
502 movff lo,compass_DZ+0
503 movff hi,compass_DZ+1
504 bsf SSP1CON2,PEN ; Stop condition
505 bra WaitMSSP ;(And return)
506
507
412 global I2C_init_compass 508 global I2C_init_compass
413 I2C_init_compass: 509 I2C_init_compass:
414 bsf compass_enabled 510 bsf compass_enabled
511 bcf compass_type2
512 ; probe compass 2
513 bsf SSP1CON2,SEN ; Start condition
514 rcall WaitMSSP
515 movlw 0x32 ; Address byte + Write bit
516 movwf SSP1BUF ; control byte
517 rcall WaitMSSP
518 btfss SSP1CON2,ACKSTAT ; ACK?
519 bsf compass_type2 ; ACK send. compass2 present
520 bsf SSP1CON2,PEN ; Stop condition
521 rcall WaitMSSP
522
523 btfsc compass_type2
524 bra I2C_init_compass2 ; Compass2
525 ; Check for compass0 or compass1...
415 bsf compass_type ; set flag 526 bsf compass_type ; set flag
416 bsf SSP1CON2,SEN ; Start condition 527 bsf SSP1CON2,SEN ; Start condition
417 rcall WaitMSSP 528 rcall WaitMSSP
418 movlw 0x3C ; address 529 movlw 0x3C ; address
419 rcall I2C_TX 530 rcall I2C_TX
420 movlw 0x0F 531 movlw 0x0F
421 rcall I2C_TX 532 rcall I2C_TX
422 bsf SSP1CON2,PEN ; Stop condition 533 bsf SSP1CON2,PEN ; Stop condition
423 rcall WaitMSSP 534 rcall WaitMSSP
424 bcf PIR1,SSPIF 535 bcf PIR1,SSP1IF
425 bsf SSP1CON2,SEN ; Start condition 536 bsf SSP1CON2,SEN ; Start condition
426 rcall WaitMSSP 537 rcall WaitMSSP
427 movlw 0x3D ; address 538 movlw 0x3D ; address
428 rcall I2C_TX 539 rcall I2C_TX
429 rcall I2C_OneByteRX ; Get one byte 540 rcall I2C_OneByteRX ; Get one byte
443 movlw 0x00 554 movlw 0x00
444 rcall I2C_TX 555 rcall I2C_TX
445 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias) 556 ; movlw b'01101001' ; ConfigA: 3Hz, 8 Samples averaged, Test Mode (Positive Bias)
446 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged 557 movlw b'01101000' ; ConfigA: 3Hz, 8 Samples averaged
447 rcall I2C_TX 558 rcall I2C_TX
448 bra I2C_init_compass_common
449
450 global I2C_init_compass_fast
451 I2C_init_compass_fast:
452 btfsc compass_type ; compass1?
453 bra I2C_init_compass_fast1 ; yes
454 ;I2C_init_compass_fast0:
455 bsf SSP1CON2,SEN ; Start condition
456 rcall WaitMSSP
457 movlw 0x3C ; address
458 rcall I2C_TX
459 movlw 0x00
460 rcall I2C_TX
461 movlw b'00111000' ; ConfigA: 75Hz, 2 Samples averaged
462 ; movlw b'00111001' ; ConfigA: 75Hz, 2 Samples averaged, Test Mode (Positive Bias)
463 rcall I2C_TX
464 I2C_init_compass_common: 559 I2C_init_compass_common:
465 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gauss) 560 movff opt_compass_gain,i2c_temp ; 0-7 (230LSB/Gauss to 1370LSB/Gauss)
466 swapf i2c_temp,F 561 swapf i2c_temp,F
467 comf i2c_temp,F 562 comf i2c_temp,F
468 bcf STATUS,C 563 bcf STATUS,C
471 clrf i2c_temp 566 clrf i2c_temp
472 rcall I2C_TX 567 rcall I2C_TX
473 movlw b'00000000' ; Continous Mode 568 movlw b'00000000' ; Continous Mode
474 rcall I2C_TX 569 rcall I2C_TX
475 bsf SSP1CON2,PEN ; Stop condition 570 bsf SSP1CON2,PEN ; Stop condition
476 rcall WaitMSSP 571 bra WaitMSSP ; (And return)
477 return
478 572
479 I2C_init_compass1: 573 I2C_init_compass1:
480 bsf SSP1CON2,SEN ; Start condition 574 bsf SSP1CON2,SEN ; Start condition
481 rcall WaitMSSP 575 rcall WaitMSSP
482 movlw 0x3C ; address 576 movlw 0x3C ; address
501 ;movlw b'00100000' ; CTRL6 (+/-4 Gauss) 595 ;movlw b'00100000' ; CTRL6 (+/-4 Gauss)
502 rcall I2C_TX 596 rcall I2C_TX
503 movlw b'00000000' ; CTRL7 Continuous Mode 597 movlw b'00000000' ; CTRL7 Continuous Mode
504 rcall I2C_TX 598 rcall I2C_TX
505 bsf SSP1CON2,PEN ; Stop condition 599 bsf SSP1CON2,PEN ; Stop condition
506 rcall WaitMSSP 600 bra WaitMSSP ; (And return)
507 return 601
508 602 I2C_init_compass2:
509 I2C_init_compass_fast1: 603 bsf SSP1CON2,SEN ; Start condition
510 bsf SSP1CON2,SEN ; Start condition
511 rcall WaitMSSP 604 rcall WaitMSSP
512 movlw 0x3C ; address 605 movlw 0x3C ; address
513 rcall I2C_TX 606 rcall I2C_TX
514 movlw 0x9F ; 1F with auto-increment (MSB=1) 607 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
515 rcall I2C_TX 608 rcall I2C_TX
516 movlw b'00000000' ; CTRL0 609 movlw b'00000000' ; CFG_REG_A_M (10Hz, Continuous)
517 rcall I2C_TX 610 rcall I2C_TX
518 movlw b'01101111' ; CTRL1 (100Hz, BDU=0, x,y,z = ON) 611 movlw b'00000000' ; CFG_REG_B_M (Low-Pass Filter off)
519 rcall I2C_TX 612 rcall I2C_TX
520 movlw b'11000000' ; CTRL2 (50Hz, +/-2g, 613 movlw b'00000000' ; CFG_REG_C_M BDU=0
521 rcall I2C_TX 614 rcall I2C_TX
522 movlw b'00000000' ; CTRL3 615 bsf SSP1CON2,PEN ; Stop condition
523 rcall I2C_TX 616 bra WaitMSSP ;(And return)
524 movlw b'00000000' ; CTRL4
525 rcall I2C_TX
526 movlw b'01110100' ; CTRL5 HIGH res, 100Hz
527 rcall I2C_TX
528 bra init_compass1_common
529 617
530 global I2C_sleep_compass 618 global I2C_sleep_compass
531 I2C_sleep_compass: 619 I2C_sleep_compass:
532 bcf compass_enabled 620 bcf compass_enabled
533 621 btfsc compass_type2 ; compass2?
534 btfsc compass_type ; compass1? 622 bra I2C_sleep_compass2 ; yes
535 bra I2C_sleep_compass1 ; yes 623 btfsc compass_type ; compass1?
536 ;I2C_sleep_compass0: 624 bra I2C_sleep_compass1 ; yes
625 I2C_sleep_compass0:
537 bsf SSP1CON2,SEN ; Start condition 626 bsf SSP1CON2,SEN ; Start condition
538 rcall WaitMSSP 627 rcall WaitMSSP
539 movlw 0x3C ; address 628 movlw 0x3C ; address
540 rcall I2C_TX 629 rcall I2C_TX
541 movlw 0x00 630 movlw 0x00
545 movlw b'00100000' ; ConfigB 634 movlw b'00100000' ; ConfigB
546 rcall I2C_TX 635 rcall I2C_TX
547 movlw b'00000010' ; Idle Mode 636 movlw b'00000010' ; Idle Mode
548 rcall I2C_TX 637 rcall I2C_TX
549 bsf SSP1CON2,PEN ; Stop condition 638 bsf SSP1CON2,PEN ; Stop condition
550 rcall WaitMSSP 639 bra WaitMSSP ; (And return)
551 return
552 640
553 I2C_sleep_compass1: 641 I2C_sleep_compass1:
554 bsf SSP1CON2,SEN ; Start condition 642 bsf SSP1CON2,SEN ; Start condition
555 rcall WaitMSSP 643 rcall WaitMSSP
556 movlw 0x3C ; address 644 movlw 0x3C ; address
568 movlw 0x26 ; CTRL_REG7 656 movlw 0x26 ; CTRL_REG7
569 rcall I2C_TX 657 rcall I2C_TX
570 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode 658 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor Power-down mode
571 rcall I2C_TX 659 rcall I2C_TX
572 bsf SSP1CON2,PEN ; Stop condition 660 bsf SSP1CON2,PEN ; Stop condition
573 rcall WaitMSSP 661 bra WaitMSSP ;(And return)
574 return 662
575 663
664 I2C_sleep_compass2:
665 ; magnetic
666 bsf SSP1CON2,SEN ; Start condition
667 rcall WaitMSSP
668 movlw 0x3C ; address
669 rcall I2C_TX
670 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
671 rcall I2C_TX
672 movlw b'00000010' ; CFG_REG_A_M (Idle mode)
673 rcall I2C_TX
674 bsf SSP1CON2,PEN ; Stop condition
675 bra WaitMSSP ; (And return)
676
677 I2C_sleep_accelerometer2:
678 ; accelerometer
679 bsf SSP1CON2,SEN ; Start condition
680 rcall WaitMSSP
681 movlw 0x32 ; address
682 rcall I2C_TX
683 movlw 0x9F ; 1F with auto-increment (MSB=1)
684 rcall I2C_TX
685 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
686 rcall I2C_TX
687 movlw b'00000000' ; CTRL_REG1_A (All off)
688 rcall I2C_TX
689 bsf SSP1CON2,PEN ; Stop condition
690 bra WaitMSSP ; (And return)
576 691
577 global I2C_init_accelerometer 692 global I2C_init_accelerometer
578 I2C_init_accelerometer: 693 I2C_init_accelerometer:
694 btfsc compass_type2 ; compass2?
695 bra I2C_init_accelerometer2 ; Yes.
696
579 btfsc compass_type ; compass1? 697 btfsc compass_type ; compass1?
580 return ; yes, ignore 698 return ; yes, ignore
581 699
582 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode 700 rcall I2C_sleep_accelerometer ; Regs can only be changed in St.By mode
701
702 bsf SSP1CON2,SEN ; Start condition
703 rcall WaitMSSP
704 movlw 0x38 ; address
705 rcall I2C_TX
706 movlw 0x0E ; XYZ_DATA_CFG
707 rcall I2C_TX
708 movlw b'00000000' ; High pass Filter=0 , +/- 2g range
709 rcall I2C_TX
710 bsf SSP1CON2,PEN ; Stop condition
711 rcall WaitMSSP
712
713
714 bsf SSP1CON2,SEN ; Start condition
715 rcall WaitMSSP
716 movlw 0x38 ; address
717 rcall I2C_TX
718 movlw 0x2A ; CTRL_REG1
719 rcall I2C_TX
720 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
721 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
722 rcall I2C_TX
723 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
724 rcall I2C_TX
725 bsf SSP1CON2,PEN ; Stop condition
726 rcall WaitMSSP
727
728 bsf SSP1CON2,SEN ; Start condition
729 rcall WaitMSSP
730 movlw 0x38 ; address
731 rcall I2C_TX
732 movlw 0x2A ; CTRL_REG1
733 rcall I2C_TX
734 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
735 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
736 rcall I2C_TX
737 bsf SSP1CON2,PEN ; Stop condition
738 bra WaitMSSP ; (And return)
739
740 I2C_init_accelerometer2:
741 bsf SSP1CON2,SEN ; Start condition
742 rcall WaitMSSP
743 movlw 0x32 ; address
744 rcall I2C_TX
745 movlw 0x9F ; 1F with auto-increment (MSB=1)
746 rcall I2C_TX
747 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
748 rcall I2C_TX
749 movlw b'01010111' ; CTRL_REG1_A (100Hz, x,y,z = ON)
750 rcall I2C_TX
751 movlw b'00000000' ; CTRL_REG2_A
752 rcall I2C_TX
753 movlw b'00000000' ; CTRL_REG3_A
754 rcall I2C_TX
755 movlw b'00000000' ; CTRL_REG4_A (BDU=0, +/-2g,
756 rcall I2C_TX
757 movlw b'00000000' ; CTRL_REG5_A
758 rcall I2C_TX
759 bsf SSP1CON2,PEN ; Stop condition
760 bra WaitMSSP ; (And return)
761
762 global I2C_sleep_accelerometer
763 I2C_sleep_accelerometer:
764 btfsc compass_type2 ; Compass2
765 bra I2C_sleep_accelerometer2 ; Yes
766 btfsc compass_type ; compass1?
767 return ; yes, ignore
583 768
584 bsf SSP1CON2,SEN ; Start condition 769 bsf SSP1CON2,SEN ; Start condition
585 rcall WaitMSSP 770 rcall WaitMSSP
586 movlw 0x38 ; address 771 movlw 0x38 ; address
587 rcall I2C_TX 772 rcall I2C_TX
588 movlw 0x0E ; XYZ_DATA_CFG 773 movlw 0x2A ; CTRL_REG1
589 rcall I2C_TX 774 rcall I2C_TX
590 movlw b'00000000' ; High pass Filter=0 , +/- 2g range 775 movlw b'00000000' ; St. By Mode
591 rcall I2C_TX 776 rcall I2C_TX
592 bsf SSP1CON2,PEN ; Stop condition 777 bsf SSP1CON2,PEN ; Stop condition
593 rcall WaitMSSP 778 bra WaitMSSP ; (And return)
594 779
595
596 bsf SSP1CON2,SEN ; Start condition
597 rcall WaitMSSP
598 movlw 0x38 ; address
599 rcall I2C_TX
600 movlw 0x2A ; CTRL_REG1
601 rcall I2C_TX
602 ; movlw b'00110000' ; CTRL_REG1: 160ms data rate, St.By Mode
603 movlw b'00110100' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode
604 rcall I2C_TX
605 movlw b'00000010' ; CTRL_REG2: High Res in Active mode
606 rcall I2C_TX
607 bsf SSP1CON2,PEN ; Stop condition
608 rcall WaitMSSP
609
610 bsf SSP1CON2,SEN ; Start condition
611 rcall WaitMSSP
612 movlw 0x38 ; address
613 rcall I2C_TX
614 movlw 0x2A ; CTRL_REG1
615 rcall I2C_TX
616 ; movlw b'00110001' ; CTRL_REG1: 160ms data rate, Active Mode
617 movlw b'00110101' ; CTRL_REG1: 160ms data rate, St.By Mode, reduced noise mode, Active Mode
618 rcall I2C_TX
619 bsf SSP1CON2,PEN ; Stop condition
620 rcall WaitMSSP
621
622 return
623
624 global I2C_sleep_accelerometer
625 I2C_sleep_accelerometer:
626 btfsc compass_type ; compass1?
627 return ; yes, ignore
628
629 bsf SSP1CON2,SEN ; Start condition
630 rcall WaitMSSP
631 movlw 0x38 ; address
632 rcall I2C_TX
633 movlw 0x2A ; CTRL_REG1
634 rcall I2C_TX
635 movlw b'00000000' ; St. By Mode
636 rcall I2C_TX
637 bsf SSP1CON2,PEN ; Stop condition
638 rcall WaitMSSP
639 return
640
641 lt2942_init_again: 780 lt2942_init_again:
642 clrf i2c_temp 781 clrf i2c_temp
643 movlw 0x02 ; Point to accumulated charge registers 782 movlw 0x02 ; Point to accumulated charge registers
644 rcall I2C_TX_GAUGE 783 rcall I2C_TX_GAUGE
645 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte 784 movff battery_acumulated_charge+1,SSP1BUF ; Data Byte
647 rcall I2C_WaitforACK 786 rcall I2C_WaitforACK
648 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte 787 movff battery_acumulated_charge+0,SSP1BUF ; Data Byte
649 rcall WaitMSSP 788 rcall WaitMSSP
650 rcall I2C_WaitforACK 789 rcall I2C_WaitforACK
651 bsf SSP1CON2,PEN ; Stop condition 790 bsf SSP1CON2,PEN ; Stop condition
652 rcall WaitMSSP; (and return) 791 rcall WaitMSSP
653 movff battery_acumulated_charge+1,sub_a+1 792 movff battery_acumulated_charge+1,sub_a+1
654 movff battery_acumulated_charge+0,sub_a+0 793 movff battery_acumulated_charge+0,sub_a+0
655 ; and init again... 794 ; and init again...
656 global lt2942_init 795
796 global lt2942_init
657 lt2942_init: ; Setup Control register B 797 lt2942_init: ; Setup Control register B
658 clrf i2c_temp 798 clrf i2c_temp
659 movlw 0x01 ; Point to control reg B 799 movlw 0x01 ; Point to control reg B
660 rcall I2C_TX_GAUGE 800 rcall I2C_TX_GAUGE
661 movlw b'11111000' ; Automatic conversion every two seconds 801 movlw b'11111000' ; Automatic conversion every two seconds
662 movff WREG, SSP1BUF ; Data Byte 802 movff WREG, SSP1BUF ; Data Byte
663 rcall WaitMSSP 803 rcall WaitMSSP
664 rcall I2C_WaitforACK 804 rcall I2C_WaitforACK
665 bsf SSP1CON2,PEN ; Stop condition 805 bsf SSP1CON2,PEN ; Stop condition
666 bra WaitMSSP ; (And return) 806 bra WaitMSSP ; (And return)
667 807
668 global lt2942_get_status 808 global lt2942_get_status
669 lt2942_get_status: ; Read status register 809 lt2942_get_status: ; Read status register
670 bcf battery_gauge_available ; Clear flag 810 bcf battery_gauge_available ; Clear flag
719 movff xC+0,batt_voltage+0 859 movff xC+0,batt_voltage+0
720 860
721 tstfsz batt_voltage+1 ; <256mV? 861 tstfsz batt_voltage+1 ; <256mV?
722 return ; No, done. 862 return ; No, done.
723 863
724 rcall lt2942_init 864 bra lt2942_init ;(and return)
725 return
726 865
727 ; global lt2942_get_temperature 866 ; global lt2942_get_temperature
728 ;lt2942_get_temperature: ; Read temperature registers 867 ;lt2942_get_temperature: ; Read temperature registers
729 ; clrf i2c_temp 868 ; clrf i2c_temp
730 ; movlw 0x0C ; Point to temperature registers 869 ; movlw 0x0C ; Point to temperature registers
872 movwf SSP1BUF ; control byte 1011 movwf SSP1BUF ; control byte
873 rcall WaitMSSP 1012 rcall WaitMSSP
874 rcall I2C_WaitforACK 1013 rcall I2C_WaitforACK
875 bsf SSP1CON2, RCEN ; Enable recieve mode 1014 bsf SSP1CON2, RCEN ; Enable recieve mode
876 bra WaitMSSP; (and return) 1015 bra WaitMSSP; (and return)
877 1016
1017
1018 global I2C_probe_OSTC_rx
1019 I2C_probe_OSTC_rx:
1020 bsf SSP1CON2,SEN ; Start condition
1021 rcall WaitMSSP
1022 movlw 0x50 ; Address byte + Write bit
1023 movwf SSP1BUF ; control byte
1024 rcall WaitMSSP
1025 btfss SSP1CON2,ACKSTAT ; ACK?
1026 bsf ostc_rx_present ; ACK send. OSTC_RX present!
1027 bsf SSP1CON2,PEN ; Stop condition
1028 rcall WaitMSSP
1029 btfss ostc_rx_present ; Do we have the RX?
1030 return ; No, Done.
1031 WAITMS .1
1032 bsf SSP1CON2,SEN ; Start condition
1033 rcall WaitMSSP
1034 movlw 0x50 ; Address byte + Write bit
1035 movwf SSP1BUF ; control byte
1036 rcall WaitMSSP
1037 rcall I2C_WaitforACK
1038 movlw 0x1B
1039 movwf SSP1BUF ; Data Byte (Get firmware)
1040 rcall WaitMSSP
1041 rcall I2C_WaitforACK
1042 bsf SSP1CON2,PEN ; Stop condition
1043 rcall WaitMSSP
1044 WAITMS .1
1045 bsf SSP1CON2,SEN ; Start condition
1046 rcall WaitMSSP
1047 movlw 0x51 ; Address byte + Read bit
1048 movwf SSP1BUF ; control byte
1049 rcall WaitMSSP
1050 bsf SSP1CON2, RCEN ; Enable recieve mode
1051 rcall WaitMSSP
1052 movff SSP1BUF,rx_firmware+0
1053 bsf SSP1CON2,ACKEN ; Master acknowlegde
1054 rcall WaitMSSP
1055
1056 ; last byte in read from RX circuity always with a NACK!
1057 bsf SSP1CON2, RCEN ; Enable recieve mode
1058 rcall WaitMSSP
1059 movff SSP1BUF,rx_firmware+1
1060 bsf SSP1CON2,ACKDT
1061 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
1062 rcall WaitMSSP
1063 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
1064 bsf SSP1CON2,PEN ; Stop condition
1065 bra WaitMSSP ;(and return)
1066
1067
1068 global I2C_get_tankdata
1069 I2C_get_tankdata:
1070 ;
1071 ; movlw .1
1072 ; movff WREG,rx_buffer+0
1073 ; movlw .125
1074 ; movff WREG,rx_buffer+1
1075 ; movlw HIGH .1500
1076 ; movff WREG,rx_buffer+2
1077 ; movlw LOW .1500 ; 150,0bar
1078 ; movff WREG,rx_buffer+3
1079 ;
1080 ; movlw .1
1081 ; movff WREG,rx_buffer+6
1082 ; movlw .140
1083 ; movff WREG,rx_buffer+7
1084 ; movlw HIGH .2251
1085 ; movff WREG,rx_buffer+8
1086 ; movlw LOW .2251 ; 225,1bar
1087 ; movff WREG,rx_buffer+9
1088 ;
1089 ;
1090 ;
1091 ; return ;mH
1092
1093
1094 bsf SSP1CON2,SEN ; Start condition
1095 rcall WaitMSSP
1096 movlw 0x50 ; Address byte + Write bit
1097 movwf SSP1BUF ; control byte
1098 rcall WaitMSSP
1099 rcall I2C_WaitforACK
1100 movlw 0x1E ; Read buffer2 (48 Bytes)
1101 movwf SSP1BUF ; Data Byte
1102 rcall WaitMSSP
1103 rcall I2C_WaitforACK
1104 bsf SSP1CON2,PEN ; Stop condition
1105 rcall WaitMSSP
1106 WAITMS .1
1107
1108 ; read 48 bytes
1109 bsf SSP1CON2,SEN ; Start condition
1110 rcall WaitMSSP
1111 movlw 0x51 ; Address byte + read bit
1112 movwf SSP1BUF ; control byte
1113 rcall WaitMSSP
1114 rcall I2C_WaitforACK
1115 movlw .47 ; 47 with ACK + 1 w/o ACK
1116 movwf temp1
1117 lfsr FSR2,rx_buffer+0
1118 I2C_get_tankdata_loop_read:
1119 bsf SSP1CON2, RCEN ; Enable recieve mode
1120 rcall WaitMSSP
1121 movff SSP1BUF,POSTINC2
1122 bcf SSP1CON2,ACKDT
1123 bsf SSP1CON2,ACKEN ; Master acknowlegde
1124 rcall WaitMSSP
1125 decfsz temp1,F
1126 bra I2C_get_tankdata_loop_read
1127
1128 ; 1 w/o ACK
1129 bsf SSP1CON2, RCEN ; Enable recieve mode
1130 rcall WaitMSSP
1131 movff SSP1BUF,POSTINC2
1132 bsf SSP1CON2,ACKDT
1133 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
1134 rcall WaitMSSP
1135 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
1136
1137 bsf SSP1CON2,PEN ; Stop condition
1138 bra WaitMSSP ;(and return)
1139
1140
1141 global I2C_update_OSTC_rx
1142 I2C_update_OSTC_rx: ; 992*64byte master loop
1143 bcf i2c_error_flag ; clear error flag
1144 ; write 64 bytes
1145 bsf SSP1CON2,SEN ; Start condition
1146 rcall WaitMSSP
1147 movlw 0x50 ; Address byte + Write bit
1148 movwf SSP1BUF ; control byte
1149 rcall WaitMSSP
1150 rcall I2C_WaitforACK
1151 lfsr FSR2,buffer ; send buffer for verify
1152 movlw .64
1153 movwf temp1
1154 I2C_update_OSTC_loop: ; 64byte flash page loop
1155 movff up,POSTINC2 ; store for verify
1156 movff up,SSP1BUF
1157 rcall WaitMSSP
1158 rcall I2C_WaitforACK
1159 call ext_flash_read_block ; Read one byte
1160 movwf up ; prepare for transmit
1161 decfsz temp1,F
1162 bra I2C_update_OSTC_loop
1163 bsf SSP1CON2,PEN ; Stop condition
1164 rcall WaitMSSP
1165 WAITMS .1
1166
1167 ; read 64 bytes
1168 bsf SSP1CON2,SEN ; Start condition
1169 rcall WaitMSSP
1170 movlw 0x51 ; Address byte + read bit
1171 movwf SSP1BUF ; control byte
1172 rcall WaitMSSP
1173 rcall I2C_WaitforACK
1174 lfsr FSR2,buffer ; send buffer for verify
1175 movlw .63 ; 63 with ACK + 1 w/o ACK
1176 movwf temp1
1177 I2C_update_OSTC_loop_read:
1178 bsf SSP1CON2, RCEN ; Enable recieve mode
1179 rcall WaitMSSP
1180 movf SSP1BUF,W
1181 cpfseq POSTINC2 ; compare readback with original
1182 bsf i2c_error_flag ; Not equal, set flag
1183 bcf SSP1CON2,ACKDT
1184 bsf SSP1CON2,ACKEN ; Master acknowlegde
1185 rcall WaitMSSP
1186 decfsz temp1,F
1187 bra I2C_update_OSTC_loop_read
1188
1189 ; 1 w/o ACK
1190 bsf SSP1CON2, RCEN ; Enable recieve mode
1191 rcall WaitMSSP
1192 movf SSP1BUF,W
1193 cpfseq POSTINC2 ; compare readback with original
1194 bsf i2c_error_flag ; Not equal, set flag
1195 bsf SSP1CON2,ACKDT
1196 bsf SSP1CON2,ACKEN ; Master NOT acknowlegde
1197 rcall WaitMSSP
1198 bcf SSP1CON2,ACKDT ; Reset ACKDT flag
1199
1200 bsf SSP1CON2,PEN ; Stop condition
1201 rcall WaitMSSP
1202 WAITMS .1
1203
1204 bsf SSP1CON2,SEN ; Start condition
1205 rcall WaitMSSP
1206 movlw 0x50 ; Address byte + Write bit
1207 movwf SSP1BUF ; control byte
1208 rcall WaitMSSP
1209 rcall I2C_WaitforACK
1210 movlw 0x1F ; Write command!
1211 movwf SSP1BUF ; Data Byte
1212 rcall WaitMSSP
1213 rcall I2C_WaitforACK
1214 bsf SSP1CON2,PEN ; Stop condition
1215 rcall WaitMSSP
1216 WAITMS .5 ; Required waiting time
1217
1218 btfss i2c_error_flag
1219 retlw .0 ; All ok
1220 retlw .255 ; an error occured
878 END 1221 END