handle master side communication of openIMU300ZI module

Dependencies:   mbed

Dependents:   VDU_2021

Committer:
Arithemetica
Date:
Sun Nov 17 09:42:48 2019 +0000
Revision:
9:9591afda2394
Parent:
8:1315e27798f4
Child:
10:d8223969c541
Update documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arithemetica 7:224ffdacb240 1 /**
Arithemetica 7:224ffdacb240 2 * @file imu_driver.hpp
Arithemetica 7:224ffdacb240 3 */
Arithemetica 7:224ffdacb240 4
Arithemetica 1:8e31413068af 5 #ifndef IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 6 #define IMU_DRIVER_HPP_
Arithemetica 1:8e31413068af 7
Arithemetica 0:8c01a98a2812 8 #include <mbed.h>
Arithemetica 0:8c01a98a2812 9 #include <cstdint>
Arithemetica 0:8c01a98a2812 10
Arithemetica 0:8c01a98a2812 11 typedef enum ImuDriverStatus {
Arithemetica 0:8c01a98a2812 12 ImuDriverStatusOK,
Arithemetica 0:8c01a98a2812 13 ImuDriverStatusDataNotReady
Arithemetica 0:8c01a98a2812 14 } ImuDriverStatus;
Arithemetica 0:8c01a98a2812 15
Arithemetica 0:8c01a98a2812 16 typedef enum ImuDriverRegister {
Arithemetica 0:8c01a98a2812 17 ReadAhrsBurstDataRegister = 0x3D,
Arithemetica 0:8c01a98a2812 18 ReadBurstDataRegister = 0x3E,
Arithemetica 0:8c01a98a2812 19 ReadExtBurstDataRegister = 0x3F
Arithemetica 0:8c01a98a2812 20 } ImuDriverRegister;
Arithemetica 0:8c01a98a2812 21
Arithemetica 0:8c01a98a2812 22 typedef struct AhrsRawData {
Arithemetica 0:8c01a98a2812 23 std::uint16_t status;
Arithemetica 0:8c01a98a2812 24 std::int16_t attitude[3];
Arithemetica 0:8c01a98a2812 25 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 26 } AhrsRawData;
Arithemetica 0:8c01a98a2812 27
Arithemetica 0:8c01a98a2812 28 typedef struct AhrsProcessedData {
Arithemetica 0:8c01a98a2812 29 float attitude[3];
Arithemetica 0:8c01a98a2812 30 } AhrsProcessedData;
Arithemetica 0:8c01a98a2812 31
Arithemetica 0:8c01a98a2812 32 typedef struct ImuRawData {
Arithemetica 0:8c01a98a2812 33 std::uint16_t status;
Arithemetica 0:8c01a98a2812 34 std::int16_t rate[3];
Arithemetica 0:8c01a98a2812 35 std::int16_t accel[3];
Arithemetica 0:8c01a98a2812 36 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 37 } ImuRawData;
Arithemetica 0:8c01a98a2812 38
Arithemetica 0:8c01a98a2812 39 typedef struct ImuProcessedData {
Arithemetica 0:8c01a98a2812 40 std::uint16_t status;
Arithemetica 0:8c01a98a2812 41
Arithemetica 0:8c01a98a2812 42 float rate[3];
Arithemetica 0:8c01a98a2812 43 float accel[3];
Arithemetica 0:8c01a98a2812 44
Arithemetica 0:8c01a98a2812 45 std::int16_t temperature;
Arithemetica 0:8c01a98a2812 46 } ImuProcessedData;
Arithemetica 0:8c01a98a2812 47
Arithemetica 0:8c01a98a2812 48 /**
Arithemetica 2:f3a7def7a7e1 49 * @brief ImuDriver is used to handle the SPI master side of openIMU300ZI module, the
Arithemetica 2:f3a7def7a7e1 50 * default setups are:
Arithemetica 0:8c01a98a2812 51 *
Arithemetica 2:f3a7def7a7e1 52 * - Data frame: 16 bits
Arithemetica 2:f3a7def7a7e1 53 * - CPOL: High (1)
Arithemetica 2:f3a7def7a7e1 54 * - CPHA: 2 Edge (1)
Arithemetica 2:f3a7def7a7e1 55 * - Frequency: 1.40625 MHz (Baudrate prescaler 32 for NUCLEO-F446RE under 180MHz)
Arithemetica 2:f3a7def7a7e1 56 * - Endian: MSB first (Mbed only support MSB AFAIK)
Arithemetica 5:e71931fcae33 57 *
Arithemetica 9:9591afda2394 58 * According to <A HREF="https://openimu.readthedocs.io/en/latest/software/SPImessaging.html">openIMU300ZI SPI framework</A>
Arithemetica 5:e71931fcae33 59 *
Arithemetica 3:8552e26cd162 60 * - Data transferred in 16-bit word-length and MSB-first
Arithemetica 5:e71931fcae33 61 * - fCLK ≤ 2.0 MHz
Arithemetica 3:8552e26cd162 62 * - CPOL = 1 (clock polarity) and CPHA = 1 (clock phase)
Arithemetica 0:8c01a98a2812 63 *
Arithemetica 2:f3a7def7a7e1 64 * @tparam spi SPI comm instance, ImuDriver currently support only SPI framework
Arithemetica 2:f3a7def7a7e1 65 * @tparam rst Reset pin name, this is used to reset the openIMU300ZI module
Arithemetica 2:f3a7def7a7e1 66 * @tparam drdy Data ready pin name, this is used as indication of data readiness
Arithemetica 2:f3a7def7a7e1 67 * @tparam ss Slave select pin name, this is used as slave select, pull low to initiate
Arithemetica 2:f3a7def7a7e1 68 * communication process.
Arithemetica 5:e71931fcae33 69 *
Arithemetica 7:224ffdacb240 70 * @todo Attach to exti in the future, and make user able to choose
Arithemetica 7:224ffdacb240 71 *
Arithemetica 0:8c01a98a2812 72 * Example of using ImuDriver:
Arithemetica 0:8c01a98a2812 73 *
Arithemetica 0:8c01a98a2812 74 * @code
Arithemetica 0:8c01a98a2812 75 * #include "imu_driver.hpp"
Arithemetica 0:8c01a98a2812 76 *
Arithemetica 0:8c01a98a2812 77 * SPI spi3(PB_5, PB_6, PB_3); // declare SPI instance globally
Arithemetica 0:8c01a98a2812 78 * Serial pc(USBTX, USBRX, 115200); // print debug message
Arithemetica 0:8c01a98a2812 79 *
Arithemetica 0:8c01a98a2812 80 * int main()
Arithemetica 0:8c01a98a2812 81 * {
Arithemetica 5:e71931fcae33 82 * // SPI instance, reset, data ready, slave select
Arithemetica 0:8c01a98a2812 83 * ImuDriver<spi3, PA_10, PA_8, PA_9> imu;
Arithemetica 0:8c01a98a2812 84 *
Arithemetica 0:8c01a98a2812 85 * while(true)
Arithemetica 0:8c01a98a2812 86 * {
Arithemetica 0:8c01a98a2812 87 * if (imu.receiveBurstMsg() == ImuDriverStatusOK)
Arithemetica 0:8c01a98a2812 88 * {
Arithemetica 0:8c01a98a2812 89 * pc.printf("%.3f, %.3f, %.3f\n\r", imu.imuProcessedData.rate[0], imu.imuProcessedData.rate[1], imu.imuProcessedData.rate[2]);
Arithemetica 0:8c01a98a2812 90 * }
Arithemetica 0:8c01a98a2812 91 * }
Arithemetica 0:8c01a98a2812 92 * }
Arithemetica 5:e71931fcae33 93 *
Arithemetica 0:8c01a98a2812 94 * @endcode
Arithemetica 0:8c01a98a2812 95 */
Arithemetica 2:f3a7def7a7e1 96 template <SPI& Spi, PinName rst, PinName drdy, PinName ss>
Arithemetica 0:8c01a98a2812 97 class ImuDriver
Arithemetica 0:8c01a98a2812 98 {
Arithemetica 0:8c01a98a2812 99 private:
Arithemetica 2:f3a7def7a7e1 100 typedef ImuDriver<Spi, rst, drdy, ss> ImuDriverType;
Arithemetica 2:f3a7def7a7e1 101 typedef ImuDriverStatus (ImuDriver<Spi, rst, drdy, ss>::*DrdyCallback)();
Arithemetica 2:f3a7def7a7e1 102
Arithemetica 2:f3a7def7a7e1 103
Arithemetica 0:8c01a98a2812 104 SPI& m_spi;
Arithemetica 0:8c01a98a2812 105 DigitalOut m_rst, m_ss;
Arithemetica 0:8c01a98a2812 106 DigitalIn m_drdy;
Arithemetica 2:f3a7def7a7e1 107 InterruptIn m_drdyExti;
Arithemetica 5:e71931fcae33 108
Arithemetica 0:8c01a98a2812 109 float m_gyroScaler[3];
Arithemetica 0:8c01a98a2812 110 float m_accelScaler[3];
Arithemetica 0:8c01a98a2812 111 float m_ahrsScaler[3];
Arithemetica 0:8c01a98a2812 112 private:
Arithemetica 0:8c01a98a2812 113 std::uint16_t m_imuSpiWrite(const std::uint16_t val) const
Arithemetica 0:8c01a98a2812 114 {
Arithemetica 0:8c01a98a2812 115 // RAII
Arithemetica 0:8c01a98a2812 116 class SpiLock
Arithemetica 0:8c01a98a2812 117 {
Arithemetica 0:8c01a98a2812 118 private:
Arithemetica 0:8c01a98a2812 119 SPI& m_spi;
Arithemetica 0:8c01a98a2812 120 public:
Arithemetica 0:8c01a98a2812 121 SpiLock(SPI& t_spi) : m_spi(t_spi)
Arithemetica 0:8c01a98a2812 122 {
Arithemetica 0:8c01a98a2812 123 m_spi.lock();
Arithemetica 0:8c01a98a2812 124 }
Arithemetica 0:8c01a98a2812 125
Arithemetica 0:8c01a98a2812 126 ~SpiLock()
Arithemetica 0:8c01a98a2812 127 {
Arithemetica 0:8c01a98a2812 128 m_spi.unlock();
Arithemetica 0:8c01a98a2812 129 }
Arithemetica 0:8c01a98a2812 130 };
Arithemetica 0:8c01a98a2812 131
Arithemetica 0:8c01a98a2812 132 SpiLock lock(m_spi);
Arithemetica 0:8c01a98a2812 133 return m_spi.write(val);
Arithemetica 0:8c01a98a2812 134 }
Arithemetica 0:8c01a98a2812 135
Arithemetica 0:8c01a98a2812 136 inline std::uint16_t m_spiGenerateReadCmd(const std::uint8_t t_val) const
Arithemetica 0:8c01a98a2812 137 {
Arithemetica 0:8c01a98a2812 138 return t_val << 8U;
Arithemetica 0:8c01a98a2812 139 }
Arithemetica 0:8c01a98a2812 140
Arithemetica 0:8c01a98a2812 141 inline bool m_spiIsDataReady()
Arithemetica 0:8c01a98a2812 142 {
Arithemetica 0:8c01a98a2812 143 return m_drdy.read() == 0;
Arithemetica 0:8c01a98a2812 144 }
Arithemetica 0:8c01a98a2812 145
Arithemetica 0:8c01a98a2812 146 inline void m_processImuRawData()
Arithemetica 0:8c01a98a2812 147 {
Arithemetica 0:8c01a98a2812 148 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 149 imuProcessedData.accel[i] = imuRawData.accel[i] / m_accelScaler[i];
Arithemetica 0:8c01a98a2812 150 imuProcessedData.rate[i] = imuRawData.rate[i] / m_gyroScaler[i];
Arithemetica 0:8c01a98a2812 151 }
Arithemetica 0:8c01a98a2812 152 }
Arithemetica 0:8c01a98a2812 153
Arithemetica 0:8c01a98a2812 154 inline void m_processAhrsRawData()
Arithemetica 0:8c01a98a2812 155 {
Arithemetica 0:8c01a98a2812 156 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 157 ahrsProcessedData.attitude[i] = ahrsRawData.attitude[i] / m_ahrsScaler[i];
Arithemetica 0:8c01a98a2812 158 }
Arithemetica 0:8c01a98a2812 159 }
Arithemetica 5:e71931fcae33 160
Arithemetica 2:f3a7def7a7e1 161 void m_receiveAhrsCallback()
Arithemetica 2:f3a7def7a7e1 162 {
Arithemetica 2:f3a7def7a7e1 163 receiveAhrsMsg();
Arithemetica 2:f3a7def7a7e1 164 }
Arithemetica 0:8c01a98a2812 165 public:
Arithemetica 0:8c01a98a2812 166 ImuRawData imuRawData;
Arithemetica 0:8c01a98a2812 167 ImuProcessedData imuProcessedData;
Arithemetica 0:8c01a98a2812 168
Arithemetica 0:8c01a98a2812 169 AhrsRawData ahrsRawData;
Arithemetica 0:8c01a98a2812 170 AhrsProcessedData ahrsProcessedData;
Arithemetica 0:8c01a98a2812 171
Arithemetica 0:8c01a98a2812 172 static const int DEFAULT_IMU_ACCEL_SCALER = 4000;
Arithemetica 0:8c01a98a2812 173 static const int DEFAULT_IMU_GYRO_SCALER = 200;
Arithemetica 0:8c01a98a2812 174 static const int DEFAULT_AHRS_ATTITUDE_SCALER = 90;
Arithemetica 0:8c01a98a2812 175 public:
Arithemetica 2:f3a7def7a7e1 176 ImuDriver() : m_spi(Spi), m_rst(rst), m_ss(ss), m_drdy(drdy), m_drdyExti(drdy)
Arithemetica 0:8c01a98a2812 177 {
Arithemetica 0:8c01a98a2812 178 for (int i = 0; i < 3; ++i) {
Arithemetica 0:8c01a98a2812 179 m_gyroScaler[i] = static_cast<float>(DEFAULT_IMU_GYRO_SCALER); // what an idiotic way of initialization LUL
Arithemetica 2:f3a7def7a7e1 180 m_accelScaler[i] = static_cast<float>(DEFAULT_IMU_ACCEL_SCALER); // Oh, I forgot that mbed os2 still stucks at c++98,
Arithemetica 0:8c01a98a2812 181 m_ahrsScaler[i] = static_cast<float>(DEFAULT_AHRS_ATTITUDE_SCALER); // how unfortunate LUL
Arithemetica 0:8c01a98a2812 182 }
Arithemetica 0:8c01a98a2812 183
Arithemetica 2:f3a7def7a7e1 184 // m_drdyExti.rise(this, &ImuDriver<Spi, rst, drdy, ss>::m_receiveAhrsCallback);
Arithemetica 0:8c01a98a2812 185 m_spi.format(16, 3);
Arithemetica 0:8c01a98a2812 186 m_spi.frequency(1406250);
Arithemetica 0:8c01a98a2812 187 }
Arithemetica 0:8c01a98a2812 188
Arithemetica 0:8c01a98a2812 189 /**
Arithemetica 5:e71931fcae33 190 * @brief This function handles the receiving of burst message function, burst message
Arithemetica 5:e71931fcae33 191 * contains accelerometer, gyroscope, status and temperature data
Arithemetica 0:8c01a98a2812 192 *
Arithemetica 0:8c01a98a2812 193 * @param t_extended bool to indicate the message type is extended or not, default false
Arithemetica 0:8c01a98a2812 194 *
Arithemetica 0:8c01a98a2812 195 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 0:8c01a98a2812 196 */
Arithemetica 0:8c01a98a2812 197 ImuDriverStatus receiveBurstMsg(bool t_extended = false)
Arithemetica 0:8c01a98a2812 198 {
Arithemetica 0:8c01a98a2812 199 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 200 std::uint16_t max_data = 0U;
Arithemetica 0:8c01a98a2812 201 std::uint16_t burst_reg = 0U;
Arithemetica 0:8c01a98a2812 202
Arithemetica 0:8c01a98a2812 203 if (!t_extended) {
Arithemetica 0:8c01a98a2812 204 max_data = 8U;
Arithemetica 0:8c01a98a2812 205 burst_reg = m_spiGenerateReadCmd(ReadBurstDataRegister);
Arithemetica 0:8c01a98a2812 206 } else {
Arithemetica 0:8c01a98a2812 207 max_data = 11U;
Arithemetica 0:8c01a98a2812 208 burst_reg = m_spiGenerateReadCmd(ReadExtBurstDataRegister);
Arithemetica 0:8c01a98a2812 209 }
Arithemetica 0:8c01a98a2812 210
Arithemetica 0:8c01a98a2812 211 m_ss.write(0); // start transfer
Arithemetica 0:8c01a98a2812 212 m_imuSpiWrite(burst_reg);
Arithemetica 0:8c01a98a2812 213
Arithemetica 0:8c01a98a2812 214 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 215
Arithemetica 0:8c01a98a2812 216 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 217 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 218 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 219 case 0:
Arithemetica 0:8c01a98a2812 220 imuRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 221 break;
Arithemetica 0:8c01a98a2812 222 case 1:
Arithemetica 0:8c01a98a2812 223 case 2:
Arithemetica 0:8c01a98a2812 224 case 3:
Arithemetica 0:8c01a98a2812 225 imuRawData.rate[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 226 break;
Arithemetica 0:8c01a98a2812 227 case 4:
Arithemetica 0:8c01a98a2812 228 case 5:
Arithemetica 0:8c01a98a2812 229 case 6:
Arithemetica 0:8c01a98a2812 230 imuRawData.accel[data_rcved - 4] = spi_data;
Arithemetica 0:8c01a98a2812 231 break;
Arithemetica 0:8c01a98a2812 232 case 7:
Arithemetica 0:8c01a98a2812 233 imuRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 234 break;
Arithemetica 0:8c01a98a2812 235 default:
Arithemetica 0:8c01a98a2812 236 break;
Arithemetica 0:8c01a98a2812 237 }
Arithemetica 0:8c01a98a2812 238
Arithemetica 0:8c01a98a2812 239 ++data_rcved;
Arithemetica 0:8c01a98a2812 240 }
Arithemetica 0:8c01a98a2812 241
Arithemetica 0:8c01a98a2812 242 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 243 m_ss.write(1);
Arithemetica 0:8c01a98a2812 244
Arithemetica 0:8c01a98a2812 245 m_processImuRawData();
Arithemetica 0:8c01a98a2812 246 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 247 } else {
Arithemetica 0:8c01a98a2812 248 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 249 }
Arithemetica 0:8c01a98a2812 250 }
Arithemetica 0:8c01a98a2812 251
Arithemetica 0:8c01a98a2812 252 /**
Arithemetica 5:e71931fcae33 253 * @brief This function handles the receiving of AHRS burst message function, AHRS
Arithemetica 5:e71931fcae33 254 * burst message contains the attitude in euler angle.
Arithemetica 0:8c01a98a2812 255 *
Arithemetica 0:8c01a98a2812 256 * @return ImuDriverStatus to indicate the result of the receive operation
Arithemetica 0:8c01a98a2812 257 *
Arithemetica 0:8c01a98a2812 258 * @note This is only suitable for AHRS app, and additional procedure needs to be done
Arithemetica 0:8c01a98a2812 259 * in order to make it work
Arithemetica 0:8c01a98a2812 260 */
Arithemetica 0:8c01a98a2812 261 ImuDriverStatus receiveAhrsMsg()
Arithemetica 0:8c01a98a2812 262 {
Arithemetica 0:8c01a98a2812 263 if (m_spiIsDataReady()) {
Arithemetica 0:8c01a98a2812 264 static std::uint8_t data_rcved = 0U;
Arithemetica 0:8c01a98a2812 265 const std::uint8_t max_data = 5U;
Arithemetica 0:8c01a98a2812 266 const std::uint16_t ahrs_reg = m_spiGenerateReadCmd(ReadAhrsBurstDataRegister);
Arithemetica 0:8c01a98a2812 267
Arithemetica 0:8c01a98a2812 268 m_ss.write(0);
Arithemetica 0:8c01a98a2812 269 m_imuSpiWrite(ahrs_reg);
Arithemetica 0:8c01a98a2812 270
Arithemetica 0:8c01a98a2812 271 while(data_rcved < max_data) {
Arithemetica 0:8c01a98a2812 272 const std::uint16_t spi_data = m_imuSpiWrite(0x0000);
Arithemetica 0:8c01a98a2812 273 switch(data_rcved) {
Arithemetica 0:8c01a98a2812 274 case 0:
Arithemetica 0:8c01a98a2812 275 ahrsRawData.status = spi_data;
Arithemetica 0:8c01a98a2812 276 break;
Arithemetica 0:8c01a98a2812 277 case 1:
Arithemetica 0:8c01a98a2812 278 case 2:
Arithemetica 0:8c01a98a2812 279 case 3:
Arithemetica 0:8c01a98a2812 280 ahrsRawData.attitude[data_rcved - 1] = spi_data;
Arithemetica 0:8c01a98a2812 281 break;
Arithemetica 0:8c01a98a2812 282 case 4:
Arithemetica 0:8c01a98a2812 283 ahrsRawData.temperature = spi_data;
Arithemetica 0:8c01a98a2812 284 break;
Arithemetica 0:8c01a98a2812 285 default:
Arithemetica 0:8c01a98a2812 286 break;
Arithemetica 0:8c01a98a2812 287 }
Arithemetica 0:8c01a98a2812 288
Arithemetica 0:8c01a98a2812 289 ++data_rcved;
Arithemetica 0:8c01a98a2812 290 }
Arithemetica 0:8c01a98a2812 291
Arithemetica 0:8c01a98a2812 292 data_rcved = 0U;
Arithemetica 0:8c01a98a2812 293 m_ss.write(1);
Arithemetica 0:8c01a98a2812 294
Arithemetica 0:8c01a98a2812 295 m_processAhrsRawData();
Arithemetica 0:8c01a98a2812 296 return ImuDriverStatusOK;
Arithemetica 0:8c01a98a2812 297 } else {
Arithemetica 0:8c01a98a2812 298 return ImuDriverStatusDataNotReady;
Arithemetica 0:8c01a98a2812 299 }
Arithemetica 0:8c01a98a2812 300 }
Arithemetica 1:8e31413068af 301 };
Arithemetica 1:8e31413068af 302
Arithemetica 1:8e31413068af 303 #endif // IMU_DRIVER_HPP_