LibDriver MPU9250
Loading...
Searching...
No Matches
driver_mpu9250.c
Go to the documentation of this file.
1
36
37#include "driver_mpu9250.h"
38#include "driver_mpu9250_code.h"
39#include <math.h>
40
44#define CHIP_NAME "TDK MPU9250"
45#define MANUFACTURER_NAME "TDK"
46#define SUPPLY_VOLTAGE_MIN 2.4f
47#define SUPPLY_VOLTAGE_MAX 3.6f
48#define MAX_CURRENT 3.7f
49#define TEMPERATURE_MIN -40.0f
50#define TEMPERATURE_MAX 85.0f
51#define DRIVER_VERSION 1000
52
56#define AK8963_IIC_ADDRESS 0x0C
57
61#define MPU9250_REG_SELF_TEST_X_GYRO 0x00
62#define MPU9250_REG_SELF_TEST_Y_GYRO 0x01
63#define MPU9250_REG_SELF_TEST_Z_GYRO 0x02
64#define MPU9250_REG_SELF_TEST_X_ACCEL 0x0D
65#define MPU9250_REG_SELF_TEST_Y_ACCEL 0x0E
66#define MPU9250_REG_SELF_TEST_Z_ACCEL 0x0F
67#define MPU9250_REG_XG_OFFSET_H 0x13
68#define MPU9250_REG_XG_OFFSET_L 0x14
69#define MPU9250_REG_YG_OFFSET_H 0x15
70#define MPU9250_REG_YG_OFFSET_L 0x16
71#define MPU9250_REG_ZG_OFFSET_H 0x17
72#define MPU9250_REG_ZG_OFFSET_L 0x18
73#define MPU9250_REG_SMPRT_DIV 0x19
74#define MPU9250_REG_CONFIG 0x1A
75#define MPU9250_REG_GYRO_CONFIG 0x1B
76#define MPU9250_REG_ACCEL_CONFIG 0x1C
77#define MPU9250_REG_ACCEL_CONFIG2 0x1D
78#define MPU9250_REG_LP_ACCEL_ODR 0x1E
79#define MPU9250_REG_WOM_THR 0x1F
80#define MPU9250_REG_MOTION_DURATION 0x20
81#define MPU9250_REG_FIFO_EN 0x23
82#define MPU9250_REG_I2C_MST_CTRL 0x24
83#define MPU9250_REG_I2C_MST_STATUS 0x36
84#define MPU9250_REG_I2C_MST_DELAY_CTRL 0x67
85#define MPU9250_REG_I2C_SLV0_ADDR 0x25
86#define MPU9250_REG_I2C_SLV0_REG 0x26
87#define MPU9250_REG_I2C_SLV0_CTRL 0x27
88#define MPU9250_REG_I2C_SLV0_DO 0x63
89#define MPU9250_REG_I2C_SLV1_ADDR 0x28
90#define MPU9250_REG_I2C_SLV1_REG 0x29
91#define MPU9250_REG_I2C_SLV1_CTRL 0x2A
92#define MPU9250_REG_I2C_SLV1_DO 0x64
93#define MPU9250_REG_I2C_SLV2_ADDR 0x2B
94#define MPU9250_REG_I2C_SLV2_REG 0x2C
95#define MPU9250_REG_I2C_SLV2_CTRL 0x2D
96#define MPU9250_REG_I2C_SLV2_DO 0x65
97#define MPU9250_REG_I2C_SLV3_ADDR 0x2E
98#define MPU9250_REG_I2C_SLV3_REG 0x2F
99#define MPU9250_REG_I2C_SLV3_CTRL 0x30
100#define MPU9250_REG_I2C_SLV3_DO 0x66
101#define MPU9250_REG_I2C_SLV4_ADDR 0x31
102#define MPU9250_REG_I2C_SLV4_REG 0x32
103#define MPU9250_REG_I2C_SLV4_CTRL 0x34
104#define MPU9250_REG_I2C_SLV4_DO 0x33
105#define MPU9250_REG_I2C_SLV4_DI 0x35
106#define MPU9250_REG_EXT_SENS_DATA_00 0x49
107#define MPU9250_REG_INT_PIN_CFG 0x37
108#define MPU9250_REG_INT_ENABLE 0x38
109#define MPU9250_REG_INT_STATUS 0x3A
110#define MPU9250_REG_ACCEL_XOUT_H 0x3B
111#define MPU9250_REG_ACCEL_XOUT_L 0x3C
112#define MPU9250_REG_ACCEL_YOUT_H 0x3D
113#define MPU9250_REG_ACCEL_YOUT_L 0x3E
114#define MPU9250_REG_ACCEL_ZOUT_H 0x3F
115#define MPU9250_REG_ACCEL_ZOUT_L 0x40
116#define MPU9250_REG_TEMP_OUT_H 0x41
117#define MPU9250_REG_TEMP_OUT_L 0x42
118#define MPU9250_REG_GYRO_XOUT_H 0x43
119#define MPU9250_REG_GYRO_XOUT_L 0x44
120#define MPU9250_REG_GYRO_YOUT_H 0x45
121#define MPU9250_REG_GYRO_YOUT_L 0x46
122#define MPU9250_REG_GYRO_ZOUT_H 0x47
123#define MPU9250_REG_GYRO_ZOUT_L 0x48
124#define MPU9250_REG_SIGNAL_PATH_RESET 0x68
125#define MPU9250_REG_MOT_DETECT_CTRL 0x69
126#define MPU9250_REG_USER_CTRL 0x6A
127#define MPU9250_REG_PWR_MGMT_1 0x6B
128#define MPU9250_REG_PWR_MGMT_2 0x6C
129#define MPU9250_REG_BANK_SEL 0x6D
130#define MPU9250_REG_MEM 0x6F
131#define MPU9250_REG_PROGRAM_START 0x70
132#define MPU9250_REG_FIFO_COUNTH 0x72
133#define MPU9250_REG_FIFO_COUNTL 0x73
134#define MPU9250_REG_R_W 0x74
135#define MPU9250_REG_WHO_AM_I 0x75
136#define MPU9250_REG_XA_OFFSET_H 0x77
137#define MPU9250_REG_XA_OFFSET_L 0x78
138#define MPU9250_REG_YA_OFFSET_H 0x7A
139#define MPU9250_REG_YA_OFFSET_L 0x7B
140#define MPU9250_REG_ZA_OFFSET_H 0x7D
141#define MPU9250_REG_ZA_OFFSET_L 0x7E
142#define AK8963_REG_WIA 0x00
143#define AK8963_REG_INFO 0x01
144#define AK8963_REG_ST1 0x02
145#define AK8963_REG_HXL 0x03
146#define AK8963_REG_HXH 0x04
147#define AK8963_REG_HYL 0x05
148#define AK8963_REG_HYH 0x06
149#define AK8963_REG_HZL 0x07
150#define AK8963_REG_HZH 0x08
151#define AK8963_REG_ST2 0x09
152#define AK8963_REG_CNTL1 0x0A
153#define AK8963_REG_CNTL2 0x0B
154#define AK8963_REG_ASTC 0x0C
155#define AK8963_REG_TS1 0x0D
156#define AK8963_REG_TS2 0x0E
157#define AK8963_REG_I2CDIS 0x0F
158#define AK8963_REG_ASAX 0x10
159#define AK8963_REG_ASAY 0x11
160#define AK8963_REG_ASAZ 0x12
161
165#define MPU9250_DMP_SAMPLE_RATE 200
166#define MPU9250_DMP_GYRO_SF (46850825LL * 200 / MPU9250_DMP_SAMPLE_RATE)
167#define MPU9250_DMP_D_PEDSTD_TIMECTR 964
168#define MPU9250_DMP_D_PEDSTD_STEPCTR (768 + 0x60)
169#define MPU9250_DMP_D_1_36 (256 + 36)
170#define MPU9250_DMP_D_1_40 (256 + 40)
171#define MPU9250_DMP_D_1_44 (256 + 44)
172#define MPU9250_DMP_D_1_72 (256 + 72)
173#define MPU9250_DMP_D_1_79 (256 + 79)
174#define MPU9250_DMP_D_1_88 (256 + 88)
175#define MPU9250_DMP_D_1_90 (256 + 90)
176#define MPU9250_DMP_D_1_92 (256 + 92)
177#define MPU9250_DMP_D_1_218 (256 + 218)
178#define MPU9250_DMP_D_0_22 (512 + 22)
179#define MPU9250_DMP_D_0_104 104
180#define MPU9250_DMP_TAPW_MIN 478
181#define MPU9250_DMP_TAP_THX 468
182#define MPU9250_DMP_TAP_THY 472
183#define MPU9250_DMP_TAP_THZ 476
184#define MPU9250_DMP_CFG_6 2753
185#define MPU9250_DMP_CFG_8 2718
186#define MPU9250_DMP_CFG_MOTION_BIAS 1208
187#define MPU9250_DMP_CFG_LP_QUAT 2712
188#define MPU9250_DMP_CFG_FIFO_ON_EVENT 2690
189#define MPU9250_DMP_FCFG_1 1062
190#define MPU9250_DMP_FCFG_2 1066
191#define MPU9250_DMP_FCFG_3 1088
192#define MPU9250_DMP_FCFG_7 1073
193#define MPU9250_DMP_D_EXT_GYRO_BIAS_X (61 * 16)
194#define MPU9250_DMP_D_EXT_GYRO_BIAS_Y (61 * 16 + 4)
195#define MPU9250_DMP_D_EXT_GYRO_BIAS_Z (61 * 16 + 8)
196#define MPU9250_DMP_D_ACCEL_BIAS 660
197#define MPU9250_DMP_FEATURE_SEND_ANY_GYRO (MPU9250_DMP_FEATURE_SEND_RAW_GYRO | \
198 MPU9250_DMP_FEATURE_SEND_CAL_GYRO)
199#define MPU9250_DMP_CFG_15 2727
200#define MPU9250_DMP_CFG_27 2742
201#define MPU9250_DMP_CFG_GYRO_RAW_DATA 2722
202#define MPU9250_DMP_CFG_20 2224
203#define MPU9250_DMP_CFG_ORIENT_INT 1853
204#define MPU9250_DMP_QUAT_ERROR_THRESH (1L << 24)
205#define MPU9250_DMP_QUAT_MAG_SQ_NORMALIZED (1L << 28)
206#define MPU9250_DMP_QUAT_MAG_SQ_MIN (MPU9250_DMP_QUAT_MAG_SQ_NORMALIZED - \
207 MPU9250_DMP_QUAT_ERROR_THRESH)
208#define MPU9250_DMP_QUAT_MAG_SQ_MAX (MPU9250_DMP_QUAT_MAG_SQ_NORMALIZED + \
209 MPU9250_DMP_QUAT_ERROR_THRESH)
210#define MPU9250_DMP_INT_SRC_TAP 0x01
211#define MPU9250_DMP_INT_SRC_ORIENT 0x08
212#define MPU9250_DMP_TAP_THRESH 250
213#define MPU9250_DMP_TAP_MIN_TAP_COUNT 1
214#define MPU9250_DMP_TAP_TIME 100
215#define MPU9250_DMP_TAP_TIME_MULTI 200
216#define MPU9250_DMP_SHAKE_REJECT_THRESH 200
217#define MPU9250_DMP_SHAKE_REJECT_TIME 40
218#define MPU9250_DMP_SHAKE_REJECT_TIMEOUT 10
219
223#define MIN(a, b) (((a) < (b)) ? (a) : (b))
224
228static uint16_t gs_st_tb[256] =
229{
230 2620, 2646, 2672, 2699, 2726, 2753, 2781, 2808,
231 2837, 2865, 2894, 2923, 2952, 2981, 3011, 3041,
232 3072, 3102, 3133, 3165, 3196, 3228, 3261, 3293,
233 3326, 3359, 3393, 3427, 3461, 3496, 3531, 3566,
234 3602, 3638, 3674, 3711, 3748, 3786, 3823, 3862,
235 3900, 3939, 3979, 4019, 4059, 4099, 4140, 4182,
236 4224, 4266, 4308, 4352, 4395, 4439, 4483, 4528,
237 4574, 4619, 4665, 4712, 4759, 4807, 4855, 4903,
238 4953, 5002, 5052, 5103, 5154, 5205, 5257, 5310,
239 5363, 5417, 5471, 5525, 5581, 5636, 5693, 5750,
240 5807, 5865, 5924, 5983, 6043, 6104, 6165, 6226,
241 6289, 6351, 6415, 6479, 6544, 6609, 6675, 6742,
242 6810, 6878, 6946, 7016, 7086, 7157, 7229, 7301,
243 7374, 7448, 7522, 7597, 7673, 7750, 7828, 7906,
244 7985, 8065, 8145, 8227, 8309, 8392, 8476, 8561,
245 8647, 8733, 8820, 8909, 8998, 9088, 9178, 9270,
246 9363, 9457, 9551, 9647, 9743, 9841, 9939, 10038,
247 10139, 10240, 10343, 10446, 10550, 10656, 10763, 10870,
248 10979, 11089, 11200, 11312, 11425, 11539, 11654, 11771,
249 11889, 12008, 12128, 12249, 12371, 12495, 12620, 12746,
250 12874, 13002, 13132, 13264, 13396, 13530, 13666, 13802,
251 13940, 14080, 14221, 14363, 14506, 14652, 14798, 14946,
252 15096, 15247, 15399, 15553, 15709, 15866, 16024, 16184,
253 16346, 16510, 16675, 16842, 17010, 17180, 17352, 17526,
254 17701, 17878, 18057, 18237, 18420, 18604, 18790, 18978,
255 19167, 19359, 19553, 19748, 19946, 20145, 20347, 20550,
256 20756, 20963, 21173, 21385, 21598, 21814, 22033, 22253,
257 22475, 22700, 22927, 23156, 23388, 23622, 23858, 24097,
258 24338, 24581, 24827, 25075, 25326, 25579, 25835, 26093,
259 26354, 26618, 26884, 27153, 27424, 27699, 27976, 28255,
260 28538, 28823, 29112, 29403, 29697, 29994, 30294, 30597,
261 30903, 31212, 31524, 31839, 32157, 32479, 32804, 33132,
262};
263
275static uint8_t a_mpu9250_read(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
276{
277 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
278 {
279 if (handle->iic_read(handle->iic_addr, reg, (uint8_t *)buf, len) != 0) /* read data */
280 {
281 return 1; /* return error */
282 }
283 else
284 {
285 return 0; /* success return 0 */
286 }
287 }
288 else /* spi interface */
289 {
290 if (handle->spi_read(reg | 0x80, (uint8_t *)buf, len) != 0) /* read data */
291 {
292 return 1; /* return error */
293 }
294 else
295 {
296 return 0; /* success return 0 */
297 }
298 }
299}
300
312static uint8_t a_mpu9250_write(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
313{
314 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
315 {
316 if (handle->iic_write(handle->iic_addr, reg, (uint8_t *)buf, len) != 0) /* write data */
317 {
318 return 1; /* return error */
319 }
320 else
321 {
322 return 0; /* success return 0 */
323 }
324 }
325 else /* spi interface */
326 {
327 if (handle->spi_write(reg & (~0x80), (uint8_t *)buf, len) != 0) /* write data */
328 {
329 return 1; /* return error */
330 }
331 else
332 {
333 return 0; /* success return 0 */
334 }
335 }
336}
337
348static uint8_t a_mpu9250_mag_write(mpu9250_handle_t *handle, uint8_t reg, uint8_t *data)
349{
350 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
351 {
352 if (handle->iic_write(AK8963_IIC_ADDRESS << 1, reg, (uint8_t *)data, 1) != 0) /* write data */
353 {
354 return 1; /* return error */
355 }
356 else
357 {
358 return 0; /* success return 0 */
359 }
360 }
361 else /* spi interface */
362 {
363 return 1;
364 }
365}
366
378static uint8_t a_mpu9250_mag_read(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
379{
380 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
381 {
382 if (handle->iic_read(AK8963_IIC_ADDRESS << 1, reg, (uint8_t *)buf, len) != 0) /* read data */
383 {
384 return 1; /* return error */
385 }
386 else
387 {
388 return 0; /* success return 0 */
389 }
390 }
391 else /* spi interface */
392 {
393 return 1;
394 }
395}
396
409static uint8_t a_mpu9250_write_mem(mpu9250_handle_t *handle, uint16_t addr, uint8_t *buf, uint16_t len)
410{
411 uint8_t tmp[2];
412
413 tmp[0] = (addr >> 8) & 0xFF; /* set the addr high */
414 tmp[1] = (addr >> 0) & 0xFF; /* set the addr low */
415
416 if (tmp[1] + len > 256) /* check the range */
417 {
418 return 2; /* return error */
419 }
420
421 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
422 {
423 if (handle->iic_write(handle->iic_addr, MPU9250_REG_BANK_SEL, (uint8_t *)tmp, 2) != 0) /* write data */
424 {
425 return 1; /* return error */
426 }
427 if (handle->iic_write(handle->iic_addr, MPU9250_REG_MEM, (uint8_t *)buf, len) != 0) /* write data */
428 {
429 return 1; /* return error */
430 }
431 }
432 else /* if spi interface */
433 {
434 if (handle->spi_write(MPU9250_REG_BANK_SEL & (~0x80), (uint8_t *)tmp, 2) != 0) /* write data */
435 {
436 return 1; /* return error */
437 }
438 if (handle->spi_write(MPU9250_REG_MEM & (~0x80), (uint8_t *)buf, len) != 0) /* write data */
439 {
440 return 1; /* return error */
441 }
442 }
443
444 return 0; /* success return 0 */
445}
446
459static uint8_t a_mpu9250_read_mem(mpu9250_handle_t *handle, uint16_t addr, uint8_t *buf, uint16_t len)
460{
461 uint8_t tmp[2];
462
463 tmp[0] = (addr >> 8) & 0xFF; /* set the addr high */
464 tmp[1] = (addr >> 0) & 0xFF; /* set the addr low */
465
466 if (tmp[1] + len > 256) /* check the range */
467 {
468 return 2; /* return error */
469 }
470
471 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
472 {
473 if (handle->iic_write(handle->iic_addr, MPU9250_REG_BANK_SEL, (uint8_t *)tmp, 2) != 0) /* write data */
474 {
475 return 1; /* return error */
476 }
477 if (handle->iic_read(handle->iic_addr, MPU9250_REG_MEM, (uint8_t *)buf, len) != 0) /* read data */
478 {
479 return 1; /* return error */
480 }
481 }
482 else /* if spi interface */
483 {
484 if (handle->spi_write(MPU9250_REG_BANK_SEL & (~0x80), (uint8_t *)tmp, 2) != 0) /* write data */
485 {
486 return 1; /* return error */
487 }
488 if (handle->spi_read(MPU9250_REG_MEM | 0x80, (uint8_t *)buf, len) != 0) /* read data */
489 {
490 return 1; /* return error */
491 }
492 }
493
494 return 0; /* success return 0 */
495}
496
504static uint8_t a_mpu9250_reset_fifo(mpu9250_handle_t *handle)
505{
506 uint8_t res;
507 uint8_t prev;
508 uint8_t int_enable;
509 uint8_t fifo_enable;
510 uint8_t user_ctrl;
511
512 res = a_mpu9250_read(handle, MPU9250_REG_INT_ENABLE, &int_enable, 1); /* read the int enable */
513 if (res != 0) /* check the result */
514 {
515 handle->debug_print("mpu9250: read int enable register failed.\n"); /* read int enable register failed */
516
517 return 1; /* return error */
518 }
519 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, &fifo_enable, 1); /* read the fifo enable */
520 if (res != 0) /* check the result */
521 {
522 handle->debug_print("mpu9250: read fifo enable register failed.\n"); /* read fifo enable register failed */
523
524 return 1; /* return error */
525 }
526 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, &user_ctrl, 1); /* read the user ctrl */
527 if (res != 0) /* check the result */
528 {
529 handle->debug_print("mpu9250: read user ctrl register failed.\n"); /* read user ctrl register failed */
530
531 return 1; /* return error */
532 }
533
534 prev = 0; /* set 0 */
535 res = a_mpu9250_write(handle, MPU9250_REG_INT_ENABLE, &prev, 1); /* disable all interrupt */
536 if (res != 0) /* check the result */
537 {
538 handle->debug_print("mpu9250: write int enable register failed.\n"); /* write int enable register failed */
539
540 return 1; /* return error */
541 }
542 prev = 0; /* set 0 */
543 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, &prev, 1); /* disable all fifo */
544 if (res != 0) /* check the result */
545 {
546 handle->debug_print("mpu9250: write fifo enable register failed.\n"); /* write fifo enable register failed */
547
548 return 1; /* return error */
549 }
550 user_ctrl &= ~(1 << 6); /* disable the fifo */
551 user_ctrl &= ~(1 << 7); /* disable the dmp */
552 if (handle->dmp_inited == 1) /* if we use dmp */
553 {
554 user_ctrl |= (1 << 2) | (1 << 3); /* reset the fifo and dmp */
555 }
556 else
557 {
558 user_ctrl |= 1 << 2; /* reset the fifo */
559 }
560 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, &user_ctrl, 1); /* write the user ctrl */
561 if (res != 0) /* check the result */
562 {
563 handle->debug_print("mpu9250: write user ctrl register failed.\n"); /* write user ctrl register failed */
564
565 return 1; /* return error */
566 }
567 handle->delay_ms(50); /* delay 50 ms */
568 if (handle->dmp_inited == 1) /* if we use dmp */
569 {
570 user_ctrl |= (1 << 6) | (1 << 7); /* enable fifo and dmp */
571 }
572 else
573 {
574 user_ctrl |= 1 << 6; /* enable fifo */
575 }
576 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, &user_ctrl, 1); /* write the user ctrl */
577 if (res != 0) /* check the result */
578 {
579 handle->debug_print("mpu9250: write user ctrl register failed.\n"); /* write user ctrl register failed */
580
581 return 1; /* return error */
582 }
583
584 res = a_mpu9250_write(handle, MPU9250_REG_INT_ENABLE, &int_enable, 1); /* restore the int enable */
585 if (res != 0) /* check the result */
586 {
587 handle->debug_print("mpu9250: write int enable register failed.\n"); /* write int enable register failed */
588
589 return 1; /* return error */
590 }
591 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, &fifo_enable, 1); /* restore the fifo enable */
592 if (res != 0) /* check the result */
593 {
594 handle->debug_print("mpu9250: write fifo enable register failed.\n"); /* write fifo enable register failed */
595
596 return 1; /* return error */
597 }
598
599 return 0; /* success return 0 */
600}
601
608static uint16_t a_mpu9250_inv_row_2_scale(int8_t *row)
609{
610 uint16_t b;
611
612 if (row[0] > 0) /* check row 0 */
613 {
614 b = 0; /* set 0 */
615 }
616 else if (row[0] < 0) /* check row 0 */
617 {
618 b = 4; /* set 4 */
619 }
620 else if (row[1] > 0) /* check row 1 */
621 {
622 b = 1; /* set 1 */
623 }
624 else if (row[1] < 0) /* check row 1 */
625 {
626 b = 5; /* set 5 */
627 }
628 else if (row[2] > 0) /* check row 2 */
629 {
630 b = 2; /* set 2 */
631 }
632 else if (row[2] < 0) /* check row 2 */
633 {
634 b = 6; /* set 6 */
635 }
636 else
637 {
638 b = 7; /* set 7 */
639 }
640
641 return b; /* return scale */
642}
643
650static uint16_t a_mpu9250_inv_orientation_matrix_to_scalar(int8_t *mtx)
651{
652 uint16_t scalar;
653
654 scalar = a_mpu9250_inv_row_2_scale(mtx); /* convert the part 0 */
655 scalar |= a_mpu9250_inv_row_2_scale(mtx + 3) << 3; /* convert the part 1 */
656 scalar |= a_mpu9250_inv_row_2_scale(mtx + 6) << 6; /* convert the part 2 */
657
658 return scalar; /* return the scalar */
659}
660
666static void a_mpu9250_dmp_decode_gesture(mpu9250_handle_t *handle, uint8_t gesture[4])
667{
668 uint8_t tap;
669 uint8_t orient;
670
671 orient = gesture[3] & 0xC0; /* set the orient */
672 tap = 0x3F & gesture[3]; /* set the tap */
673 if ((gesture[1] & MPU9250_DMP_INT_SRC_TAP) != 0) /* check the tap output */
674 {
675 uint8_t direction, count;
676
677 direction = tap >> 3; /* get the direction */
678 count = (tap % 8) + 1; /* get the count */
679 if (handle->dmp_tap_callback != NULL) /* check the dmp tap callback */
680 {
681 handle->dmp_tap_callback(direction, count); /* run the dmp tap callback */
682 }
683 }
684 if ((gesture[1] & MPU9250_DMP_INT_SRC_ORIENT) != 0) /* check the orient output */
685 {
686 if (handle->dmp_orient_callback != NULL) /* check the dmp orient callback */
687 {
688 handle->dmp_orient_callback(orient >> 6); /* run the dmp orient callback */
689 }
690 }
691}
692
703static uint8_t a_mpu9250_accel_self_test(mpu9250_handle_t *handle, int32_t *bias_regular, int32_t *bias_st)
704{
705 uint8_t res;
706 uint8_t i;
707 uint8_t otp_value_zero;
708 uint8_t regs[3];
709 float accel_st_al_min;
710 float accel_st_al_max;
711 float accel_offset_max;
712 float st_shift_cust[3];
713 float st_shift_ratio[3];
714 float ct_shift_prod[3];
715
716 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_X_ACCEL, regs, 3); /* read the tmp */
717 if (res != 0) /* check the result */
718 {
719 return 1; /* return error */
720 }
721
722 otp_value_zero = 0;
723 for (i = 0; i < 3; i++) /* 3 times */
724 {
725 if (regs[i] != 0) /* check the regs */
726 {
727 ct_shift_prod[i] = (float)gs_st_tb[regs[i] - 1]; /* get the table */
728 ct_shift_prod[i] *= 65536.f; /* x65535.0f */
729 ct_shift_prod[i] /= 16384.0f; /* 2g */
730 }
731 else
732 {
733 ct_shift_prod[i] = 0.0f; /* init 0.0f */
734 otp_value_zero = 1; /* check otp value zero */
735 }
736 }
737 if (otp_value_zero == 0) /* check otp value zero */
738 {
739 for (i = 0; i < 3; i++) /* 3 times */
740 {
741 st_shift_cust[i] = (float)(bias_st[i] - bias_regular[i]); /* set the shift cust */
742 st_shift_ratio[i] = st_shift_cust[i] / ct_shift_prod[i] - 1.f; /* set the shift ratio */
743 if (fabsf(st_shift_ratio[i]) > 0.5f) /* check the shift ratio */
744 {
745 return 1; /* return error */
746 }
747 }
748 }
749 else
750 {
751 accel_st_al_min = 0.225f * 65536.f; /* set the st al min */
752 accel_st_al_max = 0.675f * 65536.f; /* set the st al max */
753 for (i = 0; i < 3; i++) /* 3 times */
754 {
755 st_shift_cust[i] = (float)(bias_st[i] - bias_regular[i]); /* shift cust */
756 if (st_shift_cust[i] < accel_st_al_min
757 || st_shift_cust[i] > accel_st_al_max) /* check the result */
758 {
759 return 1; /* return error */
760 }
761 }
762 }
763
764 accel_offset_max = 0.5f * 65536.f; /* set the offset max */
765 for (i = 0; i < 3; i++) /* 3 times */
766 {
767 if (fabsf((float)bias_regular[i]) > accel_offset_max) /* check offset */
768 {
769 return 1; /* return error */
770 }
771 }
772
773 return 0; /* success return 0 */
774}
775
786static uint8_t a_mpu9250_gyro_self_test(mpu9250_handle_t *handle, int32_t *bias_regular, int32_t *bias_st)
787{
788 uint8_t res;
789 uint8_t i;
790 uint8_t otp_value_zero;
791 float gyro_st_al_max;
792 float gyro_offset_max;
793 float st_shift_cust[3];
794 float st_shift_ratio[3];
795 float ct_shift_prod[3];
796 uint8_t regs[3];
797
798 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_X_GYRO, regs, 3); /* read the tmp */
799 if (res != 0) /* check the result */
800 {
801 return 1; /* return error */
802 }
803
804 otp_value_zero = 0; /* init otp value zero 0.0f */
805 for (i = 0; i < 3; i++) /* 3 times */
806 {
807 if (regs[i] != 0) /* check regs */
808 {
809 ct_shift_prod[i] = (float)gs_st_tb[regs[i] - 1]; /* set shift prod */
810 ct_shift_prod[i] *= 65536.f; /* x65536.0f */
811 ct_shift_prod[i] /= 131.0f; /* 250dps */
812 }
813 else
814 {
815 ct_shift_prod[i] = 0; /* init 0.0f */
816 otp_value_zero = 1; /* set otp value zero 1 */
817 }
818 }
819
820 if (otp_value_zero == 0) /* if otp value zero == 0 */
821 {
822 for (i = 0; i < 3; i++) /* 3 times */
823 {
824 st_shift_cust[i] = (float)(bias_st[i] - bias_regular[i]); /* shift cust */
825 st_shift_ratio[i] = st_shift_cust[i] / ct_shift_prod[i]; /* shift ratio */
826 if (fabsf(st_shift_ratio[i]) < 0.5f) /* check ratio */
827 {
828 return 1; /* return error */
829 }
830 }
831 }
832 else
833 {
834 gyro_st_al_max = 60.f * 65536.f; /* set st al max */
835 for (i = 0; i < 3; i++) /* 3 times */
836 {
837 st_shift_cust[i] = (float)(bias_st[i] - bias_regular[i]); /* set cust */
838 if (st_shift_cust[i] < gyro_st_al_max) /* check st al max */
839 {
840 return 1; /* return error */
841 }
842 }
843 }
844
845 gyro_offset_max = 20.f * 65536.f; /* set gyro offset max */
846 for (i = 0; i < 3; i++) /* 3 times */
847 {
848 if( fabsf((float)bias_regular[i]) > gyro_offset_max) /* check gyro offset max */
849 {
850 return 1; /* return error */
851 }
852 }
853
854 return 0; /* success return 0 */
855}
856
868static uint8_t a_mpu9250_get_st_biases(mpu9250_handle_t *handle,
869 int32_t gyro_offset[3], int32_t accel_offset[3],
870 uint8_t hw_test_enable)
871{
872 uint8_t res;
873 uint16_t cnt;
874 uint16_t pack_cnt;
875 uint16_t i;
876 uint8_t data[12];
877
878 data[0] = 0x01; /* set 0x01 */
879 data[1] = 0x00; /* set 0x00 */
880 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, data, 2); /* write pwr mgmt1 */
881 if (res != 0) /* check the result */
882 {
883 return 1; /* return error */
884 }
885 handle->delay_ms(200); /* delay 200ms */
886 data[0] = 0; /* set 0 */
887 res = a_mpu9250_write(handle, MPU9250_REG_INT_ENABLE, data, 1); /* write int enable */
888 if (res != 0) /* check the result */
889 {
890 return 1; /* return error */
891 }
892 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, data, 1); /* write the fifo enable */
893 if (res != 0) /* check the result */
894 {
895 return 1; /* return error */
896 }
897 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, data, 1); /* write the pwr mgmt 1 */
898 if (res != 0) /* check the result */
899 {
900 return 1; /* return error */
901 }
902 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, data, 1); /* write the i2c mst ctrl */
903 if (res != 0) /* check the result */
904 {
905 return 1; /* return error */
906 }
907 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, data, 1); /* write the user ctrl */
908 if (res != 0) /* check the result */
909 {
910 return 1; /* return error */
911 }
912
913 data[0] = 1 << 3 | 1 << 2; /* set fifo and dmp reset */
914 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, data, 1); /* write user ctrl */
915 if (res != 0) /* check the result */
916 {
917 return 1; /* return error */
918 }
919 handle->delay_ms(15); /* delay 15 ms */
920 data[0] = 0x01; /* set 0x01 */
921 res = a_mpu9250_write(handle, MPU9250_REG_CONFIG, data, 1); /* write config */
922 if (res != 0) /* check the result */
923 {
924 return 1; /* return error */
925 }
926 data[0] = 0x00; /* set 0 */
927 res = a_mpu9250_write(handle, MPU9250_REG_SMPRT_DIV, data, 1); /* write div */
928 if (res != 0) /* check the result */
929 {
930 return 1; /* return error */
931 }
932 if (hw_test_enable != 0) /* if enable */
933 {
934 data[0] = 0x00 | 0xE0; /* set 250dps and test */
935 }
936 else
937 {
938 data[0] = 0x00; /* set 250dps */
939 }
940 res = a_mpu9250_write(handle, MPU9250_REG_GYRO_CONFIG, data, 1); /* read the gyro config */
941 if (res != 0) /* check the result */
942 {
943 return 1; /* return error */
944 }
945 if (hw_test_enable != 0) /* if enable */
946 {
947 data[0] = 0x18 | 0xE0; /* enable 2g and test */
948 }
949 else
950 {
951 data[0] = 0x18; /* enable 2g */
952 }
953 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG, data, 1); /* read the accel config */
954 if (res != 0) /* check the result */
955 {
956 return 1; /* return error */
957 }
958 if (hw_test_enable != 0) /* if enable */
959 {
960 handle->delay_ms(200); /* delay 200ms */
961 }
962
963 data[0] = 1 << 6; /* enable fifo */
964 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, data, 1); /* write user ctrl */
965 if (res != 0) /* check the result */
966 {
967 return 1; /* return error */
968 }
969 data[0] = 0x78; /* enable xyz */
970 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, data, 1); /* write fifo enable */
971 if (res != 0) /* check the result */
972 {
973 return 1; /* return error */
974 }
975 handle->delay_ms(50); /* delay 50 ms */
976 data[0] = 0x00; /* set disable */
977 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, data, 1); /* write fifo enable */
978 if (res != 0) /* check the result */
979 {
980 return 1; /* return error */
981 }
982
983 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_COUNTH, data, 2); /* read fifo counter */
984 if (res != 0) /* check the result */
985 {
986 return 1; /* return error */
987 }
988 cnt = ((uint16_t)data[0] << 8) | data[1]; /* set the counter */
989 pack_cnt = cnt / 12; /* set the packet counter */
990
991 gyro_offset[0] = 0; /* gyro offset 0 */
992 gyro_offset[1] = 0; /* gyro offset 1 */
993 gyro_offset[2] = 0; /* gyro offset 2 */
994 accel_offset[0] = 0; /* accel offset 0 */
995 accel_offset[1] = 0; /* accel offset 1 */
996 accel_offset[2] = 0; /* accel offset 2 */
997 for (i = 0; i < pack_cnt; i++) /* packet counter */
998 {
999 int16_t accel_cur[3];
1000 int16_t gyro_cur[3];
1001
1002 res = a_mpu9250_read(handle, MPU9250_REG_R_W, data, 12); /* read data */
1003 if (res != 0) /* check the result */
1004 {
1005 return 1; /* return error */
1006 }
1007
1008 accel_cur[0] = ((int16_t)data[0] << 8) | data[1]; /* accel cur 0 */
1009 accel_cur[1] = ((int16_t)data[2] << 8) | data[3]; /* accel cur 1 */
1010 accel_cur[2] = ((int16_t)data[4] << 8) | data[5]; /* accel cur 2 */
1011 accel_offset[0] += (int32_t)accel_cur[0]; /* accel offset 0 */
1012 accel_offset[1] += (int32_t)accel_cur[1]; /* accel offset 1 */
1013 accel_offset[2] += (int32_t)accel_cur[2]; /* accel offset 2 */
1014 gyro_cur[0] = (((int16_t)data[6] << 8) | data[7]); /* gyro cur 0 */
1015 gyro_cur[1] = (((int16_t)data[8] << 8) | data[9]); /* gyro cur 1 */
1016 gyro_cur[2] = (((int16_t)data[10] << 8) | data[11]); /* gyro cur 2 */
1017 gyro_offset[0] += (int32_t)gyro_cur[0]; /* gyro offset 0 */
1018 gyro_offset[1] += (int32_t)gyro_cur[1]; /* gyro offset 1 */
1019 gyro_offset[2] += (int32_t)gyro_cur[2]; /* gyro offset 2 */
1020 }
1021
1022 gyro_offset[0] = (int32_t)(((int64_t)gyro_offset[0] << 16) / (32768 / 250) / pack_cnt); /* set the gyro offset 0 */
1023 gyro_offset[1] = (int32_t)(((int64_t)gyro_offset[1] << 16) / (32768 / 250) / pack_cnt); /* set the gyro offset 1 */
1024 gyro_offset[2] = (int32_t)(((int64_t)gyro_offset[2] << 16) / (32768 / 250) / pack_cnt); /* set the gyro offset 2 */
1025 accel_offset[0] = (int32_t)(((int64_t)accel_offset[0] << 16) / (32768 / 16) / pack_cnt); /* set the accel offset 0 */
1026 accel_offset[1] = (int32_t)(((int64_t)accel_offset[1] << 16) / (32768 / 16) / pack_cnt); /* set the accel offset 1 */
1027 accel_offset[2] = (int32_t)(((int64_t)accel_offset[2] << 16) / (32768 / 16) / pack_cnt); /* set the accel offset 2 */
1028 if (accel_offset[2] > 0L) /* check the accel offset */
1029 {
1030 accel_offset[2] -= 65536L; /* -65536 */
1031 }
1032 else
1033 {
1034 accel_offset[2] += 65536L; /* +65536 */
1035 }
1036
1037 return 0; /* success return 0 */
1038}
1039
1048static uint8_t a_mpu9250_deinit(mpu9250_handle_t *handle)
1049{
1050 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
1051 {
1052 if (handle->iic_deinit() != 0) /* iic deinit */
1053 {
1054 return 1; /* return error */
1055 }
1056 else
1057 {
1058 return 0; /* success return 0 */
1059 }
1060 }
1061 else
1062 {
1063 if (handle->spi_deinit() != 0) /* if spi interface */
1064 {
1065 return 1; /* return error */
1066 }
1067 else
1068 {
1069 return 0; /* success return 0 */
1070 }
1071 }
1072}
1073
1088{
1089 uint8_t res;
1090 uint16_t i;
1091 uint16_t size;
1092 uint16_t this_write;
1093 uint8_t tmp[2];
1094 uint8_t cur[16];
1095
1096 if (handle == NULL) /* check handle */
1097 {
1098 return 2; /* return error */
1099 }
1100 if (handle->inited != 1) /* check handle initialization */
1101 {
1102 return 3; /* return error */
1103 }
1104 if (handle->dmp_inited != 0) /* check dmp initialization */
1105 {
1106 handle->debug_print("mpu9250: dmp is running.\n"); /* dmp is running */
1107
1108 return 4; /* return error */
1109 }
1110
1111 size = MPU9250_DMP_CODE_SIZE; /* set the code size */
1112 for (i = 0; i < size; i += this_write) /* code size times */
1113 {
1114 this_write = MIN(16, size - i); /* get the written size */
1115
1116 res = a_mpu9250_write_mem(handle, i, (uint8_t *)(gs_mpu9250_dmp_code + i),
1117 this_write); /* write data */
1118 if (res != 0) /* check result */
1119 {
1120 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1121
1122 return 1; /* return error */
1123 }
1124 res = a_mpu9250_read_mem(handle, i, cur, this_write); /* read data */
1125 if (res != 0) /* check result */
1126 {
1127 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1128
1129 return 1; /* return error */
1130 }
1131 if (memcmp(gs_mpu9250_dmp_code + i, cur, this_write) != 0) /* check the code */
1132 {
1133 handle->debug_print("mpu9250: code compare error.\n"); /* code compare error */
1134
1135 return 5; /* return error */
1136 }
1137 }
1138 tmp[0] = (0x0400 >> 8) & 0xFF; /* set the addr high */
1139 tmp[1] = (0x0400 >> 0) & 0xFF; /* set the addr low */
1140
1141 if (a_mpu9250_write(handle, MPU9250_REG_PROGRAM_START, (uint8_t *)tmp, 2) != 0) /* write data */
1142 {
1143 handle->debug_print("mpu9250: set program start failed.\n"); /* set program start failed */
1144
1145 return 6; /* return error */
1146 }
1147 handle->dmp_inited = 1; /* flag the dmp inited bit */
1148
1149 return 0; /* success return 0 */
1150}
1151
1165{
1166 uint8_t res;
1167 uint8_t tmp[4];
1168
1169 if (handle == NULL) /* check handle */
1170 {
1171 return 2; /* return error */
1172 }
1173 if (handle->inited != 1) /* check handle initialization */
1174 {
1175 return 3; /* return error */
1176 }
1177 if (handle->dmp_inited != 1) /* check dmp initialization */
1178 {
1179 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1180
1181 return 4; /* return error */
1182 }
1183
1184 ms /= 20; /* div 20 */
1185 tmp[0] = (uint8_t)((ms >> 24) & 0xFF); /* set part 0 */
1186 tmp[1] = (uint8_t)((ms >> 16) & 0xFF); /* set part 1 */
1187 tmp[2] = (uint8_t)((ms >> 8) & 0xFF); /* set part 2 */
1188 tmp[3] = (uint8_t)(ms & 0xFF); /* set part 3 */
1189
1190 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_PEDSTD_TIMECTR, tmp, 4); /* write data */
1191 if (res != 0) /* check result */
1192 {
1193 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1194
1195 return 1; /* return error */
1196 }
1197
1198 return 0; /* success return 0 */
1199}
1200
1214{
1215 uint8_t res;
1216 uint8_t tmp[4];
1217
1218 if (handle == NULL) /* check handle */
1219 {
1220 return 2; /* return error */
1221 }
1222 if (handle->inited != 1) /* check handle initialization */
1223 {
1224 return 3; /* return error */
1225 }
1226 if (handle->dmp_inited != 1) /* check dmp initialization */
1227 {
1228 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1229
1230 return 4; /* return error */
1231 }
1232
1233 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_PEDSTD_TIMECTR, tmp, 4); /* read data */
1234 if (res != 0) /* check result */
1235 {
1236 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1237
1238 return 1; /* return error */
1239 }
1240 *ms = (((uint32_t)tmp[0] << 24) | ((uint32_t)tmp[1] << 16) |
1241 ((uint32_t)tmp[2] << 8) | tmp[3]) * 20; /* get the ms */
1242
1243 return 0; /* success return 0 */
1244}
1245
1259{
1260 uint8_t res;
1261 uint8_t tmp[4];
1262
1263 if (handle == NULL) /* check handle */
1264 {
1265 return 2; /* return error */
1266 }
1267 if (handle->inited != 1) /* check handle initialization */
1268 {
1269 return 3; /* return error */
1270 }
1271 if (handle->dmp_inited != 1) /* check dmp initialization */
1272 {
1273 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1274
1275 return 4; /* return error */
1276 }
1277
1278 tmp[0] = (uint8_t)((count >> 24) & 0xFF); /* set part 0 */
1279 tmp[1] = (uint8_t)((count >> 16) & 0xFF); /* set part 1 */
1280 tmp[2] = (uint8_t)((count >> 8) & 0xFF); /* set part 2 */
1281 tmp[3] = (uint8_t)(count & 0xFF); /* set part 3 */
1282
1283 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_PEDSTD_STEPCTR, tmp, 4); /* write data */
1284 if (res != 0) /* check result */
1285 {
1286 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1287
1288 return 1; /* return error */
1289 }
1290
1291 return 0; /* success return 0 */
1292}
1293
1307{
1308 uint8_t res;
1309 uint8_t tmp[4];
1310
1311 if (handle == NULL) /* check handle */
1312 {
1313 return 2; /* return error */
1314 }
1315 if (handle->inited != 1) /* check handle initialization */
1316 {
1317 return 3; /* return error */
1318 }
1319 if (handle->dmp_inited != 1) /* check dmp initialization */
1320 {
1321 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1322
1323 return 4; /* return error */
1324 }
1325
1326 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_PEDSTD_STEPCTR, tmp, 4); /* read data */
1327 if (res != 0) /* check result */
1328 {
1329 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1330
1331 return 1; /* return error */
1332 }
1333 *count = (((uint32_t)tmp[0] << 24) | ((uint32_t)tmp[1] << 16) |
1334 ((uint32_t)tmp[2] << 8) | tmp[3]); /* get the ms */
1335
1336 return 0; /* success return 0 */
1337}
1338
1352{
1353 uint8_t res;
1354 uint8_t tmp[2];
1355
1356 if (handle == NULL) /* check handle */
1357 {
1358 return 2; /* return error */
1359 }
1360 if (handle->inited != 1) /* check handle initialization */
1361 {
1362 return 3; /* return error */
1363 }
1364 if (handle->dmp_inited != 1) /* check dmp initialization */
1365 {
1366 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1367
1368 return 4; /* return error */
1369 }
1370
1371 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1372 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
1373 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
1374
1375 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_88, tmp, 2); /* write data */
1376 if (res != 0) /* check result */
1377 {
1378 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1379
1380 return 1; /* return error */
1381 }
1382
1383 return 0; /* success return 0 */
1384}
1385
1399{
1400 uint8_t res;
1401 uint8_t tmp[2];
1402
1403 if (handle == NULL) /* check handle */
1404 {
1405 return 2; /* return error */
1406 }
1407 if (handle->inited != 1) /* check handle initialization */
1408 {
1409 return 3; /* return error */
1410 }
1411 if (handle->dmp_inited != 1) /* check dmp initialization */
1412 {
1413 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1414
1415 return 4; /* return error */
1416 }
1417
1418 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_88, tmp, 2); /* read data */
1419 if (res != 0) /* check result */
1420 {
1421 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1422
1423 return 1; /* return error */
1424 }
1425 *ms = (uint16_t)((uint16_t)tmp[0] << 8) | tmp[1]; /* get the raw time */
1426 *ms *= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1427
1428 return 0; /* success return 0 */
1429}
1430
1444{
1445 uint8_t res;
1446 uint8_t tmp[2];
1447
1448 if (handle == NULL) /* check handle */
1449 {
1450 return 2; /* return error */
1451 }
1452 if (handle->inited != 1) /* check handle initialization */
1453 {
1454 return 3; /* return error */
1455 }
1456 if (handle->dmp_inited != 1) /* check dmp initialization */
1457 {
1458 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1459
1460 return 4; /* return error */
1461 }
1462
1463 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1464 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
1465 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
1466
1467 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_90, tmp, 2); /* write data */
1468 if (res != 0) /* check result */
1469 {
1470 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1471
1472 return 1; /* return error */
1473 }
1474
1475 return 0; /* success return 0 */
1476}
1477
1491{
1492 uint8_t res;
1493 uint8_t tmp[2];
1494
1495 if (handle == NULL) /* check handle */
1496 {
1497 return 2; /* return error */
1498 }
1499 if (handle->inited != 1) /* check handle initialization */
1500 {
1501 return 3; /* return error */
1502 }
1503 if (handle->dmp_inited != 1) /* check dmp initialization */
1504 {
1505 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1506
1507 return 4; /* return error */
1508 }
1509
1510 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_90, tmp, 2); /* read data */
1511 if (res != 0) /* check result */
1512 {
1513 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1514
1515 return 1; /* return error */
1516 }
1517 *ms = (uint16_t)((uint16_t)tmp[0] << 8) | tmp[1]; /* get the raw time */
1518 *ms *= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1519
1520 return 0; /* success return 0 */
1521}
1522
1536{
1537 uint8_t res;
1538 uint8_t tmp[4];
1539 uint32_t thresh_scaled;
1540
1541 if (handle == NULL) /* check handle */
1542 {
1543 return 2; /* return error */
1544 }
1545 if (handle->inited != 1) /* check handle initialization */
1546 {
1547 return 3; /* return error */
1548 }
1549 if (handle->dmp_inited != 1) /* check dmp initialization */
1550 {
1551 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1552
1553 return 4; /* return error */
1554 }
1555
1556 thresh_scaled = MPU9250_DMP_GYRO_SF / 1000 * dps; /* convert to thresh scaled */
1557 tmp[0] = (uint8_t)(((uint32_t)thresh_scaled >> 24) & 0xFF); /* set the part 3 */
1558 tmp[1] = (uint8_t)(((uint32_t)thresh_scaled >> 16) & 0xFF); /* set the part 2 */
1559 tmp[2] = (uint8_t)(((uint32_t)thresh_scaled >> 8) & 0xFF); /* set the part 1 */
1560 tmp[3] = (uint8_t)((uint32_t)thresh_scaled & 0xFF); /* set the part 0 */
1561
1562 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_92, tmp, 4); /* write data */
1563 if (res != 0) /* check result */
1564 {
1565 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1566
1567 return 1; /* return error */
1568 }
1569
1570 return 0; /* success return 0 */
1571}
1572
1586{
1587 uint8_t res;
1588 uint8_t tmp[4];
1589 uint32_t thresh_scaled;
1590
1591 if (handle == NULL) /* check handle */
1592 {
1593 return 2; /* return error */
1594 }
1595 if (handle->inited != 1) /* check handle initialization */
1596 {
1597 return 3; /* return error */
1598 }
1599 if (handle->dmp_inited != 1) /* check dmp initialization */
1600 {
1601 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1602
1603 return 4; /* return error */
1604 }
1605
1606 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_92, tmp, 4); /* read data */
1607 if (res != 0) /* check result */
1608 {
1609 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1610
1611 return 1; /* return error */
1612 }
1613 thresh_scaled = (((uint32_t)tmp[0] << 24) | ((uint32_t)tmp[1] << 16) |
1614 ((uint32_t)tmp[2] << 8) | tmp[3]); /* get the thresh scaled */
1615 *dps = (uint16_t)((float)(thresh_scaled) /
1616 ((float)(MPU9250_DMP_GYRO_SF) / 1000.0f)); /* convert the thresh scaled */
1617
1618 return 0; /* success return 0 */
1619}
1620
1634{
1635 uint8_t res;
1636 uint8_t tmp[2];
1637
1638 if (handle == NULL) /* check handle */
1639 {
1640 return 2; /* return error */
1641 }
1642 if (handle->inited != 1) /* check handle initialization */
1643 {
1644 return 3; /* return error */
1645 }
1646 if (handle->dmp_inited != 1) /* check dmp initialization */
1647 {
1648 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1649
1650 return 4; /* return error */
1651 }
1652
1653 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1654 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
1655 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
1656
1657 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_218, tmp, 2); /* write data */
1658 if (res != 0) /* check result */
1659 {
1660 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1661
1662 return 1; /* return error */
1663 }
1664
1665 return 0; /* success return 0 */
1666}
1667
1681{
1682 uint8_t res;
1683 uint8_t tmp[2];
1684
1685 if (handle == NULL) /* check handle */
1686 {
1687 return 2; /* return error */
1688 }
1689 if (handle->inited != 1) /* check handle initialization */
1690 {
1691 return 3; /* return error */
1692 }
1693 if (handle->dmp_inited != 1) /* check dmp initialization */
1694 {
1695 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1696
1697 return 4; /* return error */
1698 }
1699
1700 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_218, tmp, 2); /* read data */
1701 if (res != 0) /* check result */
1702 {
1703 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1704
1705 return 1; /* return error */
1706 }
1707 *ms = (uint16_t)((uint16_t)tmp[0] << 8) | tmp[1]; /* get the raw time */
1708 *ms *= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1709
1710 return 0; /* success return 0 */
1711}
1712
1725uint8_t mpu9250_dmp_set_tap_time(mpu9250_handle_t *handle, uint16_t ms)
1726{
1727 uint8_t res;
1728 uint8_t tmp[2];
1729
1730 if (handle == NULL) /* check handle */
1731 {
1732 return 2; /* return error */
1733 }
1734 if (handle->inited != 1) /* check handle initialization */
1735 {
1736 return 3; /* return error */
1737 }
1738 if (handle->dmp_inited != 1) /* check dmp initialization */
1739 {
1740 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1741
1742 return 4; /* return error */
1743 }
1744
1745 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1746 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
1747 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
1748
1749 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAPW_MIN, tmp, 2); /* write data */
1750 if (res != 0) /* check result */
1751 {
1752 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1753
1754 return 1; /* return error */
1755 }
1756
1757 return 0; /* success return 0 */
1758}
1759
1772uint8_t mpu9250_dmp_get_tap_time(mpu9250_handle_t *handle, uint16_t *ms)
1773{
1774 uint8_t res;
1775 uint8_t tmp[2];
1776
1777 if (handle == NULL) /* check handle */
1778 {
1779 return 2; /* return error */
1780 }
1781 if (handle->inited != 1) /* check handle initialization */
1782 {
1783 return 3; /* return error */
1784 }
1785 if (handle->dmp_inited != 1) /* check dmp initialization */
1786 {
1787 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1788
1789 return 4; /* return error */
1790 }
1791
1792 res = a_mpu9250_read_mem(handle, MPU9250_DMP_TAPW_MIN, tmp, 2); /* read data */
1793 if (res != 0) /* check result */
1794 {
1795 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1796
1797 return 1; /* return error */
1798 }
1799 *ms = (uint16_t)((uint16_t)tmp[0] << 8) | tmp[1]; /* get the raw time */
1800 *ms *= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
1801
1802 return 0; /* success return 0 */
1803}
1804
1819{
1820 uint8_t res;
1821 uint8_t tmp;
1822
1823 if (handle == NULL) /* check handle */
1824 {
1825 return 2; /* return error */
1826 }
1827 if (handle->inited != 1) /* check handle initialization */
1828 {
1829 return 3; /* return error */
1830 }
1831 if (handle->dmp_inited != 1) /* check dmp initialization */
1832 {
1833 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1834
1835 return 4; /* return error */
1836 }
1837 if ((cnt < 1) || (cnt > 4)) /* check cnt */
1838 {
1839 handle->debug_print("mpu9250: cnt must be between 1 and 4.\n"); /* cnt must be between 1 and 4 */
1840
1841 return 5; /* return error */
1842 }
1843
1844 tmp = cnt - 1; /* set the counter */
1845 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_79, &tmp, 1); /* write data */
1846 if (res != 0) /* check result */
1847 {
1848 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1849
1850 return 1; /* return error */
1851 }
1852
1853 return 0; /* success return 0 */
1854}
1855
1869{
1870 uint8_t res;
1871 uint8_t tmp;
1872
1873 if (handle == NULL) /* check handle */
1874 {
1875 return 2; /* return error */
1876 }
1877 if (handle->inited != 1) /* check handle initialization */
1878 {
1879 return 3; /* return error */
1880 }
1881 if (handle->dmp_inited != 1) /* check dmp initialization */
1882 {
1883 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1884
1885 return 4; /* return error */
1886 }
1887
1888 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_79, &tmp, 1); /* read data */
1889 if (res != 0) /* check result */
1890 {
1891 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
1892
1893 return 1; /* return error */
1894 }
1895 *cnt = tmp + 1; /* set the counter */
1896
1897 return 0; /* success return 0 */
1898}
1899
1913{
1914 uint8_t res;
1915
1916 if (handle == NULL) /* check handle */
1917 {
1918 return 2; /* return error */
1919 }
1920 if (handle->inited != 1) /* check handle initialization */
1921 {
1922 return 3; /* return error */
1923 }
1924 if (handle->dmp_inited != 1) /* check dmp initialization */
1925 {
1926 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1927
1928 return 4; /* return error */
1929 }
1930
1931 if (enable != 0) /* enable */
1932 {
1933 uint8_t regs[9] = {0xb8, 0xaa, 0xb3, 0x8d, 0xb4, 0x98, 0x0d, 0x35, 0x5d};
1934
1935 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_MOTION_BIAS, regs, 9); /* write data */
1936 if (res != 0) /* check result */
1937 {
1938 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1939
1940 return 1; /* return error */
1941 }
1942 handle->mask |= MPU9250_DMP_FEATURE_GYRO_CAL; /* set the mask */
1943
1944 return 0; /* success return 0 */
1945 }
1946 else /* disable */
1947 {
1948 uint8_t regs[9] = {0xb8, 0xaa, 0xaa, 0xaa, 0xb0, 0x88, 0xc3, 0xc5, 0xc7};
1949
1950 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_MOTION_BIAS, regs, 9); /* write data */
1951 if (res != 0) /* check result */
1952 {
1953 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
1954
1955 return 1; /* return error */
1956 }
1957 handle->mask &= ~MPU9250_DMP_FEATURE_GYRO_CAL; /* set the mask */
1958
1959 return 0; /* success return 0 */
1960 }
1961}
1962
1976{
1977 uint8_t res;
1978
1979 if (handle == NULL) /* check handle */
1980 {
1981 return 2; /* return error */
1982 }
1983 if (handle->inited != 1) /* check handle initialization */
1984 {
1985 return 3; /* return error */
1986 }
1987 if (handle->dmp_inited != 1) /* check dmp initialization */
1988 {
1989 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
1990
1991 return 4; /* return error */
1992 }
1993
1994 if (enable != 0) /* enable */
1995 {
1996 uint8_t regs[4] = {0xC0, 0xC2, 0xC4, 0xC6};
1997
1998 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_LP_QUAT, regs, 4); /* write data */
1999 if (res != 0) /* check result */
2000 {
2001 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2002
2003 return 1; /* return error */
2004 }
2005 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2006 if (res != 0) /* check result */
2007 {
2008 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2009
2010 return 1; /* return error */
2011 }
2012 handle->mask |= MPU9250_DMP_FEATURE_3X_QUAT; /* set the mask */
2013
2014 return 0; /* success return 0 */
2015 }
2016 else /* disable */
2017 {
2018 uint8_t regs[4] = {0x8B, 0x8B, 0x8B, 0x8B};
2019
2020 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_LP_QUAT, regs, 4); /* write data */
2021 if (res != 0) /* check result */
2022 {
2023 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2024
2025 return 1; /* return error */
2026 }
2027 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2028 if (res != 0) /* check result */
2029 {
2030 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2031
2032 return 1; /* return error */
2033 }
2034 handle->mask &= ~MPU9250_DMP_FEATURE_3X_QUAT; /* set the mask */
2035
2036 return 0; /* success return 0 */
2037 }
2038}
2039
2053{
2054 uint8_t res;
2055
2056 if (handle == NULL) /* check handle */
2057 {
2058 return 2; /* return error */
2059 }
2060 if (handle->inited != 1) /* check handle initialization */
2061 {
2062 return 3; /* return error */
2063 }
2064 if (handle->dmp_inited != 1) /* check dmp initialization */
2065 {
2066 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2067
2068 return 4; /* return error */
2069 }
2070
2071 if (enable != 0) /* enable */
2072 {
2073 uint8_t regs[4] = {0x20, 0x28, 0x30, 0x38};
2074
2075 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_8, regs, 4); /* write data */
2076 if (res != 0) /* check result */
2077 {
2078 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2079
2080 return 1; /* return error */
2081 }
2082 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2083 if (res != 0) /* check result */
2084 {
2085 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2086
2087 return 1; /* return error */
2088 }
2089 handle->mask |= MPU9250_DMP_FEATURE_6X_QUAT; /* set the mask */
2090
2091 return 0; /* success return 0 */
2092 }
2093 else /* disable */
2094 {
2095 uint8_t regs[4] = {0xA3, 0xA3, 0xA3, 0xA3};
2096
2097 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_8, regs, 4); /* write data */
2098 if (res != 0) /* check result */
2099 {
2100 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2101
2102 return 1; /* return error */
2103 }
2104 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2105 if (res != 0) /* check result */
2106 {
2107 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2108
2109 return 1; /* return error */
2110 }
2111 handle->mask &= ~MPU9250_DMP_FEATURE_6X_QUAT; /* set the mask */
2112
2113 return 0; /* success return 0 */
2114 }
2115}
2116
2130{
2131 uint8_t res;
2132
2133 if (handle == NULL) /* check handle */
2134 {
2135 return 2; /* return error */
2136 }
2137 if (handle->inited != 1) /* check handle initialization */
2138 {
2139 return 3; /* return error */
2140 }
2141 if (handle->dmp_inited != 1) /* check dmp initialization */
2142 {
2143 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2144
2145 return 4; /* return error */
2146 }
2147
2148 if (mode == MPU9250_DMP_INTERRUPT_MODE_CONTINUOUS) /* continuous */
2149 {
2150 uint8_t regs_continuous[11] = {0xd8, 0xb1, 0xb9,
2151 0xf3, 0x8b, 0xa3,
2152 0x91, 0xb6, 0x09,
2153 0xb4, 0xd9};
2154
2155 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_FIFO_ON_EVENT,
2156 (uint8_t *)regs_continuous, 11); /* write data */
2157 if (res != 0) /* check result */
2158 {
2159 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2160
2161 return 1; /* return error */
2162 }
2163
2164 return 0; /* success return 0 */
2165 }
2166 else /* disable */
2167 {
2168 uint8_t regs_gesture[11] = {0xda, 0xb1, 0xb9,
2169 0xf3, 0x8b, 0xa3,
2170 0x91, 0xb6, 0xda,
2171 0xb4, 0xda};
2172
2173 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_FIFO_ON_EVENT,
2174 (uint8_t *)regs_gesture, 11); /* write data */
2175 if (res != 0) /* check result */
2176 {
2177 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2178
2179 return 1; /* return error */
2180 }
2181
2182 return 0; /* success return 0 */
2183 }
2184}
2185
2198uint8_t mpu9250_dmp_set_gyro_bias(mpu9250_handle_t *handle, int32_t bias[3])
2199{
2200 uint8_t res;
2201 uint8_t regs[4];
2202 int32_t gyro_bias_body[3];
2203
2204 if (handle == NULL) /* check handle */
2205 {
2206 return 2; /* return error */
2207 }
2208 if (handle->inited != 1) /* check handle initialization */
2209 {
2210 return 3; /* return error */
2211 }
2212 if (handle->dmp_inited != 1) /* check dmp initialization */
2213 {
2214 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2215
2216 return 4; /* return error */
2217 }
2218
2219 gyro_bias_body[0] = bias[handle->orient & 3]; /* set the body 0 */
2220 if ((handle->orient & 4) != 0) /* check bit 3 */
2221 {
2222 gyro_bias_body[0] *= -1; /* *(-1) */
2223 }
2224 gyro_bias_body[1] = bias[(handle->orient >> 3) & 3]; /* set the body 1 */
2225 if ((handle->orient & 0x20) != 0) /* check bit 6 */
2226 {
2227 gyro_bias_body[1] *= -1; /* *(-1) */
2228 }
2229 gyro_bias_body[2] = bias[(handle->orient >> 6) & 3]; /* set the body 2 */
2230 if ((handle->orient & 0x100) != 0) /* check bit 9 */
2231 {
2232 gyro_bias_body[2] *= -1; /* *(-1) */
2233 }
2234
2235 gyro_bias_body[0] = (int32_t)(((int64_t)gyro_bias_body[0] * MPU9250_DMP_GYRO_SF) >> 30); /* set body 0 */
2236 gyro_bias_body[1] = (int32_t)(((int64_t)gyro_bias_body[1] * MPU9250_DMP_GYRO_SF) >> 30); /* set body 1 */
2237 gyro_bias_body[2] = (int32_t)(((int64_t)gyro_bias_body[2] * MPU9250_DMP_GYRO_SF) >> 30); /* set body 2 */
2238
2239 regs[0] = (uint8_t)((gyro_bias_body[0] >> 24) & 0xFF); /* set part 0 */
2240 regs[1] = (uint8_t)((gyro_bias_body[0] >> 16) & 0xFF); /* set part 1 */
2241 regs[2] = (uint8_t)((gyro_bias_body[0] >> 8) & 0xFF); /* set part 2 */
2242 regs[3] = (uint8_t)(gyro_bias_body[0] & 0xFF); /* set part 3 */
2243 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_EXT_GYRO_BIAS_X, (uint8_t *)regs, 4); /* write data */
2244 if (res != 0) /* check result */
2245 {
2246 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2247
2248 return 1; /* return error */
2249 }
2250 regs[0] = (uint8_t)((gyro_bias_body[1] >> 24) & 0xFF); /* set part 0 */
2251 regs[1] = (uint8_t)((gyro_bias_body[1] >> 16) & 0xFF); /* set part 1 */
2252 regs[2] = (uint8_t)((gyro_bias_body[1] >> 8) & 0xFF); /* set part 2 */
2253 regs[3] = (uint8_t)(gyro_bias_body[1] & 0xFF); /* set part 3 */
2254 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_EXT_GYRO_BIAS_Y, (uint8_t *)regs, 4); /* write data */
2255 if (res != 0) /* check result */
2256 {
2257 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2258
2259 return 1; /* return error */
2260 }
2261 regs[0] = (uint8_t)((gyro_bias_body[2] >> 24) & 0xFF); /* set part 0 */
2262 regs[1] = (uint8_t)((gyro_bias_body[2] >> 16) & 0xFF); /* set part 1 */
2263 regs[2] = (uint8_t)((gyro_bias_body[2] >> 8) & 0xFF); /* set part 2 */
2264 regs[3] = (uint8_t)(gyro_bias_body[2] & 0xFF); /* set part 3 */
2265 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_EXT_GYRO_BIAS_Z, (uint8_t *)regs, 4); /* write data */
2266 if (res != 0) /* check result */
2267 {
2268 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2269
2270 return 1; /* return error */
2271 }
2272
2273 return 0; /* success return 0 */
2274}
2275
2288uint8_t mpu9250_dmp_set_accel_bias(mpu9250_handle_t *handle, int32_t bias[3])
2289{
2290 uint8_t res;
2291 uint8_t prev;
2292 uint8_t range;
2293 int32_t accel_bias_body[3];
2294 uint8_t regs[12];
2295 int64_t accel_sf;
2296
2297 if (handle == NULL) /* check handle */
2298 {
2299 return 2; /* return error */
2300 }
2301 if (handle->inited != 1) /* check handle initialization */
2302 {
2303 return 3; /* return error */
2304 }
2305 if (handle->dmp_inited != 1) /* check dmp initialization */
2306 {
2307 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2308
2309 return 4; /* return error */
2310 }
2311
2312 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
2313 if (res != 0) /* check result */
2314 {
2315 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
2316
2317 return 1; /* return error */
2318 }
2319 range = ((prev >> 3) & 0x3); /* get the range */
2320 if (range == 0) /* if 2g */
2321 {
2322 accel_sf = (int64_t)16384 << 15; /* set the accel sf */
2323 }
2324 else if (range == 1) /* if 4g */
2325 {
2326 accel_sf = (int64_t)8192 << 15; /* set the accel sf */
2327 }
2328 else if (range == 2) /* if 8g */
2329 {
2330 accel_sf = (int64_t)4096 << 15; /* set the accel sf */
2331 }
2332 else /* if 16g */
2333 {
2334 accel_sf = (int64_t)2048 << 15; /* set the accel sf */
2335 }
2336
2337 accel_bias_body[0] = bias[handle->orient & 3]; /* set the bias body 0 */
2338 if ((handle->orient & 4) != 0) /* check the orient */
2339 {
2340 accel_bias_body[0] *= -1; /* *(-1) */
2341 }
2342 accel_bias_body[1] = bias[(handle->orient >> 3) & 3]; /* set the bias body 1 */
2343 if ((handle->orient & 0x20) != 0) /* check the orient */
2344 {
2345 accel_bias_body[1] *= -1; /* *(-1) */
2346 }
2347 accel_bias_body[2] = bias[(handle->orient >> 6) & 3]; /* set the bias body 2 */
2348 if ((handle->orient & 0x100) != 0) /* check the orient */
2349 {
2350 accel_bias_body[2] *= -1; /* *(-1) */
2351 }
2352
2353 accel_bias_body[0] = (int32_t)(((int64_t)accel_bias_body[0] * accel_sf) >> 30); /* set the bias body 0 */
2354 accel_bias_body[1] = (int32_t)(((int64_t)accel_bias_body[1] * accel_sf) >> 30); /* set the bias body 1 */
2355 accel_bias_body[2] = (int32_t)(((int64_t)accel_bias_body[2] * accel_sf) >> 30); /* set the bias body 2 */
2356 regs[0] = (uint8_t)((accel_bias_body[0] >> 24) & 0xFF); /* set reg 0 */
2357 regs[1] = (uint8_t)((accel_bias_body[0] >> 16) & 0xFF); /* set reg 1 */
2358 regs[2] = (uint8_t)((accel_bias_body[0] >> 8) & 0xFF); /* set reg 2 */
2359 regs[3] = (uint8_t)(accel_bias_body[0] & 0xFF); /* set reg 3 */
2360 regs[4] = (uint8_t)((accel_bias_body[1] >> 24) & 0xFF); /* set reg 4 */
2361 regs[5] = (uint8_t)((accel_bias_body[1] >> 16) & 0xFF); /* set reg 5 */
2362 regs[6] = (uint8_t)((accel_bias_body[1] >> 8) & 0xFF); /* set reg 6 */
2363 regs[7] = (uint8_t)(accel_bias_body[1] & 0xFF); /* set reg 7 */
2364 regs[8] = (uint8_t)((accel_bias_body[2] >> 24) & 0xFF); /* set reg 8 */
2365 regs[9] = (uint8_t)((accel_bias_body[2] >> 16) & 0xFF); /* set reg 9 */
2366 regs[10] = (uint8_t)((accel_bias_body[2] >> 8) & 0xFF); /* set reg 10 */
2367 regs[11] = (uint8_t)(accel_bias_body[2] & 0xFF); /* set reg 11 */
2368 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_ACCEL_BIAS, (uint8_t *)regs, 12); /* write data */
2369 if (res != 0) /* check result */
2370 {
2371 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2372
2373 return 1; /* return error */
2374 }
2375
2376 return 0; /* success return 0 */
2377}
2378
2391uint8_t mpu9250_dmp_set_orientation(mpu9250_handle_t *handle, int8_t mat[9])
2392{
2393 uint8_t gyro_axes[4] = {0x4C, 0xCD, 0x6C, 0x00};
2394 uint8_t accel_axes[4] = {0x0C, 0xC9, 0x2C, 0x00 };
2395 uint8_t gyro_sign[4] = {0x36, 0x56, 0x76, 0x00 };
2396 uint8_t accel_sign[4] = {0x26, 0x46, 0x66, 0x00 };
2397 uint8_t res;
2398 uint8_t gyro_regs[3];
2399 uint8_t accel_regs[3];
2400 uint16_t orient;
2401
2402 if (handle == NULL) /* check handle */
2403 {
2404 return 2; /* return error */
2405 }
2406 if (handle->inited != 1) /* check handle initialization */
2407 {
2408 return 3; /* return error */
2409 }
2410 if (handle->dmp_inited != 1) /* check dmp initialization */
2411 {
2412 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2413
2414 return 4; /* return error */
2415 }
2416
2417 orient = a_mpu9250_inv_orientation_matrix_to_scalar(mat); /* inv orientation matrix to scalar */
2418 gyro_regs[0] = gyro_axes[orient & 3]; /* set the gyro regs 0 */
2419 gyro_regs[1] = gyro_axes[(orient >> 3) & 3]; /* set the gyro regs 1 */
2420 gyro_regs[2] = gyro_axes[(orient >> 6) & 3]; /* set the gyro regs 2 */
2421 accel_regs[0] = accel_axes[orient & 3]; /* set the accel regs 0 */
2422 accel_regs[1] = accel_axes[(orient >> 3) & 3]; /* set the accel regs 1 */
2423 accel_regs[2] = accel_axes[(orient >> 6) & 3]; /* set the accel regs 2 */
2424 res = a_mpu9250_write_mem(handle, MPU9250_DMP_FCFG_1,
2425 (uint8_t *)gyro_regs, 3); /* write data */
2426 if (res != 0) /* check result */
2427 {
2428 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2429
2430 return 1; /* return error */
2431 }
2432 res = a_mpu9250_write_mem(handle, MPU9250_DMP_FCFG_2,
2433 (uint8_t *)accel_regs, 3); /* write data */
2434 if (res != 0) /* check result */
2435 {
2436 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2437
2438 return 1; /* return error */
2439 }
2440
2441 memcpy(gyro_regs, gyro_sign, 3); /* copy the gyro regs */
2442 memcpy(accel_regs, accel_sign, 3); /* copy the accel regs */
2443 if ((orient & 4) != 0) /* bit 3 */
2444 {
2445 gyro_regs[0] |= 1; /* set 1 */
2446 accel_regs[0] |= 1; /* set 1 */
2447 }
2448 if ((orient & 0x20) != 0) /* bit 6 */
2449 {
2450 gyro_regs[1] |= 1; /* set 1 */
2451 accel_regs[1] |= 1; /* set 1 */
2452 }
2453 if ((orient & 0x100) != 0) /* bit 9 */
2454 {
2455 gyro_regs[2] |= 1; /* set 1 */
2456 accel_regs[2] |= 1; /* set 1 */
2457 }
2458 res = a_mpu9250_write_mem(handle, MPU9250_DMP_FCFG_3,
2459 (uint8_t *)gyro_regs, 3); /* write data */
2460 if (res != 0) /* check result */
2461 {
2462 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2463
2464 return 1; /* return error */
2465 }
2466 res = a_mpu9250_write_mem(handle, MPU9250_DMP_FCFG_7,
2467 (uint8_t *)accel_regs, 3); /* write data */
2468 if (res != 0) /* check result */
2469 {
2470 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2471
2472 return 1; /* return error */
2473 }
2474 handle->orient = orient; /* set the orient */
2475
2476 return 0; /* success return 0 */
2477}
2478
2495uint8_t mpu9250_dmp_set_feature(mpu9250_handle_t *handle, uint16_t mask)
2496{
2497 uint8_t res;
2498 uint8_t tmp[10];
2499
2500 if (handle == NULL) /* check handle */
2501 {
2502 return 2; /* return error */
2503 }
2504 if (handle->inited != 1) /* check handle initialization */
2505 {
2506 return 3; /* return error */
2507 }
2508 if (handle->dmp_inited != 1) /* check dmp initialization */
2509 {
2510 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2511
2512 return 4; /* return error */
2513 }
2514
2515 tmp[0] = (uint8_t)((MPU9250_DMP_GYRO_SF >> 24) & 0xFF); /* set the param 0 */
2516 tmp[1] = (uint8_t)((MPU9250_DMP_GYRO_SF >> 16) & 0xFF); /* set the param 1 */
2517 tmp[2] = (uint8_t)((MPU9250_DMP_GYRO_SF >> 8) & 0xFF); /* set the param 2 */
2518 tmp[3] = (uint8_t)(MPU9250_DMP_GYRO_SF & 0xFF); /* set the param 3 */
2519 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_0_104, tmp, 4); /* write data */
2520 if (res != 0) /* check result */
2521 {
2522 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2523
2524 return 1; /* return error */
2525 }
2526
2527 tmp[0] = 0xA3;
2528 if ((mask & MPU9250_DMP_FEATURE_SEND_RAW_ACCEL) != 0) /* set the raw accel */
2529 {
2530 tmp[1] = 0xC0; /* set the param 1 */
2531 tmp[2] = 0xC8; /* set the param 2 */
2532 tmp[3] = 0xC2; /* set the param 3 */
2533 }
2534 else
2535 {
2536 tmp[1] = 0xA3; /* set the param 1 */
2537 tmp[2] = 0xA3; /* set the param 2 */
2538 tmp[3] = 0xA3; /* set the param 3 */
2539 }
2540 if ((mask & MPU9250_DMP_FEATURE_SEND_ANY_GYRO) != 0) /* set any gyro */
2541 {
2542 tmp[4] = 0xC4; /* set the param 4 */
2543 tmp[5] = 0xCC; /* set the param 5 */
2544 tmp[6] = 0xC6; /* set the param 6 */
2545 }
2546 else
2547 {
2548 tmp[4] = 0xA3; /* set the param 4 */
2549 tmp[5] = 0xA3; /* set the param 5 */
2550 tmp[6] = 0xA3; /* set the param 6 */
2551 }
2552 tmp[7] = 0xA3; /* set the param 7 */
2553 tmp[8] = 0xA3; /* set the param 8 */
2554 tmp[9] = 0xA3; /* set the param 9 */
2555 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_15, tmp, 10); /* write data */
2556 if (res != 0) /* check result */
2557 {
2558 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2559
2560 return 1; /* return error */
2561 }
2562
2563 if ((mask & (MPU9250_DMP_FEATURE_TAP | MPU9250_DMP_FEATURE_ORIENT)) != 0) /* set the cfg */
2564 {
2565 tmp[0] = 0x20; /* set the param */
2566 }
2567 else
2568 {
2569 tmp[0] = 0xD8; /* set the param */
2570 }
2571 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_27, tmp, 1); /* write data */
2572 if (res != 0) /* check result */
2573 {
2574 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2575
2576 return 1; /* return error */
2577 }
2578
2579 if ((mask & MPU9250_DMP_FEATURE_GYRO_CAL) != 0) /* if true */
2580 {
2581 uint8_t regs[9] = {0xb8, 0xaa, 0xb3, 0x8d, 0xb4, 0x98, 0x0d, 0x35, 0x5d};
2582
2583 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_MOTION_BIAS, regs, 9); /* write data */
2584 if (res != 0) /* check result */
2585 {
2586 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2587
2588 return 1; /* return error */
2589 }
2590 }
2591 else
2592 {
2593 uint8_t regs[9] = {0xb8, 0xaa, 0xaa, 0xaa, 0xb0, 0x88, 0xc3, 0xc5, 0xc7};
2594
2595 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_MOTION_BIAS, regs, 9); /* write data */
2596 if (res != 0) /* check result */
2597 {
2598 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2599
2600 return 1; /* return error */
2601 }
2602 }
2603
2604 if ((mask & MPU9250_DMP_FEATURE_SEND_ANY_GYRO) != 0) /* check the gyro */
2605 {
2606 if ((mask & MPU9250_DMP_FEATURE_SEND_CAL_GYRO) != 0) /* set the cal gyro */
2607 {
2608 tmp[0] = 0xB2; /* set the param 0 */
2609 tmp[1] = 0x8B; /* set the param 1 */
2610 tmp[2] = 0xB6; /* set the param 2 */
2611 tmp[3] = 0x9B; /* set the param 3 */
2612 }
2613 else
2614 {
2615 tmp[0] = 0xC0; /* set the param 0 */
2616 tmp[1] = 0x80; /* set the param 1 */
2617 tmp[2] = 0xC2; /* set the param 2 */
2618 tmp[3] = 0x90; /* set the param 3 */
2619 }
2620 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_GYRO_RAW_DATA, tmp, 4); /* write data */
2621 if (res != 0) /* check result */
2622 {
2623 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2624
2625 return 1; /* return error */
2626 }
2627 }
2628
2629 if ((mask & MPU9250_DMP_FEATURE_TAP) != 0) /* check the tap */
2630 {
2631 uint8_t prev;
2632 uint16_t ms;
2633 uint16_t dps;
2634 uint32_t thresh_scaled;
2635 uint8_t range;
2636 uint16_t dmp_thresh;
2637 uint16_t dmp_thresh_2;
2638 float scaled_thresh;
2639
2640 tmp[0] = 0xF8; /* set the param */
2641 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_20, tmp, 1); /* write data */
2642 if (res != 0) /* check result */
2643 {
2644 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2645
2646 return 1; /* return error */
2647 }
2648
2649 scaled_thresh = (float)MPU9250_DMP_TAP_THRESH / MPU9250_DMP_SAMPLE_RATE; /* get the scaled thresh */
2650 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
2651 if (res != 0) /* check result */
2652 {
2653 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
2654
2655 return 1; /* return error */
2656 }
2657 range = ((prev >> 3) & 0x3); /* get the range */
2658 if (range == 0) /* if 2g */
2659 {
2660 dmp_thresh = (uint16_t)(scaled_thresh * 16384); /* set dmp thresh */
2661 dmp_thresh_2 = (uint16_t)(scaled_thresh * 12288); /* set dmp thresh2 */
2662 }
2663 else if (range == 1) /* if 4g */
2664 {
2665 dmp_thresh = (uint16_t)(scaled_thresh * 8192); /* set dmp thresh */
2666 dmp_thresh_2 = (uint16_t)(scaled_thresh * 6144); /* set dmp thresh2 */
2667 }
2668 else if (range == 2) /* if 8g */
2669 {
2670 dmp_thresh = (uint16_t)(scaled_thresh * 4096); /* set dmp thresh */
2671 dmp_thresh_2 = (uint16_t)(scaled_thresh * 3072); /* set dmp thresh2 */
2672 }
2673 else /* if 16g */
2674 {
2675 dmp_thresh = (uint16_t)(scaled_thresh * 2048); /* set dmp thresh */
2676 dmp_thresh_2 = (uint16_t)(scaled_thresh * 1536); /* set dmp thresh2 */
2677 }
2678 tmp[0] = (uint8_t)(dmp_thresh >> 8); /* set part 0 */
2679 tmp[1] = (uint8_t)(dmp_thresh & 0xFF); /* set part 1 */
2680 tmp[2] = (uint8_t)(dmp_thresh_2 >> 8); /* set part 2 */
2681 tmp[3] = (uint8_t)(dmp_thresh_2 & 0xFF); /* set part 3 */
2682
2683 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THX, tmp, 2); /* write tap threshold x */
2684 if (res != 0) /* check result */
2685 {
2686 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2687
2688 return 1; /* return error */
2689 }
2690 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_36, tmp + 2, 2); /* write register 36 */
2691 if (res != 0) /* check result */
2692 {
2693 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2694
2695 return 1; /* return error */
2696 }
2697
2698 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THY, tmp, 2); /* write tap threshold y */
2699 if (res != 0) /* check result */
2700 {
2701 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2702
2703 return 1; /* return error */
2704 }
2705 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_40, tmp + 2, 2); /* write register 40 */
2706 if (res != 0) /* check result */
2707 {
2708 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2709
2710 return 1; /* return error */
2711 }
2712
2713 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THZ, tmp, 2); /* write tap threshold z */
2714 if (res != 0) /* check result */
2715 {
2716 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2717
2718 return 1; /* return error */
2719 }
2720 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_44, tmp + 2, 2); /* write register 44 */
2721 if (res != 0) /* check result */
2722 {
2723 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2724
2725 return 1; /* return error */
2726 }
2727
2728 tmp[0] = 0x3F; /* enable all tap axis */
2729 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_72, tmp, 1); /* write data */
2730 if (res != 0) /* check result */
2731 {
2732 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2733
2734 return 1; /* return error */
2735 }
2736
2737 tmp[0] = MPU9250_DMP_TAP_MIN_TAP_COUNT - 1; /* set the counter */
2738 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_79, tmp, 1); /* write data */
2739 if (res != 0) /* check result */
2740 {
2741 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2742
2743 return 1; /* return error */
2744 }
2745
2746 ms = MPU9250_DMP_TAP_TIME; /* set tap time */
2747 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
2748 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
2749 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
2750
2751 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAPW_MIN, tmp, 2); /* write data */
2752 if (res != 0) /* check result */
2753 {
2754 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2755
2756 return 1; /* return error */
2757 }
2758
2759 ms = MPU9250_DMP_TAP_TIME_MULTI; /* set tap time multi */
2760 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
2761 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
2762 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
2763
2764 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_218, tmp, 2); /* write data */
2765 if (res != 0) /* check result */
2766 {
2767 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2768
2769 return 1; /* return error */
2770 }
2771
2772 dps = MPU9250_DMP_SHAKE_REJECT_THRESH; /* set the shake reject thresh */
2773 thresh_scaled = MPU9250_DMP_GYRO_SF / 1000 * dps; /* convert to thresh scaled */
2774 tmp[0] = (uint8_t)(((uint32_t)thresh_scaled >> 24) & 0xFF); /* set the part 3 */
2775 tmp[1] = (uint8_t)(((uint32_t)thresh_scaled >> 16) & 0xFF); /* set the part 2 */
2776 tmp[2] = (uint8_t)(((uint32_t)thresh_scaled >> 8) & 0xFF); /* set the part 1 */
2777 tmp[3] = (uint8_t)((uint32_t)thresh_scaled & 0xFF); /* set the part 0 */
2778
2779 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_92, tmp, 4); /* write data */
2780 if (res != 0) /* check result */
2781 {
2782 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2783
2784 return 1; /* return error */
2785 }
2786
2787 ms = MPU9250_DMP_SHAKE_REJECT_TIME; /* set the reject time */
2788 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
2789 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
2790 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
2791
2792 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_90, tmp, 2); /* write data */
2793 if (res != 0) /* check result */
2794 {
2795 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2796
2797 return 1; /* return error */
2798 }
2799
2800 ms = MPU9250_DMP_SHAKE_REJECT_TIMEOUT; /* set the reject timeout */
2801 ms /= (1000 / MPU9250_DMP_SAMPLE_RATE); /* convert time */
2802 tmp[0] = (ms >> 8) & 0xFF; /* set part 0 */
2803 tmp[1] = (ms >> 0) & 0xFF; /* set part 1 */
2804
2805 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_88, tmp, 2); /* write data */
2806 if (res != 0) /* check result */
2807 {
2808 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2809
2810 return 1; /* return error */
2811 }
2812 }
2813 else
2814 {
2815 tmp[0] = 0xD8; /* set the param */
2816 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_20, tmp, 1); /* write data */
2817 if (res != 0) /* check result */
2818 {
2819 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2820
2821 return 1; /* return error */
2822 }
2823 }
2824
2825 if ((mask & MPU9250_DMP_FEATURE_ORIENT) != 0) /* set the orient */
2826 {
2827 tmp[0] = 0xD9; /* set the param */
2828 }
2829 else
2830 {
2831 tmp[0] = 0xD8; /* set the param */
2832 }
2833 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_ORIENT_INT, tmp, 1); /* write data */
2834 if (res != 0) /* check result */
2835 {
2836 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2837
2838 return 1; /* return error */
2839 }
2840
2841 if ((mask & MPU9250_DMP_FEATURE_3X_QUAT) != 0) /* true */
2842 {
2843 uint8_t regs[4] = {0xC0, 0xC2, 0xC4, 0xC6};
2844
2845 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_LP_QUAT, regs, 4); /* write data */
2846 if (res != 0) /* check result */
2847 {
2848 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2849
2850 return 1; /* return error */
2851 }
2852 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2853 if (res != 0) /* check result */
2854 {
2855 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2856
2857 return 1; /* return error */
2858 }
2859 }
2860 else
2861 {
2862 uint8_t regs[4] = {0x8B, 0x8B, 0x8B, 0x8B};
2863
2864 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_LP_QUAT, regs, 4); /* write data */
2865 if (res != 0) /* check result */
2866 {
2867 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2868
2869 return 1; /* return error */
2870 }
2871 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2872 if (res != 0) /* check result */
2873 {
2874 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2875
2876 return 1; /* return error */
2877 }
2878 }
2879
2880 if ((mask & MPU9250_DMP_FEATURE_6X_QUAT) != 0) /* enable */
2881 {
2882 uint8_t regs[4] = {0x20, 0x28, 0x30, 0x38};
2883
2884 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_8, regs, 4); /* write data */
2885 if (res != 0) /* check result */
2886 {
2887 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2888
2889 return 1; /* return error */
2890 }
2891 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2892 if (res != 0) /* check result */
2893 {
2894 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2895
2896 return 1; /* return error */
2897 }
2898 }
2899 else
2900 {
2901 uint8_t regs[4] = {0xA3, 0xA3, 0xA3, 0xA3};
2902
2903 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_8, regs, 4); /* write data */
2904 if (res != 0) /* check result */
2905 {
2906 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2907
2908 return 1; /* return error */
2909 }
2910 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
2911 if (res != 0) /* check result */
2912 {
2913 handle->debug_print("mpu9250: reset fifo failed.\n"); /* reset fifo failed */
2914
2915 return 1; /* return error */
2916 }
2917 }
2918
2919 handle->mask = mask | MPU9250_DMP_FEATURE_PEDOMETER; /* set the mask */
2920
2921 return a_mpu9250_reset_fifo(handle); /* reset the fifo */
2922}
2923
2937uint8_t mpu9250_dmp_set_fifo_rate(mpu9250_handle_t *handle, uint16_t rate)
2938{
2939 uint8_t regs_end[12] = {0xFE, 0xF2, 0xAB,
2940 0xC4, 0xAA, 0xF1,
2941 0xDF, 0xDF, 0xBB,
2942 0xAF, 0xDF, 0xDF};
2943 uint8_t res;
2944 uint16_t d;
2945 uint8_t tmp[2];
2946
2947 if (handle == NULL) /* check handle */
2948 {
2949 return 2; /* return error */
2950 }
2951 if (handle->inited != 1) /* check handle initialization */
2952 {
2953 return 3; /* return error */
2954 }
2955 if (handle->dmp_inited != 1) /* check dmp initialization */
2956 {
2957 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
2958
2959 return 4; /* return error */
2960 }
2961 if (rate > MPU9250_DMP_SAMPLE_RATE) /* check rate */
2962 {
2963 handle->debug_print("mpu9250: rate > 200.\n"); /* rate > 200 */
2964
2965 return 5; /* return error */
2966 }
2967
2968 d = MPU9250_DMP_SAMPLE_RATE / rate - 1; /* set div */
2969 tmp[0] = (uint8_t)((d >> 8) & 0xFF); /* set tmp part0 */
2970 tmp[1] = (uint8_t)(d & 0xFF); /* set tmp part1 */
2971
2972 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_0_22, tmp, 2); /* write data */
2973 if (res != 0) /* check result */
2974 {
2975 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2976
2977 return 1; /* return error */
2978 }
2979 res = a_mpu9250_write_mem(handle, MPU9250_DMP_CFG_6,
2980 (uint8_t *)regs_end, 12); /* write data */
2981 if (res != 0) /* check result */
2982 {
2983 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
2984
2985 return 1; /* return error */
2986 }
2987
2988 return 0; /* success return 0 */
2989}
2990
3003uint8_t mpu9250_dmp_get_fifo_rate(mpu9250_handle_t *handle, uint16_t *rate)
3004{
3005 uint8_t res;
3006 uint16_t d;
3007 uint8_t tmp[2];
3008
3009 if (handle == NULL) /* check handle */
3010 {
3011 return 2; /* return error */
3012 }
3013 if (handle->inited != 1) /* check handle initialization */
3014 {
3015 return 3; /* return error */
3016 }
3017 if (handle->dmp_inited != 1) /* check dmp initialization */
3018 {
3019 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3020
3021 return 4; /* return error */
3022 }
3023
3024 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_0_22, tmp, 2); /* read data */
3025 if (res != 0) /* check result */
3026 {
3027 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3028
3029 return 1; /* return error */
3030 }
3031 d = (uint16_t)tmp[0] << 8 | tmp[1]; /* get the div */
3032 *rate = MPU9250_DMP_SAMPLE_RATE / (d + 1); /* set the rate */
3033
3034 return 0; /* success return 0 */
3035}
3036
3051{
3052 uint8_t res;
3053 uint8_t tmp;
3054 uint8_t pos;
3055
3056 if (handle == NULL) /* check handle */
3057 {
3058 return 2; /* return error */
3059 }
3060 if (handle->inited != 1) /* check handle initialization */
3061 {
3062 return 3; /* return error */
3063 }
3064 if (handle->dmp_inited != 1) /* check dmp initialization */
3065 {
3066 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3067
3068 return 4; /* return error */
3069 }
3070
3071 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_72, &tmp, 1); /* read data */
3072 if (res != 0) /* check result */
3073 {
3074 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3075
3076 return 1; /* return error */
3077 }
3078 pos = (uint8_t)((axis - 5) * 2); /* get the pos */
3079 if (enable == MPU9250_BOOL_TRUE) /* if enable */
3080 {
3081 tmp |= (3 << pos); /* enable */
3082 }
3083 else
3084 {
3085 tmp &= ~(3 << pos); /* disable */
3086 }
3087 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_72, &tmp, 1); /* write data */
3088 if (res != 0) /* check result */
3089 {
3090 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3091
3092 return 1; /* return error */
3093 }
3094
3095 return 0; /* success return 0 */
3096}
3097
3112{
3113 uint8_t res;
3114 uint8_t tmp;
3115 uint8_t pos;
3116
3117 if (handle == NULL) /* check handle */
3118 {
3119 return 2; /* return error */
3120 }
3121 if (handle->inited != 1) /* check handle initialization */
3122 {
3123 return 3; /* return error */
3124 }
3125 if (handle->dmp_inited != 1) /* check dmp initialization */
3126 {
3127 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3128
3129 return 4; /* return error */
3130 }
3131
3132 res = a_mpu9250_read_mem(handle, MPU9250_DMP_D_1_72, &tmp, 1); /* read data */
3133 if (res != 0) /* check result */
3134 {
3135 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3136
3137 return 1; /* return error */
3138 }
3139 pos = (uint8_t)((axis - 5) * 2); /* get the pos */
3140 if (((tmp >> pos) & 0x3) == 0x3) /* if enable */
3141 {
3142 *enable = MPU9250_BOOL_TRUE; /* set enable */
3143 }
3144 else
3145 {
3146 *enable = MPU9250_BOOL_FALSE; /* set disable */
3147 }
3148
3149 return 0; /* success return 0 */
3150}
3151
3167uint8_t mpu9250_dmp_set_tap_thresh(mpu9250_handle_t *handle, mpu9250_axis_t axis, uint16_t mg_ms)
3168{
3169 uint8_t res;
3170 uint8_t prev;
3171 uint8_t range;
3172 uint8_t tmp[4];
3173 uint16_t dmp_thresh;
3174 uint16_t dmp_thresh_2;
3175 float scaled_thresh;
3176
3177 if (handle == NULL) /* check handle */
3178 {
3179 return 2; /* return error */
3180 }
3181 if (handle->inited != 1) /* check handle initialization */
3182 {
3183 return 3; /* return error */
3184 }
3185 if (handle->dmp_inited != 1) /* check dmp initialization */
3186 {
3187 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3188
3189 return 4; /* return error */
3190 }
3191 if (mg_ms > 1600) /* check the mg/ms */
3192 {
3193 handle->debug_print("mpu9250: mg/ms > 1600.\n"); /* mg/ms > 1600 */
3194
3195 return 5; /* return error */
3196 }
3197
3198 scaled_thresh = (float)mg_ms / MPU9250_DMP_SAMPLE_RATE; /* get the scaled thresh */
3199 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG,
3200 (uint8_t *)&prev, 1); /* read accelerometer config */
3201 if (res != 0) /* check result */
3202 {
3203 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
3204
3205 return 1; /* return error */
3206 }
3207 range = ((prev >> 3) & 0x3); /* get the range */
3208 if (range == 0) /* if 2g */
3209 {
3210 dmp_thresh = (uint16_t)(scaled_thresh * 16384); /* set dmp thresh */
3211 dmp_thresh_2 = (uint16_t)(scaled_thresh * 12288); /* set dmp thresh2 */
3212 }
3213 else if (range == 1) /* if 4g */
3214 {
3215 dmp_thresh = (uint16_t)(scaled_thresh * 8192); /* set dmp thresh */
3216 dmp_thresh_2 = (uint16_t)(scaled_thresh * 6144); /* set dmp thresh2 */
3217 }
3218 else if (range == 2) /* if 8g */
3219 {
3220 dmp_thresh = (uint16_t)(scaled_thresh * 4096); /* set dmp thresh */
3221 dmp_thresh_2 = (uint16_t)(scaled_thresh * 3072); /* set dmp thresh2 */
3222 }
3223 else /* if 16g */
3224 {
3225 dmp_thresh = (uint16_t)(scaled_thresh * 2048); /* set dmp thresh */
3226 dmp_thresh_2 = (uint16_t)(scaled_thresh * 1536); /* set dmp thresh2 */
3227 }
3228 tmp[0] = (uint8_t)(dmp_thresh >> 8); /* set part 0 */
3229 tmp[1] = (uint8_t)(dmp_thresh & 0xFF); /* set part 1 */
3230 tmp[2] = (uint8_t)(dmp_thresh_2 >> 8); /* set part 2 */
3231 tmp[3] = (uint8_t)(dmp_thresh_2 & 0xFF); /* set part 3 */
3232
3233 if (axis == MPU9250_AXIS_X) /* if axis x */
3234 {
3235 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THX, tmp, 2); /* write tap threshold x */
3236 if (res != 0) /* check result */
3237 {
3238 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3239
3240 return 1; /* return error */
3241 }
3242 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_36, tmp + 2, 2); /* write register 36 */
3243 if (res != 0) /* check result */
3244 {
3245 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3246
3247 return 1; /* return error */
3248 }
3249
3250 return 0; /* success return 0 */
3251 }
3252 else if (axis == MPU9250_AXIS_Y) /* if axis y */
3253 {
3254 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THY, tmp, 2); /* write tap threshold y */
3255 if (res != 0) /* check result */
3256 {
3257 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3258
3259 return 1; /* return error */
3260 }
3261 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_40, tmp + 2, 2); /* write register 40 */
3262 if (res != 0) /* check result */
3263 {
3264 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3265
3266 return 1; /* return error */
3267 }
3268
3269 return 0; /* success return 0 */
3270 }
3271 else if (axis == MPU9250_AXIS_Z) /* if axis z */
3272 {
3273 res = a_mpu9250_write_mem(handle, MPU9250_DMP_TAP_THZ, tmp, 2); /* write tap threshold z */
3274 if (res != 0) /* check result */
3275 {
3276 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3277
3278 return 1; /* return error */
3279 }
3280 res = a_mpu9250_write_mem(handle, MPU9250_DMP_D_1_44, tmp + 2, 2); /* write register 44 */
3281 if (res != 0) /* check result */
3282 {
3283 handle->debug_print("mpu9250: write mem failed.\n"); /* write mem failed */
3284
3285 return 1; /* return error */
3286 }
3287
3288 return 0; /* success return 0 */
3289 }
3290 else
3291 {
3292 handle->debug_print("mpu9250: invalid axis.\n"); /* invalid axis */
3293
3294 return 6; /* return error */
3295 }
3296}
3297
3312uint8_t mpu9250_dmp_get_tap_thresh(mpu9250_handle_t *handle, mpu9250_axis_t axis, uint16_t *mg_ms)
3313{
3314 uint8_t res;
3315 uint8_t prev;
3316 uint8_t range;
3317 uint8_t tmp[2];
3318 uint16_t dmp_thresh;
3319 float scaled_thresh;
3320
3321 if (handle == NULL) /* check handle */
3322 {
3323 return 2; /* return error */
3324 }
3325 if (handle->inited != 1) /* check handle initialization */
3326 {
3327 return 3; /* return error */
3328 }
3329 if (handle->dmp_inited != 1) /* check dmp initialization */
3330 {
3331 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3332
3333 return 4; /* return error */
3334 }
3335
3336 if (axis == MPU9250_AXIS_X) /* if axis x */
3337 {
3338 res = a_mpu9250_read_mem(handle, MPU9250_DMP_TAP_THX, tmp, 2); /* read tap threshold x */
3339 if (res != 0) /* check result */
3340 {
3341 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3342
3343 return 1; /* return error */
3344 }
3345 }
3346 else if (axis == MPU9250_AXIS_Y) /* if axis y */
3347 {
3348 res = a_mpu9250_read_mem(handle, MPU9250_DMP_TAP_THY, tmp, 2); /* read tap threshold y */
3349 if (res != 0) /* check result */
3350 {
3351 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3352
3353 return 1; /* return error */
3354 }
3355 }
3356 else if (axis == MPU9250_AXIS_Z) /* if axis z */
3357 {
3358 res = a_mpu9250_read_mem(handle, MPU9250_DMP_TAP_THZ, tmp, 2); /* read tap threshold z */
3359 if (res != 0) /* check result */
3360 {
3361 handle->debug_print("mpu9250: read mem failed.\n"); /* read mem failed */
3362
3363 return 1; /* return error */
3364 }
3365 }
3366 else
3367 {
3368 handle->debug_print("mpu9250: invalid axis.\n"); /* invalid axis */
3369
3370 return 5; /* return error */
3371 }
3372 dmp_thresh = (uint16_t)tmp[0] << 8 | tmp[1]; /* set the dmp thresh */
3373
3374 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG,
3375 (uint8_t *)&prev, 1); /* read accelerometer config */
3376 if (res != 0) /* check result */
3377 {
3378 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
3379
3380 return 1; /* return error */
3381 }
3382 range = ((prev >> 3) & 0x3); /* get the range */
3383 if (range == 0) /* if 2g */
3384 {
3385 scaled_thresh = dmp_thresh / 16384.0f; /* set dmp thresh */
3386 }
3387 else if (range == 1) /* if 4g */
3388 {
3389 scaled_thresh = dmp_thresh / 8192.0f; /* set dmp thresh */
3390 }
3391 else if (range == 2) /* if 8g */
3392 {
3393 scaled_thresh = dmp_thresh / 4096.0f; /* set dmp thresh */
3394 }
3395 else /* if 16g */
3396 {
3397 scaled_thresh = dmp_thresh / 2048.0f; /* set dmp thresh */
3398 }
3399 *mg_ms = (uint16_t)(scaled_thresh * MPU9250_DMP_SAMPLE_RATE); /* set the mg/ms */
3400
3401 return 0; /* success return 0 */
3402}
3403
3429 int16_t (*accel_raw)[3], float (*accel_g)[3],
3430 int16_t (*gyro_raw)[3], float (*gyro_dps)[3],
3431 int32_t (*quat)[4],
3432 float *pitch, float *roll, float *yaw,
3433 uint16_t *l
3434 )
3435{
3436 uint8_t res;
3437 uint8_t i = 0;
3438 uint16_t len;
3439 uint8_t buf[2];
3440 uint8_t prev;
3441 uint16_t count;
3442 uint16_t j;
3443
3444 if (handle == NULL) /* check handle */
3445 {
3446 return 2; /* return error */
3447 }
3448 if (handle->inited != 1) /* check handle initialization */
3449 {
3450 return 3; /* return error */
3451 }
3452 if (handle->dmp_inited != 1) /* check dmp initialization */
3453 {
3454 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3455
3456 return 4; /* return error */
3457 }
3458
3459 res = a_mpu9250_read(handle, MPU9250_REG_INT_STATUS, (uint8_t *)&prev, 1); /* read config */
3460 if (res != 0) /* check result */
3461 {
3462 handle->debug_print("mpu9250: read int status failed.\n"); /* read int status failed */
3463
3464 return 1; /* return error */
3465 }
3466 if ((prev & (1 << MPU9250_INTERRUPT_FIFO_OVERFLOW)) != 0) /* if fifo overflow */
3467 {
3468 handle->debug_print("mpu9250: fifo overflow.\n"); /* fifo overflow */
3469 (void)a_mpu9250_reset_fifo(handle); /* reset the fifo */
3470
3471 return 6; /* return error */
3472 }
3473
3474 len = 0; /* set len 0 */
3475 if ((handle->mask & MPU9250_DMP_FEATURE_SEND_RAW_ACCEL) != 0) /* check the accel */
3476 {
3477 len += 6; /* size += 6 */
3478 }
3479 if ((handle->mask & MPU9250_DMP_FEATURE_SEND_ANY_GYRO) != 0) /* check the gyro */
3480 {
3481 len += 6; /* size += 6 */
3482 }
3483 if ((handle->mask & (MPU9250_DMP_FEATURE_3X_QUAT | MPU9250_DMP_FEATURE_6X_QUAT)) != 0) /* check the quat */
3484 {
3485 len += 16; /* size += 16 */
3486 }
3487 if ((handle->mask & (MPU9250_DMP_FEATURE_TAP | MPU9250_DMP_FEATURE_ORIENT)) != 0) /* check the tap and orient */
3488 {
3489 len += 4; /* size += 4 */
3490 }
3491 if (len == 0) /* check the len */
3492 {
3493 handle->debug_print("mpu9250: no data.\n"); /* no data */
3494
3495 return 8; /* return error */
3496 }
3497
3498 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_COUNTH, (uint8_t *)buf, 2); /* read fifo count */
3499 if (res != 0) /* check result */
3500 {
3501 handle->debug_print("mpu9250: read fifo count failed.\n"); /* read fifo count failed */
3502
3503 return 1; /* return error */
3504 }
3505 count = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set count */
3506 count = (count < 1024) ? count : 1024; /* just the counter */
3507 count = (count < (*l) * len) ? count : ((*l) *len); /* just outer buffer size */
3508 count = (count / len) * len; /* len times */
3509 *l = count / len; /* set the output length */
3510 res = a_mpu9250_read(handle, MPU9250_REG_R_W, handle->buf, count); /* read data */
3511 if (res != 0) /* check result */
3512 {
3513 handle->debug_print("mpu9250: read failed.\n"); /* read failed */
3514
3515 return 1; /* return error */
3516 }
3517 if (count < len) /* check the count */
3518 {
3519 handle->debug_print("mpu9250: fifo data is too little.\n"); /* fifo data is too little */
3520
3521 return 7; /* return error */
3522 }
3523
3524 for (j = 0; j < (*l); j++) /* (*l) times */
3525 {
3526 if ((handle->mask & (MPU9250_DMP_FEATURE_3X_QUAT | MPU9250_DMP_FEATURE_6X_QUAT)) != 0) /* check the quat */
3527 {
3528 int32_t quat_q14[4];
3529 int32_t quat_mag_sq;
3530 float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f;
3531
3532 i = 0; /* set 0 */
3533 quat[j][0] = ((int32_t)handle->buf[0 + len * j] << 24) | ((int32_t)handle->buf[1 + len * j] << 16) |
3534 ((int32_t)handle->buf[2 + len * j] << 8) | handle->buf[3 + len * j]; /* set the quat 0 */
3535 quat[j][1] = ((int32_t)handle->buf[4 + len * j] << 24) | ((int32_t)handle->buf[5 + len * j] << 16) |
3536 ((int32_t)handle->buf[6 + len * j] << 8) | handle->buf[7 + len * j]; /* set the quat 1 */
3537 quat[j][2] = ((int32_t)handle->buf[8 + len * j] << 24) | ((int32_t)handle->buf[9 + len * j] << 16) |
3538 ((int32_t)handle->buf[10 + len * j] << 8) | handle->buf[11 + len * j]; /* set the quat 2 */
3539 quat[j][3] = ((int32_t)handle->buf[12 + len * j] << 24) | ((int32_t)handle->buf[13 + len * j] << 16) |
3540 ((int32_t)handle->buf[14 + len * j] << 8) | handle->buf[15 + len * j]; /* set the quat 3 */
3541 i += 16; /* size += 16 */
3542
3543 quat_q14[0] = quat[j][0] >> 16; /* set the quat q14[0] */
3544 quat_q14[1] = quat[j][1] >> 16; /* set the quat q14[1] */
3545 quat_q14[2] = quat[j][2] >> 16; /* set the quat q14[2] */
3546 quat_q14[3] = quat[j][3] >> 16; /* set the quat q14[3] */
3547 quat_mag_sq = quat_q14[0] * quat_q14[0] + quat_q14[1] * quat_q14[1] +
3548 quat_q14[2] * quat_q14[2] + quat_q14[3] * quat_q14[3]; /* get the quat mag sq */
3549 if ((quat_mag_sq < MPU9250_DMP_QUAT_MAG_SQ_MIN) ||
3550 (quat_mag_sq > MPU9250_DMP_QUAT_MAG_SQ_MAX)) /* check the size */
3551 {
3552 handle->debug_print("mpu9250: quat check error.\n"); /* quat check error */
3553 (void)a_mpu9250_reset_fifo(handle); /* reset the fifo */
3554
3555 return 5; /* return error */
3556 }
3557 q0 = quat[j][0] / 1073741824.0f; /* set q0 */
3558 q1 = quat[j][1] / 1073741824.0f; /* set q1 */
3559 q2 = quat[j][2] / 1073741824.0f; /* set q2 */
3560 q3 = quat[j][3] / 1073741824.0f; /* set q3 */
3561 pitch[j] = asinf(-2 * q1 * q3 + 2 * q0* q2)* 57.3f; /* set pitch */
3562 roll[j] = atan2f(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2 * q2 + 1)* 57.3f; /* set roll */
3563 yaw[j] = atan2f(2 * (q1 * q2 + q0 * q3), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3) * 57.3f; /* set yaw */
3564 }
3565 else
3566 {
3567 quat[j][0] = 0; /* set 0 */
3568 quat[j][1] = 0; /* set 0 */
3569 quat[j][2] = 0; /* set 0 */
3570 quat[j][3] = 0; /* set 0 */
3571 pitch[j] = 0.0f; /* set 0.0f */
3572 roll[j] = 0.0f; /* set 0.0f */
3573 yaw[j] = 0.0f; /* set 0.0f */
3574 }
3575 if ((handle->mask & MPU9250_DMP_FEATURE_SEND_RAW_ACCEL) != 0) /* check the accel */
3576 {
3577 uint8_t accel_conf;
3578
3579 accel_raw[j][0] = ((int16_t)handle->buf[i + 0 + len * j] << 8) | handle->buf[i + 1 + len * j]; /* set the accel x raw data */
3580 accel_raw[j][1] = ((int16_t)handle->buf[i + 2 + len * j] << 8) | handle->buf[i + 3 + len * j]; /* set the accel y raw data */
3581 accel_raw[j][2] = ((int16_t)handle->buf[i + 4 + len * j] << 8) | handle->buf[i + 5 + len * j]; /* set the accel z raw data */
3582 i += 6; /* size += 6 */
3583
3584 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&accel_conf, 1); /* read accel config */
3585 if (res != 0) /* check result */
3586 {
3587 handle->debug_print("mpu9250: read accel config failed.\n"); /* read accel config failed */
3588
3589 return 1; /* return error */
3590 }
3591 accel_conf = (accel_conf >> 3) & 0x3; /* get the accel conf */
3592 if (accel_conf == 0) /* ±2g */
3593 {
3594 accel_g[j][0] = (float)(accel_raw[j][0]) / 16384.0f; /* set accel x */
3595 accel_g[j][1] = (float)(accel_raw[j][1]) / 16384.0f; /* set accel y */
3596 accel_g[j][2] = (float)(accel_raw[j][2]) / 16384.0f; /* set accel z */
3597 }
3598 else if (accel_conf == 1) /* ±4g */
3599 {
3600 accel_g[j][0] = (float)(accel_raw[j][0]) / 8192.0f; /* set accel x */
3601 accel_g[j][1] = (float)(accel_raw[j][1]) / 8192.0f; /* set accel y */
3602 accel_g[j][2] = (float)(accel_raw[j][2]) / 8192.0f; /* set accel z */
3603 }
3604 else if (accel_conf == 2) /* ±8g */
3605 {
3606 accel_g[j][0] = (float)(accel_raw[j][0]) / 4096.0f; /* set accel x */
3607 accel_g[j][1] = (float)(accel_raw[j][1]) / 4096.0f; /* set accel y */
3608 accel_g[j][2] = (float)(accel_raw[j][2]) / 4096.0f; /* set accel z */
3609 }
3610 else /* ±16g */
3611 {
3612 accel_g[j][0] = (float)(accel_raw[j][0]) / 2048.0f; /* set accel x */
3613 accel_g[j][1] = (float)(accel_raw[j][1]) / 2048.0f; /* set accel y */
3614 accel_g[j][2] = (float)(accel_raw[j][2]) / 2048.0f; /* set accel z */
3615 }
3616 }
3617 else
3618 {
3619 accel_raw[j][0] = 0; /* set 0 */
3620 accel_raw[j][1] = 0; /* set 0 */
3621 accel_raw[j][2] = 0; /* set 0 */
3622 accel_g[j][0] = 0.0f; /* set 0.0f */
3623 accel_g[j][1] = 0.0f; /* set 0.0f */
3624 accel_g[j][2] = 0.0f; /* set 0.0f */
3625 }
3626 if ((handle->mask & MPU9250_DMP_FEATURE_SEND_ANY_GYRO) != 0) /* check the gyro */
3627 {
3628 uint8_t gyro_conf;
3629
3630 gyro_raw[j][0] = ((int16_t)handle->buf[i + 0 + len * j] << 8) | handle->buf[i + 1 + len * j]; /* set the gyro x raw data */
3631 gyro_raw[j][1] = ((int16_t)handle->buf[i + 2 + len * j] << 8) | handle->buf[i + 3 + len * j]; /* set the gyro y raw data */
3632 gyro_raw[j][2] = ((int16_t)handle->buf[i + 4 + len * j] << 8) | handle->buf[i + 5 + len * j]; /* set the gyro z raw data */
3633 i += 6; /* size += 6 */
3634
3635 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&gyro_conf, 1); /* read gyro config */
3636 if (res != 0) /* check result */
3637 {
3638 handle->debug_print("mpu9250: read gyro config failed.\n"); /* read gyro config failed */
3639
3640 return 1; /* return error */
3641 }
3642 gyro_conf = (gyro_conf >> 3) & 0x3; /* get the gyro conf */
3643 if (gyro_conf == 0) /* ±250dps */
3644 {
3645 gyro_dps[j][0] = (float)(gyro_raw[j][0]) / 131.0f; /* set gyro x */
3646 gyro_dps[j][1] = (float)(gyro_raw[j][1]) / 131.0f; /* set gyro y */
3647 gyro_dps[j][2] = (float)(gyro_raw[j][2]) / 131.0f; /* set gyro z */
3648 }
3649 else if (gyro_conf == 1) /* ±500dps */
3650 {
3651 gyro_dps[j][0] = (float)(gyro_raw[j][0]) / 65.5f; /* set gyro x */
3652 gyro_dps[j][1] = (float)(gyro_raw[j][1]) / 65.5f; /* set gyro y */
3653 gyro_dps[j][2] = (float)(gyro_raw[j][2]) / 65.5f; /* set gyro z */
3654 }
3655 else if (gyro_conf == 2) /* ±1000dps */
3656 {
3657 gyro_dps[j][0] = (float)(gyro_raw[j][0]) / 32.8f; /* set gyro x */
3658 gyro_dps[j][1] = (float)(gyro_raw[j][1]) / 32.8f; /* set gyro y */
3659 gyro_dps[j][2] = (float)(gyro_raw[j][2]) / 32.8f; /* set gyro z */
3660 }
3661 else /* ±2000dps */
3662 {
3663 gyro_dps[j][0] = (float)(gyro_raw[j][0]) / 16.4f; /* set gyro x */
3664 gyro_dps[j][1] = (float)(gyro_raw[j][1]) / 16.4f; /* set gyro y */
3665 gyro_dps[j][2] = (float)(gyro_raw[j][2]) / 16.4f; /* set gyro z */
3666 }
3667 }
3668 else
3669 {
3670 gyro_raw[j][0] = 0; /* set 0 */
3671 gyro_raw[j][1] = 0; /* set 0 */
3672 gyro_raw[j][2] = 0; /* set 0 */
3673 gyro_dps[j][0] = 0.0f; /* set 0.0f */
3674 gyro_dps[j][1] = 0.0f; /* set 0.0f */
3675 gyro_dps[j][2] = 0.0f; /* set 0.0f */
3676 }
3677 if ((handle->mask & (MPU9250_DMP_FEATURE_TAP | MPU9250_DMP_FEATURE_ORIENT)) != 0) /* check the tap and orient */
3678 {
3679 a_mpu9250_dmp_decode_gesture(handle, handle->buf + i + len * j); /* run the decode gesture */
3680 }
3681 }
3682
3683 return 0; /* success return 0 */
3684}
3685
3698uint8_t mpu9250_dmp_set_tap_callback(mpu9250_handle_t *handle, void (*callback)(uint8_t count, uint8_t direction))
3699{
3700 if (handle == NULL) /* check handle */
3701 {
3702 return 2; /* return error */
3703 }
3704 if (handle->inited != 1) /* check handle initialization */
3705 {
3706 return 3; /* return error */
3707 }
3708 if (handle->dmp_inited != 1) /* check dmp initialization */
3709 {
3710 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3711
3712 return 4; /* return error */
3713 }
3714
3715 handle->dmp_tap_callback = callback; /* set the callback */
3716
3717 return 0; /* success return 0 */
3718}
3719
3732uint8_t mpu9250_dmp_set_orient_callback(mpu9250_handle_t *handle, void (*callback)(uint8_t orientation))
3733{
3734 if (handle == NULL) /* check handle */
3735 {
3736 return 2; /* return error */
3737 }
3738 if (handle->inited != 1) /* check handle initialization */
3739 {
3740 return 3; /* return error */
3741 }
3742 if (handle->dmp_inited != 1) /* check dmp initialization */
3743 {
3744 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3745
3746 return 4; /* return error */
3747 }
3748
3749 handle->dmp_orient_callback = callback; /* set the callback */
3750
3751 return 0; /* success return 0 */
3752}
3753
3767{
3768 uint8_t res;
3769 uint8_t prev;
3770
3771 if (handle == NULL) /* check handle */
3772 {
3773 return 2; /* return error */
3774 }
3775 if (handle->inited != 1) /* check handle initialization */
3776 {
3777 return 3; /* return error */
3778 }
3779 if (handle->dmp_inited != 1) /* check dmp initialization */
3780 {
3781 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3782
3783 return 4; /* return error */
3784 }
3785
3786 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
3787 if (res != 0) /* check result */
3788 {
3789 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
3790
3791 return 1; /* return error */
3792 }
3793 prev &= ~(1 << 7); /* clear config */
3794 prev |= enable << 7; /* set config */
3795 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
3796 if (res != 0) /* check result */
3797 {
3798 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
3799
3800 return 1; /* return error */
3801 }
3802
3803 return 0; /* success return 0 */
3804}
3805
3822 int32_t gyro_offset_raw[3], int32_t accel_offset_raw[3],
3823 int32_t gyro_offset[3], int32_t accel_offset[3])
3824{
3825 uint8_t res;
3826 uint8_t accel_conf;
3827 uint8_t gyro_conf;
3828
3829 if (handle == NULL) /* check handle */
3830 {
3831 return 2; /* return error */
3832 }
3833 if (handle->inited != 1) /* check handle initialization */
3834 {
3835 return 3; /* return error */
3836 }
3837 if (handle->dmp_inited != 1) /* check dmp initialization */
3838 {
3839 handle->debug_print("mpu9250: dmp is not inited.\n"); /* dmp is not inited */
3840
3841 return 4; /* return error */
3842 }
3843
3844 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&accel_conf, 1); /* read accel config */
3845 if (res != 0) /* check result */
3846 {
3847 handle->debug_print("mpu9250: read accel config failed.\n"); /* read accel config failed */
3848
3849 return 1; /* return error */
3850 }
3851 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&gyro_conf, 1); /* read gyro config */
3852 if (res != 0) /* check result */
3853 {
3854 handle->debug_print("mpu9250: read gyro config failed.\n"); /* read gyro config failed */
3855
3856 return 1; /* return error */
3857 }
3858 accel_conf = (accel_conf >> 3) & 0x3; /* get the accel conf */
3859 gyro_conf = (gyro_conf >> 3) & 0x3; /* get the gyro conf */
3860 if (accel_conf == 0) /* ±2g */
3861 {
3862 accel_offset[0] = (int32_t)(accel_offset_raw[0] * 16384.0f); /* set accel offset 0 */
3863 accel_offset[1] = (int32_t)(accel_offset_raw[1] * 16384.0f); /* set accel offset 1 */
3864 accel_offset[2] = (int32_t)(accel_offset_raw[2] * 16384.0f); /* set accel offset 2 */
3865 }
3866 else if (accel_conf == 1) /* ±4g */
3867 {
3868 accel_offset[0] = (int32_t)(accel_offset_raw[0] * 8192.0f); /* set accel offset 0 */
3869 accel_offset[1] = (int32_t)(accel_offset_raw[1] * 8192.0f); /* set accel offset 1 */
3870 accel_offset[2] = (int32_t)(accel_offset_raw[2] * 8192.0f); /* set accel offset 2 */
3871 }
3872 else if (accel_conf == 2) /* ±8g */
3873 {
3874 accel_offset[0] = (int32_t)(accel_offset_raw[0] * 4096.0f); /* set accel offset 0 */
3875 accel_offset[1] = (int32_t)(accel_offset_raw[1] * 4096.0f); /* set accel offset 1 */
3876 accel_offset[2] = (int32_t)(accel_offset_raw[2] * 4096.0f); /* set accel offset 2 */
3877 }
3878 else /* ±16g */
3879 {
3880 accel_offset[0] = (int32_t)(accel_offset_raw[0] * 2048.0f); /* set accel offset 0 */
3881 accel_offset[1] = (int32_t)(accel_offset_raw[1] * 2048.0f); /* set accel offset 1 */
3882 accel_offset[2] = (int32_t)(accel_offset_raw[2] * 2048.0f); /* set accel offset 2 */
3883 }
3884 if (gyro_conf == 0) /* ±250dps */
3885 {
3886 gyro_offset[0] = (int32_t)(gyro_offset_raw[0] * 131.f); /* set gyro offset 0 */
3887 gyro_offset[1] = (int32_t)(gyro_offset_raw[1] * 131.f); /* set gyro offset 1 */
3888 gyro_offset[2] = (int32_t)(gyro_offset_raw[2] * 131.f); /* set gyro offset 2 */
3889 }
3890 else if (gyro_conf == 1) /* ±500dps */
3891 {
3892 gyro_offset[0] = (int32_t)(gyro_offset_raw[0] * 65.5f); /* set gyro offset 0 */
3893 gyro_offset[1] = (int32_t)(gyro_offset_raw[1] * 65.5f); /* set gyro offset 1 */
3894 gyro_offset[2] = (int32_t)(gyro_offset_raw[2] * 65.5f); /* set gyro offset 2 */
3895 }
3896 else if (gyro_conf == 2) /* ±1000dps */
3897 {
3898 gyro_offset[0] = (int32_t)(gyro_offset_raw[0] * 32.8f); /* set gyro offset 0 */
3899 gyro_offset[1] = (int32_t)(gyro_offset_raw[1] * 32.8f); /* set gyro offset 1 */
3900 gyro_offset[2] = (int32_t)(gyro_offset_raw[2] * 32.8f); /* set gyro offset 2 */
3901 }
3902 else /* ±2000dps */
3903 {
3904 gyro_offset[0] = (int32_t)(gyro_offset_raw[0] * 16.4f); /* set gyro offset 0 */
3905 gyro_offset[1] = (int32_t)(gyro_offset_raw[1] * 16.4f); /* set gyro offset 1 */
3906 gyro_offset[2] = (int32_t)(gyro_offset_raw[2] * 16.4f); /* set gyro offset 2 */
3907 }
3908
3909 return 0; /* success return 0 */
3910}
3911
3922{
3923 if (handle == NULL) /* check handle */
3924 {
3925 return 2; /* return error */
3926 }
3927
3928 handle->iic_spi = (uint8_t)interface; /* set interface */
3929
3930 return 0; /* success return 0 */
3931}
3932
3943{
3944 if (handle == NULL) /* check handle */
3945 {
3946 return 2; /* return error */
3947 }
3948
3949 *interface = (mpu9250_interface_t)(handle->iic_spi); /* get interface */
3950
3951 return 0; /* success return 0 */
3952}
3953
3964{
3965 if (handle == NULL) /* check handle */
3966 {
3967 return 2; /* return error */
3968 }
3969
3970 handle->iic_addr = (uint8_t)addr_pin; /* set iic addr */
3971
3972 return 0; /* success return 0 */
3973}
3974
3985{
3986 if (handle == NULL) /* check handle */
3987 {
3988 return 2; /* return error */
3989 }
3990
3991 *addr_pin = (mpu9250_address_t)(handle->iic_addr); /* get iic addr */
3992
3993 return 0; /* success return 0 */
3994}
3995
4009{
4010 uint8_t res, prev;
4011 uint32_t timeout;
4012
4013 if (handle == NULL) /* check handle */
4014 {
4015 return 2; /* return error */
4016 }
4017 if (handle->debug_print == NULL) /* check debug_print */
4018 {
4019 return 3; /* return error */
4020 }
4021 if (handle->iic_init == NULL) /* check iic_init */
4022 {
4023 handle->debug_print("mpu9250: iic_init is null.\n"); /* iic_init is null */
4024
4025 return 3; /* return error */
4026 }
4027 if (handle->iic_deinit == NULL) /* check iic_deinit */
4028 {
4029 handle->debug_print("mpu9250: iic_deinit is null.\n"); /* iic_deinit is null */
4030
4031 return 3; /* return error */
4032 }
4033 if (handle->iic_read == NULL) /* check iic_read */
4034 {
4035 handle->debug_print("mpu9250: iic_read is null.\n"); /* iic_read is null */
4036
4037 return 3; /* return error */
4038 }
4039 if (handle->iic_write == NULL) /* check iic_write */
4040 {
4041 handle->debug_print("mpu9250: iic_write is null.\n"); /* iic_write is null */
4042
4043 return 3; /* return error */
4044 }
4045 if (handle->spi_init == NULL) /* check spi_init */
4046 {
4047 handle->debug_print("mpu9250: spi_init is null.\n"); /* spi_init is null */
4048
4049 return 3; /* return error */
4050 }
4051 if (handle->spi_deinit == NULL) /* check spi_deinit */
4052 {
4053 handle->debug_print("mpu9250: spi_deinit is null.\n"); /* spi_deinit is null */
4054
4055 return 3; /* return error */
4056 }
4057 if (handle->spi_read == NULL) /* check spi_read */
4058 {
4059 handle->debug_print("mpu9250: spi_read is null.\n"); /* spi_read is null */
4060
4061 return 3; /* return error */
4062 }
4063 if (handle->spi_write == NULL) /* check spi_write */
4064 {
4065 handle->debug_print("mpu9250: spi_write is null.\n"); /* spi_write is null */
4066
4067 return 3; /* return error */
4068 }
4069 if (handle->delay_ms == NULL) /* check delay_ms */
4070 {
4071 handle->debug_print("mpu9250: delay_ms is null.\n"); /* delay_ms is null */
4072
4073 return 3; /* return error */
4074 }
4075 if (handle->receive_callback == NULL) /* check receive_callback */
4076 {
4077 handle->debug_print("mpu9250: receive_callback is null.\n"); /* receive_callback is null */
4078
4079 return 3; /* return error */
4080 }
4081
4082 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
4083 {
4084 res = handle->iic_init(); /* iic init */
4085 if (res != 0) /* check the result */
4086 {
4087 handle->debug_print("mpu9250: iic init failed.\n"); /* iic init failed */
4088
4089 return 1; /* return error */
4090 }
4091 }
4092 else /* if spi interface */
4093 {
4094 res = handle->spi_init(); /* spi init */
4095 if (res != 0) /* check the result */
4096 {
4097 handle->debug_print("mpu9250: spi init failed.\n"); /* spi init failed */
4098
4099 return 1; /* return error */
4100 }
4101 }
4102
4103 res = a_mpu9250_read(handle, MPU9250_REG_WHO_AM_I, &prev, 1); /* read who am I */
4104 if (res != 0) /* check the result */
4105 {
4106 handle->debug_print("mpu9250: read who am i failed.\n"); /* read who am I failed */
4107 (void)a_mpu9250_deinit(handle); /* iic or spi deinit */
4108
4109 return 5; /* return error */
4110 }
4111 if (prev != 0x71) /* check the id */
4112 {
4113 handle->debug_print("mpu9250: id is invalid.\n"); /* id is invalid */
4114 (void)a_mpu9250_deinit(handle); /* iic or spi deinit */
4115
4116 return 5; /* return error */
4117 }
4118
4119 prev = 1 << 7; /* reset the device */
4120 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, &prev, 1); /* write pwr mgmt 1 */
4121 if (res != 0) /* check the result */
4122 {
4123 handle->debug_print("mpu9250: write pwr mgmt 1 failed.\n"); /* write pwr mgmt 1 failed */
4124 (void)a_mpu9250_deinit(handle); /* iic or spi deinit */
4125
4126 return 4; /* return error */
4127 }
4128 handle->delay_ms(10); /* delay 10 ms */
4129 timeout = 100; /* set the timeout 1000 ms */
4130 while (timeout != 0) /* check the timeout */
4131 {
4132 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, &prev, 1); /* read pwr mgmt 1 */
4133 if (res != 0) /* check the result */
4134 {
4135 handle->debug_print("mpu9250: read pwr mgmt 1 failed.\n"); /* read pwr mgmt 1 failed */
4136 (void)a_mpu9250_deinit(handle); /* iic or spi deinit */
4137
4138 return 4; /* return error */
4139 }
4140 if ((prev & (1 << 7)) == 0) /* check the result */
4141 {
4142 handle->inited = 1; /* flag the inited bit */
4143 handle->dmp_inited = 0; /* flag closed */
4144
4145 return 0; /* success return 0 */
4146 }
4147 handle->delay_ms(10); /* delay 10 ms */
4148 timeout--; /* timeout-- */
4149 }
4150
4151 handle->debug_print("mpu9250: reset failed.\n"); /* reset failed */
4152
4153 return 4; /* return error */
4154}
4155
4168{
4169 uint8_t res;
4170 uint8_t prev;
4171
4172 if (handle == NULL) /* check handle */
4173 {
4174 return 2; /* return error */
4175 }
4176 if (handle->inited != 1) /* check handle initialization */
4177 {
4178 return 3; /* return error */
4179 }
4180
4181 prev = (1 << 6) | (1 << 3) | (7 << 0); /* enter sleep mode */
4182 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, &prev, 1); /* write pwr mgmt 1 */
4183 if (res != 0) /* check the result */
4184 {
4185 handle->debug_print("mpu9250: write pwr mgmt 1 failed.\n"); /* write pwr mgmt 1 failed */
4186
4187 return 4; /* return error */
4188 }
4189 res = a_mpu9250_deinit(handle); /* deinit */
4190 if (res != 0) /* check the result */
4191 {
4192 handle->debug_print("mpu9250: deinit failed.\n"); /* deinit failed */
4193
4194 return 1; /* return error */
4195 }
4196 handle->inited = 0; /* flag closed */
4197 handle->dmp_inited = 0; /* flag closed */
4198
4199 return 0; /* success return 0 */
4200}
4201
4223 int16_t (*accel_raw)[3], float (*accel_g)[3],
4224 int16_t (*gyro_raw)[3], float (*gyro_dps)[3],
4225 int16_t (*mag_raw)[3], float (*mag_ut)[3],
4226 uint16_t *len
4227 )
4228{
4229 uint8_t res;
4230 uint8_t prev;
4231 uint8_t accel_conf;
4232 uint8_t gyro_conf;
4233
4234 if (handle == NULL) /* check handle */
4235 {
4236 return 2; /* return error */
4237 }
4238 if (handle->inited != 1) /* check handle initialization */
4239 {
4240 return 3; /* return error */
4241 }
4242 if ((*len) == 0) /* check length */
4243 {
4244 handle->debug_print("mpu9250: length is zero.\n"); /* length is zero */
4245
4246 return 4; /* return error */
4247 }
4248 if (handle->dmp_inited != 0) /* check dmp initialization */
4249 {
4250 handle->debug_print("mpu9250: dmp is running.\n"); /* dmp is running */
4251
4252 return 5; /* return error */
4253 }
4254
4255 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
4256 if (res != 0) /* check result */
4257 {
4258 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
4259
4260 return 1; /* return error */
4261 }
4262 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&accel_conf, 1); /* read accel config */
4263 if (res != 0) /* check result */
4264 {
4265 handle->debug_print("mpu9250: read accel config failed.\n"); /* read accel config failed */
4266
4267 return 1; /* return error */
4268 }
4269 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&gyro_conf, 1); /* read gyro config */
4270 if (res != 0) /* check result */
4271 {
4272 handle->debug_print("mpu9250: read gyro config failed.\n"); /* read gyro config failed */
4273
4274 return 1; /* return error */
4275 }
4276 accel_conf = (accel_conf >> 3) & 0x3; /* get the accel conf */
4277 gyro_conf = (gyro_conf >> 3) & 0x3; /* get the gyro conf */
4278 if ((prev & (1 << 6)) != 0) /* if fifo mode */
4279 {
4280 uint8_t conf;
4281 uint8_t buf[2];
4282 uint16_t count;
4283 uint16_t i;
4284
4285 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&conf, 1); /* read fifo enable */
4286 if (res != 0) /* check result */
4287 {
4288 handle->debug_print("mpu9250: read fifo enable failed.\n"); /* read fifo enable failed */
4289
4290 return 1; /* return error */
4291 }
4292 if ((conf != 0x78) && (conf != 0x79)) /* check the conf */
4293 {
4294 handle->debug_print("mpu9250: fifo conf is error.\n"); /* fifo conf is error */
4295
4296 return 6; /* return error */
4297 }
4298
4299 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_COUNTH, (uint8_t *)buf, 2); /* read fifo count */
4300 if (res != 0) /* check result */
4301 {
4302 handle->debug_print("mpu9250: read fifo count failed.\n"); /* read fifo count failed */
4303
4304 return 1; /* return error */
4305 }
4306
4307 if (conf == 0x78) /* only acc and gyro */
4308 {
4309 count = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set count */
4310 count = (count < 1024) ? count : 1024; /* just the counter */
4311 count = (count < ((*len) * 12)) ? count : ((*len) * 12); /* just outer buffer size */
4312 count = (count / 12) * 12; /* 12 times */
4313 *len = count / 12; /* set the output length */
4314 res = a_mpu9250_read(handle, MPU9250_REG_R_W, handle->buf, count); /* read data */
4315 if (res != 0) /* check result */
4316 {
4317 handle->debug_print("mpu9250: read failed.\n"); /* read failed */
4318
4319 return 1; /* return error */
4320 }
4321 for (i = 0; i < (*len); i++) /* *len times */
4322 {
4323 accel_raw[i][0] = (int16_t)((uint16_t)handle->buf[i * 12 + 0] << 8) |
4324 handle->buf[i * 12 + 1]; /* set raw accel x */
4325 accel_raw[i][1] = (int16_t)((uint16_t)handle->buf[i * 12 + 2] << 8) |
4326 handle->buf[i * 12 + 3]; /* set raw accel y */
4327 accel_raw[i][2] = (int16_t)((uint16_t)handle->buf[i * 12 + 4] << 8) |
4328 handle->buf[i * 12 + 5]; /* set raw accel z */
4329 gyro_raw[i][0] = (int16_t)((uint16_t)handle->buf[i * 12 + 6] << 8) |
4330 handle->buf[i * 12 + 7]; /* set raw gyro x */
4331 gyro_raw[i][1] = (int16_t)((uint16_t)handle->buf[i * 12 + 8] << 8) |
4332 handle->buf[i * 12 + 9]; /* set raw gyro y */
4333 gyro_raw[i][2] = (int16_t)((uint16_t)handle->buf[i * 12 + 10] << 8) |
4334 handle->buf[i * 12 + 11]; /* set raw gyro z */
4335 mag_raw[i][0] = 0; /* set 0 */
4336 mag_raw[i][1] = 0; /* set 0 */
4337 mag_raw[i][2] = 0; /* set 0 */
4338
4339 if (accel_conf == 0) /* ±2g */
4340 {
4341 accel_g[i][0] = (float)(accel_raw[i][0]) / 16384.0f; /* set accel x */
4342 accel_g[i][1] = (float)(accel_raw[i][1]) / 16384.0f; /* set accel y */
4343 accel_g[i][2] = (float)(accel_raw[i][2]) / 16384.0f; /* set accel z */
4344 }
4345 else if (accel_conf == 1) /* ±4g */
4346 {
4347 accel_g[i][0] = (float)(accel_raw[i][0]) / 8192.0f; /* set accel x */
4348 accel_g[i][1] = (float)(accel_raw[i][1]) / 8192.0f; /* set accel y */
4349 accel_g[i][2] = (float)(accel_raw[i][2]) / 8192.0f; /* set accel z */
4350 }
4351 else if (accel_conf == 2) /* ±8g */
4352 {
4353 accel_g[i][0] = (float)(accel_raw[i][0]) / 4096.0f; /* set accel x */
4354 accel_g[i][1] = (float)(accel_raw[i][1]) / 4096.0f; /* set accel y */
4355 accel_g[i][2] = (float)(accel_raw[i][2]) / 4096.0f; /* set accel z */
4356 }
4357 else /* ±16g */
4358 {
4359 accel_g[i][0] = (float)(accel_raw[i][0]) / 2048.0f; /* set accel x */
4360 accel_g[i][1] = (float)(accel_raw[i][1]) / 2048.0f; /* set accel y */
4361 accel_g[i][2] = (float)(accel_raw[i][2]) / 2048.0f; /* set accel z */
4362 }
4363
4364 if (gyro_conf == 0) /* ±250dps */
4365 {
4366 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 131.0f; /* set gyro x */
4367 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 131.0f; /* set gyro y */
4368 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 131.0f; /* set gyro z */
4369 }
4370 else if (gyro_conf == 1) /* ±500dps */
4371 {
4372 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 65.5f; /* set gyro x */
4373 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 65.5f; /* set gyro y */
4374 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 65.5f; /* set gyro z */
4375 }
4376 else if (gyro_conf == 2) /* ±1000dps */
4377 {
4378 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 32.8f; /* set gyro x */
4379 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 32.8f; /* set gyro y */
4380 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 32.8f; /* set gyro z */
4381 }
4382 else /* ±2000dps */
4383 {
4384 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 16.4f; /* set gyro x */
4385 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 16.4f; /* set gyro y */
4386 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 16.4f; /* set gyro z */
4387 }
4388 mag_ut[i][0] = 0.0f; /* set 0.0f */
4389 mag_ut[i][1] = 0.0f; /* set 0.0f */
4390 mag_ut[i][2] = 0.0f; /* set 0.0f */
4391 }
4392
4393 return 0; /* success return 0 */
4394 }
4395 else /* acc, gyro and mag */
4396 {
4397 float mag_k_x;
4398 float mag_k_y;
4399 float mag_k_z;
4400
4401 count = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set count */
4402 count = (count < 1024) ? count : 1024; /* just the counter */
4403 count = (count < ((*len) * 20)) ? count : ((*len) * 20); /* just outer buffer size */
4404 count = (count / 20) * 20; /* 20 times */
4405 *len = count / 20; /* set the output length */
4406 res = a_mpu9250_read(handle, MPU9250_REG_R_W, handle->buf, count); /* read data */
4407 if (res != 0) /* check result */
4408 {
4409 handle->debug_print("mpu9250: read failed.\n"); /* read failed */
4410
4411 return 1; /* return error */
4412 }
4413
4414 mag_k_x = ((float)handle->mag_asa[0] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the x sensitivity adjustment */
4415 mag_k_y = ((float)handle->mag_asa[1] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the y sensitivity adjustment */
4416 mag_k_z = ((float)handle->mag_asa[2] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the z sensitivity adjustment */
4417
4418 for (i = 0; i < (*len); i++) /* *len times */
4419 {
4420 accel_raw[i][0] = (int16_t)((uint16_t)handle->buf[i * 20 + 0] << 8) |
4421 handle->buf[i * 20 + 1]; /* set raw accel x */
4422 accel_raw[i][1] = (int16_t)((uint16_t)handle->buf[i * 20 + 2] << 8) |
4423 handle->buf[i * 20 + 3]; /* set raw accel y */
4424 accel_raw[i][2] = (int16_t)((uint16_t)handle->buf[i * 20 + 4] << 8) |
4425 handle->buf[i * 20 + 5]; /* set raw accel z */
4426 gyro_raw[i][0] = (int16_t)((uint16_t)handle->buf[i * 20 + 6] << 8) |
4427 handle->buf[i * 20 + 7]; /* set raw gyro x */
4428 gyro_raw[i][1] = (int16_t)((uint16_t)handle->buf[i * 20 + 8] << 8) |
4429 handle->buf[i * 20 + 9]; /* set raw gyro y */
4430 gyro_raw[i][2] = (int16_t)((uint16_t)handle->buf[i * 20 + 10] << 8) |
4431 handle->buf[i * 20 + 11]; /* set raw gyro z */
4432 mag_raw[i][0] = (int16_t)((uint16_t)handle->buf[i * 20 + 14] << 8) |
4433 handle->buf[i * 20 + 13]; /* set raw mag x */
4434 mag_raw[i][1] = (int16_t)((uint16_t)handle->buf[i * 20 + 16] << 8) |
4435 handle->buf[i * 20 + 15]; /* set raw mag y */
4436 mag_raw[i][2] = (int16_t)((uint16_t)handle->buf[i * 20 + 18] << 8) |
4437 handle->buf[i * 20 + 17]; /* set raw mag z */
4438
4439 if (accel_conf == 0) /* ±2g */
4440 {
4441 accel_g[i][0] = (float)(accel_raw[i][0]) / 16384.0f; /* set accel x */
4442 accel_g[i][1] = (float)(accel_raw[i][1]) / 16384.0f; /* set accel y */
4443 accel_g[i][2] = (float)(accel_raw[i][2]) / 16384.0f; /* set accel z */
4444 }
4445 else if (accel_conf == 1) /* ±4g */
4446 {
4447 accel_g[i][0] = (float)(accel_raw[i][0]) / 8192.0f; /* set accel x */
4448 accel_g[i][1] = (float)(accel_raw[i][1]) / 8192.0f; /* set accel y */
4449 accel_g[i][2] = (float)(accel_raw[i][2]) / 8192.0f; /* set accel z */
4450 }
4451 else if (accel_conf == 2) /* ±8g */
4452 {
4453 accel_g[i][0] = (float)(accel_raw[i][0]) / 4096.0f; /* set accel x */
4454 accel_g[i][1] = (float)(accel_raw[i][1]) / 4096.0f; /* set accel y */
4455 accel_g[i][2] = (float)(accel_raw[i][2]) / 4096.0f; /* set accel z */
4456 }
4457 else /* ±16g */
4458 {
4459 accel_g[i][0] = (float)(accel_raw[i][0]) / 2048.0f; /* set accel x */
4460 accel_g[i][1] = (float)(accel_raw[i][1]) / 2048.0f; /* set accel y */
4461 accel_g[i][2] = (float)(accel_raw[i][2]) / 2048.0f; /* set accel z */
4462 }
4463
4464 if (gyro_conf == 0) /* ±250dps */
4465 {
4466 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 131.0f; /* set gyro x */
4467 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 131.0f; /* set gyro y */
4468 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 131.0f; /* set gyro z */
4469 }
4470 else if (gyro_conf == 1) /* ±500dps */
4471 {
4472 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 65.5f; /* set gyro x */
4473 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 65.5f; /* set gyro y */
4474 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 65.5f; /* set gyro z */
4475 }
4476 else if (gyro_conf == 2) /* ±1000dps */
4477 {
4478 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 32.8f; /* set gyro x */
4479 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 32.8f; /* set gyro y */
4480 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 32.8f; /* set gyro z */
4481 }
4482 else /* ±2000dps */
4483 {
4484 gyro_dps[i][0] = (float)(gyro_raw[i][0]) / 16.4f; /* set gyro x */
4485 gyro_dps[i][1] = (float)(gyro_raw[i][1]) / 16.4f; /* set gyro y */
4486 gyro_dps[i][2] = (float)(gyro_raw[i][2]) / 16.4f; /* set gyro z */
4487 }
4488 if ((handle->buf[i * 20 + 19] & (1 << 4)) != 0) /* if 16 bits */
4489 {
4490 mag_ut[i][0] = (float)mag_raw[i][0] * mag_k_x * 0.15f; /* set the mag x */
4491 mag_ut[i][1] = (float)mag_raw[i][1] * mag_k_y * 0.15f; /* set the mag y */
4492 mag_ut[i][2] = (float)mag_raw[i][2] * mag_k_z * 0.15f; /* set the mag z */
4493 }
4494 else /* if 14 bits */
4495 {
4496 mag_ut[i][0] = (float)mag_raw[i][0] * mag_k_x * 0.6f; /* set the mag x */
4497 mag_ut[i][1] = (float)mag_raw[i][1] * mag_k_y * 0.6f; /* set the mag y */
4498 mag_ut[i][2] = (float)mag_raw[i][2] * mag_k_z * 0.6f; /* set the mag z */
4499 }
4500 }
4501
4502 return 0; /* success return 0 */
4503 }
4504 }
4505 else /* if normal mode */
4506 {
4507 *len = 1; /* set 1 */
4508 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_XOUT_H, handle->buf, 14); /* read data */
4509 if (res != 0) /* check result */
4510 {
4511 handle->debug_print("mpu9250: read failed.\n"); /* read failed */
4512
4513 return 1; /* return error */
4514 }
4515 accel_raw[0][0] = (int16_t)((uint16_t)handle->buf[0] << 8) | handle->buf[1]; /* set raw accel x */
4516 accel_raw[0][1] = (int16_t)((uint16_t)handle->buf[2] << 8) | handle->buf[3]; /* set raw accel y */
4517 accel_raw[0][2] = (int16_t)((uint16_t)handle->buf[4] << 8) | handle->buf[5]; /* set raw accel z */
4518 gyro_raw[0][0] = (int16_t)((uint16_t)handle->buf[8] << 8) | handle->buf[9]; /* set raw gyro x */
4519 gyro_raw[0][1] = (int16_t)((uint16_t)handle->buf[10] << 8) | handle->buf[11]; /* set raw gyro y */
4520 gyro_raw[0][2] = (int16_t)((uint16_t)handle->buf[12] << 8) | handle->buf[13]; /* set raw gyro z */
4521
4522 if (accel_conf == 0) /* ±2g */
4523 {
4524 accel_g[0][0] = (float)(accel_raw[0][0]) / 16384.0f; /* set accel x */
4525 accel_g[0][1] = (float)(accel_raw[0][1]) / 16384.0f; /* set accel y */
4526 accel_g[0][2] = (float)(accel_raw[0][2]) / 16384.0f; /* set accel z */
4527 }
4528 else if (accel_conf == 1) /* ±4g */
4529 {
4530 accel_g[0][0] = (float)(accel_raw[0][0]) / 8192.0f; /* set accel x */
4531 accel_g[0][1] = (float)(accel_raw[0][1]) / 8192.0f; /* set accel y */
4532 accel_g[0][2] = (float)(accel_raw[0][2]) / 8192.0f; /* set accel z */
4533 }
4534 else if (accel_conf == 2) /* ±8g */
4535 {
4536 accel_g[0][0] = (float)(accel_raw[0][0]) / 4096.0f; /* set accel x */
4537 accel_g[0][1] = (float)(accel_raw[0][1]) / 4096.0f; /* set accel y */
4538 accel_g[0][2] = (float)(accel_raw[0][2]) / 4096.0f; /* set accel z */
4539 }
4540 else /* ±16g */
4541 {
4542 accel_g[0][0] = (float)(accel_raw[0][0]) / 2048.0f; /* set accel x */
4543 accel_g[0][1] = (float)(accel_raw[0][1]) / 2048.0f; /* set accel y */
4544 accel_g[0][2] = (float)(accel_raw[0][2]) / 2048.0f; /* set accel z */
4545 }
4546
4547 if (gyro_conf == 0) /* ±250dps */
4548 {
4549 gyro_dps[0][0] = (float)(gyro_raw[0][0]) / 131.0f; /* set gyro x */
4550 gyro_dps[0][1] = (float)(gyro_raw[0][1]) / 131.0f; /* set gyro y */
4551 gyro_dps[0][2] = (float)(gyro_raw[0][2]) / 131.0f; /* set gyro z */
4552 }
4553 else if (gyro_conf == 1) /* ±500dps */
4554 {
4555 gyro_dps[0][0] = (float)(gyro_raw[0][0]) / 65.5f; /* set gyro x */
4556 gyro_dps[0][1] = (float)(gyro_raw[0][1]) / 65.5f; /* set gyro y */
4557 gyro_dps[0][2] = (float)(gyro_raw[0][2]) / 65.5f; /* set gyro z */
4558 }
4559 else if (gyro_conf == 2) /* ±1000dps */
4560 {
4561 gyro_dps[0][0] = (float)(gyro_raw[0][0]) / 32.8f; /* set gyro x */
4562 gyro_dps[0][1] = (float)(gyro_raw[0][1]) / 32.8f; /* set gyro y */
4563 gyro_dps[0][2] = (float)(gyro_raw[0][2]) / 32.8f; /* set gyro z */
4564 }
4565 else /* ±2000dps */
4566 {
4567 gyro_dps[0][0] = (float)(gyro_raw[0][0]) / 16.4f; /* set gyro x */
4568 gyro_dps[0][1] = (float)(gyro_raw[0][1]) / 16.4f; /* set gyro y */
4569 gyro_dps[0][2] = (float)(gyro_raw[0][2]) / 16.4f; /* set gyro z */
4570 }
4571 if (handle->mag_inited == 1) /* if mag is inited */
4572 {
4573 float mag_k_x;
4574 float mag_k_y;
4575 float mag_k_z;
4576
4577 res = a_mpu9250_mag_read(handle, AK8963_REG_ST1, handle->buf, 8); /* read the mag data */
4578 if (res != 0) /* check result */
4579 {
4580 handle->debug_print("mpu9250: mag read failed.\n"); /* mag read failed */
4581
4582 return 1; /* return error */
4583 }
4584 mag_raw[0][0] = (int16_t)((uint16_t)handle->buf[2] << 8) | handle->buf[1]; /* set raw mag x */
4585 mag_raw[0][1] = (int16_t)((uint16_t)handle->buf[4] << 8) | handle->buf[3]; /* set raw mag y */
4586 mag_raw[0][2] = (int16_t)((uint16_t)handle->buf[6] << 8) | handle->buf[5]; /* set raw mag z */
4587
4588 mag_k_x = ((float)handle->mag_asa[0] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the x sensitivity adjustment */
4589 mag_k_y = ((float)handle->mag_asa[1] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the y sensitivity adjustment */
4590 mag_k_z = ((float)handle->mag_asa[2] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the z sensitivity adjustment */
4591 if ((handle->buf[7] & (1 << 4)) != 0) /* if 16 bits */
4592 {
4593 mag_ut[0][0] = (float)mag_raw[0][0] * mag_k_x * 0.15f; /* set the mag x */
4594 mag_ut[0][1] = (float)mag_raw[0][1] * mag_k_y * 0.15f; /* set the mag y */
4595 mag_ut[0][2] = (float)mag_raw[0][2] * mag_k_z * 0.15f; /* set the mag z */
4596 }
4597 else /* if 14 bits */
4598 {
4599 mag_ut[0][0] = (float)mag_raw[0][0] * mag_k_x * 0.6f; /* set the mag x */
4600 mag_ut[0][1] = (float)mag_raw[0][1] * mag_k_y * 0.6f; /* set the mag y */
4601 mag_ut[0][2] = (float)mag_raw[0][2] * mag_k_z * 0.6f; /* set the mag z */
4602 }
4603 }
4604 else
4605 {
4606 mag_raw[0][0] = 0; /* set raw mag x 0 */
4607 mag_raw[0][1] = 0; /* set raw mag y 0 */
4608 mag_raw[0][2] = 0; /* set raw mag z 0 */
4609 mag_ut[0][0] = 0.0f; /* set mag x 0.0f */
4610 mag_ut[0][1] = 0.0f; /* set mag y 0.0f */
4611 mag_ut[0][2] = 0.0f; /* set mag z 0.0f */
4612 }
4613
4614 return 0; /* success return 0 */
4615 }
4616}
4617
4630uint8_t mpu9250_read_temperature(mpu9250_handle_t *handle, int16_t (*raw), float *degrees)
4631{
4632 uint8_t res;
4633 uint8_t buf[2];
4634
4635 if (handle == NULL) /* check handle */
4636 {
4637 return 2; /* return error */
4638 }
4639 if (handle->inited != 1) /* check handle initialization */
4640 {
4641 return 3; /* return error */
4642 }
4643
4644 res = a_mpu9250_read(handle, MPU9250_REG_TEMP_OUT_H, buf, 2); /* read data */
4645 if (res != 0) /* check result */
4646 {
4647 handle->debug_print("mpu9250: read failed.\n"); /* read failed */
4648
4649 return 1; /* return error */
4650 }
4651 *raw = (int16_t)((uint16_t)buf[0] << 8) | buf[1]; /* get the raw */
4652 *degrees = (float)(*raw) / 321.0f + 21.0f; /* convert the degrees */
4653
4654 return 0; /* success return 0 */
4655}
4656
4668{
4669 uint8_t res;
4670 uint8_t prev;
4671
4672 if (handle == NULL) /* check handle */
4673 {
4674 return 2; /* return error */
4675 }
4676 if (handle->inited != 1) /* check handle initialization */
4677 {
4678 return 3; /* return error */
4679 }
4680
4681 res = a_mpu9250_read(handle, MPU9250_REG_INT_STATUS, (uint8_t *)&prev, 1); /* read config */
4682 if (res != 0) /* check result */
4683 {
4684 handle->debug_print("mpu9250: read int status failed.\n"); /* read int status failed */
4685
4686 return 1; /* return error */
4687 }
4688 if ((prev & (1 << MPU9250_INTERRUPT_MOTION)) != 0) /* if motion */
4689 {
4690 if (handle->receive_callback != NULL) /* if receive callback */
4691 {
4692 handle->receive_callback(MPU9250_INTERRUPT_MOTION); /* run callback */
4693 }
4694 }
4695 if ((prev & (1 << MPU9250_INTERRUPT_FIFO_OVERFLOW)) != 0) /* if fifo overflow */
4696 {
4697 if (handle->receive_callback != NULL) /* if receive callback */
4698 {
4699 handle->receive_callback(MPU9250_INTERRUPT_FIFO_OVERFLOW); /* run callback */
4700 }
4701 (void)a_mpu9250_reset_fifo(handle); /* reset the fifo */
4702 }
4703 if ((prev & (1 << MPU9250_INTERRUPT_FSYNC_INT)) != 0) /* if fsync int */
4704 {
4705 if (handle->receive_callback != NULL) /* if receive callback */
4706 {
4707 handle->receive_callback(MPU9250_INTERRUPT_FSYNC_INT); /* run callback */
4708 }
4709 }
4710 if ((prev & (1 << MPU9250_INTERRUPT_DMP)) != 0) /* if dmp */
4711 {
4712 if (handle->receive_callback != NULL) /* if receive callback */
4713 {
4714 handle->receive_callback(MPU9250_INTERRUPT_DMP); /* run callback */
4715 }
4716 }
4717 if ((prev & (1 << MPU9250_INTERRUPT_DATA_READY)) != 0) /* if data ready */
4718 {
4719 if (handle->receive_callback != NULL) /* if receive callback */
4720 {
4721 handle->receive_callback(MPU9250_INTERRUPT_DATA_READY); /* run callback */
4722 }
4723 }
4724
4725 return 0; /* success return 0 */
4726}
4727
4742{
4743 uint8_t res;
4744 uint8_t prev;
4745 uint32_t timeout;
4746
4747 if (handle == NULL) /* check handle */
4748 {
4749 return 2; /* return error */
4750 }
4751 if (handle->inited != 1) /* check handle initialization */
4752 {
4753 return 3; /* return error */
4754 }
4755
4756 if (handle->iic_spi == MPU9250_INTERFACE_IIC) /* if iic interface */
4757 {
4758 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
4759 if (res != 0) /* check result */
4760 {
4761 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
4762
4763 return 1; /* return error */
4764 }
4765 prev &= ~(1 << 5); /* disable iic master */
4766 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
4767 if (res != 0) /* check result */
4768 {
4769 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
4770
4771 return 1; /* return error */
4772 }
4773 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read config */
4774 if (res != 0) /* check result */
4775 {
4776 handle->debug_print("mpu9250: read int pin cfg failed.\n"); /* read int pin cfg failed */
4777
4778 return 1; /* return error */
4779 }
4780 prev &= ~(1 << 1); /* clear config */
4781 prev |= 1 << 1; /* enable bypass */
4782 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write config */
4783 if (res != 0) /* check result */
4784 {
4785 handle->debug_print("mpu9250: write int pin cfg failed.\n"); /* write int pin cfg failed */
4786
4787 return 1; /* return error */
4788 }
4789
4790 res = a_mpu9250_mag_read(handle, AK8963_REG_WIA, (uint8_t *)&prev, 1); /* read who am I */
4791 if (res != 0) /* check result */
4792 {
4793 handle->debug_print("mpu9250: mag read who am i failed.\n"); /* mag read who am I failed */
4794
4795 return 1; /* return error */
4796 }
4797 if (prev != 0x48) /* check the id */
4798 {
4799 handle->debug_print("mpu9250: mag id is invalid.\n"); /* mag id is invalid */
4800
4801 return 5; /* return error */
4802 }
4803
4804 prev = 0x01; /* set the reset command */
4805 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL2, &prev); /* reset the magnetometer */
4806 if (res != 0) /* check result */
4807 {
4808 handle->debug_print("mpu9250: mag write cntl2 failed.\n"); /* mag write cntl2 failed */
4809
4810 return 1; /* return error */
4811 }
4812 timeout = 100; /* set timeout 1000ms */
4813 while (timeout != 0) /* check the timeout */
4814 {
4815 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL2, (uint8_t *)&prev, 1); /* read cntl2 */
4816 if (res != 0) /* check result */
4817 {
4818 handle->debug_print("mpu9250: mag read cntl2 failed.\n"); /* mag read cntl2 failed */
4819
4820 return 1; /* return error */
4821 }
4822 if ((prev & 0x01) == 0x00) /* check finished */
4823 {
4824 break; /* break */
4825 }
4826 handle->delay_ms(10); /* delay 10ms */
4827 timeout--; /* timeout-- */
4828 }
4829 if (timeout == 0) /* check the timeout */
4830 {
4831 handle->debug_print("mpu9250: reset failed.\n"); /* reset failed */
4832
4833 return 4; /* return error */
4834 }
4835
4836 prev = 0x00; /* power down */
4837 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, &prev); /* set the settings */
4838 if (res != 0) /* check result */
4839 {
4840 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
4841
4842 return 1; /* return error */
4843 }
4844 handle->delay_ms(10); /* delay 10ms */
4845 prev = 0x0F; /* fuse rom access mode */
4846 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, &prev); /* set the settings */
4847 if (res != 0) /* check result */
4848 {
4849 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
4850
4851 return 1; /* return error */
4852 }
4853 res = a_mpu9250_mag_read(handle, AK8963_REG_ASAX, handle->mag_asa, 3); /* read the asa */
4854 if (res != 0) /* check result */
4855 {
4856 handle->debug_print("mpu9250: mag read asa failed.\n"); /* mag read asa failed */
4857
4858 return 1; /* return error */
4859 }
4860 prev = 0x00; /* power down */
4861 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, &prev); /* set the settings */
4862 if (res != 0) /* check result */
4863 {
4864 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
4865
4866 return 1; /* return error */
4867 }
4868 handle->delay_ms(10); /* delay 10ms */
4869
4870 handle->mag_inited = 1; /* flag inited */
4871
4872 return 0; /* success return 0 */
4873 }
4874 else
4875 {
4876 handle->debug_print("mpu9250: spi don't support.\n"); /* spi don't support */
4877
4878 return 6; /* return error */
4879 }
4880}
4881
4894{
4895 uint8_t res;
4896 uint8_t prev;
4897
4898 if (handle == NULL) /* check handle */
4899 {
4900 return 2; /* return error */
4901 }
4902 if (handle->inited != 1) /* check handle initialization */
4903 {
4904 return 3; /* return error */
4905 }
4906 if (handle->mag_inited != 1) /* check mag initialization */
4907 {
4908 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
4909
4910 return 4; /* return error */
4911 }
4912
4913 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
4914 if (res != 0) /* check result */
4915 {
4916 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
4917
4918 return 1; /* return error */
4919 }
4920 prev &= ~(1 << 5); /* disable iic master */
4921 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
4922 if (res != 0) /* check result */
4923 {
4924 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
4925
4926 return 1; /* return error */
4927 }
4928 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read config */
4929 if (res != 0) /* check result */
4930 {
4931 handle->debug_print("mpu9250: read int pin cfg failed.\n"); /* read int pin cfg failed */
4932
4933 return 1; /* return error */
4934 }
4935 prev &= ~(1 << 1); /* clear config */
4936 prev |= 1 << 1; /* enable bypass */
4937 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write config */
4938 if (res != 0) /* check result */
4939 {
4940 handle->debug_print("mpu9250: write int pin cfg failed.\n"); /* write int pin cfg failed */
4941
4942 return 1; /* return error */
4943 }
4944
4945 res = a_mpu9250_mag_read(handle, AK8963_REG_WIA, (uint8_t *)&prev, 1); /* read who am I */
4946 if (res != 0) /* check result */
4947 {
4948 handle->debug_print("mpu9250: mag read who am i failed.\n"); /* mag read who am I failed */
4949
4950 return 1; /* return error */
4951 }
4952 if (prev != 0x48) /* check the id */
4953 {
4954 handle->debug_print("mpu9250: mag id is invalid.\n"); /* mag id is invalid */
4955
4956 return 5; /* return error */
4957 }
4958
4959 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL1, (uint8_t *)&prev, 1); /* read cntl1 */
4960 if (res != 0) /* check the result */
4961 {
4962 handle->debug_print("mpu9250: mag read cntl1 failed.\n"); /* mag read cntl1 failed */
4963
4964 return 1; /* return error */
4965 }
4966 prev &= ~(0xF << 0); /* clear config */
4967 prev |= 0x00 << 0; /* set the power mode */
4968 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, (uint8_t *)&prev); /* write cntl1 */
4969 if (res != 0) /* check the result */
4970 {
4971 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
4972
4973 return 1; /* return error */
4974 }
4975
4976 handle->mag_inited = 0; /* flag closed */
4977
4978 return 0; /* success return 0 */
4979}
4980
4994uint8_t mpu9250_mag_read(mpu9250_handle_t *handle, int16_t mag_raw[3], float mag_ut[3])
4995{
4996 uint8_t res;
4997 uint8_t buf[8];
4998 float mag_k_x;
4999 float mag_k_y;
5000 float mag_k_z;
5001
5002 if (handle == NULL) /* check handle */
5003 {
5004 return 2; /* return error */
5005 }
5006 if (handle->inited != 1) /* check handle initialization */
5007 {
5008 return 3; /* return error */
5009 }
5010 if (handle->mag_inited != 1) /* check mag initialization */
5011 {
5012 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5013
5014 return 4; /* return error */
5015 }
5016
5017 res = a_mpu9250_mag_read(handle, AK8963_REG_ST1, buf, 8); /* read the mag data */
5018 if (res != 0) /* check result */
5019 {
5020 handle->debug_print("mpu9250: mag read failed.\n"); /* mag read failed */
5021
5022 return 1; /* return error */
5023 }
5024 mag_raw[0] = (int16_t)((uint16_t)buf[2] << 8) | buf[1]; /* set raw mag x */
5025 mag_raw[1] = (int16_t)((uint16_t)buf[4] << 8) | buf[3]; /* set raw mag y */
5026 mag_raw[2] = (int16_t)((uint16_t)buf[6] << 8) | buf[5]; /* set raw mag z */
5027
5028 mag_k_x = ((float)handle->mag_asa[0] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the x sensitivity adjustment */
5029 mag_k_y = ((float)handle->mag_asa[1] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the y sensitivity adjustment */
5030 mag_k_z = ((float)handle->mag_asa[2] - 128.0f) * 0.5f / 128.0f + 1.0f; /* get the z sensitivity adjustment */
5031 if ((buf[7] & (1 << 4)) != 0) /* if 16 bits */
5032 {
5033 mag_ut[0] = (float)mag_raw[0] * mag_k_x * 0.15f; /* set the mag x */
5034 mag_ut[1] = (float)mag_raw[1] * mag_k_y * 0.15f; /* set the mag y */
5035 mag_ut[2] = (float)mag_raw[2] * mag_k_z * 0.15f; /* set the mag z */
5036 }
5037 else /* if 14 bits */
5038 {
5039 mag_ut[0] = (float)mag_raw[0] * mag_k_x * 0.6f; /* set the mag x */
5040 mag_ut[1] = (float)mag_raw[1] * mag_k_y * 0.6f; /* set the mag y */
5041 mag_ut[2] = (float)mag_raw[2] * mag_k_z * 0.6f; /* set the mag z */
5042 }
5043
5044 return 0; /* success return 0 */
5045}
5046
5059uint8_t mpu9250_mag_get_info(mpu9250_handle_t *handle, uint8_t *info)
5060{
5061 uint8_t res;
5062
5063 if (handle == NULL) /* check handle */
5064 {
5065 return 2; /* return error */
5066 }
5067 if (handle->inited != 1) /* check handle initialization */
5068 {
5069 return 3; /* return error */
5070 }
5071 if (handle->mag_inited != 1) /* check mag initialization */
5072 {
5073 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5074
5075 return 4; /* return error */
5076 }
5077
5078 res = a_mpu9250_mag_read(handle, AK8963_REG_INFO, (uint8_t *)info, 1); /* read info */
5079 if (res != 0) /* check the result */
5080 {
5081 handle->debug_print("mpu9250: mag read info failed.\n"); /* mag read info failed */
5082
5083 return 1; /* return error */
5084 }
5085
5086 return 0; /* success return 0 */
5087}
5088
5101uint8_t mpu9250_mag_get_status1(mpu9250_handle_t *handle, uint8_t *status)
5102{
5103 uint8_t res;
5104
5105 if (handle == NULL) /* check handle */
5106 {
5107 return 2; /* return error */
5108 }
5109 if (handle->inited != 1) /* check handle initialization */
5110 {
5111 return 3; /* return error */
5112 }
5113 if (handle->mag_inited != 1) /* check mag initialization */
5114 {
5115 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5116
5117 return 4; /* return error */
5118 }
5119
5120 res = a_mpu9250_mag_read(handle, AK8963_REG_ST1, (uint8_t *)status, 1); /* read status1 */
5121 if (res != 0) /* check the result */
5122 {
5123 handle->debug_print("mpu9250: mag read status1 failed.\n"); /* mag read status1 failed */
5124
5125 return 1; /* return error */
5126 }
5127
5128 return 0; /* success return 0 */
5129}
5130
5143uint8_t mpu9250_mag_get_status2(mpu9250_handle_t *handle, uint8_t *status)
5144{
5145 uint8_t res;
5146
5147 if (handle == NULL) /* check handle */
5148 {
5149 return 2; /* return error */
5150 }
5151 if (handle->inited != 1) /* check handle initialization */
5152 {
5153 return 3; /* return error */
5154 }
5155 if (handle->mag_inited != 1) /* check mag initialization */
5156 {
5157 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5158
5159 return 4; /* return error */
5160 }
5161
5162 res = a_mpu9250_mag_read(handle, AK8963_REG_ST2, (uint8_t *)status, 1); /* read status2 */
5163 if (res != 0) /* check the result */
5164 {
5165 handle->debug_print("mpu9250: mag read status2 failed.\n"); /* mag read status2 failed */
5166
5167 return 1; /* return error */
5168 }
5169
5170 return 0; /* success return 0 */
5171}
5172
5186{
5187 uint8_t res;
5188 uint8_t prev;
5189
5190 if (handle == NULL) /* check handle */
5191 {
5192 return 2; /* return error */
5193 }
5194 if (handle->inited != 1) /* check handle initialization */
5195 {
5196 return 3; /* return error */
5197 }
5198 if (handle->mag_inited != 1) /* check mag initialization */
5199 {
5200 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5201
5202 return 4; /* return error */
5203 }
5204
5205 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL1, (uint8_t *)&prev, 1); /* read cntl1 */
5206 if (res != 0) /* check the result */
5207 {
5208 handle->debug_print("mpu9250: mag read cntl1 failed.\n"); /* mag read cntl1 failed */
5209
5210 return 1; /* return error */
5211 }
5212 prev &= ~(0xF << 0); /* clear config */
5213 prev |= mode << 0; /* set the mode */
5214 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, (uint8_t *)&prev); /* write cntl1 */
5215 if (res != 0) /* check the result */
5216 {
5217 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
5218
5219 return 1; /* return error */
5220 }
5221
5222 return 0; /* success return 0 */
5223}
5224
5238{
5239 uint8_t res;
5240 uint8_t prev;
5241
5242 if (handle == NULL) /* check handle */
5243 {
5244 return 2; /* return error */
5245 }
5246 if (handle->inited != 1) /* check handle initialization */
5247 {
5248 return 3; /* return error */
5249 }
5250 if (handle->mag_inited != 1) /* check mag initialization */
5251 {
5252 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5253
5254 return 4; /* return error */
5255 }
5256
5257 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL1, (uint8_t *)&prev, 1); /* read cntl1 */
5258 if (res != 0) /* check the result */
5259 {
5260 handle->debug_print("mpu9250: mag read cntl1 failed.\n"); /* mag read cntl1 failed */
5261
5262 return 1; /* return error */
5263 }
5264 *mode = (mpu9250_magnetometer_mode_t)(prev & 0xF); /* get the mode */
5265
5266 return 0; /* success return 0 */
5267}
5268
5282{
5283 uint8_t res;
5284 uint8_t prev;
5285
5286 if (handle == NULL) /* check handle */
5287 {
5288 return 2; /* return error */
5289 }
5290 if (handle->inited != 1) /* check handle initialization */
5291 {
5292 return 3; /* return error */
5293 }
5294 if (handle->mag_inited != 1) /* check mag initialization */
5295 {
5296 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5297
5298 return 4; /* return error */
5299 }
5300
5301 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL1, (uint8_t *)&prev, 1); /* read cntl1 */
5302 if (res != 0) /* check the result */
5303 {
5304 handle->debug_print("mpu9250: mag read cntl1 failed.\n"); /* mag read cntl1 failed */
5305
5306 return 1; /* return error */
5307 }
5308 prev &= ~(1 << 4); /* clear config */
5309 prev |= bits << 4; /* set the mode */
5310 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL1, (uint8_t *)&prev); /* write cntl1 */
5311 if (res != 0) /* check the result */
5312 {
5313 handle->debug_print("mpu9250: mag write cntl1 failed.\n"); /* mag write cntl1 failed */
5314
5315 return 1; /* return error */
5316 }
5317
5318 return 0; /* success return 0 */
5319}
5320
5334{
5335 uint8_t res;
5336 uint8_t prev;
5337
5338 if (handle == NULL) /* check handle */
5339 {
5340 return 2; /* return error */
5341 }
5342 if (handle->inited != 1) /* check handle initialization */
5343 {
5344 return 3; /* return error */
5345 }
5346 if (handle->mag_inited != 1) /* check mag initialization */
5347 {
5348 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5349
5350 return 4; /* return error */
5351 }
5352
5353 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL1, (uint8_t *)&prev, 1); /* read cntl1 */
5354 if (res != 0) /* check the result */
5355 {
5356 handle->debug_print("mpu9250: mag read cntl1 failed.\n"); /* mag read cntl1 failed */
5357
5358 return 1; /* return error */
5359 }
5360 *bits = (mpu9250_magnetometer_bits_t)((prev >> 4) & 0x1); /* get the bits */
5361
5362 return 0; /* success return 0 */
5363}
5364
5378{
5379 uint8_t res;
5380 uint8_t prev;
5381
5382 if (handle == NULL) /* check handle */
5383 {
5384 return 2; /* return error */
5385 }
5386 if (handle->inited != 1) /* check handle initialization */
5387 {
5388 return 3; /* return error */
5389 }
5390 if (handle->mag_inited != 1) /* check mag initialization */
5391 {
5392 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5393
5394 return 4; /* return error */
5395 }
5396
5397 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL2, (uint8_t *)&prev, 1); /* read cntl2 */
5398 if (res != 0) /* check the result */
5399 {
5400 handle->debug_print("mpu9250: mag read cntl2 failed.\n"); /* mag read cntl2 failed */
5401
5402 return 1; /* return error */
5403 }
5404 prev &= ~(1 << 0); /* clear config */
5405 prev |= enable << 0; /* set the bool */
5406 res = a_mpu9250_mag_write(handle, AK8963_REG_CNTL2, (uint8_t *)&prev); /* write cntl2 */
5407 if (res != 0) /* check the result */
5408 {
5409 handle->debug_print("mpu9250: mag write cntl2 failed.\n"); /* mag write cntl2 failed */
5410
5411 return 1; /* return error */
5412 }
5413
5414 return 0; /* success return 0 */
5415}
5416
5430{
5431 uint8_t res;
5432 uint8_t prev;
5433
5434 if (handle == NULL) /* check handle */
5435 {
5436 return 2; /* return error */
5437 }
5438 if (handle->inited != 1) /* check handle initialization */
5439 {
5440 return 3; /* return error */
5441 }
5442 if (handle->mag_inited != 1) /* check mag initialization */
5443 {
5444 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5445
5446 return 4; /* return error */
5447 }
5448
5449 res = a_mpu9250_mag_read(handle, AK8963_REG_CNTL2, (uint8_t *)&prev, 1); /* read cntl2 */
5450 if (res != 0) /* check the result */
5451 {
5452 handle->debug_print("mpu9250: mag read cntl2 failed.\n"); /* mag read cntl2 failed */
5453
5454 return 1; /* return error */
5455 }
5456 *enable = (mpu9250_bool_t)(prev & 0x1); /* get the bool */
5457
5458 return 0; /* success return 0 */
5459}
5460
5474{
5475 uint8_t res;
5476 uint8_t prev;
5477
5478 if (handle == NULL) /* check handle */
5479 {
5480 return 2; /* return error */
5481 }
5482 if (handle->inited != 1) /* check handle initialization */
5483 {
5484 return 3; /* return error */
5485 }
5486 if (handle->mag_inited != 1) /* check mag initialization */
5487 {
5488 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5489
5490 return 4; /* return error */
5491 }
5492
5493 res = a_mpu9250_mag_read(handle, AK8963_REG_ASTC, (uint8_t *)&prev, 1); /* read astc */
5494 if (res != 0) /* check the result */
5495 {
5496 handle->debug_print("mpu9250: mag read astc failed.\n"); /* mag read astc failed */
5497
5498 return 1; /* return error */
5499 }
5500 prev &= ~(1 << 6); /* clear config */
5501 prev |= enable << 6; /* set the bool */
5502 res = a_mpu9250_mag_write(handle, AK8963_REG_ASTC, (uint8_t *)&prev); /* write astc */
5503 if (res != 0) /* check the result */
5504 {
5505 handle->debug_print("mpu9250: mag write astc failed.\n"); /* mag write astc failed */
5506
5507 return 1; /* return error */
5508 }
5509
5510 return 0; /* success return 0 */
5511}
5512
5526{
5527 uint8_t res;
5528 uint8_t prev;
5529
5530 if (handle == NULL) /* check handle */
5531 {
5532 return 2; /* return error */
5533 }
5534 if (handle->inited != 1) /* check handle initialization */
5535 {
5536 return 3; /* return error */
5537 }
5538 if (handle->mag_inited != 1) /* check mag initialization */
5539 {
5540 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5541
5542 return 4; /* return error */
5543 }
5544
5545 res = a_mpu9250_mag_read(handle, AK8963_REG_ASTC, (uint8_t *)&prev, 1); /* read astc */
5546 if (res != 0) /* check the result */
5547 {
5548 handle->debug_print("mpu9250: mag read astc failed.\n"); /* mag read astc failed */
5549
5550 return 1; /* return error */
5551 }
5552 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
5553
5554 return 0; /* success return 0 */
5555}
5556
5569{
5570 uint8_t res;
5571 uint8_t prev;
5572
5573 if (handle == NULL) /* check handle */
5574 {
5575 return 2; /* return error */
5576 }
5577 if (handle->inited != 1) /* check handle initialization */
5578 {
5579 return 3; /* return error */
5580 }
5581 if (handle->mag_inited != 1) /* check mag initialization */
5582 {
5583 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5584
5585 return 4; /* return error */
5586 }
5587
5588 prev = 0x1B; /* set the command */
5589 res = a_mpu9250_mag_write(handle, AK8963_REG_I2CDIS, (uint8_t *)&prev); /* write i2c-dis */
5590 if (res != 0) /* check the result */
5591 {
5592 handle->debug_print("mpu9250: mag write i2cdis failed.\n"); /* mag write i2c-dis failed */
5593
5594 return 1; /* return error */
5595 }
5596
5597 return 0; /* success return 0 */
5598}
5599
5612uint8_t mpu9250_mag_get_asa(mpu9250_handle_t *handle, uint8_t asa[3])
5613{
5614 uint8_t res;
5615
5616 if (handle == NULL) /* check handle */
5617 {
5618 return 2; /* return error */
5619 }
5620 if (handle->inited != 1) /* check handle initialization */
5621 {
5622 return 3; /* return error */
5623 }
5624 if (handle->mag_inited != 1) /* check mag initialization */
5625 {
5626 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5627
5628 return 4; /* return error */
5629 }
5630
5631 res = a_mpu9250_mag_read(handle, AK8963_REG_ASAX, (uint8_t *)asa, 3); /* read asa */
5632 if (res != 0) /* check the result */
5633 {
5634 handle->debug_print("mpu9250: mag read asa failed.\n"); /* mag read asa failed */
5635
5636 return 1; /* return error */
5637 }
5638
5639 return 0; /* success return 0 */
5640}
5641
5654{
5655 uint8_t res;
5656 uint8_t prev;
5657
5658 if (handle == NULL) /* check handle */
5659 {
5660 return 2; /* return error */
5661 }
5662 if (handle->inited != 1) /* check handle initialization */
5663 {
5664 return 3; /* return error */
5665 }
5666 if (handle->mag_inited != 1) /* check mag initialization */
5667 {
5668 handle->debug_print("mpu9250: mag is not inited.\n"); /* mag is not inited */
5669
5670 return 4; /* return error */
5671 }
5672
5673 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5674 if (res != 0) /* check result */
5675 {
5676 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5677
5678 return 1; /* return error */
5679 }
5680 prev &= ~(1 << 5); /* disable iic master */
5681 prev |= (1 << 5); /* enable iic master */
5682 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
5683 if (res != 0) /* check result */
5684 {
5685 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
5686
5687 return 1; /* return error */
5688 }
5689 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read config */
5690 if (res != 0) /* check result */
5691 {
5692 handle->debug_print("mpu9250: read int pin cfg failed.\n"); /* read int pin cfg failed */
5693
5694 return 1; /* return error */
5695 }
5696 prev &= ~(1 << 1); /* disable bypass */
5697 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write config */
5698 if (res != 0) /* check result */
5699 {
5700 handle->debug_print("mpu9250: write int pin cfg failed.\n"); /* write int pin cfg failed */
5701
5702 return 1; /* return error */
5703 }
5704
5705 prev = 0x80 | AK8963_IIC_ADDRESS; /* set the read mode and iic address */
5706 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* write config */
5707 if (res != 0) /* check result */
5708 {
5709 handle->debug_print("mpu9250: write iic slv0 addr failed.\n"); /* write iic slv0 addr failed */
5710
5711 return 1; /* return error */
5712 }
5713 prev = AK8963_REG_ST1; /* set the start register address */
5714 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_REG, (uint8_t *)&prev, 1); /* write config */
5715 if (res != 0) /* check result */
5716 {
5717 handle->debug_print("mpu9250: write iic slv0 reg failed.\n"); /* write iic slv0 reg failed */
5718
5719 return 1; /* return error */
5720 }
5721
5722 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* read fifo enable */
5723 if (res != 0) /* check result */
5724 {
5725 handle->debug_print("mpu9250: read fifo enable failed.\n"); /* read fifo enable failed */
5726
5727 return 1; /* return error */
5728 }
5729 prev &= ~(1 << 0); /* clear the settings */
5730 prev |= 1 << 0; /* enable slv0 fifo */
5731 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* write fifo enable ctrl */
5732 if (res != 0) /* check result */
5733 {
5734 handle->debug_print("mpu9250: write fifo enable failed.\n"); /* write fifo enable failed */
5735
5736 return 1; /* return error */
5737 }
5738
5739 prev = 0x88; /* start and read 8 bytes */
5740 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write config */
5741 if (res != 0) /* check result */
5742 {
5743 handle->debug_print("mpu9250: write iic slv0 ctrl failed.\n"); /* write iic slv0 ctrl failed */
5744
5745 return 1; /* return error */
5746 }
5747
5748 return 0; /* success return 0 */
5749}
5750
5763{
5764 uint8_t res;
5765 uint8_t prev;
5766
5767 if (handle == NULL) /* check handle */
5768 {
5769 return 2; /* return error */
5770 }
5771 if (handle->inited != 1) /* check handle initialization */
5772 {
5773 return 3; /* return error */
5774 }
5775
5776 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5777 if (res != 0) /* check result */
5778 {
5779 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5780
5781 return 1; /* return error */
5782 }
5783 prev &= ~(1 << 6); /* clear config */
5784 prev |= enable << 6; /* set config */
5785 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
5786 if (res != 0) /* check result */
5787 {
5788 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
5789
5790 return 1; /* return error */
5791 }
5792
5793 return 0; /* success return 0 */
5794}
5795
5808{
5809 uint8_t res;
5810 uint8_t prev;
5811
5812 if (handle == NULL) /* check handle */
5813 {
5814 return 2; /* return error */
5815 }
5816 if (handle->inited != 1) /* check handle initialization */
5817 {
5818 return 3; /* return error */
5819 }
5820
5821 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5822 if (res != 0) /* check result */
5823 {
5824 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5825
5826 return 1; /* return error */
5827 }
5828 *enable = (mpu9250_bool_t)((prev >> 6) & 0x01); /* get bool */
5829
5830 return 0; /* success return 0 */
5831}
5832
5844{
5845 uint8_t res;
5846
5847 if (handle == NULL) /* check handle */
5848 {
5849 return 2; /* return error */
5850 }
5851 if (handle->inited != 1) /* check handle initialization */
5852 {
5853 return 3; /* return error */
5854 }
5855
5856 res = a_mpu9250_reset_fifo(handle); /* reset the fifo */
5857 if (res != 0) /* check result */
5858 {
5859 handle->debug_print("mpu9250: force reset fifo failed.\n"); /* force reset fifo failed */
5860
5861 return 1; /* return error */
5862 }
5863
5864 return 0; /* success return 0 */
5865}
5866
5879{
5880 uint8_t res;
5881 uint8_t prev;
5882
5883 if (handle == NULL) /* check handle */
5884 {
5885 return 2; /* return error */
5886 }
5887 if (handle->inited != 1) /* check handle initialization */
5888 {
5889 return 3; /* return error */
5890 }
5891
5892 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5893 if (res != 0) /* check result */
5894 {
5895 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5896
5897 return 1; /* return error */
5898 }
5899 prev &= ~(1 << 5); /* clear config */
5900 prev |= enable << 5; /* set config */
5901 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
5902 if (res != 0) /* check result */
5903 {
5904 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
5905
5906 return 1; /* return error */
5907 }
5908
5909 return 0; /* success return 0 */
5910}
5911
5924{
5925 uint8_t res;
5926 uint8_t prev;
5927
5928 if (handle == NULL) /* check handle */
5929 {
5930 return 2; /* return error */
5931 }
5932 if (handle->inited != 1) /* check handle initialization */
5933 {
5934 return 3; /* return error */
5935 }
5936
5937 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5938 if (res != 0) /* check result */
5939 {
5940 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5941
5942 return 1; /* return error */
5943 }
5944 *enable = (mpu9250_bool_t)((prev >> 5) & 0x01); /* get bool */
5945
5946 return 0; /* success return 0 */
5947}
5948
5961{
5962 uint8_t res;
5963 uint8_t prev;
5964
5965 if (handle == NULL) /* check handle */
5966 {
5967 return 2; /* return error */
5968 }
5969 if (handle->inited != 1) /* check handle initialization */
5970 {
5971 return 3; /* return error */
5972 }
5973
5974 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
5975 if (res != 0) /* check result */
5976 {
5977 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
5978
5979 return 1; /* return error */
5980 }
5981 prev &= ~(1 << 4); /* clear config */
5982 prev |= enable << 4; /* set config */
5983 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
5984 if (res != 0) /* check result */
5985 {
5986 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
5987
5988 return 1; /* return error */
5989 }
5990
5991 return 0; /* success return 0 */
5992}
5993
6006{
6007 uint8_t res;
6008 uint8_t prev;
6009
6010 if (handle == NULL) /* check handle */
6011 {
6012 return 2; /* return error */
6013 }
6014 if (handle->inited != 1) /* check handle initialization */
6015 {
6016 return 3; /* return error */
6017 }
6018
6019 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6020 if (res != 0) /* check result */
6021 {
6022 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6023
6024 return 1; /* return error */
6025 }
6026 *enable = (mpu9250_bool_t)((prev >> 4) & 0x01); /* get bool */
6027
6028 return 0; /* success return 0 */
6029}
6030
6042{
6043 uint8_t res;
6044 uint8_t prev;
6045
6046 if (handle == NULL) /* check handle */
6047 {
6048 return 2; /* return error */
6049 }
6050 if (handle->inited != 1) /* check handle initialization */
6051 {
6052 return 3; /* return error */
6053 }
6054
6055 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6056 if (res != 0) /* check result */
6057 {
6058 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6059
6060 return 1; /* return error */
6061 }
6062 prev &= ~(1 << 2); /* clear config */
6063 prev |= 1 << 2; /* set config */
6064 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
6065 if (res != 0) /* check result */
6066 {
6067 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
6068
6069 return 1; /* return error */
6070 }
6071
6072 return 0; /* success return 0 */
6073}
6074
6087{
6088 uint8_t res;
6089 uint8_t prev;
6090
6091 if (handle == NULL) /* check handle */
6092 {
6093 return 2; /* return error */
6094 }
6095 if (handle->inited != 1) /* check handle initialization */
6096 {
6097 return 3; /* return error */
6098 }
6099
6100 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6101 if (res != 0) /* check result */
6102 {
6103 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6104
6105 return 1; /* return error */
6106 }
6107 *enable = (mpu9250_bool_t)((prev >> 2) & 0x01); /* get bool */
6108
6109 return 0; /* success return 0 */
6110}
6111
6123{
6124 uint8_t res;
6125 uint8_t prev;
6126
6127 if (handle == NULL) /* check handle */
6128 {
6129 return 2; /* return error */
6130 }
6131 if (handle->inited != 1) /* check handle initialization */
6132 {
6133 return 3; /* return error */
6134 }
6135
6136 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6137 if (res != 0) /* check result */
6138 {
6139 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6140
6141 return 1; /* return error */
6142 }
6143 prev &= ~(1 << 1); /* clear config */
6144 prev |= 1 << 1; /* set config */
6145 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
6146 if (res != 0) /* check result */
6147 {
6148 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
6149
6150 return 1; /* return error */
6151 }
6152
6153 return 0; /* success return 0 */
6154}
6155
6168{
6169 uint8_t res;
6170 uint8_t prev;
6171
6172 if (handle == NULL) /* check handle */
6173 {
6174 return 2; /* return error */
6175 }
6176 if (handle->inited != 1) /* check handle initialization */
6177 {
6178 return 3; /* return error */
6179 }
6180
6181 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6182 if (res != 0) /* check result */
6183 {
6184 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6185
6186 return 1; /* return error */
6187 }
6188 *enable = (mpu9250_bool_t)((prev >> 1) & 0x01); /* get bool */
6189
6190 return 0; /* success return 0 */
6191}
6192
6204{
6205 uint8_t res;
6206 uint8_t prev;
6207
6208 if (handle == NULL) /* check handle */
6209 {
6210 return 2; /* return error */
6211 }
6212 if (handle->inited != 1) /* check handle initialization */
6213 {
6214 return 3; /* return error */
6215 }
6216
6217 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6218 if (res != 0) /* check result */
6219 {
6220 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6221
6222 return 1; /* return error */
6223 }
6224 prev &= ~(1 << 0); /* clear config */
6225 prev |= 1 << 0; /* set config */
6226 res = a_mpu9250_write(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* write config */
6227 if (res != 0) /* check result */
6228 {
6229 handle->debug_print("mpu9250: write user ctrl failed.\n"); /* write user ctrl failed */
6230
6231 return 1; /* return error */
6232 }
6233
6234 return 0; /* success return 0 */
6235}
6236
6249{
6250 uint8_t res;
6251 uint8_t prev;
6252
6253 if (handle == NULL) /* check handle */
6254 {
6255 return 2; /* return error */
6256 }
6257 if (handle->inited != 1) /* check handle initialization */
6258 {
6259 return 3; /* return error */
6260 }
6261
6262 res = a_mpu9250_read(handle, MPU9250_REG_USER_CTRL, (uint8_t *)&prev, 1); /* read config */
6263 if (res != 0) /* check result */
6264 {
6265 handle->debug_print("mpu9250: read user ctrl failed.\n"); /* read user ctrl failed */
6266
6267 return 1; /* return error */
6268 }
6269 *enable = (mpu9250_bool_t)((prev >> 0) & 0x01); /* get bool */
6270
6271 return 0; /* success return 0 */
6272}
6273
6285{
6286 uint8_t res;
6287 uint8_t prev;
6288
6289 if (handle == NULL) /* check handle */
6290 {
6291 return 2; /* return error */
6292 }
6293 if (handle->inited != 1) /* check handle initialization */
6294 {
6295 return 3; /* return error */
6296 }
6297
6298 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6299 if (res != 0) /* check result */
6300 {
6301 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6302
6303 return 1; /* return error */
6304 }
6305 prev &= ~(1 << 7); /* clear config */
6306 prev |= 1 << 7; /* set config */
6307 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6308 if (res != 0) /* check result */
6309 {
6310 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6311
6312 return 1; /* return error */
6313 }
6314
6315 return 0; /* success return 0 */
6316}
6317
6330{
6331 uint8_t res;
6332 uint8_t prev;
6333
6334 if (handle == NULL) /* check handle */
6335 {
6336 return 2; /* return error */
6337 }
6338 if (handle->inited != 1) /* check handle initialization */
6339 {
6340 return 3; /* return error */
6341 }
6342
6343 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6344 if (res != 0) /* check result */
6345 {
6346 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6347
6348 return 1; /* return error */
6349 }
6350 *enable = (mpu9250_bool_t)((prev >> 7) & 0x01); /* get bool */
6351
6352 return 0; /* success return 0 */
6353}
6354
6367{
6368 uint8_t res;
6369 uint8_t prev;
6370
6371 if (handle == NULL) /* check handle */
6372 {
6373 return 2; /* return error */
6374 }
6375 if (handle->inited != 1) /* check handle initialization */
6376 {
6377 return 3; /* return error */
6378 }
6379
6380 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6381 if (res != 0) /* check result */
6382 {
6383 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6384
6385 return 1; /* return error */
6386 }
6387 prev &= ~(0x7 << 0); /* clear config */
6388 prev |= clock_source << 0; /* set config */
6389 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6390 if (res != 0) /* check result */
6391 {
6392 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6393
6394 return 1; /* return error */
6395 }
6396
6397 return 0; /* success return 0 */
6398}
6399
6412{
6413 uint8_t res;
6414 uint8_t prev;
6415
6416 if (handle == NULL) /* check handle */
6417 {
6418 return 2; /* return error */
6419 }
6420 if (handle->inited != 1) /* check handle initialization */
6421 {
6422 return 3; /* return error */
6423 }
6424
6425 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6426 if (res != 0) /* check result */
6427 {
6428 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6429
6430 return 1; /* return error */
6431 }
6432 *clock_source = (mpu9250_clock_source_t)((prev >> 0) & (0x7)); /* get clock source */
6433
6434 return 0; /* success return 0 */
6435}
6436
6449{
6450 uint8_t res;
6451 uint8_t prev;
6452
6453 if (handle == NULL) /* check handle */
6454 {
6455 return 2; /* return error */
6456 }
6457 if (handle->inited != 1) /* check handle initialization */
6458 {
6459 return 3; /* return error */
6460 }
6461
6462 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6463 if (res != 0) /* check result */
6464 {
6465 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6466
6467 return 1; /* return error */
6468 }
6469 prev &= ~(1 << 3); /* clear config */
6470 prev |= (!enable) << 3; /* set config */
6471 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6472 if (res != 0) /* check result */
6473 {
6474 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6475
6476 return 1; /* return error */
6477 }
6478
6479 return 0; /* success return 0 */
6480}
6481
6494{
6495 uint8_t res;
6496 uint8_t prev;
6497
6498 if (handle == NULL) /* check handle */
6499 {
6500 return 2; /* return error */
6501 }
6502 if (handle->inited != 1) /* check handle initialization */
6503 {
6504 return 3; /* return error */
6505 }
6506
6507 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6508 if (res != 0) /* check result */
6509 {
6510 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6511
6512 return 1; /* return error */
6513 }
6514 *enable = (mpu9250_bool_t)(!((prev >> 3) & (0x1))); /* get bool */
6515
6516 return 0; /* success return 0 */
6517}
6518
6531{
6532 uint8_t res;
6533 uint8_t prev;
6534
6535 if (handle == NULL) /* check handle */
6536 {
6537 return 2; /* return error */
6538 }
6539 if (handle->inited != 1) /* check handle initialization */
6540 {
6541 return 3; /* return error */
6542 }
6543
6544 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6545 if (res != 0) /* check result */
6546 {
6547 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6548
6549 return 1; /* return error */
6550 }
6551 prev &= ~(1 << 5); /* clear config */
6552 prev |= enable << 5; /* set config */
6553 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6554 if (res != 0) /* check result */
6555 {
6556 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6557
6558 return 1; /* return error */
6559 }
6560
6561 return 0; /* success return 0 */
6562}
6563
6576{
6577 uint8_t res;
6578 uint8_t prev;
6579
6580 if (handle == NULL) /* check handle */
6581 {
6582 return 2; /* return error */
6583 }
6584 if (handle->inited != 1) /* check handle initialization */
6585 {
6586 return 3; /* return error */
6587 }
6588
6589 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6590 if (res != 0) /* check result */
6591 {
6592 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6593
6594 return 1; /* return error */
6595 }
6596 *enable = (mpu9250_bool_t)((prev >> 5) & (0x1)); /* get bool */
6597
6598 return 0; /* success return 0 */
6599}
6600
6613{
6614 uint8_t res;
6615 uint8_t prev;
6616
6617 if (handle == NULL) /* check handle */
6618 {
6619 return 2; /* return error */
6620 }
6621 if (handle->inited != 1) /* check handle initialization */
6622 {
6623 return 3; /* return error */
6624 }
6625
6626 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6627 if (res != 0) /* check result */
6628 {
6629 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6630
6631 return 1; /* return error */
6632 }
6633 prev &= ~(1 << 6); /* clear config */
6634 prev |= enable << 6; /* set config */
6635 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6636 if (res != 0) /* check result */
6637 {
6638 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6639
6640 return 1; /* return error */
6641 }
6642
6643 return 0; /* success return 0 */
6644}
6645
6658{
6659 uint8_t res;
6660 uint8_t prev;
6661
6662 if (handle == NULL) /* check handle */
6663 {
6664 return 2; /* return error */
6665 }
6666 if (handle->inited != 1) /* check handle initialization */
6667 {
6668 return 3; /* return error */
6669 }
6670
6671 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6672 if (res != 0) /* check result */
6673 {
6674 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6675
6676 return 1; /* return error */
6677 }
6678 *enable = (mpu9250_bool_t)((prev >> 6) & (0x1)); /* get bool */
6679
6680 return 0; /* success return 0 */
6681}
6682
6695{
6696 uint8_t res;
6697 uint8_t prev;
6698
6699 if (handle == NULL) /* check handle */
6700 {
6701 return 2; /* return error */
6702 }
6703 if (handle->inited != 1) /* check handle initialization */
6704 {
6705 return 3; /* return error */
6706 }
6707
6708 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6709 if (res != 0) /* check result */
6710 {
6711 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6712
6713 return 1; /* return error */
6714 }
6715 prev &= ~(1 << 4); /* clear config */
6716 prev |= enable << 4; /* set config */
6717 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
6718 if (res != 0) /* check result */
6719 {
6720 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
6721
6722 return 1; /* return error */
6723 }
6724
6725 return 0; /* success return 0 */
6726}
6727
6740{
6741 uint8_t res;
6742 uint8_t prev;
6743
6744 if (handle == NULL) /* check handle */
6745 {
6746 return 2; /* return error */
6747 }
6748 if (handle->inited != 1) /* check handle initialization */
6749 {
6750 return 3; /* return error */
6751 }
6752
6753 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
6754 if (res != 0) /* check result */
6755 {
6756 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
6757
6758 return 1; /* return error */
6759 }
6760 *enable = (mpu9250_bool_t)((prev >> 4) & (0x1)); /* get bool */
6761
6762 return 0; /* success return 0 */
6763}
6764
6778{
6779 uint8_t res;
6780 uint8_t prev;
6781
6782 if (handle == NULL) /* check handle */
6783 {
6784 return 2; /* return error */
6785 }
6786 if (handle->inited != 1) /* check handle initialization */
6787 {
6788 return 3; /* return error */
6789 }
6790
6791 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_2, (uint8_t *)&prev, 1); /* read config */
6792 if (res != 0) /* check result */
6793 {
6794 handle->debug_print("mpu9250: read power management 2 failed.\n"); /* read power management 2 failed */
6795
6796 return 1; /* return error */
6797 }
6798 prev &= ~(1 << source); /* clear config */
6799 prev |= enable << source; /* set config */
6800 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_2, (uint8_t *)&prev, 1); /* write config */
6801 if (res != 0) /* check result */
6802 {
6803 handle->debug_print("mpu9250: write power management 2 failed.\n"); /* write power management 2 failed */
6804
6805 return 1; /* return error */
6806 }
6807
6808 return 0; /* success return 0 */
6809}
6810
6824{
6825 uint8_t res;
6826 uint8_t prev;
6827
6828 if (handle == NULL) /* check handle */
6829 {
6830 return 2; /* return error */
6831 }
6832 if (handle->inited != 1) /* check handle initialization */
6833 {
6834 return 3; /* return error */
6835 }
6836
6837 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_2, (uint8_t *)&prev, 1); /* read config */
6838 if (res != 0) /* check result */
6839 {
6840 handle->debug_print("mpu9250: read power management 2 failed.\n"); /* read power management 2 failed */
6841
6842 return 1; /* return error */
6843 }
6844 *enable = (mpu9250_bool_t)((prev >> source) & (0x1)); /* get bool */
6845
6846 return 0; /* success return 0 */
6847}
6848
6860uint8_t mpu9250_get_fifo_count(mpu9250_handle_t *handle, uint16_t* count)
6861{
6862 uint8_t res;
6863 uint8_t buf[2];
6864
6865 if (handle == NULL) /* check handle */
6866 {
6867 return 2; /* return error */
6868 }
6869 if (handle->inited != 1) /* check handle initialization */
6870 {
6871 return 3; /* return error */
6872 }
6873
6874 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_COUNTH, (uint8_t *)buf, 2); /* read fifo count */
6875 if (res != 0) /* check result */
6876 {
6877 handle->debug_print("mpu9250: read fifo count failed.\n"); /* read fifo count failed */
6878
6879 return 1; /* return error */
6880 }
6881 *count = (uint16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set count */
6882
6883 return 0; /* success return 0 */
6884}
6885
6898uint8_t mpu9250_fifo_get(mpu9250_handle_t *handle, uint8_t *buf, uint16_t len)
6899{
6900 uint8_t res;
6901
6902 if (handle == NULL) /* check handle */
6903 {
6904 return 2; /* return error */
6905 }
6906 if (handle->inited != 1) /* check handle initialization */
6907 {
6908 return 3; /* return error */
6909 }
6910
6911 res = a_mpu9250_read(handle, MPU9250_REG_R_W, buf, len); /* read fifo */
6912 if (res != 0) /* check result */
6913 {
6914 handle->debug_print("mpu9250: fifo read failed.\n"); /* fifo read failed */
6915
6916 return 1; /* return error */
6917 }
6918
6919 return 0; /* success return 0 */
6920}
6921
6934uint8_t mpu9250_fifo_set(mpu9250_handle_t *handle, uint8_t *buf, uint16_t len)
6935{
6936 uint8_t res;
6937
6938 if (handle == NULL) /* check handle */
6939 {
6940 return 2; /* return error */
6941 }
6942 if (handle->inited != 1) /* check handle initialization */
6943 {
6944 return 3; /* return error */
6945 }
6946
6947 res = a_mpu9250_write(handle, MPU9250_REG_R_W, buf, len); /* write fifo */
6948 if (res != 0) /* check result */
6949 {
6950 handle->debug_print("mpu9250: fifo write failed.\n"); /* fifo write failed */
6951
6952 return 1; /* return error */
6953 }
6954
6955 return 0; /* success return 0 */
6956}
6957
6970{
6971 uint8_t res;
6972 uint8_t prev;
6973
6974 if (handle == NULL) /* check handle */
6975 {
6976 return 2; /* return error */
6977 }
6978 if (handle->inited != 1) /* check handle initialization */
6979 {
6980 return 3; /* return error */
6981 }
6982
6983 res = a_mpu9250_read(handle, MPU9250_REG_SIGNAL_PATH_RESET, (uint8_t *)&prev, 1); /* read config */
6984 if (res != 0) /* check result */
6985 {
6986 handle->debug_print("mpu9250: read signal path reset failed.\n"); /* read signal path reset failed */
6987
6988 return 1; /* return error */
6989 }
6990 prev &= ~(1 << path); /* clear config */
6991 prev |= 1 << path; /* set config */
6992 res = a_mpu9250_write(handle, MPU9250_REG_SIGNAL_PATH_RESET, (uint8_t *)&prev, 1); /* write config */
6993 if (res != 0) /* check result */
6994 {
6995 handle->debug_print("mpu9250: write signal path reset failed.\n"); /* write signal path reset failed */
6996
6997 return 1; /* return error */
6998 }
6999
7000 return 0; /* success return 0 */
7001}
7002
7015{
7016 uint8_t res;
7017
7018 if (handle == NULL) /* check handle */
7019 {
7020 return 2; /* return error */
7021 }
7022 if (handle->inited != 1) /* check handle initialization */
7023 {
7024 return 3; /* return error */
7025 }
7026
7027 res = a_mpu9250_write(handle, MPU9250_REG_SMPRT_DIV, (uint8_t *)&d, 1); /* write config */
7028 if (res != 0) /* check result */
7029 {
7030 handle->debug_print("mpu9250: write smprt div failed.\n"); /* write smprt div failed */
7031
7032 return 1; /* return error */
7033 }
7034
7035 return 0; /* success return 0 */
7036}
7037
7050{
7051 uint8_t res;
7052
7053 if (handle == NULL) /* check handle */
7054 {
7055 return 2; /* return error */
7056 }
7057 if (handle->inited != 1) /* check handle initialization */
7058 {
7059 return 3; /* return error */
7060 }
7061
7062 res = a_mpu9250_read(handle, MPU9250_REG_SMPRT_DIV, (uint8_t *)d, 1); /* read config */
7063 if (res != 0) /* check result */
7064 {
7065 handle->debug_print("mpu9250: read smprt div failed.\n"); /* read smprt div failed */
7066
7067 return 1; /* return error */
7068 }
7069
7070 return 0; /* success return 0 */
7071}
7072
7085{
7086 uint8_t res;
7087 uint8_t prev;
7088
7089 if (handle == NULL) /* check handle */
7090 {
7091 return 2; /* return error */
7092 }
7093 if (handle->inited != 1) /* check handle initialization */
7094 {
7095 return 3; /* return error */
7096 }
7097
7098 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7099 if (res != 0) /* check result */
7100 {
7101 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7102
7103 return 1; /* return error */
7104 }
7105 prev &= ~(0x7 << 3); /* clear config */
7106 prev |= sync << 3; /* set config */
7107 res = a_mpu9250_write(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* write config */
7108 if (res != 0) /* check result */
7109 {
7110 handle->debug_print("mpu9250: write config failed.\n"); /* write config failed */
7111
7112 return 1; /* return error */
7113 }
7114
7115 return 0; /* success return 0 */
7116}
7117
7130{
7131 uint8_t res;
7132 uint8_t prev;
7133
7134 if (handle == NULL) /* check handle */
7135 {
7136 return 2; /* return error */
7137 }
7138 if (handle->inited != 1) /* check handle initialization */
7139 {
7140 return 3; /* return error */
7141 }
7142
7143 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7144 if (res != 0) /* check result */
7145 {
7146 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7147
7148 return 1; /* return error */
7149 }
7150 *sync = (mpu9250_extern_sync_t)((prev >> 3) & 0x7); /* get the extern sync */
7151
7152 return 0; /* success return 0 */
7153}
7154
7167{
7168 uint8_t res;
7169 uint8_t prev;
7170
7171 if (handle == NULL) /* check handle */
7172 {
7173 return 2; /* return error */
7174 }
7175 if (handle->inited != 1) /* check handle initialization */
7176 {
7177 return 3; /* return error */
7178 }
7179
7180 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7181 if (res != 0) /* check result */
7182 {
7183 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7184
7185 return 1; /* return error */
7186 }
7187 prev &= ~(0x7 << 0); /* clear config */
7188 prev |= filter << 0; /* set config */
7189 res = a_mpu9250_write(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* write config */
7190 if (res != 0) /* check result */
7191 {
7192 handle->debug_print("mpu9250: write config failed.\n"); /* write config failed */
7193
7194 return 1; /* return error */
7195 }
7196
7197 return 0; /* success return 0 */
7198}
7199
7212{
7213 uint8_t res;
7214 uint8_t prev;
7215
7216 if (handle == NULL) /* check handle */
7217 {
7218 return 2; /* return error */
7219 }
7220 if (handle->inited != 1) /* check handle initialization */
7221 {
7222 return 3; /* return error */
7223 }
7224
7225 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7226 if (res != 0) /* check result */
7227 {
7228 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7229
7230 return 1; /* return error */
7231 }
7232 *filter = (mpu9250_low_pass_filter_t)((prev >> 0) & 0x7); /* get the filter */
7233
7234 return 0; /* success return 0 */
7235}
7236
7249{
7250 uint8_t res;
7251 uint8_t prev;
7252
7253 if (handle == NULL) /* check handle */
7254 {
7255 return 2; /* return error */
7256 }
7257 if (handle->inited != 1) /* check handle initialization */
7258 {
7259 return 3; /* return error */
7260 }
7261
7262 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7263 if (res != 0) /* check result */
7264 {
7265 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7266
7267 return 1; /* return error */
7268 }
7269 prev &= ~(1 << 6); /* clear config */
7270 prev |= mode << 6; /* set config */
7271 res = a_mpu9250_write(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* write config */
7272 if (res != 0) /* check result */
7273 {
7274 handle->debug_print("mpu9250: write config failed.\n"); /* write config failed */
7275
7276 return 1; /* return error */
7277 }
7278
7279 return 0; /* success return 0 */
7280}
7281
7294{
7295 uint8_t res;
7296 uint8_t prev;
7297
7298 if (handle == NULL) /* check handle */
7299 {
7300 return 2; /* return error */
7301 }
7302 if (handle->inited != 1) /* check handle initialization */
7303 {
7304 return 3; /* return error */
7305 }
7306
7307 res = a_mpu9250_read(handle, MPU9250_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
7308 if (res != 0) /* check result */
7309 {
7310 handle->debug_print("mpu9250: read config failed.\n"); /* read config failed */
7311
7312 return 1; /* return error */
7313 }
7314 *mode = (mpu9250_fifo_mode)((prev >> 6) & 0x1); /* get the mode */
7315
7316 return 0; /* success return 0 */
7317}
7318
7332{
7333 uint8_t res;
7334 uint8_t prev;
7335
7336 if (handle == NULL) /* check handle */
7337 {
7338 return 2; /* return error */
7339 }
7340 if (handle->inited != 1) /* check handle initialization */
7341 {
7342 return 3; /* return error */
7343 }
7344
7345 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7346 if (res != 0) /* check result */
7347 {
7348 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7349
7350 return 1; /* return error */
7351 }
7352 prev &= ~(1 << axis); /* clear config */
7353 prev |= enable << axis; /* set config */
7354 res = a_mpu9250_write(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* write gyroscope config */
7355 if (res != 0) /* check result */
7356 {
7357 handle->debug_print("mpu9250: write gyroscope config failed.\n"); /* write gyroscope config failed */
7358
7359 return 1; /* return error */
7360 }
7361
7362 return 0; /* success return 0 */
7363}
7364
7378{
7379 uint8_t res;
7380 uint8_t prev;
7381
7382 if (handle == NULL) /* check handle */
7383 {
7384 return 2; /* return error */
7385 }
7386 if (handle->inited != 1) /* check handle initialization */
7387 {
7388 return 3; /* return error */
7389 }
7390
7391 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7392 if (res != 0) /* check result */
7393 {
7394 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7395
7396 return 1; /* return error */
7397 }
7398 *enable = (mpu9250_bool_t)((prev >> axis) & 0x01); /* get the bool */
7399
7400 return 0; /* success return 0 */
7401}
7402
7415{
7416 uint8_t res;
7417 uint8_t prev;
7418
7419 if (handle == NULL) /* check handle */
7420 {
7421 return 2; /* return error */
7422 }
7423 if (handle->inited != 1) /* check handle initialization */
7424 {
7425 return 3; /* return error */
7426 }
7427
7428 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7429 if (res != 0) /* check result */
7430 {
7431 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7432
7433 return 1; /* return error */
7434 }
7435 prev &= ~(3 << 3); /* clear config */
7436 prev |= range << 3; /* set config */
7437 res = a_mpu9250_write(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* write gyroscope config */
7438 if (res != 0) /* check result */
7439 {
7440 handle->debug_print("mpu9250: write gyroscope config failed.\n"); /* write gyroscope config failed */
7441
7442 return 1; /* return error */
7443 }
7444
7445 return 0; /* success return 0 */
7446}
7447
7460{
7461 uint8_t res;
7462 uint8_t prev;
7463
7464 if (handle == NULL) /* check handle */
7465 {
7466 return 2; /* return error */
7467 }
7468 if (handle->inited != 1) /* check handle initialization */
7469 {
7470 return 3; /* return error */
7471 }
7472
7473 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7474 if (res != 0) /* check result */
7475 {
7476 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7477
7478 return 1; /* return error */
7479 }
7480 *range = (mpu9250_gyroscope_range_t)((prev >> 3) & 0x3); /* get the range */
7481
7482 return 0; /* success return 0 */
7483}
7484
7497uint8_t mpu9250_set_gyroscope_choice(mpu9250_handle_t *handle, uint8_t choice)
7498{
7499 uint8_t res;
7500 uint8_t prev;
7501
7502 if (handle == NULL) /* check handle */
7503 {
7504 return 2; /* return error */
7505 }
7506 if (handle->inited != 1) /* check handle initialization */
7507 {
7508 return 3; /* return error */
7509 }
7510 if (choice > 3) /* check the choice */
7511 {
7512 handle->debug_print("mpu9250: choice > 3.\n"); /* choice > 3 */
7513
7514 return 4; /* return error */
7515 }
7516
7517 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7518 if (res != 0) /* check result */
7519 {
7520 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7521
7522 return 1; /* return error */
7523 }
7524 prev &= ~(3 << 0); /* clear config */
7525 prev |= choice << 0; /* set config */
7526 res = a_mpu9250_write(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* write gyroscope config */
7527 if (res != 0) /* check result */
7528 {
7529 handle->debug_print("mpu9250: write gyroscope config failed.\n"); /* write gyroscope config failed */
7530
7531 return 1; /* return error */
7532 }
7533
7534 return 0; /* success return 0 */
7535}
7536
7548uint8_t mpu9250_get_gyroscope_choice(mpu9250_handle_t *handle, uint8_t *choice)
7549{
7550 uint8_t res;
7551 uint8_t prev;
7552
7553 if (handle == NULL) /* check handle */
7554 {
7555 return 2; /* return error */
7556 }
7557 if (handle->inited != 1) /* check handle initialization */
7558 {
7559 return 3; /* return error */
7560 }
7561
7562 res = a_mpu9250_read(handle, MPU9250_REG_GYRO_CONFIG, (uint8_t *)&prev, 1); /* read gyroscope config */
7563 if (res != 0) /* check result */
7564 {
7565 handle->debug_print("mpu9250: read gyroscope config failed.\n"); /* read gyroscope config failed */
7566
7567 return 1; /* return error */
7568 }
7569 *choice = prev & 0x03; /* get the choice */
7570
7571 return 0; /* success return 0 */
7572}
7573
7587{
7588 uint8_t res;
7589 uint8_t prev;
7590
7591 if (handle == NULL) /* check handle */
7592 {
7593 return 2; /* return error */
7594 }
7595 if (handle->inited != 1) /* check handle initialization */
7596 {
7597 return 3; /* return error */
7598 }
7599
7600 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
7601 if (res != 0) /* check result */
7602 {
7603 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
7604
7605 return 1; /* return error */
7606 }
7607 prev &= ~(1 << axis); /* clear config */
7608 prev |= enable << axis; /* set config */
7609 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* write accelerometer config */
7610 if (res != 0) /* check result */
7611 {
7612 handle->debug_print("mpu9250: write accelerometer config failed.\n"); /* write accelerometer config failed */
7613
7614 return 1; /* return error */
7615 }
7616
7617 return 0; /* success return 0 */
7618}
7619
7633{
7634 uint8_t res;
7635 uint8_t prev;
7636
7637 if (handle == NULL) /* check handle */
7638 {
7639 return 2; /* return error */
7640 }
7641 if (handle->inited != 1) /* check handle initialization */
7642 {
7643 return 3; /* return error */
7644 }
7645
7646 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
7647 if (res != 0) /* check result */
7648 {
7649 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
7650
7651 return 1; /* return error */
7652 }
7653 *enable = (mpu9250_bool_t)((prev >> axis) & 0x01); /* get the bool */
7654
7655 return 0; /* success return 0 */
7656}
7657
7670{
7671 uint8_t res;
7672 uint8_t prev;
7673
7674 if (handle == NULL) /* check handle */
7675 {
7676 return 2; /* return error */
7677 }
7678 if (handle->inited != 1) /* check handle initialization */
7679 {
7680 return 3; /* return error */
7681 }
7682
7683 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
7684 if (res != 0) /* check result */
7685 {
7686 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
7687
7688 return 1; /* return error */
7689 }
7690 prev &= ~(3 << 3); /* clear config */
7691 prev |= range << 3; /* set config */
7692 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* write accelerometer config */
7693 if (res != 0) /* check result */
7694 {
7695 handle->debug_print("mpu9250: write accelerometer config failed.\n"); /* write accelerometer config failed */
7696
7697 return 1; /* return error */
7698 }
7699
7700 return 0; /* success return 0 */
7701}
7702
7715{
7716 uint8_t res;
7717 uint8_t prev;
7718
7719 if (handle == NULL) /* check handle */
7720 {
7721 return 2; /* return error */
7722 }
7723 if (handle->inited != 1) /* check handle initialization */
7724 {
7725 return 3; /* return error */
7726 }
7727
7728 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG, (uint8_t *)&prev, 1); /* read accelerometer config */
7729 if (res != 0) /* check result */
7730 {
7731 handle->debug_print("mpu9250: read accelerometer config failed.\n"); /* read accelerometer config failed */
7732
7733 return 1; /* return error */
7734 }
7735 *range = (mpu9250_accelerometer_range_t)((prev >> 3) & 0x3); /* get the range */
7736
7737 return 0; /* success return 0 */
7738}
7739
7751{
7752 uint8_t res;
7753 uint8_t prev;
7754
7755 if (handle == NULL) /* check handle */
7756 {
7757 return 2; /* return error */
7758 }
7759 if (handle->inited != 1) /* check handle initialization */
7760 {
7761 return 3; /* return error */
7762 }
7763
7764 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* read config */
7765 if (res != 0) /* check result */
7766 {
7767 handle->debug_print("mpu9250: read accelerometer 2 config failed.\n"); /* read accelerometer 2 config failed */
7768
7769 return 1; /* return error */
7770 }
7771 prev &= ~(1 << 6); /* clear config */
7772 prev |= 1 << 6; /* set config */
7773 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* write config */
7774 if (res != 0) /* check result */
7775 {
7776 handle->debug_print("mpu9250: write accelerometer 2 config failed.\n"); /* write accelerometer 2 config failed */
7777
7778 return 1; /* return error */
7779 }
7780
7781 return 0; /* success return 0 */
7782}
7783
7797{
7798 uint8_t res;
7799 uint8_t prev;
7800
7801 if (handle == NULL) /* check handle */
7802 {
7803 return 2; /* return error */
7804 }
7805 if (handle->inited != 1) /* check handle initialization */
7806 {
7807 return 3; /* return error */
7808 }
7809 if (choice > 1) /* check the choice */
7810 {
7811 handle->debug_print("mpu9250: choice > 1.\n"); /* choice > 1 */
7812
7813 return 4; /* return error */
7814 }
7815
7816 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* read accelerometer config */
7817 if (res != 0) /* check result */
7818 {
7819 handle->debug_print("mpu9250: read accelerometer 2 config failed.\n"); /* read accelerometer 2 config failed */
7820
7821 return 1; /* return error */
7822 }
7823 prev &= ~(1 << 3); /* clear config */
7824 prev |= choice << 3; /* set config */
7825 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* write accelerometer config */
7826 if (res != 0) /* check result */
7827 {
7828 handle->debug_print("mpu9250: write accelerometer 2 config failed.\n"); /* write accelerometer 2 config failed */
7829
7830 return 1; /* return error */
7831 }
7832
7833 return 0; /* success return 0 */
7834}
7835
7849{
7850 uint8_t res;
7851 uint8_t prev;
7852
7853 if (handle == NULL) /* check handle */
7854 {
7855 return 2; /* return error */
7856 }
7857 if (handle->inited != 1) /* check handle initialization */
7858 {
7859 return 3; /* return error */
7860 }
7861
7862 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* read accelerometer config */
7863 if (res != 0) /* check result */
7864 {
7865 handle->debug_print("mpu9250: read accelerometer 2 config failed.\n"); /* read accelerometer 2 config failed */
7866
7867 return 1; /* return error */
7868 }
7869 *choice = (prev >> 3) & 0x1; /* get the choice */
7870
7871 return 0; /* success return 0 */
7872}
7873
7886{
7887 uint8_t res;
7888 uint8_t prev;
7889
7890 if (handle == NULL) /* check handle */
7891 {
7892 return 2; /* return error */
7893 }
7894 if (handle->inited != 1) /* check handle initialization */
7895 {
7896 return 3; /* return error */
7897 }
7898
7899 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* read config */
7900 if (res != 0) /* check result */
7901 {
7902 handle->debug_print("mpu9250: read accelerometer 2 config failed.\n"); /* read accelerometer 2 config failed */
7903
7904 return 1; /* return error */
7905 }
7906 prev &= ~(0x7 << 0); /* clear config */
7907 prev |= filter << 0; /* set config */
7908 res = a_mpu9250_write(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* write config */
7909 if (res != 0) /* check result */
7910 {
7911 handle->debug_print("mpu9250: write accelerometer 2 config failed.\n"); /* write accelerometer 2 config failed */
7912
7913 return 1; /* return error */
7914 }
7915
7916 return 0; /* success return 0 */
7917}
7918
7931{
7932 uint8_t res;
7933 uint8_t prev;
7934
7935 if (handle == NULL) /* check handle */
7936 {
7937 return 2; /* return error */
7938 }
7939 if (handle->inited != 1) /* check handle initialization */
7940 {
7941 return 3; /* return error */
7942 }
7943
7944 res = a_mpu9250_read(handle, MPU9250_REG_ACCEL_CONFIG2, (uint8_t *)&prev, 1); /* read config */
7945 if (res != 0) /* check result */
7946 {
7947 handle->debug_print("mpu9250: read accelerometer 2 config failed.\n"); /* read accelerometer 2 config failed */
7948
7949 return 1; /* return error */
7950 }
7951 *filter = (mpu9250_accelerometer_low_pass_filter_t)(prev & 0x7); /* get the filter */
7952
7953 return 0; /* success return 0 */
7954}
7955
7968{
7969 uint8_t res;
7970 uint8_t prev;
7971
7972 if (handle == NULL) /* check handle */
7973 {
7974 return 2; /* return error */
7975 }
7976 if (handle->inited != 1) /* check handle initialization */
7977 {
7978 return 3; /* return error */
7979 }
7980
7981 res = a_mpu9250_read(handle, MPU9250_REG_LP_ACCEL_ODR, (uint8_t *)&prev, 1); /* read config */
7982 if (res != 0) /* check result */
7983 {
7984 handle->debug_print("mpu9250: read lp accelerometer odr failed.\n"); /* read lp accelerometer odr failed */
7985
7986 return 1; /* return error */
7987 }
7988 prev &= ~(0xF << 0); /* clear config */
7989 prev |= rate << 0; /* set config */
7990 res = a_mpu9250_write(handle, MPU9250_REG_LP_ACCEL_ODR, (uint8_t *)&prev, 1); /* write config */
7991 if (res != 0) /* check result */
7992 {
7993 handle->debug_print("mpu9250: write lp accelerometer odr failed.\n"); /* write lp accelerometer odr failed */
7994
7995 return 1; /* return error */
7996 }
7997
7998 return 0; /* success return 0 */
7999}
8000
8013{
8014 uint8_t res;
8015 uint8_t prev;
8016
8017 if (handle == NULL) /* check handle */
8018 {
8019 return 2; /* return error */
8020 }
8021 if (handle->inited != 1) /* check handle initialization */
8022 {
8023 return 3; /* return error */
8024 }
8025
8026 res = a_mpu9250_read(handle, MPU9250_REG_LP_ACCEL_ODR, (uint8_t *)&prev, 1); /* read config */
8027 if (res != 0) /* check result */
8028 {
8029 handle->debug_print("mpu9250: read lp accelerometer odr failed.\n"); /* read lp accelerometer odr failed */
8030
8031 return 1; /* return error */
8032 }
8033 *rate = (mpu9250_low_power_accel_output_rate_t)(prev & 0xF); /* get the rate */
8034
8035 return 0; /* success return 0 */
8036}
8037
8050{
8051 uint8_t res;
8052 uint8_t prev;
8053
8054 if (handle == NULL) /* check handle */
8055 {
8056 return 2; /* return error */
8057 }
8058 if (handle->inited != 1) /* check handle initialization */
8059 {
8060 return 3; /* return error */
8061 }
8062
8063 res = a_mpu9250_read(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* read config */
8064 if (res != 0) /* check result */
8065 {
8066 handle->debug_print("mpu9250: read motion detect ctrl failed.\n"); /* read motion detect ctrl failed */
8067
8068 return 1; /* return error */
8069 }
8070 prev &= ~(1 << 7); /* clear config */
8071 prev |= enable << 7; /* set config */
8072 res = a_mpu9250_write(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* write config */
8073 if (res != 0) /* check result */
8074 {
8075 handle->debug_print("mpu9250: write motion detect ctrl failed.\n"); /* write motion detect ctrl failed */
8076
8077 return 1; /* return error */
8078 }
8079
8080 return 0; /* success return 0 */
8081}
8082
8095{
8096 uint8_t res;
8097 uint8_t prev;
8098
8099 if (handle == NULL) /* check handle */
8100 {
8101 return 2; /* return error */
8102 }
8103 if (handle->inited != 1) /* check handle initialization */
8104 {
8105 return 3; /* return error */
8106 }
8107
8108 res = a_mpu9250_read(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* read config */
8109 if (res != 0) /* check result */
8110 {
8111 handle->debug_print("mpu9250: read motion detect ctrl failed.\n"); /* read motion detect ctrl failed */
8112
8113 return 1; /* return error */
8114 }
8115 *enable = (mpu9250_bool_t)((prev >> 7) & 0x01); /* get the bool */
8116
8117 return 0; /* success return 0 */
8118}
8119
8132{
8133 uint8_t res;
8134 uint8_t prev;
8135
8136 if (handle == NULL) /* check handle */
8137 {
8138 return 2; /* return error */
8139 }
8140 if (handle->inited != 1) /* check handle initialization */
8141 {
8142 return 3; /* return error */
8143 }
8144
8145 res = a_mpu9250_read(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* read config */
8146 if (res != 0) /* check result */
8147 {
8148 handle->debug_print("mpu9250: read motion detect ctrl failed.\n"); /* read motion detect ctrl failed */
8149
8150 return 1; /* return error */
8151 }
8152 prev &= ~(1 << 6); /* clear config */
8153 prev |= enable << 6; /* set config */
8154 res = a_mpu9250_write(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* write config */
8155 if (res != 0) /* check result */
8156 {
8157 handle->debug_print("mpu9250: write motion detect ctrl failed.\n"); /* write motion detect ctrl failed */
8158
8159 return 1; /* return error */
8160 }
8161
8162 return 0; /* success return 0 */
8163}
8164
8177{
8178 uint8_t res;
8179 uint8_t prev;
8180
8181 if (handle == NULL) /* check handle */
8182 {
8183 return 2; /* return error */
8184 }
8185 if (handle->inited != 1) /* check handle initialization */
8186 {
8187 return 3; /* return error */
8188 }
8189
8190 res = a_mpu9250_read(handle, MPU9250_REG_MOT_DETECT_CTRL, (uint8_t *)&prev, 1); /* read config */
8191 if (res != 0) /* check result */
8192 {
8193 handle->debug_print("mpu9250: read motion detect ctrl failed.\n"); /* read motion detect ctrl failed */
8194
8195 return 1; /* return error */
8196 }
8197 *enable = (mpu9250_bool_t)((prev >> 6) & 0x01); /* get the bool */
8198
8199 return 0; /* success return 0 */
8200}
8201
8215{
8216 uint8_t res;
8217 uint8_t prev;
8218
8219 if (handle == NULL) /* check handle */
8220 {
8221 return 2; /* return error */
8222 }
8223 if (handle->inited != 1) /* check handle initialization */
8224 {
8225 return 3; /* return error */
8226 }
8227
8228 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* read fifo enable config */
8229 if (res != 0) /* check result */
8230 {
8231 handle->debug_print("mpu9250: read fifo enable config failed.\n"); /* read fifo enable config failed */
8232
8233 return 1; /* return error */
8234 }
8235 prev &= ~(1 << fifo); /* clear config */
8236 prev |= enable << fifo; /* set config */
8237 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* write fifo enable config */
8238 if (res != 0) /* check result */
8239 {
8240 handle->debug_print("mpu9250: write fifo enable config failed.\n"); /* write fifo enable config failed */
8241
8242 return 1; /* return error */
8243 }
8244
8245 return 0; /* success return 0 */
8246}
8247
8261{
8262 uint8_t res;
8263 uint8_t prev;
8264
8265 if (handle == NULL) /* check handle */
8266 {
8267 return 2; /* return error */
8268 }
8269 if (handle->inited != 1) /* check handle initialization */
8270 {
8271 return 3; /* return error */
8272 }
8273
8274 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* read fifo enable config */
8275 if (res != 0) /* check result */
8276 {
8277 handle->debug_print("mpu9250: read fifo enable config failed.\n"); /* read fifo enable config failed */
8278
8279 return 1; /* return error */
8280 }
8281 *enable = (mpu9250_bool_t)((prev >> fifo) & 0x01); /* get the bool */
8282
8283 return 0; /* success return 0 */
8284}
8285
8298{
8299 uint8_t res;
8300 uint8_t prev;
8301
8302 if (handle == NULL) /* check handle */
8303 {
8304 return 2; /* return error */
8305 }
8306 if (handle->inited != 1) /* check handle initialization */
8307 {
8308 return 3; /* return error */
8309 }
8310
8311 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8312 if (res != 0) /* check result */
8313 {
8314 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8315
8316 return 1; /* return error */
8317 }
8318 prev &= ~(1 << 7); /* clear config */
8319 prev |= level << 7; /* set config */
8320 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8321 if (res != 0) /* check result */
8322 {
8323 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8324
8325 return 1; /* return error */
8326 }
8327
8328 return 0; /* success return 0 */
8329}
8330
8343{
8344 uint8_t res;
8345 uint8_t prev;
8346
8347 if (handle == NULL) /* check handle */
8348 {
8349 return 2; /* return error */
8350 }
8351 if (handle->inited != 1) /* check handle initialization */
8352 {
8353 return 3; /* return error */
8354 }
8355
8356 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8357 if (res != 0) /* check result */
8358 {
8359 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8360
8361 return 1; /* return error */
8362 }
8363 *level = (mpu9250_pin_level_t)((prev >> 7) & 0x01); /* get the level */
8364
8365 return 0; /* success return 0 */
8366}
8367
8380{
8381 uint8_t res;
8382 uint8_t prev;
8383
8384 if (handle == NULL) /* check handle */
8385 {
8386 return 2; /* return error */
8387 }
8388 if (handle->inited != 1) /* check handle initialization */
8389 {
8390 return 3; /* return error */
8391 }
8392
8393 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8394 if (res != 0) /* check result */
8395 {
8396 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8397
8398 return 1; /* return error */
8399 }
8400 prev &= ~(1 << 6); /* clear config */
8401 prev |= type << 6; /* set config */
8402 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8403 if (res != 0) /* check result */
8404 {
8405 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8406
8407 return 1; /* return error */
8408 }
8409
8410 return 0; /* success return 0 */
8411}
8412
8425{
8426 uint8_t res;
8427 uint8_t prev;
8428
8429 if (handle == NULL) /* check handle */
8430 {
8431 return 2; /* return error */
8432 }
8433 if (handle->inited != 1) /* check handle initialization */
8434 {
8435 return 3; /* return error */
8436 }
8437
8438 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8439 if (res != 0) /* check result */
8440 {
8441 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8442
8443 return 1; /* return error */
8444 }
8445 *type = (mpu9250_pin_type_t)((prev >> 6) & 0x01); /* get the pin type */
8446
8447 return 0; /* success return 0 */
8448}
8449
8462{
8463 uint8_t res;
8464 uint8_t prev;
8465
8466 if (handle == NULL) /* check handle */
8467 {
8468 return 2; /* return error */
8469 }
8470 if (handle->inited != 1) /* check handle initialization */
8471 {
8472 return 3; /* return error */
8473 }
8474
8475 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8476 if (res != 0) /* check result */
8477 {
8478 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8479
8480 return 1; /* return error */
8481 }
8482 prev &= ~(1 << 5); /* clear config */
8483 prev |= (!enable) << 5; /* set config */
8484 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8485 if (res != 0) /* check result */
8486 {
8487 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8488
8489 return 1; /* return error */
8490 }
8491
8492 return 0; /* success return 0 */
8493}
8494
8507{
8508 uint8_t res;
8509 uint8_t prev;
8510
8511 if (handle == NULL) /* check handle */
8512 {
8513 return 2; /* return error */
8514 }
8515 if (handle->inited != 1) /* check handle initialization */
8516 {
8517 return 3; /* return error */
8518 }
8519
8520 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8521 if (res != 0) /* check result */
8522 {
8523 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8524
8525 return 1; /* return error */
8526 }
8527 *enable = (mpu9250_bool_t)(!((prev >> 5) & 0x01)); /* get the bool */
8528
8529 return 0; /* success return 0 */
8530}
8531
8544{
8545 uint8_t res;
8546 uint8_t prev;
8547
8548 if (handle == NULL) /* check handle */
8549 {
8550 return 2; /* return error */
8551 }
8552 if (handle->inited != 1) /* check handle initialization */
8553 {
8554 return 3; /* return error */
8555 }
8556
8557 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8558 if (res != 0) /* check result */
8559 {
8560 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8561
8562 return 1; /* return error */
8563 }
8564 prev &= ~(1 << 4); /* clear config */
8565 prev |= enable << 4; /* set config */
8566 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8567 if (res != 0) /* check result */
8568 {
8569 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8570
8571 return 1; /* return error */
8572 }
8573
8574 return 0; /* success return 0 */
8575}
8576
8589{
8590 uint8_t res;
8591 uint8_t prev;
8592
8593 if (handle == NULL) /* check handle */
8594 {
8595 return 2; /* return error */
8596 }
8597 if (handle->inited != 1) /* check handle initialization */
8598 {
8599 return 3; /* return error */
8600 }
8601
8602 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8603 if (res != 0) /* check result */
8604 {
8605 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8606
8607 return 1; /* return error */
8608 }
8609 *enable = (mpu9250_bool_t)((prev >> 4) & 0x01); /* get the bool */
8610
8611 return 0; /* success return 0 */
8612}
8613
8626{
8627 uint8_t res;
8628 uint8_t prev;
8629
8630 if (handle == NULL) /* check handle */
8631 {
8632 return 2; /* return error */
8633 }
8634 if (handle->inited != 1) /* check handle initialization */
8635 {
8636 return 3; /* return error */
8637 }
8638
8639 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8640 if (res != 0) /* check result */
8641 {
8642 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8643
8644 return 1; /* return error */
8645 }
8646 prev &= ~(1 << 3); /* clear config */
8647 prev |= level << 3; /* set config */
8648 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8649 if (res != 0) /* check result */
8650 {
8651 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8652
8653 return 1; /* return error */
8654 }
8655
8656 return 0; /* success return 0 */
8657}
8658
8671{
8672 uint8_t res;
8673 uint8_t prev;
8674
8675 if (handle == NULL) /* check handle */
8676 {
8677 return 2; /* return error */
8678 }
8679 if (handle->inited != 1) /* check handle initialization */
8680 {
8681 return 3; /* return error */
8682 }
8683
8684 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8685 if (res != 0) /* check result */
8686 {
8687 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8688
8689 return 1; /* return error */
8690 }
8691 *level = (mpu9250_pin_level_t)((prev >> 3) & 0x01); /* get the level */
8692
8693 return 0; /* success return 0 */
8694}
8695
8708{
8709 uint8_t res;
8710 uint8_t prev;
8711
8712 if (handle == NULL) /* check handle */
8713 {
8714 return 2; /* return error */
8715 }
8716 if (handle->inited != 1) /* check handle initialization */
8717 {
8718 return 3; /* return error */
8719 }
8720
8721 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8722 if (res != 0) /* check result */
8723 {
8724 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8725
8726 return 1; /* return error */
8727 }
8728 prev &= ~(1 << 2); /* clear config */
8729 prev |= enable << 2; /* set config */
8730 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8731 if (res != 0) /* check result */
8732 {
8733 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8734
8735 return 1; /* return error */
8736 }
8737
8738 return 0; /* success return 0 */
8739}
8740
8753{
8754 uint8_t res;
8755 uint8_t prev;
8756
8757 if (handle == NULL) /* check handle */
8758 {
8759 return 2; /* return error */
8760 }
8761 if (handle->inited != 1) /* check handle initialization */
8762 {
8763 return 3; /* return error */
8764 }
8765
8766 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin config */
8767 if (res != 0) /* check result */
8768 {
8769 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8770
8771 return 1; /* return error */
8772 }
8773 *enable = (mpu9250_bool_t)((prev >> 2) & 0x01); /* get the bool */
8774
8775 return 0; /* success return 0 */
8776}
8777
8790{
8791 uint8_t res;
8792 uint8_t prev;
8793
8794 if (handle == NULL) /* check handle */
8795 {
8796 return 2; /* return error */
8797 }
8798 if (handle->inited != 1) /* check handle initialization */
8799 {
8800 return 3; /* return error */
8801 }
8802
8803 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin */
8804 if (res != 0) /* check result */
8805 {
8806 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8807
8808 return 1; /* return error */
8809 }
8810 prev &= ~(1 << 1); /* clear config */
8811 prev |= enable << 1; /* set config */
8812 res = a_mpu9250_write(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* write interrupt pin */
8813 if (res != 0) /* check result */
8814 {
8815 handle->debug_print("mpu9250: write interrupt pin failed.\n"); /* write interrupt pin failed */
8816
8817 return 1; /* return error */
8818 }
8819
8820 return 0; /* success return 0 */
8821}
8822
8835{
8836 uint8_t res;
8837 uint8_t prev;
8838
8839 if (handle == NULL) /* check handle */
8840 {
8841 return 2; /* return error */
8842 }
8843 if (handle->inited != 1) /* check handle initialization */
8844 {
8845 return 3; /* return error */
8846 }
8847
8848 res = a_mpu9250_read(handle, MPU9250_REG_INT_PIN_CFG, (uint8_t *)&prev, 1); /* read interrupt pin */
8849 if (res != 0) /* check result */
8850 {
8851 handle->debug_print("mpu9250: read interrupt pin failed.\n"); /* read interrupt pin failed */
8852
8853 return 1; /* return error */
8854 }
8855 *enable = (mpu9250_bool_t)((prev >> 1) & 0x01); /* get the bool */
8856
8857 return 0; /* success return 0 */
8858}
8859
8873{
8874 uint8_t res;
8875 uint8_t prev;
8876
8877 if (handle == NULL) /* check handle */
8878 {
8879 return 2; /* return error */
8880 }
8881 if (handle->inited != 1) /* check handle initialization */
8882 {
8883 return 3; /* return error */
8884 }
8885
8886 res = a_mpu9250_read(handle, MPU9250_REG_INT_ENABLE, (uint8_t *)&prev, 1); /* read interrupt enable */
8887 if (res != 0) /* check result */
8888 {
8889 handle->debug_print("mpu9250: read interrupt enable failed.\n"); /* read interrupt enable failed */
8890
8891 return 1; /* return error */
8892 }
8893 prev &= ~(1 << type); /* clear config */
8894 prev |= enable << type; /* set config */
8895 res = a_mpu9250_write(handle, MPU9250_REG_INT_ENABLE, (uint8_t *)&prev, 1); /* write interrupt enable */
8896 if (res != 0) /* check result */
8897 {
8898 handle->debug_print("mpu9250: write interrupt enable failed.\n"); /* write interrupt enable failed */
8899
8900 return 1; /* return error */
8901 }
8902
8903 return 0; /* success return 0 */
8904}
8905
8919{
8920 uint8_t res;
8921 uint8_t prev;
8922
8923 if (handle == NULL) /* check handle */
8924 {
8925 return 2; /* return error */
8926 }
8927 if (handle->inited != 1) /* check handle initialization */
8928 {
8929 return 3; /* return error */
8930 }
8931
8932 res = a_mpu9250_read(handle, MPU9250_REG_INT_ENABLE, (uint8_t *)&prev, 1); /* read interrupt enable */
8933 if (res != 0) /* check result */
8934 {
8935 handle->debug_print("mpu9250: read interrupt enable failed.\n"); /* read interrupt enable failed */
8936
8937 return 1; /* return error */
8938 }
8939 *enable = (mpu9250_bool_t)((prev >> type) & 0x01); /* get the bool */
8940
8941 return 0; /* success return 0 */
8942}
8943
8955uint8_t mpu9250_get_interrupt_status(mpu9250_handle_t *handle, uint8_t *status)
8956{
8957 uint8_t res;
8958
8959 if (handle == NULL) /* check handle */
8960 {
8961 return 2; /* return error */
8962 }
8963 if (handle->inited != 1) /* check handle initialization */
8964 {
8965 return 3; /* return error */
8966 }
8967
8968 res = a_mpu9250_read(handle, MPU9250_REG_INT_STATUS, (uint8_t *)status, 1); /* read interrupt status */
8969 if (res != 0) /* check result */
8970 {
8971 handle->debug_print("mpu9250: read interrupt status failed.\n"); /* read interrupt status failed */
8972
8973 return 1; /* return error */
8974 }
8975
8976 return 0; /* success return 0 */
8977}
8978
8991{
8992 uint8_t res;
8993
8994 if (handle == NULL) /* check handle */
8995 {
8996 return 2; /* return error */
8997 }
8998 if (handle->inited != 1) /* check handle initialization */
8999 {
9000 return 3; /* return error */
9001 }
9002
9003 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_X_GYRO, (uint8_t *)&data, 1); /* write self test x */
9004 if (res != 0) /* check result */
9005 {
9006 handle->debug_print("mpu9250: write self test x failed.\n"); /* write self test x failed */
9007
9008 return 1; /* return error */
9009 }
9010
9011 return 0; /* success return 0 */
9012}
9013
9025uint8_t mpu9250_get_gyroscope_x_test(mpu9250_handle_t *handle, uint8_t *data)
9026{
9027 uint8_t res;
9028
9029 if (handle == NULL) /* check handle */
9030 {
9031 return 2; /* return error */
9032 }
9033 if (handle->inited != 1) /* check handle initialization */
9034 {
9035 return 3; /* return error */
9036 }
9037
9038 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_X_GYRO, (uint8_t *)data, 1); /* read self test x */
9039 if (res != 0) /* check result */
9040 {
9041 handle->debug_print("mpu9250: read self test x failed.\n"); /* read self test x failed */
9042
9043 return 1; /* return error */
9044 }
9045
9046 return 0; /* success return 0 */
9047}
9048
9061{
9062 uint8_t res;
9063
9064 if (handle == NULL) /* check handle */
9065 {
9066 return 2; /* return error */
9067 }
9068 if (handle->inited != 1) /* check handle initialization */
9069 {
9070 return 3; /* return error */
9071 }
9072
9073 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_Y_GYRO, (uint8_t *)&data, 1); /* write self test y */
9074 if (res != 0) /* check result */
9075 {
9076 handle->debug_print("mpu9250: write self test y failed.\n"); /* write self test y failed */
9077
9078 return 1; /* return error */
9079 }
9080
9081 return 0; /* success return 0 */
9082}
9083
9095uint8_t mpu9250_get_gyroscope_y_test(mpu9250_handle_t *handle, uint8_t *data)
9096{
9097 uint8_t res;
9098
9099 if (handle == NULL) /* check handle */
9100 {
9101 return 2; /* return error */
9102 }
9103 if (handle->inited != 1) /* check handle initialization */
9104 {
9105 return 3; /* return error */
9106 }
9107
9108 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_Y_GYRO, (uint8_t *)data, 1); /* read self test y */
9109 if (res != 0) /* check result */
9110 {
9111 handle->debug_print("mpu9250: read self test y failed.\n"); /* read self test y failed */
9112
9113 return 1; /* return error */
9114 }
9115
9116 return 0; /* success return 0 */
9117}
9118
9131{
9132 uint8_t res;
9133
9134 if (handle == NULL) /* check handle */
9135 {
9136 return 2; /* return error */
9137 }
9138 if (handle->inited != 1) /* check handle initialization */
9139 {
9140 return 3; /* return error */
9141 }
9142
9143 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_Z_GYRO, (uint8_t *)&data, 1); /* write self test z */
9144 if (res != 0) /* check result */
9145 {
9146 handle->debug_print("mpu9250: write self test z failed.\n"); /* write self test z failed */
9147
9148 return 1; /* return error */
9149 }
9150
9151 return 0; /* success return 0 */
9152}
9153
9165uint8_t mpu9250_get_gyroscope_z_test(mpu9250_handle_t *handle, uint8_t *data)
9166{
9167 uint8_t res;
9168
9169 if (handle == NULL) /* check handle */
9170 {
9171 return 2; /* return error */
9172 }
9173 if (handle->inited != 1) /* check handle initialization */
9174 {
9175 return 3; /* return error */
9176 }
9177
9178 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_Z_GYRO, (uint8_t *)data, 1); /* read self test z */
9179 if (res != 0) /* check result */
9180 {
9181 handle->debug_print("mpu9250: read self test z failed.\n"); /* read self test z failed */
9182
9183 return 1; /* return error */
9184 }
9185
9186 return 0; /* success return 0 */
9187}
9188
9201{
9202 uint8_t res;
9203
9204 if (handle == NULL) /* check handle */
9205 {
9206 return 2; /* return error */
9207 }
9208 if (handle->inited != 1) /* check handle initialization */
9209 {
9210 return 3; /* return error */
9211 }
9212
9213 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_X_ACCEL, (uint8_t *)&data, 1); /* write self test x */
9214 if (res != 0) /* check result */
9215 {
9216 handle->debug_print("mpu9250: write self test x failed.\n"); /* write self test x failed */
9217
9218 return 1; /* return error */
9219 }
9220
9221 return 0; /* success return 0 */
9222}
9223
9236{
9237 uint8_t res;
9238
9239 if (handle == NULL) /* check handle */
9240 {
9241 return 2; /* return error */
9242 }
9243 if (handle->inited != 1) /* check handle initialization */
9244 {
9245 return 3; /* return error */
9246 }
9247
9248 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_X_ACCEL, (uint8_t *)data, 1); /* read self test x */
9249 if (res != 0) /* check result */
9250 {
9251 handle->debug_print("mpu9250: read self test x failed.\n"); /* read self test x failed */
9252
9253 return 1; /* return error */
9254 }
9255
9256 return 0; /* success return 0 */
9257}
9258
9271{
9272 uint8_t res;
9273
9274 if (handle == NULL) /* check handle */
9275 {
9276 return 2; /* return error */
9277 }
9278 if (handle->inited != 1) /* check handle initialization */
9279 {
9280 return 3; /* return error */
9281 }
9282
9283 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_Y_ACCEL, (uint8_t *)&data, 1); /* write self test y */
9284 if (res != 0) /* check result */
9285 {
9286 handle->debug_print("mpu9250: write self test y failed.\n"); /* write self test y failed */
9287
9288 return 1; /* return error */
9289 }
9290
9291 return 0; /* success return 0 */
9292}
9293
9306{
9307 uint8_t res;
9308
9309 if (handle == NULL) /* check handle */
9310 {
9311 return 2; /* return error */
9312 }
9313 if (handle->inited != 1) /* check handle initialization */
9314 {
9315 return 3; /* return error */
9316 }
9317
9318 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_Y_ACCEL, (uint8_t *)data, 1); /* read self test y */
9319 if (res != 0) /* check result */
9320 {
9321 handle->debug_print("mpu9250: read self test y failed.\n"); /* read self test y failed */
9322
9323 return 1; /* return error */
9324 }
9325
9326 return 0; /* success return 0 */
9327}
9328
9341{
9342 uint8_t res;
9343
9344 if (handle == NULL) /* check handle */
9345 {
9346 return 2; /* return error */
9347 }
9348 if (handle->inited != 1) /* check handle initialization */
9349 {
9350 return 3; /* return error */
9351 }
9352
9353 res = a_mpu9250_write(handle, MPU9250_REG_SELF_TEST_Z_ACCEL, (uint8_t *)&data, 1); /* write self test z */
9354 if (res != 0) /* check result */
9355 {
9356 handle->debug_print("mpu9250: write self test z failed.\n"); /* write self test z failed */
9357
9358 return 1; /* return error */
9359 }
9360
9361 return 0; /* success return 0 */
9362}
9363
9376{
9377 uint8_t res;
9378
9379 if (handle == NULL) /* check handle */
9380 {
9381 return 2; /* return error */
9382 }
9383 if (handle->inited != 1) /* check handle initialization */
9384 {
9385 return 3; /* return error */
9386 }
9387
9388 res = a_mpu9250_read(handle, MPU9250_REG_SELF_TEST_Z_ACCEL, (uint8_t *)data, 1); /* read self test z */
9389 if (res != 0) /* check result */
9390 {
9391 handle->debug_print("mpu9250: read self test z failed.\n"); /* read self test z failed */
9392
9393 return 1; /* return error */
9394 }
9395
9396 return 0; /* success return 0 */
9397}
9398
9411{
9412 uint8_t res;
9413 uint8_t buf[2];
9414
9415 if (handle == NULL) /* check handle */
9416 {
9417 return 2; /* return error */
9418 }
9419 if (handle->inited != 1) /* check handle initialization */
9420 {
9421 return 3; /* return error */
9422 }
9423
9424 buf[0] = (offset >> 8) & 0xFF; /* set high */
9425 buf[1] = (offset >> 0) & 0xFF; /* set low */
9426 res = a_mpu9250_write(handle, MPU9250_REG_XA_OFFSET_H, buf, 2); /* write xa offset */
9427 if (res != 0) /* check result */
9428 {
9429 handle->debug_print("mpu9250: write xa offset failed.\n"); /* write xa offset failed*/
9430
9431 return 1; /* return error */
9432 }
9433
9434 return 0; /* success return 0 */
9435}
9436
9449{
9450 uint8_t res;
9451 uint8_t buf[2];
9452
9453 if (handle == NULL) /* check handle */
9454 {
9455 return 2; /* return error */
9456 }
9457 if (handle->inited != 1) /* check handle initialization */
9458 {
9459 return 3; /* return error */
9460 }
9461
9462 res = a_mpu9250_read(handle, MPU9250_REG_XA_OFFSET_H, buf, 2); /* read xa offset */
9463 if (res != 0) /* check result */
9464 {
9465 handle->debug_print("mpu9250: read xa offset failed.\n"); /* read xa offset failed*/
9466
9467 return 1; /* return error */
9468 }
9469 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9470
9471 return 0; /* success return 0 */
9472}
9473
9486{
9487 uint8_t res;
9488 uint8_t buf[2];
9489
9490 if (handle == NULL) /* check handle */
9491 {
9492 return 2; /* return error */
9493 }
9494 if (handle->inited != 1) /* check handle initialization */
9495 {
9496 return 3; /* return error */
9497 }
9498
9499 buf[0] = (offset >> 8) & 0xFF; /* set high */
9500 buf[1] = (offset >> 0) & 0xFF; /* set low */
9501 res = a_mpu9250_write(handle, MPU9250_REG_YA_OFFSET_H, buf, 2); /* write ya offset */
9502 if (res != 0) /* check result */
9503 {
9504 handle->debug_print("mpu9250: write ya offset failed.\n"); /* write ya offset failed*/
9505
9506 return 1; /* return error */
9507 }
9508
9509 return 0; /* success return 0 */
9510}
9511
9524{
9525 uint8_t res;
9526 uint8_t buf[2];
9527
9528 if (handle == NULL) /* check handle */
9529 {
9530 return 2; /* return error */
9531 }
9532 if (handle->inited != 1) /* check handle initialization */
9533 {
9534 return 3; /* return error */
9535 }
9536
9537 res = a_mpu9250_read(handle, MPU9250_REG_YA_OFFSET_H, buf, 2); /* read ya offset */
9538 if (res != 0) /* check result */
9539 {
9540 handle->debug_print("mpu9250: read ya offset failed.\n"); /* read ya offset failed*/
9541
9542 return 1; /* return error */
9543 }
9544 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9545
9546 return 0; /* success return 0 */
9547}
9548
9561{
9562 uint8_t res;
9563 uint8_t buf[2];
9564
9565 if (handle == NULL) /* check handle */
9566 {
9567 return 2; /* return error */
9568 }
9569 if (handle->inited != 1) /* check handle initialization */
9570 {
9571 return 3; /* return error */
9572 }
9573
9574 buf[0] = (offset >> 8) & 0xFF; /* set high */
9575 buf[1] = (offset >> 0) & 0xFF; /* set low */
9576 res = a_mpu9250_write(handle, MPU9250_REG_ZA_OFFSET_H, buf, 2); /* write za offset */
9577 if (res != 0) /* check result */
9578 {
9579 handle->debug_print("mpu9250: write za offset failed.\n"); /* write za offset failed*/
9580
9581 return 1; /* return error */
9582 }
9583
9584 return 0; /* success return 0 */
9585}
9586
9599{
9600 uint8_t res;
9601 uint8_t buf[2];
9602
9603 if (handle == NULL) /* check handle */
9604 {
9605 return 2; /* return error */
9606 }
9607 if (handle->inited != 1) /* check handle initialization */
9608 {
9609 return 3; /* return error */
9610 }
9611
9612 res = a_mpu9250_read(handle, MPU9250_REG_ZA_OFFSET_H, buf, 2); /* read za offset */
9613 if (res != 0) /* check result */
9614 {
9615 handle->debug_print("mpu9250: read za offset failed.\n"); /* read za offset failed*/
9616
9617 return 1; /* return error */
9618 }
9619 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9620
9621 return 0; /* success return 0 */
9622}
9623
9636{
9637 if (handle == NULL) /* check handle */
9638 {
9639 return 2; /* return error */
9640 }
9641 if (handle->inited != 1) /* check handle initialization */
9642 {
9643 return 3; /* return error */
9644 }
9645
9646 *reg = (int16_t)(mg / 0.98f); /* convert real data to register data */
9647
9648 return 0; /* success return 0 */
9649}
9650
9663{
9664 if (handle == NULL) /* check handle */
9665 {
9666 return 2; /* return error */
9667 }
9668 if (handle->inited != 1) /* check handle initialization */
9669 {
9670 return 3; /* return error */
9671 }
9672
9673 *mg = (float)(reg) * 0.98f; /* convert raw data to real data */
9674
9675 return 0; /* success return 0 */
9676}
9677
9689uint8_t mpu9250_set_gyro_x_offset(mpu9250_handle_t *handle, int16_t offset)
9690{
9691 uint8_t res;
9692 uint8_t buf[2];
9693
9694 if (handle == NULL) /* check handle */
9695 {
9696 return 2; /* return error */
9697 }
9698 if (handle->inited != 1) /* check handle initialization */
9699 {
9700 return 3; /* return error */
9701 }
9702
9703 buf[0] = (offset >> 8) & 0xFF; /* set high */
9704 buf[1] = (offset >> 0) & 0xFF; /* set low */
9705 res = a_mpu9250_write(handle, MPU9250_REG_XG_OFFSET_H, buf, 2); /* write xg offset */
9706 if (res != 0) /* check result */
9707 {
9708 handle->debug_print("mpu9250: write xg offset failed.\n"); /* write xg offset failed*/
9709
9710 return 1; /* return error */
9711 }
9712
9713 return 0; /* success return 0 */
9714}
9715
9727uint8_t mpu9250_get_gyro_x_offset(mpu9250_handle_t *handle, int16_t *offset)
9728{
9729 uint8_t res;
9730 uint8_t buf[2];
9731
9732 if (handle == NULL) /* check handle */
9733 {
9734 return 2; /* return error */
9735 }
9736 if (handle->inited != 1) /* check handle initialization */
9737 {
9738 return 3; /* return error */
9739 }
9740
9741 res = a_mpu9250_read(handle, MPU9250_REG_XG_OFFSET_H, buf, 2); /* read xg offset */
9742 if (res != 0) /* check result */
9743 {
9744 handle->debug_print("mpu9250: read xg offset failed.\n"); /* read xg offset failed*/
9745
9746 return 1; /* return error */
9747 }
9748 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9749
9750 return 0; /* success return 0 */
9751}
9752
9764uint8_t mpu9250_set_gyro_y_offset(mpu9250_handle_t *handle, int16_t offset)
9765{
9766 uint8_t res;
9767 uint8_t buf[2];
9768
9769 if (handle == NULL) /* check handle */
9770 {
9771 return 2; /* return error */
9772 }
9773 if (handle->inited != 1) /* check handle initialization */
9774 {
9775 return 3; /* return error */
9776 }
9777
9778 buf[0] = (offset >> 8) & 0xFF; /* set high */
9779 buf[1] = (offset >> 0) & 0xFF; /* set low */
9780 res = a_mpu9250_write(handle, MPU9250_REG_YG_OFFSET_H, buf, 2); /* write yg offset */
9781 if (res != 0) /* check result */
9782 {
9783 handle->debug_print("mpu9250: write yg offset failed.\n"); /* write yg offset failed*/
9784
9785 return 1; /* return error */
9786 }
9787
9788 return 0; /* success return 0 */
9789}
9790
9802uint8_t mpu9250_get_gyro_y_offset(mpu9250_handle_t *handle, int16_t *offset)
9803{
9804 uint8_t res;
9805 uint8_t buf[2];
9806
9807 if (handle == NULL) /* check handle */
9808 {
9809 return 2; /* return error */
9810 }
9811 if (handle->inited != 1) /* check handle initialization */
9812 {
9813 return 3; /* return error */
9814 }
9815
9816 res = a_mpu9250_read(handle, MPU9250_REG_YG_OFFSET_H, buf, 2); /* read yg offset */
9817 if (res != 0) /* check result */
9818 {
9819 handle->debug_print("mpu9250: read yg offset failed.\n"); /* read yg offset failed*/
9820
9821 return 1; /* return error */
9822 }
9823 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9824
9825 return 0; /* success return 0 */
9826}
9827
9839uint8_t mpu9250_set_gyro_z_offset(mpu9250_handle_t *handle, int16_t offset)
9840{
9841 uint8_t res;
9842 uint8_t buf[2];
9843
9844 if (handle == NULL) /* check handle */
9845 {
9846 return 2; /* return error */
9847 }
9848 if (handle->inited != 1) /* check handle initialization */
9849 {
9850 return 3; /* return error */
9851 }
9852
9853 buf[0] = (offset >> 8) & 0xFF; /* set high */
9854 buf[1] = (offset >> 0) & 0xFF; /* set low */
9855 res = a_mpu9250_write(handle, MPU9250_REG_ZG_OFFSET_H, buf, 2); /* write zg offset */
9856 if (res != 0) /* check result */
9857 {
9858 handle->debug_print("mpu9250: write zg offset failed.\n"); /* write zg offset failed*/
9859
9860 return 1; /* return error */
9861 }
9862
9863 return 0; /* success return 0 */
9864}
9865
9877uint8_t mpu9250_get_gyro_z_offset(mpu9250_handle_t *handle, int16_t *offset)
9878{
9879 uint8_t res;
9880 uint8_t buf[2];
9881
9882 if (handle == NULL) /* check handle */
9883 {
9884 return 2; /* return error */
9885 }
9886 if (handle->inited != 1) /* check handle initialization */
9887 {
9888 return 3; /* return error */
9889 }
9890
9891 res = a_mpu9250_read(handle, MPU9250_REG_ZG_OFFSET_H, buf, 2); /* read zg offset */
9892 if (res != 0) /* check result */
9893 {
9894 handle->debug_print("mpu9250: read zg offset failed.\n"); /* read zg offset failed*/
9895
9896 return 1; /* return error */
9897 }
9898 *offset = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set the offset */
9899
9900 return 0; /* success return 0 */
9901}
9902
9914uint8_t mpu9250_gyro_offset_convert_to_register(mpu9250_handle_t *handle, float dps, int16_t *reg)
9915{
9916 if (handle == NULL) /* check handle */
9917 {
9918 return 2; /* return error */
9919 }
9920 if (handle->inited != 1) /* check handle initialization */
9921 {
9922 return 3; /* return error */
9923 }
9924
9925 *reg = (int16_t)(dps / 0.0305f); /* convert real data to register data */
9926
9927 return 0; /* success return 0 */
9928}
9929
9941uint8_t mpu9250_gyro_offset_convert_to_data(mpu9250_handle_t *handle, int16_t reg, float *dps)
9942{
9943 if (handle == NULL) /* check handle */
9944 {
9945 return 2; /* return error */
9946 }
9947 if (handle->inited != 1) /* check handle initialization */
9948 {
9949 return 3; /* return error */
9950 }
9951
9952 *dps = (float)(reg) * 0.0305f; /* convert raw data to real data */
9953
9954 return 0; /* success return 0 */
9955}
9956
9968uint8_t mpu9250_set_motion_threshold(mpu9250_handle_t *handle, uint8_t threshold)
9969{
9970 uint8_t res;
9971
9972 if (handle == NULL) /* check handle */
9973 {
9974 return 2; /* return error */
9975 }
9976 if (handle->inited != 1) /* check handle initialization */
9977 {
9978 return 3; /* return error */
9979 }
9980
9981 res = a_mpu9250_write(handle, MPU9250_REG_WOM_THR, (uint8_t *)&threshold, 1); /* write motion threshold */
9982 if (res != 0) /* check result */
9983 {
9984 handle->debug_print("mpu9250: write motion threshold failed.\n"); /* write motion threshold failed*/
9985
9986 return 1; /* return error */
9987 }
9988
9989 return 0; /* success return 0 */
9990}
9991
10003uint8_t mpu9250_get_motion_threshold(mpu9250_handle_t *handle, uint8_t *threshold)
10004{
10005 uint8_t res;
10006
10007 if (handle == NULL) /* check handle */
10008 {
10009 return 2; /* return error */
10010 }
10011 if (handle->inited != 1) /* check handle initialization */
10012 {
10013 return 3; /* return error */
10014 }
10015
10016 res = a_mpu9250_read(handle, MPU9250_REG_WOM_THR, (uint8_t *)threshold, 1); /* read motion threshold */
10017 if (res != 0) /* check result */
10018 {
10019 handle->debug_print("mpu9250: read motion threshold failed.\n"); /* read motion threshold failed*/
10020
10021 return 1; /* return error */
10022 }
10023
10024 return 0; /* success return 0 */
10025}
10026
10039{
10040 if (handle == NULL) /* check handle */
10041 {
10042 return 2; /* return error */
10043 }
10044 if (handle->inited != 1) /* check handle initialization */
10045 {
10046 return 3; /* return error */
10047 }
10048
10049 *reg = (uint8_t)(mg / 4.0f); /* convert real data to register data */
10050
10051 return 0; /* success return 0 */
10052}
10053
10065uint8_t mpu9250_motion_threshold_convert_to_data(mpu9250_handle_t *handle, uint8_t reg, float *mg)
10066{
10067 if (handle == NULL) /* check handle */
10068 {
10069 return 2; /* return error */
10070 }
10071 if (handle->inited != 1) /* check handle initialization */
10072 {
10073 return 3; /* return error */
10074 }
10075
10076 *mg = (float)(reg) * 4.0f; /* convert raw data to real data */
10077
10078 return 0; /* success return 0 */
10079}
10080
10093uint8_t mpu9250_self_test(mpu9250_handle_t *handle, int32_t gyro_offset_raw[3], int32_t accel_offset_raw[3])
10094{
10095 uint8_t res;
10096 uint8_t prev;
10097 int32_t gyro_offset_raw_st[3];
10098 int32_t accel_offset_raw_st[3];
10099
10100 if (handle == NULL) /* check handle */
10101 {
10102 return 2; /* return error */
10103 }
10104 if (handle->inited != 1) /* check handle initialization */
10105 {
10106 return 3; /* return error */
10107 }
10108
10109 res = a_mpu9250_get_st_biases(handle, gyro_offset_raw, accel_offset_raw, 0); /* get st biases */
10110 if (res != 0) /* check result */
10111 {
10112 handle->debug_print("mpu9250: get st biases failed.\n"); /* get st biases failed */
10113
10114 return 1; /* return error */
10115 }
10116 res = a_mpu9250_get_st_biases(handle, gyro_offset_raw_st, accel_offset_raw_st, 1); /* get st biases */
10117 if (res != 0) /* check result */
10118 {
10119 handle->debug_print("mpu9250: get st biases failed.\n"); /* get st biases failed */
10120
10121 return 1; /* return error */
10122 }
10123 res = a_mpu9250_accel_self_test(handle, accel_offset_raw, accel_offset_raw_st); /* accel self test */
10124 if (res != 0) /* check result */
10125 {
10126 handle->debug_print("mpu9250: accel self test failed.\n"); /* accel self test failed */
10127
10128 return 1; /* return error */
10129 }
10130 res = a_mpu9250_gyro_self_test(handle, gyro_offset_raw, gyro_offset_raw_st); /* gyro self test */
10131 if (res != 0) /* check result */
10132 {
10133 handle->debug_print("mpu9250: gyro self test failed.\n"); /* gyro self test failed */
10134
10135 return 1; /* return error */
10136 }
10137
10138 prev = 1 << 7; /* reset the device */
10139 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, &prev, 1); /* write pwr mgmt 1 */
10140 if (res != 0) /* check the result */
10141 {
10142 handle->debug_print("mpu9250: write pwr mgmt 1 failed.\n"); /* write pwr mgmt 1 failed */
10143
10144 return 1; /* return error */
10145 }
10146 handle->delay_ms(100); /* delay 100ms */
10147 res = a_mpu9250_read(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* read config */
10148 if (res != 0) /* check result */
10149 {
10150 handle->debug_print("mpu9250: read power management 1 failed.\n"); /* read power management 1 failed */
10151
10152 return 1; /* return error */
10153 }
10154 prev &= ~(1 << 6); /* clear config */
10155 res = a_mpu9250_write(handle, MPU9250_REG_PWR_MGMT_1, (uint8_t *)&prev, 1); /* write config */
10156 if (res != 0) /* check result */
10157 {
10158 handle->debug_print("mpu9250: write power management 1 failed.\n"); /* write power management 1 failed */
10159
10160 return 1; /* return error */
10161 }
10162
10163 return 0; /* success return 0 */
10164}
10165
10178{
10179 uint8_t res;
10180 uint8_t prev;
10181
10182 if (handle == NULL) /* check handle */
10183 {
10184 return 2; /* return error */
10185 }
10186 if (handle->inited != 1) /* check handle initialization */
10187 {
10188 return 3; /* return error */
10189 }
10190
10191 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10192 if (res != 0) /* check result */
10193 {
10194 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10195
10196 return 1; /* return error */
10197 }
10198 prev &= ~0xF; /* clear the buffer */
10199 prev |= clk; /* set the clock */
10200 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* write i2c mst ctrl */
10201 if (res != 0) /* check result */
10202 {
10203 handle->debug_print("mpu9250: write i2c mst ctrl failed.\n"); /* write i2c mst ctrl failed */
10204
10205 return 1; /* return error */
10206 }
10207
10208 return 0; /* success return 0 */
10209}
10210
10223{
10224 uint8_t res;
10225 uint8_t prev;
10226
10227 if (handle == NULL) /* check handle */
10228 {
10229 return 2; /* return error */
10230 }
10231 if (handle->inited != 1) /* check handle initialization */
10232 {
10233 return 3; /* return error */
10234 }
10235
10236 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10237 if (res != 0) /* check result */
10238 {
10239 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10240
10241 return 1; /* return error */
10242 }
10243 *clk = (mpu9250_iic_clock_t)(prev & 0x0F); /* get the clock */
10244
10245 return 0; /* success return 0 */
10246}
10247
10260{
10261 uint8_t res;
10262 uint8_t prev;
10263
10264 if (handle == NULL) /* check handle */
10265 {
10266 return 2; /* return error */
10267 }
10268 if (handle->inited != 1) /* check handle initialization */
10269 {
10270 return 3; /* return error */
10271 }
10272
10273 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10274 if (res != 0) /* check result */
10275 {
10276 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10277
10278 return 1; /* return error */
10279 }
10280 prev &= ~(1 << 7); /* clear the settings */
10281 prev |= enable << 7; /* set the bool */
10282 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* write i2c mst ctrl */
10283 if (res != 0) /* check result */
10284 {
10285 handle->debug_print("mpu9250: write i2c mst ctrl failed.\n"); /* write i2c mst ctrl failed */
10286
10287 return 1; /* return error */
10288 }
10289
10290 return 0; /* success return 0 */
10291}
10292
10305{
10306 uint8_t res;
10307 uint8_t prev;
10308
10309 if (handle == NULL) /* check handle */
10310 {
10311 return 2; /* return error */
10312 }
10313 if (handle->inited != 1) /* check handle initialization */
10314 {
10315 return 3; /* return error */
10316 }
10317
10318 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10319 if (res != 0) /* check result */
10320 {
10321 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10322
10323 return 1; /* return error */
10324 }
10325 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
10326
10327 return 0; /* success return 0 */
10328}
10329
10342{
10343 uint8_t res;
10344 uint8_t prev;
10345
10346 if (handle == NULL) /* check handle */
10347 {
10348 return 2; /* return error */
10349 }
10350 if (handle->inited != 1) /* check handle initialization */
10351 {
10352 return 3; /* return error */
10353 }
10354
10355 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10356 if (res != 0) /* check result */
10357 {
10358 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10359
10360 return 1; /* return error */
10361 }
10362 prev &= ~(1 << 6); /* clear the settings */
10363 prev |= enable << 6; /* set the bool */
10364 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* write i2c mst ctrl */
10365 if (res != 0) /* check result */
10366 {
10367 handle->debug_print("mpu9250: write i2c mst ctrl failed.\n"); /* write i2c mst ctrl failed */
10368
10369 return 1; /* return error */
10370 }
10371
10372 return 0; /* success return 0 */
10373}
10374
10387{
10388 uint8_t res;
10389 uint8_t prev;
10390
10391 if (handle == NULL) /* check handle */
10392 {
10393 return 2; /* return error */
10394 }
10395 if (handle->inited != 1) /* check handle initialization */
10396 {
10397 return 3; /* return error */
10398 }
10399
10400 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10401 if (res != 0) /* check result */
10402 {
10403 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10404
10405 return 1; /* return error */
10406 }
10407 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
10408
10409 return 0; /* success return 0 */
10410}
10411
10424{
10425 uint8_t res;
10426 uint8_t prev;
10427
10428 if (handle == NULL) /* check handle */
10429 {
10430 return 2; /* return error */
10431 }
10432 if (handle->inited != 1) /* check handle initialization */
10433 {
10434 return 3; /* return error */
10435 }
10436
10437 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10438 if (res != 0) /* check result */
10439 {
10440 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10441
10442 return 1; /* return error */
10443 }
10444 prev &= ~(1 << 4); /* clear the settings */
10445 prev |= mode << 4; /* set the mode */
10446 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* write i2c mst ctrl */
10447 if (res != 0) /* check result */
10448 {
10449 handle->debug_print("mpu9250: write i2c mst ctrl failed.\n"); /* write i2c mst ctrl failed */
10450
10451 return 1; /* return error */
10452 }
10453
10454 return 0; /* success return 0 */
10455}
10456
10469{
10470 uint8_t res;
10471 uint8_t prev;
10472
10473 if (handle == NULL) /* check handle */
10474 {
10475 return 2; /* return error */
10476 }
10477 if (handle->inited != 1) /* check handle initialization */
10478 {
10479 return 3; /* return error */
10480 }
10481
10482 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10483 if (res != 0) /* check result */
10484 {
10485 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10486
10487 return 1; /* return error */
10488 }
10489 *mode = (mpu9250_iic_read_mode_t)((prev >> 4) & 0x1); /* get the mode */
10490
10491 return 0; /* success return 0 */
10492}
10493
10508{
10509 uint8_t res;
10510 uint8_t prev;
10511
10512 if (handle == NULL) /* check handle */
10513 {
10514 return 2; /* return error */
10515 }
10516 if (handle->inited != 1) /* check handle initialization */
10517 {
10518 return 3; /* return error */
10519 }
10520
10521 if ((slave == MPU9250_IIC_SLAVE_0) ||
10522 (slave == MPU9250_IIC_SLAVE_1) ||
10523 (slave == MPU9250_IIC_SLAVE_2)
10524 ) /* slave0-2 */
10525 {
10526 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* read fifo enable */
10527 if (res != 0) /* check result */
10528 {
10529 handle->debug_print("mpu9250: read fifo enable failed.\n"); /* read fifo enable failed */
10530
10531 return 1; /* return error */
10532 }
10533 prev &= ~(1 << slave); /* clear the settings */
10534 prev |= enable << slave; /* set the bool */
10535 res = a_mpu9250_write(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* write fifo enable ctrl */
10536 if (res != 0) /* check result */
10537 {
10538 handle->debug_print("mpu9250: write fifo enable failed.\n"); /* write fifo enable failed */
10539
10540 return 1; /* return error */
10541 }
10542 }
10543 else if (slave == MPU9250_IIC_SLAVE_3) /* slave3 */
10544 {
10545 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10546 if (res != 0) /* check result */
10547 {
10548 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10549
10550 return 1; /* return error */
10551 }
10552 prev &= ~(1 << 5); /* clear the settings */
10553 prev |= enable << 5; /* set the bool */
10554 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* write i2c mst ctrl */
10555 if (res != 0) /* check result */
10556 {
10557 handle->debug_print("mpu9250: write i2c mst ctrl failed.\n"); /* write i2c mst ctrl failed */
10558
10559 return 1; /* return error */
10560 }
10561 }
10562 else
10563 {
10564 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
10565
10566 return 4; /* return error */
10567 }
10568
10569 return 0; /* success return 0 */
10570}
10571
10586{
10587 uint8_t res;
10588 uint8_t prev;
10589
10590 if (handle == NULL) /* check handle */
10591 {
10592 return 2; /* return error */
10593 }
10594 if (handle->inited != 1) /* check handle initialization */
10595 {
10596 return 3; /* return error */
10597 }
10598
10599 if ((slave == MPU9250_IIC_SLAVE_0) ||
10600 (slave == MPU9250_IIC_SLAVE_1) ||
10601 (slave == MPU9250_IIC_SLAVE_2)
10602 ) /* slave0-2 */
10603 {
10604 res = a_mpu9250_read(handle, MPU9250_REG_FIFO_EN, (uint8_t *)&prev, 1); /* read fifo enable */
10605 if (res != 0) /* check result */
10606 {
10607 handle->debug_print("mpu9250: read fifo enable failed.\n"); /* read fifo enable failed */
10608
10609 return 1; /* return error */
10610 }
10611 *enable = (mpu9250_bool_t)((prev >> slave) & 0x1); /* get the bool */
10612 }
10613 else if (slave == MPU9250_IIC_SLAVE_3) /* slave3 */
10614 {
10615 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_CTRL, (uint8_t *)&prev, 1); /* read i2c mst ctrl */
10616 if (res != 0) /* check result */
10617 {
10618 handle->debug_print("mpu9250: read i2c mst ctrl failed.\n"); /* read i2c mst ctrl failed */
10619
10620 return 1; /* return error */
10621 }
10622 *enable = (mpu9250_bool_t)((prev >> 5) & 0x01); /* get the bool */
10623 }
10624 else
10625 {
10626 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
10627
10628 return 4; /* return error */
10629 }
10630
10631 return 0; /* success return 0 */
10632}
10633
10648{
10649 uint8_t res;
10650 uint8_t prev;
10651
10652 if (handle == NULL) /* check handle */
10653 {
10654 return 2; /* return error */
10655 }
10656 if (handle->inited != 1) /* check handle initialization */
10657 {
10658 return 3; /* return error */
10659 }
10660
10661 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
10662 {
10663 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* read i2c slv0 addr */
10664 if (res != 0) /* check result */
10665 {
10666 handle->debug_print("mpu9250: read i2c slv0 addr failed.\n"); /* read i2c slv0 addr failed */
10667
10668 return 1; /* return error */
10669 }
10670 prev &= ~(1 << 7); /* clear the settings */
10671 prev |= mode << 7; /* set the bool */
10672 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* write i2c slv0 addr */
10673 if (res != 0) /* check result */
10674 {
10675 handle->debug_print("mpu9250: write i2c slv0 addr failed.\n"); /* write i2c slv0 addr failed */
10676
10677 return 1; /* return error */
10678 }
10679 }
10680 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
10681 {
10682 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* read i2c slv1 addr */
10683 if (res != 0) /* check result */
10684 {
10685 handle->debug_print("mpu9250: read i2c slv1 addr failed.\n"); /* read i2c slv1 addr failed */
10686
10687 return 1; /* return error */
10688 }
10689 prev &= ~(1 << 7); /* clear the settings */
10690 prev |= mode << 7; /* set the bool */
10691 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* write i2c slv1 addr */
10692 if (res != 0) /* check result */
10693 {
10694 handle->debug_print("mpu9250: write i2c slv1 addr failed.\n"); /* write i2c slv1 addr failed */
10695
10696 return 1; /* return error */
10697 }
10698 }
10699 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
10700 {
10701 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* read i2c slv2 addr */
10702 if (res != 0) /* check result */
10703 {
10704 handle->debug_print("mpu9250: read i2c slv2 addr failed.\n"); /* read i2c slv2 addr failed */
10705
10706 return 1; /* return error */
10707 }
10708 prev &= ~(1 << 7); /* clear the settings */
10709 prev |= mode << 7; /* set the bool */
10710 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* write i2c slv2 addr */
10711 if (res != 0) /* check result */
10712 {
10713 handle->debug_print("mpu9250: write i2c slv2 addr failed.\n"); /* write i2c slv2 addr failed */
10714
10715 return 1; /* return error */
10716 }
10717 }
10718 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
10719 {
10720 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* read i2c slv3 addr */
10721 if (res != 0) /* check result */
10722 {
10723 handle->debug_print("mpu9250: read i2c slv3 addr failed.\n"); /* read i2c slv3 addr failed */
10724
10725 return 1; /* return error */
10726 }
10727 prev &= ~(1 << 7); /* clear the settings */
10728 prev |= mode << 7; /* set the bool */
10729 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* write i2c slv3 addr */
10730 if (res != 0) /* check result */
10731 {
10732 handle->debug_print("mpu9250: write i2c slv3 addr failed.\n"); /* write i2c slv3 addr failed */
10733
10734 return 1; /* return error */
10735 }
10736 }
10737 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
10738 {
10739 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* read i2c slv4 addr */
10740 if (res != 0) /* check result */
10741 {
10742 handle->debug_print("mpu9250: read i2c slv4 addr failed.\n"); /* read i2c slv4 addr failed */
10743
10744 return 1; /* return error */
10745 }
10746 prev &= ~(1 << 7); /* clear the settings */
10747 prev |= mode << 7; /* set the bool */
10748 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* write i2c slv4 addr */
10749 if (res != 0) /* check result */
10750 {
10751 handle->debug_print("mpu9250: write i2c slv4 addr failed.\n"); /* write i2c slv4 addr failed */
10752
10753 return 1; /* return error */
10754 }
10755 }
10756 else
10757 {
10758 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
10759
10760 return 4; /* return error */
10761 }
10762
10763 return 0; /* success return 0 */
10764}
10765
10780{
10781 uint8_t res;
10782 uint8_t prev;
10783
10784 if (handle == NULL) /* check handle */
10785 {
10786 return 2; /* return error */
10787 }
10788 if (handle->inited != 1) /* check handle initialization */
10789 {
10790 return 3; /* return error */
10791 }
10792
10793 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
10794 {
10795 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* read i2c slv0 addr */
10796 if (res != 0) /* check result */
10797 {
10798 handle->debug_print("mpu9250: read i2c slv0 addr failed.\n"); /* read i2c slv0 addr failed */
10799
10800 return 1; /* return error */
10801 }
10802 *mode = (mpu9250_iic_mode_t)((prev >> 7) & 0x1); /* get the mode */
10803 }
10804 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
10805 {
10806 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* read i2c slv1 addr */
10807 if (res != 0) /* check result */
10808 {
10809 handle->debug_print("mpu9250: read i2c slv1 addr failed.\n"); /* read i2c slv1 addr failed */
10810
10811 return 1; /* return error */
10812 }
10813 *mode = (mpu9250_iic_mode_t)((prev >> 7) & 0x1); /* get the mode */
10814 }
10815 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
10816 {
10817 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* read i2c slv2 addr */
10818 if (res != 0) /* check result */
10819 {
10820 handle->debug_print("mpu9250: read i2c slv2 addr failed.\n"); /* read i2c slv2 addr failed */
10821
10822 return 1; /* return error */
10823 }
10824 *mode = (mpu9250_iic_mode_t)((prev >> 7) & 0x1); /* get the mode */
10825 }
10826 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
10827 {
10828 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* read i2c slv3 addr */
10829 if (res != 0) /* check result */
10830 {
10831 handle->debug_print("mpu9250: read i2c slv3 addr failed.\n"); /* read i2c slv3 addr failed */
10832
10833 return 1; /* return error */
10834 }
10835 *mode = (mpu9250_iic_mode_t)((prev >> 7) & 0x1); /* get the mode */
10836 }
10837 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
10838 {
10839 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* read i2c slv4 addr */
10840 if (res != 0) /* check result */
10841 {
10842 handle->debug_print("mpu9250: read i2c slv4 addr failed.\n"); /* read i2c slv4 addr failed */
10843
10844 return 1; /* return error */
10845 }
10846 *mode = (mpu9250_iic_mode_t)((prev >> 7) & 0x1); /* get the mode */
10847 }
10848 else
10849 {
10850 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
10851
10852 return 4; /* return error */
10853 }
10854
10855 return 0; /* success return 0 */
10856}
10857
10871uint8_t mpu9250_set_iic_address(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t addr_7bit)
10872{
10873 uint8_t res;
10874 uint8_t prev;
10875
10876 if (handle == NULL) /* check handle */
10877 {
10878 return 2; /* return error */
10879 }
10880 if (handle->inited != 1) /* check handle initialization */
10881 {
10882 return 3; /* return error */
10883 }
10884
10885 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
10886 {
10887 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* read i2c slv0 addr */
10888 if (res != 0) /* check result */
10889 {
10890 handle->debug_print("mpu9250: read i2c slv0 addr failed.\n"); /* read i2c slv0 addr failed */
10891
10892 return 1; /* return error */
10893 }
10894 prev &= ~0x7F; /* clear the settings */
10895 prev |= addr_7bit & 0x7F; /* set the address */
10896 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* write i2c slv0 addr */
10897 if (res != 0) /* check result */
10898 {
10899 handle->debug_print("mpu9250: write i2c slv0 addr failed.\n"); /* write i2c slv0 addr failed */
10900
10901 return 1; /* return error */
10902 }
10903 }
10904 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
10905 {
10906 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* read i2c slv1 addr */
10907 if (res != 0) /* check result */
10908 {
10909 handle->debug_print("mpu9250: read i2c slv1 addr failed.\n"); /* read i2c slv1 addr failed */
10910
10911 return 1; /* return error */
10912 }
10913 prev &= ~0x7F; /* clear the settings */
10914 prev |= addr_7bit & 0x7F; /* set the address */
10915 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* write i2c slv1 addr */
10916 if (res != 0) /* check result */
10917 {
10918 handle->debug_print("mpu9250: write i2c slv1 addr failed.\n"); /* write i2c slv1 addr failed */
10919
10920 return 1; /* return error */
10921 }
10922 }
10923 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
10924 {
10925 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* read i2c slv2 addr */
10926 if (res != 0) /* check result */
10927 {
10928 handle->debug_print("mpu9250: read i2c slv2 addr failed.\n"); /* read i2c slv2 addr failed */
10929
10930 return 1; /* return error */
10931 }
10932 prev &= ~0x7F; /* clear the settings */
10933 prev |= addr_7bit & 0x7F; /* set the address */
10934 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* write i2c slv2 addr */
10935 if (res != 0) /* check result */
10936 {
10937 handle->debug_print("mpu9250: write i2c slv2 addr failed.\n"); /* write i2c slv2 addr failed */
10938
10939 return 1; /* return error */
10940 }
10941 }
10942 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
10943 {
10944 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* read i2c slv3 addr */
10945 if (res != 0) /* check result */
10946 {
10947 handle->debug_print("mpu9250: read i2c slv3 addr failed.\n"); /* read i2c slv3 addr failed */
10948
10949 return 1; /* return error */
10950 }
10951 prev &= ~0x7F; /* clear the settings */
10952 prev |= addr_7bit & 0x7F; /* set the address */
10953 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* write i2c slv3 addr */
10954 if (res != 0) /* check result */
10955 {
10956 handle->debug_print("mpu9250: write i2c slv3 addr failed.\n"); /* write i2c slv3 addr failed */
10957
10958 return 1; /* return error */
10959 }
10960 }
10961 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
10962 {
10963 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* read i2c slv4 addr */
10964 if (res != 0) /* check result */
10965 {
10966 handle->debug_print("mpu9250: read i2c slv4 addr failed.\n"); /* read i2c slv4 addr failed */
10967
10968 return 1; /* return error */
10969 }
10970 prev &= ~0x7F; /* clear the settings */
10971 prev |= addr_7bit & 0x7F; /* set the address */
10972 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* write i2c slv4 addr */
10973 if (res != 0) /* check result */
10974 {
10975 handle->debug_print("mpu9250: write i2c slv4 addr failed.\n"); /* write i2c slv4 addr failed */
10976
10977 return 1; /* return error */
10978 }
10979 }
10980 else
10981 {
10982 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
10983
10984 return 4; /* return error */
10985 }
10986
10987 return 0; /* success return 0 */
10988}
10989
11003uint8_t mpu9250_get_iic_address(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t *addr_7bit)
11004{
11005 uint8_t res;
11006 uint8_t prev;
11007
11008 if (handle == NULL) /* check handle */
11009 {
11010 return 2; /* return error */
11011 }
11012 if (handle->inited != 1) /* check handle initialization */
11013 {
11014 return 3; /* return error */
11015 }
11016
11017 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11018 {
11019 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_ADDR, (uint8_t *)&prev, 1); /* read i2c slv0 addr */
11020 if (res != 0) /* check result */
11021 {
11022 handle->debug_print("mpu9250: read i2c slv0 addr failed.\n"); /* read i2c slv0 addr failed */
11023
11024 return 1; /* return error */
11025 }
11026 *addr_7bit = prev & 0x7F; /* get the address */
11027 }
11028 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11029 {
11030 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_ADDR, (uint8_t *)&prev, 1); /* read i2c slv1 addr */
11031 if (res != 0) /* check result */
11032 {
11033 handle->debug_print("mpu9250: read i2c slv1 addr failed.\n"); /* read i2c slv1 addr failed */
11034
11035 return 1; /* return error */
11036 }
11037 *addr_7bit = prev & 0x7F; /* get the address */
11038 }
11039 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11040 {
11041 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_ADDR, (uint8_t *)&prev, 1); /* read i2c slv2 addr */
11042 if (res != 0) /* check result */
11043 {
11044 handle->debug_print("mpu9250: read i2c slv2 addr failed.\n"); /* read i2c slv2 addr failed */
11045
11046 return 1; /* return error */
11047 }
11048 *addr_7bit = prev & 0x7F; /* get the address */
11049 }
11050 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11051 {
11052 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_ADDR, (uint8_t *)&prev, 1); /* read i2c slv3 addr */
11053 if (res != 0) /* check result */
11054 {
11055 handle->debug_print("mpu9250: read i2c slv3 addr failed.\n"); /* read i2c slv3 addr failed */
11056
11057 return 1; /* return error */
11058 }
11059 *addr_7bit = prev & 0x7F; /* get the address */
11060 }
11061 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
11062 {
11063 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_ADDR, (uint8_t *)&prev, 1); /* read i2c slv4 addr */
11064 if (res != 0) /* check result */
11065 {
11066 handle->debug_print("mpu9250: read i2c slv4 addr failed.\n"); /* read i2c slv4 addr failed */
11067
11068 return 1; /* return error */
11069 }
11070 *addr_7bit = prev & 0x7F; /* get the address */
11071 }
11072 else
11073 {
11074 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11075
11076 return 4; /* return error */
11077 }
11078
11079 return 0; /* success return 0 */
11080}
11081
11096{
11097 uint8_t res;
11098
11099 if (handle == NULL) /* check handle */
11100 {
11101 return 2; /* return error */
11102 }
11103 if (handle->inited != 1) /* check handle initialization */
11104 {
11105 return 3; /* return error */
11106 }
11107
11108 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11109 {
11110 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_REG, (uint8_t *)&reg, 1); /* write i2c slv0 reg */
11111 if (res != 0) /* check result */
11112 {
11113 handle->debug_print("mpu9250: write i2c slv0 reg failed.\n"); /* write i2c slv0 reg failed */
11114
11115 return 1; /* return error */
11116 }
11117 }
11118 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11119 {
11120 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_REG, (uint8_t *)&reg, 1); /* write i2c slv1 reg */
11121 if (res != 0) /* check result */
11122 {
11123 handle->debug_print("mpu9250: write i2c slv1 reg failed.\n"); /* write i2c slv1 reg failed */
11124
11125 return 1; /* return error */
11126 }
11127 }
11128 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11129 {
11130 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_REG, (uint8_t *)&reg, 1); /* write i2c slv2 reg */
11131 if (res != 0) /* check result */
11132 {
11133 handle->debug_print("mpu9250: write i2c slv2 reg failed.\n"); /* write i2c slv2 reg failed */
11134
11135 return 1; /* return error */
11136 }
11137 }
11138 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11139 {
11140 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_REG, (uint8_t *)&reg, 1); /* write i2c slv3 reg */
11141 if (res != 0) /* check result */
11142 {
11143 handle->debug_print("mpu9250: write i2c slv3 reg failed.\n"); /* write i2c slv3 reg failed */
11144
11145 return 1; /* return error */
11146 }
11147 }
11148 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
11149 {
11150 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_REG, (uint8_t *)&reg, 1); /* write i2c slv4 reg */
11151 if (res != 0) /* check result */
11152 {
11153 handle->debug_print("mpu9250: write i2c slv4 reg failed.\n"); /* write i2c slv4 reg failed */
11154
11155 return 1; /* return error */
11156 }
11157 }
11158 else
11159 {
11160 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11161
11162 return 4; /* return error */
11163 }
11164
11165 return 0; /* success return 0 */
11166}
11167
11182{
11183 uint8_t res;
11184
11185 if (handle == NULL) /* check handle */
11186 {
11187 return 2; /* return error */
11188 }
11189 if (handle->inited != 1) /* check handle initialization */
11190 {
11191 return 3; /* return error */
11192 }
11193
11194 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11195 {
11196 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_REG, (uint8_t *)reg, 1); /* read i2c slv0 reg */
11197 if (res != 0) /* check result */
11198 {
11199 handle->debug_print("mpu9250: read i2c slv0 reg failed.\n"); /* read i2c slv0 reg failed */
11200
11201 return 1; /* return error */
11202 }
11203 }
11204 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11205 {
11206 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_REG, (uint8_t *)reg, 1); /* read i2c slv1 reg */
11207 if (res != 0) /* check result */
11208 {
11209 handle->debug_print("mpu9250: read i2c slv1 reg failed.\n"); /* read i2c slv1 reg failed */
11210
11211 return 1; /* return error */
11212 }
11213 }
11214 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11215 {
11216 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_REG, (uint8_t *)reg, 1); /* read i2c slv2 reg */
11217 if (res != 0) /* check result */
11218 {
11219 handle->debug_print("mpu9250: read i2c slv2 reg failed.\n"); /* read i2c slv2 reg failed */
11220
11221 return 1; /* return error */
11222 }
11223 }
11224 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11225 {
11226 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_REG, (uint8_t *)reg, 1); /* read i2c slv3 reg */
11227 if (res != 0) /* check result */
11228 {
11229 handle->debug_print("mpu9250: read i2c slv3 reg failed.\n"); /* read i2c slv3 reg failed */
11230
11231 return 1; /* return error */
11232 }
11233 }
11234 else if (slave == MPU9250_IIC_SLAVE_4) /* slave4 */
11235 {
11236 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_REG, (uint8_t *)reg, 1); /* read i2c slv4 reg */
11237 if (res != 0) /* check result */
11238 {
11239 handle->debug_print("mpu9250: read i2c slv4 reg failed.\n"); /* read i2c slv4 reg failed */
11240
11241 return 1; /* return error */
11242 }
11243 }
11244 else
11245 {
11246 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11247
11248 return 4; /* return error */
11249 }
11250
11251 return 0; /* success return 0 */
11252}
11253
11268{
11269 uint8_t res;
11270
11271 if (handle == NULL) /* check handle */
11272 {
11273 return 2; /* return error */
11274 }
11275 if (handle->inited != 1) /* check handle initialization */
11276 {
11277 return 3; /* return error */
11278 }
11279
11280 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11281 {
11282 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_DO, (uint8_t *)&data, 1); /* write i2c slv0 do */
11283 if (res != 0) /* check result */
11284 {
11285 handle->debug_print("mpu9250: write i2c slv0 do failed.\n"); /* write i2c slv0 do fail */
11286
11287 return 1; /* return error */
11288 }
11289 }
11290 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11291 {
11292 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_DO, (uint8_t *)&data, 1); /* write i2c slv1 do */
11293 if (res != 0) /* check result */
11294 {
11295 handle->debug_print("mpu9250: write i2c slv1 do failed.\n"); /* write i2c slv1 do fail */
11296
11297 return 1; /* return error */
11298 }
11299 }
11300 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11301 {
11302 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_DO, (uint8_t *)&data, 1); /* write i2c slv2 do */
11303 if (res != 0) /* check result */
11304 {
11305 handle->debug_print("mpu9250: write i2c slv2 do failed.\n"); /* write i2c slv2 do fail */
11306
11307 return 1; /* return error */
11308 }
11309 }
11310 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11311 {
11312 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_DO, (uint8_t *)&data, 1); /* write i2c slv3 do */
11313 if (res != 0) /* check result */
11314 {
11315 handle->debug_print("mpu9250: write i2c slv3 do failed.\n"); /* write i2c slv3 do fail */
11316
11317 return 1; /* return error */
11318 }
11319 }
11320 else
11321 {
11322 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11323
11324 return 4; /* return error */
11325 }
11326
11327 return 0; /* success return 0 */
11328}
11329
11344{
11345 uint8_t res;
11346
11347 if (handle == NULL) /* check handle */
11348 {
11349 return 2; /* return error */
11350 }
11351 if (handle->inited != 1) /* check handle initialization */
11352 {
11353 return 3; /* return error */
11354 }
11355
11356 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11357 {
11358 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_DO, (uint8_t *)data, 1); /* read i2c slv0 do */
11359 if (res != 0) /* check result */
11360 {
11361 handle->debug_print("mpu9250: read i2c slv0 do failed.\n"); /* read i2c slv0 do failed */
11362
11363 return 1; /* return error */
11364 }
11365 }
11366 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11367 {
11368 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_DO, (uint8_t *)data, 1); /* read i2c slv1 do */
11369 if (res != 0) /* check result */
11370 {
11371 handle->debug_print("mpu9250: read i2c slv1 do failed.\n"); /* read i2c slv1 do failed */
11372
11373 return 1; /* return error */
11374 }
11375 }
11376 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11377 {
11378 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_DO, (uint8_t *)data, 1); /* read i2c slv2 do */
11379 if (res != 0) /* check result */
11380 {
11381 handle->debug_print("mpu9250: read i2c slv2 do failed.\n"); /* read i2c slv2 do failed */
11382
11383 return 1; /* return error */
11384 }
11385 }
11386 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11387 {
11388 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_DO, (uint8_t *)data, 1); /* read i2c slv3 do */
11389 if (res != 0) /* check result */
11390 {
11391 handle->debug_print("mpu9250: read i2c slv3 do failed.\n"); /* read i2c slv3 do failed */
11392
11393 return 1; /* return error */
11394 }
11395 }
11396 else
11397 {
11398 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11399
11400 return 4; /* return error */
11401 }
11402
11403 return 0; /* success return 0 */
11404}
11405
11420{
11421 uint8_t res;
11422 uint8_t prev;
11423
11424 if (handle == NULL) /* check handle */
11425 {
11426 return 2; /* return error */
11427 }
11428 if (handle->inited != 1) /* check handle initialization */
11429 {
11430 return 3; /* return error */
11431 }
11432
11433 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11434 {
11435 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11436 if (res != 0) /* check result */
11437 {
11438 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11439
11440 return 1; /* return error */
11441 }
11442 prev &= ~(1 << 7); /* clear the settings */
11443 prev |= enable << 7; /* set the bool */
11444 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write i2c slv0 ctrl */
11445 if (res != 0) /* check result */
11446 {
11447 handle->debug_print("mpu9250: write i2c slv0 ctrl failed.\n"); /* write i2c slv0 ctrl failed */
11448
11449 return 1; /* return error */
11450 }
11451 }
11452 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11453 {
11454 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11455 if (res != 0) /* check result */
11456 {
11457 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11458
11459 return 1; /* return error */
11460 }
11461 prev &= ~(1 << 7); /* clear the settings */
11462 prev |= enable << 7; /* set the bool */
11463 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* write i2c slv1 ctrl */
11464 if (res != 0) /* check result */
11465 {
11466 handle->debug_print("mpu9250: write i2c slv1 ctrl failed.\n"); /* write i2c slv1 ctrl failed */
11467
11468 return 1; /* return error */
11469 }
11470 }
11471 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11472 {
11473 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11474 if (res != 0) /* check result */
11475 {
11476 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11477
11478 return 1; /* return error */
11479 }
11480 prev &= ~(1 << 7); /* clear the settings */
11481 prev |= enable << 7; /* set the bool */
11482 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* write i2c slv2 ctrl */
11483 if (res != 0) /* check result */
11484 {
11485 handle->debug_print("mpu9250: write i2c slv2 ctrl failed.\n"); /* write i2c slv2 ctrl failed */
11486
11487 return 1; /* return error */
11488 }
11489 }
11490 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11491 {
11492 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11493 if (res != 0) /* check result */
11494 {
11495 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11496
11497 return 1; /* return error */
11498 }
11499 prev &= ~(1 << 7); /* clear the settings */
11500 prev |= enable << 7; /* set the bool */
11501 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* write i2c slv3 ctrl */
11502 if (res != 0) /* check result */
11503 {
11504 handle->debug_print("mpu9250: write i2c slv3 ctrl failed.\n"); /* write i2c slv3 ctrl failed */
11505
11506 return 1; /* return error */
11507 }
11508 }
11509 else
11510 {
11511 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11512
11513 return 4; /* return error */
11514 }
11515
11516 return 0; /* success return 0 */
11517}
11518
11533{
11534 uint8_t res;
11535 uint8_t prev;
11536
11537 if (handle == NULL) /* check handle */
11538 {
11539 return 2; /* return error */
11540 }
11541 if (handle->inited != 1) /* check handle initialization */
11542 {
11543 return 3; /* return error */
11544 }
11545
11546 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11547 {
11548 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11549 if (res != 0) /* check result */
11550 {
11551 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11552
11553 return 1; /* return error */
11554 }
11555 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
11556 }
11557 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11558 {
11559 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11560 if (res != 0) /* check result */
11561 {
11562 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11563
11564 return 1; /* return error */
11565 }
11566 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
11567 }
11568 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11569 {
11570 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11571 if (res != 0) /* check result */
11572 {
11573 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11574
11575 return 1; /* return error */
11576 }
11577 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
11578 }
11579 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11580 {
11581 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11582 if (res != 0) /* check result */
11583 {
11584 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11585
11586 return 1; /* return error */
11587 }
11588 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
11589 }
11590 else
11591 {
11592 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11593
11594 return 4; /* return error */
11595 }
11596
11597 return 0; /* success return 0 */
11598}
11599
11614{
11615 uint8_t res;
11616 uint8_t prev;
11617
11618 if (handle == NULL) /* check handle */
11619 {
11620 return 2; /* return error */
11621 }
11622 if (handle->inited != 1) /* check handle initialization */
11623 {
11624 return 3; /* return error */
11625 }
11626
11627 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11628 {
11629 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11630 if (res != 0) /* check result */
11631 {
11632 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11633
11634 return 1; /* return error */
11635 }
11636 prev &= ~(1 << 6); /* clear the settings */
11637 prev |= enable << 6; /* set the bool */
11638 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write i2c slv0 ctrl */
11639 if (res != 0) /* check result */
11640 {
11641 handle->debug_print("mpu9250: write i2c slv0 ctrl failed.\n"); /* write i2c slv0 ctrl failed */
11642
11643 return 1; /* return error */
11644 }
11645 }
11646 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11647 {
11648 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11649 if (res != 0) /* check result */
11650 {
11651 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11652
11653 return 1; /* return error */
11654 }
11655 prev &= ~(1 << 6); /* clear the settings */
11656 prev |= enable << 6; /* set the bool */
11657 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* write i2c slv1 ctrl */
11658 if (res != 0) /* check result */
11659 {
11660 handle->debug_print("mpu9250: write i2c slv1 ctrl failed.\n"); /* write i2c slv1 ctrl failed */
11661
11662 return 1; /* return error */
11663 }
11664 }
11665 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11666 {
11667 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11668 if (res != 0) /* check result */
11669 {
11670 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11671
11672 return 1; /* return error */
11673 }
11674 prev &= ~(1 << 6); /* clear the settings */
11675 prev |= enable << 6; /* set the bool */
11676 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* write i2c slv2 ctrl */
11677 if (res != 0) /* check result */
11678 {
11679 handle->debug_print("mpu9250: write i2c slv2 ctrl failed.\n"); /* write i2c slv2 ctrl failed */
11680
11681 return 1; /* return error */
11682 }
11683 }
11684 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11685 {
11686 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11687 if (res != 0) /* check result */
11688 {
11689 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11690
11691 return 1; /* return error */
11692 }
11693 prev &= ~(1 << 6); /* clear the settings */
11694 prev |= enable << 6; /* set the bool */
11695 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* write i2c slv3 ctrl */
11696 if (res != 0) /* check result */
11697 {
11698 handle->debug_print("mpu9250: write i2c slv3 ctrl failed.\n"); /* write i2c slv3 ctrl failed */
11699
11700 return 1; /* return error */
11701 }
11702 }
11703 else
11704 {
11705 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11706
11707 return 4; /* return error */
11708 }
11709
11710 return 0; /* success return 0 */
11711}
11712
11727{
11728 uint8_t res;
11729 uint8_t prev;
11730
11731 if (handle == NULL) /* check handle */
11732 {
11733 return 2; /* return error */
11734 }
11735 if (handle->inited != 1) /* check handle initialization */
11736 {
11737 return 3; /* return error */
11738 }
11739
11740 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11741 {
11742 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11743 if (res != 0) /* check result */
11744 {
11745 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11746
11747 return 1; /* return error */
11748 }
11749 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
11750 }
11751 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11752 {
11753 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11754 if (res != 0) /* check result */
11755 {
11756 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11757
11758 return 1; /* return error */
11759 }
11760 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
11761 }
11762 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11763 {
11764 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11765 if (res != 0) /* check result */
11766 {
11767 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11768
11769 return 1; /* return error */
11770 }
11771 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
11772 }
11773 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11774 {
11775 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11776 if (res != 0) /* check result */
11777 {
11778 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11779
11780 return 1; /* return error */
11781 }
11782 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
11783 }
11784 else
11785 {
11786 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11787
11788 return 4; /* return error */
11789 }
11790
11791 return 0; /* success return 0 */
11792}
11793
11808{
11809 uint8_t res;
11810 uint8_t prev;
11811
11812 if (handle == NULL) /* check handle */
11813 {
11814 return 2; /* return error */
11815 }
11816 if (handle->inited != 1) /* check handle initialization */
11817 {
11818 return 3; /* return error */
11819 }
11820
11821 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11822 {
11823 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11824 if (res != 0) /* check result */
11825 {
11826 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11827
11828 return 1; /* return error */
11829 }
11830 prev &= ~(1 << 5); /* clear the settings */
11831 prev |= mode << 5; /* set the bool */
11832 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write i2c slv0 ctrl */
11833 if (res != 0) /* check result */
11834 {
11835 handle->debug_print("mpu9250: write i2c slv0 ctrl failed.\n"); /* write i2c slv0 ctrl failed */
11836
11837 return 1; /* return error */
11838 }
11839 }
11840 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11841 {
11842 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11843 if (res != 0) /* check result */
11844 {
11845 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11846
11847 return 1; /* return error */
11848 }
11849 prev &= ~(1 << 5); /* clear the settings */
11850 prev |= mode << 5; /* set the bool */
11851 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* write i2c slv1 ctrl */
11852 if (res != 0) /* check result */
11853 {
11854 handle->debug_print("mpu9250: write i2c slv1 ctrl failed.\n"); /* write i2c slv1 ctrl failed */
11855
11856 return 1; /* return error */
11857 }
11858 }
11859 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11860 {
11861 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11862 if (res != 0) /* check result */
11863 {
11864 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11865
11866 return 1; /* return error */
11867 }
11868 prev &= ~(1 << 5); /* clear the settings */
11869 prev |= mode << 5; /* set the bool */
11870 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* write i2c slv2 ctrl */
11871 if (res != 0) /* check result */
11872 {
11873 handle->debug_print("mpu9250: write i2c slv2 ctrl failed.\n"); /* write i2c slv2 ctrl failed */
11874
11875 return 1; /* return error */
11876 }
11877 }
11878 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11879 {
11880 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11881 if (res != 0) /* check result */
11882 {
11883 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11884
11885 return 1; /* return error */
11886 }
11887 prev &= ~(1 << 5); /* clear the settings */
11888 prev |= mode << 5; /* set the bool */
11889 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* write i2c slv3 ctrl */
11890 if (res != 0) /* check result */
11891 {
11892 handle->debug_print("mpu9250: write i2c slv3 ctrl failed.\n"); /* write i2c slv3 ctrl failed */
11893
11894 return 1; /* return error */
11895 }
11896 }
11897 else
11898 {
11899 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11900
11901 return 4; /* return error */
11902 }
11903
11904 return 0; /* success return 0 */
11905}
11906
11921{
11922 uint8_t res;
11923 uint8_t prev;
11924
11925 if (handle == NULL) /* check handle */
11926 {
11927 return 2; /* return error */
11928 }
11929 if (handle->inited != 1) /* check handle initialization */
11930 {
11931 return 3; /* return error */
11932 }
11933
11934 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
11935 {
11936 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
11937 if (res != 0) /* check result */
11938 {
11939 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
11940
11941 return 1; /* return error */
11942 }
11943 *mode = (mpu9250_iic_transaction_mode_t)((prev >> 5) & 0x1); /* get the bool */
11944 }
11945 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
11946 {
11947 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
11948 if (res != 0) /* check result */
11949 {
11950 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
11951
11952 return 1; /* return error */
11953 }
11954 *mode = (mpu9250_iic_transaction_mode_t)((prev >> 5) & 0x1); /* get the bool */
11955 }
11956 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
11957 {
11958 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
11959 if (res != 0) /* check result */
11960 {
11961 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
11962
11963 return 1; /* return error */
11964 }
11965 *mode = (mpu9250_iic_transaction_mode_t)((prev >> 5) & 0x1); /* get the bool */
11966 }
11967 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
11968 {
11969 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
11970 if (res != 0) /* check result */
11971 {
11972 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
11973
11974 return 1; /* return error */
11975 }
11976 *mode = (mpu9250_iic_transaction_mode_t)((prev >> 5) & 0x1); /* get the bool */
11977 }
11978 else
11979 {
11980 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
11981
11982 return 4; /* return error */
11983 }
11984
11985 return 0; /* success return 0 */
11986}
11987
12002{
12003 uint8_t res;
12004 uint8_t prev;
12005
12006 if (handle == NULL) /* check handle */
12007 {
12008 return 2; /* return error */
12009 }
12010 if (handle->inited != 1) /* check handle initialization */
12011 {
12012 return 3; /* return error */
12013 }
12014
12015 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
12016 {
12017 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
12018 if (res != 0) /* check result */
12019 {
12020 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
12021
12022 return 1; /* return error */
12023 }
12024 prev &= ~(1 << 4); /* clear the settings */
12025 prev |= order << 4; /* set the order */
12026 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write i2c slv0 ctrl */
12027 if (res != 0) /* check result */
12028 {
12029 handle->debug_print("mpu9250: write i2c slv0 ctrl failed.\n"); /* write i2c slv0 ctrl failed */
12030
12031 return 1; /* return error */
12032 }
12033 }
12034 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
12035 {
12036 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
12037 if (res != 0) /* check result */
12038 {
12039 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
12040
12041 return 1; /* return error */
12042 }
12043 prev &= ~(1 << 4); /* clear the settings */
12044 prev |= order << 4; /* set the order */
12045 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* write i2c slv1 ctrl */
12046 if (res != 0) /* check result */
12047 {
12048 handle->debug_print("mpu9250: write i2c slv1 ctrl failed.\n"); /* write i2c slv1 ctrl failed */
12049
12050 return 1; /* return error */
12051 }
12052 }
12053 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
12054 {
12055 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
12056 if (res != 0) /* check result */
12057 {
12058 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
12059
12060 return 1; /* return error */
12061 }
12062 prev &= ~(1 << 4); /* clear the settings */
12063 prev |= order << 4; /* set the order */
12064 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* write i2c slv2 ctrl */
12065 if (res != 0) /* check result */
12066 {
12067 handle->debug_print("mpu9250: write i2c slv2 ctrl failed.\n"); /* write i2c slv2 ctrl failed */
12068
12069 return 1; /* return error */
12070 }
12071 }
12072 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
12073 {
12074 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
12075 if (res != 0) /* check result */
12076 {
12077 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
12078
12079 return 1; /* return error */
12080 }
12081 prev &= ~(1 << 4); /* clear the settings */
12082 prev |= order << 4; /* set the order */
12083 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* write i2c slv3 ctrl */
12084 if (res != 0) /* check result */
12085 {
12086 handle->debug_print("mpu9250: write i2c slv3 ctrl failed.\n"); /* write i2c slv3 ctrl failed */
12087
12088 return 1; /* return error */
12089 }
12090 }
12091 else
12092 {
12093 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
12094
12095 return 4; /* return error */
12096 }
12097
12098 return 0; /* success return 0 */
12099}
12100
12115{
12116 uint8_t res;
12117 uint8_t prev;
12118
12119 if (handle == NULL) /* check handle */
12120 {
12121 return 2; /* return error */
12122 }
12123 if (handle->inited != 1) /* check handle initialization */
12124 {
12125 return 3; /* return error */
12126 }
12127
12128 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
12129 {
12130 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
12131 if (res != 0) /* check result */
12132 {
12133 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
12134
12135 return 1; /* return error */
12136 }
12137 *order = (mpu9250_iic_group_order_t)((prev >> 4) & 0x1); /* get the order */
12138 }
12139 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
12140 {
12141 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
12142 if (res != 0) /* check result */
12143 {
12144 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
12145
12146 return 1; /* return error */
12147 }
12148 *order = (mpu9250_iic_group_order_t)((prev >> 4) & 0x1); /* get the order */
12149 }
12150 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
12151 {
12152 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
12153 if (res != 0) /* check result */
12154 {
12155 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
12156
12157 return 1; /* return error */
12158 }
12159 *order = (mpu9250_iic_group_order_t)((prev >> 4) & 0x1); /* get the order */
12160 }
12161 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
12162 {
12163 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
12164 if (res != 0) /* check result */
12165 {
12166 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
12167
12168 return 1; /* return error */
12169 }
12170 *order = (mpu9250_iic_group_order_t)((prev >> 4) & 0x1); /* get the order */
12171 }
12172 else
12173 {
12174 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
12175
12176 return 4; /* return error */
12177 }
12178
12179 return 0; /* success return 0 */
12180}
12181
12197{
12198 uint8_t res;
12199 uint8_t prev;
12200
12201 if (handle == NULL) /* check handle */
12202 {
12203 return 2; /* return error */
12204 }
12205 if (handle->inited != 1) /* check handle initialization */
12206 {
12207 return 3; /* return error */
12208 }
12209 if (len > 0xF) /* check handle initialization */
12210 {
12211 handle->debug_print("mpu9250: len > 0xF.\n"); /* len > 0xF */
12212
12213 return 5; /* return error */
12214 }
12215
12216 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
12217 {
12218 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
12219 if (res != 0) /* check result */
12220 {
12221 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
12222
12223 return 1; /* return error */
12224 }
12225 prev &= ~0xF; /* clear the settings */
12226 prev |= len; /* set the len */
12227 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* write i2c slv0 ctrl */
12228 if (res != 0) /* check result */
12229 {
12230 handle->debug_print("mpu9250: write i2c slv0 ctrl failed.\n"); /* write i2c slv0 ctrl failed */
12231
12232 return 1; /* return error */
12233 }
12234 }
12235 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
12236 {
12237 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
12238 if (res != 0) /* check result */
12239 {
12240 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
12241
12242 return 1; /* return error */
12243 }
12244 prev &= ~0xF; /* clear the settings */
12245 prev |= len; /* set the len */
12246 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* write i2c slv1 ctrl */
12247 if (res != 0) /* check result */
12248 {
12249 handle->debug_print("mpu9250: write i2c slv1 ctrl failed.\n"); /* write i2c slv1 ctrl failed */
12250
12251 return 1; /* return error */
12252 }
12253 }
12254 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
12255 {
12256 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
12257 if (res != 0) /* check result */
12258 {
12259 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
12260
12261 return 1; /* return error */
12262 }
12263 prev &= ~0xF; /* clear the settings */
12264 prev |= len; /* set the len */
12265 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* write i2c slv2 ctrl */
12266 if (res != 0) /* check result */
12267 {
12268 handle->debug_print("mpu9250: write i2c slv2 ctrl failed.\n"); /* write i2c slv2 ctrl failed */
12269
12270 return 1; /* return error */
12271 }
12272 }
12273 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
12274 {
12275 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
12276 if (res != 0) /* check result */
12277 {
12278 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
12279
12280 return 1; /* return error */
12281 }
12282 prev &= ~0xF; /* clear the settings */
12283 prev |= len; /* set the len */
12284 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* write i2c slv3 ctrl */
12285 if (res != 0) /* check result */
12286 {
12287 handle->debug_print("mpu9250: write i2c slv3 ctrl failed.\n"); /* write i2c slv3 ctrl failed */
12288
12289 return 1; /* return error */
12290 }
12291 }
12292 else
12293 {
12294 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
12295
12296 return 4; /* return error */
12297 }
12298
12299 return 0; /* success return 0 */
12300}
12301
12316{
12317 uint8_t res;
12318 uint8_t prev;
12319
12320 if (handle == NULL) /* check handle */
12321 {
12322 return 2; /* return error */
12323 }
12324 if (handle->inited != 1) /* check handle initialization */
12325 {
12326 return 3; /* return error */
12327 }
12328
12329 if (slave == MPU9250_IIC_SLAVE_0) /* slave0 */
12330 {
12331 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV0_CTRL, (uint8_t *)&prev, 1); /* read i2c slv0 ctrl */
12332 if (res != 0) /* check result */
12333 {
12334 handle->debug_print("mpu9250: read i2c slv0 ctrl failed.\n"); /* read i2c slv0 ctrl failed */
12335
12336 return 1; /* return error */
12337 }
12338 *len = prev & 0x0F; /* get the len */
12339 }
12340 else if (slave == MPU9250_IIC_SLAVE_1) /* slave1 */
12341 {
12342 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV1_CTRL, (uint8_t *)&prev, 1); /* read i2c slv1 ctrl */
12343 if (res != 0) /* check result */
12344 {
12345 handle->debug_print("mpu9250: read i2c slv1 ctrl failed.\n"); /* read i2c slv1 ctrl failed */
12346
12347 return 1; /* return error */
12348 }
12349 *len = prev & 0x0F; /* get the len */
12350 }
12351 else if (slave == MPU9250_IIC_SLAVE_2) /* slave2 */
12352 {
12353 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV2_CTRL, (uint8_t *)&prev, 1); /* read i2c slv2 ctrl */
12354 if (res != 0) /* check result */
12355 {
12356 handle->debug_print("mpu9250: read i2c slv2 ctrl failed.\n"); /* read i2c slv2 ctrl failed */
12357
12358 return 1; /* return error */
12359 }
12360 *len = prev & 0x0F; /* get the len */
12361 }
12362 else if (slave == MPU9250_IIC_SLAVE_3) /* slave2 */
12363 {
12364 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV3_CTRL, (uint8_t *)&prev, 1); /* read i2c slv3 ctrl */
12365 if (res != 0) /* check result */
12366 {
12367 handle->debug_print("mpu9250: read i2c slv3 ctrl failed.\n"); /* read i2c slv3 ctrl failed */
12368
12369 return 1; /* return error */
12370 }
12371 *len = prev & 0x0F; /* get the len */
12372 }
12373 else
12374 {
12375 handle->debug_print("mpu9250: invalid slave.\n"); /* invalid slave */
12376
12377 return 4; /* return error */
12378 }
12379
12380 return 0; /* success return 0 */
12381}
12382
12394uint8_t mpu9250_get_iic_status(mpu9250_handle_t *handle, uint8_t *status)
12395{
12396 uint8_t res;
12397
12398 if (handle == NULL) /* check handle */
12399 {
12400 return 2; /* return error */
12401 }
12402 if (handle->inited != 1) /* check handle initialization */
12403 {
12404 return 3; /* return error */
12405 }
12406
12407 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_STATUS, (uint8_t *)status, 1); /* read i2c mst status */
12408 if (res != 0) /* check result */
12409 {
12410 handle->debug_print("mpu9250: read i2c mst status failed.\n"); /* read i2c mst status failed */
12411
12412 return 1; /* return error */
12413 }
12414
12415 return 0; /* success return 0 */
12416}
12417
12431{
12432 uint8_t res;
12433 uint8_t prev;
12434
12435 if (handle == NULL) /* check handle */
12436 {
12437 return 2; /* return error */
12438 }
12439 if (handle->inited != 1) /* check handle initialization */
12440 {
12441 return 3; /* return error */
12442 }
12443
12444 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_DELAY_CTRL, (uint8_t *)&prev, 1); /* read i2c mst delay ctrl */
12445 if (res != 0) /* check result */
12446 {
12447 handle->debug_print("mpu9250: read i2c mst delay ctrl failed.\n"); /* read i2c mst delay ctrl failed */
12448
12449 return 1; /* return error */
12450 }
12451 prev &= ~(1 << delay); /* clear the settings */
12452 prev |= enable << delay; /* set the bool */
12453 res = a_mpu9250_write(handle, MPU9250_REG_I2C_MST_DELAY_CTRL, (uint8_t *)&prev, 1); /* write i2c mst delay ctrl */
12454 if (res != 0) /* check result */
12455 {
12456 handle->debug_print("mpu9250: write i2c mst delay ctrl failed.\n"); /* write i2c mst delay ctrl failed */
12457
12458 return 1; /* return error */
12459 }
12460
12461 return 0; /* success return 0 */
12462}
12463
12477{
12478 uint8_t res;
12479 uint8_t prev;
12480
12481 if (handle == NULL) /* check handle */
12482 {
12483 return 2; /* return error */
12484 }
12485 if (handle->inited != 1) /* check handle initialization */
12486 {
12487 return 3; /* return error */
12488 }
12489
12490 res = a_mpu9250_read(handle, MPU9250_REG_I2C_MST_DELAY_CTRL, (uint8_t *)&prev, 1); /* read i2c mst delay ctrl */
12491 if (res != 0) /* check result */
12492 {
12493 handle->debug_print("mpu9250: read i2c mst delay ctrl failed.\n"); /* read i2c mst delay ctrl failed */
12494
12495 return 1; /* return error */
12496 }
12497 *enable = (mpu9250_bool_t)((prev >> delay) & 0x1); /* get the bool */
12498
12499 return 0; /* success return 0 */
12500}
12501
12514{
12515 uint8_t res;
12516 uint8_t prev;
12517
12518 if (handle == NULL) /* check handle */
12519 {
12520 return 2; /* return error */
12521 }
12522 if (handle->inited != 1) /* check handle initialization */
12523 {
12524 return 3; /* return error */
12525 }
12526
12527 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12528 if (res != 0) /* check result */
12529 {
12530 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12531
12532 return 1; /* return error */
12533 }
12534 prev &= ~(1 << 7); /* clear the settings */
12535 prev |= enable << 7; /* set the bool */
12536 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* write i2c slv4 ctrl */
12537 if (res != 0) /* check result */
12538 {
12539 handle->debug_print("mpu9250: write i2c slv4 ctrl failed.\n"); /* write i2c slv4 ctrl failed */
12540
12541 return 1; /* return error */
12542 }
12543
12544 return 0; /* success return 0 */
12545}
12546
12559{
12560 uint8_t res;
12561 uint8_t prev;
12562
12563 if (handle == NULL) /* check handle */
12564 {
12565 return 2; /* return error */
12566 }
12567 if (handle->inited != 1) /* check handle initialization */
12568 {
12569 return 3; /* return error */
12570 }
12571
12572 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12573 if (res != 0) /* check result */
12574 {
12575 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12576
12577 return 1; /* return error */
12578 }
12579 *enable = (mpu9250_bool_t)((prev >> 7) & 0x1); /* get the bool */
12580
12581 return 0; /* success return 0 */
12582}
12583
12596{
12597 uint8_t res;
12598 uint8_t prev;
12599
12600 if (handle == NULL) /* check handle */
12601 {
12602 return 2; /* return error */
12603 }
12604 if (handle->inited != 1) /* check handle initialization */
12605 {
12606 return 3; /* return error */
12607 }
12608
12609 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12610 if (res != 0) /* check result */
12611 {
12612 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12613
12614 return 1; /* return error */
12615 }
12616 prev &= ~(1 << 6); /* clear the settings */
12617 prev |= enable << 6; /* set the bool */
12618 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* write i2c slv4 ctrl */
12619 if (res != 0) /* check result */
12620 {
12621 handle->debug_print("mpu9250: write i2c slv4 ctrl failed.\n"); /* write i2c slv4 ctrl failed */
12622
12623 return 1; /* return error */
12624 }
12625
12626 return 0; /* success return 0 */
12627}
12628
12641{
12642 uint8_t res;
12643 uint8_t prev;
12644
12645 if (handle == NULL) /* check handle */
12646 {
12647 return 2; /* return error */
12648 }
12649 if (handle->inited != 1) /* check handle initialization */
12650 {
12651 return 3; /* return error */
12652 }
12653
12654 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12655 if (res != 0) /* check result */
12656 {
12657 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12658
12659 return 1; /* return error */
12660 }
12661 *enable = (mpu9250_bool_t)((prev >> 6) & 0x1); /* get the bool */
12662
12663 return 0; /* success return 0 */
12664}
12665
12678{
12679 uint8_t res;
12680 uint8_t prev;
12681
12682 if (handle == NULL) /* check handle */
12683 {
12684 return 2; /* return error */
12685 }
12686 if (handle->inited != 1) /* check handle initialization */
12687 {
12688 return 3; /* return error */
12689 }
12690
12691 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12692 if (res != 0) /* check result */
12693 {
12694 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12695
12696 return 1; /* return error */
12697 }
12698 prev &= ~(1 << 5); /* clear the settings */
12699 prev |= mode << 5; /* set the mode */
12700 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* write i2c slv4 ctrl */
12701 if (res != 0) /* check result */
12702 {
12703 handle->debug_print("mpu9250: write i2c slv4 ctrl failed.\n"); /* write i2c slv4 ctrl failed */
12704
12705 return 1; /* return error */
12706 }
12707
12708 return 0; /* success return 0 */
12709}
12710
12723{
12724 uint8_t res;
12725 uint8_t prev;
12726
12727 if (handle == NULL) /* check handle */
12728 {
12729 return 2; /* return error */
12730 }
12731 if (handle->inited != 1) /* check handle initialization */
12732 {
12733 return 3; /* return error */
12734 }
12735
12736 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12737 if (res != 0) /* check result */
12738 {
12739 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12740
12741 return 1; /* return error */
12742 }
12743 *mode = (mpu9250_iic4_transaction_mode_t)((prev >> 5) & 0x1); /* get the mode */
12744
12745 return 0; /* success return 0 */
12746}
12747
12760uint8_t mpu9250_set_iic_delay(mpu9250_handle_t *handle, uint8_t delay)
12761{
12762 uint8_t res;
12763 uint8_t prev;
12764
12765 if (handle == NULL) /* check handle */
12766 {
12767 return 2; /* return error */
12768 }
12769 if (handle->inited != 1) /* check handle initialization */
12770 {
12771 return 3; /* return error */
12772 }
12773 if (delay > 0x1F) /* check the delay */
12774 {
12775 handle->debug_print("mpu9250: delay > 0x1F.\n"); /* delay > 0x1F */
12776
12777 return 4; /* return error */
12778 }
12779
12780 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12781 if (res != 0) /* check result */
12782 {
12783 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12784
12785 return 1; /* return error */
12786 }
12787 prev &= ~0x1F; /* clear the settings */
12788 prev |= delay; /* set the delay */
12789 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* write i2c slv4 ctrl */
12790 if (res != 0) /* check result */
12791 {
12792 handle->debug_print("mpu9250: write i2c slv4 ctrl failed.\n"); /* write i2c slv4 ctrl failed */
12793
12794 return 1; /* return error */
12795 }
12796
12797 return 0; /* success return 0 */
12798}
12799
12811uint8_t mpu9250_get_iic_delay(mpu9250_handle_t *handle, uint8_t *delay)
12812{
12813 uint8_t res;
12814 uint8_t prev;
12815
12816 if (handle == NULL) /* check handle */
12817 {
12818 return 2; /* return error */
12819 }
12820 if (handle->inited != 1) /* check handle initialization */
12821 {
12822 return 3; /* return error */
12823 }
12824
12825 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_CTRL, (uint8_t *)&prev, 1); /* read i2c slv4 ctrl */
12826 if (res != 0) /* check result */
12827 {
12828 handle->debug_print("mpu9250: read i2c slv4 ctrl failed.\n"); /* read i2c slv4 ctrl failed */
12829
12830 return 1; /* return error */
12831 }
12832 *delay = prev & 0x1F; /* get the delay */
12833
12834 return 0; /* success return 0 */
12835}
12836
12848uint8_t mpu9250_set_iic4_data_out(mpu9250_handle_t *handle, uint8_t data)
12849{
12850 uint8_t res;
12851
12852 if (handle == NULL) /* check handle */
12853 {
12854 return 2; /* return error */
12855 }
12856 if (handle->inited != 1) /* check handle initialization */
12857 {
12858 return 3; /* return error */
12859 }
12860
12861 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_DO, (uint8_t *)&data, 1); /* write i2c slv4 do */
12862 if (res != 0) /* check result */
12863 {
12864 handle->debug_print("mpu9250: write i2c slv4 do failed.\n"); /* write i2c slv4 do fail */
12865
12866 return 1; /* return error */
12867 }
12868
12869 return 0; /* success return 0 */
12870}
12871
12883uint8_t mpu9250_get_iic4_data_out(mpu9250_handle_t *handle, uint8_t *data)
12884{
12885 uint8_t res;
12886
12887 if (handle == NULL) /* check handle */
12888 {
12889 return 2; /* return error */
12890 }
12891 if (handle->inited != 1) /* check handle initialization */
12892 {
12893 return 3; /* return error */
12894 }
12895
12896 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_DO, (uint8_t *)data, 1); /* read i2c slv4 do */
12897 if (res != 0) /* check result */
12898 {
12899 handle->debug_print("mpu9250: read i2c slv4 do failed.\n"); /* read i2c slv4 do failed */
12900
12901 return 1; /* return error */
12902 }
12903
12904 return 0; /* success return 0 */
12905}
12906
12918uint8_t mpu9250_set_iic4_data_in(mpu9250_handle_t *handle, uint8_t data)
12919{
12920 uint8_t res;
12921
12922 if (handle == NULL) /* check handle */
12923 {
12924 return 2; /* return error */
12925 }
12926 if (handle->inited != 1) /* check handle initialization */
12927 {
12928 return 3; /* return error */
12929 }
12930
12931 res = a_mpu9250_write(handle, MPU9250_REG_I2C_SLV4_DI, (uint8_t *)&data, 1); /* write i2c slv4 di */
12932 if (res != 0) /* check result */
12933 {
12934 handle->debug_print("mpu9250: write i2c slv4 di failed.\n"); /* write i2c slv4 di failed */
12935
12936 return 1; /* return error */
12937 }
12938
12939 return 0; /* success return 0 */
12940}
12941
12953uint8_t mpu9250_get_iic4_data_in(mpu9250_handle_t *handle, uint8_t *data)
12954{
12955 uint8_t res;
12956
12957 if (handle == NULL) /* check handle */
12958 {
12959 return 2; /* return error */
12960 }
12961 if (handle->inited != 1) /* check handle initialization */
12962 {
12963 return 3; /* return error */
12964 }
12965
12966 res = a_mpu9250_read(handle, MPU9250_REG_I2C_SLV4_DI, (uint8_t *)data, 1); /* read i2c slv4 di */
12967 if (res != 0) /* check result */
12968 {
12969 handle->debug_print("mpu9250: read i2c slv4 di failed.\n"); /* read i2c slv4 di failed */
12970
12971 return 1; /* return error */
12972 }
12973
12974 return 0; /* success return 0 */
12975}
12976
12990uint8_t mpu9250_read_extern_sensor_data(mpu9250_handle_t *handle, uint8_t *data, uint8_t len)
12991{
12992 uint8_t res;
12993
12994 if (handle == NULL) /* check handle */
12995 {
12996 return 2; /* return error */
12997 }
12998 if (handle->inited != 1) /* check handle initialization */
12999 {
13000 return 3; /* return error */
13001 }
13002 if (len > 24) /* check handle initialization */
13003 {
13004 handle->debug_print("mpu9250: len > 24.\n"); /* len > 24 */
13005
13006 return 4; /* return error */
13007 }
13008
13009 res = a_mpu9250_read(handle, MPU9250_REG_EXT_SENS_DATA_00, (uint8_t *)data, len); /* read ext sens data 00 */
13010 if (res != 0) /* check result */
13011 {
13012 handle->debug_print("mpu9250: read ext sens data 00 failed.\n"); /* read ext sens data 00 failed */
13013
13014 return 1; /* return error */
13015 }
13016
13017 return 0; /* success return 0 */
13018}
13019
13033uint8_t mpu9250_set_reg(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
13034{
13035 if (handle == NULL) /* check handle */
13036 {
13037 return 2; /* return error */
13038 }
13039 if (handle->inited != 1) /* check handle initialization */
13040 {
13041 return 3; /* return error */
13042 }
13043
13044 return a_mpu9250_write(handle, reg, buf, len); /* write data */
13045}
13046
13060uint8_t mpu9250_get_reg(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
13061{
13062 if (handle == NULL) /* check handle */
13063 {
13064 return 2; /* return error */
13065 }
13066 if (handle->inited != 1) /* check handle initialization */
13067 {
13068 return 3; /* return error */
13069 }
13070
13071 return a_mpu9250_read(handle, reg, buf, len); /* read data */
13072}
13073
13083{
13084 if (info == NULL) /* check handle */
13085 {
13086 return 2; /* return error */
13087 }
13088
13089 memset(info, 0, sizeof(mpu9250_info_t)); /* initialize mpu9250 info structure */
13090 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
13091 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
13092 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
13093 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
13094 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
13095 info->max_current_ma = MAX_CURRENT; /* set maximum current */
13096 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
13097 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
13098 info->driver_version = DRIVER_VERSION; /* set driver version */
13099
13100 return 0; /* success return 0 */
13101}
#define MPU9250_REG_MOT_DETECT_CTRL
#define MPU9250_DMP_D_1_79
#define MPU9250_DMP_TAP_THX
#define MPU9250_REG_YA_OFFSET_H
#define AK8963_REG_WIA
#define MPU9250_DMP_CFG_FIFO_ON_EVENT
#define MPU9250_DMP_FEATURE_SEND_ANY_GYRO
#define MPU9250_REG_SELF_TEST_Y_ACCEL
#define MPU9250_REG_SELF_TEST_Y_GYRO
#define MPU9250_DMP_SHAKE_REJECT_TIMEOUT
#define MPU9250_DMP_CFG_GYRO_RAW_DATA
#define MPU9250_REG_WHO_AM_I
#define MPU9250_DMP_CFG_8
#define MPU9250_REG_SELF_TEST_Z_ACCEL
#define MPU9250_REG_SELF_TEST_X_ACCEL
#define MPU9250_DMP_CFG_15
#define MPU9250_REG_I2C_SLV1_ADDR
#define MPU9250_DMP_CFG_27
#define MPU9250_REG_I2C_SLV3_REG
#define MPU9250_REG_I2C_MST_CTRL
#define MAX_CURRENT
#define MPU9250_DMP_D_0_22
#define AK8963_REG_CNTL2
#define MPU9250_DMP_TAP_THZ
#define AK8963_REG_ASTC
#define MPU9250_DMP_D_1_218
#define MPU9250_REG_I2C_SLV4_ADDR
#define MPU9250_REG_I2C_SLV1_REG
#define AK8963_REG_CNTL1
#define MPU9250_REG_I2C_MST_STATUS
#define MPU9250_DMP_TAP_TIME_MULTI
#define MIN(a, b)
inner function definition
#define MPU9250_REG_EXT_SENS_DATA_00
#define MPU9250_REG_I2C_MST_DELAY_CTRL
#define MPU9250_REG_I2C_SLV0_ADDR
#define MPU9250_DMP_GYRO_SF
#define MPU9250_DMP_SAMPLE_RATE
mpu9250 dmp code definition
#define MPU9250_REG_FIFO_COUNTH
#define MPU9250_DMP_D_1_92
#define MPU9250_DMP_D_EXT_GYRO_BIAS_Y
#define MPU9250_REG_R_W
#define MPU9250_REG_INT_ENABLE
#define MPU9250_DMP_CFG_6
#define MPU9250_REG_XG_OFFSET_H
#define MPU9250_DMP_D_1_88
#define MPU9250_DMP_D_1_36
#define MPU9250_DMP_D_1_44
#define SUPPLY_VOLTAGE_MAX
#define MPU9250_REG_FIFO_EN
#define MPU9250_REG_I2C_SLV1_CTRL
#define MPU9250_DMP_TAP_MIN_TAP_COUNT
#define MPU9250_DMP_INT_SRC_TAP
#define MPU9250_REG_INT_PIN_CFG
#define MPU9250_DMP_TAPW_MIN
#define MPU9250_DMP_CFG_ORIENT_INT
#define MPU9250_REG_USER_CTRL
#define AK8963_REG_I2CDIS
#define MPU9250_DMP_D_1_90
#define MPU9250_REG_SMPRT_DIV
#define MPU9250_REG_ZA_OFFSET_H
#define MPU9250_DMP_QUAT_MAG_SQ_MAX
#define MPU9250_DMP_SHAKE_REJECT_THRESH
#define MPU9250_DMP_D_ACCEL_BIAS
#define MPU9250_REG_PROGRAM_START
#define MPU9250_REG_I2C_SLV1_DO
#define MPU9250_REG_SELF_TEST_X_GYRO
chip register definition
#define MPU9250_REG_I2C_SLV0_DO
#define MPU9250_DMP_FCFG_7
#define MPU9250_DMP_D_0_104
#define MPU9250_DMP_FCFG_2
#define MPU9250_REG_YG_OFFSET_H
#define TEMPERATURE_MAX
#define MPU9250_REG_MEM
#define MPU9250_REG_SELF_TEST_Z_GYRO
#define MPU9250_REG_XA_OFFSET_H
#define MPU9250_REG_ACCEL_CONFIG
#define MPU9250_DMP_D_1_72
#define MPU9250_DMP_SHAKE_REJECT_TIME
#define MPU9250_DMP_TAP_THRESH
#define MPU9250_DMP_TAP_TIME
#define MANUFACTURER_NAME
#define MPU9250_REG_WOM_THR
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MPU9250_REG_GYRO_CONFIG
#define MPU9250_REG_SIGNAL_PATH_RESET
#define MPU9250_REG_I2C_SLV2_REG
#define MPU9250_REG_I2C_SLV2_DO
#define AK8963_REG_ASAX
#define MPU9250_DMP_D_PEDSTD_STEPCTR
#define MPU9250_REG_ZG_OFFSET_H
#define MPU9250_DMP_TAP_THY
#define MPU9250_DMP_D_PEDSTD_TIMECTR
#define MPU9250_DMP_D_EXT_GYRO_BIAS_X
#define MPU9250_DMP_D_1_40
#define MPU9250_REG_I2C_SLV3_CTRL
#define AK8963_REG_INFO
#define MPU9250_REG_CONFIG
#define MPU9250_REG_I2C_SLV2_ADDR
#define MPU9250_DMP_CFG_LP_QUAT
#define MPU9250_REG_I2C_SLV4_DI
#define MPU9250_DMP_QUAT_MAG_SQ_MIN
#define MPU9250_REG_ACCEL_CONFIG2
#define MPU9250_REG_I2C_SLV2_CTRL
#define AK8963_REG_ST1
#define MPU9250_REG_I2C_SLV3_ADDR
#define MPU9250_REG_I2C_SLV4_REG
#define MPU9250_REG_PWR_MGMT_2
#define CHIP_NAME
chip information definition
#define MPU9250_REG_ACCEL_XOUT_H
#define MPU9250_REG_I2C_SLV4_DO
#define MPU9250_REG_LP_ACCEL_ODR
#define MPU9250_REG_I2C_SLV3_DO
#define AK8963_IIC_ADDRESS
chip ak8963 iic address definition
#define DRIVER_VERSION
#define MPU9250_REG_TEMP_OUT_H
#define MPU9250_DMP_CFG_20
#define MPU9250_DMP_CFG_MOTION_BIAS
#define MPU9250_REG_I2C_SLV4_CTRL
#define MPU9250_REG_I2C_SLV0_CTRL
#define MPU9250_REG_PWR_MGMT_1
#define MPU9250_DMP_FCFG_1
#define MPU9250_DMP_FCFG_3
#define MPU9250_DMP_INT_SRC_ORIENT
#define MPU9250_DMP_D_EXT_GYRO_BIAS_Z
#define MPU9250_REG_INT_STATUS
#define AK8963_REG_ST2
#define MPU9250_REG_I2C_SLV0_REG
#define MPU9250_REG_BANK_SEL
driver mpu9250 header file
driver mpu9250 code header file
uint8_t mpu9250_get_addr_pin(mpu9250_handle_t *handle, mpu9250_address_t *addr_pin)
get the chip address pin
uint8_t mpu9250_set_accelerometer_z_test(mpu9250_handle_t *handle, uint8_t data)
set the accelerometer z test
uint8_t mpu9250_get_accel_compare_with_previous_sample(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the accel compare with previous sample status
uint8_t mpu9250_get_interrupt_pin_type(mpu9250_handle_t *handle, mpu9250_pin_type_t *type)
get the interrupt pin type
uint8_t mpu9250_set_accelerometer_low_pass_filter(mpu9250_handle_t *handle, mpu9250_accelerometer_low_pass_filter_t filter)
set the accelerometer low pass filter
uint8_t mpu9250_set_wake_on_motion(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable wake on motion
uint8_t mpu9250_set_accel_compare_with_previous_sample(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable accel compare with previous sample
uint8_t mpu9250_set_fifo_mode(mpu9250_handle_t *handle, mpu9250_fifo_mode mode)
set the fifo mode
uint8_t mpu9250_get_iic_read_mode(mpu9250_handle_t *handle, mpu9250_iic_read_mode_t *mode)
get the iic read mode
uint8_t mpu9250_self_test(mpu9250_handle_t *handle, int32_t gyro_offset_raw[3], int32_t accel_offset_raw[3])
run the self test
uint8_t mpu9250_set_addr_pin(mpu9250_handle_t *handle, mpu9250_address_t addr_pin)
set the chip address pin
uint8_t mpu9250_set_accelerometer_z_offset(mpu9250_handle_t *handle, int16_t offset)
set the accelerometer z offset
uint8_t mpu9250_get_interrupt_read_clear(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the interrupt reading clear status
uint8_t mpu9250_set_ptat(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the temperature sensor
uint8_t mpu9250_get_iic_master(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic master status
uint8_t mpu9250_get_iic_enable(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t *enable)
get the iic status
uint8_t mpu9250_get_iic4_enable(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic4 status
uint8_t mpu9250_get_fsync_interrupt_level(mpu9250_handle_t *handle, mpu9250_pin_level_t *level)
get the fsync interrupt level
uint8_t mpu9250_set_fifo_enable(mpu9250_handle_t *handle, mpu9250_fifo_t fifo, mpu9250_bool_t enable)
enable or disable the fifo function
uint8_t mpu9250_get_fifo_mode(mpu9250_handle_t *handle, mpu9250_fifo_mode *mode)
get the fifo mode
uint8_t mpu9250_get_accelerometer_low_pass_filter(mpu9250_handle_t *handle, mpu9250_accelerometer_low_pass_filter_t *filter)
get the accelerometer low pass filter
uint8_t mpu9250_set_standby_mode(mpu9250_handle_t *handle, mpu9250_source_t source, mpu9250_bool_t enable)
set source into standby mode
uint8_t mpu9250_set_iic_wait_for_external_sensor(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable iic wait for external sensor
uint8_t mpu9250_get_gyro_z_offset(mpu9250_handle_t *handle, int16_t *offset)
get the gyro z offset
uint8_t mpu9250_get_iic_mode(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_mode_t *mode)
get the iic mode
uint8_t mpu9250_get_iic_bypass(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic bypass status
uint8_t mpu9250_get_gyroscope_y_test(mpu9250_handle_t *handle, uint8_t *data)
get the gyroscope y test
uint8_t mpu9250_get_sample_rate_divider(mpu9250_handle_t *handle, uint8_t *d)
get the sample rate divider
uint8_t mpu9250_set_cycle_wake_up(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the cycle wake up mode
uint8_t mpu9250_set_accelerometer_y_test(mpu9250_handle_t *handle, uint8_t data)
set the accelerometer y test
uint8_t mpu9250_get_wake_on_motion(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the wake on motion status
uint8_t mpu9250_set_interrupt_latch(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the interrupt latch
uint8_t mpu9250_set_iic4_data_in(mpu9250_handle_t *handle, uint8_t data)
set the iic4 data in
uint8_t mpu9250_get_gyro_y_offset(mpu9250_handle_t *handle, int16_t *offset)
get the gyro y offset
mpu9250_accelerometer_range_t
mpu9250 accelerometer range enumeration definition
uint8_t mpu9250_get_accelerometer_z_offset(mpu9250_handle_t *handle, int16_t *offset)
get the accelerometer z offset
uint8_t mpu9250_get_iic_byte_swap(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t *enable)
get the iic byte swap status
uint8_t mpu9250_get_fifo_count(mpu9250_handle_t *handle, uint16_t *count)
get the fifo counter value
uint8_t mpu9250_get_iic_master_reset(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic master reset status
uint8_t mpu9250_set_extern_sync(mpu9250_handle_t *handle, mpu9250_extern_sync_t sync)
set the extern sync type
uint8_t mpu9250_accelerometer_offset_convert_to_data(mpu9250_handle_t *handle, int16_t reg, float *mg)
convert the register raw data to the accelerometer offset
uint8_t mpu9250_get_iic4_interrupt(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic4 interrupt status
uint8_t mpu9250_fifo_get(mpu9250_handle_t *handle, uint8_t *buf, uint16_t len)
fifo read bytes
uint8_t mpu9250_get_accelerometer_test(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t *enable)
get the accelerometer test
mpu9250_pin_type_t
mpu9250 pin type enumeration definition
uint8_t mpu9250_set_gyro_standby(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the gyro standby
mpu9250_signal_path_reset_t
mpu9250 signal path reset enumeration definition
uint8_t mpu9250_set_iic_multi_master(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable iic multi master
uint8_t mpu9250_get_sleep(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the sleep status
uint8_t mpu9250_get_accelerometer_z_test(mpu9250_handle_t *handle, uint8_t *data)
get the accelerometer z test
uint8_t mpu9250_set_signal_path_reset(mpu9250_handle_t *handle, mpu9250_signal_path_reset_t path)
set the signal path reset
uint8_t mpu9250_get_accelerometer_range(mpu9250_handle_t *handle, mpu9250_accelerometer_range_t *range)
get the accelerometer range
uint8_t mpu9250_get_ptat(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the temperature sensor status
uint8_t mpu9250_get_iic_multi_master(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic multi master status
mpu9250_iic_delay_t
mpu9250 iic delay enumeration definition
uint8_t mpu9250_get_iic_data_out(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t *data)
get the iic data out
uint8_t mpu9250_set_iic_transferred_len(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t len)
set the iic transferred length
uint8_t mpu9250_fifo_reset(mpu9250_handle_t *handle)
reset the fifo
uint8_t mpu9250_set_iic_read_mode(mpu9250_handle_t *handle, mpu9250_iic_read_mode_t mode)
set the iic read mode
uint8_t mpu9250_accelerometer_offset_convert_to_register(mpu9250_handle_t *handle, float mg, int16_t *reg)
convert the accelerometer offset to the register raw data
uint8_t mpu9250_get_sensor_reset(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the sensor reset status
mpu9250_address_t
mpu9250 address enumeration definition
mpu9250_gyroscope_range_t
mpu9250 gyroscope range enumeration definition
uint8_t mpu9250_set_iic4_data_out(mpu9250_handle_t *handle, uint8_t data)
set the iic4 data out
uint8_t mpu9250_set_iic_register(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t reg)
set the iic register
uint8_t mpu9250_get_accelerometer_choice(mpu9250_handle_t *handle, uint8_t *choice)
get the accelerometer choice
uint8_t mpu9250_set_iic_data_out(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t data)
set the iic data out
uint8_t mpu9250_set_fsync_interrupt(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the fsync interrupt
uint8_t mpu9250_get_iic4_transaction_mode(mpu9250_handle_t *handle, mpu9250_iic4_transaction_mode_t *mode)
get the iic4 transaction mode
uint8_t mpu9250_get_interrupt(mpu9250_handle_t *handle, mpu9250_interrupt_t type, mpu9250_bool_t *enable)
get the interrupt status
uint8_t mpu9250_set_accelerometer_y_offset(mpu9250_handle_t *handle, int16_t offset)
set the accelerometer y offset
uint8_t mpu9250_motion_threshold_convert_to_data(mpu9250_handle_t *handle, uint8_t reg, float *mg)
convert the register raw data to the motion threshold
uint8_t mpu9250_set_iic_bypass(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic bypass
uint8_t mpu9250_get_standby_mode(mpu9250_handle_t *handle, mpu9250_source_t source, mpu9250_bool_t *enable)
get the source mode
uint8_t mpu9250_get_clock_source(mpu9250_handle_t *handle, mpu9250_clock_source_t *clock_source)
get the chip clock source
uint8_t mpu9250_info(mpu9250_info_t *info)
get the chip's information
uint8_t mpu9250_set_accelerometer_test(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t enable)
set the accelerometer test
uint8_t mpu9250_set_interrupt_level(mpu9250_handle_t *handle, mpu9250_pin_level_t level)
set the interrupt level
uint8_t mpu9250_set_gyroscope_x_test(mpu9250_handle_t *handle, uint8_t data)
set the gyroscope x test
uint8_t mpu9250_get_iic4_data_out(mpu9250_handle_t *handle, uint8_t *data)
get the iic4 data out
uint8_t mpu9250_set_interrupt_read_clear(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the interrupt reading clear
uint8_t mpu9250_get_extern_sync(mpu9250_handle_t *handle, mpu9250_extern_sync_t *sync)
get the extern sync type
uint8_t mpu9250_set_interface(mpu9250_handle_t *handle, mpu9250_interface_t interface)
set the chip interface
uint8_t mpu9250_get_interface(mpu9250_handle_t *handle, mpu9250_interface_t *interface)
get the chip interface
mpu9250_iic_transaction_mode_t
mpu9250 iic transaction mode enumeration definition
mpu9250_iic_slave_t
mpu9250 iic slave enumeration definition
uint8_t mpu9250_irq_handler(mpu9250_handle_t *handle)
irq handler
uint8_t mpu9250_set_gyroscope_y_test(mpu9250_handle_t *handle, uint8_t data)
set the gyroscope y test
uint8_t mpu9250_get_gyro_standby(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the gyro standby status
uint8_t mpu9250_set_iic_mode(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_mode_t mode)
set the iic mode
mpu9250_interrupt_t
mpu9250 interrupt enumeration definition
uint8_t mpu9250_get_gyroscope_x_test(mpu9250_handle_t *handle, uint8_t *data)
get the gyroscope x test
uint8_t mpu9250_get_fsync_interrupt(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the fsync interrupt status
mpu9250_extern_sync_t
mpu9250 extern sync enumeration definition
uint8_t mpu9250_gyro_offset_convert_to_data(mpu9250_handle_t *handle, int16_t reg, float *dps)
convert the register raw data to the gyro offset
uint8_t mpu9250_sensor_reset(mpu9250_handle_t *handle)
reset all sensors
mpu9250_source_t
mpu9250 source enumeration definition
uint8_t mpu9250_set_iic_delay_enable(mpu9250_handle_t *handle, mpu9250_iic_delay_t delay, mpu9250_bool_t enable)
enable or disable the iic delay
struct mpu9250_info_s mpu9250_info_t
mpu9250 information structure definition
uint8_t mpu9250_get_iic_transaction_mode(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_transaction_mode_t *mode)
get the iic transaction mode
mpu9250_interface_t
mpu9250 interface enumeration definition
mpu9250_bool_t
mpu9250 bool enumeration definition
uint8_t mpu9250_set_iic_transaction_mode(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_transaction_mode_t mode)
set the iic transaction mode
uint8_t mpu9250_get_accelerometer_y_test(mpu9250_handle_t *handle, uint8_t *data)
get the accelerometer y test
uint8_t mpu9250_set_gyro_y_offset(mpu9250_handle_t *handle, int16_t offset)
set the gyro y offset
uint8_t mpu9250_get_gyroscope_z_test(mpu9250_handle_t *handle, uint8_t *data)
get the gyroscope z test
uint8_t mpu9250_set_interrupt(mpu9250_handle_t *handle, mpu9250_interrupt_t type, mpu9250_bool_t enable)
enable or disable the interrupt
uint8_t mpu9250_get_accelerometer_x_test(mpu9250_handle_t *handle, uint8_t *data)
get the accelerometer x test
uint8_t mpu9250_set_motion_threshold(mpu9250_handle_t *handle, uint8_t threshold)
set the motion_threshold
uint8_t mpu9250_set_fsync_interrupt_level(mpu9250_handle_t *handle, mpu9250_pin_level_t level)
set the fsync interrupt level
uint8_t mpu9250_get_low_pass_filter(mpu9250_handle_t *handle, mpu9250_low_pass_filter_t *filter)
get the low pass filter
uint8_t mpu9250_get_interrupt_status(mpu9250_handle_t *handle, uint8_t *status)
get the interrupt status
uint8_t mpu9250_set_gyroscope_choice(mpu9250_handle_t *handle, uint8_t choice)
set the gyroscope choice
uint8_t mpu9250_set_iic_fifo_enable(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t enable)
enable or disable the iic fifo
uint8_t mpu9250_deinit(mpu9250_handle_t *handle)
close the chip
uint8_t mpu9250_read(mpu9250_handle_t *handle, int16_t(*accel_raw)[3], float(*accel_g)[3], int16_t(*gyro_raw)[3], float(*gyro_dps)[3], int16_t(*mag_raw)[3], float(*mag_ut)[3], uint16_t *len)
read the data
struct mpu9250_handle_s mpu9250_handle_t
mpu9250 handle structure definition
uint8_t mpu9250_read_temperature(mpu9250_handle_t *handle, int16_t(*raw), float *degrees)
read the temperature
uint8_t mpu9250_get_accelerometer_y_offset(mpu9250_handle_t *handle, int16_t *offset)
get the accelerometer y offset
uint8_t mpu9250_get_fifo_enable(mpu9250_handle_t *handle, mpu9250_fifo_t fifo, mpu9250_bool_t *enable)
get the fifo function status
uint8_t mpu9250_set_fifo_1024kb(mpu9250_handle_t *handle)
set fifo 1024kb
uint8_t mpu9250_get_gyroscope_range(mpu9250_handle_t *handle, mpu9250_gyroscope_range_t *range)
get the gyroscope range
uint8_t mpu9250_get_interrupt_level(mpu9250_handle_t *handle, mpu9250_pin_level_t *level)
get the interrupt level
uint8_t mpu9250_motion_threshold_convert_to_register(mpu9250_handle_t *handle, float mg, uint8_t *reg)
convert the motion threshold to the register raw data
uint8_t mpu9250_set_low_pass_filter(mpu9250_handle_t *handle, mpu9250_low_pass_filter_t filter)
set the low pass filter
uint8_t mpu9250_set_gyroscope_test(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t enable)
set the gyroscope test
uint8_t mpu9250_set_clock_source(mpu9250_handle_t *handle, mpu9250_clock_source_t clock_source)
set the chip clock source
uint8_t mpu9250_get_gyroscope_test(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t *enable)
get the gyroscope test
uint8_t mpu9250_device_reset(mpu9250_handle_t *handle)
reset the chip
uint8_t mpu9250_set_low_power_accel_output_rate(mpu9250_handle_t *handle, mpu9250_low_power_accel_output_rate_t rate)
set the low power accel output rate
uint8_t mpu9250_get_cycle_wake_up(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the cycle wake up mode status
mpu9250_low_pass_filter_t
mpu9250 low pass filter enumeration definition
uint8_t mpu9250_get_iic_group_order(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_group_order_t *order)
get the iic group order
uint8_t mpu9250_get_iic4_data_in(mpu9250_handle_t *handle, uint8_t *data)
get the iic4 data in
uint8_t mpu9250_set_iic_clock(mpu9250_handle_t *handle, mpu9250_iic_clock_t clk)
set the iic clock
uint8_t mpu9250_get_disable_iic_slave(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic slave status
uint8_t mpu9250_get_iic_delay_enable(mpu9250_handle_t *handle, mpu9250_iic_delay_t delay, mpu9250_bool_t *enable)
get the iic delay status
uint8_t mpu9250_set_disable_iic_slave(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic slave mode
uint8_t mpu9250_get_iic_transferred_len(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t *len)
get the iic transferred length
uint8_t mpu9250_get_gyro_x_offset(mpu9250_handle_t *handle, int16_t *offset)
get the gyro x offset
uint8_t mpu9250_set_iic_address(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t addr_7bit)
set the iic address
mpu9250_fifo_mode
mpu9250 fifo mode enumeration definition
mpu9250_low_power_accel_output_rate_t
mpu9250 low power accel output rate enumeration definition
uint8_t mpu9250_get_iic_status(mpu9250_handle_t *handle, uint8_t *status)
get the iic status
uint8_t mpu9250_read_extern_sensor_data(mpu9250_handle_t *handle, uint8_t *data, uint8_t len)
read the extern sensor data
uint8_t mpu9250_set_sample_rate_divider(mpu9250_handle_t *handle, uint8_t d)
set the sample rate divider
uint8_t mpu9250_fifo_set(mpu9250_handle_t *handle, uint8_t *buf, uint16_t len)
fifo write bytes
uint8_t mpu9250_get_iic_fifo_enable(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t *enable)
get the iic fifo status
uint8_t mpu9250_set_gyro_x_offset(mpu9250_handle_t *handle, int16_t offset)
set the gyro x offset
uint8_t mpu9250_set_accelerometer_x_offset(mpu9250_handle_t *handle, int16_t offset)
set the accelerometer x offset
mpu9250_iic_read_mode_t
mpu9250 iic read mode enumeration definition
uint8_t mpu9250_set_accelerometer_x_test(mpu9250_handle_t *handle, uint8_t data)
set the accelerometer x test
uint8_t mpu9250_set_fifo(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable fifo
uint8_t mpu9250_get_fifo_reset(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the fifo reset status
uint8_t mpu9250_init(mpu9250_handle_t *handle)
initialize the chip
uint8_t mpu9250_set_iic_enable(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t enable)
enable or disable the iic
uint8_t mpu9250_get_iic_delay(mpu9250_handle_t *handle, uint8_t *delay)
get the iic delay
mpu9250_iic4_transaction_mode_t
mpu9250 iic4 transaction mode enumeration definition
uint8_t mpu9250_set_iic_delay(mpu9250_handle_t *handle, uint8_t delay)
set the iic delay
uint8_t mpu9250_set_accelerometer_choice(mpu9250_handle_t *handle, uint8_t choice)
set the accelerometer choice
mpu9250_iic_clock_t
mpu9250 iic clock enumeration definition
uint8_t mpu9250_set_iic4_interrupt(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic4 interrupt
uint8_t mpu9250_set_gyroscope_range(mpu9250_handle_t *handle, mpu9250_gyroscope_range_t range)
set the gyroscope range
mpu9250_iic_group_order_t
mpu9250 iic group order enumeration definition
uint8_t mpu9250_iic_master_reset(mpu9250_handle_t *handle)
reset the iic master controller
uint8_t mpu9250_get_device_reset(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the device reset status
uint8_t mpu9250_get_accelerometer_x_offset(mpu9250_handle_t *handle, int16_t *offset)
get the accelerometer x offset
uint8_t mpu9250_set_iic_byte_swap(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_bool_t enable)
enable or disable the iic byte swap
uint8_t mpu9250_set_gyro_z_offset(mpu9250_handle_t *handle, int16_t offset)
set the gyro z offset
uint8_t mpu9250_set_interrupt_pin_type(mpu9250_handle_t *handle, mpu9250_pin_type_t type)
set the interrupt pin type
uint8_t mpu9250_get_fifo(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the fifo status
uint8_t mpu9250_set_iic4_transaction_mode(mpu9250_handle_t *handle, mpu9250_iic4_transaction_mode_t mode)
set the iic4 transaction mode
uint8_t mpu9250_get_interrupt_latch(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the interrupt latch status
uint8_t mpu9250_get_gyroscope_choice(mpu9250_handle_t *handle, uint8_t *choice)
get the gyroscope choice
uint8_t mpu9250_set_gyroscope_z_test(mpu9250_handle_t *handle, uint8_t data)
set the gyroscope z test
uint8_t mpu9250_set_iic_master(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic master mode
uint8_t mpu9250_get_iic_register(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t *reg)
get the iic register
uint8_t mpu9250_set_accelerometer_range(mpu9250_handle_t *handle, mpu9250_accelerometer_range_t range)
set the accelerometer range
uint8_t mpu9250_gyro_offset_convert_to_register(mpu9250_handle_t *handle, float dps, int16_t *reg)
convert the gyro offset to the register raw data
uint8_t mpu9250_get_iic_wait_for_external_sensor(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
get the iic wait for external sensor status
mpu9250_iic_mode_t
mpu9250 iic mode enumeration definition
mpu9250_axis_t
mpu9250 axis enumeration definition
uint8_t mpu9250_get_iic_clock(mpu9250_handle_t *handle, mpu9250_iic_clock_t *clk)
get the iic clock
mpu9250_accelerometer_low_pass_filter_t
mpu9250 accelerometer low pass filter enumeration definition
uint8_t mpu9250_set_iic_group_order(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, mpu9250_iic_group_order_t order)
set the iic group order
uint8_t mpu9250_get_motion_threshold(mpu9250_handle_t *handle, uint8_t *threshold)
get the motion_threshold
uint8_t mpu9250_set_sleep(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the sleep mode
uint8_t mpu9250_get_iic_address(mpu9250_handle_t *handle, mpu9250_iic_slave_t slave, uint8_t *addr_7bit)
get the iic address
uint8_t mpu9250_get_low_power_accel_output_rate(mpu9250_handle_t *handle, mpu9250_low_power_accel_output_rate_t *rate)
get the low power accel output rate
uint8_t mpu9250_set_iic4_enable(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic4
mpu9250_pin_level_t
mpu9250 pin level enumeration definition
mpu9250_fifo_t
mpu9250 fifo enumeration definition
mpu9250_clock_source_t
mpu9250 clock source enumeration definition
uint8_t mpu9250_force_fifo_reset(mpu9250_handle_t *handle)
force reset the fifo
@ MPU9250_IIC_SLAVE_3
@ MPU9250_IIC_SLAVE_2
@ MPU9250_IIC_SLAVE_4
@ MPU9250_IIC_SLAVE_0
@ MPU9250_IIC_SLAVE_1
@ MPU9250_INTERRUPT_MOTION
@ MPU9250_INTERRUPT_DATA_READY
@ MPU9250_INTERRUPT_FSYNC_INT
@ MPU9250_INTERRUPT_DMP
@ MPU9250_INTERRUPT_FIFO_OVERFLOW
@ MPU9250_INTERFACE_IIC
@ MPU9250_BOOL_TRUE
@ MPU9250_BOOL_FALSE
@ MPU9250_AXIS_X
@ MPU9250_AXIS_Z
@ MPU9250_AXIS_Y
uint8_t mpu9250_dmp_set_accel_bias(mpu9250_handle_t *handle, int32_t bias[3])
dmp set the accel bias
uint8_t mpu9250_dmp_set_tap_callback(mpu9250_handle_t *handle, void(*callback)(uint8_t count, uint8_t direction))
dmp set the tap callback
uint8_t mpu9250_dmp_get_fifo_rate(mpu9250_handle_t *handle, uint16_t *rate)
dmp get the fifo rate
uint8_t mpu9250_dmp_set_gyro_bias(mpu9250_handle_t *handle, int32_t bias[3])
dmp set the gyro bias
uint8_t mpu9250_dmp_set_shake_reject_timeout(mpu9250_handle_t *handle, uint16_t ms)
dmp set the shake reject timeout
uint8_t mpu9250_dmp_get_pedometer_walk_time(mpu9250_handle_t *handle, uint32_t *ms)
dmp get the pedometer walk time
uint8_t mpu9250_dmp_get_tap_time_multi(mpu9250_handle_t *handle, uint16_t *ms)
dmp get max time between taps to register as a multi tap
uint8_t mpu9250_dmp_set_6x_quaternion(mpu9250_handle_t *handle, mpu9250_bool_t enable)
dmp enable or disable generate 6 axis quaternions from dmp
uint8_t mpu9250_dmp_set_orientation(mpu9250_handle_t *handle, int8_t mat[9])
dmp set the orientation
uint8_t mpu9250_dmp_set_fifo_rate(mpu9250_handle_t *handle, uint16_t rate)
dmp set the fifo rate
uint8_t mpu9250_dmp_load_firmware(mpu9250_handle_t *handle)
load the dmp firmware
uint8_t mpu9250_dmp_set_3x_quaternion(mpu9250_handle_t *handle, mpu9250_bool_t enable)
dmp enable or disable generate 3 axis quaternions from dmp
uint8_t mpu9250_dmp_set_tap_axes(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t enable)
dmp enable or disable the tap axes
uint8_t mpu9250_dmp_get_tap_axes(mpu9250_handle_t *handle, mpu9250_axis_t axis, mpu9250_bool_t *enable)
dmp get the tap axes status
uint8_t mpu9250_dmp_set_enable(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the dmp
uint8_t mpu9250_dmp_set_feature(mpu9250_handle_t *handle, uint16_t mask)
dmp enable or disable the dmp feature
uint8_t mpu9250_dmp_set_shake_reject_thresh(mpu9250_handle_t *handle, uint16_t dps)
dmp set the shake reject thresh
uint8_t mpu9250_dmp_set_orient_callback(mpu9250_handle_t *handle, void(*callback)(uint8_t orientation))
dmp set the orient callback
uint8_t mpu9250_dmp_set_interrupt_mode(mpu9250_handle_t *handle, mpu9250_dmp_interrupt_mode_t mode)
dmp set the interrupt mode
uint8_t mpu9250_dmp_get_shake_reject_time(mpu9250_handle_t *handle, uint16_t *ms)
dmp get the shake reject time
uint8_t mpu9250_dmp_get_pedometer_step_count(mpu9250_handle_t *handle, uint32_t *count)
dmp get the pedometer step count
uint8_t mpu9250_dmp_set_pedometer_step_count(mpu9250_handle_t *handle, uint32_t count)
dmp set the pedometer step count
uint8_t mpu9250_dmp_get_tap_thresh(mpu9250_handle_t *handle, mpu9250_axis_t axis, uint16_t *mg_ms)
dmp get the tap thresh
uint8_t mpu9250_dmp_set_shake_reject_time(mpu9250_handle_t *handle, uint16_t ms)
dmp set the shake reject time
uint8_t mpu9250_dmp_get_min_tap_count(mpu9250_handle_t *handle, uint8_t *cnt)
dmp get the min tap count
uint8_t mpu9250_dmp_get_tap_time(mpu9250_handle_t *handle, uint16_t *ms)
dmp get the tap time
mpu9250_dmp_interrupt_mode_t
mpu9250 dmp interrupt mode enumeration definition
uint8_t mpu9250_dmp_set_gyro_calibrate(mpu9250_handle_t *handle, mpu9250_bool_t enable)
dmp enable or disable gyro calibrate
uint8_t mpu9250_dmp_get_shake_reject_timeout(mpu9250_handle_t *handle, uint16_t *ms)
dmp get the shake reject timeout
uint8_t mpu9250_dmp_gyro_accel_raw_offset_convert(mpu9250_handle_t *handle, int32_t gyro_offset_raw[3], int32_t accel_offset_raw[3], int32_t gyro_offset[3], int32_t accel_offset[3])
dmp gyro accel raw offset convert
uint8_t mpu9250_dmp_set_tap_time(mpu9250_handle_t *handle, uint16_t ms)
dmp set the tap time
uint8_t mpu9250_dmp_set_tap_thresh(mpu9250_handle_t *handle, mpu9250_axis_t axis, uint16_t mg_ms)
dmp set the tap thresh
uint8_t mpu9250_dmp_set_min_tap_count(mpu9250_handle_t *handle, uint8_t cnt)
dmp set the min tap count
uint8_t mpu9250_dmp_set_pedometer_walk_time(mpu9250_handle_t *handle, uint32_t ms)
dmp set the pedometer walk time
uint8_t mpu9250_dmp_read(mpu9250_handle_t *handle, int16_t(*accel_raw)[3], float(*accel_g)[3], int16_t(*gyro_raw)[3], float(*gyro_dps)[3], int32_t(*quat)[4], float *pitch, float *roll, float *yaw, uint16_t *l)
dmp read the data
uint8_t mpu9250_dmp_set_tap_time_multi(mpu9250_handle_t *handle, uint16_t ms)
dmp set max time between taps to register as a multi tap
#define MPU9250_DMP_CODE_SIZE
mpu9250 dmp code definition
uint8_t mpu9250_dmp_get_shake_reject_thresh(mpu9250_handle_t *handle, uint16_t *dps)
dmp get the shake reject thresh
@ MPU9250_DMP_FEATURE_6X_QUAT
@ MPU9250_DMP_FEATURE_3X_QUAT
@ MPU9250_DMP_FEATURE_GYRO_CAL
@ MPU9250_DMP_FEATURE_PEDOMETER
@ MPU9250_DMP_FEATURE_SEND_CAL_GYRO
@ MPU9250_DMP_FEATURE_SEND_RAW_ACCEL
@ MPU9250_DMP_FEATURE_TAP
@ MPU9250_DMP_FEATURE_ORIENT
@ MPU9250_DMP_INTERRUPT_MODE_CONTINUOUS
uint8_t mpu9250_set_reg(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t mpu9250_get_reg(mpu9250_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t mpu9250_mag_get_status1(mpu9250_handle_t *handle, uint8_t *status)
magnetometer get the status1
mpu9250_magnetometer_bits_t
mpu9250 magnetometer bits enumeration definition
uint8_t mpu9250_mag_get_status2(mpu9250_handle_t *handle, uint8_t *status)
magnetometer get the status2
uint8_t mpu9250_mag_set_mode(mpu9250_handle_t *handle, mpu9250_magnetometer_mode_t mode)
magnetometer set the mode
uint8_t mpu9250_mag_iic_disable(mpu9250_handle_t *handle)
magnetometer disable the iic
uint8_t mpu9250_mag_get_mode(mpu9250_handle_t *handle, mpu9250_magnetometer_mode_t *mode)
magnetometer get the mode
uint8_t mpu9250_mag_get_self_test(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
magnetometer get the self test status
uint8_t mpu9250_mag_get_bits(mpu9250_handle_t *handle, mpu9250_magnetometer_bits_t *bits)
magnetometer get the bits
uint8_t mpu9250_mag_set_bits(mpu9250_handle_t *handle, mpu9250_magnetometer_bits_t bits)
magnetometer set the bits
mpu9250_magnetometer_mode_t
mpu9250 magnetometer mode enumeration definition
uint8_t mpu9250_mag_set_reset(mpu9250_handle_t *handle, mpu9250_bool_t enable)
magnetometer enable or disable reset
uint8_t mpu9250_mag_get_info(mpu9250_handle_t *handle, uint8_t *info)
magnetometer get the information
uint8_t mpu9250_mag_get_asa(mpu9250_handle_t *handle, uint8_t asa[3])
magnetometer get the asa data
uint8_t mpu9250_mag_get_reset(mpu9250_handle_t *handle, mpu9250_bool_t *enable)
magnetometer get the reset status
uint8_t mpu9250_mag_init(mpu9250_handle_t *handle)
initialize the magnetometer of mpu9250
uint8_t mpu9250_mag_deinit(mpu9250_handle_t *handle)
magnetometer deinit
uint8_t mpu9250_mag_set_fifo_mode(mpu9250_handle_t *handle)
magnetometer set into fifo mode
uint8_t mpu9250_mag_set_self_test(mpu9250_handle_t *handle, mpu9250_bool_t enable)
magnetometer enable or disable self test
uint8_t mpu9250_mag_read(mpu9250_handle_t *handle, int16_t mag_raw[3], float mag_ut[3])
mag read the data
uint8_t buf[1024]
void(* dmp_tap_callback)(uint8_t count, uint8_t direction)
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* receive_callback)(uint8_t type)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(void)
void(* dmp_orient_callback)(uint8_t orientation)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint32_t driver_version
char manufacturer_name[32]