handle master side communication of openIMU300ZI module

Dependencies:   mbed

Dependents:   VDU_2021

Committer:
Arithemetica
Date:
Tue Dec 03 14:12:48 2019 +0000
Revision:
14:6c31a7ab45f0
Parent:
13:a743a5e4c485
Child:
15:ca49fdec90fc
Add exti functionality

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 12:bb490978e153 19 typedef enum ImuDriverStatus {
Arithemetica 14:6c31a7ab45f0 20 ImuDriverStatusOK,
Arithemetica 14:6c31a7ab45f0 21 ImuDriverStatusDataNotReady,
Arithemetica 14:6c31a7ab45f0 22 ImuDriverStatusInvalidCall
Arithemetica 14:6c31a7ab45f0 23 } ImuDriverStatus;
Arithemetica 12:bb490978e153 24
Arithemetica 0:8c01a98a2812 25 typedef enum ImuDriverRegister {
Arithemetica 0:8c01a98a2812 26 ReadAhrsBurstDataRegister = 0x3D,
Arithemetica 0:8c01a98a2812 27 ReadBurstDataRegister = 0x3E,
Arithemetica 0:8c01a98a2812 28 ReadExtBurstDataRegister = 0x3F
Arithemetica 0:8c01a98a2812 29 } ImuDriverRegister;
Arithemetica 0:8c01a98a2812 30
Arithemetica 0:8c01a98a2812 31 typedef struct AhrsRawData {
Arithemetica 0:8c01a98a2812 32 std::uint16_t status;
Arithemetica 0:8c01a98a2812 33 std::int16_t attitude[3];
Arithemetica 0:8c01a98a2812 34 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 35 } AhrsRawData;
Arithemetica 0:8c01a98a2812 36
Arithemetica 10:d8223969c541 37 /**
Arithemetica 12:bb490978e153 38 * @brief AhrsProcessedData is data storage class fpr processed AHRS data
Arithemetica 14:6c31a7ab45f0 39 *
Arithemetica 11:866dd73e4ab3 40 * The data storage struct consists of:
Arithemetica 11:866dd73e4ab3 41 *
Arithemetica 10:d8223969c541 42 * - attitude: pitch, roll, yaw in deg, range from -180 ~ +180 deg
Arithemetica 10:d8223969c541 43 */
Arithemetica 0:8c01a98a2812 44 typedef struct AhrsProcessedData {
Arithemetica 0:8c01a98a2812 45 float attitude[3];
Arithemetica 0:8c01a98a2812 46 } AhrsProcessedData;
Arithemetica 0:8c01a98a2812 47
Arithemetica 0:8c01a98a2812 48 typedef struct ImuRawData {
Arithemetica 0:8c01a98a2812 49 std::uint16_t status;
Arithemetica 0:8c01a98a2812 50 std::int16_t rate[3];
Arithemetica 0:8c01a98a2812 51 std::int16_t accel[3];
Arithemetica 0:8c01a98a2812 52 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 53 } ImuRawData;
Arithemetica 0:8c01a98a2812 54
Arithemetica 10:d8223969c541 55 /**
Arithemetica 11:866dd73e4ab3 56 * @brief ImuProcessedData is data storage class for processed imu data
Arithemetica 10:d8223969c541 57 *
Arithemetica 11:866dd73e4ab3 58 * This data storage class consists of:
Arithemetica 14:6c31a7ab45f0 59 *
Arithemetica 10:d8223969c541 60 * - status: if status = 0x0010 (over range error), status = 0 (no error)
Arithemetica 10:d8223969c541 61 * - gyroscope: deg/s, range from -300 deg/s ~ 300 deg/s
Arithemetica 10:d8223969c541 62 * - accelerometer: g, range from -4.5g ~ 4.5g
Arithemetica 10:d8223969c541 63 * - temperature: Celcius
Arithemetica 10:d8223969c541 64 */
Arithemetica 0:8c01a98a2812 65 typedef struct ImuProcessedData {
Arithemetica 0:8c01a98a2812 66 std::uint16_t status;
Arithemetica 0:8c01a98a2812 67
Arithemetica 0:8c01a98a2812 68 float rate[3];
Arithemetica 0:8c01a98a2812 69 float accel[3];
Arithemetica 0:8c01a98a2812 70
Arithemetica 0:8c01a98a2812 71 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 72 } ImuProcessedData;
Arithemetica 0:8c01a98a2812 73
Arithemetica 0:8c01a98a2812 74 /**
Arithemetica 12:bb490978e153 75 * @brief ImuDriver is used to handle the master side of SPI communication of openIMU300ZI module, the
Arithemetica 2:f3a7def7a7e1 76 * default setups are:
Arithemetica 0:8c01a98a2812 77 *
Arithemetica 2:f3a7def7a7e1 78 * - Data frame: 16 bits
Arithemetica 2:f3a7def7a7e1 79 * - CPOL: High (1)
Arithemetica 2:f3a7def7a7e1 80 * - CPHA: 2 Edge (1)
Arithemetica 2:f3a7def7a7e1 81 * - Frequency: 1.40625 MHz (Baudrate prescaler 32 for NUCLEO-F446RE under 180MHz)
Arithemetica 2:f3a7def7a7e1 82 * - Endian: MSB first (Mbed only support MSB AFAIK)
Arithemetica 5:e71931fcae33 83 *
Arithemetica 10:d8223969c541 84 * According to <A HREF="https://openimu.readthedocs.io/en/latest/software/SPImessaging.html">openIMU300ZI SPI framework</A>:
Arithemetica 5:e71931fcae33 85 *
Arithemetica 3:8552e26cd162 86 * - Data transferred in 16-bit word-length and MSB-first
Arithemetica 5:e71931fcae33 87 * - fCLK ≤ 2.0 MHz
Arithemetica 3:8552e26cd162 88 * - CPOL = 1 (clock polarity) and CPHA = 1 (clock phase)
Arithemetica 0:8c01a98a2812 89 *
Arithemetica 2:f3a7def7a7e1 90 * @tparam spi SPI comm instance, ImuDriver currently support only SPI framework
Arithemetica 2:f3a7def7a7e1 91 * @tparam rst Reset pin name, this is used to reset the openIMU300ZI module
Arithemetica 2:f3a7def7a7e1 92 * @tparam drdy Data ready pin name, this is used as indication of data readiness
Arithemetica 2:f3a7def7a7e1 93 * @tparam ss Slave select pin name, this is used as slave select, pull low to initiate
Arithemetica 2:f3a7def7a7e1 94 * communication process.
Arithemetica 5:e71931fcae33 95 *
Arithemetica 7:224ffdacb240 96 * @todo Attach to exti in the future, and make user able to choose
Arithemetica 7:224ffdacb240 97 *
Arithemetica 0:8c01a98a2812 98 * Example of using ImuDriver:
Arithemetica 0:8c01a98a2812 99 *
Arithemetica 0:8c01a98a2812 100 * @code
Arithemetica 0:8c01a98a2812 101 * #include "imu_driver.hpp"
Arithemetica 0:8c01a98a2812 102 *
Arithemetica 0:8c01a98a2812 103 * SPI spi3(PB_5, PB_6, PB_3); // declare SPI instance globally
Arithemetica 0:8c01a98a2812 104 * Serial pc(USBTX, USBRX, 115200); // print debug message
Arithemetica 0:8c01a98a2812 105 *
Arithemetica 0:8c01a98a2812 106 * int main()
Arithemetica 0:8c01a98a2812 107 * {
Arithemetica 5:e71931fcae33 108 * // SPI instance, reset, data ready, slave select
Arithemetica 0:8c01a98a2812 109 * ImuDriver<spi3, PA_10, PA_8, PA_9> imu;
Arithemetica 0:8c01a98a2812 110 *
Arithemetica 0:8c01a98a2812 111 * while(true)
Arithemetica 0:8c01a98a2812 112 * {
Arithemetica 0:8c01a98a2812 113 * if (imu.receiveBurstMsg() == ImuDriverStatusOK)
Arithemetica 0:8c01a98a2812 114 * {
Arithemetica 0:8c01a98a2812 115 * pc.printf("%.3f, %.3f, %.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
Arithemetica 0:8c01a98a2812 116 * }
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 14:6c31a7ab45f0 190
Arithemetica 14:6c31a7ab45f0 191 if (m_extiConfig & ImuExtiRcvAhrsMsg) {
Arithemetica 14:6c31a7ab45f0 192 receiveAhrsMsg();
Arithemetica 14:6c31a7ab45f0 193 }
Arithemetica 2:f3a7def7a7e1 194 }
Arithemetica 14:6c31a7ab45f0 195 private:
Arithemetica 14:6c31a7ab45f0 196 ImuDriverStatus receiveBurstMsgImpl(bool t_extended = false)
Arithemetica 14:6c31a7ab45f0 197 {
Arithemetica 0:8c01a98a2812 198 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 199 std::uint16_t max_data = 0U;
Arithemetica 0:8c01a98a2812 200 std::uint16_t burst_reg = 0U;
Arithemetica 0:8c01a98a2812 201
Arithemetica 0:8c01a98a2812 202 if (!t_extended) {
Arithemetica 0:8c01a98a2812 203 max_data = 8U;
Arithemetica 0:8c01a98a2812 204 burst_reg = m_spiGenerateReadCmd(ReadBurstDataRegister);
Arithemetica 0:8c01a98a2812 205 } else {
Arithemetica 0:8c01a98a2812 206 max_data = 11U;
Arithemetica 0:8c01a98a2812 207 burst_reg = m_spiGenerateReadCmd(ReadExtBurstDataRegister);
Arithemetica 0:8c01a98a2812 208 }
Arithemetica 0:8c01a98a2812 209
Arithemetica 0:8c01a98a2812 210 m_ss.write(0); // start transfer
Arithemetica 0:8c01a98a2812 211 m_imuSpiWrite(burst_reg);
Arithemetica 0:8c01a98a2812 212
Arithemetica 0:8c01a98a2812 213 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 214
Arithemetica 0:8c01a98a2812 215 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 216 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 217 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 218 case 0:
Arithemetica 0:8c01a98a2812 219 imuRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 220 break;
Arithemetica 0:8c01a98a2812 221 case 1:
Arithemetica 0:8c01a98a2812 222 case 2:
Arithemetica 0:8c01a98a2812 223 case 3:
Arithemetica 0:8c01a98a2812 224 imuRawData.rate[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 225 break;
Arithemetica 0:8c01a98a2812 226 case 4:
Arithemetica 0:8c01a98a2812 227 case 5:
Arithemetica 0:8c01a98a2812 228 case 6:
Arithemetica 0:8c01a98a2812 229 imuRawData.accel[data_rcved - 4] = spi_data;
Arithemetica 0:8c01a98a2812 230 break;
Arithemetica 0:8c01a98a2812 231 case 7:
Arithemetica 0:8c01a98a2812 232 imuRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 233 break;
Arithemetica 0:8c01a98a2812 234 default:
Arithemetica 0:8c01a98a2812 235 break;
Arithemetica 0:8c01a98a2812 236 }
Arithemetica 0:8c01a98a2812 237
Arithemetica 0:8c01a98a2812 238 ++data_rcved;
Arithemetica 0:8c01a98a2812 239 }
Arithemetica 0:8c01a98a2812 240
Arithemetica 0:8c01a98a2812 241 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 242 m_ss.write(1);
Arithemetica 0:8c01a98a2812 243
Arithemetica 0:8c01a98a2812 244 m_processImuRawData();
Arithemetica 0:8c01a98a2812 245 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 246 } else {
Arithemetica 0:8c01a98a2812 247 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 248 }
Arithemetica 0:8c01a98a2812 249 }
Arithemetica 14:6c31a7ab45f0 250 public:
Arithemetica 14:6c31a7ab45f0 251 /**
Arithemetica 14:6c31a7ab45f0 252 * imuRawData contains raw data received from @ref receiveBurstMsg(bool t_extended)
Arithemetica 14:6c31a7ab45f0 253 */
Arithemetica 14:6c31a7ab45f0 254 ImuRawData imuRawData;
Arithemetica 14:6c31a7ab45f0 255
Arithemetica 14:6c31a7ab45f0 256 /**
Arithemetica 14:6c31a7ab45f0 257 * imuProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 258 */
Arithemetica 14:6c31a7ab45f0 259 ImuProcessedData imuProcessedData;
Arithemetica 14:6c31a7ab45f0 260
Arithemetica 14:6c31a7ab45f0 261 /**
Arithemetica 14:6c31a7ab45f0 262 * ahrsRawaData contains raw data received from @ref receiveAhrsMsg()
Arithemetica 14:6c31a7ab45f0 263 */
Arithemetica 14:6c31a7ab45f0 264 AhrsRawData ahrsRawData;
Arithemetica 14:6c31a7ab45f0 265
Arithemetica 14:6c31a7ab45f0 266 /**
Arithemetica 14:6c31a7ab45f0 267 * ahrsProcessedData contains processed data, which is merely scaling operation
Arithemetica 14:6c31a7ab45f0 268 */
Arithemetica 14:6c31a7ab45f0 269 AhrsProcessedData ahrsProcessedData;
Arithemetica 14:6c31a7ab45f0 270
Arithemetica 14:6c31a7ab45f0 271 static const int DEFAULT_IMU_ACCEL_SCALER = 4000;
Arithemetica 14:6c31a7ab45f0 272 static const int DEFAULT_IMU_GYRO_SCALER = 100;
Arithemetica 14:6c31a7ab45f0 273 static const int DEFAULT_AHRS_ATTITUDE_SCALER = 90;
Arithemetica 14:6c31a7ab45f0 274 public:
Arithemetica 14:6c31a7ab45f0 275 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 276 {
Arithemetica 14:6c31a7ab45f0 277 for (int i = 0; i < 3; ++i) {
Arithemetica 14:6c31a7ab45f0 278 m_gyroScaler[i] = static_cast<float>(DEFAULT_IMU_GYRO_SCALER); // what an idiotic way of initialization LUL
Arithemetica 14:6c31a7ab45f0 279 m_accelScaler[i] = static_cast<float>(DEFAULT_IMU_ACCEL_SCALER); // Oh, I forgot that mbed os2 still stucks at c++98,
Arithemetica 14:6c31a7ab45f0 280 m_ahrsScaler[i] = static_cast<float>(DEFAULT_AHRS_ATTITUDE_SCALER); // how unfortunate LUL
Arithemetica 14:6c31a7ab45f0 281 }
Arithemetica 14:6c31a7ab45f0 282
Arithemetica 14:6c31a7ab45f0 283 if (t_exti_config != ImuExtiRcvNotUsed) {
Arithemetica 14:6c31a7ab45f0 284 m_drdyExti.fall(this, &ImuDriver<Spi, rst, drdy, ss>::m_dataReadyExtiCallback);
Arithemetica 14:6c31a7ab45f0 285 }
Arithemetica 14:6c31a7ab45f0 286
Arithemetica 14:6c31a7ab45f0 287 m_spi.format(16, 3);
Arithemetica 14:6c31a7ab45f0 288 m_spi.frequency(1406250);
Arithemetica 14:6c31a7ab45f0 289 }
Arithemetica 14:6c31a7ab45f0 290
Arithemetica 14:6c31a7ab45f0 291 /**
Arithemetica 14:6c31a7ab45f0 292 * @brief This function handles the receiving of burst message function, burst message
Arithemetica 14:6c31a7ab45f0 293 * contains accelerometer (g), gyroscope (deg/s), status and temperature data (Celcius)
Arithemetica 14:6c31a7ab45f0 294 *
Arithemetica 14:6c31a7ab45f0 295 * @param t_extended bool to indicate the message type is extended or not, default false
Arithemetica 14:6c31a7ab45f0 296 *
Arithemetica 14:6c31a7ab45f0 297 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 14:6c31a7ab45f0 298 */
Arithemetica 14:6c31a7ab45f0 299 ImuDriverStatus receiveBurstMsg(bool t_extended = false) {
Arithemetica 14:6c31a7ab45f0 300 return (m_extiConfig & ImuExtiRcvNormalMsg ? ImuDriverStatusInvalidCall : receiveBurstMsg(t_extended));
Arithemetica 14:6c31a7ab45f0 301 }
Arithemetica 0:8c01a98a2812 302
Arithemetica 0:8c01a98a2812 303 /**
Arithemetica 5:e71931fcae33 304 * @brief This function handles the receiving of AHRS burst message function, AHRS
Arithemetica 10:d8223969c541 305 * burst message contains the attitude in euler angle (deg).
Arithemetica 0:8c01a98a2812 306 *
Arithemetica 0:8c01a98a2812 307 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 0:8c01a98a2812 308 *
Arithemetica 0:8c01a98a2812 309 * @note This is only suitable for AHRS app, and additional procedure needs to be done
Arithemetica 0:8c01a98a2812 310 * in order to make it work
Arithemetica 0:8c01a98a2812 311 */
Arithemetica 0:8c01a98a2812 312 ImuDriverStatus receiveAhrsMsg()
Arithemetica 0:8c01a98a2812 313 {
Arithemetica 0:8c01a98a2812 314 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 315 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 316 const std::uint8_t max_data = 5U;
Arithemetica 0:8c01a98a2812 317 const std::uint16_t ahrs_reg = m_spiGenerateReadCmd(ReadAhrsBurstDataRegister);
Arithemetica 0:8c01a98a2812 318
Arithemetica 0:8c01a98a2812 319 m_ss.write(0);
Arithemetica 0:8c01a98a2812 320 m_imuSpiWrite(ahrs_reg);
Arithemetica 0:8c01a98a2812 321
Arithemetica 0:8c01a98a2812 322 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 323 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 324 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 325 case 0:
Arithemetica 0:8c01a98a2812 326 ahrsRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 327 break;
Arithemetica 0:8c01a98a2812 328 case 1:
Arithemetica 0:8c01a98a2812 329 case 2:
Arithemetica 0:8c01a98a2812 330 case 3:
Arithemetica 0:8c01a98a2812 331 ahrsRawData.attitude[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 332 break;
Arithemetica 0:8c01a98a2812 333 case 4:
Arithemetica 0:8c01a98a2812 334 ahrsRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 335 break;
Arithemetica 0:8c01a98a2812 336 default:
Arithemetica 0:8c01a98a2812 337 break;
Arithemetica 0:8c01a98a2812 338 }
Arithemetica 0:8c01a98a2812 339
Arithemetica 0:8c01a98a2812 340 ++data_rcved;
Arithemetica 0:8c01a98a2812 341 }
Arithemetica 0:8c01a98a2812 342
Arithemetica 0:8c01a98a2812 343 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 344 m_ss.write(1);
Arithemetica 0:8c01a98a2812 345
Arithemetica 0:8c01a98a2812 346 m_processAhrsRawData();
Arithemetica 0:8c01a98a2812 347 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 348 } else {
Arithemetica 0:8c01a98a2812 349 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 350 }
Arithemetica 0:8c01a98a2812 351 }
Arithemetica 14:6c31a7ab45f0 352
Arithemetica 14:6c31a7ab45f0 353 ImuDriverStatus configExti(const ImuExtiConfig t_exti_config) {
Arithemetica 14:6c31a7ab45f0 354 m_extiConfig = t_exti_config;
Arithemetica 14:6c31a7ab45f0 355 }
Arithemetica 1:8e31413068af 356 };
Arithemetica 1:8e31413068af 357
Arithemetica 1:8e31413068af 358 #endif // IMU_DRIVER_HPP_