LibDriver MPU9250
Loading...
Searching...
No Matches
driver_mpu9250_read_test.c
Go to the documentation of this file.
1
36
38
39static mpu9250_handle_t gs_handle;
40
51uint8_t mpu9250_read_test(mpu9250_interface_t interface, mpu9250_address_t addr, uint32_t times)
52{
53 uint8_t res;
54 uint32_t i;
55 mpu9250_info_t info;
56
57 /* link interface function */
70
71 /* get information */
72 res = mpu9250_info(&info);
73 if (res != 0)
74 {
75 mpu9250_interface_debug_print("mpu9250: get info failed.\n");
76
77 return 1;
78 }
79 else
80 {
81 /* print chip info */
82 mpu9250_interface_debug_print("mpu9250: chip is %s.\n", info.chip_name);
83 mpu9250_interface_debug_print("mpu9250: manufacturer is %s.\n", info.manufacturer_name);
84 mpu9250_interface_debug_print("mpu9250: interface is %s.\n", info.interface);
85 mpu9250_interface_debug_print("mpu9250: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
86 mpu9250_interface_debug_print("mpu9250: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
87 mpu9250_interface_debug_print("mpu9250: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
88 mpu9250_interface_debug_print("mpu9250: max current is %0.2fmA.\n", info.max_current_ma);
89 mpu9250_interface_debug_print("mpu9250: max temperature is %0.1fC.\n", info.temperature_max);
90 mpu9250_interface_debug_print("mpu9250: min temperature is %0.1fC.\n", info.temperature_min);
91 }
92
93 /* start read test */
94 mpu9250_interface_debug_print("mpu9250: start read test.\n");
95
96 /* set the interface */
97 res = mpu9250_set_interface(&gs_handle, interface);
98 if (res != 0)
99 {
100 mpu9250_interface_debug_print("mpu9250: set interface failed.\n");
101
102 return 1;
103 }
104
105 /* set the addr pin */
106 res = mpu9250_set_addr_pin(&gs_handle, addr);
107 if (res != 0)
108 {
109 mpu9250_interface_debug_print("mpu9250: set addr pin failed.\n");
110
111 return 1;
112 }
113
114 /* init */
115 res = mpu9250_init(&gs_handle);
116 if (res != 0)
117 {
118 mpu9250_interface_debug_print("mpu9250: init failed.\n");
119
120 return 1;
121 }
122
123 /* delay 100 ms */
125
126 /* disable sleep */
127 res = mpu9250_set_sleep(&gs_handle, MPU9250_BOOL_FALSE);
128 if (res != 0)
129 {
130 mpu9250_interface_debug_print("mpu9250: set sleep failed.\n");
131 (void)mpu9250_deinit(&gs_handle);
132
133 return 1;
134 }
135
136 /* if spi interface, disable iic interface */
137 if (interface == MPU9250_INTERFACE_SPI)
138 {
139 /* disable iic */
141 if (res != 0)
142 {
143 mpu9250_interface_debug_print("mpu9250: set disable iic slave failed.\n");
144 (void)mpu9250_deinit(&gs_handle);
145
146 return 1;
147 }
148 }
149
150 /* set fifo 1024kb */
151 res = mpu9250_set_fifo_1024kb(&gs_handle);
152 if (res != 0)
153 {
154 mpu9250_interface_debug_print("mpu9250: set fifo 1024kb failed.\n");
155 (void)mpu9250_deinit(&gs_handle);
156
157 return 1;
158 }
159
160 /* set pll */
162 if (res != 0)
163 {
164 mpu9250_interface_debug_print("mpu9250: set clock source failed.\n");
165 (void)mpu9250_deinit(&gs_handle);
166
167 return 1;
168 }
169
170 /* set 50Hz */
171 res = mpu9250_set_sample_rate_divider(&gs_handle, (1000 / 50) - 1);
172 if (res != 0)
173 {
174 mpu9250_interface_debug_print("mpu9250: set sample rate divider failed.\n");
175 (void)mpu9250_deinit(&gs_handle);
176
177 return 1;
178 }
179
180 /* enable temperature sensor */
181 res = mpu9250_set_ptat(&gs_handle, MPU9250_BOOL_TRUE);
182 if (res != 0)
183 {
184 mpu9250_interface_debug_print("mpu9250: set ptat failed.\n");
185 (void)mpu9250_deinit(&gs_handle);
186
187 return 1;
188 }
189
190 /* disable cycle wake up */
192 if (res != 0)
193 {
194 mpu9250_interface_debug_print("mpu9250: set cycle wake up failed.\n");
195 (void)mpu9250_deinit(&gs_handle);
196
197 return 1;
198 }
199
200 /* enable acc x */
202 if (res != 0)
203 {
204 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
205 (void)mpu9250_deinit(&gs_handle);
206
207 return 1;
208 }
209
210 /* enable acc y */
212 if (res != 0)
213 {
214 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
215 (void)mpu9250_deinit(&gs_handle);
216
217 return 1;
218 }
219
220 /* enable acc z */
222 if (res != 0)
223 {
224 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
225 (void)mpu9250_deinit(&gs_handle);
226
227 return 1;
228 }
229
230 /* enable gyro x */
232 if (res != 0)
233 {
234 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
235 (void)mpu9250_deinit(&gs_handle);
236
237 return 1;
238 }
239
240 /* enable gyro y */
242 if (res != 0)
243 {
244 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
245 (void)mpu9250_deinit(&gs_handle);
246
247 return 1;
248 }
249
250 /* enable gyro z */
252 if (res != 0)
253 {
254 mpu9250_interface_debug_print("mpu9250: set standby mode failed.\n");
255 (void)mpu9250_deinit(&gs_handle);
256
257 return 1;
258 }
259
260 /* disable gyroscope x test */
262 if (res != 0)
263 {
264 mpu9250_interface_debug_print("mpu9250: set gyroscope test failed.\n");
265 (void)mpu9250_deinit(&gs_handle);
266
267 return 1;
268 }
269
270 /* disable gyroscope y test */
272 if (res != 0)
273 {
274 mpu9250_interface_debug_print("mpu9250: set gyroscope test failed.\n");
275 (void)mpu9250_deinit(&gs_handle);
276
277 return 1;
278 }
279
280 /* disable gyroscope z test */
282 if (res != 0)
283 {
284 mpu9250_interface_debug_print("mpu9250: set gyroscope test failed.\n");
285 (void)mpu9250_deinit(&gs_handle);
286
287 return 1;
288 }
289
290 /* disable accelerometer x test */
292 if (res != 0)
293 {
294 mpu9250_interface_debug_print("mpu9250: set accelerometer test failed.\n");
295 (void)mpu9250_deinit(&gs_handle);
296
297 return 1;
298 }
299
300 /* disable accelerometer y test */
302 if (res != 0)
303 {
304 mpu9250_interface_debug_print("mpu9250: set accelerometer test failed.\n");
305 (void)mpu9250_deinit(&gs_handle);
306
307 return 1;
308 }
309
310 /* disable accelerometer z test */
312 if (res != 0)
313 {
314 mpu9250_interface_debug_print("mpu9250: set accelerometer test failed.\n");
315 (void)mpu9250_deinit(&gs_handle);
316
317 return 1;
318 }
319
320 /* disable fifo */
321 res = mpu9250_set_fifo(&gs_handle, MPU9250_BOOL_FALSE);
322 if (res != 0)
323 {
324 mpu9250_interface_debug_print("mpu9250: set fifo failed.\n");
325 (void)mpu9250_deinit(&gs_handle);
326
327 return 1;
328 }
329
330 /* disable temp fifo */
332 if (res != 0)
333 {
334 mpu9250_interface_debug_print("mpu9250: set fifo enable failed.\n");
335 (void)mpu9250_deinit(&gs_handle);
336
337 return 1;
338 }
339
340 /* disable xg fifo */
342 if (res != 0)
343 {
344 mpu9250_interface_debug_print("mpu9250: set fifo enable failed.\n");
345 (void)mpu9250_deinit(&gs_handle);
346
347 return 1;
348 }
349
350 /* disable yg fifo */
352 if (res != 0)
353 {
354 mpu9250_interface_debug_print("mpu9250: set fifo enable failed.\n");
355 (void)mpu9250_deinit(&gs_handle);
356
357 return 1;
358 }
359
360 /* disable zg fifo */
362 if (res != 0)
363 {
364 mpu9250_interface_debug_print("mpu9250: set fifo enable failed.\n");
365 (void)mpu9250_deinit(&gs_handle);
366
367 return 1;
368 }
369
370 /* disable accel fifo */
372 if (res != 0)
373 {
374 mpu9250_interface_debug_print("mpu9250: set fifo enable failed.\n");
375 (void)mpu9250_deinit(&gs_handle);
376
377 return 1;
378 }
379
380 /* set interrupt level low */
382 if (res != 0)
383 {
384 mpu9250_interface_debug_print("mpu9250: set interrupt level failed.\n");
385 (void)mpu9250_deinit(&gs_handle);
386
387 return 1;
388 }
389
390 /* push-pull */
392 if (res != 0)
393 {
394 mpu9250_interface_debug_print("mpu9250: set interrupt pin type failed.\n");
395 (void)mpu9250_deinit(&gs_handle);
396
397 return 1;
398 }
399
400 /* disable motion */
402 if (res != 0)
403 {
404 mpu9250_interface_debug_print("mpu9250: set interrupt failed.\n");
405 (void)mpu9250_deinit(&gs_handle);
406
407 return 1;
408 }
409
410 /* disable fifo overflow */
412 if (res != 0)
413 {
414 mpu9250_interface_debug_print("mpu9250: set interrupt failed.\n");
415 (void)mpu9250_deinit(&gs_handle);
416
417 return 1;
418 }
419
420 /* disable dmp interrupt */
422 if (res != 0)
423 {
424 mpu9250_interface_debug_print("mpu9250: set interrupt failed.\n");
425 (void)mpu9250_deinit(&gs_handle);
426
427 return 1;
428 }
429
430 /* disable fsync int */
432 if (res != 0)
433 {
434 mpu9250_interface_debug_print("mpu9250: set interrupt failed.\n");
435 (void)mpu9250_deinit(&gs_handle);
436
437 return 1;
438 }
439
440 /* disable data ready */
442 if (res != 0)
443 {
444 mpu9250_interface_debug_print("mpu9250: set interrupt failed.\n");
445 (void)mpu9250_deinit(&gs_handle);
446
447 return 1;
448 }
449
450 /* enable latch */
452 if (res != 0)
453 {
454 mpu9250_interface_debug_print("mpu9250: set interrupt latch failed.\n");
455 (void)mpu9250_deinit(&gs_handle);
456
457 return 1;
458 }
459
460 /* enable interrupt read clear */
462 if (res != 0)
463 {
464 mpu9250_interface_debug_print("mpu9250: set interrupt read clear failed.\n");
465 (void)mpu9250_deinit(&gs_handle);
466
467 return 1;
468 }
469
470 /* disable sync input */
472 if (res != 0)
473 {
474 mpu9250_interface_debug_print("mpu9250: set extern sync failed.\n");
475 (void)mpu9250_deinit(&gs_handle);
476
477 return 1;
478 }
479
480 /* disable fsync interrupt */
482 if (res != 0)
483 {
484 mpu9250_interface_debug_print("mpu9250: set fsync interrupt failed.\n");
485 (void)mpu9250_deinit(&gs_handle);
486
487 return 1;
488 }
489
490 /* fsync interrupt level low */
492 if (res != 0)
493 {
494 mpu9250_interface_debug_print("mpu9250: set fsync interrupt level failed.\n");
495 (void)mpu9250_deinit(&gs_handle);
496
497 return 1;
498 }
499
500 /* disable iic master */
502 if (res != 0)
503 {
504 mpu9250_interface_debug_print("mpu9250: set iic master failed.\n");
505 (void)mpu9250_deinit(&gs_handle);
506
507 return 1;
508 }
509
510 /* disable iic bypass */
512 if (res != 0)
513 {
514 mpu9250_interface_debug_print("mpu9250: set iic bypass failed.\n");
515 (void)mpu9250_deinit(&gs_handle);
516
517 return 1;
518 }
519
520 /* disable gyro standby */
522 if (res != 0)
523 {
524 mpu9250_interface_debug_print("mpu9250: set gyro standby failed.\n");
525 (void)mpu9250_deinit(&gs_handle);
526
527 return 1;
528 }
529
530 /* set the fifo normal mode */
532 if (res != 0)
533 {
534 mpu9250_interface_debug_print("mpu9250: set fifo mode failed.\n");
535 (void)mpu9250_deinit(&gs_handle);
536
537 return 1;
538 }
539
540 /* set gyroscope choice 0 */
541 res = mpu9250_set_gyroscope_choice(&gs_handle, 0);
542 if (res != 0)
543 {
544 mpu9250_interface_debug_print("mpu9250: set gyroscope choice failed.\n");
545 (void)mpu9250_deinit(&gs_handle);
546
547 return 1;
548 }
549
550 /* set low pass filter 3 */
552 if (res != 0)
553 {
554 mpu9250_interface_debug_print("mpu9250: set low pass filter failed.\n");
555 (void)mpu9250_deinit(&gs_handle);
556
557 return 1;
558 }
559
560 /* set accelerometer choice 0 */
561 res = mpu9250_set_accelerometer_choice(&gs_handle, 0);
562 if (res != 0)
563 {
564 mpu9250_interface_debug_print("mpu9250: set accelerometer choice failed.\n");
565 (void)mpu9250_deinit(&gs_handle);
566
567 return 1;
568 }
569
570 /* set accelerometer low pass filter 3 */
572 if (res != 0)
573 {
574 mpu9250_interface_debug_print("mpu9250: set accelerometer low pass filter failed.\n");
575 (void)mpu9250_deinit(&gs_handle);
576
577 return 1;
578 }
579
580 /* set low power accel output rate 62.5Hz */
582 if (res != 0)
583 {
584 mpu9250_interface_debug_print("mpu9250: set low power accel output rate failed.\n");
585 (void)mpu9250_deinit(&gs_handle);
586
587 return 1;
588 }
589
590 /* disable wake on motion */
592 if (res != 0)
593 {
594 mpu9250_interface_debug_print("mpu9250: set wake on motion failed.\n");
595 (void)mpu9250_deinit(&gs_handle);
596
597 return 1;
598 }
599
600 /* enable accel compare with previous sample */
602 if (res != 0)
603 {
604 mpu9250_interface_debug_print("mpu9250: set accel compare with previous sample failed.\n");
605 (void)mpu9250_deinit(&gs_handle);
606
607 return 1;
608 }
609
610 /* ±2g */
612 if (res != 0)
613 {
614 mpu9250_interface_debug_print("mpu9250: set accelerometer range failed.\n");
615 (void)mpu9250_deinit(&gs_handle);
616
617 return 1;
618 }
619
620 /* delay 100 ms */
622
623 mpu9250_interface_debug_print("mpu9250: set accelerometer range 2g.\n");
624 for (i = 0; i < times; i++)
625 {
626 int16_t accel_raw[1][3];
627 float accel_g[1][3];
628 int16_t gyro_raw[1][3];
629 float gyro_dps[1][3];
630 int16_t mag_raw[1][3];
631 float mag_ut[1][3];
632 uint16_t len;
633
634 len = 1;
635 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
636 if (res != 0)
637 {
638 mpu9250_interface_debug_print("mpu9250: read failed.\n");
639 (void)mpu9250_deinit(&gs_handle);
640
641 return 1;
642 }
643 mpu9250_interface_debug_print("mpu9250: acc x is %0.2fg.\n", accel_g[0][0]);
644 mpu9250_interface_debug_print("mpu9250: acc y is %0.2fg.\n", accel_g[0][1]);
645 mpu9250_interface_debug_print("mpu9250: acc z is %0.2fg.\n", accel_g[0][2]);
646
647 /* delay 1000 ms */
649 }
650
651 /* ±4g */
653 if (res != 0)
654 {
655 mpu9250_interface_debug_print("mpu9250: set accelerometer range failed.\n");
656 (void)mpu9250_deinit(&gs_handle);
657
658 return 1;
659 }
660
661 /* delay 100 ms */
663
664 mpu9250_interface_debug_print("mpu9250: set accelerometer range 4g.\n");
665 for (i = 0; i < times; i++)
666 {
667 int16_t accel_raw[1][3];
668 float accel_g[1][3];
669 int16_t gyro_raw[1][3];
670 float gyro_dps[1][3];
671 int16_t mag_raw[1][3];
672 float mag_ut[1][3];
673 uint16_t len;
674
675 len = 1;
676 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
677 if (res != 0)
678 {
679 mpu9250_interface_debug_print("mpu9250: read failed.\n");
680 (void)mpu9250_deinit(&gs_handle);
681
682 return 1;
683 }
684 mpu9250_interface_debug_print("mpu9250: acc x is %0.2fg.\n", accel_g[0][0]);
685 mpu9250_interface_debug_print("mpu9250: acc y is %0.2fg.\n", accel_g[0][1]);
686 mpu9250_interface_debug_print("mpu9250: acc z is %0.2fg.\n", accel_g[0][2]);
687
688 /* delay 1000 ms */
690 }
691
692 /* ±8g */
694 if (res != 0)
695 {
696 mpu9250_interface_debug_print("mpu9250: set accelerometer range failed.\n");
697 (void)mpu9250_deinit(&gs_handle);
698
699 return 1;
700 }
701
702 /* delay 100 ms */
704
705 mpu9250_interface_debug_print("mpu9250: set accelerometer range 8g.\n");
706 for (i = 0; i < times; i++)
707 {
708 int16_t accel_raw[1][3];
709 float accel_g[1][3];
710 int16_t gyro_raw[1][3];
711 float gyro_dps[1][3];
712 int16_t mag_raw[1][3];
713 float mag_ut[1][3];
714 uint16_t len;
715
716 len = 1;
717 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
718 if (res != 0)
719 {
720 mpu9250_interface_debug_print("mpu9250: read failed.\n");
721 (void)mpu9250_deinit(&gs_handle);
722
723 return 1;
724 }
725 mpu9250_interface_debug_print("mpu9250: acc x is %0.2fg.\n", accel_g[0][0]);
726 mpu9250_interface_debug_print("mpu9250: acc y is %0.2fg.\n", accel_g[0][1]);
727 mpu9250_interface_debug_print("mpu9250: acc z is %0.2fg.\n", accel_g[0][2]);
728
729 /* delay 1000 ms */
731 }
732
733 /* ±16g */
735 if (res != 0)
736 {
737 mpu9250_interface_debug_print("mpu9250: set accelerometer range failed.\n");
738 (void)mpu9250_deinit(&gs_handle);
739
740 return 1;
741 }
742
743 /* delay 100 ms */
745
746 mpu9250_interface_debug_print("mpu9250: set accelerometer range 16g.\n");
747 for (i = 0; i < times; i++)
748 {
749 int16_t accel_raw[1][3];
750 float accel_g[1][3];
751 int16_t gyro_raw[1][3];
752 float gyro_dps[1][3];
753 int16_t mag_raw[1][3];
754 float mag_ut[1][3];
755 uint16_t len;
756
757 len = 1;
758 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
759 if (res != 0)
760 {
761 mpu9250_interface_debug_print("mpu9250: read failed.\n");
762 (void)mpu9250_deinit(&gs_handle);
763
764 return 1;
765 }
766 mpu9250_interface_debug_print("mpu9250: acc x is %0.2fg.\n", accel_g[0][0]);
767 mpu9250_interface_debug_print("mpu9250: acc y is %0.2fg.\n", accel_g[0][1]);
768 mpu9250_interface_debug_print("mpu9250: acc z is %0.2fg.\n", accel_g[0][2]);
769
770 /* delay 1000 ms */
772 }
773
774 /* ±250dps */
776 if (res != 0)
777 {
778 mpu9250_interface_debug_print("mpu9250: set gyroscope range failed.\n");
779 (void)mpu9250_deinit(&gs_handle);
780
781 return 1;
782 }
783
784 /* delay 100 ms */
786
787 mpu9250_interface_debug_print("mpu9250: set gyroscope range 250dps.\n");
788 for (i = 0; i < times; i++)
789 {
790 int16_t accel_raw[1][3];
791 float accel_g[1][3];
792 int16_t gyro_raw[1][3];
793 float gyro_dps[1][3];
794 int16_t mag_raw[1][3];
795 float mag_ut[1][3];
796 uint16_t len;
797
798 len = 1;
799 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
800 if (res != 0)
801 {
802 mpu9250_interface_debug_print("mpu9250: read failed.\n");
803 (void)mpu9250_deinit(&gs_handle);
804
805 return 1;
806 }
807 mpu9250_interface_debug_print("mpu9250: gyro x is %0.2fdps.\n", gyro_dps[0][0]);
808 mpu9250_interface_debug_print("mpu9250: gyro y is %0.2fdps.\n", gyro_dps[0][1]);
809 mpu9250_interface_debug_print("mpu9250: gyro z is %0.2fdps.\n", gyro_dps[0][2]);
810
811 /* delay 1000 ms */
813 }
814
815 /* ±500dps */
817 if (res != 0)
818 {
819 mpu9250_interface_debug_print("mpu9250: set gyroscope range failed.\n");
820 (void)mpu9250_deinit(&gs_handle);
821
822 return 1;
823 }
824
825 /* delay 100 ms */
827
828 mpu9250_interface_debug_print("mpu9250: set gyroscope range 500dps.\n");
829 for (i = 0; i < times; i++)
830 {
831 int16_t accel_raw[1][3];
832 float accel_g[1][3];
833 int16_t gyro_raw[1][3];
834 float gyro_dps[1][3];
835 int16_t mag_raw[1][3];
836 float mag_ut[1][3];
837 uint16_t len;
838
839 len = 1;
840 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
841 if (res != 0)
842 {
843 mpu9250_interface_debug_print("mpu9250: read failed.\n");
844 (void)mpu9250_deinit(&gs_handle);
845
846 return 1;
847 }
848 mpu9250_interface_debug_print("mpu9250: gyro x is %0.2fdps.\n", gyro_dps[0][0]);
849 mpu9250_interface_debug_print("mpu9250: gyro y is %0.2fdps.\n", gyro_dps[0][1]);
850 mpu9250_interface_debug_print("mpu9250: gyro z is %0.2fdps.\n", gyro_dps[0][2]);
851
852 /* delay 1000 ms */
854 }
855
856 /* ±1000dps */
858 if (res != 0)
859 {
860 mpu9250_interface_debug_print("mpu9250: set gyroscope range failed.\n");
861 (void)mpu9250_deinit(&gs_handle);
862
863 return 1;
864 }
865
866 /* delay 100 ms */
868
869 mpu9250_interface_debug_print("mpu9250: set gyroscope range 1000dps.\n");
870 for (i = 0; i < times; i++)
871 {
872 int16_t accel_raw[1][3];
873 float accel_g[1][3];
874 int16_t gyro_raw[1][3];
875 float gyro_dps[1][3];
876 int16_t mag_raw[1][3];
877 float mag_ut[1][3];
878 uint16_t len;
879
880 len = 1;
881 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
882 if (res != 0)
883 {
884 mpu9250_interface_debug_print("mpu9250: read failed.\n");
885 (void)mpu9250_deinit(&gs_handle);
886
887 return 1;
888 }
889 mpu9250_interface_debug_print("mpu9250: gyro x is %0.2fdps.\n", gyro_dps[0][0]);
890 mpu9250_interface_debug_print("mpu9250: gyro y is %0.2fdps.\n", gyro_dps[0][1]);
891 mpu9250_interface_debug_print("mpu9250: gyro z is %0.2fdps.\n", gyro_dps[0][2]);
892
893 /* delay 1000 ms */
895 }
896
897 /* ±2000dps */
899 if (res != 0)
900 {
901 mpu9250_interface_debug_print("mpu9250: set gyroscope range failed.\n");
902 (void)mpu9250_deinit(&gs_handle);
903
904 return 1;
905 }
906
907 /* delay 100 ms */
909
910 mpu9250_interface_debug_print("mpu9250: set gyroscope range 2000dps.\n");
911 for (i = 0; i < times; i++)
912 {
913 int16_t accel_raw[1][3];
914 float accel_g[1][3];
915 int16_t gyro_raw[1][3];
916 float gyro_dps[1][3];
917 int16_t mag_raw[1][3];
918 float mag_ut[1][3];
919 uint16_t len;
920
921 len = 1;
922 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
923 if (res != 0)
924 {
925 mpu9250_interface_debug_print("mpu9250: read failed.\n");
926 (void)mpu9250_deinit(&gs_handle);
927
928 return 1;
929 }
930 mpu9250_interface_debug_print("mpu9250: gyro x is %0.2fdps.\n", gyro_dps[0][0]);
931 mpu9250_interface_debug_print("mpu9250: gyro y is %0.2fdps.\n", gyro_dps[0][1]);
932 mpu9250_interface_debug_print("mpu9250: gyro z is %0.2fdps.\n", gyro_dps[0][2]);
933
934 /* delay 1000 ms */
936 }
937
938 mpu9250_interface_debug_print("mpu9250: read temperature.\n");
939 for (i = 0; i < times; i++)
940 {
941 int16_t raw;
942 float degrees;
943
944 /* read temperature */
945 res = mpu9250_read_temperature(&gs_handle, &raw, &degrees);
946 if (res != 0)
947 {
948 mpu9250_interface_debug_print("mpu9250: read temperature failed.\n");
949 (void)mpu9250_deinit(&gs_handle);
950
951 return 1;
952 }
953 mpu9250_interface_debug_print("mpu9250: temperature %0.2fC.\n", degrees);
954
955 /* delay 1000 ms */
957 }
958
959 /* if iic interface */
960 if (interface == MPU9250_INTERFACE_IIC)
961 {
962 /* mag init */
963 res = mpu9250_mag_init(&gs_handle);
964 if (res != 0)
965 {
966 mpu9250_interface_debug_print("mpu9250: mag init failed.\n");
967 (void)mpu9250_deinit(&gs_handle);
968
969 return 1;
970 }
971
972 /* 100Hz */
974 if (res != 0)
975 {
976 mpu9250_interface_debug_print("mpu9250: mag set mode failed.\n");
977 (void)mpu9250_mag_deinit(&gs_handle);
978 (void)mpu9250_deinit(&gs_handle);
979
980 return 1;
981 }
982
983 /* read magnetometer */
984 mpu9250_interface_debug_print("mpu9250: read magnetometer.\n");
985
986 /* set magnetometer 14 bits */
987 mpu9250_interface_debug_print("mpu9250: set magnetometer 14 bits.\n");
988
989 /* set 14 bits */
991 if (res != 0)
992 {
993 mpu9250_interface_debug_print("mpu9250: mag set bits failed.\n");
994 (void)mpu9250_mag_deinit(&gs_handle);
995 (void)mpu9250_deinit(&gs_handle);
996
997 return 1;
998 }
999
1000 for (i = 0; i < times; i++)
1001 {
1002 int16_t accel_raw[1][3];
1003 float accel_g[1][3];
1004 int16_t gyro_raw[1][3];
1005 float gyro_dps[1][3];
1006 int16_t mag_raw[1][3];
1007 float mag_ut[1][3];
1008 uint16_t len;
1009
1010 len = 1;
1011 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
1012 if (res != 0)
1013 {
1014 mpu9250_interface_debug_print("mpu9250: read failed.\n");
1015 (void)mpu9250_mag_deinit(&gs_handle);
1016 (void)mpu9250_deinit(&gs_handle);
1017
1018 return 1;
1019 }
1020 mpu9250_interface_debug_print("mpu9250: mag x is %0.2fuT.\n", mag_ut[0][0]);
1021 mpu9250_interface_debug_print("mpu9250: mag y is %0.2fuT.\n", mag_ut[0][1]);
1022 mpu9250_interface_debug_print("mpu9250: mag z is %0.2fuT.\n", mag_ut[0][2]);
1023
1024 /* delay 1000 ms */
1026 }
1027
1028 /* set magnetometer 16 bits */
1029 mpu9250_interface_debug_print("mpu9250: set magnetometer 16 bits.\n");
1030
1031 /* set 16 bits */
1033 if (res != 0)
1034 {
1035 mpu9250_interface_debug_print("mpu9250: mag set bits failed.\n");
1036 (void)mpu9250_mag_deinit(&gs_handle);
1037 (void)mpu9250_deinit(&gs_handle);
1038
1039 return 1;
1040 }
1041
1042 for (i = 0; i < times; i++)
1043 {
1044 int16_t accel_raw[1][3];
1045 float accel_g[1][3];
1046 int16_t gyro_raw[1][3];
1047 float gyro_dps[1][3];
1048 int16_t mag_raw[1][3];
1049 float mag_ut[1][3];
1050 uint16_t len;
1051
1052 len = 1;
1053 res = mpu9250_read(&gs_handle, accel_raw, accel_g, gyro_raw, gyro_dps, mag_raw, mag_ut, &len);
1054 if (res != 0)
1055 {
1056 mpu9250_interface_debug_print("mpu9250: read failed.\n");
1057 (void)mpu9250_mag_deinit(&gs_handle);
1058 (void)mpu9250_deinit(&gs_handle);
1059
1060 return 1;
1061 }
1062 mpu9250_interface_debug_print("mpu9250: mag x is %0.2fuT.\n", mag_ut[0][0]);
1063 mpu9250_interface_debug_print("mpu9250: mag y is %0.2fuT.\n", mag_ut[0][1]);
1064 mpu9250_interface_debug_print("mpu9250: mag z is %0.2fuT.\n", mag_ut[0][2]);
1065
1066 /* delay 1000 ms */
1068 }
1069
1070 /* mpu9250 mag read test */
1071 mpu9250_interface_debug_print("mpu9250: mpu9250 mag read test.\n");
1072
1073 for (i = 0; i < times; i++)
1074 {
1075 int16_t mag_raw[3];
1076 float mag_ut[3];
1077
1078 res = mpu9250_mag_read(&gs_handle, mag_raw, mag_ut);
1079 if (res != 0)
1080 {
1081 mpu9250_interface_debug_print("mpu9250: mag read failed.\n");
1082 (void)mpu9250_mag_deinit(&gs_handle);
1083 (void)mpu9250_deinit(&gs_handle);
1084
1085 return 1;
1086 }
1087 mpu9250_interface_debug_print("mpu9250: mag x is %0.2fuT.\n", mag_ut[0]);
1088 mpu9250_interface_debug_print("mpu9250: mag y is %0.2fuT.\n", mag_ut[1]);
1089 mpu9250_interface_debug_print("mpu9250: mag z is %0.2fuT.\n", mag_ut[2]);
1090
1091 /* delay 1000 ms */
1093 }
1094
1095 /* mag deinit */
1096 (void)mpu9250_mag_deinit(&gs_handle);
1097 }
1098
1099 /* finish read test */
1100 mpu9250_interface_debug_print("mpu9250: finish read test.\n");
1101 (void)mpu9250_deinit(&gs_handle);
1102
1103 return 0;
1104}
driver mpu9250 read test header file
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_set_addr_pin(mpu9250_handle_t *handle, mpu9250_address_t addr_pin)
set the chip address pin
uint8_t mpu9250_set_ptat(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the temperature sensor
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_set_standby_mode(mpu9250_handle_t *handle, mpu9250_source_t source, mpu9250_bool_t enable)
set source into standby mode
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_interrupt_latch(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the interrupt latch
uint8_t mpu9250_set_extern_sync(mpu9250_handle_t *handle, mpu9250_extern_sync_t sync)
set the extern sync type
uint8_t mpu9250_set_gyro_standby(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the gyro standby
mpu9250_address_t
mpu9250 address enumeration definition
uint8_t mpu9250_set_fsync_interrupt(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the fsync interrupt
uint8_t mpu9250_set_iic_bypass(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic bypass
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_interrupt_read_clear(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the interrupt reading clear
uint8_t mpu9250_set_interface(mpu9250_handle_t *handle, mpu9250_interface_t interface)
set the chip interface
struct mpu9250_info_s mpu9250_info_t
mpu9250 information structure definition
mpu9250_interface_t
mpu9250 interface enumeration definition
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_set_fsync_interrupt_level(mpu9250_handle_t *handle, mpu9250_pin_level_t level)
set the fsync interrupt level
uint8_t mpu9250_set_gyroscope_choice(mpu9250_handle_t *handle, uint8_t choice)
set the gyroscope choice
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_set_fifo_1024kb(mpu9250_handle_t *handle)
set fifo 1024kb
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_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_set_disable_iic_slave(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic slave mode
uint8_t mpu9250_set_sample_rate_divider(mpu9250_handle_t *handle, uint8_t d)
set the sample rate divider
uint8_t mpu9250_set_fifo(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable fifo
uint8_t mpu9250_init(mpu9250_handle_t *handle)
initialize the chip
uint8_t mpu9250_set_accelerometer_choice(mpu9250_handle_t *handle, uint8_t choice)
set the accelerometer choice
uint8_t mpu9250_set_gyroscope_range(mpu9250_handle_t *handle, mpu9250_gyroscope_range_t range)
set the gyroscope range
uint8_t mpu9250_set_interrupt_pin_type(mpu9250_handle_t *handle, mpu9250_pin_type_t type)
set the interrupt pin type
uint8_t mpu9250_set_iic_master(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the iic master mode
uint8_t mpu9250_set_accelerometer_range(mpu9250_handle_t *handle, mpu9250_accelerometer_range_t range)
set the accelerometer range
uint8_t mpu9250_set_sleep(mpu9250_handle_t *handle, mpu9250_bool_t enable)
enable or disable the sleep mode
@ MPU9250_ACCELEROMETER_RANGE_4G
@ MPU9250_ACCELEROMETER_RANGE_2G
@ MPU9250_ACCELEROMETER_RANGE_16G
@ MPU9250_ACCELEROMETER_RANGE_8G
@ MPU9250_PIN_TYPE_PUSH_PULL
@ MPU9250_GYROSCOPE_RANGE_2000DPS
@ MPU9250_GYROSCOPE_RANGE_250DPS
@ MPU9250_GYROSCOPE_RANGE_500DPS
@ MPU9250_GYROSCOPE_RANGE_1000DPS
@ MPU9250_INTERRUPT_MOTION
@ MPU9250_INTERRUPT_DATA_READY
@ MPU9250_INTERRUPT_FSYNC_INT
@ MPU9250_INTERRUPT_DMP
@ MPU9250_INTERRUPT_FIFO_OVERFLOW
@ MPU9250_EXTERN_SYNC_INPUT_DISABLED
@ MPU9250_SOURCE_GYRO_X
@ MPU9250_SOURCE_ACC_Z
@ MPU9250_SOURCE_GYRO_Y
@ MPU9250_SOURCE_GYRO_Z
@ MPU9250_SOURCE_ACC_X
@ MPU9250_SOURCE_ACC_Y
@ MPU9250_INTERFACE_SPI
@ MPU9250_INTERFACE_IIC
@ MPU9250_BOOL_TRUE
@ MPU9250_BOOL_FALSE
@ MPU9250_LOW_PASS_FILTER_3
@ MPU9250_FIFO_MODE_NORMAL
@ MPU9250_LOW_POWER_ACCEL_OUTPUT_RATE_62P50
@ MPU9250_AXIS_X
@ MPU9250_AXIS_Z
@ MPU9250_AXIS_Y
@ MPU9250_ACCELEROMETER_LOW_PASS_FILTER_3
@ MPU9250_PIN_LEVEL_LOW
@ MPU9250_FIFO_XG
@ MPU9250_FIFO_YG
@ MPU9250_FIFO_ZG
@ MPU9250_FIFO_ACCEL
@ MPU9250_FIFO_TEMP
@ MPU9250_CLOCK_SOURCE_PLL
void mpu9250_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t mpu9250_interface_spi_write(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus write
uint8_t mpu9250_interface_iic_deinit(void)
interface iic bus deinit
uint8_t mpu9250_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t mpu9250_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t mpu9250_interface_iic_init(void)
interface iic bus init
uint8_t mpu9250_interface_spi_read(uint8_t reg, uint8_t *buf, uint16_t len)
interface spi bus read
uint8_t mpu9250_interface_spi_init(void)
interface spi bus init
void mpu9250_interface_receive_callback(uint8_t type)
interface receive callback
void mpu9250_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t mpu9250_interface_spi_deinit(void)
interface spi bus deinit
uint8_t mpu9250_mag_set_mode(mpu9250_handle_t *handle, mpu9250_magnetometer_mode_t mode)
magnetometer set the mode
uint8_t mpu9250_mag_set_bits(mpu9250_handle_t *handle, mpu9250_magnetometer_bits_t bits)
magnetometer set the bits
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_read(mpu9250_handle_t *handle, int16_t mag_raw[3], float mag_ut[3])
mag read the data
@ MPU9250_MAGNETOMETER_BITS_16
@ MPU9250_MAGNETOMETER_BITS_14
@ MPU9250_MAGNETOMETER_MODE_CONTINUOUS2
uint8_t mpu9250_read_test(mpu9250_interface_t interface, mpu9250_address_t addr, uint32_t times)
read test
uint32_t driver_version
char manufacturer_name[32]