-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMPU_9250.h
317 lines (271 loc) · 6.98 KB
/
MPU_9250.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "LSP.h"
#ifdef USE_MPU_9250
#ifndef MPU_9250_H
#define MPU_9250_H
#include "stdint.h"
#define MPU_SLAVE_ADDRESS 0xD0
// read/write
// The value in this register indicates the self test output generated
// during manufacturing tests. This value is to be used to check
// against subsequent self test outputs performed by the end user.
#define SELF_TEST_X_GYRO_ADDR 0x00 //
#define SELF_TEST_Y_GYRO_ADDR 0x01
#define SELF_TEST_Z_GYRO_ADDR 0x02
#define SELF_TEST_X_ACCEL_ADDR 0x0D
#define SELF_TEST_Y_ACCEL_ADDR 0x0E
#define SELF_TEST_Z_ACCEL_ADDR 0x0F
#define XG_OFFSET_H_ADDR 0x13
#define XG_OFFSET_L_ADDR 0x14
#define YG_OFFSET_H_ADDR 0x15
#define YG_OFFSET_L_ADDR 0x16
#define ZG_OFFSET_H_ADDR 0x17
#define ZG_OFFSET_L_ADDR 0x18
#define SMPLRT_DIV_ADDR 0x19
#define CONFIG_ADDR 0x1A
#define GYRO_CONFIG_ADDR 0x1B
#define ACCEL_CONFIG_ADDR 0x1C
#define ACCEL_CONFIG_2_ADDR 0x1D
#define LP_ACCEL_ODR_ADDR 0x1E
#define WOM_THR_ADDR 0x1F
#define FIFO_EN_ADDR 0x23
#define I2C_MST_CTRL_ADDR 0x24
#define I2C_SLV0_ADDR 0x25
#define I2C_SLV0_REG_ADDR 0x26
#define I2C_SLV0_CTRL_ADDR 0x27
#define I2C_SLV1_ADDR_ADDR 0x28
#define I2C_SLV1_REG_ADDR 0x29
#define I2C_SLV1_CTRL_ADDR 0x2A
#define I2C_SLV2_ADDR_ADDR 0x2B
#define I2C_SLV2_REG_ADDR 0x2C
#define I2C_SLV2_CTRL_ADDR 0x2D
#define I2C_SLV3_ADDR_ADDR 0x2E
#define I2C_SLV3_REG_ADDR 0x2F
#define I2C_SLV3_CTRL_ADDR 0x30
#define I2C_SLV4_ADDR_ADDR 0x31
#define I2C_SLV4_REG_ADDR 0x32
#define I2C_SLV4_DO_ADDR 0x33
#define I2C_SLV4_CTRL_ADDR 0x34
#define I2C_SLV4_DI_ADDR 0x35
#define I2C_MST_STATUS_ADDR 0x36
#define INT_PIN_CFG_ADDR 0x37
#define INT_ENABLE_ADDR 0x38
#define INT_STATUS_ADDR 0x3A
// read only
// accel
#define ACCEL_XOUT_H_ADDR 0x3B
#define ACCEL_XOUT_L_ADDR 0x3C
#define ACCEL_YOUT_H_ADDR 0x3D
#define ACCEL_YOUT_L_ADDR 0x3E
#define ACCEL_ZOUT_H_ADDR 0x3F
#define ACCEL_ZOUT_L_ADDR 0x40
// temp
#define TEMP_OUT_H_ADDR 0x41
#define TEMP_OUT_L_ADDR 0x42
//gyro
#define GRYO_XOUT_H_ADDR 0x43
#define GRYO_XOUT_L_ADDR 0x44
#define GRYO_YOUT_H_ADDR 0x45
#define GRYO_YOUT_L_ADDR 0x46
#define GRYO_ZOUT_H_ADDR 0x47
#define GRYO_ZOUT_L_ADDR 0x48
// ext sen data, read only
#define EXT_SENS_DATA_00 0x49
// more registers in between
#define EXT_SENS_DATA_23 0x60
// read/write
#define I2C_SLV0_DO_ADDR 0x63
#define I2C_SLV1_DO_ADDR 0x64
#define I2C_SLV2_DO_ADDR 0x65
#define I2C_SLV3_DO_ADDR 0x66
#define I2C_MST_DELAY_CTRL_ADDR 0x67
#define SIGNAL_PATH_RESET_ADDR 0x68
#define MOT_DETECT_CTRL_ADDR 0x69
#define USER_CTRL_ADDR 0x6A
#define PWR_MGMT_1_ADDR 0x6B // reset is 0x01
#define PWR_MGMT_2_ADDR 0x6C
#define FIFO_COUNTH_ADDR 0x72
#define FIFO_COUNTL_ADDR 0x73
#define FIFO_R_W_ADDR 0x74
#define WHO_AM_I_ADDR 0x75 // reset is 0x71, read only
#define XA_OFFSET_H_ADDR 0x77
#define XA_OFFSET_L_ADDR 0x78
#define YA_OFFSET_H_ADDR 0x7A
#define YA_OFFSET_L_ADDR 0x7B
#define ZA_OFFSET_H_ADDR 0x7D
#define ZA_OFFSET_L_ADDR 0x7E
#define FS_SEL 1
#define OFF_SET_LSB 0
#define OFF_SET_DPS 1
// custom defines
#define X_REG 0
#define Y_REG 1
#define Z_REG 2
#define ACCEL_DATA_RDY 0
#define GRYO_DATA_RDY 1
#define MAG_DATA_RDY 2
// Set initial input parameters
#define AFS_2G 0
#define AFS_4G 1
#define AFS_8G 2
#define AFS_16G 3
#define GFS_250DPS 0
#define GFS_500DPS 1
#define GFS_1000DPS 2
#define GFS_2000DPS 3
#define MFS_14BITS 0 // 0.6 mG per LSB
#define MFS_16BITS 1 // 0.15 mG per LSB
#define M_8Hz 0x02
#define M_100Hz 0x06
typedef union{
struct{
int16_t value;
}Value;
struct{
int8_t lowByte;
int8_t highByte;
}Bytes;
}GyroOffsUsr;
typedef union{
struct{
int8_t value;
}Data;
struct{
unsigned DlpfCfg:3;
unsigned extSyncSet:3; // Enables the FSYNC pin data to be sampled.
unsigned fifoMode:1;// when set to 1, when the fifo is full, additional writes will not be written to fifo. If set to 0,
// when the fifo is full, additional writes will be written to the fifo, replacing the oldest data
unsigned :1;
}Status;
}Config_MPU;
typedef union{
struct{
uint8_t value;
}Data;
struct{
unsigned fChoiceB:2;
unsigned :1;
unsigned gyroFsSel:2;
unsigned zGyroCten:1;
unsigned yGyroCten:1;
unsigned xGyroCten:1;
}Status;
}GyroConfig;
typedef union{
struct{
uint8_t value;
}Data;
struct
{
unsigned :3;
unsigned accelFsSel:2; // accel full scale select
unsigned azStEn:1;
unsigned ayStEn:1;
unsigned axStEn:1; // x Accel self-test
}Status;
}AccelConfig;
typedef union{
struct{
uint8_t value;
}Data;
struct
{
unsigned a_dlpfCfg:2; // Accel full scale select
unsigned accel_fChoiceB:1;
unsigned :4;
}Status;
}AccelConfig2;
typedef union {
struct {
uint8_t value;
}Data;
struct {
unsigned posc_clksel:4;
unsigned :4;
}Status;
}LowPowerAccelerometerODRControl;
typedef union {
struct {
uint8_t value;
}Data;
struct {
unsigned slv_0:1;
unsigned slv_1:1;
unsigned slv_2:1;
unsigned accel:1;
unsigned gyro_zout:1;
unsigned gyro_yout:1;
unsigned gyro_xout:1;
unsigned temp_out:1;
}Status;
}FIFO_Enable;
typedef union {
struct {
uint8_t value;
}Data;
struct {
unsigned i2cMstClk:4;
unsigned i2cMst_P_NsrR:1;
unsigned slv3FifoEn:1;
unsigned waitForEs:1;
unsigned multMstEn:1;
}Status;
}I2c_MasterControl;
typedef union {
struct {
uint8_t value;
}Data;
struct {
unsigned rawRdyEn:1;
unsigned :2;
unsigned fsyncIntEn:1;
unsigned fifoOverflowEn:1;
unsigned :1;
unsigned womEn:1;
unsigned :1;
}Status;
}InterruptEnable;
typedef union {
struct {
uint8_t value;
}Data;
struct {
unsigned rawDataRdyInt:1;
unsigned :2;
unsigned fsyncInt:1;
unsigned fifoOverflowInt:1;
unsigned :1;
unsigned womInt:1;
unsigned :1;
}Status;
}InterruptStatus;
typedef union {
struct {
uint16_t value;
}Data;
struct {
uint8_t lowByte;
uint8_t highByte;
}Status;
}GyroOut;
//prototypes
int32_t OffSetGyroGet(GyroOffsUsr gyroValue, int8_t type);
void Config_MPUSend(Config_MPU *configMPU) ;
void GyroConfigSend(GyroConfig *gyroConfig);
void AccelConfigSend(AccelConfig *accelConfig);
void AccelConfig2Send(AccelConfig2 *accelConfig2);
void FIFO_EnableSend(FIFO_Enable *fifoEnable);
void I2C_MasterControlSend(I2c_MasterControl *i2cMasterControl);
void InterruptEnableSend(InterruptEnable *interruptEnable);
int GyroOutGet(uint8_t regAddr, uint8_t size);
int AccelOutGet(uint8_t regAddr, uint8_t size);
int MPU9250Init(void);
// these were ported over from a C++ project from https://github.com/kriswiner/MPU9250/blob/master/Dual_MPU9250/MPU9250.cpp
int InitMPU9250(uint8_t devAddr, uint8_t Ascale, uint8_t Gscale, uint8_t sampleRate);
int CalibrateMPU9250(uint8_t devAddr, float * dest1, float * dest2);
float GetGres(uint8_t Gscale);
float GetAres(uint8_t Ascale);
// place these 2 function calls where needed
int MPU_WriteByte(uint8_t devAddr, uint8_t regAddr, uint8_t data);
int MPU_ReadBytes(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t size);
#endif // MPU_9250_H
#endif // USE_MPU_9250