forked from omriiluz/WS2812B-LED-Driver-ChibiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU9150.h
322 lines (281 loc) · 12.1 KB
/
MPU9150.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
318
319
320
/*! \file MPU9150.h
*
* Intended for use with ChibiOS RT
*
* InvenSense Inc. Part: MPU-9150
* Ref: www.invensense.com
* Based on product specification Revision 4.0
*
*/
/*! \addtogroup mpu9150
* @{
*/
#ifndef _MPU9150_H
#define _MPU9150_H
#ifdef __cplusplus
extern "C" {
#endif
#include "ch.h"
#include "hal.h"
//#include "fc_net.h"
typedef uint8_t mpu9150_i2c_data;
typedef uint8_t mpu9150_reg_data;
typedef uint8_t mpu9150_reg_addr;
#define MPU9150_MAX_TX_BUFFER 50
#define MPU9150_MAX_RX_BUFFER 50
/*! register 55 INT pin/Bypass */
#define MPU9150_CLKOUT_EN ((mpu9150_reg_data)(1<<0))
#define MPU9150_I2C_BYPASS ((mpu9150_reg_data)(1<<1))
#define MPU9150_FSCYNC_INT_EN ((mpu9150_reg_data)(1<<2))
#define MPU9150_FSCYNC_INT_LEVEL ((mpu9150_reg_data)(1<<3))
#define MPU9150_INT_RD_CLEAR ((mpu9150_reg_data)(1<<4))
#define MPU9150_LATCH_INT_EN ((mpu9150_reg_data)(1<<5))
#define MPU9150_INT_OPEN ((mpu9150_reg_data)(1<<6))
#define MPU9150_INT_LEVEL ((mpu9150_reg_data)(1<<7))
/*! register 107 Power management 1 */
#define MPU9150_PM1_X_GYRO_CLOCKREF ((mpu9150_reg_data)(1<<0))
#define MPU9150_PM1_SLEEP ((mpu9150_reg_data)(1<<6))
#define MPU9150_PM1_RESET ((mpu9150_reg_data)(1<<7))
#define MPU9150_INT_EN_DATA_RD_EN ((mpu9150_reg_data)(1<<0))
#if DEBUG_MPU9150
/* see hal/include/i2c.h */
typedef struct i2c_error_info {
const char* err_string;
int error_number;
} i2c_error_info;
const char* i2c_errno_str(int32_t err) ;
#endif
/*! \typedef mpu9150_magn_regaddr
* MPU Magnetometer addresses
*
* Note the i2c address of the Magnetometer is 0x0c and is accessed through the auxiliary i2c bus
* when the mpu9150 is in 'pass-through' mode. See block diagram.
*/
typedef enum {
MAGN_DEVICE_ID = 0x00,//!< MAGN_DEVICE_ID
MAGN_INFORMATION = 0x01,//!< MAGN_INFORMATION
MAGN_STATUS_1 = 0x02,//!< MAGN_STATUS_1
MAGN_HXL = 0x03,//!< MAGN_HXL
MAGN_HXH = 0x04,//!< MAGN_HXH
MAGN_HYL = 0x05,//!< MAGN_HYL
MAGN_HYH = 0x06,//!< MAGN_HYH
MAGN_HZL = 0x07,//!< MAGN_HZL
MAGN_HZH = 0x08,//!< MAGN_HZH
MAGN_STATUS_2 = 0x09,//!< MAGN_STATUS_2
MAGN_CNTL = 0x0A,//!< MAGN_CNTL
MAGN_ASTC = 0x0C,//!< MAGN_ASTC
MAGN_TS1 = 0x0D,//!< MAGN_TS1
MAGN_TS2 = 0x0E,//!< MAGN_TS2
MAGN_I2C_DIS = 0x0F,//!< MAGN_I2C_DIS
MAGN_ASAX = 0x10,//!< MAGN_ASAX
MAGN_ASAY = 0x11,//!< MAGN_ASAY
MAGN_ASAZ = 0x12 //!< MAGN_ASAZ
} mpu9150_magn_regaddr;
/*! \typedef mpu9150_a_g_regaddr
*
* i2c slave address: 0x68 (see also who_am_i register)
*
* MPU Accelerometer and Gyroscope Register addresses
*/
typedef enum {
A_G_SELF_TEST_X = 0x0D, //!< A_G_SELF_TEST_X
A_G_SELF_TEST_Y = 0x0E,//!< A_G_SELF_TEST_Y
A_G_SELF_TEST_Z = 0x0F,//!< A_G_SELF_TEST_Z
A_G_SELF_TEST_A = 0x10,//!< A_G_SELF_TEST_A
A_G_SMPLRT_DIV = 0x19,//!< A_G_SMPLRT_DIV
A_G_CONFIG = 0x1A,//!< A_G_CONFIG
A_G_GYRO_CONFIG = 0x1B,//!< A_G_GYRO_CONFIG
A_G_ACCEL_CONFIG = 0x1C,//!< A_G_ACCEL_CONFIG
A_G_FF_THR = 0x1D,//!< A_G_FF_THR
A_G_FF_DUR = 0x1E,//!< A_G_FF_DUR
A_G_MOT_THR = 0x1F,//!< A_G_MOT_THR
A_G_MOT_DUR = 0x20,//!< A_G_MOT_DUR
A_G_ZRMOT_THR = 0x21,//!< A_G_ZRMOT_THR
A_G_ZRMOT_DUR = 0x22,//!< A_G_ZRMOT_DUR
A_G_FIFO_EN = 0x23,//!< A_G_FIFO_EN
A_G_I2C_MST_CTRL = 0x24,//!< A_G_I2C_MST_CTRL
A_G_I2C_SLV0_ADDR = 0x25,//!< A_G_I2C_SLV0_ADDR
A_G_I2C_SLV0_REG = 0x26,//!< A_G_I2C_SLV0_REG
A_G_I2C_SLV0_CTRL = 0x27,//!< A_G_I2C_SLV0_CTRL
A_G_I2C_SLV1_ADDR = 0x28,//!< A_G_I2C_SLV1_ADDR
A_G_I2C_SLV1_REG = 0x29,//!< A_G_I2C_SLV1_REG
A_G_I2C_SLV1_CTRL = 0x2A,//!< A_G_I2C_SLV1_CTRL
A_G_I2C_SLV2_ADDR = 0x2B,//!< A_G_I2C_SLV2_ADDR
A_G_I2C_SLV2_REG = 0x2C,//!< A_G_I2C_SLV2_REG
A_G_I2C_SLV2_CTRL = 0x2D,//!< A_G_I2C_SLV2_CTRL
A_G_I2C_SLV3_ADDR = 0x2E,//!< A_G_I2C_SLV3_ADDR
A_G_I2C_SLV3_REG = 0x2F,//!< A_G_I2C_SLV3_REG
A_G_I2C_SLV3_CTRL = 0x30,//!< A_G_I2C_SLV3_CTRL
A_G_I2C_SLV4_ADDR = 0x31,//!< A_G_I2C_SLV4_ADDR
A_G_I2C_SLV4_REG = 0x32,//!< A_G_I2C_SLV4_REG
A_G_I2C_SLV4_DO = 0x33,//!< A_G_I2C_SLV4_DO
A_G_I2C_SLV4_CTRL = 0x34,//!< A_G_I2C_SLV4_CTRL
A_G_I2C_SLV4_DI = 0x35,//!< A_G_I2C_SLV4_DI
A_G_I2C_MST_STATUS = 0x36,//!< A_G_I2C_MST_STATUS
A_G_INT_PIN_CFG = 0x37,//!< A_G_INT_PIN_CFG
A_G_INT_ENABLE = 0x38,//!< A_G_INT_ENABLE
A_G_INT_STATUS = 0x3A,//!< A_G_INT_STATUS
A_G_ACCEL_XOUT_H = 0x3B,//!< A_G_ACCEL_XOUT_H
A_G_ACCEL_XOUT_L = 0x3C,//!< A_G_ACCEL_XOUT_L
A_G_ACCEL_YOUT_H = 0x3D,//!< A_G_ACCEL_YOUT_H
A_G_ACCEL_YOUT_L = 0x3E,//!< A_G_ACCEL_YOUT_L
A_G_ACCEL_ZOUT_H = 0x3F,//!< A_G_ACCEL_ZOUT_H
A_G_ACCEL_ZOUT_L = 0x40,//!< A_G_ACCEL_ZOUT_L
A_G_TEMP_OUT_H = 0x41,//!< A_G_TEMP_OUT_H
A_G_TEMP_OUT_L = 0x42,//!< A_G_TEMP_OUT_L
A_G_GYRO_XOUT_H = 0x43,//!< A_G_GYRO_XOUT_H
A_G_GYRO_XOUT_L = 0x44,//!< A_G_GYRO_XOUT_L
A_G_GYRO_YOUT_H = 0x45,//!< A_G_GYRO_YOUT_H
A_G_GYRO_YOUT_L = 0x46,//!< A_G_GYRO_YOUT_L
A_G_GYRO_ZOUT_H = 0x47,//!< A_G_GYRO_ZOUT_H
A_G_GYRO_ZOUT_L = 0x48,//!< A_G_GYRO_ZOUT_L
A_G_EXT_SENS_DATA_00 = 0x49,//!< A_G_EXT_SENS_DATA_00
A_G_EXT_SENS_DATA_01 = 0x4A,//!< A_G_EXT_SENS_DATA_01
A_G_EXT_SENS_DATA_02 = 0x4B,//!< A_G_EXT_SENS_DATA_02
A_G_EXT_SENS_DATA_03 = 0x4C,//!< A_G_EXT_SENS_DATA_03
A_G_EXT_SENS_DATA_04 = 0x4D,//!< A_G_EXT_SENS_DATA_04
A_G_EXT_SENS_DATA_05 = 0x4E,//!< A_G_EXT_SENS_DATA_05
A_G_EXT_SENS_DATA_06 = 0x4F,//!< A_G_EXT_SENS_DATA_06
A_G_EXT_SENS_DATA_07 = 0x50,//!< A_G_EXT_SENS_DATA_07
A_G_EXT_SENS_DATA_08 = 0x51,//!< A_G_EXT_SENS_DATA_08
A_G_EXT_SENS_DATA_09 = 0x52,//!< A_G_EXT_SENS_DATA_09
A_G_EXT_SENS_DATA_10 = 0x53,//!< A_G_EXT_SENS_DATA_10
A_G_EXT_SENS_DATA_11 = 0x54,//!< A_G_EXT_SENS_DATA_11
A_G_EXT_SENS_DATA_12 = 0x55,//!< A_G_EXT_SENS_DATA_12
A_G_EXT_SENS_DATA_13 = 0x56,//!< A_G_EXT_SENS_DATA_13
A_G_EXT_SENS_DATA_14 = 0x57,//!< A_G_EXT_SENS_DATA_14
A_G_EXT_SENS_DATA_15 = 0x58,//!< A_G_EXT_SENS_DATA_15
A_G_EXT_SENS_DATA_16 = 0x59,//!< A_G_EXT_SENS_DATA_16
A_G_EXT_SENS_DATA_17 = 0x5A,//!< A_G_EXT_SENS_DATA_17
A_G_EXT_SENS_DATA_18 = 0x5B,//!< A_G_EXT_SENS_DATA_18
A_G_EXT_SENS_DATA_19 = 0x5C,//!< A_G_EXT_SENS_DATA_19
A_G_EXT_SENS_DATA_20 = 0x5D,//!< A_G_EXT_SENS_DATA_20
A_G_EXT_SENS_DATA_21 = 0x5E,//!< A_G_EXT_SENS_DATA_21
A_G_EXT_SENS_DATA_22 = 0x5F,//!< A_G_EXT_SENS_DATA_22
A_G_EXT_SENS_DATA_23 = 0x60,//!< A_G_EXT_SENS_DATA_23
A_G_MOT_DETECT_STATUS = 0x61,//!< A_G_MOT_DETECT_STATUS
A_G_I2C_SLV0_D0 = 0x63,//!< A_G_I2C_SLV0_D0
A_G_I2C_SLV1_D0 = 0x64,//!< A_G_I2C_SLV1_D0
A_G_I2C_SLV2_DO = 0x65,//!< A_G_I2C_SLV2_DO
A_G_I2C_SLV3_DO = 0x66,//!< A_G_I2C_SLV3_DO
A_G_I2C_MST_DELAY_CTRL = 0x67,//!< A_G_I2C_MST_DELAY_CTRL
A_G_SIGNAL_PATH_RESET = 0x68,//!< A_G_SIGNAL_PATH_RESET
A_G_MOT_DETECT_CTRL = 0x69,//!< A_G_MOT_DETECT_CTRL
A_G_USER_CTRL = 0x6A,//!< A_G_USER_CTRL
A_G_PWR_MGMT_1 = 0x6B,//!< A_G_PWR_MGMT_1
A_G_PWR_MGMT_2 = 0x6C,//!< A_G_PWR_MGMT_2
A_G_FIFO_COUNTH = 0x72,//!< A_G_FIFO_COUNTH
A_G_FIFO_COUNTL = 0x73,//!< A_G_FIFO_COUNTL
A_G_FIFO_R_W = 0x74,//!< A_G_FIFO_R_W
A_G_WHO_AM_I = 0x75 //!< A_G_WHO_AM_I
} mpu9150_a_g_regaddr;
/*! \typedef Accel High Pass Filter
* See Accel Config register ACCEL_HPF[2:0]
*/
typedef enum mpu9150_accel_hpf {
MPU9150_A_HPF_RESET = 0b000,//!< MPU9150_A_HPF_RESET
MPU9150_A_HPF_5HZ = 0b001,//!< MPU9150_A_HPF_5HZ
MPU9150_A_HPF_2p5HZ = 0b010,//!< MPU9150_A_HPF_2p5HZ
MPU9150_A_HPF_1p25HZ = 0b011,//!< MPU9150_A_HPF_1p25HZ
MPU9150_A_HPF_0p63HZ = 0b100,//!< MPU9150_A_HPF_0p63HZ
MPU9150_A_HPF_HOLD = 0b111 //!< MPU9150_A_HPF_HOLD
} mpu9150_accel_hpf;
/*! \typedef Accel Full Scale
* See Accel Config AFS_SEL[1:0]
*/
typedef enum mpu9150_accel_scale {
MPU9150_A_SCALE_pm2g = (0b00 << 3),//!< MPU9150_A_SCALE_pm2g
MPU9150_A_SCALE_pm4g = (0b01 << 3),//!< MPU9150_A_SCALE_pm4g
MPU9150_A_SCALE_pm8g = (0b10 << 3),//!< MPU9150_A_SCALE_pm8g
MPU9150_A_SCALE_pm16g = (0b11 << 3) //!< MPU9150_A_SCALE_pm16g
} mpu9150_accel_scale;
/*! \typedef Gyro Full Scale
*
*/
typedef enum mpu9150_gyro_scale {
MPU9150_G_SCALE_pm250 = (0b00 << 3),//!< MPU9150_G_SCALE_pm250
MPU9150_G_SCALE_pm500 = (0b01 << 3),//!< MPU9150_G_SCALE_pm500
MPU9150_G_SCALE_pm1000 = (0b10 << 3),//!< MPU9150_G_SCALE_pm1000
MPU9150_G_SCALE_pm2000 = (0b11 << 3) //!< MPU9150_G_SCALE_pm2000
} mpu9150_gyro_scale;
/*! \typedef Structure for keeping track of an MPU9150 transaction
*
*
*/
typedef struct mpu9150_driver {
i2cflags_t i2c_errors;
I2CDriver* i2c_instance; /*! which stm32f407 I2C instance to use (there are 3) */
mpu9150_i2c_data txbuf[MPU9150_MAX_TX_BUFFER]; /*! Transmit buffer */
mpu9150_i2c_data rxbuf[MPU9150_MAX_RX_BUFFER]; /*! Receive buffer */
} MPU9150_Driver;
/*! \typedef Structure for accelerometer data
*
*
*/
typedef struct mpu9150_accel_data {
uint16_t x;
uint16_t y;
uint16_t z;
} MPU9150_accel_data;
/*! \typedef Structure for gyroscope data
*
*
*/
typedef struct mpu9150_gyro_data {
uint16_t x;
uint16_t y;
uint16_t z;
} MPU9150_gyro_data;
/*! \typedef Read Data from mpu9150
*
*/
typedef struct mpu9150_read_data {
MPU9150_gyro_data gyro_xyz;
MPU9150_accel_data accel_xyz;
int16_t celsius;
} MPU9150_read_data;
/*! \typedef mpu9150_config
*
* Configuration for the MPU IMU connections
*/
typedef struct {
/*! \brief The I2C SDA port */
ioportid_t i2c_sda_port;
/*! \brief The I2C SDA pad */
uint16_t i2c_sda_pad;
/*! \brief The I2C SCL port */
ioportid_t i2c_scl_port;
/*! \brief The I2C SCL pad */
uint16_t i2c_scl_pad;
/*! \brief The INT port */
ioportid_t int_port;
/*! \brief The INT pad number. */
uint16_t int_pad;
} mpu9150_connect;
extern const I2CConfig mpu9150_config;
extern const mpu9150_connect mpu9150_connections ;
extern MPU9150_read_data mpu9150_current_read;
extern EventSource mpu9150_int_event;
extern MPU9150_Driver mpu9150_driver;
void mpu9150_start(I2CDriver* i2c) ;
void mpu9150_reset(I2CDriver* i2cptr) ;
void mpu9150_write_gyro_config(I2CDriver* i2cptr, mpu9150_reg_data d) ;
void mpu9150_write_accel_config(I2CDriver* i2cptr, mpu9150_reg_data d) ;
void mpu9150_a_g_read_id(I2CDriver* i2cptr) ;
void mpu9150_magn_read_id(I2CDriver* i2cptr);
void mpu9150_write_pm1(I2CDriver* i2cptr, mpu9150_reg_data d) ;
void mpu9150_write_pin_cfg(I2CDriver* i2cptr, mpu9150_reg_data d) ;
void mpu9150_write_int_enable(I2CDriver* i2cptr, mpu9150_reg_data d) ;
void mpu9150_a_g_read_int_status(I2CDriver* i2cptr) ;
void mpu9150_a_read_x_y_z(I2CDriver* i2cptr, MPU9150_accel_data* d) ;
void mpu9150_g_read_x_y_z(I2CDriver* i2cptr, MPU9150_gyro_data* d) ;
void mpu9150_write_gyro_sample_rate_div(I2CDriver* i2cptr, mpu9150_reg_data d) ;
int16_t mpu9150_temp_to_dC(int16_t raw_temp) ;
int16_t mpu9150_a_g_read_temperature(I2CDriver* i2cptr) ;
/*!
* @}
*/
#ifdef __cplusplus
}
#endif
#endif