forked from hanyazou/BMI160-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBMI160Gen.cpp
More file actions
205 lines (181 loc) · 4.31 KB
/
BMI160Gen.cpp
File metadata and controls
205 lines (181 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "BMI160Gen.h"
#include "SPI.h"
//#define DEBUG
bool BMI160GenClass::begin(const int spi_cs_pin, const int intr_pin)
{
return begin(SPI_MODE, Wire, spi_cs_pin, intr_pin);
}
bool BMI160GenClass::begin(Mode mode, TwoWire &wirePort,const int arg1, const int arg2)
{
this->mode = mode;
_i2cPort = &wirePort;
switch (this->mode) {
case INVALID_MODE:
return false;
case I2C_MODE:
i2c_addr = arg1;
break;
case SPI_MODE:
spi_ss = arg1;
break;
default:
return false;
}
if (0 <= arg2) {
interrupt_pin = digitalPinToInterrupt(arg2);
#ifdef DEBUG
Serial.print("BMI160GenClass::begin(): pin#=");
Serial.print(arg2);
Serial.print(" -> interrupt=");
Serial.println(interrupt_pin);
#endif
}
return CurieIMUClass::begin();
}
void BMI160GenClass::attachInterrupt(void (*callback)(void))
{
CurieIMUClass::attachInterrupt(NULL);
if (0 <= interrupt_pin) {
::attachInterrupt(interrupt_pin, callback, FALLING);
} else {
Serial.println("BMI160GenClass::attachInterrupt: No valid interruption pin.");
}
}
void BMI160GenClass::ss_init()
{
switch (this->mode) {
case I2C_MODE:
i2c_init();
break;
case SPI_MODE:
spi_init();
break;
default:
break;
}
}
int BMI160GenClass::ss_xfer(uint8_t *buf, unsigned tx_cnt, unsigned rx_cnt)
{
switch (this->mode) {
case I2C_MODE:
return i2c_xfer(buf, tx_cnt, rx_cnt);
case SPI_MODE:
if (rx_cnt) /* For read transfers, assume 1st byte contains register address */
buf[0] |= (1 << BMI160_SPI_READ_BIT);
return spi_xfer(buf, tx_cnt, rx_cnt);
default:
return 0;
}
}
void BMI160GenClass::i2c_init()
{
#ifdef DEBUG
Serial.println("BMI160GenClass::i2c_init()...");
#endif // DEBUG
//_i2cPort->begin();
_i2cPort->beginTransmission(i2c_addr);
if( _i2cPort->endTransmission() != 0 )
Serial.println("BMI160GenClass::i2c_init(): I2C failed.");
#ifdef DEBUG
int id = getDeviceID();
Serial.print("BMI160GenClass::i2c_init(): CHIP ID = 0x");
Serial.println(id, HEX);
Serial.println("BMI160GenClass::i2c_init()...done");
#endif // DEBUG
}
int BMI160GenClass::i2c_xfer(uint8_t *buf, unsigned tx_cnt, unsigned rx_cnt)
{
uint8_t *p;
#ifdef DEBUG
Serial.print("i2c_xfer(offs=0x");
Serial.print(*buf, HEX);
Serial.print(", tx=");
Serial.print(tx_cnt);
Serial.print(", rx=");
Serial.print(rx_cnt);
Serial.print("):");
#endif // DEBUG
_i2cPort->beginTransmission(i2c_addr);
p = buf;
while (0 < tx_cnt) {
tx_cnt--;
_i2cPort->write(*p++);
}
if( _i2cPort->endTransmission() != 0 ) {
Serial.println("Wire.endTransmission() failed.");
}
if (0 < rx_cnt) {
_i2cPort->requestFrom(i2c_addr, rx_cnt);
p = buf;
while ( _i2cPort->available() && 0 < rx_cnt) {
rx_cnt--;
#ifdef DEBUG
int t = *p++ = _i2cPort->read();
Serial.print(" ");
Serial.print(t, HEX);
#else
*p++ = _i2cPort->read();;
#endif // DEBUG
}
}
#ifdef DEBUG
Serial.println("");
#endif // DEBUG
return (0);
}
void BMI160GenClass::spi_init()
{
#ifdef DEBUG
Serial.println("BMI160GenClass::spi_init()...");
#endif // DEBUG
// start the SPI library:
SPI.begin();
if (0 <= spi_ss) {
pinMode(spi_ss, OUTPUT);
} else {
Serial.println("BMI160GenClass::spi_init(): WARNING: No chip select pin specified.");
}
#ifdef DEBUG
Serial.println("BMI160GenClass::spi_init()...done");
#endif // DEBUG
}
int BMI160GenClass::spi_xfer(uint8_t *buf, unsigned tx_cnt, unsigned rx_cnt)
{
uint8_t *p;
#ifdef DEBUG
Serial.print("BMI160GenClass::spi_xfer(");
Serial.print((unsigned long)buf, HEX);
Serial.print("=");
Serial.print(*buf, HEX);
Serial.print(",");
Serial.print(tx_cnt);
Serial.print(",");
Serial.print(rx_cnt);
Serial.print("):");
#endif // DEBUG
if (0 <= spi_ss)
digitalWrite(spi_ss, LOW);
p = buf;
while (0 < tx_cnt) {
tx_cnt--;
SPI.transfer(*p++);
}
p = buf;
while (0 < rx_cnt) {
rx_cnt--;
#ifdef DEBUG
int t = *p++ = SPI.transfer(0);
Serial.print(" ");
Serial.print(t, HEX);
#else
*p++ = SPI.transfer(0);
#endif // DEBUG
}
if (0 <= spi_ss)
digitalWrite(spi_ss, HIGH);
#ifdef DEBUG
Serial.println("");
#endif // DEBUG
return (0);
}
BMI160GenClass BMI160;