handle master side communication of openIMU300ZI module

Dependencies:   mbed

Dependents:   VDU_2021

Committer:
Arithemetica
Date:
Tue Dec 17 14:30:08 2019 +0000
Revision:
17:629b2f317d0a
Parent:
16:6583ad08d0d0
Child:
19:3ee54f9a56f3
fix the jitter in the singal due to the frequency of spi clock is too high

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arithemetica 7:224ffdacb240 1 /**
Arithemetica 7:224ffdacb240 2 * @file imu_driver.hpp
Arithemetica 10:d8223969c541 3 * @brief ImuDriver class and other data storage struct definition. The C language version
Arithemetica 10:d8223969c541 4 * can be found at <A HREF="https://github.com/osjacky430/imu_driver">this github website</A>
Arithemetica 7:224ffdacb240 5 */
Arithemetica 7:224ffdacb240 6
Arithemetica 1:8e31413068af 7 #ifndef IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 8 #define IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 9
Arithemetica 0:8c01a98a2812 10 #include <mbed.h>
Arithemetica 0:8c01a98a2812 11 #include <cstdint>
Arithemetica 0:8c01a98a2812 12
Arithemetica 14:6c31a7ab45f0 13 typedef enum ImuExtiConfig {
Arithemetica 14:6c31a7ab45f0 14 ImuExtiRcvNotUsed = 0b00,
Arithemetica 14:6c31a7ab45f0 15 ImuExtiRcvNormalMsg = 0b01,
Arithemetica 14:6c31a7ab45f0 16 ImuExtiRcvAhrsMsg = 0b10
Arithemetica 14:6c31a7ab45f0 17 } ImuExtiConfig;
Arithemetica 14:6c31a7ab45f0 18
Arithemetica 17:629b2f317d0a 19 /**
Arithemetica 17:629b2f317d0a 20 * @brief ImuDriverStatus
Arithemetica 17:629b2f317d0a 21 */
Arithemetica 12:bb490978e153 22 typedef enum ImuDriverStatus {
Arithemetica 14:6c31a7ab45f0 23 ImuDriverStatusOK,
Arithemetica 14:6c31a7ab45f0 24 ImuDriverStatusDataNotReady,
Arithemetica 14:6c31a7ab45f0 25 ImuDriverStatusInvalidCall
Arithemetica 14:6c31a7ab45f0 26 } ImuDriverStatus;
Arithemetica 12:bb490978e153 27
Arithemetica 0:8c01a98a2812 28 typedef enum ImuDriverRegister {
Arithemetica 0:8c01a98a2812 29 ReadAhrsBurstDataRegister = 0x3D,
Arithemetica 0:8c01a98a2812 30 ReadBurstDataRegister = 0x3E,
Arithemetica 0:8c01a98a2812 31 ReadExtBurstDataRegister = 0x3F
Arithemetica 0:8c01a98a2812 32 } ImuDriverRegister;
Arithemetica 0:8c01a98a2812 33
Arithemetica 0:8c01a98a2812 34 typedef struct AhrsRawData {
Arithemetica 0:8c01a98a2812 35 std::uint16_t status;
Arithemetica 0:8c01a98a2812 36 std::int16_t attitude[3];
Arithemetica 0:8c01a98a2812 37 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 38 } AhrsRawData;
Arithemetica 0:8c01a98a2812 39
Arithemetica 10:d8223969c541 40 /**
Arithemetica 12:bb490978e153 41 * @brief AhrsProcessedData is data storage class fpr processed AHRS data
Arithemetica 14:6c31a7ab45f0 42 *
Arithemetica 11:866dd73e4ab3 43 * The data storage struct consists of:
Arithemetica 11:866dd73e4ab3 44 *
Arithemetica 10:d8223969c541 45 * - attitude: pitch, roll, yaw in deg, range from -180 ~ +180 deg
Arithemetica 10:d8223969c541 46 */
Arithemetica 0:8c01a98a2812 47 typedef struct AhrsProcessedData {
Arithemetica 0:8c01a98a2812 48 float attitude[3];
Arithemetica 0:8c01a98a2812 49 } AhrsProcessedData;
Arithemetica 0:8c01a98a2812 50
Arithemetica 0:8c01a98a2812 51 typedef struct ImuRawData {
Arithemetica 0:8c01a98a2812 52 std::uint16_t status;
Arithemetica 0:8c01a98a2812 53 std::int16_t rate[3];
Arithemetica 0:8c01a98a2812 54 std::int16_t accel[3];
Arithemetica 0:8c01a98a2812 55 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 56 } ImuRawData;
Arithemetica 0:8c01a98a2812 57
Arithemetica 10:d8223969c541 58 /**
Arithemetica 11:866dd73e4ab3 59 * @brief ImuProcessedData is data storage class for processed imu data
Arithemetica 10:d8223969c541 60 *
Arithemetica 11:866dd73e4ab3 61 * This data storage class consists of:
Arithemetica 14:6c31a7ab45f0 62 *
Arithemetica 10:d8223969c541 63 * - status: if status = 0x0010 (over range error), status = 0 (no error)
Arithemetica 10:d8223969c541 64 * - gyroscope: deg/s, range from -300 deg/s ~ 300 deg/s
Arithemetica 10:d8223969c541 65 * - accelerometer: g, range from -4.5g ~ 4.5g
Arithemetica 10:d8223969c541 66 * - temperature: Celcius
Arithemetica 10:d8223969c541 67 */
Arithemetica 0:8c01a98a2812 68 typedef struct ImuProcessedData {
Arithemetica 0:8c01a98a2812 69 std::uint16_t status;
Arithemetica 0:8c01a98a2812 70
Arithemetica 0:8c01a98a2812 71 float rate[3];
Arithemetica 0:8c01a98a2812 72 float accel[3];
Arithemetica 0:8c01a98a2812 73
Arithemetica 0:8c01a98a2812 74 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 75 } ImuProcessedData;
Arithemetica 0:8c01a98a2812 76
Arithemetica 0:8c01a98a2812 77 /**
Arithemetica 12:bb490978e153 78 * @brief ImuDriver is used to handle the master side of SPI communication of openIMU300ZI module, the
Arithemetica 2:f3a7def7a7e1 79 * default setups are:
Arithemetica 0:8c01a98a2812 80 *
Arithemetica 2:f3a7def7a7e1 81 * - Data frame: 16 bits
Arithemetica 2:f3a7def7a7e1 82 * - CPOL: High (1)
Arithemetica 2:f3a7def7a7e1 83 * - CPHA: 2 Edge (1)
Arithemetica 17:629b2f317d0a 84 * - Frequency: 1 MHz (Default frequency of spi)
Arithemetica 2:f3a7def7a7e1 85 * - Endian: MSB first (Mbed only support MSB AFAIK)
Arithemetica 5:e71931fcae33 86 *
Arithemetica 10:d8223969c541 87 * According to <A HREF="https://openimu.readthedocs.io/en/latest/software/SPImessaging.html">openIMU300ZI SPI framework</A>:
Arithemetica 5:e71931fcae33 88 *
Arithemetica 3:8552e26cd162 89 * - Data transferred in 16-bit word-length and MSB-first
Arithemetica 5:e71931fcae33 90 * - fCLK ≤ 2.0 MHz
Arithemetica 3:8552e26cd162 91 * - CPOL = 1 (clock polarity) and CPHA = 1 (clock phase)
Arithemetica 0:8c01a98a2812 92 *
Arithemetica 2:f3a7def7a7e1 93 * @tparam spi SPI comm instance, ImuDriver currently support only SPI framework
Arithemetica 2:f3a7def7a7e1 94 * @tparam rst Reset pin name, this is used to reset the openIMU300ZI module
Arithemetica 2:f3a7def7a7e1 95 * @tparam drdy Data ready pin name, this is used as indication of data readiness
Arithemetica 2:f3a7def7a7e1 96 * @tparam ss Slave select pin name, this is used as slave select, pull low to initiate
Arithemetica 2:f3a7def7a7e1 97 * communication process.
Arithemetica 5:e71931fcae33 98 *
Arithemetica 7:224ffdacb240 99 * @todo Attach to exti in the future, and make user able to choose
Arithemetica 7:224ffdacb240 100 *
Arithemetica 0:8c01a98a2812 101 * Example of using ImuDriver:
Arithemetica 0:8c01a98a2812 102 *
Arithemetica 0:8c01a98a2812 103 * @code
Arithemetica 0:8c01a98a2812 104 * #include "imu_driver.hpp"
Arithemetica 0:8c01a98a2812 105 *
Arithemetica 0:8c01a98a2812 106 * SPI spi3(PB_5, PB_6, PB_3); // declare SPI instance globally
Arithemetica 0:8c01a98a2812 107 * Serial pc(USBTX, USBRX, 115200); // print debug message
Arithemetica 0:8c01a98a2812 108 *
Arithemetica 0:8c01a98a2812 109 * int main()
Arithemetica 0:8c01a98a2812 110 * {
Arithemetica 5:e71931fcae33 111 * // SPI instance, reset, data ready, slave select
Arithemetica 17:629b2f317d0a 112 * ImuDriver<spi3, PA_10, PA_8, PA_9> imu(ImuExtiRcvNormalMsg);
Arithemetica 0:8c01a98a2812 113 *
Arithemetica 0:8c01a98a2812 114 * while(true)
Arithemetica 0:8c01a98a2812 115 * {
Arithemetica 17:629b2f317d0a 116 * pc.printf("%.3f, %.3f, %.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
Arithemetica 0:8c01a98a2812 117 * }
Arithemetica 0:8c01a98a2812 118 * }
Arithemetica 5:e71931fcae33 119 *
Arithemetica 0:8c01a98a2812 120 * @endcode
Arithemetica 0:8c01a98a2812 121 */
Arithemetica 2:f3a7def7a7e1 122 template <SPI& Spi, PinName rst, PinName drdy, PinName ss>
Arithemetica 0:8c01a98a2812 123 class ImuDriver
Arithemetica 0:8c01a98a2812 124 {
Arithemetica 0:8c01a98a2812 125 private:
Arithemetica 14:6c31a7ab45f0 126 ImuExtiConfig m_extiConfig;
Arithemetica 14:6c31a7ab45f0 127
Arithemetica 0:8c01a98a2812 128 SPI& m_spi;
Arithemetica 0:8c01a98a2812 129 DigitalOut m_rst, m_ss;
Arithemetica 0:8c01a98a2812 130 DigitalIn m_drdy;
Arithemetica 2:f3a7def7a7e1 131 InterruptIn m_drdyExti;
Arithemetica 5:e71931fcae33 132
Arithemetica 0:8c01a98a2812 133 float m_gyroScaler[3];
Arithemetica 0:8c01a98a2812 134 float m_accelScaler[3];
Arithemetica 0:8c01a98a2812 135 float m_ahrsScaler[3];
Arithemetica 0:8c01a98a2812 136 private:
Arithemetica 0:8c01a98a2812 137 std::uint16_t m_imuSpiWrite(const std::uint16_t val) const
Arithemetica 0:8c01a98a2812 138 {
Arithemetica 0:8c01a98a2812 139 // RAII
Arithemetica 0:8c01a98a2812 140 class SpiLock
Arithemetica 0:8c01a98a2812 141 {
Arithemetica 0:8c01a98a2812 142 private:
Arithemetica 0:8c01a98a2812 143 SPI& m_spi;
Arithemetica 0:8c01a98a2812 144 public:
Arithemetica 0:8c01a98a2812 145 SpiLock(SPI& t_spi) : m_spi(t_spi)
Arithemetica 0:8c01a98a2812 146 {
Arithemetica 0:8c01a98a2812 147 m_spi.lock();
Arithemetica 0:8c01a98a2812 148 }
Arithemetica 0:8c01a98a2812 149
Arithemetica 0:8c01a98a2812 150 ~SpiLock()
Arithemetica 0:8c01a98a2812 151 {
Arithemetica 0:8c01a98a2812 152 m_spi.unlock();
Arithemetica 0:8c01a98a2812 153 }
Arithemetica 0:8c01a98a2812 154 };
Arithemetica 0:8c01a98a2812 155
Arithemetica 0:8c01a98a2812 156 SpiLock lock(m_spi);
Arithemetica 0:8c01a98a2812 157 return m_spi.write(val);
Arithemetica 0:8c01a98a2812 158 }
Arithemetica 0:8c01a98a2812 159
Arithemetica 0:8c01a98a2812 160 inline std::uint16_t m_spiGenerateReadCmd(const std::uint8_t t_val) const
Arithemetica 0:8c01a98a2812 161 {
Arithemetica 0:8c01a98a2812 162 return t_val << 8U;
Arithemetica 0:8c01a98a2812 163 }
Arithemetica 0:8c01a98a2812 164
Arithemetica 0:8c01a98a2812 165 inline bool m_spiIsDataReady()
Arithemetica 0:8c01a98a2812 166 {
Arithemetica 0:8c01a98a2812 167 return m_drdy.read() == 0;
Arithemetica 0:8c01a98a2812 168 }
Arithemetica 0:8c01a98a2812 169
Arithemetica 0:8c01a98a2812 170 inline void m_processImuRawData()
Arithemetica 0:8c01a98a2812 171 {
Arithemetica 0:8c01a98a2812 172 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 173 imuProcessedData.accel[i] = imuRawData.accel[i] / m_accelScaler[i];
Arithemetica 0:8c01a98a2812 174 imuProcessedData.rate[i] = imuRawData.rate[i] / m_gyroScaler[i];
Arithemetica 0:8c01a98a2812 175 }
Arithemetica 0:8c01a98a2812 176 }
Arithemetica 0:8c01a98a2812 177
Arithemetica 0:8c01a98a2812 178 inline void m_processAhrsRawData()
Arithemetica 0:8c01a98a2812 179 {
Arithemetica 0:8c01a98a2812 180 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 181 ahrsProcessedData.attitude[i] = ahrsRawData.attitude[i] / m_ahrsScaler[i];
Arithemetica 0:8c01a98a2812 182 }
Arithemetica 0:8c01a98a2812 183 }
Arithemetica 5:e71931fcae33 184
Arithemetica 14:6c31a7ab45f0 185 void m_dataReadyExtiCallback()
Arithemetica 2:f3a7def7a7e1 186 {
Arithemetica 14:6c31a7ab45f0 187 if (m_extiConfig & ImuExtiRcvNormalMsg) {
Arithemetica 14:6c31a7ab45f0 188 receiveBurstMsgImpl();
Arithemetica 14:6c31a7ab45f0 189 }
Arithemetica 15:ca49fdec90fc 190
Arithemetica 14:6c31a7ab45f0 191 if (m_extiConfig & ImuExtiRcvAhrsMsg) {
Arithemetica 16:6583ad08d0d0 192 receiveAhrsMsgImpl();
Arithemetica 14:6c31a7ab45f0 193 }
Arithemetica 2:f3a7def7a7e1 194 }
Arithemetica 14:6c31a7ab45f0 195 private:
Arithemetica 16:6583ad08d0d0 196 ImuDriver(); // cannot initialize via default constructor
Arithemetica 16:6583ad08d0d0 197
Arithemetica 14:6c31a7ab45f0 198 ImuDriverStatus receiveBurstMsgImpl(bool t_extended = false)
Arithemetica 15:ca49fdec90fc 199 {
Arithemetica 0:8c01a98a2812 200 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 201 std::uint16_t max_data = 0U;
Arithemetica 0:8c01a98a2812 202 std::uint16_t burst_reg = 0U;
Arithemetica 0:8c01a98a2812 203
Arithemetica 0:8c01a98a2812 204 if (!t_extended) {
Arithemetica 0:8c01a98a2812 205 max_data = 8U;
Arithemetica 0:8c01a98a2812 206 burst_reg = m_spiGenerateReadCmd(ReadBurstDataRegister);
Arithemetica 0:8c01a98a2812 207 } else {
Arithemetica 0:8c01a98a2812 208 max_data = 11U;
Arithemetica 0:8c01a98a2812 209 burst_reg = m_spiGenerateReadCmd(ReadExtBurstDataRegister);
Arithemetica 0:8c01a98a2812 210 }
Arithemetica 0:8c01a98a2812 211
Arithemetica 0:8c01a98a2812 212 m_ss.write(0); // start transfer
Arithemetica 0:8c01a98a2812 213 m_imuSpiWrite(burst_reg);
Arithemetica 0:8c01a98a2812 214
Arithemetica 0:8c01a98a2812 215 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 216
Arithemetica 0:8c01a98a2812 217 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 218 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 219 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 220 case 0:
Arithemetica 0:8c01a98a2812 221 imuRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 222 break;
Arithemetica 0:8c01a98a2812 223 case 1:
Arithemetica 0:8c01a98a2812 224 case 2:
Arithemetica 0:8c01a98a2812 225 case 3:
Arithemetica 0:8c01a98a2812 226 imuRawData.rate[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 227 break;
Arithemetica 0:8c01a98a2812 228 case 4:
Arithemetica 0:8c01a98a2812 229 case 5:
Arithemetica 0:8c01a98a2812 230 case 6:
Arithemetica 0:8c01a98a2812 231 imuRawData.accel[data_rcved - 4] = spi_data;
Arithemetica 0:8c01a98a2812 232 break;
Arithemetica 0:8c01a98a2812 233 case 7:
Arithemetica 0:8c01a98a2812 234 imuRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 235 break;
Arithemetica 0:8c01a98a2812 236 default:
Arithemetica 0:8c01a98a2812 237 break;
Arithemetica 0:8c01a98a2812 238 }
Arithemetica 0:8c01a98a2812 239
Arithemetica 0:8c01a98a2812 240 ++data_rcved;
Arithemetica 0:8c01a98a2812 241 }
Arithemetica 0:8c01a98a2812 242
Arithemetica 0:8c01a98a2812 243 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 244 m_ss.write(1);
Arithemetica 0:8c01a98a2812 245
Arithemetica 0:8c01a98a2812 246 m_processImuRawData();
Arithemetica 0:8c01a98a2812 247 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 248 } else {
Arithemetica 0:8c01a98a2812 249 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 250 }
Arithemetica 0:8c01a98a2812 251 }
Arithemetica 15:ca49fdec90fc 252
Arithemetica 15:ca49fdec90fc 253 ImuDriverStatus receiveAhrsMsgImpl()
Arithemetica 15:ca49fdec90fc 254 {
Arithemetica 15:ca49fdec90fc 255 if (m_spiIsDataReady()) {
Arithemetica 15:ca49fdec90fc 256 static std::uint8_t data_rcved = 0U;
Arithemetica 15:ca49fdec90fc 257 const std::uint8_t max_data = 5U;
Arithemetica 15:ca49fdec90fc 258 const std::uint16_t ahrs_reg = m_spiGenerateReadCmd(ReadAhrsBurstDataRegister);
Arithemetica 15:ca49fdec90fc 259
Arithemetica 15:ca49fdec90fc 260 m_ss.write(0);
Arithemetica 15:ca49fdec90fc 261 m_imuSpiWrite(ahrs_reg);
Arithemetica 15:ca49fdec90fc 262
Arithemetica 15:ca49fdec90fc 263 while(data_rcved < max_data) {
Arithemetica 15:ca49fdec90fc 264 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 15:ca49fdec90fc 265 switch(data_rcved) {
Arithemetica 15:ca49fdec90fc 266 case 0:
Arithemetica 15:ca49fdec90fc 267 ahrsRawData.status = spi_data;
Arithemetica 15:ca49fdec90fc 268 break;
Arithemetica 15:ca49fdec90fc 269 case 1:
Arithemetica 15:ca49fdec90fc 270 case 2:
Arithemetica 15:ca49fdec90fc 271 case 3:
Arithemetica 15:ca49fdec90fc 272 ahrsRawData.attitude[data_rcved - 1] = spi_data;
Arithemetica 15:ca49fdec90fc 273 break;
Arithemetica 15:ca49fdec90fc 274 case 4:
Arithemetica 15:ca49fdec90fc 275 ahrsRawData.temperature = spi_data;
Arithemetica 15:ca49fdec90fc 276 break;
Arithemetica 15:ca49fdec90fc 277 default:
Arithemetica 15:ca49fdec90fc 278 break;
Arithemetica 15:ca49fdec90fc 279 }
Arithemetica 15:ca49fdec90fc 280
Arithemetica 15:ca49fdec90fc 281 ++data_rcved;
Arithemetica 17:629b2f317d0a 282
Arithemetica 15:ca49fdec90fc 283 }
Arithemetica 15:ca49fdec90fc 284
Arithemetica 15:ca49fdec90fc 285 data_rcved = 0U;
Arithemetica 15:ca49fdec90fc 286 m_ss.write(1);
Arithemetica 15:ca49fdec90fc 287
Arithemetica 15:ca49fdec90fc 288 m_processAhrsRawData();
Arithemetica 15:ca49fdec90fc 289 return ImuDriverStatusOK;
Arithemetica 15:ca49fdec90fc 290 } else {
Arithemetica 15:ca49fdec90fc 291 return ImuDriverStatusDataNotReady;
Arithemetica 15:ca49fdec90fc 292 }
Arithemetica 15:ca49fdec90fc 293 }
Arithemetica 14:6c31a7ab45f0 294 public:
Arithemetica 14:6c31a7ab45f0 295 /**
Arithemetica 14:6c31a7ab45f0 296 * imuRawData contains raw data received from @ref receiveBurstMsg(bool t_extended)
Arithemetica 14:6c31a7ab45f0 297 */
Arithemetica 14:6c31a7ab45f0 298 ImuRawData imuRawData;
Arithemetica 14:6c31a7ab45f0 299
Arithemetica 14:6c31a7ab45f0 300 /**
Arithemetica 14:6c31a7ab45f0 301 * imuProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 302 */
Arithemetica 14:6c31a7ab45f0 303 ImuProcessedData imuProcessedData;
Arithemetica 14:6c31a7ab45f0 304
Arithemetica 14:6c31a7ab45f0 305 /**
Arithemetica 14:6c31a7ab45f0 306 * ahrsRawaData contains raw data received from @ref receiveAhrsMsg()
Arithemetica 14:6c31a7ab45f0 307 */
Arithemetica 14:6c31a7ab45f0 308 AhrsRawData ahrsRawData;
Arithemetica 14:6c31a7ab45f0 309
Arithemetica 14:6c31a7ab45f0 310 /**
Arithemetica 14:6c31a7ab45f0 311 * ahrsProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 312 */
Arithemetica 14:6c31a7ab45f0 313 AhrsProcessedData ahrsProcessedData;
Arithemetica 14:6c31a7ab45f0 314
Arithemetica 14:6c31a7ab45f0 315 static const int DEFAULT_IMU_ACCEL_SCALER = 4000;
Arithemetica 14:6c31a7ab45f0 316 static const int DEFAULT_IMU_GYRO_SCALER = 100;
Arithemetica 14:6c31a7ab45f0 317 static const int DEFAULT_AHRS_ATTITUDE_SCALER = 90;
Arithemetica 14:6c31a7ab45f0 318 public:
Arithemetica 14:6c31a7ab45f0 319 explicit ImuDriver(const ImuExtiConfig t_exti_config) : m_extiConfig(t_exti_config), m_spi(Spi), m_rst(rst), m_ss(ss), m_drdy(drdy), m_drdyExti(drdy)
Arithemetica 14:6c31a7ab45f0 320 {
Arithemetica 14:6c31a7ab45f0 321 for (int i = 0; i < 3; ++i) {
Arithemetica 14:6c31a7ab45f0 322 m_gyroScaler[i] = static_cast<float>(DEFAULT_IMU_GYRO_SCALER); // what an idiotic way of initialization LUL
Arithemetica 14:6c31a7ab45f0 323 m_accelScaler[i] = static_cast<float>(DEFAULT_IMU_ACCEL_SCALER); // Oh, I forgot that mbed os2 still stucks at c++98,
Arithemetica 14:6c31a7ab45f0 324 m_ahrsScaler[i] = static_cast<float>(DEFAULT_AHRS_ATTITUDE_SCALER); // how unfortunate LUL
Arithemetica 14:6c31a7ab45f0 325 }
Arithemetica 14:6c31a7ab45f0 326
Arithemetica 14:6c31a7ab45f0 327 if (t_exti_config != ImuExtiRcvNotUsed) {
Arithemetica 14:6c31a7ab45f0 328 m_drdyExti.fall(this, &ImuDriver<Spi, rst, drdy, ss>::m_dataReadyExtiCallback);
Arithemetica 17:629b2f317d0a 329 m_drdyExti.enable_irq();
Arithemetica 14:6c31a7ab45f0 330 }
Arithemetica 14:6c31a7ab45f0 331
Arithemetica 14:6c31a7ab45f0 332 m_spi.format(16, 3);
Arithemetica 17:629b2f317d0a 333 m_spi.frequency();
Arithemetica 14:6c31a7ab45f0 334 }
Arithemetica 14:6c31a7ab45f0 335
Arithemetica 14:6c31a7ab45f0 336 /**
Arithemetica 14:6c31a7ab45f0 337 * @brief This function handles the receiving of burst message function, burst message
Arithemetica 14:6c31a7ab45f0 338 * contains accelerometer (g), gyroscope (deg/s), status and temperature data (Celcius)
Arithemetica 14:6c31a7ab45f0 339 *
Arithemetica 14:6c31a7ab45f0 340 * @param t_extended bool to indicate the message type is extended or not, default false
Arithemetica 14:6c31a7ab45f0 341 *
Arithemetica 14:6c31a7ab45f0 342 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 14:6c31a7ab45f0 343 */
Arithemetica 15:ca49fdec90fc 344 ImuDriverStatus receiveBurstMsg(bool t_extended = false)
Arithemetica 15:ca49fdec90fc 345 {
Arithemetica 15:ca49fdec90fc 346 return (m_extiConfig & ImuExtiRcvNormalMsg ? ImuDriverStatusInvalidCall : receiveBurstMsgImpl(t_extended));
Arithemetica 14:6c31a7ab45f0 347 }
Arithemetica 0:8c01a98a2812 348
Arithemetica 0:8c01a98a2812 349 /**
Arithemetica 5:e71931fcae33 350 * @brief This function handles the receiving of AHRS burst message function, AHRS
Arithemetica 10:d8223969c541 351 * burst message contains the attitude in euler angle (deg).
Arithemetica 0:8c01a98a2812 352 *
Arithemetica 0:8c01a98a2812 353 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 0:8c01a98a2812 354 *
Arithemetica 0:8c01a98a2812 355 * @note This is only suitable for AHRS app, and additional procedure needs to be done
Arithemetica 0:8c01a98a2812 356 * in order to make it work
Arithemetica 0:8c01a98a2812 357 */
Arithemetica 0:8c01a98a2812 358 ImuDriverStatus receiveAhrsMsg()
Arithemetica 0:8c01a98a2812 359 {
Arithemetica 15:ca49fdec90fc 360 return (m_extiConfig & ImuExtiRcvAhrsMsg ? ImuDriverStatusInvalidCall : receiveAhrsMsgImpl());
Arithemetica 15:ca49fdec90fc 361 }
Arithemetica 0:8c01a98a2812 362
Arithemetica 17:629b2f317d0a 363 /**
Arithemetica 17:629b2f317d0a 364 * @brief This function handles the external interrupt option, whether receive AHRS
Arithemetica 17:629b2f317d0a 365 * burst message or normal imu message.
Arithemetica 17:629b2f317d0a 366 *
Arithemetica 17:629b2f317d0a 367 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 17:629b2f317d0a 368 *
Arithemetica 17:629b2f317d0a 369 * @note Never try receiving two messages at the same time
Arithemetica 17:629b2f317d0a 370 */
Arithemetica 17:629b2f317d0a 371 ImuDriverStatus configExti(const ImuExtiConfig t_exti_config) const
Arithemetica 15:ca49fdec90fc 372 {
Arithemetica 14:6c31a7ab45f0 373 m_extiConfig = t_exti_config;
Arithemetica 17:629b2f317d0a 374
Arithemetica 17:629b2f317d0a 375 if (m_extoConfig == ImuExtiRcvNotUsed) {
Arithemetica 17:629b2f317d0a 376 m_drdyExti.disable_irq();
Arithemetica 17:629b2f317d0a 377 } else {
Arithemetica 17:629b2f317d0a 378 m_drdyExti.enable_irq();
Arithemetica 17:629b2f317d0a 379 }
Arithemetica 17:629b2f317d0a 380
Arithemetica 17:629b2f317d0a 381 return ImuDriverStatusOK;
Arithemetica 14:6c31a7ab45f0 382 }
Arithemetica 1:8e31413068af 383 };
Arithemetica 1:8e31413068af 384
Arithemetica 1:8e31413068af 385 #endif // IMU_DRIVER_HPP_