Gian Paolo Donnarumma / LIS3MDL
Committer:
gpmbed
Date:
Tue Apr 13 13:46:21 2021 +0000
Revision:
0:4aa6b3804281
Child:
1:fe199024ddfe
LIS3MDL lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gpmbed 0:4aa6b3804281 1 #ifndef LIS3MDL_h
gpmbed 0:4aa6b3804281 2 #define LIS3MDL_h
gpmbed 0:4aa6b3804281 3
gpmbed 0:4aa6b3804281 4 #include "mbed.h"
gpmbed 0:4aa6b3804281 5 #include "vector.h"
gpmbed 0:4aa6b3804281 6
gpmbed 0:4aa6b3804281 7 #define LIS3MDL_PI 3.1415926535897932384626433832795
gpmbed 0:4aa6b3804281 8 #define LIS3MDL_Rad2Degree 57.295779513082320876798154814105
gpmbed 0:4aa6b3804281 9
gpmbed 0:4aa6b3804281 10 //Registers
gpmbed 0:4aa6b3804281 11 #define LIS3MDL_WHO_AM_I 0x0F
gpmbed 0:4aa6b3804281 12 #define LIS3MDL_CTRL_REG1 0x20
gpmbed 0:4aa6b3804281 13 #define LIS3MDL_CTRL_REG2 0x21
gpmbed 0:4aa6b3804281 14 #define LIS3MDL_CTRL_REG3 0x22
gpmbed 0:4aa6b3804281 15 #define LIS3MDL_CTRL_REG4 0x23
gpmbed 0:4aa6b3804281 16 #define LIS3MDL_CTRL_REG5 0x24
gpmbed 0:4aa6b3804281 17 #define LIS3MDL_STATUS_REG 0x27
gpmbed 0:4aa6b3804281 18 #define LIS3MDL_OUT_X_L 0x28
gpmbed 0:4aa6b3804281 19 #define LIS3MDL_OUT_X_H 0x29
gpmbed 0:4aa6b3804281 20 #define LIS3MDL_OUT_Y_L 0x2A
gpmbed 0:4aa6b3804281 21 #define LIS3MDL_OUT_Y_H 0x2B
gpmbed 0:4aa6b3804281 22 #define LIS3MDL_OUT_Z_L 0x2C
gpmbed 0:4aa6b3804281 23 #define LIS3MDL_OUT_Z_H 0x2D
gpmbed 0:4aa6b3804281 24 #define LIS3MDL_TEMP_OUT_L 0x2E
gpmbed 0:4aa6b3804281 25 #define LIS3MDL_TEMP_OUT_H 0x2F
gpmbed 0:4aa6b3804281 26 #define LIS3MDL_INT_CFG 0x30
gpmbed 0:4aa6b3804281 27 #define LIS3MDL_INT_SRC 0x31
gpmbed 0:4aa6b3804281 28 #define LIS3MDL_INT_THS_L 0x32
gpmbed 0:4aa6b3804281 29 #define LIS3MDL_INT_THS_H 0x33
gpmbed 0:4aa6b3804281 30
gpmbed 0:4aa6b3804281 31 #define LIS3MDL_ADDRESS 0x3C
gpmbed 0:4aa6b3804281 32
gpmbed 0:4aa6b3804281 33 //Register Masks
gpmbed 0:4aa6b3804281 34
gpmbed 0:4aa6b3804281 35 //WHO_AM_I masks
gpmbed 0:4aa6b3804281 36 #define LIS3MDL_I_AM_MASK 0x3D
gpmbed 0:4aa6b3804281 37
gpmbed 0:4aa6b3804281 38 // CTRL_REG1 masks
gpmbed 0:4aa6b3804281 39 #define LIS3MDL_TEMP_EN_MASK 0xF0
gpmbed 0:4aa6b3804281 40 #define LIS3MDL_OM_MASK 0x06
gpmbed 0:4aa6b3804281 41 #define LIS3MDL_DO_MASK 0x1C
gpmbed 0:4aa6b3804281 42 #define LIS3MDL_ST_MASK 0x01
gpmbed 0:4aa6b3804281 43
gpmbed 0:4aa6b3804281 44 // CTRL_REG2 masks
gpmbed 0:4aa6b3804281 45 #define LIS3MDL_FS_MASK 0x60
gpmbed 0:4aa6b3804281 46 #define LIS3MDL_REBOOT_MASK 0x08
gpmbed 0:4aa6b3804281 47 #define LIS3MDL_SOFT_RST_MASK 0x04
gpmbed 0:4aa6b3804281 48
gpmbed 0:4aa6b3804281 49 // CTRL_REG3 masks
gpmbed 0:4aa6b3804281 50 #define LIS3MDL_LP_MASK 0x20
gpmbed 0:4aa6b3804281 51 #define LIS3MDL_SIM_MASK 0x04
gpmbed 0:4aa6b3804281 52 #define LIS3MDL_MD_MASK 0x03
gpmbed 0:4aa6b3804281 53
gpmbed 0:4aa6b3804281 54 // CTRL_REG4 masks
gpmbed 0:4aa6b3804281 55 #define LIS3MDL_OMZ_MASK 0x0C
gpmbed 0:4aa6b3804281 56 #define LIS3MDL_BLE_MASK 0x02
gpmbed 0:4aa6b3804281 57
gpmbed 0:4aa6b3804281 58 // CTRL_REG5 masks
gpmbed 0:4aa6b3804281 59 #define LIS3MDL_BDU_MASK 0x40
gpmbed 0:4aa6b3804281 60
gpmbed 0:4aa6b3804281 61 // STATUS_REG masks
gpmbed 0:4aa6b3804281 62 #define LIS3MDL_ZYXOR_MASK 0x80
gpmbed 0:4aa6b3804281 63 #define LIS3MDL_ZOR_MASK 0x40
gpmbed 0:4aa6b3804281 64 #define LIS3MDL_YOR_MASK 0x20
gpmbed 0:4aa6b3804281 65 #define LIS3MDL_XOR_MASK 0x10
gpmbed 0:4aa6b3804281 66 #define LIS3MDL_ZYXDA_MASK 0x08
gpmbed 0:4aa6b3804281 67 #define LIS3MDL_ZDA_MASK 0x04
gpmbed 0:4aa6b3804281 68 #define LIS3MDL_YDA_MASK 0x02
gpmbed 0:4aa6b3804281 69 #define LIS3MDL_XDA_MASK 0x01
gpmbed 0:4aa6b3804281 70
gpmbed 0:4aa6b3804281 71 // INT_CFG masks
gpmbed 0:4aa6b3804281 72 #define LIS3MDL_XIEN_MASK 0x80
gpmbed 0:4aa6b3804281 73 #define LIS3MDL_YIEN_MASK 0x40
gpmbed 0:4aa6b3804281 74 #define LIS3MDL_ZIEN_MASK 0x20
gpmbed 0:4aa6b3804281 75 #define LIS3MDL_IEA_MASK 0x04
gpmbed 0:4aa6b3804281 76 #define LIS3MDL_LIR_MASK 0x02
gpmbed 0:4aa6b3804281 77 #define LIS3MDL_IEN_MASK 0x01
gpmbed 0:4aa6b3804281 78
gpmbed 0:4aa6b3804281 79 // INT_SRC masks
gpmbed 0:4aa6b3804281 80 #define LIS3MDL_PTH_X_MASK 0x80
gpmbed 0:4aa6b3804281 81 #define LIS3MDL_PTH_Y_MASK 0x40
gpmbed 0:4aa6b3804281 82 #define LIS3MDL_PTH_Z_MASK 0x20
gpmbed 0:4aa6b3804281 83 #define LIS3MDL_NTH_X_MASK 0x01
gpmbed 0:4aa6b3804281 84 #define LIS3MDL_NTH_Y_MASK 0x08
gpmbed 0:4aa6b3804281 85 #define LIS3MDL_NTH_Z_MASK 0x04
gpmbed 0:4aa6b3804281 86 #define LIS3MDL_MROI_MASK 0x02
gpmbed 0:4aa6b3804281 87 #define LIS3MDL_INT_MASK 0x01
gpmbed 0:4aa6b3804281 88
gpmbed 0:4aa6b3804281 89 // INT_THS_L masks
gpmbed 0:4aa6b3804281 90 #define LIS3MDL_THS_L_MASK 0xFF
gpmbed 0:4aa6b3804281 91
gpmbed 0:4aa6b3804281 92 // INT_THS_H masks
gpmbed 0:4aa6b3804281 93 #define LIS3MDL_THS_H_MASK 0x7F
gpmbed 0:4aa6b3804281 94
gpmbed 0:4aa6b3804281 95 //typedef enum { false, true } bool;
gpmbed 0:4aa6b3804281 96
gpmbed 0:4aa6b3804281 97 class LIS3MDL
gpmbed 0:4aa6b3804281 98 {
gpmbed 0:4aa6b3804281 99 /*
gpmbed 0:4aa6b3804281 100 public:
gpmbed 0:4aa6b3804281 101 template <typename T> struct vector
gpmbed 0:4aa6b3804281 102 {
gpmbed 0:4aa6b3804281 103 T x, y, z;
gpmbed 0:4aa6b3804281 104 };
gpmbed 0:4aa6b3804281 105 */
gpmbed 0:4aa6b3804281 106 public:
gpmbed 0:4aa6b3804281 107 /*CONSTRUCTOR:
gpmbed 0:4aa6b3804281 108 * Create itself the i2c serial line
gpmbed 0:4aa6b3804281 109 */
gpmbed 0:4aa6b3804281 110 LIS3MDL(PinName sda, PinName scl);
gpmbed 0:4aa6b3804281 111 bool init(void);
gpmbed 0:4aa6b3804281 112
gpmbed 0:4aa6b3804281 113 int16_t getAxisX(void);
gpmbed 0:4aa6b3804281 114 int16_t getAxisY(void);
gpmbed 0:4aa6b3804281 115 int16_t getAxisZ(void);
gpmbed 0:4aa6b3804281 116
gpmbed 0:4aa6b3804281 117 float getAxisX_mag();
gpmbed 0:4aa6b3804281 118 float getAxisY_mag();
gpmbed 0:4aa6b3804281 119 float getHeading();
gpmbed 0:4aa6b3804281 120
gpmbed 0:4aa6b3804281 121 int16_t getTemperature(void);
gpmbed 0:4aa6b3804281 122 void getMag(int16_t* ax, int16_t* ay, int16_t* az);
gpmbed 0:4aa6b3804281 123
gpmbed 0:4aa6b3804281 124 bool whoAmI(void);
gpmbed 0:4aa6b3804281 125
gpmbed 0:4aa6b3804281 126 bool getTempEnabled(void);
gpmbed 0:4aa6b3804281 127 bool setTempEnabled(bool enable);
gpmbed 0:4aa6b3804281 128 uint8_t getOperativeMode(void);
gpmbed 0:4aa6b3804281 129 bool setOperativeMode(uint8_t mode);
gpmbed 0:4aa6b3804281 130 uint8_t getDataRate(void);
gpmbed 0:4aa6b3804281 131 bool setDataRate(uint8_t rate);
gpmbed 0:4aa6b3804281 132 bool getSelfTestEnabled(void);
gpmbed 0:4aa6b3804281 133 bool setSelfTestEnabled(bool enable);
gpmbed 0:4aa6b3804281 134
gpmbed 0:4aa6b3804281 135 uint8_t getFullScale(void);
gpmbed 0:4aa6b3804281 136 bool setFullScale(uint8_t sccale);
gpmbed 0:4aa6b3804281 137 bool reboot(void);
gpmbed 0:4aa6b3804281 138 bool softReset(void);
gpmbed 0:4aa6b3804281 139
gpmbed 0:4aa6b3804281 140 bool getLowPowerMode(void);
gpmbed 0:4aa6b3804281 141 bool setLowPowerMode(bool mode);
gpmbed 0:4aa6b3804281 142 bool getSPIMode(void);
gpmbed 0:4aa6b3804281 143 bool setSPIMode(bool mode);
gpmbed 0:4aa6b3804281 144 uint8_t getOperatingMode(void);
gpmbed 0:4aa6b3804281 145 bool setOperatingMode(uint8_t mode);
gpmbed 0:4aa6b3804281 146
gpmbed 0:4aa6b3804281 147 uint8_t getOMZ(void);
gpmbed 0:4aa6b3804281 148 bool setOMZ(uint8_t mode);
gpmbed 0:4aa6b3804281 149 bool getEndian(void);
gpmbed 0:4aa6b3804281 150 bool setEndian(bool selection);
gpmbed 0:4aa6b3804281 151
gpmbed 0:4aa6b3804281 152 bool disableDataUpdate(void);
gpmbed 0:4aa6b3804281 153 bool enableDataUpdate(void);
gpmbed 0:4aa6b3804281 154 bool getDataUpdateStatus(void);
gpmbed 0:4aa6b3804281 155
gpmbed 0:4aa6b3804281 156 bool getXYZOverrun(void);
gpmbed 0:4aa6b3804281 157 bool getZOverrun(void);
gpmbed 0:4aa6b3804281 158 bool getYOverrun(void);
gpmbed 0:4aa6b3804281 159 bool getXOverrun(void);
gpmbed 0:4aa6b3804281 160
gpmbed 0:4aa6b3804281 161 bool getXYZDataAvailable(void);
gpmbed 0:4aa6b3804281 162 bool getZDataAvailable(void);
gpmbed 0:4aa6b3804281 163 bool getYDataAvailabl(void);
gpmbed 0:4aa6b3804281 164 bool getXDataAvailable(void);
gpmbed 0:4aa6b3804281 165
gpmbed 0:4aa6b3804281 166 bool enableXInterupt(void);
gpmbed 0:4aa6b3804281 167 bool disableXInterupt(void);
gpmbed 0:4aa6b3804281 168 bool isXIntEnabled(void);
gpmbed 0:4aa6b3804281 169 bool enableYInterupt(void);
gpmbed 0:4aa6b3804281 170 bool disableYInterupt(void);
gpmbed 0:4aa6b3804281 171 bool isYIntEnabled(void);
gpmbed 0:4aa6b3804281 172 bool enableZInterupt(void);
gpmbed 0:4aa6b3804281 173 bool disableZInterupt(void);
gpmbed 0:4aa6b3804281 174 bool isZIntEnabled(void);
gpmbed 0:4aa6b3804281 175 bool setIntHigh(void);
gpmbed 0:4aa6b3804281 176 bool setIntLow(void);
gpmbed 0:4aa6b3804281 177 bool checkIntConfig(void);
gpmbed 0:4aa6b3804281 178 bool setInteruptLatch(bool latch);
gpmbed 0:4aa6b3804281 179 bool checkInteruptLatch(void);
gpmbed 0:4aa6b3804281 180 bool enableIntInterupt(void);
gpmbed 0:4aa6b3804281 181 bool disableIntInterupt(void);
gpmbed 0:4aa6b3804281 182 bool checkIntInterupt(void);
gpmbed 0:4aa6b3804281 183
gpmbed 0:4aa6b3804281 184 bool posXThreshold(void);
gpmbed 0:4aa6b3804281 185 bool posYThreshold(void);
gpmbed 0:4aa6b3804281 186 bool posZThreshold(void);
gpmbed 0:4aa6b3804281 187 bool negXThreshold(void);
gpmbed 0:4aa6b3804281 188 bool negYThreshold(void);
gpmbed 0:4aa6b3804281 189 bool negZThreshold(void);
gpmbed 0:4aa6b3804281 190 bool measurementOverflow(void);
gpmbed 0:4aa6b3804281 191 bool checkInteruptEvent(void);
gpmbed 0:4aa6b3804281 192
gpmbed 0:4aa6b3804281 193 void getMotion(int16_t* ax, int16_t* ay, int16_t* az);
gpmbed 0:4aa6b3804281 194
gpmbed 0:4aa6b3804281 195 bool setInteruptThreshold(uint16_t threshold);
gpmbed 0:4aa6b3804281 196 uint16_t getInteruptThreshold(void);
gpmbed 0:4aa6b3804281 197
gpmbed 0:4aa6b3804281 198 private:
gpmbed 0:4aa6b3804281 199 I2C i2c;
gpmbed 0:4aa6b3804281 200 int i2c_write(uint8_t DeviceAddr, uint8_t RegisterAddr,uint8_t value, uint16_t NumByteToWrite);
gpmbed 0:4aa6b3804281 201 int i2c_read(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead);
gpmbed 0:4aa6b3804281 202 bool writeRegister(const uint8_t register_addr, const uint8_t value);
gpmbed 0:4aa6b3804281 203 bool writeRegisters(const uint8_t msb_register, const uint8_t msb_value, const uint8_t lsb_register, const uint8_t lsb_value);
gpmbed 0:4aa6b3804281 204 bool writeMaskedRegister(const uint8_t register_addr, const uint8_t mask, const bool value);
gpmbed 0:4aa6b3804281 205 bool writeMaskedRegister(const int register_addr, const int mask, const int value);
gpmbed 0:4aa6b3804281 206 uint8_t readRegister(const uint8_t register_addr);
gpmbed 0:4aa6b3804281 207 uint16_t readRegisters(const uint8_t msb_register, const uint8_t lsb_register);
gpmbed 0:4aa6b3804281 208 uint8_t readMaskedRegister(const uint8_t register_addr, const uint8_t mask);
gpmbed 0:4aa6b3804281 209
gpmbed 0:4aa6b3804281 210 uint8_t _address;
gpmbed 0:4aa6b3804281 211 uint8_t _whoami;
gpmbed 0:4aa6b3804281 212 };
gpmbed 0:4aa6b3804281 213
gpmbed 0:4aa6b3804281 214
gpmbed 0:4aa6b3804281 215
gpmbed 0:4aa6b3804281 216 #endif
gpmbed 0:4aa6b3804281 217
gpmbed 0:4aa6b3804281 218
gpmbed 0:4aa6b3804281 219