Naresh Krish / LIS2MDL

Dependents:   itracker-mbed-os-example-lis2mdl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS2MDL.h Source File

LIS2MDL.h

00001 /* 
00002     Created by Naresh Krishnamoorthy
00003   
00004   The LIS2MDL is a low power magnetometer, here used as 3 DoF solution.
00005   Library may be used freely and without limit with attribution.
00006 */
00007 
00008 #ifndef LIS2MDL_h
00009 #define LIS2MDL_h
00010 
00011 #include "mbed.h"
00012 
00013 //*********************************
00014 //Register map for LIS2MDL'
00015 // 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
00016 //*********************************
00017 #define LIS2MDL_OFFSET_X_REG_L        0x45
00018 #define LIS2MDL_OFFSET_X_REG_L        0x46
00019 #define LIS2MDL_OFFSET_X_REG_L        0x47
00020 #define LIS2MDL_OFFSET_X_REG_L        0x48
00021 #define LIS2MDL_OFFSET_X_REG_L        0x49
00022 #define LIS2MDL_OFFSET_X_REG_L        0x4A
00023 #define LIS2MDL_WHO_AM_I              0x4F
00024 #define LIS2MDL_CFG_REG_A             0x60
00025 #define LIS2MDL_CFG_REG_B             0x61
00026 #define LIS2MDL_CFG_REG_C             0x62
00027 #define LIS2MDL_INT_CTRL_REG          0x63
00028 #define LIS2MDL_INT_SOURCE_REG        0x64
00029 #define LIS2MDL_INT_THS_L_REG         0x65
00030 #define LIS2MDL_INT_THS_H_REG         0x66
00031 #define LIS2MDL_STATUS_REG            0x67
00032 #define LIS2MDL_OUTX_L_REG            0x68
00033 #define LIS2MDL_OUTX_H_REG            0x69
00034 #define LIS2MDL_OUTY_L_REG            0x6A
00035 #define LIS2MDL_OUTY_H_REG            0x6B
00036 #define LIS2MDL_OUTZ_L_REG            0x6C
00037 #define LIS2MDL_OUTZ_H_REG            0x6D
00038 #define LIS2MDL_TEMP_OUT_L_REG        0x6E
00039 #define LIS2MDL_TEMP_OUT_H_REG        0x6F
00040 
00041 #define LIS2MDL_ADDRESS               (0x1E << 1)
00042 
00043 //******************************
00044 // MODR legal values
00045 //******************************
00046 #define MODR_10Hz   0x00
00047 #define MODR_20Hz   0x01
00048 #define MODR_50Hz   0x02
00049 #define MODR_100Hz  0x03
00050 
00051 /** LIS2MDL class.
00052  *  Used for interfacing with the LIS2MDL sensor on board the itracker
00053  */
00054 class LIS2MDL
00055 {
00056 public:
00057     /**Public constructor
00058      * @param p_i2c Mbed I2C class object
00059      * @param addr Address of the I2C object  
00060      *  
00061     */
00062     LIS2MDL(I2C& p_i2c, uint8_t addr);
00063     
00064     /** init function to set the sensors initialisation parameters
00065     *  @param  MODR See the MODR legal values in the defines in LIS2MDL.h
00066     *
00067     */
00068     void init(uint8_t MODR);
00069     
00070     /** Function to get the CHIP ID
00071     *
00072     *  @return uin8_t returns the chip id. In this can 64
00073     *  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
00074     */
00075     uint8_t getChipID();
00076     
00077     /** Read the raw sensor data
00078     *   @params destination pointer to the array that will store the results
00079     *   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
00080     */
00081     void readData(int16_t * destination);
00082     
00083     /** Function to get the status register value
00084     *   
00085     *   @return uint8_t value of the status register
00086     *   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
00087     */
00088     uint8_t status();
00089     
00090     /** Function to reset the LIS2MDL sensor
00091     */
00092     void reset();
00093     
00094     /** Function to generate the offset bias stored in the chip as part of the calib 
00095     *   @param dest1 Magnetic Bias offset of the sensor
00096     *   @param dest2 Magnetic Scale offset of the sensor    
00097     */
00098     void offsetBias(float * dest1, float * dest2);
00099     
00100     /** Function to read the temperature of the internal tempo sensor
00101     *
00102     *   @return uint8_t temperature reading of the internal temp sensor
00103     */
00104     int16_t readTemperature();
00105     
00106     /** Self check function for the sensor
00107     *
00108     */
00109     void lis2mdlSelfCheck();
00110 
00111     /** I2C function for writing a Byte to the LIS2MDL sensor
00112     *   @param address address of the sensor
00113     *   @param subaddress register location to which to write data
00114     *   @param data data to be written 
00115     */
00116     void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
00117     
00118     /** I2C function for reading a Byte from the LIS2MDL sensor
00119     *   @param address address of the sensor
00120     *   @param subaddress register location to which to write data
00121     */
00122     uint8_t readByte(uint8_t address, char subAddress);
00123     
00124     /** I2C function for reading many Bytes from the LIS2MDL sensor
00125     *   @param address address of the sensor
00126     *   @param subaddress register location to which to write data
00127     *   @param count number of bytes to read
00128     *   @param dest pointer to the array which will store the read values    
00129     */
00130     void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, char * dest);
00131 
00132 protected:
00133     I2C *_i2c_p;
00134     I2C &_i2c;
00135 
00136 };
00137 
00138 #endif