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); } }
LIS2MDL.h@0:d7138994c637, 2018-02-12 (annotated)
- Committer:
- knaresh89
- Date:
- Mon Feb 12 04:59:47 2018 +0000
- Revision:
- 0:d7138994c637
LIS2MDL sensor driver for mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
knaresh89 | 0:d7138994c637 | 1 | /* |
knaresh89 | 0:d7138994c637 | 2 | Created by Naresh Krishnamoorthy |
knaresh89 | 0:d7138994c637 | 3 | |
knaresh89 | 0:d7138994c637 | 4 | The LIS2MDL is a low power magnetometer, here used as 3 DoF solution. |
knaresh89 | 0:d7138994c637 | 5 | Library may be used freely and without limit with attribution. |
knaresh89 | 0:d7138994c637 | 6 | */ |
knaresh89 | 0:d7138994c637 | 7 | |
knaresh89 | 0:d7138994c637 | 8 | #ifndef LIS2MDL_h |
knaresh89 | 0:d7138994c637 | 9 | #define LIS2MDL_h |
knaresh89 | 0:d7138994c637 | 10 | |
knaresh89 | 0:d7138994c637 | 11 | #include "mbed.h" |
knaresh89 | 0:d7138994c637 | 12 | |
knaresh89 | 0:d7138994c637 | 13 | //********************************* |
knaresh89 | 0:d7138994c637 | 14 | //Register map for LIS2MDL' |
knaresh89 | 0:d7138994c637 | 15 | // 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 |
knaresh89 | 0:d7138994c637 | 16 | //********************************* |
knaresh89 | 0:d7138994c637 | 17 | #define LIS2MDL_OFFSET_X_REG_L 0x45 |
knaresh89 | 0:d7138994c637 | 18 | #define LIS2MDL_OFFSET_X_REG_L 0x46 |
knaresh89 | 0:d7138994c637 | 19 | #define LIS2MDL_OFFSET_X_REG_L 0x47 |
knaresh89 | 0:d7138994c637 | 20 | #define LIS2MDL_OFFSET_X_REG_L 0x48 |
knaresh89 | 0:d7138994c637 | 21 | #define LIS2MDL_OFFSET_X_REG_L 0x49 |
knaresh89 | 0:d7138994c637 | 22 | #define LIS2MDL_OFFSET_X_REG_L 0x4A |
knaresh89 | 0:d7138994c637 | 23 | #define LIS2MDL_WHO_AM_I 0x4F |
knaresh89 | 0:d7138994c637 | 24 | #define LIS2MDL_CFG_REG_A 0x60 |
knaresh89 | 0:d7138994c637 | 25 | #define LIS2MDL_CFG_REG_B 0x61 |
knaresh89 | 0:d7138994c637 | 26 | #define LIS2MDL_CFG_REG_C 0x62 |
knaresh89 | 0:d7138994c637 | 27 | #define LIS2MDL_INT_CTRL_REG 0x63 |
knaresh89 | 0:d7138994c637 | 28 | #define LIS2MDL_INT_SOURCE_REG 0x64 |
knaresh89 | 0:d7138994c637 | 29 | #define LIS2MDL_INT_THS_L_REG 0x65 |
knaresh89 | 0:d7138994c637 | 30 | #define LIS2MDL_INT_THS_H_REG 0x66 |
knaresh89 | 0:d7138994c637 | 31 | #define LIS2MDL_STATUS_REG 0x67 |
knaresh89 | 0:d7138994c637 | 32 | #define LIS2MDL_OUTX_L_REG 0x68 |
knaresh89 | 0:d7138994c637 | 33 | #define LIS2MDL_OUTX_H_REG 0x69 |
knaresh89 | 0:d7138994c637 | 34 | #define LIS2MDL_OUTY_L_REG 0x6A |
knaresh89 | 0:d7138994c637 | 35 | #define LIS2MDL_OUTY_H_REG 0x6B |
knaresh89 | 0:d7138994c637 | 36 | #define LIS2MDL_OUTZ_L_REG 0x6C |
knaresh89 | 0:d7138994c637 | 37 | #define LIS2MDL_OUTZ_H_REG 0x6D |
knaresh89 | 0:d7138994c637 | 38 | #define LIS2MDL_TEMP_OUT_L_REG 0x6E |
knaresh89 | 0:d7138994c637 | 39 | #define LIS2MDL_TEMP_OUT_H_REG 0x6F |
knaresh89 | 0:d7138994c637 | 40 | |
knaresh89 | 0:d7138994c637 | 41 | #define LIS2MDL_ADDRESS (0x1E << 1) |
knaresh89 | 0:d7138994c637 | 42 | |
knaresh89 | 0:d7138994c637 | 43 | //****************************** |
knaresh89 | 0:d7138994c637 | 44 | // MODR legal values |
knaresh89 | 0:d7138994c637 | 45 | //****************************** |
knaresh89 | 0:d7138994c637 | 46 | #define MODR_10Hz 0x00 |
knaresh89 | 0:d7138994c637 | 47 | #define MODR_20Hz 0x01 |
knaresh89 | 0:d7138994c637 | 48 | #define MODR_50Hz 0x02 |
knaresh89 | 0:d7138994c637 | 49 | #define MODR_100Hz 0x03 |
knaresh89 | 0:d7138994c637 | 50 | |
knaresh89 | 0:d7138994c637 | 51 | /** LIS2MDL class. |
knaresh89 | 0:d7138994c637 | 52 | * Used for interfacing with the LIS2MDL sensor on board the itracker |
knaresh89 | 0:d7138994c637 | 53 | */ |
knaresh89 | 0:d7138994c637 | 54 | class LIS2MDL |
knaresh89 | 0:d7138994c637 | 55 | { |
knaresh89 | 0:d7138994c637 | 56 | public: |
knaresh89 | 0:d7138994c637 | 57 | /**Public constructor |
knaresh89 | 0:d7138994c637 | 58 | * @param p_i2c Mbed I2C class object |
knaresh89 | 0:d7138994c637 | 59 | * @param addr Address of the I2C object |
knaresh89 | 0:d7138994c637 | 60 | * |
knaresh89 | 0:d7138994c637 | 61 | */ |
knaresh89 | 0:d7138994c637 | 62 | LIS2MDL(I2C& p_i2c, uint8_t addr); |
knaresh89 | 0:d7138994c637 | 63 | |
knaresh89 | 0:d7138994c637 | 64 | /** init function to set the sensors initialisation parameters |
knaresh89 | 0:d7138994c637 | 65 | * @param MODR See the MODR legal values in the defines in LIS2MDL.h |
knaresh89 | 0:d7138994c637 | 66 | * |
knaresh89 | 0:d7138994c637 | 67 | */ |
knaresh89 | 0:d7138994c637 | 68 | void init(uint8_t MODR); |
knaresh89 | 0:d7138994c637 | 69 | |
knaresh89 | 0:d7138994c637 | 70 | /** Function to get the CHIP ID |
knaresh89 | 0:d7138994c637 | 71 | * |
knaresh89 | 0:d7138994c637 | 72 | * @return uin8_t returns the chip id. In this can 64 |
knaresh89 | 0:d7138994c637 | 73 | * 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 |
knaresh89 | 0:d7138994c637 | 74 | */ |
knaresh89 | 0:d7138994c637 | 75 | uint8_t getChipID(); |
knaresh89 | 0:d7138994c637 | 76 | |
knaresh89 | 0:d7138994c637 | 77 | /** Read the raw sensor data |
knaresh89 | 0:d7138994c637 | 78 | * @params destination pointer to the array that will store the results |
knaresh89 | 0:d7138994c637 | 79 | * 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 |
knaresh89 | 0:d7138994c637 | 80 | */ |
knaresh89 | 0:d7138994c637 | 81 | void readData(int16_t * destination); |
knaresh89 | 0:d7138994c637 | 82 | |
knaresh89 | 0:d7138994c637 | 83 | /** Function to get the status register value |
knaresh89 | 0:d7138994c637 | 84 | * |
knaresh89 | 0:d7138994c637 | 85 | * @return uint8_t value of the status register |
knaresh89 | 0:d7138994c637 | 86 | * 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 |
knaresh89 | 0:d7138994c637 | 87 | */ |
knaresh89 | 0:d7138994c637 | 88 | uint8_t status(); |
knaresh89 | 0:d7138994c637 | 89 | |
knaresh89 | 0:d7138994c637 | 90 | /** Function to reset the LIS2MDL sensor |
knaresh89 | 0:d7138994c637 | 91 | */ |
knaresh89 | 0:d7138994c637 | 92 | void reset(); |
knaresh89 | 0:d7138994c637 | 93 | |
knaresh89 | 0:d7138994c637 | 94 | /** Function to generate the offset bias stored in the chip as part of the calib |
knaresh89 | 0:d7138994c637 | 95 | * @param dest1 Magnetic Bias offset of the sensor |
knaresh89 | 0:d7138994c637 | 96 | * @param dest2 Magnetic Scale offset of the sensor |
knaresh89 | 0:d7138994c637 | 97 | */ |
knaresh89 | 0:d7138994c637 | 98 | void offsetBias(float * dest1, float * dest2); |
knaresh89 | 0:d7138994c637 | 99 | |
knaresh89 | 0:d7138994c637 | 100 | /** Function to read the temperature of the internal tempo sensor |
knaresh89 | 0:d7138994c637 | 101 | * |
knaresh89 | 0:d7138994c637 | 102 | * @return uint8_t temperature reading of the internal temp sensor |
knaresh89 | 0:d7138994c637 | 103 | */ |
knaresh89 | 0:d7138994c637 | 104 | int16_t readTemperature(); |
knaresh89 | 0:d7138994c637 | 105 | |
knaresh89 | 0:d7138994c637 | 106 | /** Self check function for the sensor |
knaresh89 | 0:d7138994c637 | 107 | * |
knaresh89 | 0:d7138994c637 | 108 | */ |
knaresh89 | 0:d7138994c637 | 109 | void lis2mdlSelfCheck(); |
knaresh89 | 0:d7138994c637 | 110 | |
knaresh89 | 0:d7138994c637 | 111 | /** I2C function for writing a Byte to the LIS2MDL sensor |
knaresh89 | 0:d7138994c637 | 112 | * @param address address of the sensor |
knaresh89 | 0:d7138994c637 | 113 | * @param subaddress register location to which to write data |
knaresh89 | 0:d7138994c637 | 114 | * @param data data to be written |
knaresh89 | 0:d7138994c637 | 115 | */ |
knaresh89 | 0:d7138994c637 | 116 | void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); |
knaresh89 | 0:d7138994c637 | 117 | |
knaresh89 | 0:d7138994c637 | 118 | /** I2C function for reading a Byte from the LIS2MDL sensor |
knaresh89 | 0:d7138994c637 | 119 | * @param address address of the sensor |
knaresh89 | 0:d7138994c637 | 120 | * @param subaddress register location to which to write data |
knaresh89 | 0:d7138994c637 | 121 | */ |
knaresh89 | 0:d7138994c637 | 122 | uint8_t readByte(uint8_t address, char subAddress); |
knaresh89 | 0:d7138994c637 | 123 | |
knaresh89 | 0:d7138994c637 | 124 | /** I2C function for reading many Bytes from the LIS2MDL sensor |
knaresh89 | 0:d7138994c637 | 125 | * @param address address of the sensor |
knaresh89 | 0:d7138994c637 | 126 | * @param subaddress register location to which to write data |
knaresh89 | 0:d7138994c637 | 127 | * @param count number of bytes to read |
knaresh89 | 0:d7138994c637 | 128 | * @param dest pointer to the array which will store the read values |
knaresh89 | 0:d7138994c637 | 129 | */ |
knaresh89 | 0:d7138994c637 | 130 | void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, char * dest); |
knaresh89 | 0:d7138994c637 | 131 | |
knaresh89 | 0:d7138994c637 | 132 | protected: |
knaresh89 | 0:d7138994c637 | 133 | I2C *_i2c_p; |
knaresh89 | 0:d7138994c637 | 134 | I2C &_i2c; |
knaresh89 | 0:d7138994c637 | 135 | |
knaresh89 | 0:d7138994c637 | 136 | }; |
knaresh89 | 0:d7138994c637 | 137 | |
knaresh89 | 0:d7138994c637 | 138 | #endif |