annotate src/i2c.asm @ 642:a9a0188091e4

fix rare upgrade issue with OSTC sport 2019 hardware
author heinrichsweikamp
date Thu, 14 Jan 2021 16:24:07 +0100
parents 8c1f1f334275
children 7d8a4c60ec1a 5b7fe7777425
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
3 ; File i2c.asm * combined next generation V3.09.4b
0
heinrichsweikamp
parents:
diff changeset
4 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
5 ; I2C Interface
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
6 ;
0
heinrichsweikamp
parents:
diff changeset
7 ; Copyright (c) 2012, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
8 ;=============================================================================
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
9 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
10 ; Compass0
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
11 ; --------
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
12 ; HMC5883L's read address (8-Bit): 0x3D
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
13 ; HMC5883L's write address (8-Bit): 0x3C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
14 ; MMA8452Q's read address (8-Bit): 0x39
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
15 ; MMA8452Q's write address (8-Bit): 0x38
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
16 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
17 ; Compass1
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
18 ; --------
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
19 ; LSM303D's read address (8-Bit): 0x3D
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
20 ; LSM303D's write address (8-Bit): 0x3C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
21 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
22 ; Compass2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
23 ; --------
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
24 ; LSM303AGR's Compass read address (8-Bit): 0x3D
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
25 ; LSM303AGR's Compass write address (8-Bit): 0x3C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
26 ; LSM303AGR's Acceleration read address (8-Bit): 0x33
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
27 ; LSM303AGR's Acceleration write address (8-Bit): 0x32
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
28 ;
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
29 ; Compass3
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
30 ; --------
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
31 ; LSM303C's Compass read address (8-Bit): 0x3D
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
32 ; LSM303C's Compass write address (8-Bit): 0x3C
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
33 ; LSM303C's Acceleration read address (8-Bit): 0x3B
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
34 ; LSM303C's Acceleration write address (8-Bit): 0x3A
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
35 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
36 ; RX Circuity
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
37 ; -----------
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
38 ; RX Circuity read address (8-Bit): 0x51
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
39 ; RX Circuity write address (8-Bit): 0x50
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
40 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
41 ;
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
42
0
heinrichsweikamp
parents:
diff changeset
43 ; HISTORY
heinrichsweikamp
parents:
diff changeset
44 ; 2012-08-22 : [mH] Creation
565
0ba88db66492 CHANGE: Limit button sensitivity to 80%
heinrichsweikamp
parents: 564
diff changeset
45 ; 2018-02-18 : [mH] Sync with hwOS Sport release
0
heinrichsweikamp
parents:
diff changeset
46
heinrichsweikamp
parents:
diff changeset
47
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
48 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
49 #include "wait.inc"
113
heinrichsweikamp
parents: 20
diff changeset
50 #include "math.inc"
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
51 #include "eeprom_rs232.inc"
113
heinrichsweikamp
parents: 20
diff changeset
52
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
53
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
54 ;=============================================================================
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
55 i2c CODE
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
56 ;=============================================================================
0
heinrichsweikamp
parents:
diff changeset
57
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
58
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
59 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
60 ; Helper Function - send 1 Byte, wait for end of transmission and check ackn
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
61 ;
0
heinrichsweikamp
parents:
diff changeset
62 I2C_TX:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
63 movwf SSP1BUF ; put byte to be sent into TX buffer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
64 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
65 bra I2C_Check_ACK ; check for acknowledge by receiver and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
66
0
heinrichsweikamp
parents:
diff changeset
67
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
68 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
69 ; Helper Function - get two Bytes and divide hi:lo/16 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
70 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
71 I2C_TwoBytesRX_div16:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
72 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
73 movff SSP1BUF,hi ; copy data byte to hi
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
74 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
75 movff SSP1BUF,lo ; copy data byte to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
76 I2C_TwoBytesRX_div16_2: ; divide hi:lo/16 (signed) only
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
77 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
78 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
79 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
80 rrcf hi ; /2
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
81 rrcf lo
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
82 ;bra I2C_TwoBytesRX_div8 ; continue dividing hi:lo/8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
83
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
84
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
85 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
86 ; Helper Function - divide hi:lo/8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
87 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
88 I2C_TwoBytesRX_div8:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
89 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
90 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
91 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
92 rrcf hi ; /4
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
93 rrcf lo
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
94 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
95 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
96 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
97 rrcf hi ; /8
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
98 rrcf lo
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
99 bcf STATUS,C
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
100 btfsc hi,7 ; copy sign bit to carry
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
101 bsf STATUS,C
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
102 rrcf hi ; /16
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
103 rrcf lo
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
104 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
105
0
heinrichsweikamp
parents:
diff changeset
106
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
107 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
108 ; Read Accelerometer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
109 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
110 global I2C_RX_accelerometer
0
heinrichsweikamp
parents:
diff changeset
111 I2C_RX_accelerometer:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
112 btfsc compass_type3 ; compass3 ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
113 bra I2C_RX_accelerometer_compass3 ; YES
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
114 btfsc compass_type2 ; compass2 ?
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
115 bra I2C_RX_accelerometer_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
116 btfsc compass_type1 ; compass1 ?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
117 bra I2C_RX_accelerometer_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
118 ;bra I2C_RX_accelerometer_compass0 ; NO - compass0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
119
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
120 I2C_RX_accelerometer_compass0:
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
121 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
122 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
123 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
124 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
125 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
126 rcall I2C_TX ; send byte
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
127 bsf SSP1CON2,RSEN ; repeated start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
128 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
129 movlw 0x39 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
130 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
131 rcall I2C_OneByteRX ; get status byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
132 movf SSP1BUF,W ; copy status byte to WREG
158
683321c09cfa nicer boot into surfacemode
heinrichsweikamp
parents: 113
diff changeset
133
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
134 ; Non-flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
135 ; Chip orientation on the PCB requires
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
136 ; Original = corrected
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
137 ; x = -x
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
138 ; y = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
139 ; z = -z
0
heinrichsweikamp
parents:
diff changeset
140
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
141 ; Flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
142 ; Chip orientation on the PCB requires
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
143 ; Original = corrected
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
144 ; x = x
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
145 ; y = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
146 ; z = -z
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
147
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
148 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
149 btfsc flip_screen ; 180° rotation?
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
150 bra I2C_RX_accelerometer2 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
151 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
152 negf lo ; - ....
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
153 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
154 incf hi,F ; - YES - do it
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
155 I2C_RX_accelerometer2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
156 MOVII mpr,accel_DX ; copy result to accel_DX
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
157
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
158 rcall I2C_TwoBytesRX_div16 ; get two bytes and divide /16 (signed)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
159 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
160 bra I2C_RX_accelerometer3 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
161 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
162 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
163 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
164 incf hi,F ; YES - do it
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
165 I2C_RX_accelerometer3:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
166 MOVII mpr,accel_DY ; copy result to accel_DY
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
167
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
168 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
169 movff SSP1BUF,hi ; copy data byte to hi
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
170 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
171 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
172 ; according to data sheet there should be no master acknowledge for the last byte (accel_DZ+0)...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
173 movff SSP1BUF,lo ; copy data byte to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
174 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
175 comf hi ; 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
176 negf lo ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
177 btfsc STATUS,C ; carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
178 incf hi,F ; YES - do it
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
179 MOVII mpr,accel_DZ ; copy result to accel_DZ
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
180 bsf SSP1CON2,PEN ; stop condition
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
181 bra WaitMSSP ; wait for TX to complete and return
0
heinrichsweikamp
parents:
diff changeset
182
heinrichsweikamp
parents:
diff changeset
183
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
184 I2C_RX_accelerometer_compass1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
185 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
186 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
187 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
188 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
189 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
190 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
191 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
192 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
193 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
194
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
195 I2C_RX_accelerometer_common: ; common part for compass 1,2 and 3
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
196 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
197
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
198 ; Non-flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
199 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
200 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
201 ; x = -x (Compass 1)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
202 ; x = x (Compass 2)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
203 ; y = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
204 ; z = -z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
205
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
206 ; Flipped screen:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
207 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
208 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
209 ; x = x (Compass 1)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
210 ; x = -x (Compass 2)
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
211 ; y = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
212 ; z = -z
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
213
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
214 ; Dump the accelerator data
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
215 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
216 movff SSP1BUF,lo ; accel_DX+0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
217 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
218 movff SSP1BUF,hi ; accel_DX+1
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
219 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
220 btfss compass_type2 ; compass 2?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
221 bra I2C_RX_accelerometer1_c1 ; NO - compass 1
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
222 ; compass 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
223 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
224 bra I2C_RX_accelerometer2_c1 ; NO - continue with normal compass1 routines for Y and Z
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
225 ; flipped compass 2, negate x
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
226 comf hi ; YES - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
227 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
228 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
229 incf hi,F ; YES - do it
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
230 bra I2C_RX_accelerometer2_c1 ; - continue with normal compass1 routines for Y and Z
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
231
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
232 I2C_RX_accelerometer1_c1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
233 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
234 bra I2C_RX_accelerometer2_c1 ; YES
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
235 ; non-flipped compass 1, negate x
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
236 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
237 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
238 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
239 incf hi,F ; YES - do it
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
240 I2C_RX_accelerometer2_c1:
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
241 ; flipped compass 1, non-flipped compass 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
242 MOVII mpr,accel_DX ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
243 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
244 movff SSP1BUF,lo ; copy accel_DY+0 to lo
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
245 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
246 movff SSP1BUF,hi ; copy accel_DY+1 to hi
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
247 rcall I2C_TwoBytesRX_div16_2 ; divide hi:lo/16 (signed) only
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
248 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
249 bra I2C_RX_accelerometer3_c1 ; YES
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
250 comf hi ; NO - 16 bit sign change
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
251 negf lo ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
252 btfsc STATUS,C ; - carry to propagate?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
253 incf hi,F ; YES - do it
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
254 I2C_RX_accelerometer3_c1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
255 MOVII mpr,accel_DY ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
256 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
257 movff SSP1BUF,lo ; accel_DZ+0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
258 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
259 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
260 ; according to data sheet there should be no master Acknowledge for the last byte (accel_DZ+1)...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
261 movff SSP1BUF,hi ; accel_DZ+1
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
262 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
263 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
264 rcall I2C_TwoBytesRX_div16_2 ; divide lo:hi/16 (signed) only
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
265 comf hi ; 16 bit sign change for Z
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
266 negf lo ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
267 btfsc STATUS,C ; carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
268 incf hi,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
269 MOVII mpr,accel_DZ ; copy result
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
270 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
271
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
272
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
273 I2C_RX_accelerometer_compass2:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
274 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
275 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
276 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
277 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
278 movlw b'10101000' ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
279 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
280 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
281 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
282 movlw 0x33 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
283 bra I2C_RX_accelerometer_common ; continue with common part
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
284
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
285 I2C_RX_accelerometer_compass3:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
286 bsf SSP1CON2,SEN ; start condition
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
287 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
288 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
289 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
290 movlw 0x28 ; 0x28 (OUT_X_L_A)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
291 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
292 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
293 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
294 movlw 0x3B ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
295 bra I2C_RX_accelerometer_common ; continue with common part
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
296
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
297
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
298 ;-----------------------------------------------------------------------------
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
299 ; Helper Function - receive 1 Byte with Acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
300 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
301 I2C_OneByteRX:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
302 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
303 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
304 bsf SSP1CON2,ACKEN ; send master acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
305 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
306
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
307
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
308 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
309 ; Read Compass
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
310 ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
311 IFDEF _compass
0
heinrichsweikamp
parents:
diff changeset
312
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
313 global I2C_RX_compass
0
heinrichsweikamp
parents:
diff changeset
314 I2C_RX_compass:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
315 btfsc compass_type3 ; compass 3 ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
316 bra I2C_RX_compass3 ; YES
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
317 btfsc compass_type2 ; compass 2 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
318 bra I2C_RX_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
319 btfsc compass_type1 ; compass 1 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
320 bra I2C_RX_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
321 ;bra I2C_RX_compass0 ; NO - compass 0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
322
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
323 I2C_RX_compass0:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
324 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
325 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
326 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
327 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
328 movlw 0x03 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
329 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
330 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
331 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
332 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
333 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
334 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
335 rcall I2C_TX ; send byte
0
heinrichsweikamp
parents:
diff changeset
336
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
337 ; Compass IC sends data in following order:
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
338 ; x MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
339 ; x LSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
340 ; z MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
341 ; z LSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
342 ; y MSB
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
343 ; y LSB
0
heinrichsweikamp
parents:
diff changeset
344
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
345 ; Non-flipped screen
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
346 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
347 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
348 ; x = -y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
349 ; z = z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
350 ; y = x
0
heinrichsweikamp
parents:
diff changeset
351
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
352 ; Flipped screen
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
353 ; Chip orientation on the PCB requires
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
354 ; Original = Corrected
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
355 ; x = y
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
356 ; z = z
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
357 ; y = -x
166
30ebaf72170d BUGFIX: Flip compass with flipped screen, too
heinrichsweikamp
parents: 158
diff changeset
358
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
359 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
360 movff SSP1BUF,compass_DY+1 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
361 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
362 movff SSP1BUF,compass_DY+0 ; data byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
363 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
364 bra I2C_RX_compass0_2 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
365 banksel compass_DY ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
366 comf compass_DY+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
367 negf compass_DY+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
368 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
369 incf compass_DY+1,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
370 banksel common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
371 I2C_RX_compass0_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
372 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
373 movff SSP1BUF,compass_DZ+1 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
374 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
375 movff SSP1BUF,compass_DZ+0 ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
376 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
377 movff SSP1BUF,compass_DX+1 ; data byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
378 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
379 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
380 movff SSP1BUF,compass_DX+0 ; data byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
381 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
382 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
383 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
384 return ; NO - done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
385 banksel compass_DX ; YES - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
386 comf compass_DX+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
387 negf compass_DX+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
388 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
389 incf compass_DX+1,F ; YES - do it
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
390 banksel common ; back to bank common
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
391 return ; done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
392
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
393
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
394 I2C_RX_compass1: ; compass type 1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
395 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
396 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
397 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
398 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
399 movlw b'10001000' ; 0x08 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
400 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
401 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
402 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
403 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
404 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
405 ;rcall WaitMSSP ; TODO needed? (mH)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
406 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
407 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
408 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
409 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
410 rcall I2C_TwoBytesRX_div8 ; divide hi,lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
411 MOVII mpr,compass_DX ; copy result
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
412 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
413 bra I2C_RX_compass1_1 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
414 banksel compass_DX ; YES - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
415 comf compass_DX+1 ; - 16 bit sign change
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
416 negf compass_DX+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
417 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
418 incf compass_DX+1,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
419 banksel common
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
420 I2C_RX_compass1_1:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
421 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
422 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
423 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
424 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
425 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
426 MOVII mpr,compass_DY
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
427 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
428 bra I2C_RX_compass1_2 ; NO
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
429 banksel compass_DY ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
430 comf compass_DY+1 ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
431 negf compass_DY+0 ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
432 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
433 incf compass_DY+1,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
434 banksel common
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
435 I2C_RX_compass1_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
436 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
437 movff SSP1BUF,lo ; data byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
438 bsf SSP1CON2, RCEN ; Enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
439 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
440 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
441 rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
442 MOVII mpr,compass_DZ ; copy result
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
443 bsf SSP1CON2,PEN ; stop condition
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
444 bra WaitMSSP ; ... and return
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
445
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
446
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
447 I2C_RX_compass2: ; compass type 2
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
448 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
449 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
450 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
451 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
452 movlw 0xE8 ; 0x68 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
453 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
454 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
455 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
456 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
457 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
458 I2C_RX_compass2_xx: ; compass 3 joins in here
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
459 ; rcall WaitMSSP ; wait for TX to complete (not needed, as included in I2C_TX)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
460 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
461 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
462 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
463 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
464 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
465 btfsc flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
466 bra I2C_RX_compass2_1 ; YES - do nothing with X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
467 ; NO - flip X
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
468 comf hi ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
469 negf lo ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
470 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
471 incf hi,F ; YES - do it
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
472 I2C_RX_compass2_1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
473 MOVII mpr,compass_DX
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
474 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
475 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
476 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
477 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
478 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
479 btfss flip_screen ; 180° rotation?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
480 bra I2C_RX_compass2_2 ; NO - do nothing with Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
481 ; YES - flip Y
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
482 comf hi ; - 16 bit sign change
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
483 negf lo ; - ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
484 btfsc STATUS,C ; - carry to propagate?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
485 incf hi,F ; YES - do it
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
486 I2C_RX_compass2_2:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
487 MOVII mpr,compass_DY
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
488 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
489 movff SSP1BUF,lo ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
490 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
491 movff SSP1BUF,hi ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
492 ; rcall I2C_TwoBytesRX_div8 ; divide hi, lo by 8 (signed)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
493 MOVII mpr,compass_DZ ; copy result
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
494 bsf SSP1CON2,PEN ; stop condition
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
495 bra WaitMSSP ; ...and return
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
496
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
497
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
498 I2C_RX_compass3: ; compass type 3
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
499 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
500 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
501 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
502 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
503 movlw 0xA8 ; 0x28 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
504 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
505 bsf SSP1CON2,RSEN ; repeated start condition (!)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
506 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
507 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
508 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
509 bra I2C_RX_compass2_xx ; join with compass 2 code
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
510
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
511 ENDIF ; _compass
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
512
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
513
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
514 ;-----------------------------------------------------------------------------
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
515 ; Initialize Compass / Accelerometer Chip
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
516 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
517 global I2C_init_compass
0
heinrichsweikamp
parents:
diff changeset
518 I2C_init_compass:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
519 bsf compass_enabled ; flag compass will be enabled
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
520 bcf compass_type2 ; clear in preparation of chip detection
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
521 bcf compass_type3 ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
522
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
523 ; probe for compass 3
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
524 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
525 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
526 movlw 0x3A ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
527 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
528 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
529 btfss SSP1CON2,ACKSTAT ; ACK received?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
530 bsf compass_type3 ; YES - ACK was send, compass 3 found
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
531 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
532 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
533
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
534 btfsc compass_type3 ; compass 3 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
535 bra I2C_init_compass3 ; YES - initialize compass 3
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
536
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
537 ; probe for compass 2
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
538 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
539 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
540 movlw 0x32 ; address byte + write bit
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
541 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
542 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
543 btfss SSP1CON2,ACKSTAT ; ACK received?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
544 bsf compass_type2 ; YES - ACK send, compass 2 found
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
545 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
546 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
547
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
548 btfsc compass_type2 ; compass 2 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
549 bra I2C_init_compass2 ; YES - initialize compass 2
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
550
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
551 ; probe for compass 0 or 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
552 bsf compass_type1 ; assume compass 1 by default
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
553 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
554 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
555 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
556 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
557 movlw 0x0F ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
558 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
559 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
560 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
561 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
562 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
563 movlw 0x3D ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
564 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
565 rcall I2C_OneByteRX ; receive 1 byte with acknowledge
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
566 movlw 0x49 ; 0x49 = compass 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
567 cpfseq SSP1BUF ; 0x49 received?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
568 bcf compass_type1 ; NO - clear flag for compass 1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
569 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
570 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
571
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
572 btfsc compass_type1 ; compass 1 found?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
573 bra I2C_init_compass1 ; YES - initialize compass 1
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
574 ;bra I2C_init_compass0 ; NO - must be compass 0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
575
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
576
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
577 I2C_init_compass0:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
578 ; magnetic
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
579 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
580 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
581 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
582 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
583 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
584 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
585 movlw b'01101000' ; ConfigA: 3 Hz, 8 samples averaged
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
586 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
587 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
588 swapf i2c_temp1,F ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
589 comf i2c_temp1,F ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
590 bcf STATUS,C ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
591 rlcf i2c_temp1 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
592 movf i2c_temp1,W ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
593 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
594 movlw b'00000000' ; select continuous mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
595 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
596 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
597 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
598
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
599 ; accelerometer
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
600 rcall I2C_sleep_accelerometer0 ; registers can only be changed in standby mode
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
601
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
602 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
603 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
604 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
605 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
606 movlw 0x0E ; XYZ_DATA_CFG
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
607 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
608 movlw b'00000000' ; high pass filter = 0, +/- 2 g range
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
609 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
610 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
611 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
612 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
613 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
614 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
615 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
616 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
617 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
618 ; movlw b'00110000' ; CTRL_REG1: 160 ms data rate, standby mode
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
619 movlw b'00110100' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
620 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
621 movlw b'00000010' ; CTRL_REG2: high-res in active mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
622 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
623 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
624 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
625
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
626 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
627 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
628 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
629 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
630 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
631 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
632 ; movlw b'00110001' ; CTRL_REG1: 160 ms data rate, active mode
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
633 movlw b'00110101' ; CTRL_REG1: 160 ms data rate, standby mode, reduced noise mode, active Mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
634 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
635 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
636 bra WaitMSSP ; wait for TX to complete and return
0
heinrichsweikamp
parents:
diff changeset
637
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
638
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
639 I2C_init_compass1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
640 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
641 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
642 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
643 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
644 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
645 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
646 movlw b'00000000' ; CTRL0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
647 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
648 movlw b'00101111' ; CTRL1 (6.25 Hz, BDU=0, x,y,z = ON)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
649 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
650 movlw b'11000000' ; CTRL2 (50 Hz, +/- 2g)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
651 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
652 movlw b'00000000' ; CTRL3
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
653 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
654 movlw b'00000000' ; CTRL4
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
655 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
656 movlw b'01100100' ; CTRL5 HIGH res, 6.25 Hz
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
657 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
658 movff opt_compass_gain,i2c_temp1 ; 0-7 (230 LSB/Gauss to 1370 LSB/Gauss)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
659 movlw b'01100000' ; CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
660 dcfsnz i2c_temp1,F ; = 1?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
661 movlw b'01100000' ; YES - CTRL6 Full scale (+/-12 Gauss -> 2730 LSB/Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
662 dcfsnz i2c_temp1,F ; = 2?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
663 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
664 dcfsnz i2c_temp1,F ; = 3?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
665 movlw b'01000000' ; YES - CTRL6 (+/-8 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
666 dcfsnz i2c_temp1,F ; = 4?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
667 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
668 dcfsnz i2c_temp1,F ; = 5?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
669 movlw b'00100000' ; YES - CTRL6 (+/-4 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
670 dcfsnz i2c_temp1,F ; = 6?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
671 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
672 dcfsnz i2c_temp1,F ; = 7?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
673 movlw b'00000000' ; YES - CTRL6 (+/-2 Gauss)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
674 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
675 movlw b'00000000' ; CTRL7 Continuous Mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
676 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
677 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
678 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
679
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
680 ; accelerometer initializes along with magnetic sensor
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
681
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
682
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
683 I2C_init_compass2:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
684 ; magnetic
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
685 bsf SSP1CON2,SEN ; start condition
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
686 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
687 movlw 0x3C ; address
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
688 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
689 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
690 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
691 movlw b'10000000' ; CFG_REG_A_M (10Hz, Continuous) 0x60 0x00
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
692 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
693 movlw b'00000011' ; CFG_REG_B_M (low-pass filter enabled) 0x61 (set pulse is released every 63 ODR)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
694 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
695 movlw b'00010000' ; CFG_REG_C_M BDU=1 0x62 0x57
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
696 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
697 bsf SSP1CON2,PEN ; stop condition
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
698 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
699
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
700 ; accelerometer
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
701 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
702 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
703 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
704 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
705 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
706 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
707 movlw b'00000000' ; TEMP_CFG_REG_A (Temp sensor off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
708 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
709 movlw b'00100111' ; CTRL_REG1_A (10Hz, x,y,z = ON)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
710 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
711 movlw b'00000000' ; CTRL_REG2_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
712 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
713 movlw b'00000000' ; CTRL_REG3_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
714 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
715 movlw b'00001000' ; CTRL_REG4_A (BDU=0, +/-2g, HR=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
716 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
717 ; movlw b'00000000' ; CTRL_REG5_A
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
718 ; rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
719 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
720 bra WaitMSSP ; wait for TX to complete and return
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
721
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
722
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
723 I2C_init_compass3:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
724 ; magnetic
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
725 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
726 rcall WaitMSSP ; wait for TX to complete
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
727 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
728 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
729 movlw 0xA0 ; 0x20 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
730 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
731 movlw b'01110000' ; CTRL_REG1_M (10Hz) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
732 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
733 movlw b'01100000' ; CTRL_REG2_M (Full-scale: +/- 16gauss) 0x21
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
734 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
735 movlw b'01000000' ; CTRL_REG3_M (Continuous) 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
736 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
737 movlw b'00000000' ; CTRL_REG4_M (Z in Low-power mode) 0x23
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
738 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
739 movlw b'00000000' ; CTRL_REG5_M 0x24
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
740 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
741 movlw b'00000000' ; CTRL_REG5_M 0x24
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
742 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
743 bsf SSP1CON2,PEN ; Stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
744 rcall WaitMSSP ; wait for TX to complete
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
745
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
746 ;accelerometer
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
747 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
748 rcall WaitMSSP ; wait for TX to complete
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
749 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
750 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
751 movlw 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
752 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
753 movlw b'10010111' ; CTRL_REG1_A (100Hz, x,y,z = ON, BDU=OFF) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
754 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
755 movlw b'00000000' ; CTRL_REG2_A 0x21
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
756 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
757 movlw b'00000000' ; CTRL_REG3_A 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
758 rcall I2C_TX ; send byte
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
759 movlw b'11001100' ; CTRL_REG4_A 0x23
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
760 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
761 bsf SSP1CON2,PEN ; stop condition
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
762 bra WaitMSSP ; wait for TX to complete and return
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
763
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
764
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
765 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
766 ; Deactivate Compass / Accelerometer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
767 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
768 global I2C_sleep_compass
0
heinrichsweikamp
parents:
diff changeset
769 I2C_sleep_compass:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
770 btfss compass_enabled ; compass active?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
771 return ; NO - done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
772 bcf compass_enabled
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
773 btfsc compass_type3 ; compass 3 ?
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
774 bra I2C_sleep_compass3 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
775 btfsc compass_type2 ; compass 2 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
776 bra I2C_sleep_compass2 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
777 btfsc compass_type1 ; compass 1 ?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
778 bra I2C_sleep_compass1 ; YES
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
779 ;bra I2C_sleep_compass0 ; NO - must be compass 0 then
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
780
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
781
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
782 I2C_sleep_compass0:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
783 ; magnetic
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
784 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
785 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
786 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
787 rcall I2C_TX ; send byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
788 movlw 0x00 ; ??
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
789 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
790 movlw b'01101000' ; ConfigA
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
791 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
792 movlw b'00100000' ; ConfigB
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
793 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
794 movlw b'00000010' ; idle mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
795 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
796 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
797 rcall WaitMSSP ; wait for TX to complete
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
798
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
799 I2C_sleep_accelerometer0:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
800 ; accelerometer
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
801 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
802 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
803 movlw 0x38 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
804 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
805 movlw 0x2A ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
806 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
807 movlw b'00000000' ; standby mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
808 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
809 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
810 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
811
0
heinrichsweikamp
parents:
diff changeset
812
427
ceb1b7329dce add code for new compass chip
heinrichsweikamp
parents: 275
diff changeset
813 I2C_sleep_compass1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
814 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
815 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
816 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
817 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
818 movlw 0x20 ; CTRL_REG1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
819 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
820 movlw b'00000000' ; data for CTRL_REG1: acceleration sensor power-down mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
821 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
822 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
823 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
824 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
825 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
826 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
827 rcall I2C_TX ; send byte
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
828 movlw 0x26 ; CTRL_REG7
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
829 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
830 movlw b'00000010' ; data for CTRL_REG7: magnetic sensor power-down mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
831 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
832 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
833 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
834
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
835 ; accelerometer sleeps with magnetic sensor
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
836
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
837
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
838 I2C_sleep_compass2:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
839 ; magnetic
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
840 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
841 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
842 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
843 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
844 movlw 0xE0 ; 0x60 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
845 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
846 movlw b'00000011' ; CFG_REG_A_M 0x60 (idle mode))
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
847 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
848 movlw b'00000100' ; CFG_REG_B_M 0x61 (set pulse is released only at power-on after PD condition)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
849 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
850 movlw b'01010001' ; CFG_REG_C_M 0x62
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
851 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
852 movlw b'00000000' ; INT_CTRL_REG_M 0x63
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
853 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
854 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
855 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
856
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
857 ; accelerometer
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
858 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
859 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
860 movlw 0x32 ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
861 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
862 movlw 0x9F ; 1F with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
863 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
864 movlw b'00000000' ; TEMP_CFG_REG_A 0x1F (temp sensor off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
865 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
866 movlw b'00000000' ; CTRL_REG1_A 0x20 (all off)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
867 rcall I2C_TX ; send byte
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
868 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
869 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
870
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
871
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
872 I2C_sleep_compass3:
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
873 ; magnetic
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
874 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
875 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
876 movlw 0x3C ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
877 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
878 movlw 0xA2 ; 0x22 with auto-increment (MSB=1)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
879 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
880 movlw b'01000010' ; CTRL_REG3_M (power-down) 0x22
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
881 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
882 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
883 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
884
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
885 ; accelerometer
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
886 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
887 rcall WaitMSSP ; wait for TX to complete
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
888 movlw 0x3A ; address
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
889 rcall I2C_TX ; send byte
624
7bdcc591196c Support for Compass3 hardware
heinrichsweikamp
parents: 623
diff changeset
890 movlw 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
891 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
892 movlw b'00000000' ; CTRL_REG1_A (100Hz, x,y,z = OFF) 0x20
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
893 rcall I2C_TX ; send byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
894 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
895 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
896
0
heinrichsweikamp
parents:
diff changeset
897
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
898 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
899 ; Helper Function - wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
900 ;
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
901 WaitMSSP:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
902 clrf i2c_temp1 ; wait for max 256 loops
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
903 WaitMSSP_loop:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
904 decfsz i2c_temp1,F ; decrement loop counter, timeout?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
905 bra WaitMSSP2 ; NO
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
906 bra I2CFail ; YES
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
907 WaitMSSP2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
908 btfss PIR1,SSP1IF ; TX completed?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
909 bra WaitMSSP_loop ; NO - loop
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
910 bcf PIR1,SSP1IF ; YES - clear TX completion flag
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
911 return ; - done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
912
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
913
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
914 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
915 ; Helper Function - check for Acknowledge by Receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
916 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
917 I2C_Check_ACK:
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
918 btfss SSP1CON2,ACKSTAT ; ACK received from slave?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
919 return ; YES - done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
920 ;bra I2CFail ; NO - do some clean up
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
921
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
922
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
923 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
924 ; Helper Function - clean up I2C Interface after missing Acknowledge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
925 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
926 I2CFail:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
927 bsf active_reset_ostc_rx ; reset RX circuitry (which may be the cause for the hang up)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
928 rcall I2CReset ; reset I2C
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
929 bcf PIR1,SSP1IF ; clear TX completion flag
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
930 bsf i2c_error_flag ; set error flag
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
931 bcf active_reset_ostc_rx ; release reset from RX circuitry
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
932 return ; done
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
933
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
934
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
935 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
936 ; Helper Function - Reset I2C Module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
937 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
938 ; recover in case something went wrong, i.g. slave holds SDA low
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
939 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
940 I2CReset:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
941 clrf SSP1CON1 ; reset entire module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
942 clrf SSP1CON2 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
943 clrf SSP1STAT ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
944 bcf TRISC,3 ; SCL as OUTPUT
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
945 bsf TRISC,4 ; SDA as input
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
946 bcf PORTC,3 ; SCL = 0
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
947 movlw d'9' ; clock-out 9 clock cycles manually
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
948 movwf i2c_temp1 ; ...
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
949 I2CReset_1:
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
950 bsf PORTC,3 ; SCL = 1
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
951 nop ; pause for 4 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
952 nop ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
953 nop ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
954 nop ; ...
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
955 btfsc PORTC,4 ; SDA = 1 ?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
956 bra I2CReset_2 ; YES - SDA has been released from slave
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
957 bcf PORTC,3 ; NO - set SCL = 0
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
958 nop ; - pause for 2 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
959 nop ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
960 bcf PORTC,3 ; - SCL = 0
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
961 nop ; - pause for 2 CPU cycles
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
962 nop ; - ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
963 decfsz i2c_temp1,F ; - clock counter, all cycles done?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
964 bra I2CReset_1 ; NO - loop
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
965 I2CReset_2:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
966 bsf TRISC,3 ; SCL as input
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
967 clrf SSP1CON1 ; setup I2C mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
968 WAITMS d'10' ; wait 10 ms (reset-timeout for I2C devices)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
969 movlw b'00000000' ; enable slew rate control
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
970 movwf SSP1STAT ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
971 movlw b'00101000' ; configure I2C module
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
972 movwf SSP1CON1 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
973 movlw b'00000000' ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
974 movwf SSP1CON2 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
975 movlw 0x9C ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
976 movwf SSP1ADD ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
977 return ; done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
978
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
979
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
980 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
981 ; Helper Function - Initialize Gauge IC again after an UVLO Event
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
982 ;
556
dd28d4efd4d2 fix a potential issue in the battery managment
heinrichsweikamp
parents: 498
diff changeset
983 lt2942_init_again:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
984 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
985 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
986 movff battery_accumulated_charge+1,SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
987 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
988 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
989 movff battery_accumulated_charge+0,SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
990 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
991 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
992 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
993 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
994 MOVII battery_accumulated_charge,sub_a ; copy result to sub_a
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
995 ;bra lt2942_init ; and initialize again...
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
996
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
997
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
998 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
999 ; Initialize Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1000 ;
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1001 global lt2942_init
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1002 lt2942_init:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1003 movlw 0x01 ; point to control reg B
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1004 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1005 movlw b'11111000' ; automatic conversion every two seconds
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1006 movwf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1007 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1008 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1009 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1010 bra WaitMSSP ; wait for TX to complete and return
113
heinrichsweikamp
parents: 20
diff changeset
1011
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1012 ;-----------------------------------------------------------------------------
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1013 ; Sleep Gauge IC
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1014 ;
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1015 global lt2942_sleep
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1016 lt2942_sleep:
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1017 movlw 0x01 ; point to control reg B
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1018 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1019 movlw b'00111000' ; sleep
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1020 movwf SSP1BUF ; data byte
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1021 rcall WaitMSSP ; wait for TX to complete
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1022 rcall I2C_Check_ACK ; check for acknowledge by receiver
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1023 bsf SSP1CON2,PEN ; stop condition
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1024 bra WaitMSSP ; wait for TX to complete and return
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1025
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1026 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1027 ; Read Gauge IC - Status Register
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1028 ;
113
heinrichsweikamp
parents: 20
diff changeset
1029 global lt2942_get_status
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1030 lt2942_get_status:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1031 bcf battery_gauge_available ; clear flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1032 movlw 0x00 ; point to status register
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1033 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1034 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1035 movff SSP1BUF,WREG ; copy received byte to WREG
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1036 btfss WREG,7 ; 2942 found?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1037 bsf battery_gauge_available ; YES - set flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1038 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1039 bra WaitMSSP ; wait for TX to complete and return
113
heinrichsweikamp
parents: 20
diff changeset
1040
heinrichsweikamp
parents: 20
diff changeset
1041
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1042 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1043 ; Read Gauge IC - Voltage
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1044 ;
113
heinrichsweikamp
parents: 20
diff changeset
1045 global lt2942_get_voltage
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1046 lt2942_get_voltage:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1047 movlw 0x08 ; point to voltage registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1048 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1049 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1050 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1051 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1052 movff SSP1BUF,xA+1 ; copy received byte to xA+1
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1053 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1054 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1055 movff SSP1BUF,xA+0 ; copy received byte to xA+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1056 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1057 rcall WaitMSSP ; wait for TX to complete
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1058
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1059 ; convert voltage from raw value to Volt
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1060 MOVLI .6000,xB ; load conversion multiplicand into xB
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1061 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1062 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1063 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
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1064 movff xC+3,batt_voltage+1 ; ...
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1065
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1066 tstfsz batt_voltage+1 ; < 256 mV ?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1067 return ; NO - done
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1068 bra lt2942_init ; YES - initialize gauge and return
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1069
615
b87f23fae743 work on new battery menu
heinrichsweikamp
parents: 608
diff changeset
1070
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1071 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1072 ; Read Gauge IC - Temperature
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1073 ;
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1074 global lt2942_get_temperature
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1075 lt2942_get_temperature: ; read battery temperature
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1076 movlw 0x0C ; point to temperature register
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1077 call I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1078 call I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1079 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1080 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1081 movff SSP1BUF,xA+1 ; store raw temperature, high byte
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1082 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1083 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1084 movff SSP1BUF,xA+0 ; store raw temperature, low byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1085 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1086 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1087
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1088 ; convert temperature from raw value to Kelvin
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1089 MOVLI .6000,xB ; load conversion multiplicand into xB
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1090 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1091 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 %
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1092 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
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1093 movff xC+3,battery_temperature+1 ; ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1094
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1095 ; check if battery is being charged right now
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1096 btfss cc_active ; in CC charging mode?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1097 return ; NO - not charging, done
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1098
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1099 ; ignore false readings (>125°C)
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1100 movlw LOW .3307
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1101 movwf sub_a+0
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1102 movlw HIGH .3307
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1103 movwf sub_a+1
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1104 MOVII battery_temperature, sub_b
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1105 call cmpU16 ; sub_a - sub_b (with UNSIGNED values)
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1106 btfsc neg_flag ; result negative?
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1107 return ; YES - temperature > 125°C, not possible here. Skip test.
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1108
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1109 ; check for over-temperature while charging
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1110 MOVLI max_battery_charge_temp,sub_a
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1111 MOVII battery_temperature, sub_b
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1112 call cmpU16 ; sub_a - sub_b (with UNSIGNED values)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1113 btfss neg_flag ; result negative?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1114 return ; NO - temperature <= threshold, ok, done
640
8c1f1f334275 3.13 release
heinrichsweikamp
parents: 634
diff changeset
1115 return
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1116 ; YES - too hot, disable charging circuitry
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1117 bsf charge_disable ; - set charging-inhibit signal
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1118 bcf charge_enable ; - activate charging-inhibit signal
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1119 bsf battery_overtemp ; - flag that the battery charging over-temperature protection has tripped
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1120 return ; - done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1121
113
heinrichsweikamp
parents: 20
diff changeset
1122
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1123 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1124 ; Read Gauge IC - Read State of Charge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1125 ;
113
heinrichsweikamp
parents: 20
diff changeset
1126 global lt2942_get_accumulated_charge
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1127 lt2942_get_accumulated_charge: ; read accumulated charge and compute percent
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1128 movlw 0x00 ; point to status register
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1129 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1130 rcall I2C_RX_GAUGE ; receive byte from the LT2942 Gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1131 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1132 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1133 movff SSP1BUF,gauge_status_byte ; copy received byte to gauge_status_byte
466
7f4c3b2e6bb7 battery gauge handling
heinrichsweikamp
parents: 449
diff changeset
1134
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1135 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1136 rcall WaitMSSP ; wait for TX to complete ; dummy read (control byte)
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1137 movf SSP1BUF,W ; dump to WREG
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1138 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1139 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1140
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1141 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1142 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1143 movff SSP1BUF,sub_a+1 ; copy received byte to sub_a+1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1144 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1145 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1146
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1147 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1148 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1149 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1150 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1151 rcall WaitMSSP ; wait for TX to complete
113
heinrichsweikamp
parents: 20
diff changeset
1152
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1153 btfsc gauge_status_byte,0 ; UVLO event ?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1154 rcall lt2942_init_again ; YES - do an re-initialization
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1155
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1156 MOVII sub_a,battery_accumulated_charge ; save raw value
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1157
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1158 ; Compute batt_percent = (charge - battery_offset) / 365
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1159 MOVII battery_offset,sub_b ; get battery offset
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1160 call subU16 ; sub_c = sub_a - sub_b (with signed values)
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1161 clrf batt_percent ; default batt_percent to zero
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1162 btfsc neg_flag ; result negative?
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1163 bra lt2942_set_to_zero_percent ; YES - keep LT2942 at zero percent and return
113
heinrichsweikamp
parents: 20
diff changeset
1164
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1165 ; > zero, set batt_percent properly
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1166 MOVII sub_c,xA ; copy net charge to xA
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1167 MOVII battery_capacity,xB ; get battery capacity into xB
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1168 call div16x16 ; xC = xA / xB with xA as remainder
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1169 movff xC+0,batt_percent ; result is battery percentage
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1170 movlw .100 ; max. value is 100 %
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1171 cpfslt batt_percent ; batt_percent < 100 % ?
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1172 movwf batt_percent ; NO - limit to 100 %
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1173 return ; done
113
heinrichsweikamp
parents: 20
diff changeset
1174
449
heinrichsweikamp
parents: 448
diff changeset
1175 lt2942_set_to_zero_percent:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1176 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1177 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1178 movff battery_offset+1,SSP1BUF ; send battery offset, high byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1179 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1180 rcall I2C_Check_ACK ; check for acknowledge by receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1181 movff battery_offset+0,SSP1BUF ; send battery offset, low byte
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1182 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1183 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1184 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1185 bra WaitMSSP ; wait for TX to complete and return
449
heinrichsweikamp
parents: 448
diff changeset
1186
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1187
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1188 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1189 ; Read Gauge IC - Reset Accumulating Register to 0xFFFF
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1190 ;
113
heinrichsweikamp
parents: 20
diff changeset
1191 global lt2942_charge_done
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1192 lt2942_charge_done:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1193 movlw 0x02 ; point to accumulated charge registers
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1194 rcall I2C_TX_GAUGE ; send byte to the LT2942 gauge IC
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1195 setf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1196 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1197 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1198 setf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1199 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1200 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1201 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1202 bra WaitMSSP ; wait for TX to complete and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1203
113
heinrichsweikamp
parents: 20
diff changeset
1204
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1205 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1206 ; Helper Function - send 1 Byte to the LT2942 Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1207 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1208 I2C_TX_GAUGE:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1209 movwf i2c_temp2 ; save data byte to be sent
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1210 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1211 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1212 movlw b'11001000' ; address byte + Write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1213 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1214 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1215 rcall I2C_Check_ACK ; check for acknowledge by receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1216 movf i2c_temp2,W ; restore data byte to be sent
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1217 bra I2C_TX ; send byte and return
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1218
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1219
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1220 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1221 ; Helper Function - receive 1 Byte from the LT2942 Gauge IC
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1222 ;
113
heinrichsweikamp
parents: 20
diff changeset
1223 I2C_RX_GAUGE:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1224 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1225 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1226 movlw b'11001001' ; address byte + Read bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1227 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1228 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1229 rcall I2C_Check_ACK ; check for acknowledge by receiver
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1230 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1231 bra WaitMSSP ; wait for reception and return
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1232
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1233
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1234 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1235 ; Reset Hardware and Software Battery Gauge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1236 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1237 ; called from comm.asm and menu_tree.asm
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1238 ;
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1239 global reset_battery_gauge_and_lt2942
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1240 reset_battery_gauge_and_lt2942:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1241 btfsc battery_gauge_available ; battery gauge chip available?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1242 call lt2942_charge_done ; YES - reset meter to 0xFFFF
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1243 ;bra reset_battery_gauge ; continue resetting gauge registers
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1244
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1245
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1246 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1247 ; Reset Software Battery Gauge
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1248 ;
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1249 global reset_battery_gauge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1250 reset_battery_gauge:
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1251 bsf block_battery_gauge ; suspend ISR from accessing the battery registers
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1252 movlw .100 ; set battery level to 100%
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1253 movwf batt_percent ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1254 banksel battery_gauge ; select bank ISR data
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1255 clrf battery_gauge+0 ; null the battery registers
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1256 clrf battery_gauge+1 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1257 clrf battery_gauge+2 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1258 clrf battery_gauge+3 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1259 clrf battery_gauge+4 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1260 clrf battery_gauge+5 ; ...
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1261 banksel common ; back to bank common
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1262 goto eeprom_battery_gauge_write ; update battery registers in EEPROM, unblock ISR and return
631
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1263
185ba2f91f59 3.09 beta 1 release
heinrichsweikamp
parents: 628
diff changeset
1264
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1265
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1266 IFDEF _rx_functions
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1267
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1268 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1269 ; OSTC TR - probe if TR Module available
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1270 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1271 global I2C_probe_OSTC_rx
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1272 I2C_probe_OSTC_rx:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1273 bcf ostc_rx_present ; default to no TR module available
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1274 clrf WREG ; bank-safe set to zero of ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1275 movff WREG,rx_firmware_cur_major ; ... current TR module firmware, major
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1276 movff WREG,rx_firmware_cur_minor ; ... current TR module firmware, minor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1277 movlw .5 ; max number of tries for detecting a TR module
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1278 movwf hy ; initialize loop counter for tries
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1279 I2C_probe_OSTC_rx_1:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1280 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1281 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1282 movlw 0x50 ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1283 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1284 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1285 btfss SSP1CON2,ACKSTAT ; ACK received?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1286 bsf ostc_rx_present ; YES - TR module detected
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1287 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1288 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1289 btfss ostc_rx_present ; was a TR module detected?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1290 return ; NO - done
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1291
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1292 WAITMS .1 ; wait 1 ms
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1293
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1294 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1295 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1296 movlw 0x50 ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1297 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1298 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1299 rcall I2C_Check_ACK ; check for acknowledge by receiver
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1300 movlw 0x1B ; command: get firmware
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1301 movwf SSP1BUF ; send command
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1302 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1303 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1304 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1305 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1306
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1307 WAITMS .1 ; wait 1 ms
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1308
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1309 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1310 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1311 movlw 0x51 ; address byte + Read bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1312 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1313 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1314 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1315 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1316 movff SSP1BUF,rx_firmware_cur_major ; store as firmware version, major
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1317 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1318 rcall WaitMSSP ; wait for TX to complete
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1319
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1320 ; last byte in read from RX circuity always with a NACK!
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1321 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1322 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1323 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1324 bsf SSP1CON2,ACKDT ; set ACKDT flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1325 bsf SSP1CON2,ACKEN ; master NOT acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1326 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1327 bcf SSP1CON2,ACKDT ; reset ACKDT flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1328 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1329 rcall WaitMSSP ; wait for TX to complete
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1330
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1331 ; wait for TR module becoming ready
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1332 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1333 movlw .147 ; code for not ready, minor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1334 cpfseq i2c_temp1 ; equal?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1335 return ; NO - TR module ready, done
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1336 movff rx_firmware_cur_major,i2c_temp1 ; YES - copy major firmware version to bank common
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1337 movlw .27 ; - code for not ready, major
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1338 cpfseq i2c_temp1 ; - equal?
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1339 return ; NO - TR module ready, done
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1340 bsf active_reset_ostc_rx ; YES - apply reset to TR module
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1341 WAITMS .5 ; - wait 5 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1342 bcf active_reset_ostc_rx ; - release reset
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1343 WAITMS .250 ; - wait for 250 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1344 WAITMS .250 ; - wait another 250 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1345 decfsz hy,F ; - decrement counter for number of tries, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1346 bra I2C_probe_OSTC_rx_1 ; - NO - try again
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1347 bcf ostc_rx_present ; - YES - something is wrong, flag TR module as not available
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1348 return ; - done
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1349
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1350
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1351 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1352 ; OSTC TR - get Tank Data
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1353 ;
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1354 global I2C_get_tankdata
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1355 I2C_get_tankdata:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1356 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1357 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1358 movlw 0x50 ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1359 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1360 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1361 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1362 movlw 0x1E ; read buffer2 (48 bytes)
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1363 movwf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1364 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1365 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1366 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1367 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1368
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1369 WAITMS .1 ; wait 1 ms
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1370
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1371 ; read 48 bytes
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1372 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1373 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1374 movlw 0x51 ; address byte + read bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1375 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1376 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1377 rcall I2C_Check_ACK ; check for acknowledge by receiver
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1378 movlw .47 ; 47 with ACK + 1 w/o ACK
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1379 movwf i2c_temp2 ; initialize loop counter
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1380 lfsr FSR2,rx_buffer ; point to start of rx data buffer
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1381 I2C_get_tankdata_loop_read:
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1382 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1383 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1384 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1385 bcf SSP1CON2,ACKDT ; reset ACKDT flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1386 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1387 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1388 decfsz i2c_temp2,F ; decrement loop counter, done?
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1389 bra I2C_get_tankdata_loop_read ; NO - loop
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1390 ; read last byte without ACK
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1391 bsf SSP1CON2,RCEN ; enable receive mode
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1392 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1393 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1394 bsf SSP1CON2,ACKDT ; set ACKDT flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1395 bsf SSP1CON2,ACKEN ; master NOT acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1396 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1397 bcf SSP1CON2,ACKDT ; reset ACKDT flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1398 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1399 bra WaitMSSP ; wait for TX to complete and return
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1400
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1401
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1402 ;-----------------------------------------------------------------------------
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1403 ; OSTC TR - Firmware Update
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1404 ;
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1405 IFDEF _rx_update
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1406
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1407 global I2C_update_OSTC_rx
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1408 I2C_update_OSTC_rx: ; transfer 64 byte to RX co-processor
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1409 ; setup for write
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1410 bcf i2c_error_flag ; clear error flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1411 lfsr FSR2,buffer ; initialize pointer to send buffer used for verify
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1412 movlw .64 ; initialize loop counter: 64 byte with ACK
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1413 movwf i2c_temp2 ; ...
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1414 ; address write
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1415 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1416 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1417 movlw 0x50 ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1418 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1419 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1420 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1421 ; write 64 bytes
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1422 I2C_update_OSTC_loop:
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1423 TBLRD*+ ; read a byte from program memory
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1424 movff TABLAT,POSTINC2 ; copy to send buffer
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1425 movff TABLAT,SSP1BUF ; copy to I2C data buffer
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1426 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1427 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1428 decfsz i2c_temp2,F ;decrement loop counter, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1429 bra I2C_update_OSTC_loop ; NO - loop
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1430 bsf SSP1CON2,PEN ; YES - stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1431 rcall WaitMSSP ; - wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1432 WAITMS .1 ; - wait another 1 ms
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1433 ; setup for read-back
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1434 lfsr FSR2,buffer ; reset pointer to begin of send buffer
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1435 movlw .63 ; initialize loop counter: 63 byte with ACK + 1 w/o ACK
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1436 movwf i2c_temp2 ; ...
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1437 ; address read-back
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1438 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1439 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1440 movlw 0x51 ; address byte + read bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1441 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1442 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1443 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1444 ; read-back 64 bytes
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1445 I2C_update_OSTC_loop_read:
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1446 bsf SSP1CON2,RCEN ; enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1447 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1448 movf SSP1BUF,W ; copy received byte to WREG
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1449 cpfseq POSTINC2 ; compare read-back byte with sent byte, equal?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1450 bsf i2c_error_flag ; NO - not equal, set error flag
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1451 bcf SSP1CON2,ACKDT ; reset ACKDT flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1452 bsf SSP1CON2,ACKEN ; master acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1453 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1454 decfsz i2c_temp2,F ; decrement loop counter, became zero?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1455 bra I2C_update_OSTC_loop_read ; NO - loop
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1456 ; 1 w/o ACK
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1457 bsf SSP1CON2, RCEN ; YES - enable receive mode
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1458 rcall WaitMSSP ; - wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1459 movf SSP1BUF,W ; - get 64th byte
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1460 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1461 bsf i2c_error_flag ; NO - not equal, set error flag
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1462 bsf SSP1CON2,ACKDT ; - set ACKDT flag
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1463 bsf SSP1CON2,ACKEN ; - master NOT acknowledge
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1464 rcall WaitMSSP ; - wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1465 bcf SSP1CON2,ACKDT ; - reset ACKDT flag
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1466 ; stop
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1467 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1468 rcall WaitMSSP ; wait for TX to complete
560
b7eb98dbd800 bump to 2.96beta (REFACTORED VERSION)
heinrichsweikamp
parents: 556
diff changeset
1469 WAITMS .1
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1470 ; address commit
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1471 bsf SSP1CON2,SEN ; start condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1472 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1473 movlw 0x50 ; address byte + write bit
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1474 movwf SSP1BUF ; control byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1475 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1476 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1477 movlw 0x1F ; write command
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1478 movwf SSP1BUF ; data byte
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1479 rcall WaitMSSP ; wait for TX to complete
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1480 rcall I2C_Check_ACK ; check for acknowledge by receiver
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1481 bsf SSP1CON2,PEN ; stop condition
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1482 rcall WaitMSSP ; wait for TX to complete
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1483 WAITMS .5 ; required waiting time
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1484 ; error check
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1485 btfss i2c_error_flag ; did an error occur?
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1486 retlw .0 ; NO - data transfered successfully
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1487 retlw .255 ; YES - error in data transfer occurred
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1488
628
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1489 ENDIF ; _rx_update
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1490
cd58f7fc86db 3.05 stable work
heinrichsweikamp
parents: 627
diff changeset
1491 ENDIF ; _rx_functions
604
ca4556fb60b9 bump to 2.99beta, work on 3.00 stable
heinrichsweikamp
parents: 582
diff changeset
1492
634
4050675965ea 3.10 stable release
heinrichsweikamp
parents: 631
diff changeset
1493 ;-----------------------------------------------------------------------------
623
c40025d8e750 3.03 beta released
heinrichsweikamp
parents: 621
diff changeset
1494
582
b455b31ce022 work on 2.97 stable
heinrichsweikamp
parents: 574
diff changeset
1495 END