mbed library for MPU9250 (SPI interface)
Dependents: InvertedPendulum2017
Lib_MPU9250.h@1:5334e111af53, 2018-04-20 (annotated)
- Committer:
- bluefish
- Date:
- Fri Apr 20 18:13:45 2018 +0000
- Revision:
- 1:5334e111af53
- Child:
- 2:c142d5a352c5
MPU9250??????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluefish | 1:5334e111af53 | 1 | #ifndef __LIBRARY_MPU9250_SPI_H__ |
bluefish | 1:5334e111af53 | 2 | #define __LIBRARY_MPU9250_SPI_H__ |
bluefish | 1:5334e111af53 | 3 | |
bluefish | 1:5334e111af53 | 4 | #include "mbed.h" |
bluefish | 1:5334e111af53 | 5 | |
bluefish | 1:5334e111af53 | 6 | #define MPU9250_FREQ_DEFAULT (4000000) // default clock speed |
bluefish | 1:5334e111af53 | 7 | #define MPU9250_ADDR (0x68) |
bluefish | 1:5334e111af53 | 8 | #define AK8963_ADDR (0x0C) // magnetometer address |
bluefish | 1:5334e111af53 | 9 | |
bluefish | 1:5334e111af53 | 10 | #define I2C_WRITE_FLAG (0x00) |
bluefish | 1:5334e111af53 | 11 | #define I2C_READ_FLAG (0x01) |
bluefish | 1:5334e111af53 | 12 | |
bluefish | 1:5334e111af53 | 13 | #define MPU9250A_2G ((float)0.000061035156f) // 0.000061035156 g/LSB |
bluefish | 1:5334e111af53 | 14 | #define MPU9250A_4G ((float)0.000122070312f) // 0.000122070312 g/LSB |
bluefish | 1:5334e111af53 | 15 | #define MPU9250A_8G ((float)0.000244140625f) // 0.000244140625 g/LSB |
bluefish | 1:5334e111af53 | 16 | #define MPU9250A_16G ((float)0.000488281250f) // 0.000488281250 g/LSB |
bluefish | 1:5334e111af53 | 17 | |
bluefish | 1:5334e111af53 | 18 | #define MPU9250G_250DPS ((float)0.007633587786f) // 0.007633587786 dps/LSB |
bluefish | 1:5334e111af53 | 19 | #define MPU9250G_500DPS ((float)0.015267175572f) // 0.015267175572 dps/LSB |
bluefish | 1:5334e111af53 | 20 | #define MPU9250G_1000DPS ((float)0.030487804878f) // 0.030487804878 dps/LSB |
bluefish | 1:5334e111af53 | 21 | #define MPU9250G_2000DPS ((float)0.060975609756f) // 0.060975609756 dps/LSB |
bluefish | 1:5334e111af53 | 22 | |
bluefish | 1:5334e111af53 | 23 | /* |
bluefish | 1:5334e111af53 | 24 | #define MPU9250G_250DPS_RAD ((float)0.00013323124f) // 0.00013323124 rad/LSB |
bluefish | 1:5334e111af53 | 25 | #define MPU9250G_500DPS_RAD ((float)0.00026646248f) // 0.00026646248 rad/LSB |
bluefish | 1:5334e111af53 | 26 | #define MPU9250G_1000DPS_RAD ((float)0.00053211257f) // 0.00053211257 rad/LSB |
bluefish | 1:5334e111af53 | 27 | #define MPU9250G_2000DPS_RAD ((float)0.00106422515f) // 0.00106422515 rad/LSB |
bluefish | 1:5334e111af53 | 28 | */ |
bluefish | 1:5334e111af53 | 29 | |
bluefish | 1:5334e111af53 | 30 | #define MPU9250M_4800uT ((float)0.6f) // 0.6 uT/LSB |
bluefish | 1:5334e111af53 | 31 | #define MPU9250T_85degC ((float)0.002995177763f) // 0.002995177763 degC/LSB |
bluefish | 1:5334e111af53 | 32 | |
bluefish | 1:5334e111af53 | 33 | #define MPU9250G_DEG2RAD ((float)0.01745329251f) // degree to radian |
bluefish | 1:5334e111af53 | 34 | |
bluefish | 1:5334e111af53 | 35 | // enum |
bluefish | 1:5334e111af53 | 36 | typedef enum _MPU9250REG{ |
bluefish | 1:5334e111af53 | 37 | // gyro and accel |
bluefish | 1:5334e111af53 | 38 | SELF_TEST_X_GYRO = 0x00, |
bluefish | 1:5334e111af53 | 39 | SELF_TEST_Y_GYRO = 0x01, |
bluefish | 1:5334e111af53 | 40 | SELF_TEST_Z_GYRO = 0x02, |
bluefish | 1:5334e111af53 | 41 | |
bluefish | 1:5334e111af53 | 42 | SELF_TEST_X_ACCEL = 0x0D, |
bluefish | 1:5334e111af53 | 43 | SELF_TEST_Y_ACCEL = 0x0E, |
bluefish | 1:5334e111af53 | 44 | SELF_TEST_Z_ACCEL = 0x0F, |
bluefish | 1:5334e111af53 | 45 | |
bluefish | 1:5334e111af53 | 46 | XG_OFFSET_H = 0x13, |
bluefish | 1:5334e111af53 | 47 | XG_OFFSET_L = 0x14, |
bluefish | 1:5334e111af53 | 48 | YG_OFFSET_H = 0x15, |
bluefish | 1:5334e111af53 | 49 | YG_OFFSET_L = 0x16, |
bluefish | 1:5334e111af53 | 50 | ZG_OFFSET_H = 0x17, |
bluefish | 1:5334e111af53 | 51 | ZG_OFFSET_L = 0x18, |
bluefish | 1:5334e111af53 | 52 | |
bluefish | 1:5334e111af53 | 53 | SMPLRT_DIV = 0x19, |
bluefish | 1:5334e111af53 | 54 | CONFIG = 0x1A, |
bluefish | 1:5334e111af53 | 55 | GYRO_CONFIG = 0x1B, |
bluefish | 1:5334e111af53 | 56 | ACCEL_CONFIG = 0x1C, |
bluefish | 1:5334e111af53 | 57 | ACCEL_CONFIG2 = 0x1D, |
bluefish | 1:5334e111af53 | 58 | LP_ACCEL_ODR = 0x1E, |
bluefish | 1:5334e111af53 | 59 | WOM_THR = 0x1F, |
bluefish | 1:5334e111af53 | 60 | |
bluefish | 1:5334e111af53 | 61 | FIFO_EN = 0x23, |
bluefish | 1:5334e111af53 | 62 | |
bluefish | 1:5334e111af53 | 63 | I2C_MST_CTRL = 0x24, |
bluefish | 1:5334e111af53 | 64 | I2C_SLV0_ADDR = 0x25, |
bluefish | 1:5334e111af53 | 65 | I2C_SLV0_REG = 0x26, |
bluefish | 1:5334e111af53 | 66 | I2C_SLV0_CTRL = 0x27, |
bluefish | 1:5334e111af53 | 67 | I2C_SLV1_ADDR = 0x28, |
bluefish | 1:5334e111af53 | 68 | I2C_SLV1_REG = 0x29, |
bluefish | 1:5334e111af53 | 69 | I2C_SLV1_CTRL = 0x2A, |
bluefish | 1:5334e111af53 | 70 | I2C_SLV2_ADDR = 0x2B, |
bluefish | 1:5334e111af53 | 71 | I2C_SLV2_REG = 0x2C, |
bluefish | 1:5334e111af53 | 72 | I2C_SLV2_CTRL = 0x2D, |
bluefish | 1:5334e111af53 | 73 | I2C_SLV3_ADDR = 0x2E, |
bluefish | 1:5334e111af53 | 74 | I2C_SLV3_REG = 0x2F, |
bluefish | 1:5334e111af53 | 75 | I2C_SLV3_CTRL = 0x30, |
bluefish | 1:5334e111af53 | 76 | I2C_SLV4_ADDR = 0x31, |
bluefish | 1:5334e111af53 | 77 | I2C_SLV4_REG = 0x32, |
bluefish | 1:5334e111af53 | 78 | I2C_SLV4_DO = 0x33, |
bluefish | 1:5334e111af53 | 79 | I2C_SLV4_CTRL = 0x34, |
bluefish | 1:5334e111af53 | 80 | I2C_SLV4_DI = 0x35, |
bluefish | 1:5334e111af53 | 81 | I2C_MST_STATUS = 0x36, |
bluefish | 1:5334e111af53 | 82 | |
bluefish | 1:5334e111af53 | 83 | INT_PIN_CFG = 0x37, |
bluefish | 1:5334e111af53 | 84 | INT_ENABLE = 0x38, |
bluefish | 1:5334e111af53 | 85 | INT_STATUS = 0x3A, |
bluefish | 1:5334e111af53 | 86 | |
bluefish | 1:5334e111af53 | 87 | ACCEL_XOUT_H = 0x3B, |
bluefish | 1:5334e111af53 | 88 | ACCEL_XOUT_L = 0x3C, |
bluefish | 1:5334e111af53 | 89 | ACCEL_YOUT_H = 0x3D, |
bluefish | 1:5334e111af53 | 90 | ACCEL_YOUT_L = 0x3E, |
bluefish | 1:5334e111af53 | 91 | ACCEL_ZOUT_H = 0x3F, |
bluefish | 1:5334e111af53 | 92 | ACCEL_ZOUT_L = 0x40, |
bluefish | 1:5334e111af53 | 93 | |
bluefish | 1:5334e111af53 | 94 | TEMP_OUT_H = 0x41, |
bluefish | 1:5334e111af53 | 95 | TEMP_OUT_L = 0x42, |
bluefish | 1:5334e111af53 | 96 | |
bluefish | 1:5334e111af53 | 97 | GYRO_XOUT_H = 0x43, |
bluefish | 1:5334e111af53 | 98 | GYRO_XOUT_L = 0x44, |
bluefish | 1:5334e111af53 | 99 | GYRO_YOUT_H = 0x45, |
bluefish | 1:5334e111af53 | 100 | GYRO_YOUT_L = 0x46, |
bluefish | 1:5334e111af53 | 101 | GYRO_ZOUT_H = 0x47, |
bluefish | 1:5334e111af53 | 102 | GYRO_ZOUT_L = 0x48, |
bluefish | 1:5334e111af53 | 103 | |
bluefish | 1:5334e111af53 | 104 | EXT_SENS_DATA_00 = 0x49, |
bluefish | 1:5334e111af53 | 105 | EXT_SENS_DATA_01 = 0x4A, |
bluefish | 1:5334e111af53 | 106 | EXT_SENS_DATA_02 = 0x4B, |
bluefish | 1:5334e111af53 | 107 | EXT_SENS_DATA_03 = 0x4C, |
bluefish | 1:5334e111af53 | 108 | EXT_SENS_DATA_04 = 0x4D, |
bluefish | 1:5334e111af53 | 109 | EXT_SENS_DATA_05 = 0x4E, |
bluefish | 1:5334e111af53 | 110 | EXT_SENS_DATA_06 = 0x4F, |
bluefish | 1:5334e111af53 | 111 | EXT_SENS_DATA_07 = 0x50, |
bluefish | 1:5334e111af53 | 112 | EXT_SENS_DATA_08 = 0x51, |
bluefish | 1:5334e111af53 | 113 | EXT_SENS_DATA_09 = 0x52, |
bluefish | 1:5334e111af53 | 114 | EXT_SENS_DATA_10 = 0x53, |
bluefish | 1:5334e111af53 | 115 | EXT_SENS_DATA_11 = 0x54, |
bluefish | 1:5334e111af53 | 116 | EXT_SENS_DATA_12 = 0x55, |
bluefish | 1:5334e111af53 | 117 | EXT_SENS_DATA_13 = 0x56, |
bluefish | 1:5334e111af53 | 118 | EXT_SENS_DATA_14 = 0x57, |
bluefish | 1:5334e111af53 | 119 | EXT_SENS_DATA_15 = 0x58, |
bluefish | 1:5334e111af53 | 120 | EXT_SENS_DATA_16 = 0x59, |
bluefish | 1:5334e111af53 | 121 | EXT_SENS_DATA_17 = 0x5A, |
bluefish | 1:5334e111af53 | 122 | EXT_SENS_DATA_18 = 0x5B, |
bluefish | 1:5334e111af53 | 123 | EXT_SENS_DATA_19 = 0x5C, |
bluefish | 1:5334e111af53 | 124 | EXT_SENS_DATA_20 = 0x5D, |
bluefish | 1:5334e111af53 | 125 | EXT_SENS_DATA_21 = 0x5E, |
bluefish | 1:5334e111af53 | 126 | EXT_SENS_DATA_22 = 0x5F, |
bluefish | 1:5334e111af53 | 127 | EXT_SENS_DATA_23 = 0x60, |
bluefish | 1:5334e111af53 | 128 | |
bluefish | 1:5334e111af53 | 129 | I2C_SLV0_DO = 0x63, |
bluefish | 1:5334e111af53 | 130 | I2C_SLV1_DO = 0x64, |
bluefish | 1:5334e111af53 | 131 | I2C_SLV2_DO = 0x65, |
bluefish | 1:5334e111af53 | 132 | I2C_SLV3_DO = 0x66, |
bluefish | 1:5334e111af53 | 133 | |
bluefish | 1:5334e111af53 | 134 | I2C_MST_DELAY_CTRL = 0x67, |
bluefish | 1:5334e111af53 | 135 | SIGNAL_PATH_RESET = 0x68, |
bluefish | 1:5334e111af53 | 136 | MOT_DETECT_CTRL = 0x69, |
bluefish | 1:5334e111af53 | 137 | USER_CTRL = 0x6A, |
bluefish | 1:5334e111af53 | 138 | PWR_MGMT_1 = 0x6B, |
bluefish | 1:5334e111af53 | 139 | PWR_MGMT_2 = 0x6C, |
bluefish | 1:5334e111af53 | 140 | |
bluefish | 1:5334e111af53 | 141 | FIFO_COUNTH = 0x72, |
bluefish | 1:5334e111af53 | 142 | FIFO_COUNTL = 0x73, |
bluefish | 1:5334e111af53 | 143 | FIFO_R_W = 0x74, |
bluefish | 1:5334e111af53 | 144 | WHO_AM_I = 0x75, |
bluefish | 1:5334e111af53 | 145 | |
bluefish | 1:5334e111af53 | 146 | XA_OFFSET_H = 0x77, |
bluefish | 1:5334e111af53 | 147 | XA_OFFSET_L = 0x78, |
bluefish | 1:5334e111af53 | 148 | YA_OFFSET_H = 0x7A, |
bluefish | 1:5334e111af53 | 149 | YA_OFFSET_L = 0x7B, |
bluefish | 1:5334e111af53 | 150 | ZA_OFFSET_H = 0x7D, |
bluefish | 1:5334e111af53 | 151 | ZA_OFFSET_L = 0x7E, |
bluefish | 1:5334e111af53 | 152 | } MPU9250REG; |
bluefish | 1:5334e111af53 | 153 | |
bluefish | 1:5334e111af53 | 154 | typedef enum _AK8963REG{ |
bluefish | 1:5334e111af53 | 155 | // magnetometer |
bluefish | 1:5334e111af53 | 156 | WIA = 0x00, |
bluefish | 1:5334e111af53 | 157 | INFO = 0x01, |
bluefish | 1:5334e111af53 | 158 | ST1 = 0x02, |
bluefish | 1:5334e111af53 | 159 | |
bluefish | 1:5334e111af53 | 160 | HXL = 0x03, |
bluefish | 1:5334e111af53 | 161 | HXH = 0x04, |
bluefish | 1:5334e111af53 | 162 | HYL = 0x05, |
bluefish | 1:5334e111af53 | 163 | HYH = 0x06, |
bluefish | 1:5334e111af53 | 164 | HZL = 0x07, |
bluefish | 1:5334e111af53 | 165 | HZH = 0x08, |
bluefish | 1:5334e111af53 | 166 | |
bluefish | 1:5334e111af53 | 167 | ST2 = 0x09, |
bluefish | 1:5334e111af53 | 168 | CNTL1 = 0x0A, |
bluefish | 1:5334e111af53 | 169 | CNTL2 = 0x0B, |
bluefish | 1:5334e111af53 | 170 | ASTC = 0x0C, |
bluefish | 1:5334e111af53 | 171 | TS1 = 0x0D, |
bluefish | 1:5334e111af53 | 172 | TS2 = 0x0E, |
bluefish | 1:5334e111af53 | 173 | I2CDIS = 0x0F, |
bluefish | 1:5334e111af53 | 174 | |
bluefish | 1:5334e111af53 | 175 | ASAX = 0x10, |
bluefish | 1:5334e111af53 | 176 | ASAY = 0x11, |
bluefish | 1:5334e111af53 | 177 | ASAZ = 0x12, |
bluefish | 1:5334e111af53 | 178 | } AK8963REG; |
bluefish | 1:5334e111af53 | 179 | |
bluefish | 1:5334e111af53 | 180 | typedef enum _MPU9250BIT{ |
bluefish | 1:5334e111af53 | 181 | BIT_SLEEP = 0x40, |
bluefish | 1:5334e111af53 | 182 | BIT_H_RESET = 0x80, |
bluefish | 1:5334e111af53 | 183 | BITS_CLKSEL = 0x07, |
bluefish | 1:5334e111af53 | 184 | MPU_CLK_SEL_PLLGYROX = 0x01, |
bluefish | 1:5334e111af53 | 185 | MPU_CLK_SEL_PLLGYROZ = 0x03, |
bluefish | 1:5334e111af53 | 186 | MPU_EXT_SYNC_GYROX = 0x02, |
bluefish | 1:5334e111af53 | 187 | BITS_FS_250DPS = 0x00, |
bluefish | 1:5334e111af53 | 188 | BITS_FS_500DPS = 0x08, |
bluefish | 1:5334e111af53 | 189 | BITS_FS_1000DPS = 0x10, |
bluefish | 1:5334e111af53 | 190 | BITS_FS_2000DPS = 0x18, |
bluefish | 1:5334e111af53 | 191 | BITS_FS_2G = 0x00, |
bluefish | 1:5334e111af53 | 192 | BITS_FS_4G = 0x08, |
bluefish | 1:5334e111af53 | 193 | BITS_FS_8G = 0x10, |
bluefish | 1:5334e111af53 | 194 | BITS_FS_16G = 0x18, |
bluefish | 1:5334e111af53 | 195 | BITS_FS_MASK = 0x18, |
bluefish | 1:5334e111af53 | 196 | BITS_DLPF_CFG_256HZ_NOLPF2 = 0x00, |
bluefish | 1:5334e111af53 | 197 | BITS_DLPF_CFG_188HZ = 0x01, |
bluefish | 1:5334e111af53 | 198 | BITS_DLPF_CFG_98HZ = 0x02, |
bluefish | 1:5334e111af53 | 199 | BITS_DLPF_CFG_42HZ = 0x03, |
bluefish | 1:5334e111af53 | 200 | BITS_DLPF_CFG_20HZ = 0x04, |
bluefish | 1:5334e111af53 | 201 | BITS_DLPF_CFG_10HZ = 0x05, |
bluefish | 1:5334e111af53 | 202 | BITS_DLPF_CFG_5HZ = 0x06, |
bluefish | 1:5334e111af53 | 203 | BITS_DLPF_CFG_2100HZ_NOLPF = 0x07, |
bluefish | 1:5334e111af53 | 204 | BITS_DLPF_CFG_MASK = 0x07, |
bluefish | 1:5334e111af53 | 205 | BIT_INT_ANYRD_2CLEAR = 0x10, |
bluefish | 1:5334e111af53 | 206 | BIT_RAW_RDY_EN = 0x01, |
bluefish | 1:5334e111af53 | 207 | BIT_I2C_IF_DIS = 0x10 |
bluefish | 1:5334e111af53 | 208 | } MPU9250BIT; |
bluefish | 1:5334e111af53 | 209 | |
bluefish | 1:5334e111af53 | 210 | class MPU9250{ |
bluefish | 1:5334e111af53 | 211 | public: |
bluefish | 1:5334e111af53 | 212 | MPU9250( PinName cs, PinName mosi, PinName miso, PinName sck ); // constructor( SPI ) |
bluefish | 1:5334e111af53 | 213 | MPU9250( PinName sda, PinName scl, uint8_t addr = MPU9250_ADDR ); // constructor( I2C ) |
bluefish | 1:5334e111af53 | 214 | ~MPU9250(); // destructor |
bluefish | 1:5334e111af53 | 215 | |
bluefish | 1:5334e111af53 | 216 | void begin( void ); |
bluefish | 1:5334e111af53 | 217 | void reset( void ); |
bluefish | 1:5334e111af53 | 218 | |
bluefish | 1:5334e111af53 | 219 | void setAccelRange( MPU9250BIT range ); |
bluefish | 1:5334e111af53 | 220 | void setGyroRange( MPU9250BIT range ); |
bluefish | 1:5334e111af53 | 221 | void setDLPF( MPU9250BIT range ); |
bluefish | 1:5334e111af53 | 222 | |
bluefish | 1:5334e111af53 | 223 | void read6AxisRaw( |
bluefish | 1:5334e111af53 | 224 | int16_t* ax, int16_t* ay, int16_t* az, |
bluefish | 1:5334e111af53 | 225 | int16_t* gx, int16_t* gy, int16_t* gz ); |
bluefish | 1:5334e111af53 | 226 | void read9AxisRaw( |
bluefish | 1:5334e111af53 | 227 | int16_t* ax, int16_t* ay, int16_t* az, |
bluefish | 1:5334e111af53 | 228 | int16_t* gx, int16_t* gy, int16_t* gz, |
bluefish | 1:5334e111af53 | 229 | int16_t* mx, int16_t* my, int16_t* mz ); |
bluefish | 1:5334e111af53 | 230 | void readAccelRaw( int16_t* ax, int16_t* ay, int16_t* az ); |
bluefish | 1:5334e111af53 | 231 | void readGyroRaw( int16_t* gx, int16_t* gy, int16_t* gz ); |
bluefish | 1:5334e111af53 | 232 | void readMagRaw( int16_t* mx, int16_t* my, int16_t* mz ); |
bluefish | 1:5334e111af53 | 233 | int16_t readTempRaw( void ); |
bluefish | 1:5334e111af53 | 234 | |
bluefish | 1:5334e111af53 | 235 | void read6Axis( |
bluefish | 1:5334e111af53 | 236 | float* ax, float* ay, float* az, |
bluefish | 1:5334e111af53 | 237 | float* gx, float* gy, float* gz ); |
bluefish | 1:5334e111af53 | 238 | void read9Axis( |
bluefish | 1:5334e111af53 | 239 | float* ax, float* ay, float* az, |
bluefish | 1:5334e111af53 | 240 | float* gx, float* gy, float* gz, |
bluefish | 1:5334e111af53 | 241 | float* mx, float* my, float* mz ); |
bluefish | 1:5334e111af53 | 242 | void readAccel( float* ax, float* ay, float* az ); |
bluefish | 1:5334e111af53 | 243 | void readGyro( float* gx, float* gy, float* gz ); |
bluefish | 1:5334e111af53 | 244 | void readMag( float* mx, float* my, float* mz ); |
bluefish | 1:5334e111af53 | 245 | float readTemp( void ); |
bluefish | 1:5334e111af53 | 246 | |
bluefish | 1:5334e111af53 | 247 | private: |
bluefish | 1:5334e111af53 | 248 | DigitalOut* _cs; |
bluefish | 1:5334e111af53 | 249 | SPI* _spi; |
bluefish | 1:5334e111af53 | 250 | I2C* _i2c; |
bluefish | 1:5334e111af53 | 251 | uint8_t _i2c_addr; |
bluefish | 1:5334e111af53 | 252 | |
bluefish | 1:5334e111af53 | 253 | float _accscale; |
bluefish | 1:5334e111af53 | 254 | float _gyroscale; |
bluefish | 1:5334e111af53 | 255 | // float _gyroscalerad; |
bluefish | 1:5334e111af53 | 256 | |
bluefish | 1:5334e111af53 | 257 | uint8_t _readRegister( MPU9250REG addr ); |
bluefish | 1:5334e111af53 | 258 | uint8_t _writeRegister( MPU9250REG addr, uint8_t data ); |
bluefish | 1:5334e111af53 | 259 | uint8_t _readBuffer( MPU9250REG addr, uint8_t len, uint8_t* buf ); |
bluefish | 1:5334e111af53 | 260 | uint8_t _writeBuffer( MPU9250REG addr, uint8_t len, uint8_t* buf ); |
bluefish | 1:5334e111af53 | 261 | }; |
bluefish | 1:5334e111af53 | 262 | |
bluefish | 1:5334e111af53 | 263 | #endif |