-
Notifications
You must be signed in to change notification settings - Fork 4
/
Adafruit_LIS2MDL.cpp
339 lines (289 loc) · 10.4 KB
/
Adafruit_LIS2MDL.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
/*!
* @file Adafruit_LIS2MDL.cpp
*
* @mainpage Adafruit LIS2MDL Breakout
*
* @section intro_sec Introduction
*
* This is a library for the LIS2MDL Magnentometer/compass
*
* Designed specifically to work with the Adafruit LSM303AGR and LIS2MDL
* Breakouts
*
* These displays use I2C to communicate, 2 pins are required to interface.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products
* from Adafruit!
*
* @section author Author
*
* Written by Bryan Siepert for Adafruit Industries.
*
* @section license License
*
* BSD license, all text above must be included in any redistribution
*
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <limits.h>
#include "Adafruit_LIS2MDL.h"
/***************************************************************************
MAGNETOMETER
***************************************************************************/
/***************************************************************************
PRIVATE FUNCTIONS
***************************************************************************/
/**************************************************************************/
/*!
@brief Reads the raw data from the sensor
*/
/**************************************************************************/
void Adafruit_LIS2MDL::read() {
Adafruit_BusIO_Register data_reg = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_OUTX_L_REG, 6);
uint16_t buffer[3];
data_reg.read((uint8_t *)buffer, 6);
raw.x = buffer[0];
raw.y = buffer[1];
raw.z = buffer[2];
}
/***************************************************************************
CONSTRUCTOR
***************************************************************************/
/**************************************************************************/
/*!
@brief Instantiates a new Adafruit_LIS2MDL class
@param sensorID an option ID to differentiate the sensor from others
*/
/**************************************************************************/
Adafruit_LIS2MDL::Adafruit_LIS2MDL(int32_t sensorID) {
_sensorID = sensorID;
// Clear the raw mag data
raw.x = 0;
raw.y = 0;
raw.z = 0;
}
/***************************************************************************
PUBLIC FUNCTIONS
***************************************************************************/
/*!
* @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_LIS2MDL::begin(uint8_t i2c_address, TwoWire *wire) {
if (!i2c_dev) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
}
spi_dev = NULL;
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.
* @return True if initialization was successful, otherwise false.
*/
boolean Adafruit_LIS2MDL::begin_SPI(uint8_t cs_pin, SPIClass *theSPI) {
i2c_dev = NULL;
if (!spi_dev) {
spi_dev = new Adafruit_SPIDevice(cs_pin,
1000000, // 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
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_LIS2MDL::begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
int8_t mosi_pin) {
i2c_dev = NULL;
if (!spi_dev) {
spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin,
1000000, // 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_LIS2MDL::_init(void) {
if (spi_dev) {
// enable 4-wire SPI and disable I2C
Adafruit_BusIO_Register cfg_reg =
Adafruit_BusIO_Register(spi_dev, 0x62, ADDRBIT8_HIGH_TOREAD);
cfg_reg.write(0b00100100);
delay(10);
}
// Check connection
Adafruit_BusIO_Register chip_id = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_WHO_AM_I, 1);
// make sure we're talking to the right chip
if (chip_id.read() != _CHIP_ID) {
// No LIS2MDL detected ... return false
return false;
}
if (config_a)
delete config_a;
config_a = new Adafruit_BusIO_Register(i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD,
LIS2MDL_CFG_REG_A, 1);
// enable int latching
reset();
return true;
}
/*!
* @brief Resets the sensor to an initial state
*/
void Adafruit_LIS2MDL::reset(void) {
Adafruit_BusIO_Register config_c = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_CFG_REG_C, 1);
Adafruit_BusIO_RegisterBits reset =
Adafruit_BusIO_RegisterBits(config_a, 1, 5);
Adafruit_BusIO_RegisterBits reboot =
Adafruit_BusIO_RegisterBits(config_a, 1, 6);
Adafruit_BusIO_RegisterBits mode =
Adafruit_BusIO_RegisterBits(config_a, 2, 0);
Adafruit_BusIO_RegisterBits temp_compensation =
Adafruit_BusIO_RegisterBits(config_a, 1, 7);
Adafruit_BusIO_RegisterBits bdu =
Adafruit_BusIO_RegisterBits(&config_c, 1, 4);
reset.write(1);
delay(100);
reboot.write(1);
delay(100);
if (spi_dev) {
// enable 4-wire SPI and disable I2C
Adafruit_BusIO_Register cfg_reg =
Adafruit_BusIO_Register(spi_dev, 0x62, ADDRBIT8_HIGH_TOREAD);
cfg_reg.write(0b00100100);
delay(10);
}
bdu.write(1);
temp_compensation.write(1);
mode.write(0x00); // set to continuous mode
setDataRate(LIS2MDL_RATE_100_HZ);
}
/**************************************************************************/
/*!
@brief Sets the magnetometer's update rate
@param rate The new `lis2mdl_rate_t` to set
*/
/**************************************************************************/
void Adafruit_LIS2MDL::setDataRate(lis2mdl_rate_t rate) {
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(config_a, 2, 2);
data_rate.write(rate);
}
/**************************************************************************/
/*!
@brief Gets the magnetometer's update rate
@returns The current data rate as a `lis2mdl_rate_t`
*/
/**************************************************************************/
lis2mdl_rate_t Adafruit_LIS2MDL::getDataRate(void) {
Adafruit_BusIO_RegisterBits data_rate =
Adafruit_BusIO_RegisterBits(config_a, 2, 2);
return (lis2mdl_rate_t)data_rate.read();
}
/**************************************************************************/
/*!
@brief Gets the most recent sensor event
@param event The `sensors_event_t` to fill with event data
@returns true, always
*/
/**************************************************************************/
bool Adafruit_LIS2MDL::getEvent(sensors_event_t *event) {
/* Clear the event */
memset(event, 0, sizeof(sensors_event_t));
/* Read new data */
read();
event->version = sizeof(sensors_event_t);
event->sensor_id = _sensorID;
event->type = SENSOR_TYPE_MAGNETIC_FIELD;
event->timestamp = millis();
event->magnetic.x =
(float)raw.x * LIS2MDL_MAG_LSB * LIS2MDL_MILLIGAUSS_TO_MICROTESLA;
event->magnetic.y =
(float)raw.y * LIS2MDL_MAG_LSB * LIS2MDL_MILLIGAUSS_TO_MICROTESLA;
event->magnetic.z =
(float)raw.z * LIS2MDL_MAG_LSB * LIS2MDL_MILLIGAUSS_TO_MICROTESLA;
return true;
}
/**************************************************************************/
/*!
@brief Gets the sensor_t data
*/
/**************************************************************************/
void Adafruit_LIS2MDL::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, "LIS2MDL", 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->max_value = 5000; // 50 gauss = 5000 uTesla
sensor->min_value = -5000; // -50 gauss = -5000 uTesla
sensor->resolution = 0.15; // 1.65 gauss = 0.15 uTesla
}
/*************************************************************************/
/*!
@brief Enable interrupts
@param enable Set to True to enable interrupts, set to False to disable
*/
void Adafruit_LIS2MDL::enableInterrupts(bool enable) {
Adafruit_BusIO_Register int_ctrl = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_INT_CRTL_REG);
Adafruit_BusIO_Register cfg_c = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_CFG_REG_C);
Adafruit_BusIO_RegisterBits enable_ints =
Adafruit_BusIO_RegisterBits(&int_ctrl, 1, 0);
Adafruit_BusIO_RegisterBits int_pin_output =
Adafruit_BusIO_RegisterBits(&cfg_c, 1, 6);
enable_ints.write(enable);
int_pin_output.write(enable);
}
/*************************************************************************/
/*!
@brief Sets the polarity of the interrupt pin
@param active_high Set to true to make the int pin active high, false
to set as active low
*/
void Adafruit_LIS2MDL::interruptsActiveHigh(bool active_high) {
Adafruit_BusIO_Register int_ctrl = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS2MDL_INT_CRTL_REG);
Adafruit_BusIO_RegisterBits active_high_bit =
Adafruit_BusIO_RegisterBits(&int_ctrl, 1, 2);
active_high_bit.write(active_high);
}