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