Mbed driver for the Stmicroelectronics LIS2MDL sensor
Dependents: itracker-mbed-os-example-lis2mdl
Introduction
The LIS2MDL is an ultra-low-power, high-performance 3-axis digital magnetic sensor.
The LIS2MDL has a magnetic field dynamic range of ±50 gauss.
The LIS2MDL includes an I2C serial bus interface that supports standard, fast mode, fast mode plus, and high-speed (100 kHz, 400 kHz, 1 MHz, and 3.4 MHz) and an SPI serial standard interface.
More details in datasheet here: http://www.st.com/resource/en/datasheet/lis2mdl.pdf
This driver assumes the user will deploy the sensor over I2C. SPI update will be committed soon :)
Sample Code:
LIS2mDL sample program
uint8_t MODR = MODR_100Hz; int16_t temp[3] = {0, 0, 0}; float magBias[3] = {0,0,0}, magScale[3] = {0,0,0}; // Attach interrupt to this pin if you want to use magnetometer sensor interrupt // Before that make sure to set the correct interrupt value in the INT_CTRL_REG, INT_THS_L_REG and the INT_THS_H_REG (threshold value) DigitalIn interruptPin(LIS2MDL_intPin); // The itracker has the LIS2MDL sensor on the 13 and 11 I2C pins I2C i2c(p13,p11); // main() method. Runs in its own thread in the OS int main() { //Create LIS2MDL object LIS2MDL sensor(i2c, LIS2MDL_ADDRESS); //Reset the sensor to ensure correct starting config register values sensor.reset(); wait(3); //Intialise the CHIP with the MODR value chosen sensor.init(MODR); //Test the Chip ID. Should return 64 (0x40) sensor.getChipID(); // Calcaulte the offset bias to be used in future reading. See self check for example usage sensor.offsetBias(magBias, magScale); // Read the internal temp sensor sensor.readTemperature(); //Get readings from the sensor; for(int i=0; i<60; i++) { int16_t temp[3] = {0, 0, 0}; sensor.readData(temp); wait(0.1); } }
Diff: LIS2MDL.h
- Revision:
- 0:d7138994c637
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LIS2MDL.h Mon Feb 12 04:59:47 2018 +0000 @@ -0,0 +1,138 @@ +/* + Created by Naresh Krishnamoorthy + + The LIS2MDL is a low power magnetometer, here used as 3 DoF solution. + Library may be used freely and without limit with attribution. +*/ + +#ifndef LIS2MDL_h +#define LIS2MDL_h + +#include "mbed.h" + +//********************************* +//Register map for LIS2MDL' +// http://www.st.com/content/ccc/resource/technical/document/datasheet/group3/29/13/d1/e0/9a/4d/4f/30/DM00395193/files/DM00395193.pdf/jcr:content/translations/en.DM00395193.pdf +//********************************* +#define LIS2MDL_OFFSET_X_REG_L 0x45 +#define LIS2MDL_OFFSET_X_REG_L 0x46 +#define LIS2MDL_OFFSET_X_REG_L 0x47 +#define LIS2MDL_OFFSET_X_REG_L 0x48 +#define LIS2MDL_OFFSET_X_REG_L 0x49 +#define LIS2MDL_OFFSET_X_REG_L 0x4A +#define LIS2MDL_WHO_AM_I 0x4F +#define LIS2MDL_CFG_REG_A 0x60 +#define LIS2MDL_CFG_REG_B 0x61 +#define LIS2MDL_CFG_REG_C 0x62 +#define LIS2MDL_INT_CTRL_REG 0x63 +#define LIS2MDL_INT_SOURCE_REG 0x64 +#define LIS2MDL_INT_THS_L_REG 0x65 +#define LIS2MDL_INT_THS_H_REG 0x66 +#define LIS2MDL_STATUS_REG 0x67 +#define LIS2MDL_OUTX_L_REG 0x68 +#define LIS2MDL_OUTX_H_REG 0x69 +#define LIS2MDL_OUTY_L_REG 0x6A +#define LIS2MDL_OUTY_H_REG 0x6B +#define LIS2MDL_OUTZ_L_REG 0x6C +#define LIS2MDL_OUTZ_H_REG 0x6D +#define LIS2MDL_TEMP_OUT_L_REG 0x6E +#define LIS2MDL_TEMP_OUT_H_REG 0x6F + +#define LIS2MDL_ADDRESS (0x1E << 1) + +//****************************** +// MODR legal values +//****************************** +#define MODR_10Hz 0x00 +#define MODR_20Hz 0x01 +#define MODR_50Hz 0x02 +#define MODR_100Hz 0x03 + +/** LIS2MDL class. + * Used for interfacing with the LIS2MDL sensor on board the itracker + */ +class LIS2MDL +{ +public: + /**Public constructor + * @param p_i2c Mbed I2C class object + * @param addr Address of the I2C object + * + */ + LIS2MDL(I2C& p_i2c, uint8_t addr); + + /** init function to set the sensors initialisation parameters + * @param MODR See the MODR legal values in the defines in LIS2MDL.h + * + */ + void init(uint8_t MODR); + + /** Function to get the CHIP ID + * + * @return uin8_t returns the chip id. In this can 64 + * see http://www.st.com/content/ccc/resource/technical/document/datasheet/group3/29/13/d1/e0/9a/4d/4f/30/DM00395193/files/DM00395193.pdf/jcr:content/translations/en.DM00395193.pdf + */ + uint8_t getChipID(); + + /** Read the raw sensor data + * @params destination pointer to the array that will store the results + * see http://www.st.com/content/ccc/resource/technical/document/datasheet/group3/29/13/d1/e0/9a/4d/4f/30/DM00395193/files/DM00395193.pdf/jcr:content/translations/en.DM00395193.pdf + */ + void readData(int16_t * destination); + + /** Function to get the status register value + * + * @return uint8_t value of the status register + * see http://www.st.com/content/ccc/resource/technical/document/datasheet/group3/29/13/d1/e0/9a/4d/4f/30/DM00395193/files/DM00395193.pdf/jcr:content/translations/en.DM00395193.pdf + */ + uint8_t status(); + + /** Function to reset the LIS2MDL sensor + */ + void reset(); + + /** Function to generate the offset bias stored in the chip as part of the calib + * @param dest1 Magnetic Bias offset of the sensor + * @param dest2 Magnetic Scale offset of the sensor + */ + void offsetBias(float * dest1, float * dest2); + + /** Function to read the temperature of the internal tempo sensor + * + * @return uint8_t temperature reading of the internal temp sensor + */ + int16_t readTemperature(); + + /** Self check function for the sensor + * + */ + void lis2mdlSelfCheck(); + + /** I2C function for writing a Byte to the LIS2MDL sensor + * @param address address of the sensor + * @param subaddress register location to which to write data + * @param data data to be written + */ + void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); + + /** I2C function for reading a Byte from the LIS2MDL sensor + * @param address address of the sensor + * @param subaddress register location to which to write data + */ + uint8_t readByte(uint8_t address, char subAddress); + + /** I2C function for reading many Bytes from the LIS2MDL sensor + * @param address address of the sensor + * @param subaddress register location to which to write data + * @param count number of bytes to read + * @param dest pointer to the array which will store the read values + */ + void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, char * dest); + +protected: + I2C *_i2c_p; + I2C &_i2c; + +}; + +#endif \ No newline at end of file