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 "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define MPU9DOF_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.fsy = MIKROBUS( mikrobus, MIKROBUS_RST ); \
66  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
67 
73 #define MPU9DOF_RETVAL uint8_t
74 
75 #define MPU9DOF_OK 0x00
76 #define MPU9DOF_INIT_ERROR 0xFF
77 
83 #define MPU9DOF_XLG_I2C_ADDR_1 0x69 //Device address jumper pos 1 (0xD2 >> 1)
84 #define MPU9DOF_XLG_I2C_ADDR_0 0x68 //Device address jumper pos 0
85 #define MPU9DOF_M_I2C_ADDR_0 0x0C // Address of the magnetometer in bypass mode
86 #define MPU9DOF_M_I2C_ADDR_1 0x0D
87 
93 #define MPU9DOF_WHO_AM_I_MAG 0x00 // should return = 0x48
94 #define MPU9DOF_INFO 0x01
95 #define MPU9DOF_MAG_ST1 0x02 // data ready status bit 0
96 #define MPU9DOF_MAG_ADDRESS 0x0C
97 #define MPU9DOF_MAG_XOUT_L 0x03 // data
98 #define MPU9DOF_MAG_XOUT_H 0x04
99 #define MPU9DOF_MAG_YOUT_L 0x05
100 #define MPU9DOF_MAG_YOUT_H 0x06
101 #define MPU9DOF_MAG_ZOUT_L 0x07
102 #define MPU9DOF_MAG_ZOUT_H 0x08
103 #define MPU9DOF_MAG_ST2 0x09 // Data overflow bit 3 and data read error status bit 2
104 #define MPU9DOF_MAG_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
105 #define MPU9DOF_MAG_ASTC 0x0C // Self test control
106 #define MPU9DOF_MAG_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value
107 #define MPU9DOF_MAG_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value
108 #define MPU9DOF_MAG_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value
109 
110 #define MPU9DOF_XGOFFS_TC 0x00 // Bit 7 PWR_MODE, bits 6:1 XG_OFFS_TC, bit 0 OTP_BNK_VLD
111 #define MPU9DOF_YGOFFS_TC 0x01
112 #define MPU9DOF_ZGOFFS_TC 0x02
113 #define MPU9DOF_X_FINE_GAIN 0x03 // [7:0] fine gain
114 #define MPU9DOF_Y_FINE_GAIN 0x04
115 #define MPU9DOF_Z_FINE_GAIN 0x05
116 #define MPU9DOF_XA_OFFSET_H 0x06 // User-defined trim values for accelerometer
117 #define MPU9DOF_XA_OFFSET_L_TC 0x07
118 #define MPU9DOF_YA_OFFSET_H 0x08
119 #define MPU9DOF_YA_OFFSET_L_TC 0x09
120 #define MPU9DOF_ZA_OFFSET_H 0x0A
121 #define MPU9DOF_ZA_OFFSET_L_TC 0x0B
122 #define MPU9DOF_SELF_TEST_X 0x0D
123 #define MPU9DOF_SELF_TEST_Y 0x0E
124 #define MPU9DOF_SELF_TEST_Z 0x0F
125 #define MPU9DOF_SELF_TEST_A 0x10
126 #define MPU9DOF_XG_OFFS_USRH 0x13 // User-defined trim values for gyroscope, populate with calibration routine
127 #define MPU9DOF_XG_OFFS_USRL 0x14
128 #define MPU9DOF_YG_OFFS_USRH 0x15
129 #define MPU9DOF_YG_OFFS_USRL 0x16
130 #define MPU9DOF_ZG_OFFS_USRH 0x17
131 #define MPU9DOF_ZG_OFFS_USRL 0x18
132 #define MPU9DOF_SMPLRT_DIV 0x19
133 #define MPU9DOF_CONFIG 0x1A
134 #define MPU9DOF_GYRO_CONFIG 0x1B
135 #define MPU9DOF_ACCEL_CONFIG 0x1C
136 #define MPU9DOF_FF_THR 0x1D // Free-fall
137 #define MPU9DOF_FF_DUR 0x1E // Free-fall
138 #define MPU9DOF_MOT_THR 0x1F // Motion detection threshold bits [7:0]
139 #define MPU9DOF_MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms
140 #define MPU9DOF_ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0]
141 #define MPU9DOF_ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms
142 #define MPU9DOF_FIFO_EN 0x23
143 #define MPU9DOF_I2C_MST_CTRL 0x24
144 #define MPU9DOF_I2C_SLV0_ADDR 0x25
145 #define MPU9DOF_I2C_SLV0_REG 0x26
146 #define MPU9DOF_I2C_SLV0_CTRL 0x27
147 #define MPU9DOF_I2C_SLV1_ADDR 0x28
148 #define MPU9DOF_I2C_SLV1_REG 0x29
149 #define MPU9DOF_I2C_SLV1_CTRL 0x2A
150 #define MPU9DOF_I2C_SLV2_ADDR 0x2B
151 #define MPU9DOF_I2C_SLV2_REG 0x2C
152 #define MPU9DOF_I2C_SLV2_CTRL 0x2D
153 #define MPU9DOF_I2C_SLV3_ADDR 0x2E
154 #define MPU9DOF_I2C_SLV3_REG 0x2F
155 #define MPU9DOF_I2C_SLV3_CTRL 0x30
156 #define MPU9DOF_I2C_SLV4_ADDR 0x31
157 #define MPU9DOF_I2C_SLV4_REG 0x32
158 #define MPU9DOF_I2C_SLV4_DO 0x33
159 #define MPU9DOF_I2C_SLV4_CTRL 0x34
160 #define MPU9DOF_I2C_SLV4_DI 0x35
161 #define MPU9DOF_I2C_MST_STATUS 0x36
162 #define MPU9DOF_INT_PIN_CFG 0x37
163 #define MPU9DOF_INT_ENABLE 0x38
164 #define MPU9DOF_DMP_INT_STATUS 0x39 // Check DMP interrupt
165 #define MPU9DOF_INT_STATUS 0x3A
166 #define MPU9DOF_ACCEL_XOUT_H 0x3B
167 #define MPU9DOF_ACCEL_XOUT_L 0x3C
168 #define MPU9DOF_ACCEL_YOUT_H 0x3D
169 #define MPU9DOF_ACCEL_YOUT_L 0x3E
170 #define MPU9DOF_ACCEL_ZOUT_H 0x3F
171 #define MPU9DOF_ACCEL_ZOUT_L 0x40
172 #define MPU9DOF_TEMP_OUT_H 0x41
173 #define MPU9DOF_TEMP_OUT_L 0x42
174 #define MPU9DOF_GYRO_XOUT_H 0x43
175 #define MPU9DOF_GYRO_XOUT_L 0x44
176 #define MPU9DOF_GYRO_YOUT_H 0x45
177 #define MPU9DOF_GYRO_YOUT_L 0x46
178 #define MPU9DOF_GYRO_ZOUT_H 0x47
179 #define MPU9DOF_GYRO_ZOUT_L 0x48
180 #define MPU9DOF_EXT_SENS_DATA_00 0x49
181 #define MPU9DOF_EXT_SENS_DATA_01 0x4A
182 #define MPU9DOF_EXT_SENS_DATA_02 0x4B
183 #define MPU9DOF_EXT_SENS_DATA_03 0x4C
184 #define MPU9DOF_EXT_SENS_DATA_04 0x4D
185 #define MPU9DOF_EXT_SENS_DATA_05 0x4E
186 #define MPU9DOF_EXT_SENS_DATA_06 0x4F
187 #define MPU9DOF_EXT_SENS_DATA_07 0x50
188 #define MPU9DOF_EXT_SENS_DATA_08 0x51
189 #define MPU9DOF_EXT_SENS_DATA_09 0x52
190 #define MPU9DOF_EXT_SENS_DATA_10 0x53
191 #define MPU9DOF_EXT_SENS_DATA_11 0x54
192 #define MPU9DOF_EXT_SENS_DATA_12 0x55
193 #define MPU9DOF_EXT_SENS_DATA_13 0x56
194 #define MPU9DOF_EXT_SENS_DATA_14 0x57
195 #define MPU9DOF_EXT_SENS_DATA_15 0x58
196 #define MPU9DOF_EXT_SENS_DATA_16 0x59
197 #define MPU9DOF_EXT_SENS_DATA_17 0x5A
198 #define MPU9DOF_EXT_SENS_DATA_18 0x5B
199 #define MPU9DOF_EXT_SENS_DATA_19 0x5C
200 #define MPU9DOF_EXT_SENS_DATA_20 0x5D
201 #define MPU9DOF_EXT_SENS_DATA_21 0x5E
202 #define MPU9DOF_EXT_SENS_DATA_22 0x5F
203 #define MPU9DOF_EXT_SENS_DATA_23 0x60
204 #define MPU9DOF_MOT_DETECT_STATUS 0x61
205 #define MPU9DOF_I2C_SLV0_DO 0x63
206 #define MPU9DOF_I2C_SLV1_DO 0x64
207 #define MPU9DOF_I2C_SLV2_DO 0x65
208 #define MPU9DOF_I2C_SLV3_DO 0x66
209 #define MPU9DOF_I2C_MST_DELAY_CTRL 0x67
210 #define MPU9DOF_SIGNAL_PATH_RESET 0x68
211 #define MPU9DOF_MOT_DETECT_CTRL 0x69
212 #define MPU9DOF_USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP
213 #define MPU9DOF_PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode
214 #define MPU9DOF_PWR_MGMT_2 0x6C
215 #define MPU9DOF_DMP_BANK 0x6D // Activates a specific bank in the DMP
216 #define MPU9DOF_DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank
217 #define MPU9DOF_DMP_REG 0x6F // Register in DMP from which to read or to which to write
218 #define MPU9DOF_DMP_REG_1 0x70
219 #define MPU9DOF_DMP_REG_2 0x71
220 #define MPU9DOF_FIFO_COUNTH 0x72
221 #define MPU9DOF_FIFO_COUNTL 0x73
222 #define MPU9DOF_FIFO_R_W 0x74
223 #define MPU9DOF_WHO_AM_I_XLG 0x75 // Should return = 0x68
224 
230 #define MPU9DOF_BIT_SLEEP 0x40
231 #define MPU9DOF_BIT_H_RESET 0x80
232 #define MPU9DOF_BITS_CLKSEL 0x07
233 #define MPU9DOF_MPU_CLK_SEL_PLLGYROX 0x01
234 #define MPU9DOF_MPU_CLK_SEL_PLLGYROZ 0x03
235 #define MPU9DOF_MPU_EXT_SYNC_GYROX 0x02
236 #define MPU9DOF_BITS_AFSL_SEL_2G 0x00
237 #define MPU9DOF_BITS_AFSL_SEL_4G 0x08
238 #define MPU9DOF_BITS_AFSL_SEL_8G 0x10
239 #define MPU9DOF_BITS_AFSL_SEL_16G 0x18
240 #define MPU9DOF_BITS_FS_250DPS 0x00
241 #define MPU9DOF_BITS_FS_500DPS 0x08
242 #define MPU9DOF_BITS_FS_1000DPS 0x10
243 #define MPU9DOF_BITS_FS_2000DPS 0x18
244 #define MPU9DOF_BITS_FS_MASK 0x18
245 #define MPU9DOF_BITS_DLPF_CFG_256HZ_NOLPF2 0x00
246 #define MPU9DOF_BITS_DLPF_CFG_188HZ 0x01
247 #define MPU9DOF_BITS_DLPF_CFG_98HZ 0x02
248 #define MPU9DOF_BITS_DLPF_CFG_42HZ 0x03
249 #define MPU9DOF_BITS_DLPF_CFG_20HZ 0x04
250 #define MPU9DOF_BITS_DLPF_CFG_10HZ 0x05
251 #define MPU9DOF_BITS_DLPF_CFG_5HZ 0x06
252 #define MPU9DOF_BITS_DLPF_CFG_2100HZ_NOLPF 0x07
253 #define MPU9DOF_BITS_DLPF_CFG_MASK 0x07
254 #define MPU9DOF_BIT_INT_ANYRD_2CLEAR 0x10
255 #define MPU9DOF_BIT_RAW_RDY_EN 0x01
256 #define MPU9DOF_BIT_I2C_IF_DIS 0x10
257 #define MPU9DOF_BIT_INT_PIN_CFG 0x02
258 #define MPU9DOF_BIT_FIFO_EN 0x78
259 #define MPU9DOF_BIT_FIFO_DIS 0x00
260 #define MPU9DOF_DEFAULT 0x00
261  // End group macro
264 // --------------------------------------------------------------- PUBLIC TYPES
273 typedef struct
274 {
275  // Output pins
276 
277  digital_out_t fsy;
278 
279  // Input pins
280 
281  digital_in_t int_pin;
282 
283  // Modules
284 
285  i2c_master_t i2c;
286 
287  // ctx variable
288 
289  uint8_t slave_address;
291 
292 } mpu9dof_t;
293 
297 typedef struct
298 {
299  // Communication gpio pins
300 
301  pin_name_t scl;
302  pin_name_t sda;
303 
304  // Additional gpio pins
305 
306  pin_name_t fsy;
307  pin_name_t int_pin;
308 
309  // static variable
310 
311  uint32_t i2c_speed;
312  uint8_t i2c_address;
314 
315 } mpu9dof_cfg_t;
316  // End types group
318 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
319 
325 #ifdef __cplusplus
326 extern "C"{
327 #endif
328 
338 
347 
356 
367 void mpu9dof_generic_write ( mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
368 
380 void mpu9dof_generic_read ( mpu9dof_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
381 
391 void mpu9dof_write_data_mag ( mpu9dof_t *ctx, uint8_t address, uint8_t write_command );
392 
403 uint8_t mpu9dof_read_data_mag ( mpu9dof_t *ctx, uint8_t address );
404 
415 int16_t mpu9dof_get_axis ( mpu9dof_t *ctx, uint8_t adr_reg_high );
416 
427 int16_t mpu9dof_get_axis_mag ( mpu9dof_t *ctx, uint8_t adr_reg_low );
428 
439 void mpu9dof_read_gyro ( mpu9dof_t *ctx, int16_t *gyro_x, int16_t *gyro_y, int16_t *gyro_z );
440 
451 void mpu9dof_read_accel ( mpu9dof_t *ctx, int16_t *accel_x, int16_t *accel_y, int16_t *accel_z );
452 
463 void mpu9dof_read_mag ( mpu9dof_t *ctx, int16_t *mag_x, int16_t *mag_y, int16_t *mag_z );
464 
475 
485 uint8_t get_int_pin_state ( mpu9dof_t *ctx );
486 
495 
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 #endif // _MPU9DOF_H_
509  // End public_function group
512 
513 // ------------------------------------------------------------------------- END
mpu9dof_t::slave_address
uint8_t slave_address
Definition: mpu9dof.h:289
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:307
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:302
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:298
mpu9dof_cfg_t::scl
pin_name_t scl
Definition: mpu9dof.h:301
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:306
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:285
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:313
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:281
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:312
mpu9dof_t::magnetometer_address
uint8_t magnetometer_address
Definition: mpu9dof.h:290
mpu9dof_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: mpu9dof.h:311
MPU9DOF_RETVAL
#define MPU9DOF_RETVAL
Definition: mpu9dof.h:73
mpu9dof_t::fsy
digital_out_t fsy
Definition: mpu9dof.h:277
mpu9dof_t
Click ctx object definition.
Definition: mpu9dof.h:274
mpu9dof_init
MPU9DOF_RETVAL mpu9dof_init(mpu9dof_t *ctx, mpu9dof_cfg_t *cfg)
Initialization function.