Driver for MPU9250 with SPI .

Committer:
wngudwls000
Date:
Thu May 13 10:35:07 2021 +0000
Revision:
12:c47d8cbfdd15
mpu_9250_spi

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wngudwls000 12:c47d8cbfdd15 1 #ifndef MPU9250REGISTERMAP_H
wngudwls000 12:c47d8cbfdd15 2 #define MPU9250REGISTERMAP_H
wngudwls000 12:c47d8cbfdd15 3 #include <cmath>
wngudwls000 12:c47d8cbfdd15 4 #include "mbed.h"
wngudwls000 12:c47d8cbfdd15 5 // MPU9250 with SPI interface library Ver. 0.98
wngudwls000 12:c47d8cbfdd15 6 // Made by HeeJae Park
wngudwls000 12:c47d8cbfdd15 7 // 2019.05.27
wngudwls000 12:c47d8cbfdd15 8 //Magnetometer Registers ================================
wngudwls000 12:c47d8cbfdd15 9 #define AK8963_WHO_AM_I 0x00 // should return 0x48
wngudwls000 12:c47d8cbfdd15 10 #define AK8963_INFO 0x01
wngudwls000 12:c47d8cbfdd15 11 #define AK8963_ST1 0x02 // data ready status bit 0
wngudwls000 12:c47d8cbfdd15 12 #define AK8963_XOUT_L 0x03 // data
wngudwls000 12:c47d8cbfdd15 13 #define AK8963_XOUT_H 0x04
wngudwls000 12:c47d8cbfdd15 14 #define AK8963_YOUT_L 0x05
wngudwls000 12:c47d8cbfdd15 15 #define AK8963_YOUT_H 0x06
wngudwls000 12:c47d8cbfdd15 16 #define AK8963_ZOUT_L 0x07
wngudwls000 12:c47d8cbfdd15 17 #define AK8963_ZOUT_H 0x08
wngudwls000 12:c47d8cbfdd15 18 #define AK8963_ST2 0x09 // Data overflow bit 3 and data read error status bit 2
wngudwls000 12:c47d8cbfdd15 19 #define AK8963_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
wngudwls000 12:c47d8cbfdd15 20 #define AK8963_CNTL2 0x0B
wngudwls000 12:c47d8cbfdd15 21 #define AK8963_ASTC 0x0C // Self test control
wngudwls000 12:c47d8cbfdd15 22 #define AK8963_I2CDIS 0x0F // I2C disable
wngudwls000 12:c47d8cbfdd15 23 #define AK8963_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
wngudwls000 12:c47d8cbfdd15 24 #define AK8963_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
wngudwls000 12:c47d8cbfdd15 25 #define AK8963_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
wngudwls000 12:c47d8cbfdd15 26 //IMU Registers ==========================================
wngudwls000 12:c47d8cbfdd15 27 #define SELF_TEST_X_GYRO 0x00
wngudwls000 12:c47d8cbfdd15 28 #define SELF_TEST_Y_GYRO 0x01
wngudwls000 12:c47d8cbfdd15 29 #define SELF_TEST_Z_GYRO 0x02
wngudwls000 12:c47d8cbfdd15 30 #define SELF_TEST_X_ACCEL 0x0D
wngudwls000 12:c47d8cbfdd15 31 #define SELF_TEST_Y_ACCEL 0x0E
wngudwls000 12:c47d8cbfdd15 32 #define SELF_TEST_Z_ACCEL 0x0F
wngudwls000 12:c47d8cbfdd15 33 #define SELF_TEST_A 0x10
wngudwls000 12:c47d8cbfdd15 34 #define XG_OFFSET_H 0x13 // User-defined trim values for gyroscope
wngudwls000 12:c47d8cbfdd15 35 #define XG_OFFSET_L 0x14
wngudwls000 12:c47d8cbfdd15 36 #define YG_OFFSET_H 0x15
wngudwls000 12:c47d8cbfdd15 37 #define YG_OFFSET_L 0x16
wngudwls000 12:c47d8cbfdd15 38 #define ZG_OFFSET_H 0x17
wngudwls000 12:c47d8cbfdd15 39 #define ZG_OFFSET_L 0x18
wngudwls000 12:c47d8cbfdd15 40 #define SMPLRT_DIV 0x19
wngudwls000 12:c47d8cbfdd15 41 #define MPU_CONFIG 0x1A
wngudwls000 12:c47d8cbfdd15 42 #define GYRO_CONFIG 0x1B
wngudwls000 12:c47d8cbfdd15 43 #define ACCEL_CONFIG 0x1C
wngudwls000 12:c47d8cbfdd15 44 #define ACCEL_CONFIG2 0x1D
wngudwls000 12:c47d8cbfdd15 45 #define LP_ACCEL_ODR 0x1E
wngudwls000 12:c47d8cbfdd15 46 #define WOM_THR 0x1F
wngudwls000 12:c47d8cbfdd15 47 #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
wngudwls000 12:c47d8cbfdd15 48 #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
wngudwls000 12:c47d8cbfdd15 49 #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
wngudwls000 12:c47d8cbfdd15 50 #define FIFO_EN 0x23
wngudwls000 12:c47d8cbfdd15 51 #define I2C_MST_CTRL 0x24
wngudwls000 12:c47d8cbfdd15 52 #define I2C_SLV0_ADDR 0x25
wngudwls000 12:c47d8cbfdd15 53 #define I2C_SLV0_REG 0x26
wngudwls000 12:c47d8cbfdd15 54 #define I2C_SLV0_CTRL 0x27
wngudwls000 12:c47d8cbfdd15 55 #define I2C_SLV1_ADDR 0x28
wngudwls000 12:c47d8cbfdd15 56 #define I2C_SLV1_REG 0x29
wngudwls000 12:c47d8cbfdd15 57 #define I2C_SLV1_CTRL 0x2A
wngudwls000 12:c47d8cbfdd15 58 #define I2C_SLV2_ADDR 0x2B
wngudwls000 12:c47d8cbfdd15 59 #define I2C_SLV2_REG 0x2C
wngudwls000 12:c47d8cbfdd15 60 #define I2C_SLV2_CTRL 0x2D
wngudwls000 12:c47d8cbfdd15 61 #define I2C_SLV3_ADDR 0x2E
wngudwls000 12:c47d8cbfdd15 62 #define I2C_SLV3_REG 0x2F
wngudwls000 12:c47d8cbfdd15 63 #define I2C_SLV3_CTRL 0x30
wngudwls000 12:c47d8cbfdd15 64 #define I2C_SLV4_ADDR 0x31
wngudwls000 12:c47d8cbfdd15 65 #define I2C_SLV4_REG 0x32
wngudwls000 12:c47d8cbfdd15 66 #define I2C_SLV4_DO 0x33
wngudwls000 12:c47d8cbfdd15 67 #define I2C_SLV4_CTRL 0x34
wngudwls000 12:c47d8cbfdd15 68 #define I2C_SLV4_DI 0x35
wngudwls000 12:c47d8cbfdd15 69 #define I2C_MST_STATUS 0x36
wngudwls000 12:c47d8cbfdd15 70 #define INT_PIN_CFG 0x37
wngudwls000 12:c47d8cbfdd15 71 #define INT_ENABLE 0x38
wngudwls000 12:c47d8cbfdd15 72 #define DMP_INT_STATUS 0x39 // Check DMP interrupt
wngudwls000 12:c47d8cbfdd15 73 #define INT_STATUS 0x3A
wngudwls000 12:c47d8cbfdd15 74 #define ACCEL_XOUT_H 0x3B
wngudwls000 12:c47d8cbfdd15 75 #define ACCEL_XOUT_L 0x3C
wngudwls000 12:c47d8cbfdd15 76 #define ACCEL_YOUT_H 0x3D
wngudwls000 12:c47d8cbfdd15 77 #define ACCEL_YOUT_L 0x3E
wngudwls000 12:c47d8cbfdd15 78 #define ACCEL_ZOUT_H 0x3F
wngudwls000 12:c47d8cbfdd15 79 #define ACCEL_ZOUT_L 0x40
wngudwls000 12:c47d8cbfdd15 80 #define TEMP_OUT_H 0x41
wngudwls000 12:c47d8cbfdd15 81 #define TEMP_OUT_L 0x42
wngudwls000 12:c47d8cbfdd15 82 #define GYRO_XOUT_H 0x43
wngudwls000 12:c47d8cbfdd15 83 #define GYRO_XOUT_L 0x44
wngudwls000 12:c47d8cbfdd15 84 #define GYRO_YOUT_H 0x45
wngudwls000 12:c47d8cbfdd15 85 #define GYRO_YOUT_L 0x46
wngudwls000 12:c47d8cbfdd15 86 #define GYRO_ZOUT_H 0x47
wngudwls000 12:c47d8cbfdd15 87 #define GYRO_ZOUT_L 0x48
wngudwls000 12:c47d8cbfdd15 88 #define EXT_SENS_DATA_00 0x49
wngudwls000 12:c47d8cbfdd15 89 #define EXT_SENS_DATA_01 0x4A
wngudwls000 12:c47d8cbfdd15 90 #define EXT_SENS_DATA_02 0x4B
wngudwls000 12:c47d8cbfdd15 91 #define EXT_SENS_DATA_03 0x4C
wngudwls000 12:c47d8cbfdd15 92 #define EXT_SENS_DATA_04 0x4D
wngudwls000 12:c47d8cbfdd15 93 #define EXT_SENS_DATA_05 0x4E
wngudwls000 12:c47d8cbfdd15 94 #define EXT_SENS_DATA_06 0x4F
wngudwls000 12:c47d8cbfdd15 95 #define EXT_SENS_DATA_07 0x50
wngudwls000 12:c47d8cbfdd15 96 #define EXT_SENS_DATA_08 0x51
wngudwls000 12:c47d8cbfdd15 97 #define EXT_SENS_DATA_09 0x52
wngudwls000 12:c47d8cbfdd15 98 #define EXT_SENS_DATA_10 0x53
wngudwls000 12:c47d8cbfdd15 99 #define EXT_SENS_DATA_11 0x54
wngudwls000 12:c47d8cbfdd15 100 #define EXT_SENS_DATA_12 0x55
wngudwls000 12:c47d8cbfdd15 101 #define EXT_SENS_DATA_13 0x56
wngudwls000 12:c47d8cbfdd15 102 #define EXT_SENS_DATA_14 0x57
wngudwls000 12:c47d8cbfdd15 103 #define EXT_SENS_DATA_15 0x58
wngudwls000 12:c47d8cbfdd15 104 #define EXT_SENS_DATA_16 0x59
wngudwls000 12:c47d8cbfdd15 105 #define EXT_SENS_DATA_17 0x5A
wngudwls000 12:c47d8cbfdd15 106 #define EXT_SENS_DATA_18 0x5B
wngudwls000 12:c47d8cbfdd15 107 #define EXT_SENS_DATA_19 0x5C
wngudwls000 12:c47d8cbfdd15 108 #define EXT_SENS_DATA_20 0x5D
wngudwls000 12:c47d8cbfdd15 109 #define EXT_SENS_DATA_21 0x5E
wngudwls000 12:c47d8cbfdd15 110 #define EXT_SENS_DATA_22 0x5F
wngudwls000 12:c47d8cbfdd15 111 #define EXT_SENS_DATA_23 0x60
wngudwls000 12:c47d8cbfdd15 112 #define MOT_DETECT_STATUS 0x61
wngudwls000 12:c47d8cbfdd15 113 #define I2C_SLV0_DO 0x63
wngudwls000 12:c47d8cbfdd15 114 #define I2C_SLV1_DO 0x64
wngudwls000 12:c47d8cbfdd15 115 #define I2C_SLV2_DO 0x65
wngudwls000 12:c47d8cbfdd15 116 #define I2C_SLV3_DO 0x66
wngudwls000 12:c47d8cbfdd15 117 #define I2C_MST_DELAY_CTRL 0x67
wngudwls000 12:c47d8cbfdd15 118 #define SIGNAL_PATH_RESET 0x68
wngudwls000 12:c47d8cbfdd15 119 #define MOT_DETECT_CTRL 0x69
wngudwls000 12:c47d8cbfdd15 120 #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
wngudwls000 12:c47d8cbfdd15 121 #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
wngudwls000 12:c47d8cbfdd15 122 #define PWR_MGMT_2 0x6C
wngudwls000 12:c47d8cbfdd15 123 #define DMP_BANK 0x6D // Activates a specific bank in the DMP
wngudwls000 12:c47d8cbfdd15 124 #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
wngudwls000 12:c47d8cbfdd15 125 #define DMP_REG 0x6F // Register in DMP from which to read or to which to write
wngudwls000 12:c47d8cbfdd15 126 #define DMP_REG_1 0x70
wngudwls000 12:c47d8cbfdd15 127 #define DMP_REG_2 0x71
wngudwls000 12:c47d8cbfdd15 128 #define FIFO_COUNTH 0x72
wngudwls000 12:c47d8cbfdd15 129 #define FIFO_COUNTL 0x73
wngudwls000 12:c47d8cbfdd15 130 #define FIFO_R_W 0x74
wngudwls000 12:c47d8cbfdd15 131 #define WHO_AM_I_MPU9250 0x75 // Should return 0x71
wngudwls000 12:c47d8cbfdd15 132 #define XA_OFFSET_H 0x77
wngudwls000 12:c47d8cbfdd15 133 #define XA_OFFSET_L 0x78
wngudwls000 12:c47d8cbfdd15 134 #define YA_OFFSET_H 0x7A
wngudwls000 12:c47d8cbfdd15 135 #define YA_OFFSET_L 0x7B
wngudwls000 12:c47d8cbfdd15 136 #define ZA_OFFSET_H 0x7D
wngudwls000 12:c47d8cbfdd15 137 #define ZA_OFFSET_L 0x7E
wngudwls000 12:c47d8cbfdd15 138 // =================== Importat values
wngudwls000 12:c47d8cbfdd15 139 #define AK8963_I2C_ADDR 0x0C
wngudwls000 12:c47d8cbfdd15 140 #define AK8963_RESET 0x01// @ CNTL2
wngudwls000 12:c47d8cbfdd15 141 #define MPU9250_WHOAMI_DEFAULT_VALUE 0x71 // 고유번호
wngudwls000 12:c47d8cbfdd15 142 #define AK8963_WHOAMI_DEFAULT_VALUE 0x48
wngudwls000 12:c47d8cbfdd15 143 #define SPI_LS_CLOCK 15000000 // 1 MHz
wngudwls000 12:c47d8cbfdd15 144 #define SPI_HS_CLOCK 15000000 // 15 MHz
wngudwls000 12:c47d8cbfdd15 145 #define I2C_READ_FLAG 0x80 // for all I2C
wngudwls000 12:c47d8cbfdd15 146 #define SPI_READ 0x80 //SPI READ
wngudwls000 12:c47d8cbfdd15 147 #define I2C_MST_EN 0x20 // @ USER_CTRL
wngudwls000 12:c47d8cbfdd15 148 #define I2C_MST_CLK 0x0D // @I2C_MST_CTRL 400KHz
wngudwls000 12:c47d8cbfdd15 149 #define I2C_SLV0_EN 0x80 // @I2C_SLV0_CTRL slave 0 enable
wngudwls000 12:c47d8cbfdd15 150 #define CLOCK_SEL_PLL 0x01 // @ PWR_MGMNT_1
wngudwls000 12:c47d8cbfdd15 151 #define PWR_RESET 0x80 // @ PWR_MGMNT_1
wngudwls000 12:c47d8cbfdd15 152 #define SEN_ENABLE 0x00 // @ PWR_MGMNT_2
wngudwls000 12:c47d8cbfdd15 153 // some conversion
wngudwls000 12:c47d8cbfdd15 154 #ifndef M_PI
wngudwls000 12:c47d8cbfdd15 155 #define M_PI 3.14159265358979323846
wngudwls000 12:c47d8cbfdd15 156 #endif
wngudwls000 12:c47d8cbfdd15 157 #define DEG_TO_RAD ( M_PI /180)
wngudwls000 12:c47d8cbfdd15 158 #define RAD_TO_DEG (180/M_PI)
wngudwls000 12:c47d8cbfdd15 159 #define TWO_PI (2*M_PI)
wngudwls000 12:c47d8cbfdd15 160 // complementary filter
wngudwls000 12:c47d8cbfdd15 161
wngudwls000 12:c47d8cbfdd15 162
wngudwls000 12:c47d8cbfdd15 163
wngudwls000 12:c47d8cbfdd15 164 #endif // MPU9250REGISTERMAP_H