-
Notifications
You must be signed in to change notification settings - Fork 8
/
Adafruit_LIS3MDL.cpp
553 lines (491 loc) · 18.5 KB
/
Adafruit_LIS3MDL.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*!
* @file Adafruit_LIS3MDL.cpp
*
* @mainpage Adafruit LIS3MDL Breakout
*
* @section intro_sec Introduction
*
* This is a library for the Adafruit LIS3MDL magnetometer breakout board
* ----> https://www.adafruit.com/product/4479
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @author Limor Fried (Adafruit Industries)
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Arduino.h"
#include <Adafruit_LIS3MDL.h>
#include <Wire.h>
/**************************************************************************/
/*!
@brief Instantiates a new LIS3MDL class
*/
/**************************************************************************/
Adafruit_LIS3MDL::Adafruit_LIS3MDL() {}
/*!
* @brief Sets up the hardware and initializes I2C
* @param i2c_address
* The I2C address to be used.
* @param wire
* The Wire object to be used for I2C connections.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_LIS3MDL::begin_I2C(uint8_t i2c_address, TwoWire *wire) {
if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;
spi_dev = NULL;
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
if (!i2c_dev->begin()) {
return false;
}
return _init();
}
/*!
* @brief Sets up the hardware and initializes hardware SPI
* @param cs_pin The arduino pin # connected to chip select
* @param theSPI The SPI object to be used for SPI connections.
* @param frequency The SPI bus frequency
* @return True if initialization was successful, otherwise false.
*/
boolean Adafruit_LIS3MDL::begin_SPI(uint8_t cs_pin, SPIClass *theSPI,
uint32_t frequency) {
if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;
i2c_dev = NULL;
spi_dev = new Adafruit_SPIDevice(cs_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0, // data mode
theSPI);
if (!spi_dev->begin()) {
return false;
}
return _init();
}
/*!
* @brief Sets up the hardware and initializes software SPI
* @param cs_pin The arduino pin # connected to chip select
* @param sck_pin The arduino pin # connected to SPI clock
* @param miso_pin The arduino pin # connected to SPI MISO
* @param mosi_pin The arduino pin # connected to SPI MOSI
* @param frequency The SPI bus frequency
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_LIS3MDL::begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
int8_t mosi_pin, uint32_t frequency) {
if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;
i2c_dev = NULL;
spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0); // data mode
if (!spi_dev->begin()) {
return false;
}
return _init();
}
/*!
* @brief Common initialization code for I2C & SPI
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_LIS3MDL::_init(void) {
// Check connection
Adafruit_BusIO_Register chip_id =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_WHO_AM_I, 1);
// make sure we're talking to the right chip
if (chip_id.read() != 0x3D) {
// No LIS3MDL detected ... return false
return false;
}
reset();
// set high quality performance mode
setPerformanceMode(LIS3MDL_ULTRAHIGHMODE);
// 155Hz default rate
setDataRate(LIS3MDL_DATARATE_155_HZ);
// lowest range
setRange(LIS3MDL_RANGE_4_GAUSS);
setOperationMode(LIS3MDL_CONTINUOUSMODE);
return true;
}
/**************************************************************************/
/*!
@brief Performs a software reset
*/
/**************************************************************************/
void Adafruit_LIS3MDL::reset(void) {
Adafruit_BusIO_Register CTRL_REG2 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits resetbits =
Adafruit_BusIO_RegisterBits(&CTRL_REG2, 1, 2);
resetbits.write(0x1);
delay(10);
getRange();
}
/**************************************************************************/
/*!
@brief Read the XYZ data from the magnetometer and store in the internal
x, y and z (and x_g, y_g, z_g) member variables.
*/
/**************************************************************************/
void Adafruit_LIS3MDL::read(void) {
uint8_t buffer[6];
Adafruit_BusIO_Register XYZDataReg = Adafruit_BusIO_Register(
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_OUT_X_L, 6);
XYZDataReg.read(buffer, 6);
x = buffer[0];
x |= buffer[1] << 8;
y = buffer[2];
y |= buffer[3] << 8;
z = buffer[4];
z |= buffer[5] << 8;
float scale = 1; // LSB per gauss
switch (rangeBuffered) {
case LIS3MDL_RANGE_16_GAUSS:
scale = 1711;
break;
case LIS3MDL_RANGE_12_GAUSS:
scale = 2281;
break;
case LIS3MDL_RANGE_8_GAUSS:
scale = 3421;
break;
case LIS3MDL_RANGE_4_GAUSS:
scale = 6842;
break;
}
x_gauss = (float)x / scale;
y_gauss = (float)y / scale;
z_gauss = (float)z / scale;
}
/**************************************************************************/
/*!
@brief Gets the most recent sensor event, Adafruit Unified Sensor format
@param event Pointer to an Adafruit Unified sensor_event_t object that
we'll fill in
@returns True on successful read
*/
/**************************************************************************/
bool Adafruit_LIS3MDL::getEvent(sensors_event_t *event) {
/* Clear the event */
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _sensorID;
event->type = SENSOR_TYPE_MAGNETIC_FIELD;
event->timestamp = millis();
read();
event->magnetic.x = x_gauss * 100; // microTesla per gauss
event->magnetic.y = y_gauss * 100; // microTesla per gauss
event->magnetic.z = z_gauss * 100; // microTesla per gauss
return true;
}
/**************************************************************************/
/*!
@brief Gets the sensor_t device data, Adafruit Unified Sensor format
@param sensor Pointer to an Adafruit Unified sensor_t object that we'll
fill in
*/
/**************************************************************************/
void Adafruit_LIS3MDL::getSensor(sensor_t *sensor) {
/* Clear the sensor_t object */
memset(sensor, 0, sizeof(sensor_t));
/* Insert the sensor name in the fixed length char array */
strncpy(sensor->name, "LIS3MDL", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_MAGNETIC_FIELD;
sensor->min_delay = 0;
sensor->min_value = -1600; // -16 gauss in uTesla
sensor->max_value = 1600; // +16 gauss in uTesla
sensor->resolution = 0.015; // 100/6842 uTesla per LSB at +-4 gauss range
}
/**************************************************************************/
/*!
@brief Set the performance mode, LIS3MDL_LOWPOWERMODE, LIS3MDL_MEDIUMMODE,
LIS3MDL_HIGHMODE or LIS3MDL_ULTRAHIGHMODE
@param mode Enumerated lis3mdl_performancemode_t
*/
/**************************************************************************/
void Adafruit_LIS3MDL::setPerformanceMode(lis3mdl_performancemode_t mode) {
// write xy
Adafruit_BusIO_Register CTRL_REG1 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits performancemodebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG1, 2, 5);
performancemodebits.write((uint8_t)mode);
// write z
Adafruit_BusIO_Register CTRL_REG4 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG4, 1);
Adafruit_BusIO_RegisterBits performancemodezbits =
Adafruit_BusIO_RegisterBits(&CTRL_REG4, 2, 2);
performancemodezbits.write((uint8_t)mode);
}
/**************************************************************************/
/*!
@brief Get the performance mode
@returns Enumerated lis3mdl_performancemode_t, LIS3MDL_LOWPOWERMODE,
LIS3MDL_MEDIUMMODE, LIS3MDL_HIGHMODE or LIS3MDL_ULTRAHIGHMODE
*/
/**************************************************************************/
lis3mdl_performancemode_t Adafruit_LIS3MDL::getPerformanceMode(void) {
Adafruit_BusIO_Register CTRL_REG1 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits performancemodebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG1, 2, 5);
return (lis3mdl_performancemode_t)performancemodebits.read();
}
/**************************************************************************/
/*!
@brief Sets the data rate for the LIS3MDL (controls power consumption)
from 0.625 Hz to 80Hz
@param dataRate Enumerated lis3mdl_dataRate_t
*/
/**************************************************************************/
void Adafruit_LIS3MDL::setDataRate(lis3mdl_dataRate_t dataRate) {
if (dataRate == LIS3MDL_DATARATE_155_HZ) {
// set OP to UHP
setPerformanceMode(LIS3MDL_ULTRAHIGHMODE);
}
if (dataRate == LIS3MDL_DATARATE_300_HZ) {
// set OP to HP
setPerformanceMode(LIS3MDL_HIGHMODE);
}
if (dataRate == LIS3MDL_DATARATE_560_HZ) {
// set OP to MP
setPerformanceMode(LIS3MDL_MEDIUMMODE);
}
if (dataRate == LIS3MDL_DATARATE_1000_HZ) {
// set OP to LP
setPerformanceMode(LIS3MDL_LOWPOWERMODE);
}
delay(10);
Adafruit_BusIO_Register CTRL_REG1 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits dataratebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG1, 4, 1); // includes FAST_ODR
dataratebits.write((uint8_t)dataRate);
}
/**************************************************************************/
/*!
@brief Gets the data rate for the LIS3MDL (controls power consumption)
@return Enumerated lis3mdl_dataRate_t from 0.625 Hz to 80Hz
*/
/**************************************************************************/
lis3mdl_dataRate_t Adafruit_LIS3MDL::getDataRate(void) {
Adafruit_BusIO_Register CTRL_REG1 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits dataratebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG1, 4, 1); // includes FAST_ODR
return (lis3mdl_dataRate_t)dataratebits.read();
}
/**************************************************************************/
/*!
@brief Set the operation mode, LIS3MDL_CONTINUOUSMODE,
LIS3MDL_SINGLEMODE or LIS3MDL_POWERDOWNMODE
@param mode Enumerated lis3mdl_operationmode_t
*/
/**************************************************************************/
void Adafruit_LIS3MDL::setOperationMode(lis3mdl_operationmode_t mode) {
// write x and y
Adafruit_BusIO_Register CTRL_REG3 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG3, 1);
Adafruit_BusIO_RegisterBits opmodebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG3, 2, 0);
opmodebits.write((uint8_t)mode);
}
/**************************************************************************/
/*!
@brief Get the operation mode
@returns Enumerated lis3mdl_operationmode_t, LIS3MDL_CONTINUOUSMODE,
LIS3MDL_SINGLEMODE or LIS3MDL_POWERDOWNMODE
*/
/**************************************************************************/
lis3mdl_operationmode_t Adafruit_LIS3MDL::getOperationMode(void) {
Adafruit_BusIO_Register CTRL_REG3 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG3, 1);
Adafruit_BusIO_RegisterBits opmodebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG3, 2, 0);
return (lis3mdl_operationmode_t)opmodebits.read();
}
/**************************************************************************/
/*!
@brief Set the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
@param range Enumerated lis3mdl_range_t
*/
/**************************************************************************/
void Adafruit_LIS3MDL::setRange(lis3mdl_range_t range) {
Adafruit_BusIO_Register CTRL_REG2 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits rangebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG2, 2, 5);
rangebits.write((uint8_t)range);
rangeBuffered = range;
}
/**************************************************************************/
/*!
@brief Read the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
@returns Enumerated lis3mdl_range_t
*/
/**************************************************************************/
lis3mdl_range_t Adafruit_LIS3MDL::getRange(void) {
Adafruit_BusIO_Register CTRL_REG2 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG2, 1);
Adafruit_BusIO_RegisterBits rangebits =
Adafruit_BusIO_RegisterBits(&CTRL_REG2, 2, 5);
rangeBuffered = (lis3mdl_range_t)rangebits.read();
return rangeBuffered;
}
/**************************************************************************/
/*!
@brief Set the interrupt threshold value
@param value 16-bit unsigned raw value
*/
/**************************************************************************/
void Adafruit_LIS3MDL::setIntThreshold(uint16_t value) {
value &= 0x7FFF; // high bit must be 0!
Adafruit_BusIO_Register INT_THS =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_INT_THS_L, 2);
INT_THS.write(value);
}
/**************************************************************************/
/*!
@brief Get the interrupt threshold value
@returns 16-bit unsigned raw value
*/
/**************************************************************************/
uint16_t Adafruit_LIS3MDL::getIntThreshold(void) {
Adafruit_BusIO_Register INT_THS =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_INT_THS_L, 2);
return INT_THS.read();
}
/**************************************************************************/
/*!
@brief Configure INT_CFG
@param enableX Enable interrupt generation on X-axis
@param enableY Enable interrupt generation on Y-axis
@param enableZ Enable interrupt generation on Z-axis
@param polarity Sets the polarity of the INT output logic
@param latch If true (latched) the INT pin remains in the same state
until INT_SRC is read.
@param enableInt Interrupt enable on INT pin
*/
/**************************************************************************/
void Adafruit_LIS3MDL::configInterrupt(bool enableX, bool enableY, bool enableZ,
bool polarity, bool latch,
bool enableInt) {
uint8_t value = 0x08; // set default bits, see table 36
value |= enableX << 7;
value |= enableY << 6;
value |= enableZ << 5;
value |= polarity << 2;
value |= latch << 1;
value |= enableInt;
Adafruit_BusIO_Register INT_CFG = Adafruit_BusIO_Register(
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_INT_CFG, 1);
INT_CFG.write(value);
}
/**************************************************************************/
/*!
@brief Enable or disable self-test
@param flag If true, enable self-test
*/
/**************************************************************************/
void Adafruit_LIS3MDL::selfTest(bool flag) {
Adafruit_BusIO_Register CTRL_REG1 =
Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
LIS3MDL_REG_CTRL_REG1, 1);
Adafruit_BusIO_RegisterBits stbit =
Adafruit_BusIO_RegisterBits(&CTRL_REG1, 1, 0);
stbit.write(flag);
}
/**************************************************************************/
/*!
@brief Get the magnetic data rate.
@returns The data rate in float
*/
float Adafruit_LIS3MDL::magneticFieldSampleRate(void) {
switch (this->getDataRate()) {
case LIS3MDL_DATARATE_0_625_HZ:
return 0.625f;
case LIS3MDL_DATARATE_1_25_HZ:
return 1.25f;
case LIS3MDL_DATARATE_2_5_HZ:
return 2.5f;
case LIS3MDL_DATARATE_5_HZ:
return 5.0f;
case LIS3MDL_DATARATE_10_HZ:
return 10.0f;
case LIS3MDL_DATARATE_20_HZ:
return 20.0f;
case LIS3MDL_DATARATE_40_HZ:
return 40.0f;
case LIS3MDL_DATARATE_80_HZ:
return 80.0f;
case LIS3MDL_DATARATE_155_HZ:
return 155.0f;
case LIS3MDL_DATARATE_300_HZ:
return 300.0f;
case LIS3MDL_DATARATE_560_HZ:
return 560.0f;
case LIS3MDL_DATARATE_1000_HZ:
return 1000.0f;
}
return 0;
}
/**************************************************************************/
/*!
@brief Check for available data from magnetic
@returns 1 if available, 0 if not
*/
int Adafruit_LIS3MDL::magneticFieldAvailable(void) {
Adafruit_BusIO_Register REG_STATUS = Adafruit_BusIO_Register(
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_STATUS, 1);
return (REG_STATUS.read() & 0x08) ? 1 : 0;
}
/**************************************************************************/
/*!
@brief Read magnetic data
@param x reference to x axis
@param y reference to y axis
@param z reference to z axis
@returns 1 if success, 0 if not
*/
int Adafruit_LIS3MDL::readMagneticField(float &x, float &y, float &z) {
int16_t data[3];
Adafruit_BusIO_Register XYZDataReg = Adafruit_BusIO_Register(
i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_OUT_X_L, 6);
if (!XYZDataReg.read((uint8_t *)data, sizeof(data))) {
x = y = z = NAN;
return 0;
}
x = data[0] * 4.0 * 100.0 / 32768.0;
y = data[1] * 4.0 * 100.0 / 32768.0;
z = data[2] * 4.0 * 100.0 / 32768.0;
return 1;
}