LibDriver MAG3110
Loading...
Searching...
No Matches
driver_mag3110.c
Go to the documentation of this file.
1
36
37#include "driver_mag3110.h"
38
42#define CHIP_NAME "NXP MAG3110"
43#define MANUFACTURER_NAME "NXP"
44#define SUPPLY_VOLTAGE_MIN 1.95f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.9f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define MAG3110_ADDRESS (0x0E << 1)
55
59#define MAG3110_REG_DR_STATUS 0x00
60#define MAG3110_REG_OUT_X_MSB 0x01
61#define MAG3110_REG_OUT_X_LSB 0x02
62#define MAG3110_REG_OUT_Y_MSB 0x03
63#define MAG3110_REG_OUT_Y_LSB 0x04
64#define MAG3110_REG_OUT_Z_MSB 0x05
65#define MAG3110_REG_OUT_Z_LSB 0x06
66#define MAG3110_REG_WHO_AM_I 0x07
67#define MAG3110_REG_SYSMOD 0x08
68#define MAG3110_REG_OFF_X_MSB 0x09
69#define MAG3110_REG_OFF_X_LSB 0x0A
70#define MAG3110_REG_OFF_Y_MSB 0x0B
71#define MAG3110_REG_OFF_Y_LSB 0x0C
72#define MAG3110_REG_OFF_Z_MSB 0x0D
73#define MAG3110_REG_OFF_Z_LSB 0x0E
74#define MAG3110_REG_DIE_TEMP 0x0F
75#define MAG3110_REG_CTRL_REG1 0x10
76#define MAG3110_REG_CTRL_REG2 0x11
77
89static uint8_t a_mag3110_iic_read(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
90{
91 if (handle->iic_read(MAG3110_ADDRESS, reg, (uint8_t *)buf, len) != 0) /* read data */
92 {
93 return 1; /* return error */
94 }
95
96 return 0; /* success return 0 */
97}
98
110static uint8_t a_mag3110_iic_write(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
111{
112 if (handle->iic_write(MAG3110_ADDRESS, reg, (uint8_t *)buf, len) != 0) /* write data */
113 {
114 return 1; /* return error */
115 }
116
117 return 0; /* success return 0 */
118}
119
131uint8_t mag3110_get_data_ready_status(mag3110_handle_t *handle, uint8_t *status)
132{
133 uint8_t res;
134
135 if (handle == NULL) /* check handle */
136 {
137 return 2; /* return error */
138 }
139 if (handle->inited != 1) /* check handle initialization */
140 {
141 return 3; /* return error */
142 }
143
144 res = a_mag3110_iic_read(handle, MAG3110_REG_DR_STATUS, status, 1); /* read status config */
145 if (res != 0) /* check result */
146 {
147 handle->debug_print("mag3110: read status failed.\n"); /* read status failed */
148
149 return 1; /* return error */
150 }
151
152 return 0; /* success return 0 */
153}
154
167{
168 uint8_t res;
169 uint8_t prev;
170
171 if (handle == NULL) /* check handle */
172 {
173 return 2; /* return error */
174 }
175 if (handle->inited != 1) /* check handle initialization */
176 {
177 return 3; /* return error */
178 }
179
180 res = a_mag3110_iic_read(handle, MAG3110_REG_SYSMOD, &prev, 1); /* read mode config */
181 if (res != 0) /* check result */
182 {
183 handle->debug_print("mag3110: read mode failed.\n"); /* read mode failed */
184
185 return 1; /* return error */
186 }
187 *mode = (mag3110_mode_status_t)((prev >> 0) & 0x03); /* set mode */
188
189 return 0; /* success return 0 */
190}
191
203uint8_t mag3110_offset_convert_to_register(mag3110_handle_t *handle, float ut, uint16_t *reg)
204{
205 int16_t r;
206
207 if (handle == NULL) /* check handle */
208 {
209 return 2; /* return error */
210 }
211 if (handle->inited != 1) /* check handle initialization */
212 {
213 return 3; /* return error */
214 }
215
216 r = (int16_t)(ut / 0.1f); /* convert real data to register data */
217 r &= ~(1 << 15); /* clear bit */
218 *reg = (uint16_t)(r); /* set register */
219
220 return 0; /* success return 0 */
221}
222
234uint8_t mag3110_offset_convert_to_data(mag3110_handle_t *handle, uint16_t reg, float *ut)
235{
236 int16_t r;
237
238 if (handle == NULL) /* check handle */
239 {
240 return 2; /* return error */
241 }
242 if (handle->inited != 1) /* check handle initialization */
243 {
244 return 3; /* return error */
245 }
246
247 r = (int16_t)reg; /* set register */
248 if ((r & (1 << 14)) != 0) /* check bit 14 */
249 {
250 r |= (1 << 15); /* set bit 15 */
251 }
252 *ut = (float)(r) * 0.1f; /* convert raw data to real data */
253
254 return 0; /* success return 0 */
255}
256
269uint8_t mag3110_set_offset_x(mag3110_handle_t *handle, uint16_t offset)
270{
271 uint8_t res;
272 uint16_t prev;
273 uint8_t buf[2];
274
275 if (handle == NULL) /* check handle */
276 {
277 return 2; /* return error */
278 }
279 if (handle->inited != 1) /* check handle initialization */
280 {
281 return 3; /* return error */
282 }
283 if (offset > 0x8000U) /* check offset */
284 {
285 handle->debug_print("mag3110: offset > 0x8000.\n"); /* offset > 0x8000 */
286
287 return 4; /* return error */
288 }
289
290 prev = offset << 1; /* set offset */
291 buf[0] = (prev >> 8) & 0xFF; /* set msb */
292 buf[1] = (prev >> 0) & 0xFF; /* set lsb */
293 res = a_mag3110_iic_write(handle, MAG3110_REG_OFF_X_MSB, buf, 2); /* write offset */
294 if (res != 0) /* check result */
295 {
296 handle->debug_print("mag3110: write offset failed.\n"); /* write offset failed */
297
298 return 1; /* return error */
299 }
300
301 return 0; /* success return 0 */
302}
303
315uint8_t mag3110_get_offset_x(mag3110_handle_t *handle, uint16_t *offset)
316{
317 uint8_t res;
318 uint16_t prev;
319 uint8_t buf[2];
320
321 if (handle == NULL) /* check handle */
322 {
323 return 2; /* return error */
324 }
325 if (handle->inited != 1) /* check handle initialization */
326 {
327 return 3; /* return error */
328 }
329
330 res = a_mag3110_iic_read(handle, MAG3110_REG_OFF_X_MSB, buf, 2); /* read offset */
331 if (res != 0) /* check result */
332 {
333 handle->debug_print("mag3110: read offset failed.\n"); /* read offset failed */
334
335 return 1; /* return error */
336 }
337 prev = ((uint16_t)buf[0] << 8) | buf[1]; /* set prev */
338 *offset = prev >> 1; /* set offset */
339
340 return 0; /* success return 0 */
341}
342
355uint8_t mag3110_set_offset_y(mag3110_handle_t *handle, uint16_t offset)
356{
357 uint8_t res;
358 uint16_t prev;
359 uint8_t buf[2];
360
361 if (handle == NULL) /* check handle */
362 {
363 return 2; /* return error */
364 }
365 if (handle->inited != 1) /* check handle initialization */
366 {
367 return 3; /* return error */
368 }
369 if (offset > 0x8000U) /* check offset */
370 {
371 handle->debug_print("mag3110: offset > 0x8000.\n"); /* offset > 0x8000 */
372
373 return 4; /* return error */
374 }
375
376 prev = offset << 1; /* set offset */
377 buf[0] = (prev >> 8) & 0xFF; /* set msb */
378 buf[1] = (prev >> 0) & 0xFF; /* set lsb */
379 res = a_mag3110_iic_write(handle, MAG3110_REG_OFF_Y_MSB, buf, 2); /* write offset */
380 if (res != 0) /* check result */
381 {
382 handle->debug_print("mag3110: write offset failed.\n"); /* write offset failed */
383
384 return 1; /* return error */
385 }
386
387 return 0; /* success return 0 */
388}
389
401uint8_t mag3110_get_offset_y(mag3110_handle_t *handle, uint16_t *offset)
402{
403 uint8_t res;
404 uint16_t prev;
405 uint8_t buf[2];
406
407 if (handle == NULL) /* check handle */
408 {
409 return 2; /* return error */
410 }
411 if (handle->inited != 1) /* check handle initialization */
412 {
413 return 3; /* return error */
414 }
415
416 res = a_mag3110_iic_read(handle, MAG3110_REG_OFF_Y_MSB, buf, 2); /* read offset */
417 if (res != 0) /* check result */
418 {
419 handle->debug_print("mag3110: read offset failed.\n"); /* read offset failed */
420
421 return 1; /* return error */
422 }
423 prev = ((uint16_t)buf[0] << 8) | buf[1]; /* set prev */
424 *offset = prev >> 1; /* set offset */
425
426 return 0; /* success return 0 */
427}
428
441uint8_t mag3110_set_offset_z(mag3110_handle_t *handle, uint16_t offset)
442{
443 uint8_t res;
444 uint16_t prev;
445 uint8_t buf[2];
446
447 if (handle == NULL) /* check handle */
448 {
449 return 2; /* return error */
450 }
451 if (handle->inited != 1) /* check handle initialization */
452 {
453 return 3; /* return error */
454 }
455 if (offset > 0x8000U) /* check offset */
456 {
457 handle->debug_print("mag3110: offset > 0x8000.\n"); /* offset > 0x8000 */
458
459 return 4; /* return error */
460 }
461
462 prev = offset << 1; /* set offset */
463 buf[0] = (prev >> 8) & 0xFF; /* set msb */
464 buf[1] = (prev >> 0) & 0xFF; /* set lsb */
465 res = a_mag3110_iic_write(handle, MAG3110_REG_OFF_Z_MSB, buf, 2); /* write offset */
466 if (res != 0) /* check result */
467 {
468 handle->debug_print("mag3110: write offset failed.\n"); /* write offset failed */
469
470 return 1; /* return error */
471 }
472
473 return 0; /* success return 0 */
474}
475
487uint8_t mag3110_get_offset_z(mag3110_handle_t *handle, uint16_t *offset)
488{
489 uint8_t res;
490 uint16_t prev;
491 uint8_t buf[2];
492
493 if (handle == NULL) /* check handle */
494 {
495 return 2; /* return error */
496 }
497 if (handle->inited != 1) /* check handle initialization */
498 {
499 return 3; /* return error */
500 }
501
502 res = a_mag3110_iic_read(handle, MAG3110_REG_OFF_Z_MSB, buf, 2); /* read offset */
503 if (res != 0) /* check result */
504 {
505 handle->debug_print("mag3110: read offset failed.\n"); /* read offset failed */
506
507 return 1; /* return error */
508 }
509 prev = ((uint16_t)buf[0] << 8) | buf[1]; /* set prev */
510 *offset = prev >> 1; /* set offset */
511
512 return 0; /* success return 0 */
513}
514
527uint8_t mag3110_read_die_temperature(mag3110_handle_t *handle, int8_t *raw, float *degree)
528{
529 uint8_t res;
530 uint8_t prev;
531
532 if (handle == NULL) /* check handle */
533 {
534 return 2; /* return error */
535 }
536 if (handle->inited != 1) /* check handle initialization */
537 {
538 return 3; /* return error */
539 }
540
541 res = a_mag3110_iic_read(handle, MAG3110_REG_DIE_TEMP, &prev, 1); /* read die temp */
542 if (res != 0) /* check result */
543 {
544 handle->debug_print("mag3110: read die temp failed.\n"); /* read die temp failed */
545
546 return 1; /* return error */
547 }
548 *raw = (int8_t)prev; /* set raw */
549 *degree = (float)(*raw); /* set degree */
550
551 return 0; /* success return 0 */
552}
553
566{
567 uint8_t res;
568 uint8_t prev;
569
570 if (handle == NULL) /* check handle */
571 {
572 return 2; /* return error */
573 }
574 if (handle->inited != 1) /* check handle initialization */
575 {
576 return 3; /* return error */
577 }
578
579 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
580 if (res != 0) /* check result */
581 {
582 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
583
584 return 1; /* return error */
585 }
586 prev &= ~(0x1F << 3); /* clear settings */
587 prev |= (rate_over_sample << 3); /* set settings */
588 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* write ctrl config */
589 if (res != 0) /* check result */
590 {
591 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
592
593 return 1; /* return error */
594 }
595
596 return 0; /* success return 0 */
597}
598
611{
612 uint8_t res;
613 uint8_t prev;
614
615 if (handle == NULL) /* check handle */
616 {
617 return 2; /* return error */
618 }
619 if (handle->inited != 1) /* check handle initialization */
620 {
621 return 3; /* return error */
622 }
623
624 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
625 if (res != 0) /* check result */
626 {
627 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
628
629 return 1; /* return error */
630 }
631 *rate_over_sample = (mag3110_rate_over_sample_t)((prev >> 3) & 0x1F); /* set rate over sample */
632
633 return 0; /* success return 0 */
634}
635
648{
649 uint8_t res;
650 uint8_t prev;
651
652 if (handle == NULL) /* check handle */
653 {
654 return 2; /* return error */
655 }
656 if (handle->inited != 1) /* check handle initialization */
657 {
658 return 3; /* return error */
659 }
660
661 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
662 if (res != 0) /* check result */
663 {
664 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
665
666 return 1; /* return error */
667 }
668 prev &= ~(1 << 2); /* clear settings */
669 prev |= (enable << 2); /* set bool */
670 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* write ctrl config */
671 if (res != 0) /* check result */
672 {
673 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
674
675 return 1; /* return error */
676 }
677
678 return 0; /* success return 0 */
679}
680
693{
694 uint8_t res;
695 uint8_t prev;
696
697 if (handle == NULL) /* check handle */
698 {
699 return 2; /* return error */
700 }
701 if (handle->inited != 1) /* check handle initialization */
702 {
703 return 3; /* return error */
704 }
705
706 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
707 if (res != 0) /* check result */
708 {
709 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
710
711 return 1; /* return error */
712 }
713 *enable = (mag3110_bool_t)((prev >> 2) & 0x01); /* set bool */
714
715 return 0; /* success return 0 */
716}
717
730{
731 uint8_t res;
732 uint8_t prev;
733
734 if (handle == NULL) /* check handle */
735 {
736 return 2; /* return error */
737 }
738 if (handle->inited != 1) /* check handle initialization */
739 {
740 return 3; /* return error */
741 }
742
743 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
744 if (res != 0) /* check result */
745 {
746 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
747
748 return 1; /* return error */
749 }
750 prev &= ~(1 << 1); /* clear settings */
751 prev |= (enable << 1); /* set bool */
752 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* write ctrl config */
753 if (res != 0) /* check result */
754 {
755 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
756
757 return 1; /* return error */
758 }
759
760 return 0; /* success return 0 */
761}
762
775{
776 uint8_t res;
777 uint8_t prev;
778
779 if (handle == NULL) /* check handle */
780 {
781 return 2; /* return error */
782 }
783 if (handle->inited != 1) /* check handle initialization */
784 {
785 return 3; /* return error */
786 }
787
788 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
789 if (res != 0) /* check result */
790 {
791 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
792
793 return 1; /* return error */
794 }
795 *enable = (mag3110_bool_t)((prev >> 1) & 0x01); /* set bool */
796
797 return 0; /* success return 0 */
798}
799
812{
813 uint8_t res;
814 uint8_t prev;
815
816 if (handle == NULL) /* check handle */
817 {
818 return 2; /* return error */
819 }
820 if (handle->inited != 1) /* check handle initialization */
821 {
822 return 3; /* return error */
823 }
824
825 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
826 if (res != 0) /* check result */
827 {
828 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
829
830 return 1; /* return error */
831 }
832 prev &= ~(1 << 0); /* clear settings */
833 prev |= (mode << 0); /* set mode */
834 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* write ctrl config */
835 if (res != 0) /* check result */
836 {
837 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
838
839 return 1; /* return error */
840 }
841
842 return 0; /* success return 0 */
843}
844
857{
858 uint8_t res;
859 uint8_t prev;
860
861 if (handle == NULL) /* check handle */
862 {
863 return 2; /* return error */
864 }
865 if (handle->inited != 1) /* check handle initialization */
866 {
867 return 3; /* return error */
868 }
869
870 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
871 if (res != 0) /* check result */
872 {
873 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
874
875 return 1; /* return error */
876 }
877 *mode = (mag3110_mode_t)((prev >> 0) & 0x01); /* set mode */
878
879 return 0; /* success return 0 */
880}
881
894{
895 uint8_t res;
896 uint8_t prev;
897
898 if (handle == NULL) /* check handle */
899 {
900 return 2; /* return error */
901 }
902 if (handle->inited != 1) /* check handle initialization */
903 {
904 return 3; /* return error */
905 }
906
907 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
908 if (res != 0) /* check result */
909 {
910 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
911
912 return 1; /* return error */
913 }
914 prev &= ~(1 << 7); /* clear settings */
915 prev |= (enable << 7); /* set bool */
916 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* write ctrl config */
917 if (res != 0) /* check result */
918 {
919 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
920
921 return 1; /* return error */
922 }
923
924 return 0; /* success return 0 */
925}
926
939{
940 uint8_t res;
941 uint8_t prev;
942
943 if (handle == NULL) /* check handle */
944 {
945 return 2; /* return error */
946 }
947 if (handle->inited != 1) /* check handle initialization */
948 {
949 return 3; /* return error */
950 }
951
952 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
953 if (res != 0) /* check result */
954 {
955 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
956
957 return 1; /* return error */
958 }
959 prev &= ~(1 << 5); /* clear settings */
960 prev |= (enable << 5); /* set bool */
961 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* write ctrl config */
962 if (res != 0) /* check result */
963 {
964 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
965
966 return 1; /* return error */
967 }
968
969 return 0; /* success return 0 */
970}
971
984{
985 uint8_t res;
986 uint8_t prev;
987
988 if (handle == NULL) /* check handle */
989 {
990 return 2; /* return error */
991 }
992 if (handle->inited != 1) /* check handle initialization */
993 {
994 return 3; /* return error */
995 }
996
997 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
998 if (res != 0) /* check result */
999 {
1000 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1001
1002 return 1; /* return error */
1003 }
1004 *enable = (mag3110_bool_t)((prev >> 5) & 0x01); /* set bool */
1005
1006 return 0; /* success return 0 */
1007}
1008
1021{
1022 uint8_t res;
1023 uint8_t prev;
1024 uint16_t timeout = 1000;
1025
1026 if (handle == NULL) /* check handle */
1027 {
1028 return 2; /* return error */
1029 }
1030 if (handle->inited != 1) /* check handle initialization */
1031 {
1032 return 3; /* return error */
1033 }
1034
1035 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
1036 if (res != 0) /* check result */
1037 {
1038 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1039
1040 return 1; /* return error */
1041 }
1042 prev &= ~(1 << 4); /* clear settings */
1043 prev |= (1 << 4); /* set bool */
1044 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* write ctrl config */
1045 if (res != 0) /* check result */
1046 {
1047 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
1048
1049 return 1; /* return error */
1050 }
1051 while (timeout != 0) /* check timeout */
1052 {
1053 handle->delay_ms(1); /* delay 1ms */
1054 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
1055 if (res != 0) /* check result */
1056 {
1057 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1058
1059 return 1; /* return error */
1060 }
1061 if ((prev & (1 << 4)) == 0) /* check the flag */
1062 {
1063 return 0; /* success return 0 */
1064 }
1065 timeout--; /* timeout-- */
1066 }
1067 handle->debug_print("mag3110: reset failed.\n"); /* reset failed */
1068
1069 return 4; /* return error */
1070}
1071
1085{
1086 uint8_t res;
1087 uint8_t id;
1088 uint8_t prev;
1089 uint8_t done;
1090 uint16_t timeout = 1000;
1091
1092 if (handle == NULL) /* check handle */
1093 {
1094 return 2; /* return error */
1095 }
1096 if (handle->debug_print == NULL) /* check debug_print */
1097 {
1098 return 3; /* return error */
1099 }
1100 if (handle->iic_init == NULL) /* check iic_init */
1101 {
1102 handle->debug_print("mag3110: iic_init is null.\n"); /* iic_init is null */
1103
1104 return 3; /* return error */
1105 }
1106 if (handle->iic_deinit == NULL) /* check iic_deinit */
1107 {
1108 handle->debug_print("mag3110: iic_deinit is null.\n"); /* iic_deinit is null */
1109
1110 return 3; /* return error */
1111 }
1112 if (handle->iic_read == NULL) /* check iic_read */
1113 {
1114 handle->debug_print("mag3110: iic_read is null.\n"); /* iic_read is null */
1115
1116 return 3; /* return error */
1117 }
1118 if (handle->iic_write == NULL) /* check iic_write */
1119 {
1120 handle->debug_print("mag3110: iic_write is null.\n"); /* iic_write is null */
1121
1122 return 3; /* return error */
1123 }
1124 if (handle->delay_ms == NULL) /* check delay_ms */
1125 {
1126 handle->debug_print("mag3110: delay_ms is null.\n"); /* delay_ms is null */
1127
1128 return 3; /* return error */
1129 }
1130
1131 if (handle->iic_init() != 0) /* iic init */
1132 {
1133 handle->debug_print("mag3110: iic init failed.\n"); /* iic init failed */
1134
1135 return 1; /* return error */
1136 }
1138 (uint8_t *)&id, 1) != 0) /* read id failed */
1139 {
1140 handle->debug_print("mag3110: read id failed.\n"); /* read id failed */
1141 (void)handle->iic_deinit(); /* iic deinit */
1142
1143 return 1; /* return error */
1144 }
1145 if (id != 0xC4) /* check id */
1146 {
1147 handle->debug_print("mag3110: id is invalid.\n"); /* id is invalid */
1148 (void)handle->iic_deinit(); /* iic deinit */
1149
1150 return 4; /* return error */
1151 }
1152
1153 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
1154 if (res != 0) /* check result */
1155 {
1156 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1157 (void)handle->iic_deinit(); /* iic deinit */
1158
1159 return 5; /* return error */
1160 }
1161 prev |= (1 << 4); /* set bool */
1162 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* write ctrl config */
1163 if (res != 0) /* check result */
1164 {
1165 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
1166 (void)handle->iic_deinit(); /* iic deinit */
1167
1168 return 5; /* return error */
1169 }
1170 done = 0; /* init 0 */
1171 while (timeout != 0) /* check timeout */
1172 {
1173 handle->delay_ms(1); /* delay 1ms */
1174 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG2, &prev, 1); /* read ctrl config */
1175 if (res != 0) /* check result */
1176 {
1177 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1178 (void)handle->iic_deinit(); /* iic deinit */
1179
1180 return 5; /* return error */
1181 }
1182 if ((prev & (1 << 4)) == 0) /* check the flag */
1183 {
1184 done = 1; /* set done */
1185
1186 break; /* break */
1187 }
1188 timeout--; /* timeout-- */
1189 }
1190 if (done == 0) /* check done */
1191 {
1192 handle->debug_print("mag3110: reset timeout.\n"); /* reset timeout */
1193 (void)handle->iic_deinit(); /* iic deinit */
1194
1195 return 5; /* return error */
1196 }
1197 handle->inited = 1; /* flag finish initialization */
1198
1199 return 0; /* success return 0 */
1200}
1201
1214{
1215 uint8_t res;
1216 uint8_t prev;
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
1227 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
1228 if (res != 0) /* check result */
1229 {
1230 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1231
1232 return 4; /* return error */
1233 }
1234 prev &= ~(1 << 0); /* clear settings */
1235 res = a_mag3110_iic_write(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* write ctrl config */
1236 if (res != 0) /* check result */
1237 {
1238 handle->debug_print("mag3110: write ctrl failed.\n"); /* write ctrl failed */
1239
1240 return 4; /* return error */
1241 }
1242 if (handle->iic_deinit() != 0) /* iic deinit */
1243 {
1244 handle->debug_print("mag3110: iic deinit failed.\n"); /* return error */
1245
1246 return 1; /* iic deinit failed */
1247 }
1248 handle->inited = 0; /* flag close */
1249
1250 return 0; /* success return 0 */
1251}
1252
1266uint8_t mag3110_read(mag3110_handle_t *handle, int16_t raw[3], float ut[3])
1267{
1268 uint8_t res;
1269 uint8_t prev;
1270 uint8_t status;
1271 uint8_t buf[6];
1272 uint16_t timeout = 1000;
1273
1274 if (handle == NULL) /* check handle */
1275 {
1276 return 2; /* return error */
1277 }
1278 if (handle->inited != 1) /* check handle initialization */
1279 {
1280 return 3; /* return error */
1281 }
1282
1283 res = a_mag3110_iic_read(handle, MAG3110_REG_CTRL_REG1, &prev, 1); /* read ctrl config */
1284 if (res != 0) /* check result */
1285 {
1286 handle->debug_print("mag3110: read ctrl failed.\n"); /* read ctrl failed */
1287
1288 return 1; /* return error */
1289 }
1290 if ((prev & (1 << 1)) != 0) /* check trigger mode */
1291 {
1292 while (timeout != 0) /* check timeout */
1293 {
1294 handle->delay_ms(1); /* delay 1ms */
1295 timeout--; /* timeout-- */
1296 res = a_mag3110_iic_read(handle, MAG3110_REG_DR_STATUS, &status, 1); /* read status config */
1297 if (res != 0) /* check result */
1298 {
1299 handle->debug_print("mag3110: read status failed.\n"); /* read status failed */
1300
1301 return 1; /* return error */
1302 }
1303 if ((status & (1 << 3)) != 0) /* check date ready */
1304 {
1305 break; /* break */
1306 }
1307 }
1308 if (timeout == 0) /* check timeout */
1309 {
1310 handle->debug_print("mag3110: read timeout.\n"); /* read timeout */
1311
1312 return 4; /* return error */
1313 }
1314 }
1315 if ((prev & (1 << 2)) != 0) /* fast read */
1316 {
1317 res = a_mag3110_iic_read(handle, MAG3110_REG_OUT_X_MSB, buf, 3); /* read data config */
1318 if (res != 0) /* check result */
1319 {
1320 handle->debug_print("mag3110: read data failed.\n"); /* read data failed */
1321
1322 return 1; /* return error */
1323 }
1324 raw[0] = (int16_t)(((uint16_t)buf[0] << 8) | 0x00); /* set x raw */
1325 raw[1] = (int16_t)(((uint16_t)buf[1] << 8) | 0x00); /* set y raw */
1326 raw[2] = (int16_t)(((uint16_t)buf[2] << 8) | 0x00); /* set z raw */
1327 ut[0] = (float)(raw[0]) * 0.1f; /* calculate x */
1328 ut[1] = (float)(raw[1]) * 0.1f; /* calculate y */
1329 ut[2] = (float)(raw[2]) * 0.1f; /* calculate z */
1330 }
1331 else /* full */
1332 {
1333 res = a_mag3110_iic_read(handle, MAG3110_REG_OUT_X_MSB, buf, 6); /* read data config */
1334 if (res != 0) /* check result */
1335 {
1336 handle->debug_print("mag3110: read data failed.\n"); /* read data failed */
1337
1338 return 1; /* return error */
1339 }
1340 raw[0] = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* set x raw */
1341 raw[1] = (int16_t)(((uint16_t)buf[2] << 8) | buf[3]); /* set y raw */
1342 raw[2] = (int16_t)(((uint16_t)buf[4] << 8) | buf[5]); /* set z raw */
1343 ut[0] = (float)(raw[0]) * 0.1f; /* calculate x */
1344 ut[1] = (float)(raw[1]) * 0.1f; /* calculate y */
1345 ut[2] = (float)(raw[2]) * 0.1f; /* calculate z */
1346 }
1347
1348 return 0; /* success return 0 */
1349}
1350
1364uint8_t mag3110_set_reg(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1365{
1366 if (handle == NULL) /* check handle */
1367 {
1368 return 2; /* return error */
1369 }
1370 if (handle->inited != 1) /* check handle initialization */
1371 {
1372 return 3; /* return error */
1373 }
1374
1375 return a_mag3110_iic_write(handle, reg, buf, len); /* write data */
1376}
1377
1391uint8_t mag3110_get_reg(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1392{
1393 if (handle == NULL) /* check handle */
1394 {
1395 return 2; /* return error */
1396 }
1397 if (handle->inited != 1) /* check handle initialization */
1398 {
1399 return 3; /* return error */
1400 }
1401
1402 return a_mag3110_iic_read(handle, reg, buf, len); /* read data */
1403}
1404
1414{
1415 if (info == NULL) /* check handle */
1416 {
1417 return 2; /* return error */
1418 }
1419
1420 memset(info, 0, sizeof(mag3110_info_t)); /* initialize mag3110 info structure */
1421 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1422 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1423 strncpy(info->interface, "IIC", 8); /* copy interface name */
1424 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1425 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1426 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1427 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1428 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1429 info->driver_version = DRIVER_VERSION; /* set driver version */
1430
1431 return 0; /* success return 0 */
1432}
#define MAG3110_REG_OFF_X_MSB
#define MAG3110_REG_SYSMOD
#define MAX_CURRENT
#define MAG3110_REG_CTRL_REG2
#define MAG3110_REG_WHO_AM_I
#define MAG3110_REG_DR_STATUS
chip register definition
#define MAG3110_ADDRESS
iic address definition
#define SUPPLY_VOLTAGE_MAX
#define MAG3110_REG_CTRL_REG1
#define MAG3110_REG_OFF_Y_MSB
#define TEMPERATURE_MAX
#define MAG3110_REG_DIE_TEMP
#define MAG3110_REG_OUT_X_MSB
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define MAG3110_REG_OFF_Z_MSB
driver mag3110 header file
uint8_t mag3110_set_rate_over_sample(mag3110_handle_t *handle, mag3110_rate_over_sample_t rate_over_sample)
set rate over sample
uint8_t mag3110_info(mag3110_info_t *info)
get chip's information
uint8_t mag3110_get_data_ready_status(mag3110_handle_t *handle, uint8_t *status)
get data ready status
uint8_t mag3110_set_trigger_mode(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable trigger mode
uint8_t mag3110_get_offset_z(mag3110_handle_t *handle, uint16_t *offset)
get offset z
struct mag3110_handle_s mag3110_handle_t
mag3110 handle structure definition
uint8_t mag3110_get_trigger_mode(mag3110_handle_t *handle, mag3110_bool_t *enable)
get trigger mode status
uint8_t mag3110_set_mode(mag3110_handle_t *handle, mag3110_mode_t mode)
set mode
uint8_t mag3110_get_fast_read_mode(mag3110_handle_t *handle, mag3110_bool_t *enable)
get fast read mode status
uint8_t mag3110_set_disable_offset_correction(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable offset correction
mag3110_bool_t
mag3110 bool enumeration definition
uint8_t mag3110_deinit(mag3110_handle_t *handle)
close the chip
uint8_t mag3110_init(mag3110_handle_t *handle)
initialize the chip
uint8_t mag3110_set_offset_y(mag3110_handle_t *handle, uint16_t offset)
set offset y
uint8_t mag3110_set_offset_x(mag3110_handle_t *handle, uint16_t offset)
set offset x
struct mag3110_info_s mag3110_info_t
mag3110 information structure definition
uint8_t mag3110_get_offset_y(mag3110_handle_t *handle, uint16_t *offset)
get offset y
uint8_t mag3110_get_rate_over_sample(mag3110_handle_t *handle, mag3110_rate_over_sample_t *rate_over_sample)
get rate over sample
uint8_t mag3110_get_offset_x(mag3110_handle_t *handle, uint16_t *offset)
get offset x
uint8_t mag3110_get_mode_status(mag3110_handle_t *handle, mag3110_mode_status_t *mode)
get mode status
mag3110_rate_over_sample_t
mag3110 rate over sample enumeration definition
uint8_t mag3110_set_automatic_magnetic_sensor_reset(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable automatic magnetic sensor reset
uint8_t mag3110_read(mag3110_handle_t *handle, int16_t raw[3], float ut[3])
read data
uint8_t mag3110_set_fast_read_mode(mag3110_handle_t *handle, mag3110_bool_t enable)
enable or disable fast read mode
uint8_t mag3110_offset_convert_to_register(mag3110_handle_t *handle, float ut, uint16_t *reg)
convert the offset to the register raw data
uint8_t mag3110_offset_convert_to_data(mag3110_handle_t *handle, uint16_t reg, float *ut)
convert the register raw data to the offset threshold
uint8_t mag3110_get_mode(mag3110_handle_t *handle, mag3110_mode_t *mode)
get mode
uint8_t mag3110_reset(mag3110_handle_t *handle)
reset
uint8_t mag3110_get_disable_offset_correction(mag3110_handle_t *handle, mag3110_bool_t *enable)
get offset correction status
uint8_t mag3110_read_die_temperature(mag3110_handle_t *handle, int8_t *raw, float *degree)
read die temperature
uint8_t mag3110_set_offset_z(mag3110_handle_t *handle, uint16_t offset)
set offset z
mag3110_mode_status_t
mag3110 mode status enumeration definition
mag3110_mode_t
mag3110 mode enumeration definition
uint8_t mag3110_get_reg(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t mag3110_set_reg(mag3110_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
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]