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