LIS3MDL I2C Mbed class with compass heading function

Committer:
gpmbed
Date:
Tue Apr 13 13:52:14 2021 +0000
Revision:
1:fe199024ddfe
Parent:
0:4aa6b3804281
comments removed

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
gpmbed 0:4aa6b3804281 127
gpmbed 0:4aa6b3804281 128 private:
gpmbed 0:4aa6b3804281 129 I2C i2c;
gpmbed 0:4aa6b3804281 130 int i2c_write(uint8_t DeviceAddr, uint8_t RegisterAddr,uint8_t value, uint16_t NumByteToWrite);
gpmbed 0:4aa6b3804281 131 int i2c_read(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead);
gpmbed 0:4aa6b3804281 132 bool writeRegister(const uint8_t register_addr, const uint8_t value);
gpmbed 0:4aa6b3804281 133 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 134 bool writeMaskedRegister(const uint8_t register_addr, const uint8_t mask, const bool value);
gpmbed 0:4aa6b3804281 135 bool writeMaskedRegister(const int register_addr, const int mask, const int value);
gpmbed 0:4aa6b3804281 136 uint8_t readRegister(const uint8_t register_addr);
gpmbed 0:4aa6b3804281 137 uint16_t readRegisters(const uint8_t msb_register, const uint8_t lsb_register);
gpmbed 0:4aa6b3804281 138 uint8_t readMaskedRegister(const uint8_t register_addr, const uint8_t mask);
gpmbed 0:4aa6b3804281 139
gpmbed 0:4aa6b3804281 140 uint8_t _address;
gpmbed 0:4aa6b3804281 141 uint8_t _whoami;
gpmbed 0:4aa6b3804281 142 };
gpmbed 0:4aa6b3804281 143
gpmbed 0:4aa6b3804281 144
gpmbed 0:4aa6b3804281 145
gpmbed 0:4aa6b3804281 146 #endif
gpmbed 0:4aa6b3804281 147
gpmbed 0:4aa6b3804281 148
gpmbed 0:4aa6b3804281 149