Latest version of my quadcopter controller with an LPC1768 and MPU9250.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPU9250_SPI.h Source File

MPU9250_SPI.h

00001 /*CODED by Qiyong Mu on 21/06/2014
00002 kylongmu@msn.com
00003 */
00004 
00005 
00006 #ifndef mpu9250_spi_h
00007 #define mpu9250_spi_h
00008 #include "mbed.h"
00009 
00010 
00011 class mpu9250_spi
00012 {
00013     SPI& spi;
00014     DigitalOut cs;
00015     
00016   public:
00017     mpu9250_spi(SPI& _spi, PinName _cs);
00018     unsigned int WriteReg( uint8_t WriteAddr, uint8_t WriteData );
00019     unsigned int ReadReg( uint8_t WriteAddr, uint8_t WriteData );
00020     void ReadRegs( uint8_t ReadAddr, uint8_t *ReadBuf, unsigned int Bytes );
00021 
00022     bool init(int sample_rate_div,int low_pass_filter);
00023       bool init2(int sample_rate_div,int low_pass_filter);
00024     void read_temp();
00025     void read_acc();
00026     void read_rot();
00027     unsigned int set_gyro_scale(int scale);
00028     unsigned int set_acc_scale(int scale);
00029     void calib_acc();
00030     void AK8963_calib_Magnetometer();
00031     void select();
00032     void deselect();
00033     unsigned int whoami();
00034     uint8_t  AK8963_whoami();
00035     void AK8963_read_Magnetometer();
00036     void read_all();
00037 
00038 
00039     
00040     float acc_divider;
00041     float gyro_divider;
00042     
00043     int calib_data[3];
00044     float Magnetometer_ASA[3];
00045 
00046     float accelerometer_data[3];
00047     float Temperature;
00048     float gyroscope_data[3];
00049     float Magnetometer[3];
00050     
00051   private:
00052     PinName _CS_pin;
00053     PinName _SO_pin;
00054     PinName _SCK_pin;
00055     float _error;
00056 };
00057 
00058 #endif
00059 
00060 
00061 
00062 // mpu9250 registers
00063 #define MPUREG_XG_OFFS_TC 0x00
00064 #define MPUREG_YG_OFFS_TC 0x01
00065 #define MPUREG_ZG_OFFS_TC 0x02
00066 #define MPUREG_X_FINE_GAIN 0x03
00067 #define MPUREG_Y_FINE_GAIN 0x04
00068 #define MPUREG_Z_FINE_GAIN 0x05
00069 #define MPUREG_XA_OFFS_H 0x06
00070 #define MPUREG_XA_OFFS_L 0x07
00071 #define MPUREG_YA_OFFS_H 0x08
00072 #define MPUREG_YA_OFFS_L 0x09
00073 #define MPUREG_ZA_OFFS_H 0x0A
00074 #define MPUREG_ZA_OFFS_L 0x0B
00075 #define MPUREG_PRODUCT_ID 0x0C
00076 #define MPUREG_SELF_TEST_X 0x0D
00077 #define MPUREG_SELF_TEST_Y 0x0E
00078 #define MPUREG_SELF_TEST_Z 0x0F
00079 #define MPUREG_SELF_TEST_A 0x10
00080 #define MPUREG_XG_OFFS_USRH 0x13
00081 #define MPUREG_XG_OFFS_USRL 0x14
00082 #define MPUREG_YG_OFFS_USRH 0x15
00083 #define MPUREG_YG_OFFS_USRL 0x16
00084 #define MPUREG_ZG_OFFS_USRH 0x17
00085 #define MPUREG_ZG_OFFS_USRL 0x18
00086 #define MPUREG_SMPLRT_DIV 0x19
00087 #define MPUREG_CONFIG 0x1A
00088 #define MPUREG_GYRO_CONFIG 0x1B
00089 #define MPUREG_ACCEL_CONFIG 0x1C
00090 #define MPUREG_ACCEL_CONFIG_2      0x1D
00091 #define MPUREG_LP_ACCEL_ODR        0x1E
00092 #define MPUREG_MOT_THR             0x1F
00093 #define MPUREG_FIFO_EN             0x23
00094 #define MPUREG_I2C_MST_CTRL        0x24
00095 #define MPUREG_I2C_SLV0_ADDR       0x25
00096 #define MPUREG_I2C_SLV0_REG        0x26
00097 #define MPUREG_I2C_SLV0_CTRL       0x27
00098 #define MPUREG_I2C_SLV1_ADDR       0x28
00099 #define MPUREG_I2C_SLV1_REG        0x29
00100 #define MPUREG_I2C_SLV1_CTRL       0x2A
00101 #define MPUREG_I2C_SLV2_ADDR       0x2B
00102 #define MPUREG_I2C_SLV2_REG        0x2C
00103 #define MPUREG_I2C_SLV2_CTRL       0x2D
00104 #define MPUREG_I2C_SLV3_ADDR       0x2E
00105 #define MPUREG_I2C_SLV3_REG        0x2F
00106 #define MPUREG_I2C_SLV3_CTRL       0x30
00107 #define MPUREG_I2C_SLV4_ADDR       0x31
00108 #define MPUREG_I2C_SLV4_REG        0x32
00109 #define MPUREG_I2C_SLV4_DO         0x33
00110 #define MPUREG_I2C_SLV4_CTRL       0x34
00111 #define MPUREG_I2C_SLV4_DI         0x35
00112 #define MPUREG_I2C_MST_STATUS      0x36
00113 #define MPUREG_INT_PIN_CFG 0x37
00114 #define MPUREG_INT_ENABLE 0x38
00115 #define MPUREG_ACCEL_XOUT_H 0x3B
00116 #define MPUREG_ACCEL_XOUT_L 0x3C
00117 #define MPUREG_ACCEL_YOUT_H 0x3D
00118 #define MPUREG_ACCEL_YOUT_L 0x3E
00119 #define MPUREG_ACCEL_ZOUT_H 0x3F
00120 #define MPUREG_ACCEL_ZOUT_L 0x40
00121 #define MPUREG_TEMP_OUT_H 0x41
00122 #define MPUREG_TEMP_OUT_L 0x42
00123 #define MPUREG_GYRO_XOUT_H 0x43
00124 #define MPUREG_GYRO_XOUT_L 0x44
00125 #define MPUREG_GYRO_YOUT_H 0x45
00126 #define MPUREG_GYRO_YOUT_L 0x46
00127 #define MPUREG_GYRO_ZOUT_H 0x47
00128 #define MPUREG_GYRO_ZOUT_L 0x48
00129 #define MPUREG_EXT_SENS_DATA_00    0x49
00130 #define MPUREG_EXT_SENS_DATA_01    0x4A
00131 #define MPUREG_EXT_SENS_DATA_02    0x4B
00132 #define MPUREG_EXT_SENS_DATA_03    0x4C
00133 #define MPUREG_EXT_SENS_DATA_04    0x4D
00134 #define MPUREG_EXT_SENS_DATA_05    0x4E
00135 #define MPUREG_EXT_SENS_DATA_06    0x4F
00136 #define MPUREG_EXT_SENS_DATA_07    0x50
00137 #define MPUREG_EXT_SENS_DATA_08    0x51
00138 #define MPUREG_EXT_SENS_DATA_09    0x52
00139 #define MPUREG_EXT_SENS_DATA_10    0x53
00140 #define MPUREG_EXT_SENS_DATA_11    0x54
00141 #define MPUREG_EXT_SENS_DATA_12    0x55
00142 #define MPUREG_EXT_SENS_DATA_13    0x56
00143 #define MPUREG_EXT_SENS_DATA_14    0x57
00144 #define MPUREG_EXT_SENS_DATA_15    0x58
00145 #define MPUREG_EXT_SENS_DATA_16    0x59
00146 #define MPUREG_EXT_SENS_DATA_17    0x5A
00147 #define MPUREG_EXT_SENS_DATA_18    0x5B
00148 #define MPUREG_EXT_SENS_DATA_19    0x5C
00149 #define MPUREG_EXT_SENS_DATA_20    0x5D
00150 #define MPUREG_EXT_SENS_DATA_21    0x5E
00151 #define MPUREG_EXT_SENS_DATA_22    0x5F
00152 #define MPUREG_EXT_SENS_DATA_23    0x60
00153 #define MPUREG_I2C_SLV0_DO         0x63
00154 #define MPUREG_I2C_SLV1_DO         0x64
00155 #define MPUREG_I2C_SLV2_DO         0x65
00156 #define MPUREG_I2C_SLV3_DO         0x66
00157 #define MPUREG_I2C_MST_DELAY_CTRL  0x67
00158 #define MPUREG_SIGNAL_PATH_RESET   0x68
00159 #define MPUREG_MOT_DETECT_CTRL     0x69
00160 #define MPUREG_USER_CTRL 0x6A
00161 #define MPUREG_PWR_MGMT_1 0x6B
00162 #define MPUREG_PWR_MGMT_2 0x6C
00163 #define MPUREG_BANK_SEL 0x6D
00164 #define MPUREG_MEM_START_ADDR 0x6E
00165 #define MPUREG_MEM_R_W 0x6F
00166 #define MPUREG_DMP_CFG_1 0x70
00167 #define MPUREG_DMP_CFG_2 0x71
00168 #define MPUREG_FIFO_COUNTH 0x72
00169 #define MPUREG_FIFO_COUNTL 0x73
00170 #define MPUREG_FIFO_R_W 0x74
00171 #define MPUREG_WHOAMI 0x75
00172 #define MPUREG_XA_OFFSET_H         0x77
00173 #define MPUREG_XA_OFFSET_L         0x78
00174 #define MPUREG_YA_OFFSET_H         0x7A
00175 #define MPUREG_YA_OFFSET_L         0x7B
00176 #define MPUREG_ZA_OFFSET_H         0x7D
00177 #define MPUREG_ZA_OFFSET_L         0x7E
00178 /* ---- AK8963 Reg In MPU9250 ----------------------------------------------- */
00179 
00180 #define AK8963_I2C_ADDR             0x0c//0x18
00181 #define AK8963_Device_ID            0x48
00182 
00183 // Read-only Reg
00184 #define AK8963_WIA                  0x00
00185 #define AK8963_INFO                 0x01
00186 #define AK8963_ST1                  0x02
00187 #define AK8963_HXL                  0x03
00188 #define AK8963_HXH                  0x04
00189 #define AK8963_HYL                  0x05
00190 #define AK8963_HYH                  0x06
00191 #define AK8963_HZL                  0x07
00192 #define AK8963_HZH                  0x08
00193 #define AK8963_ST2                  0x09
00194 // Write/Read Reg
00195 #define AK8963_CNTL1                0x0A
00196 #define AK8963_CNTL2                0x0B
00197 #define AK8963_ASTC                 0x0C
00198 #define AK8963_TS1                  0x0D
00199 #define AK8963_TS2                  0x0E
00200 #define AK8963_I2CDIS               0x0F
00201 // Read-only Reg ( ROM )
00202 #define AK8963_ASAX                 0x10
00203 #define AK8963_ASAY                 0x11
00204 #define AK8963_ASAZ                 0x12
00205 
00206 // Configuration bits mpu9250
00207 #define BIT_SLEEP 0x40
00208 #define BIT_H_RESET 0x80
00209 #define BITS_CLKSEL 0x07
00210 #define MPU_CLK_SEL_PLLGYROX 0x01
00211 #define MPU_CLK_SEL_PLLGYROZ 0x03
00212 #define MPU_EXT_SYNC_GYROX 0x02
00213 #define BITS_FS_250DPS              0x00
00214 #define BITS_FS_500DPS              0x08
00215 #define BITS_FS_1000DPS             0x10
00216 #define BITS_FS_2000DPS             0x18
00217 #define BITS_FS_2G                  0x00
00218 #define BITS_FS_4G                  0x08
00219 #define BITS_FS_8G                  0x10
00220 #define BITS_FS_16G                 0x18
00221 #define BITS_FS_MASK                0x18
00222 #define BITS_DLPF_CFG_256HZ_NOLPF2  0x00
00223 #define BITS_DLPF_CFG_188HZ         0x01
00224 #define BITS_DLPF_CFG_98HZ          0x02
00225 #define BITS_DLPF_CFG_42HZ          0x03
00226 #define BITS_DLPF_CFG_20HZ          0x04
00227 #define BITS_DLPF_CFG_10HZ          0x05
00228 #define BITS_DLPF_CFG_5HZ           0x06
00229 #define BITS_DLPF_CFG_2100HZ_NOLPF  0x07
00230 #define BITS_DLPF_CFG_MASK          0x07
00231 #define BIT_INT_ANYRD_2CLEAR        0x10
00232 #define BIT_RAW_RDY_EN              0x01
00233 #define BIT_I2C_IF_DIS              0x10
00234 
00235 #define READ_FLAG   0x80
00236 
00237 /* ---- Sensitivity --------------------------------------------------------- */
00238 
00239 #define MPU9250A_2g       ((float)0.000061035156f) // 0.000061035156 g/LSB
00240 #define MPU9250A_4g       ((float)0.000122070312f) // 0.000122070312 g/LSB
00241 #define MPU9250A_8g       ((float)0.000244140625f) // 0.000244140625 g/LSB
00242 #define MPU9250A_16g      ((float)0.000488281250f) // 0.000488281250 g/LSB
00243 
00244 #define MPU9250G_250dps   ((float)0.007633587786f) // 0.007633587786 dps/LSB
00245 #define MPU9250G_500dps   ((float)0.015267175572f) // 0.015267175572 dps/LSB
00246 #define MPU9250G_1000dps  ((float)0.030487804878f) // 0.030487804878 dps/LSB
00247 #define MPU9250G_2000dps  ((float)0.060975609756f) // 0.060975609756 dps/LSB
00248 
00249 #define MPU9250M_4800uT   ((float)0.6f)            // 0.6 uT/LSB
00250 
00251 #define MPU9250T_85degC   ((float)0.002995177763f) // 0.002995177763 degC/LSB
00252 
00253 #define     Magnetometer_Sensitivity_Scale_Factor ((float)0.15f)    
00254