-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLSM9DS1.cpp
370 lines (309 loc) · 9.03 KB
/
LSM9DS1.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// LSM9DS1 Driver
// Emmanuel FLETY - March 2017
#include <energia.h>
#include <SPI.h>
#include <Wire.h>
#include "common.h"
#include "LSM9DS1.h"
extern Word AccelerationX;
extern Word AccelerationY;
extern Word AccelerationZ;
extern Word GyroscopeX;
extern Word GyroscopeY;
extern Word GyroscopeZ;
extern Word MagnetometerX;
extern Word MagnetometerY;
extern Word MagnetometerZ;
extern Word Temperature;
extern byte CommunicationMode;
unsigned char CheckWhoAmI(unsigned char Byte)
{
switch(Byte)
{
case WHO_AM_I_TOKEN:
return(1);
default:
return(0);
}
}
void InitLSM9DS1(void)
{
// We initialize the sensors (accel + gyro) in normal mode to get the proper
// 16 bit resolution
// We use an ODR > 400 Hz as we used to sample the motion sensor at 2ms (<=> 500 Hz)
// All axis X,Y,Z enabled
// TODO : set sensitivity back to +- 8G, 2 gaus
// Gyroscope configuration
WriteAccLSM9DS1(CTRL_REG1_G, 0b10111000); // 476 Hz ODR + 100 Hz cut-off / 2000°/s
WriteAccLSM9DS1(CTRL_REG2_G, 0b00000000); // HPF & LPF2 Filters bypassed
WriteAccLSM9DS1(CTRL_REG3_G, 0b00000000);
WriteAccLSM9DS1(CTRL_REG4_G, 0b00111000); // Gyro axis enable
// Accelerometer configuration
WriteAccLSM9DS1(CTRL_REG5_XL, 0b00111000); // Accel axis enable
//WriteAccLSM9DS1(CTRL_REG6_XL, 0b00010000); // ODR = Gyro ODR / +-4g / LP BW based on ODR = 211 Hz
WriteAccLSM9DS1(CTRL_REG6_XL, 0b00011000); // ODR = Gyro ODR / +-8g / LP BW based on ODR = 211 Hz
WriteAccLSM9DS1(CTRL_REG8, 0b01000100); // BDU on / 4-wire SPI / Auto INC
WriteAccLSM9DS1(CTRL_REG9, 0b00000100); // SPI only / I2C disable
// Magneto configuration
WriteMagLSM9DS1(CTRL_REG1_M, 0b11010100); // Temp comp. EN / Hi Perf / 20 Hz
WriteMagLSM9DS1(CTRL_REG2_M, 0b00000000); // +- 4 gauss
WriteMagLSM9DS1(CTRL_REG3_M, 0b10000000); // I2C dis. / SPI R+W / continuous output
WriteMagLSM9DS1(CTRL_REG4_M, 0b00001000); // Z axis Hi Perf
WriteMagLSM9DS1(CTRL_REG5_M, 0b00000000); // Block update BDU Off
}
void RebootLSM9DS1(void)
{
WriteAccLSM9DS1(CTRL_REG8,0b01000101); // BIT 0 = SOFT RESET
delay(10); // wait reboot finished
}
void WriteAccLSM9DS1(unsigned char RegAddress, unsigned char Data)
{
if(CommunicationMode == SPI_MODE)
{
RegAddress = RegAddress & 0b01111111; // Write and MS bit = 0, no auto-increment
// Asserting the chip
digitalWrite(ACC_CS, LOW);
SPI.transfer(RegAddress);
SPI.transfer(Data);
digitalWrite(ACC_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_ACCELGYRO);
Wire.write(RegAddress);
Wire.write(Data);
Wire.endTransmission();
}
}
// Single Byte read routine
unsigned char ReadAccLSM9DS1(unsigned char RegAddress)
{
unsigned char DataLSM9DS1;
if(CommunicationMode == SPI_MODE)
{
RegAddress = RegAddress | 0b10000000; // Read bit =1 and MS bit = 0 (no auto-increment)
// Asserting the chip
digitalWrite(ACC_CS, LOW);
SPI.transfer(RegAddress);
DataLSM9DS1 = SPI.transfer(0xFF); // dummy write
digitalWrite(ACC_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_MAG);
Wire.write(RegAddress);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_MAG, 1);
DataLSM9DS1 = Wire.read();
}
return(DataLSM9DS1);
}
void WriteMagLSM9DS1(unsigned char RegAddress, unsigned char Data)
{
if(CommunicationMode == SPI_MODE)
{
RegAddress = RegAddress & 0b01111111; // Write and MS bit = 0, no auto-increment
// Asserting the chip
digitalWrite(MAG_CS, LOW);
SPI.transfer(RegAddress);
SPI.transfer(Data);
digitalWrite(MAG_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_MAG);
Wire.write(RegAddress);
Wire.write(Data);
Wire.endTransmission();
}
}
// Single Byte read routine
unsigned char ReadMagLSM9DS1(unsigned char RegAddress)
{
unsigned char DataLSM9DS1;
if(CommunicationMode == SPI_MODE)
{
RegAddress = RegAddress | 0b10000000; // Read bit =1 and MS bit = 0 (no auto-increment)
// Asserting the chip
digitalWrite(MAG_CS, LOW);
SPI.transfer(RegAddress);
DataLSM9DS1 = SPI.transfer(0xFF); // dummy write
digitalWrite(MAG_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_MAG);
Wire.write(RegAddress);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_MAG, 1);
DataLSM9DS1 = Wire.read();
}
return(DataLSM9DS1);
}
void ReadAccel(void)
{
unsigned char LSB, MSB;
if(CommunicationMode == SPI_MODE)
{
// Asserting the chip
digitalWrite(ACC_CS, LOW);
SPI.transfer(OUT_X_L_A | READ_AND_AUTOINCREMENT);
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
// We swap X and Y based on the new chip (LS1) vs. the former one (LS0)
// and chip orientation on PCB.
// In addition, axis orientation is inverted
// Could use a axis order swapper + Sign
// Orientation of the chip is 3x mom. switches / led down, power switch / antenna up
AccelerationY.Val[0] = LSB;
AccelerationY.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
AccelerationX.Val[0] = LSB;
AccelerationX.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
AccelerationZ.Val[0] = LSB;
AccelerationZ.Val[1] = MSB;
digitalWrite(ACC_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_ACCELGYRO);
Wire.write(OUT_X_L_A | JUST_READ);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_ACCELGYRO, 6);
LSB = Wire.read();
MSB = Wire.read();
AccelerationY.Val[0] = LSB;
AccelerationY.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
AccelerationX.Val[0] = LSB;
AccelerationX.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
AccelerationZ.Val[0] = LSB;
AccelerationZ.Val[1] = MSB;
}
// Sign
AccelerationX.Value = -AccelerationX.Value;
AccelerationY.Value = -AccelerationY.Value;
}
void ReadGyro(void)
{
unsigned char LSB, MSB;
if(CommunicationMode == SPI_MODE)
{
// Asserting the chip
digitalWrite(ACC_CS, LOW);
SPI.transfer(OUT_X_L_G | READ_AND_AUTOINCREMENT);
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
GyroscopeY.Val[0] = LSB;
GyroscopeY.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
GyroscopeX.Val[0] = LSB;
GyroscopeX.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
GyroscopeZ.Val[0] = LSB;
GyroscopeZ.Val[1] = MSB;
digitalWrite(ACC_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_ACCELGYRO);
Wire.write(OUT_X_L_G | JUST_READ);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_ACCELGYRO, 6);
LSB = Wire.read();
MSB = Wire.read();
GyroscopeY.Val[0] = LSB;
GyroscopeY.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
GyroscopeX.Val[0] = LSB;
GyroscopeX.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
GyroscopeZ.Val[0] = LSB;
GyroscopeZ.Val[1] = MSB;
}
// Sign
GyroscopeY.Value = -GyroscopeY.Value;
GyroscopeX.Value = -GyroscopeX.Value;
}
void ReadMagneto(void)
{
unsigned char LSB, MSB;
if(CommunicationMode == SPI_MODE)
{
// Asserting the chip
digitalWrite(MAG_CS, LOW);
SPI.transfer(OUT_X_L_M | MAG_READ_AND_AUTOINCREMENT);
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
MagnetometerY.Val[0] = LSB;
MagnetometerY.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
MagnetometerX.Val[0] = LSB;
MagnetometerX.Val[1] = MSB;
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
MagnetometerZ.Val[0] = LSB;
MagnetometerZ.Val[1] = MSB;
digitalWrite(MAG_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_ACCELGYRO);
Wire.write(OUT_X_L_M | JUST_READ);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_ACCELGYRO, 6);
LSB = Wire.read();
MSB = Wire.read();
MagnetometerY.Val[0] = LSB;
MagnetometerY.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
MagnetometerX.Val[0] = LSB;
MagnetometerX.Val[1] = MSB;
LSB = Wire.read();
MSB = Wire.read();
MagnetometerZ.Val[0] = LSB;
MagnetometerZ.Val[1] = MSB;
}
// Sign
MagnetometerX.Value = -MagnetometerX.Value;
MagnetometerZ.Value = -MagnetometerZ.Value;
}
void ReadTemperature(void)
{
unsigned char LSB, MSB;
// Temperature 12 bit / 8 LSB per degre centigrade
if(CommunicationMode == SPI_MODE)
{
// Asserting the chip
digitalWrite(ACC_CS, LOW);
SPI.transfer(OUT_TEMP_L | READ_AND_AUTOINCREMENT);
LSB = SPI.transfer(0xFF);
MSB = SPI.transfer(0xFF);
Temperature.Val[0] = LSB;
Temperature.Val[1] = MSB;
digitalWrite(ACC_CS, HIGH);
}
else
{
Wire.beginTransmission(LSM9DS1_ADDRESS_MAG);
Wire.write(OUT_TEMP_L | JUST_READ);
Wire.endTransmission();
Wire.requestFrom(LSM9DS1_ADDRESS_MAG, 2);
LSB = Wire.read();
MSB = Wire.read();
Temperature.Val[0] = LSB;
Temperature.Val[1] = MSB;
}
}