mpu9dof  2.0.0.0
mpu9dof.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef MPU9DOF_H
36 #define MPU9DOF_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_i2c_master.h"
41 
42 // -------------------------------------------------------------- PUBLIC MACROS
52 #define MPU9DOF_MAP_MIKROBUS( cfg, mikrobus ) \
53  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
54  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
55  cfg.fsy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
56  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
57 
63 #define MPU9DOF_RETVAL uint8_t
64 
65 #define MPU9DOF_OK 0x00
66 #define MPU9DOF_INIT_ERROR 0xFF
67 
73 #define MPU9DOF_XLG_I2C_ADDR_1 0x69 //Device address jumper pos 1 (0xD2 >> 1)
74 #define MPU9DOF_XLG_I2C_ADDR_0 0x68 //Device address jumper pos 0
75 #define MPU9DOF_M_I2C_ADDR_0 0x0C // Address of the magnetometer in bypass mode
76 #define MPU9DOF_M_I2C_ADDR_1 0x0D
77 
83 #define MPU9DOF_WHO_AM_I_MAG 0x00 // should return = 0x48
84 #define MPU9DOF_INFO 0x01
85 #define MPU9DOF_MAG_ST1 0x02 // data ready status bit 0
86 #define MPU9DOF_MAG_ADDRESS 0x0C
87 #define MPU9DOF_MAG_XOUT_L 0x03 // data
88 #define MPU9DOF_MAG_XOUT_H 0x04
89 #define MPU9DOF_MAG_YOUT_L 0x05
90 #define MPU9DOF_MAG_YOUT_H 0x06
91 #define MPU9DOF_MAG_ZOUT_L 0x07
92 #define MPU9DOF_MAG_ZOUT_H 0x08
93 #define MPU9DOF_MAG_ST2 0x09 // Data overflow bit 3 and data read error status bit 2
94 #define MPU9DOF_MAG_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
95 #define MPU9DOF_MAG_ASTC 0x0C // Self test control
96 #define MPU9DOF_MAG_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
97 #define MPU9DOF_MAG_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
98 #define MPU9DOF_MAG_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
99 
100 #define MPU9DOF_XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD
101 #define MPU9DOF_YGOFFS_TC 0x01
102 #define MPU9DOF_ZGOFFS_TC 0x02
103 #define MPU9DOF_X_FINE_GAIN 0x03 // [7:0] fine gain
104 #define MPU9DOF_Y_FINE_GAIN 0x04
105 #define MPU9DOF_Z_FINE_GAIN 0x05
106 #define MPU9DOF_XA_OFFSET_H 0x06 // User-defined trim values for accelerometer
107 #define MPU9DOF_XA_OFFSET_L_TC 0x07
108 #define MPU9DOF_YA_OFFSET_H 0x08
109 #define MPU9DOF_YA_OFFSET_L_TC 0x09
110 #define MPU9DOF_ZA_OFFSET_H 0x0A
111 #define MPU9DOF_ZA_OFFSET_L_TC 0x0B
112 #define MPU9DOF_SELF_TEST_X 0x0D
113 #define MPU9DOF_SELF_TEST_Y 0x0E
114 #define MPU9DOF_SELF_TEST_Z 0x0F
115 #define MPU9DOF_SELF_TEST_A 0x10
116 #define MPU9DOF_XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope, populate with calibration routine
117 #define MPU9DOF_XG_OFFS_USRL 0x14
118 #define MPU9DOF_YG_OFFS_USRH 0x15
119 #define MPU9DOF_YG_OFFS_USRL 0x16
120 #define MPU9DOF_ZG_OFFS_USRH 0x17
121 #define MPU9DOF_ZG_OFFS_USRL 0x18
122 #define MPU9DOF_SMPLRT_DIV 0x19
123 #define MPU9DOF_CONFIG 0x1A
124 #define MPU9DOF_GYRO_CONFIG 0x1B
125 #define MPU9DOF_ACCEL_CONFIG 0x1C
126 #define MPU9DOF_FF_THR 0x1D // Free-fall
127 #define MPU9DOF_FF_DUR 0x1E // Free-fall
128 #define MPU9DOF_MOT_THR 0x1F // Motion detection threshold bits [7:0]
129 #define MPU9DOF_MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
130 #define MPU9DOF_ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
131 #define MPU9DOF_ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
132 #define MPU9DOF_FIFO_EN 0x23
133 #define MPU9DOF_I2C_MST_CTRL 0x24
134 #define MPU9DOF_I2C_SLV0_ADDR 0x25
135 #define MPU9DOF_I2C_SLV0_REG 0x26
136 #define MPU9DOF_I2C_SLV0_CTRL 0x27
137 #define MPU9DOF_I2C_SLV1_ADDR 0x28
138 #define MPU9DOF_I2C_SLV1_REG 0x29
139 #define MPU9DOF_I2C_SLV1_CTRL 0x2A
140 #define MPU9DOF_I2C_SLV2_ADDR 0x2B
141 #define MPU9DOF_I2C_SLV2_REG 0x2C
142 #define MPU9DOF_I2C_SLV2_CTRL 0x2D
143 #define MPU9DOF_I2C_SLV3_ADDR 0x2E
144 #define MPU9DOF_I2C_SLV3_REG 0x2F
145 #define MPU9DOF_I2C_SLV3_CTRL 0x30
146 #define MPU9DOF_I2C_SLV4_ADDR 0x31
147 #define MPU9DOF_I2C_SLV4_REG 0x32
148 #define MPU9DOF_I2C_SLV4_DO 0x33
149 #define MPU9DOF_I2C_SLV4_CTRL 0x34
150 #define MPU9DOF_I2C_SLV4_DI 0x35
151 #define MPU9DOF_I2C_MST_STATUS 0x36
152 #define MPU9DOF_INT_PIN_CFG 0x37
153 #define MPU9DOF_INT_ENABLE 0x38
154 #define MPU9DOF_DMP_INT_STATUS 0x39 // Check DMP interrupt
155 #define MPU9DOF_INT_STATUS 0x3A
156 #define MPU9DOF_ACCEL_XOUT_H 0x3B
157 #define MPU9DOF_ACCEL_XOUT_L 0x3C
158 #define MPU9DOF_ACCEL_YOUT_H 0x3D
159 #define MPU9DOF_ACCEL_YOUT_L 0x3E
160 #define MPU9DOF_ACCEL_ZOUT_H 0x3F
161 #define MPU9DOF_ACCEL_ZOUT_L 0x40
162 #define MPU9DOF_TEMP_OUT_H 0x41
163 #define MPU9DOF_TEMP_OUT_L 0x42
164 #define MPU9DOF_GYRO_XOUT_H 0x43
165 #define MPU9DOF_GYRO_XOUT_L 0x44
166 #define MPU9DOF_GYRO_YOUT_H 0x45
167 #define MPU9DOF_GYRO_YOUT_L 0x46
168 #define MPU9DOF_GYRO_ZOUT_H 0x47
169 #define MPU9DOF_GYRO_ZOUT_L 0x48
170 #define MPU9DOF_EXT_SENS_DATA_00 0x49
171 #define MPU9DOF_EXT_SENS_DATA_01 0x4A
172 #define MPU9DOF_EXT_SENS_DATA_02 0x4B
173 #define MPU9DOF_EXT_SENS_DATA_03 0x4C
174 #define MPU9DOF_EXT_SENS_DATA_04 0x4D
175 #define MPU9DOF_EXT_SENS_DATA_05 0x4E
176 #define MPU9DOF_EXT_SENS_DATA_06 0x4F
177 #define MPU9DOF_EXT_SENS_DATA_07 0x50
178 #define MPU9DOF_EXT_SENS_DATA_08 0x51
179 #define MPU9DOF_EXT_SENS_DATA_09 0x52
180 #define MPU9DOF_EXT_SENS_DATA_10 0x53
181 #define MPU9DOF_EXT_SENS_DATA_11 0x54
182 #define MPU9DOF_EXT_SENS_DATA_12 0x55
183 #define MPU9DOF_EXT_SENS_DATA_13 0x56
184 #define MPU9DOF_EXT_SENS_DATA_14 0x57
185 #define MPU9DOF_EXT_SENS_DATA_15 0x58
186 #define MPU9DOF_EXT_SENS_DATA_16 0x59
187 #define MPU9DOF_EXT_SENS_DATA_17 0x5A
188 #define MPU9DOF_EXT_SENS_DATA_18 0x5B
189 #define MPU9DOF_EXT_SENS_DATA_19 0x5C
190 #define MPU9DOF_EXT_SENS_DATA_20 0x5D
191 #define MPU9DOF_EXT_SENS_DATA_21 0x5E
192 #define MPU9DOF_EXT_SENS_DATA_22 0x5F
193 #define MPU9DOF_EXT_SENS_DATA_23 0x60
194 #define MPU9DOF_MOT_DETECT_STATUS 0x61
195 #define MPU9DOF_I2C_SLV0_DO 0x63
196 #define MPU9DOF_I2C_SLV1_DO 0x64
197 #define MPU9DOF_I2C_SLV2_DO 0x65
198 #define MPU9DOF_I2C_SLV3_DO 0x66
199 #define MPU9DOF_I2C_MST_DELAY_CTRL 0x67
200 #define MPU9DOF_SIGNAL_PATH_RESET 0x68
201 #define MPU9DOF_MOT_DETECT_CTRL 0x69
202 #define MPU9DOF_USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
203 #define MPU9DOF_PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
204 #define MPU9DOF_PWR_MGMT_2 0x6C
205 #define MPU9DOF_DMP_BANK 0x6D // Activates a specific bank in the DMP
206 #define MPU9DOF_DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
207 #define MPU9DOF_DMP_REG 0x6F // Register in DMP from which to read or to which to write
208 #define MPU9DOF_DMP_REG_1 0x70
209 #define MPU9DOF_DMP_REG_2 0x71
210 #define MPU9DOF_FIFO_COUNTH 0x72
211 #define MPU9DOF_FIFO_COUNTL 0x73
212 #define MPU9DOF_FIFO_R_W 0x74
213 #define MPU9DOF_WHO_AM_I_XLG 0x75 // Should return = 0x68
214 
220 #define MPU9DOF_BIT_SLEEP 0x40
221 #define MPU9DOF_BIT_H_RESET 0x80
222 #define MPU9DOF_BITS_CLKSEL 0x07
223 #define MPU9DOF_MPU_CLK_SEL_PLLGYROX 0x01
224 #define MPU9DOF_MPU_CLK_SEL_PLLGYROZ 0x03
225 #define MPU9DOF_MPU_EXT_SYNC_GYROX 0x02
226 #define MPU9DOF_BITS_AFSL_SEL_2G 0x00
227 #define MPU9DOF_BITS_AFSL_SEL_4G 0x08
228 #define MPU9DOF_BITS_AFSL_SEL_8G 0x10
229 #define MPU9DOF_BITS_AFSL_SEL_16G 0x18
230 #define MPU9DOF_BITS_FS_250DPS 0x00
231 #define MPU9DOF_BITS_FS_500DPS 0x08
232 #define MPU9DOF_BITS_FS_1000DPS 0x10
233 #define MPU9DOF_BITS_FS_2000DPS 0x18
234 #define MPU9DOF_BITS_FS_MASK 0x18
235 #define MPU9DOF_BITS_DLPF_CFG_256HZ_NOLPF2 0x00
236 #define MPU9DOF_BITS_DLPF_CFG_188HZ 0x01
237 #define MPU9DOF_BITS_DLPF_CFG_98HZ 0x02
238 #define MPU9DOF_BITS_DLPF_CFG_42HZ 0x03
239 #define MPU9DOF_BITS_DLPF_CFG_20HZ 0x04
240 #define MPU9DOF_BITS_DLPF_CFG_10HZ 0x05
241 #define MPU9DOF_BITS_DLPF_CFG_5HZ 0x06
242 #define MPU9DOF_BITS_DLPF_CFG_2100HZ_NOLPF 0x07
243 #define MPU9DOF_BITS_DLPF_CFG_MASK 0x07
244 #define MPU9DOF_BIT_INT_ANYRD_2CLEAR 0x10
245 #define MPU9DOF_BIT_RAW_RDY_EN 0x01
246 #define MPU9DOF_BIT_I2C_IF_DIS 0x10
247 #define MPU9DOF_BIT_INT_PIN_CFG 0x02
248 #define MPU9DOF_BIT_FIFO_EN 0x78
249 #define MPU9DOF_BIT_FIFO_DIS 0x00
250 #define MPU9DOF_DEFAULT 0x00
251  // End group macro
254 // --------------------------------------------------------------- PUBLIC TYPES
263 typedef struct
264 {
265  // Output pins
266 
267  digital_out_t fsy;
268 
269  // Input pins
270 
271  digital_in_t int_pin;
272 
273  // Modules
274 
275  i2c_master_t i2c;
276 
277  // ctx variable
278 
279  uint8_t slave_address;
281 
282 } mpu9dof_t;
283 
287 typedef struct
288 {
289  // Communication gpio pins
290 
291  pin_name_t scl;
292  pin_name_t sda;
293 
294  // Additional gpio pins
295 
296  pin_name_t fsy;
297  pin_name_t int_pin;
298 
299  // static variable
300 
301  uint32_t i2c_speed;
302  uint8_t i2c_address;
304 
305 } mpu9dof_cfg_t;
306  // End types group
308 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
309 
315 #ifdef __cplusplus
316 extern "C"{
317 #endif
318 
328 
337 
346 
357 void mpu9dof_generic_write ( mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
358 
370 void mpu9dof_generic_read ( mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
371 
381 void mpu9dof_write_data_mag ( mpu9dof_t *ctx, uint8_t address, uint8_t write_command );
382 
393 uint8_t mpu9dof_read_data_mag ( mpu9dof_t *ctx, uint8_t address );
394 
405 int16_t mpu9dof_get_axis ( mpu9dof_t *ctx, uint8_t adr_reg_high );
406 
417 int16_t mpu9dof_get_axis_mag ( mpu9dof_t *ctx, uint8_t adr_reg_low );
418 
429 void mpu9dof_read_gyro ( mpu9dof_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z );
430 
441 void mpu9dof_read_accel ( mpu9dof_t *ctx, int16_t *accel_x, int16_t *accel_y, int16_t *accel_z );
442 
453 void mpu9dof_read_mag ( mpu9dof_t *ctx, int16_t *mag_x, int16_t *mag_y, int16_t *mag_z );
454 
465 
475 uint8_t get_int_pin_state ( mpu9dof_t *ctx );
476 
485 
494 
495 #ifdef __cplusplus
496 }
497 #endif
498 #endif // _MPU9DOF_H_
499  // End public_function group
502 
503 // ------------------------------------------------------------------------- END
mpu9dof_t::slave_address
uint8_t slave_address
Definition: mpu9dof.h:279
mpu9dof_cfg_setup
void mpu9dof_cfg_setup(mpu9dof_cfg_t *cfg)
Config Object Initialization function.
mpu9dof_cfg_t::int_pin
pin_name_t int_pin
Definition: mpu9dof.h:297
mpu9dof_generic_read
void mpu9dof_generic_read(mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
mpu9dof_cfg_t::sda
pin_name_t sda
Definition: mpu9dof.h:292
mpu9dof_get_axis_mag
int16_t mpu9dof_get_axis_mag(mpu9dof_t *ctx, uint8_t adr_reg_low)
Function get low and high register data.
mpu9dof_write_data_mag
void mpu9dof_write_data_mag(mpu9dof_t *ctx, uint8_t address, uint8_t write_command)
Generic write data function.
mpu9dof_cfg_t
Click configuration structure definition.
Definition: mpu9dof.h:288
mpu9dof_cfg_t::scl
pin_name_t scl
Definition: mpu9dof.h:291
adapter_turn_fsy_on
void adapter_turn_fsy_on(mpu9dof_t *ctx)
Turn FSY on function.
mpu9dof_cfg_t::fsy
pin_name_t fsy
Definition: mpu9dof.h:296
mpu9dof_default_cfg
void mpu9dof_default_cfg(mpu9dof_t *ctx)
Click Default Configuration function.
get_int_pin_state
uint8_t get_int_pin_state(mpu9dof_t *ctx)
Get int pin state.
mpu9dof_generic_write
void mpu9dof_generic_write(mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
mpu9dof_read_temperature
float mpu9dof_read_temperature(mpu9dof_t *ctx)
Function read temperature data in degrees [ �C ].
mpu9dof_read_accel
void mpu9dof_read_accel(mpu9dof_t *ctx, int16_t *accel_x, int16_t *accel_y, int16_t *accel_z)
Function read axis.
mpu9dof_t::i2c
i2c_master_t i2c
Definition: mpu9dof.h:275
adapter_turn_fsy_off
void adapter_turn_fsy_off(mpu9dof_t *ctx)
Turn FSY off function.
mpu9dof_cfg_t::i2c_mag_address
uint8_t i2c_mag_address
Definition: mpu9dof.h:303
mpu9dof_get_axis
int16_t mpu9dof_get_axis(mpu9dof_t *ctx, uint8_t adr_reg_high)
Function get low and high register data.
mpu9dof_read_mag
void mpu9dof_read_mag(mpu9dof_t *ctx, int16_t *mag_x, int16_t *mag_y, int16_t *mag_z)
Function read axis.
mpu9dof_t::int_pin
digital_in_t int_pin
Definition: mpu9dof.h:271
mpu9dof_read_gyro
void mpu9dof_read_gyro(mpu9dof_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z)
Function read axis.
mpu9dof_read_data_mag
uint8_t mpu9dof_read_data_mag(mpu9dof_t *ctx, uint8_t address)
Generic read data function.
mpu9dof_cfg_t::i2c_address
uint8_t i2c_address
Definition: mpu9dof.h:302
mpu9dof_t::magnetometer_address
uint8_t magnetometer_address
Definition: mpu9dof.h:280
mpu9dof_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: mpu9dof.h:301
MPU9DOF_RETVAL
#define MPU9DOF_RETVAL
Definition: mpu9dof.h:63
mpu9dof_t::fsy
digital_out_t fsy
Definition: mpu9dof.h:267
mpu9dof_t
Click ctx object definition.
Definition: mpu9dof.h:264
mpu9dof_init
MPU9DOF_RETVAL mpu9dof_init(mpu9dof_t *ctx, mpu9dof_cfg_t *cfg)
Initialization function.