comparison src/i2c.asm @ 624:7bdcc591196c

Support for Compass3 hardware
author heinrichsweikamp
date Thu, 13 Jun 2019 10:07:55 +0200
parents c40025d8e750
children bf5fee575701
comparison
equal deleted inserted replaced
623:c40025d8e750 624:7bdcc591196c
23 ; -------- 23 ; --------
24 ; LSM303AGR's Compass read address (8-Bit): 0x3D 24 ; LSM303AGR's Compass read address (8-Bit): 0x3D
25 ; LSM303AGR's Compass write address (8-Bit): 0x3C 25 ; LSM303AGR's Compass write address (8-Bit): 0x3C
26 ; LSM303AGR's Acceleration read address (8-Bit): 0x33 26 ; LSM303AGR's Acceleration read address (8-Bit): 0x33
27 ; LSM303AGR's Acceleration write address (8-Bit): 0x32 27 ; LSM303AGR's Acceleration write address (8-Bit): 0x32
28 ;
29 ; Compass3
30 ; --------
31 ; LSM303C's Compass read address (8-Bit): 0x3D
32 ; LSM303C's Compass write address (8-Bit): 0x3C
33 ; LSM303C's Acceleration read address (8-Bit): 0x3B
34 ; LSM303C's Acceleration write address (8-Bit): 0x3A
28 ; 35 ;
29 ; RX Circuity 36 ; RX Circuity
30 ; ----------- 37 ; -----------
31 ; RX Circuity read address (8-Bit): 0x51 38 ; RX Circuity read address (8-Bit): 0x51
32 ; RX Circuity write address (8-Bit): 0x50 39 ; RX Circuity write address (8-Bit): 0x50
82 rrcf lo 89 rrcf lo
83 return 90 return
84 91
85 global I2C_RX_accelerometer 92 global I2C_RX_accelerometer
86 I2C_RX_accelerometer: 93 I2C_RX_accelerometer:
94 btfsc compass_type3 ; compass3
95 bra I2C_RX_accelerometer_compass3 ; yes
87 btfsc compass_type2 ; compass2 ? 96 btfsc compass_type2 ; compass2 ?
88 bra I2C_RX_accelerometer_compass2 ; YES 97 bra I2C_RX_accelerometer_compass2 ; YES
89 btfsc compass_type ; compass1 ? 98 btfsc compass_type ; compass1 ?
90 bra I2C_RX_accelerometer_compass1 ; YES 99 bra I2C_RX_accelerometer_compass1 ; YES
91 I2C_RX_accelerometer_compass0: 100 I2C_RX_accelerometer_compass0:
159 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 168 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
160 rcall I2C_TX 169 rcall I2C_TX
161 bsf SSP1CON2,RSEN ; repeated start condition (!) 170 bsf SSP1CON2,RSEN ; repeated start condition (!)
162 rcall WaitMSSP 171 rcall WaitMSSP
163 movlw 0x3D ; address 172 movlw 0x3D ; address
164 I2C_RX_accelerometer_compass1_xx: ; compass2 continues here... 173 I2C_RX_accelerometer_compass1_xx: ; compass2 and 3 continue here...
165 rcall I2C_TX 174 rcall I2C_TX
166 175
167 ; Non-flipped screen: 176 ; Non-flipped screen:
168 ; Chip orientation on the PCB requires 177 ; Chip orientation on the PCB requires
169 ; Original = Corrected 178 ; Original = Corrected
247 movlw b'10101000' ; 0x28 with auto-increment (MSB=1) 256 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
248 rcall I2C_TX 257 rcall I2C_TX
249 bsf SSP1CON2,RSEN ; repeated start condition (!) 258 bsf SSP1CON2,RSEN ; repeated start condition (!)
250 rcall WaitMSSP 259 rcall WaitMSSP
251 movlw 0x33 ; address 260 movlw 0x33 ; address
252 bra I2C_RX_accelerometer_compass1_xx 261 bra I2C_RX_accelerometer_compass1_xx
262
263 I2C_RX_accelerometer_compass3:
264 bsf SSP1CON2,SEN ; Start condition
265 rcall WaitMSSP
266 movlw 0x3A ; address
267 rcall I2C_TX
268 movlw 0x28 ; 0x28 (OUT_X_L_A)
269 rcall I2C_TX
270 bsf SSP1CON2,RSEN ; Repeated start condition (!)
271 rcall WaitMSSP
272 movlw 0x3B ; address
273 bra I2C_RX_accelerometer_compass1_xx
253 274
254 I2C_OneByteRX: 275 I2C_OneByteRX:
255 bsf SSP1CON2,RCEN ; enable receive mode 276 bsf SSP1CON2,RCEN ; enable receive mode
256 rcall WaitMSSP 277 rcall WaitMSSP
257 bsf SSP1CON2,ACKEN ; master acknowledge 278 bsf SSP1CON2,ACKEN ; master acknowledge
261 ;----------------------------------------------------------------------------- 282 ;-----------------------------------------------------------------------------
262 IFDEF _compass 283 IFDEF _compass
263 284
264 global I2C_RX_compass 285 global I2C_RX_compass
265 I2C_RX_compass: 286 I2C_RX_compass:
287 btfsc compass_type3 ; compass3
288 bra I2C_RX_compass3 ; YES
266 btfsc compass_type2 ; compass2 ? 289 btfsc compass_type2 ; compass2 ?
267 bra I2C_RX_compass2 ; YES 290 bra I2C_RX_compass2 ; YES
268 btfsc compass_type ; compass1 ? 291 btfsc compass_type ; compass1 ?
269 bra I2C_RX_compass1 ; YES 292 bra I2C_RX_compass1 ; YES
270 I2C_RX_compass0: 293 I2C_RX_compass0:
400 rcall I2C_TX 423 rcall I2C_TX
401 bsf SSP1CON2,RSEN ; repeated start condition (!) 424 bsf SSP1CON2,RSEN ; repeated start condition (!)
402 rcall WaitMSSP 425 rcall WaitMSSP
403 movlw 0x3D ; address 426 movlw 0x3D ; address
404 rcall I2C_TX 427 rcall I2C_TX
428 I2C_RX_compass2_xx: ; compass 3 enters here
405 ; rcall WaitMSSP 429 ; rcall WaitMSSP
406 rcall I2C_OneByteRX ; get one byte 430 rcall I2C_OneByteRX ; get one byte
407 movff SSP1BUF,lo ; data byte 431 movff SSP1BUF,lo ; data byte
408 rcall I2C_OneByteRX ; get one byte 432 rcall I2C_OneByteRX ; get one byte
409 movff SSP1BUF,hi ; data byte 433 movff SSP1BUF,hi ; data byte
438 ; rcall I2C_TwoBytesRX_div8_2 462 ; rcall I2C_TwoBytesRX_div8_2
439 MOVII mpr,compass_DZ 463 MOVII mpr,compass_DZ
440 bsf SSP1CON2,PEN ; stop condition 464 bsf SSP1CON2,PEN ; stop condition
441 bra WaitMSSP ;(And return) 465 bra WaitMSSP ;(And return)
442 466
467 I2C_RX_compass3:
468 bsf SSP1CON2,SEN ; Start condition
469 rcall WaitMSSP
470 movlw 0x3C ; address
471 rcall I2C_TX
472 movlw 0xA8 ; 0x28 with auto-increment (MSB=1)
473 rcall I2C_TX
474 bsf SSP1CON2,RSEN ; Repeated start condition (!)
475 rcall WaitMSSP
476 movlw 0x3D ; address
477 rcall I2C_TX
478 bra I2C_RX_compass2_xx
479
443 ENDIF ; _compass 480 ENDIF ; _compass
444 481
445 ;----------------------------------------------------------------------------- 482 ;-----------------------------------------------------------------------------
446 483
447 global I2C_init_compass 484 global I2C_init_compass
448 I2C_init_compass: 485 I2C_init_compass:
449 bsf compass_enabled 486 bsf compass_enabled
450 bcf compass_type2 487 bcf compass_type2
488 bcf compass_type3
489
490 ; probe compass 3
491 bsf SSP1CON2,SEN ; Start condition
492 rcall WaitMSSP
493 movlw 0x3A ; Address byte + Write bit
494 movwf SSP1BUF ; control byte
495 rcall WaitMSSP
496 btfss SSP1CON2,ACKSTAT ; ACK?
497 bsf compass_type3 ; ACK send. compass2 present
498 bsf SSP1CON2,PEN ; Stop condition
499 rcall WaitMSSP
500
501 btfsc compass_type3
502 bra I2C_init_compass3 ; Compass3
451 503
452 ; check for compass2 504 ; check for compass2
453 bsf SSP1CON2,SEN ; start condition 505 bsf SSP1CON2,SEN ; start condition
454 rcall WaitMSSP 506 rcall WaitMSSP
455 movlw 0x32 ; address byte + Write bit 507 movlw 0x32 ; address byte + Write bit
566 movlw b'00000000' ; CFG_REG_C_M BDU=0 0x62 618 movlw b'00000000' ; CFG_REG_C_M BDU=0 0x62
567 rcall I2C_TX 619 rcall I2C_TX
568 bsf SSP1CON2,PEN ; stop condition 620 bsf SSP1CON2,PEN ; stop condition
569 bra WaitMSSP ; ... and return 621 bra WaitMSSP ; ... and return
570 622
623 I2C_init_compass3:
624 bsf SSP1CON2,SEN ; Start condition
625 rcall WaitMSSP
626 movlw 0x3C ; address
627 rcall I2C_TX
628 movlw 0xA0 ; 0x20 with auto-increment (MSB=1)
629 rcall I2C_TX
630 movlw b'00010000' ; CTRL_REG1_M (10Hz) 0x20
631 rcall I2C_TX
632 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21
633 rcall I2C_TX
634 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22
635 rcall I2C_TX
636 movlw b'00000000' ; CTRL_REG4_M (Z in Low-power mode) 0x23
637 rcall I2C_TX
638 movlw b'00000000' ; CTRL_REG5_M 0x24
639 rcall I2C_TX
640 movlw b'00000000' ; CTRL_REG5_M 0x24
641 rcall I2C_TX
642 bsf SSP1CON2,PEN ; Stop condition
643 bra WaitMSSP ;(And return)
644
571 645
572 global I2C_sleep_compass 646 global I2C_sleep_compass
573 I2C_sleep_compass: 647 I2C_sleep_compass:
574 bcf compass_enabled 648 bcf compass_enabled
649 btfsc compass_type3 ; compass3?
650 bra I2C_sleep_compass3 ; YES
575 btfsc compass_type2 ; compass2 ? 651 btfsc compass_type2 ; compass2 ?
576 bra I2C_sleep_compass2 ; YES 652 bra I2C_sleep_compass2 ; YES
577 btfsc compass_type ; compass1 ? 653 btfsc compass_type ; compass1 ?
578 bra I2C_sleep_compass1 ; YES 654 bra I2C_sleep_compass1 ; YES
579 I2C_sleep_compass0: 655 I2C_sleep_compass0:
631 movlw b'00000000' ; INT_CTRL_REG_M 0x63 707 movlw b'00000000' ; INT_CTRL_REG_M 0x63
632 rcall I2C_TX 708 rcall I2C_TX
633 bsf SSP1CON2,PEN ; stop condition 709 bsf SSP1CON2,PEN ; stop condition
634 bra WaitMSSP ; ... and return 710 bra WaitMSSP ; ... and return
635 711
712 I2C_sleep_compass3:
713 ; magnetic
714 bsf SSP1CON2,SEN ; Start condition
715 rcall WaitMSSP
716 movlw 0x3C ; address
717 rcall I2C_TX
718 movlw 0xA2 ; 0x22 with auto-increment (MSB=1)
719 movlw b'01000010' ; CTRL_REG3_M (Power-down) 0x22
720 rcall I2C_TX
721 bsf SSP1CON2,PEN ; Stop condition
722 bra WaitMSSP ;(And return)
723
636 I2C_sleep_accelerometer2: 724 I2C_sleep_accelerometer2:
637 ; accelerometer 725 ; accelerometer
638 bsf SSP1CON2,SEN ; start condition 726 bsf SSP1CON2,SEN ; start condition
639 rcall WaitMSSP 727 rcall WaitMSSP
640 movlw 0x32 ; address 728 movlw 0x32 ; address
645 rcall I2C_TX 733 rcall I2C_TX
646 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off) 734 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off)
647 rcall I2C_TX 735 rcall I2C_TX
648 bsf SSP1CON2,PEN ; stop condition 736 bsf SSP1CON2,PEN ; stop condition
649 bra WaitMSSP ; ... and return 737 bra WaitMSSP ; ... and return
738
739 I2C_sleep_accelerometer3:
740 ; accelerometer
741 bsf SSP1CON2,SEN ; Start condition
742 rcall WaitMSSP
743 movlw 0x3A ; address
744 rcall I2C_TX
745 movlw 0x20
746 rcall I2C_TX
747 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = ON) 0x20
748 rcall I2C_TX
749 bsf SSP1CON2,PEN ; Stop condition
750 bra WaitMSSP ; (And return)
650 751
651 752
652 WaitMSSP: 753 WaitMSSP:
653 decfsz i2c_temp1,F ; check for timeout during I2C action 754 decfsz i2c_temp1,F ; check for timeout during I2C action
654 bra WaitMSSP2 755 bra WaitMSSP2
710 return 811 return
711 812
712 813
713 global I2C_init_accelerometer 814 global I2C_init_accelerometer
714 I2C_init_accelerometer: 815 I2C_init_accelerometer:
816 btfsc compass_type3 ; compass3?
817 bra I2C_init_accelerometer3 ; YES
818
715 btfsc compass_type2 ; compass2 ? 819 btfsc compass_type2 ; compass2 ?
716 bra I2C_init_accelerometer2 ; YES 820 bra I2C_init_accelerometer2 ; YES
717 821
718 btfsc compass_type ; compass1 ? 822 btfsc compass_type ; compass1 ?
719 return ; YES - ignore 823 return ; YES - ignore
777 ; movlw b'00000000' ; CTRL_REG5_A 881 ; movlw b'00000000' ; CTRL_REG5_A
778 ; rcall I2C_TX 882 ; rcall I2C_TX
779 bsf SSP1CON2,PEN ; stop condition 883 bsf SSP1CON2,PEN ; stop condition
780 bra WaitMSSP ; ... and return 884 bra WaitMSSP ; ... and return
781 885
886 I2C_init_accelerometer3:
887 bsf SSP1CON2,SEN ; Start condition
888 rcall WaitMSSP
889 movlw 0x3A ; address
890 rcall I2C_TX
891 movlw 0x20
892 rcall I2C_TX
893 movlw b'00110111' ; CTRL_REG1_A (100Hz, x,y,z = ON) 0x20
894 rcall I2C_TX
895 movlw b'00000000' ; CTRL_REG2_A 0x21
896 rcall I2C_TX
897 movlw b'00000000' ; CTRL_REG3_A 0x22
898 rcall I2C_TX
899 bsf SSP1CON2,PEN ; Stop condition
900 bra WaitMSSP ; (And return)
901
782 902
783 global I2C_sleep_accelerometer 903 global I2C_sleep_accelerometer
784 I2C_sleep_accelerometer: 904 I2C_sleep_accelerometer:
905 btfsc compass_type3 ; Compass3
906 bra I2C_sleep_accelerometer3 ; YES
785 btfsc compass_type2 ; Compass2 907 btfsc compass_type2 ; Compass2
786 bra I2C_sleep_accelerometer2 ; YES 908 bra I2C_sleep_accelerometer2 ; YES
787 btfsc compass_type ; compass1? 909 btfsc compass_type ; compass1?
788 return ; YES - ignore 910 return ; YES - ignore
789 911