@@ -16,13 +16,147 @@ You should have received a copy of the GNU Lesser General Public
16
16
License along with this library; if not, write to the Free Software
17
17
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
18
*/
19
+
19
20
#include < Arduino.h>
21
+ // #include "../../../../arduino/samd/libraries/Wire/Wire.h"
22
+ #include " WireIoExt.h"
23
+
24
+ static const uint32_t TWI_CLOCK_SME = 100000 ;
25
+
26
+ #define HTS221_ADDRESS 0x5F
27
+ // Define a few of the registers that we will be accessing on the HTS221
28
+ #define WHO_AM_I 0x0F
29
+ #define WHO_AM_I_RETURN 0xBC // This read-only register contains the device identifier, set to BCh
30
+
31
+
32
+ #define TCA6416A_ADDRESS 0x20
33
+ #define CONF_PORT_0 0b01111111
34
+ #define INIT_P0 0b10000000
35
+
36
+ #define CONF_PORT_1 0b00110101
37
+ #define INIT_P1 0b11001010
38
+ #define VALUE_GPS_FORCE_ON 0b10001010
39
+
40
+ // Protocol PowerOn Default
41
+ #define INPUT_PORT_0 0x00 // Read byte xxxx xxxx (undefined)
42
+ #define INPUT_PORT_1 0x01 // Read byte xxxx xxxx
43
+ #define OUTPUT_PORT_0 0x02 // Read/write byte 1111 1111
44
+ #define OUTPUT_PORT_1 0x03 // Read/write byte 1111 1111
45
+ #define P_INVERT_PORT_0 0x04 // Read/write byte 0000 0000
46
+ #define P_INVERT_PORT_1 0x05 // Read/write byte 0000 0000
47
+ #define CONFIG_PORT_0 0x06 // Read/write byte 1111 1111
48
+ #define CONFIG_PORT_1 0x07 // Read/write byte 1111 1111
49
+
50
+
51
+ #define _9AX_INT2_A_G_PIN 0x1
52
+ #define _9AX_INT1_A_G_PIN 0x2
53
+ #define _9AX_INT_M_PIN 0x4
54
+ #define _9AX_DRDY_M_PIN 0x8
55
+ #define PRE_INT_PIN 0x10
56
+ #define ALS_GPIO0_PIN 0x20
57
+ #define ALS_GPIO1_PIN 0x40
58
+ #define BLE_RESET_PIN 0x80
59
+
60
+ #define SFX_STDBY_STS_PIN 0x1
61
+ #define SFX_RESET_PIN 0x2
62
+ #define SFX_RADIO_STS_PIN 0x4
63
+ #define SFX_WAKEUP_PIN 0x8
64
+ #define NFC_FD_PIN 0x10
65
+ #define HUT_DRDY_PIN 0x20
66
+ #define GPS_FORCE_ON_PIN 0x40
67
+ #define GPS_RESET_PIN 0x80
68
+
69
+
70
+ static byte readRegister (byte _address, byte regToRead)
71
+ {
72
+ WireTemp.beginTransmission (_address);
73
+ WireTemp.write (regToRead);
74
+ WireTemp.endTransmission (false ); // endTransmission but keep the connection active
75
+
76
+ WireTemp.requestFrom (_address, 1 ); // Ask for 1 byte, once done, bus is released by default
77
+
78
+ while (!WireTemp.available ()) ; // Wait for the data to come back
79
+ return WireTemp.read (); // Return this one byte
80
+ }
81
+
82
+ // Writes a single byte (dataToWrite) into regToWrite
83
+ static bool writeRegister (byte _address, byte regToWrite, byte dataToWrite)
84
+ {
85
+ WireTemp.beginTransmission (_address);
86
+
87
+ if (!WireTemp.write (regToWrite)) {
88
+ return false ;
89
+ }
90
+ if (!WireTemp.write (dataToWrite)) {
91
+ return false ;
92
+ }
93
+
94
+ uint8_t errorNo = WireTemp.endTransmission (); // Stop transmitting
95
+ return (errorNo == 0 );
96
+ }
97
+
98
+ uint8_t resetsDbg[2 ];
99
+ uint8_t actual[2 ];
100
+
101
+ uint8_t smeInitError=0xFF ;
102
+ static void ioExtenderInit (void ) {
103
+ WireTemp.begin ();
104
+
105
+
106
+ // first test the I2C bus
107
+ uint8_t data = readRegister (HTS221_ADDRESS, WHO_AM_I);
108
+ if (data == WHO_AM_I_RETURN){
109
+ smeInitError = 0 ;
110
+
111
+ if (writeRegister ( TCA6416A_ADDRESS, CONFIG_PORT_0, CONF_PORT_0)!=false ) {
112
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0, INIT_P0); // keep the BLE reset High
113
+ }
114
+
115
+ if (writeRegister ( TCA6416A_ADDRESS, CONFIG_PORT_1, CONF_PORT_1)!=false ){
116
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1, INIT_P1); // keep the resets High
117
+ }
118
+
119
+ // wait a while
120
+ delay (20 );
121
+ resetsDbg[0 ]=0 ;
122
+ resetsDbg[1 ]=0 ;
123
+
124
+
125
+ actual[0 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0);
126
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0, (actual[0 ]&~BLE_RESET_PIN));
127
+ resetsDbg[0 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0);
128
+
129
+ if ((actual[1 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1)) != false ) {
130
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1, (actual[1 ]&(~GPS_RESET_PIN) &(~SFX_WAKEUP_PIN)));
131
+ resetsDbg[1 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1);
132
+ }
133
+
134
+ // wait a while
135
+ delay (20 );
136
+
137
+ // move the reset high
138
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0, actual[0 ]);
139
+ resetsDbg[0 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_0);
140
+
141
+ writeRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1, actual[1 ]);
142
+ resetsDbg[1 ] = readRegister ( TCA6416A_ADDRESS, OUTPUT_PORT_1);
143
+
144
+ } else {
145
+
146
+ smeInitError = 1 ;
147
+ }
148
+
149
+ }
150
+
20
151
21
152
void initVariant () {
22
153
// reset the I/O Extender
23
154
pinMode (PIN_IO_EXT_RST, OUTPUT);
24
155
digitalWrite (PIN_IO_EXT_RST, LOW);
25
156
delay (10 ); // just wait a while
26
- digitalWrite (PIN_IO_EXT_RST, HIGH);
27
- }
157
+ digitalWrite (PIN_IO_EXT_RST, HIGH);
28
158
159
+
160
+ // initialize the IO_Extender
161
+ ioExtenderInit ();
162
+ }
0 commit comments