LibDriver QMC5883L
Loading...
Searching...
No Matches
driver_qmc5883l.c
Go to the documentation of this file.
1
36
37#include "driver_qmc5883l.h"
38
42#define CHIP_NAME "QST QMC5883L"
43#define MANUFACTURER_NAME "QST"
44#define SUPPLY_VOLTAGE_MIN 2.16f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 2.6f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define QMC5883L_ADDRESS 0x1A
55
59#define QMC5883L_REG_X_LSB 0x00
60#define QMC5883L_REG_X_MSB 0x01
61#define QMC5883L_REG_Y_LSB 0x02
62#define QMC5883L_REG_Y_MSB 0x03
63#define QMC5883L_REG_Z_LSB 0x04
64#define QMC5883L_REG_Z_MSB 0x05
65#define QMC5883L_REG_STATUS 0x06
66#define QMC5883L_REG_TEMP_LSB 0x07
67#define QMC5883L_REG_TEMP_MSB 0x08
68#define QMC5883L_REG_CONTROL1 0x09
69#define QMC5883L_REG_CONTROL2 0x0A
70#define QMC5883L_REG_PERIOD 0x0B
71#define QMC5883L_REG_ID 0x0D
72
86{
87 uint8_t res;
88 uint8_t prev;
89 uint8_t id;
90
91 if (handle == NULL) /* check handle */
92 {
93 return 2; /* return error */
94 }
95 if (handle->debug_print == NULL) /* check debug_print */
96 {
97 return 3; /* return error */
98 }
99 if (handle->iic_init == NULL) /* check iic_init */
100 {
101 handle->debug_print("qmc5883l: iic_init is null.\n"); /* iic_init is null */
102
103 return 3; /* return error */
104 }
105 if (handle->iic_deinit == NULL) /* check iic_deinit */
106 {
107 handle->debug_print("qmc5883l: iic_deinit is null.\n"); /* iic_deinit is null */
108
109 return 3; /* return error */
110 }
111 if (handle->iic_read == NULL) /* check iic_read */
112 {
113 handle->debug_print("qmc5883l: iic_read is null.\n"); /* iic_read is null */
114
115 return 3; /* return error */
116 }
117 if (handle->iic_write == NULL) /* check iic_write */
118 {
119 handle->debug_print("qmc5883l: iic_write is null.\n"); /* iic_write is null */
120
121 return 3; /* return error */
122 }
123 if (handle->delay_ms == NULL) /* check delay_ms */
124 {
125 handle->debug_print("qmc5883l: delay_ms is null.\n"); /* delay_ms is null */
126
127 return 3; /* return error */
128 }
129
130 if (handle->iic_init() != 0) /* iic init */
131 {
132 handle->debug_print("qmc5883l: iic init failed.\n"); /* iic init failed */
133
134 return 1; /* return error */
135 }
136 if (handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_ID, (uint8_t *)&id, 1) != 0) /* read id failed */
137 {
138 handle->debug_print("qmc5883l: read failed.\n"); /* read failed */
139 (void)handle->iic_deinit(); /* iic deinit */
140
141 return 4; /* return error */
142 }
143 if (id != 0xFF) /* check id */
144 {
145 handle->debug_print("qmc5883l: id is invalid.\n"); /* id is invalid */
146 (void)handle->iic_deinit(); /* iic deinit */
147
148 return 4; /* return error */
149 }
150 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
151 if (res != 0) /* check result */
152 {
153 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
154 (void)handle->iic_deinit(); /* iic deinit */
155
156 return 5; /* return error */
157 }
158 prev &= ~(1 << 7); /* clear settings */
159 prev |= 1 << 7; /* set bool */
160 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* write control1 */
161 if (res != 0) /* check result */
162 {
163 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
164 (void)handle->iic_deinit(); /* iic deinit */
165
166 return 5; /* return error */
167 }
168 handle->delay_ms(100); /* delay 100ms */
169 handle->inited = 1; /* flag finish initialization */
170
171 return 0; /* success return 0 */
172}
173
186{
187 uint8_t res;
188 uint8_t prev;
189
190 if (handle == NULL) /* check handle */
191 {
192 return 2; /* return error */
193 }
194 if (handle->inited != 1) /* check handle initialization */
195 {
196 return 3; /* return error */
197 }
198
199 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
200 if (res != 0) /* check result */
201 {
202 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
203
204 return 4; /* return error */
205 }
206 prev &= ~(1 << 7); /* clear settings */
207 prev |= 1 << 7; /* set bool */
208 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* write control1 */
209 if (res != 0) /* check result */
210 {
211 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
212
213 return 4; /* return error */
214 }
215 handle->delay_ms(10); /* delay 10ms */
216 if (handle->iic_deinit() != 0) /* iic deinit */
217 {
218 handle->debug_print("qmc5883l: iic deinit failed.\n"); /* return error */
219
220 return 1; /* iic deinit failed */
221 }
222 handle->inited = 0; /* flag close */
223
224 return 0; /* success return 0 */
225}
226
238uint8_t qmc5883l_get_status(qmc5883l_handle_t *handle, uint8_t *status)
239{
240 uint8_t res;
241
242 if (handle == NULL) /* check handle */
243 {
244 return 2; /* return error */
245 }
246 if (handle->inited != 1) /* check handle initialization */
247 {
248 return 3; /* return error */
249 }
250
251 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_STATUS, (uint8_t *)status, 1); /* read status config */
252 if (res != 0) /* check result */
253 {
254 handle->debug_print("qmc5883l: read status failed.\n"); /* read status failed */
255
256 return 1; /* return error */
257 }
258
259 return 0; /* success return 0 */
260}
261
276uint8_t qmc5883l_read_temperature(qmc5883l_handle_t *handle, int16_t *raw, float *deg)
277{
278 uint8_t res;
279 uint8_t buf[2];
280
281 if (handle == NULL) /* check handle */
282 {
283 return 2; /* return error */
284 }
285 if (handle->inited != 1) /* check handle initialization */
286 {
287 return 3; /* return error */
288 }
289
290 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_TEMP_LSB, buf, 2); /* read temp config */
291 if (res != 0) /* check result */
292 {
293 handle->debug_print("qmc5883l: read temp failed.\n"); /* return temp failed */
294
295 return 1; /* return error */
296 }
297 *raw = (int16_t)(((uint16_t)(buf[1]) << 8) | buf[0]); /* combine data */
298 *deg = (float)(*raw) / 100.0f; /* convert data */
299
300 return 0; /* success return 0 */
301}
302
315{
316 uint8_t res;
317 uint8_t prev;
318
319 if (handle == NULL) /* check handle */
320 {
321 return 2; /* return error */
322 }
323 if (handle->inited != 1) /* check handle initialization */
324 {
325 return 3; /* return error */
326 }
327
328 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
329 if (res != 0) /* check result */
330 {
331 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
332
333 return 1; /* return error */
334 }
335 prev &= ~(3 << 0); /* clear settings */
336 prev |= mode << 0; /* set mode */
337 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* write control1 */
338 if (res != 0) /* check result */
339 {
340 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
341
342 return 1; /* return error */
343 }
344
345 return 0; /* success return 0 */
346}
347
360{
361 uint8_t res;
362 uint8_t prev;
363
364 if (handle == NULL) /* check handle */
365 {
366 return 2; /* return error */
367 }
368 if (handle->inited != 1) /* check handle initialization */
369 {
370 return 3; /* return error */
371 }
372
373 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
374 if (res != 0) /* check result */
375 {
376 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
377
378 return 1; /* return error */
379 }
380 *mode = (qmc5883l_mode_t)((prev >> 0) & 0x3); /* get mode */
381
382 return 0; /* success return 0 */
383}
384
397{
398 uint8_t res;
399 uint8_t prev;
400
401 if (handle == NULL) /* check handle */
402 {
403 return 2; /* return error */
404 }
405 if (handle->inited != 1) /* check handle initialization */
406 {
407 return 3; /* return error */
408 }
409
410 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
411 if (res != 0) /* check result */
412 {
413 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
414
415 return 1; /* return error */
416 }
417 prev &= ~(3 << 2); /* clear settings */
418 prev |= rate << 2; /* set rate */
419 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* write control1 */
420 if (res != 0) /* check result */
421 {
422 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
423
424 return 1; /* return error */
425 }
426
427 return 0; /* success return 0 */
428}
429
442{
443 uint8_t res;
444 uint8_t prev;
445
446 if (handle == NULL) /* check handle */
447 {
448 return 2; /* return error */
449 }
450 if (handle->inited != 1) /* check handle initialization */
451 {
452 return 3; /* return error */
453 }
454
455 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
456 if (res != 0) /* check result */
457 {
458 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
459
460 return 1; /* return error */
461 }
462 *rate = (qmc5883l_output_rate_t)((prev >> 2) & 0x3); /* get rate */
463
464 return 0; /* success return 0 */
465}
466
479{
480 uint8_t res;
481 uint8_t prev;
482
483 if (handle == NULL) /* check handle */
484 {
485 return 2; /* return error */
486 }
487 if (handle->inited != 1) /* check handle initialization */
488 {
489 return 3; /* return error */
490 }
491
492 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
493 if (res != 0) /* check result */
494 {
495 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
496
497 return 1; /* return error */
498 }
499 prev &= ~(3 << 4); /* clear settings */
500 prev |= scale << 4; /* set scale */
501 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* write control1 */
502 if (res != 0) /* check result */
503 {
504 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
505
506 return 1; /* return error */
507 }
508
509 return 0; /* success return 0 */
510}
511
524{
525 uint8_t res;
526 uint8_t prev;
527
528 if (handle == NULL) /* check handle */
529 {
530 return 2; /* return error */
531 }
532 if (handle->inited != 1) /* check handle initialization */
533 {
534 return 3; /* return error */
535 }
536
537 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
538 if (res != 0) /* check result */
539 {
540 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
541
542 return 1; /* return error */
543 }
544 *scale = (qmc5883l_full_scale_t)((prev >> 4) & 0x3); /* get scale */
545
546 return 0; /* success return 0 */
547}
548
561{
562 uint8_t res;
563 uint8_t prev;
564
565 if (handle == NULL) /* check handle */
566 {
567 return 2; /* return error */
568 }
569 if (handle->inited != 1) /* check handle initialization */
570 {
571 return 3; /* return error */
572 }
573
574 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
575 if (res != 0) /* check result */
576 {
577 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
578
579 return 1; /* return error */
580 }
581 prev &= ~(3 << 6); /* clear settings */
582 prev |= sample << 6; /* set sample */
583 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* write control1 */
584 if (res != 0) /* check result */
585 {
586 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
587
588 return 1; /* return error */
589 }
590
591 return 0; /* success return 0 */
592}
593
606{
607 uint8_t res;
608 uint8_t prev;
609
610 if (handle == NULL) /* check handle */
611 {
612 return 2; /* return error */
613 }
614 if (handle->inited != 1) /* check handle initialization */
615 {
616 return 3; /* return error */
617 }
618
619 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
620 if (res != 0) /* check result */
621 {
622 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
623
624 return 1; /* return error */
625 }
626 *sample = (qmc5883l_over_sample_t)((prev >> 6) & 0x3); /* get sample */
627
628 return 0; /* success return 0 */
629}
630
643{
644 uint8_t res;
645 uint8_t prev;
646
647 if (handle == NULL) /* check handle */
648 {
649 return 2; /* return error */
650 }
651 if (handle->inited != 1) /* check handle initialization */
652 {
653 return 3; /* return error */
654 }
655
656 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
657 if (res != 0) /* check result */
658 {
659 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
660
661 return 1; /* return error */
662 }
663 prev &= ~(1 << 0); /* clear settings */
664 prev |= (!enable) << 0; /* set bool */
665 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* write control1 */
666 if (res != 0) /* check result */
667 {
668 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
669
670 return 1; /* return error */
671 }
672
673 return 0; /* success return 0 */
674}
675
688{
689 uint8_t res;
690 uint8_t prev;
691
692 if (handle == NULL) /* check handle */
693 {
694 return 2; /* return error */
695 }
696 if (handle->inited != 1) /* check handle initialization */
697 {
698 return 3; /* return error */
699 }
700
701 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
702 if (res != 0) /* check result */
703 {
704 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
705
706 return 1; /* return error */
707 }
708 *enable = (qmc5883l_bool_t)(!(prev & 0x01)); /* get the bool */
709
710 return 0; /* success return 0 */
711}
712
725{
726 uint8_t res;
727 uint8_t prev;
728
729 if (handle == NULL) /* check handle */
730 {
731 return 2; /* return error */
732 }
733 if (handle->inited != 1) /* check handle initialization */
734 {
735 return 3; /* return error */
736 }
737
738 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
739 if (res != 0) /* check result */
740 {
741 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
742
743 return 1; /* return error */
744 }
745 prev &= ~(1 << 6); /* clear settings */
746 prev |= enable << 6; /* set bool */
747 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* write control1 */
748 if (res != 0) /* check result */
749 {
750 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
751
752 return 1; /* return error */
753 }
754
755 return 0; /* success return 0 */
756}
757
770{
771 uint8_t res;
772 uint8_t prev;
773
774 if (handle == NULL) /* check handle */
775 {
776 return 2; /* return error */
777 }
778 if (handle->inited != 1) /* check handle initialization */
779 {
780 return 3; /* return error */
781 }
782
783 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
784 if (res != 0) /* check result */
785 {
786 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
787
788 return 1; /* return error */
789 }
790 *enable = (qmc5883l_bool_t)((prev >> 6) & 0x01); /* get the bool */
791
792 return 0; /* success return 0 */
793}
794
806{
807 uint8_t res;
808 uint8_t prev;
809
810 if (handle == NULL) /* check handle */
811 {
812 return 2; /* return error */
813 }
814 if (handle->inited != 1) /* check handle initialization */
815 {
816 return 3; /* return error */
817 }
818
819 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* read control1 */
820 if (res != 0) /* check result */
821 {
822 handle->debug_print("qmc5883l: read control1.\n"); /* read control1 failed */
823
824 return 1; /* return error */
825 }
826 prev &= ~(1 << 7); /* clear settings */
827 prev |= 1 << 7; /* set bool */
828 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL2, (uint8_t *)&prev, 1); /* write control1 */
829 if (res != 0) /* check result */
830 {
831 handle->debug_print("qmc5883l: write control1.\n"); /* write control1 failed */
832
833 return 1; /* return error */
834 }
835 handle->delay_ms(100); /* delay 100ms */
836
837 return 0; /* success return 0 */
838}
839
851uint8_t qmc5883l_set_period(qmc5883l_handle_t *handle, uint8_t fbr)
852{
853 uint8_t res;
854 uint8_t prev;
855
856 if (handle == NULL) /* check handle */
857 {
858 return 2; /* return error */
859 }
860 if (handle->inited != 1) /* check handle initialization */
861 {
862 return 3; /* return error */
863 }
864
865 prev = fbr; /* set fbr */
866 res = handle->iic_write(QMC5883L_ADDRESS, QMC5883L_REG_PERIOD, (uint8_t *)&prev, 1); /* write period */
867 if (res != 0) /* check result */
868 {
869 handle->debug_print("qmc5883l: write period.\n"); /* write period failed */
870
871 return 1; /* return error */
872 }
873
874 return 0; /* success return 0 */
875}
876
888uint8_t qmc5883l_get_period(qmc5883l_handle_t *handle, uint8_t *fbr)
889{
890 uint8_t res;
891
892 if (handle == NULL) /* check handle */
893 {
894 return 2; /* return error */
895 }
896 if (handle->inited != 1) /* check handle initialization */
897 {
898 return 3; /* return error */
899 }
900
901 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_PERIOD, (uint8_t *)fbr, 1); /* read period */
902 if (res != 0) /* check result */
903 {
904 handle->debug_print("qmc5883l: read period.\n"); /* read period failed */
905
906 return 1; /* return error */
907 }
908
909 return 0; /* success return 0 */
910}
911
924uint8_t qmc5883l_read(qmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
925{
926 uint8_t res;
927 uint8_t prev;
928 uint8_t status;
929 uint16_t num = 5000;
930 uint8_t buf[6];
931 float resolution;
932
933 if (handle == NULL) /* check handle */
934 {
935 return 2; /* return error */
936 }
937 if (handle->inited != 1) /* check handle initialization */
938 {
939 return 3; /* return error */
940 }
941
942 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_CONTROL1, (uint8_t *)&prev, 1); /* read control1 */
943 if (res != 0) /* check result */
944 {
945 handle->debug_print("qmc5883l: read control1 failed.\n"); /* read control1 failed */
946
947 return 1; /* return error */
948 }
949 prev = prev >> 4; /* set gain */
950 switch (prev) /* choose resolution */
951 {
952 case 0x00 :
953 {
954 resolution = 1000.0f / 12000.0f; /* set resolution 2gauss */
955
956 break; /* break */
957 }
958 case 0x01 :
959 {
960 resolution = 1000.0f / 3000.0f; /* set resolution 8gauss */
961
962 break; /* break */
963 }
964 default : /* unknown code */
965 {
966 resolution = 0.00f; /* set resolution 0.00 */
967
968 break; /* break */
969 }
970 }
971 while (num != 0) /* check num */
972 {
973 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_STATUS, (uint8_t *)&status, 1); /* read status register */
974 if (res != 0) /* check result */
975 {
976 handle->debug_print("qmc5883l: read failed.\n"); /* read status failed */
977
978 return 1; /* return error */
979 }
980 if ((status & 0x01) != 0) /* check status */
981 {
982 break; /* break loop */
983 }
984 handle->delay_ms(10); /* check 10 ms */
985 num--;
986 if (num == 0) /* if timeout */
987 {
988 handle->debug_print("qmc5883l: ready bit not be set.\n"); /* timeout */
989
990 return 1; /* return error */
991 }
992 }
993 res = handle->iic_read(QMC5883L_ADDRESS, QMC5883L_REG_X_LSB, (uint8_t *)buf, 6); /* read raw data */
994 if (res != 0) /* check result */
995 {
996 handle->debug_print("qmc5883l: read data failed.\n"); /* read data failed */
997
998 return 1; /* return error */
999 }
1000 raw[0] = (int16_t)(((uint16_t)buf[1] << 8) | buf[0]); /* get x raw */
1001 raw[1] = (int16_t)(((uint16_t)buf[3] << 8) | buf[2]); /* get y raw */
1002 raw[2] = (int16_t)(((uint16_t)buf[5] << 8) | buf[4]); /* get z raw */
1003 m_gauss[0] = (float)(raw[0]) * resolution; /* calculate x */
1004 m_gauss[1] = (float)(raw[1]) * resolution; /* calculate y */
1005 m_gauss[2] = (float)(raw[2]) * resolution; /* calculate z */
1006
1007 return 0; /* success return 0 */
1008}
1009
1023uint8_t qmc5883l_set_reg(qmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1024{
1025 if (handle == NULL) /* check handle */
1026 {
1027 return 2; /* return error */
1028 }
1029 if (handle->inited != 1) /* check handle initialization */
1030 {
1031 return 3; /* return error */
1032 }
1033
1034 return handle->iic_write(QMC5883L_ADDRESS, reg, buf, len); /* write data */
1035}
1036
1050uint8_t qmc5883l_get_reg(qmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1051{
1052 if (handle == NULL) /* check handle */
1053 {
1054 return 2; /* return error */
1055 }
1056 if (handle->inited != 1) /* check handle initialization */
1057 {
1058 return 3; /* return error */
1059 }
1060
1061 return handle->iic_read(QMC5883L_ADDRESS, reg, buf, len); /* read data */
1062}
1063
1073{
1074 if (info == NULL) /* check handle */
1075 {
1076 return 2; /* return error */
1077 }
1078
1079 memset(info, 0, sizeof(qmc5883l_info_t)); /* initialize qmc5883l info structure */
1080 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1081 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1082 strncpy(info->interface, "IIC", 8); /* copy interface name */
1083 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1084 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1085 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1086 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1087 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1088 info->driver_version = DRIVER_VERSION; /* set driver version */
1089
1090 return 0; /* success return 0 */
1091}
#define QMC5883L_REG_ID
#define MAX_CURRENT
#define QMC5883L_REG_PERIOD
#define SUPPLY_VOLTAGE_MAX
#define QMC5883L_REG_X_LSB
chip register definition
#define QMC5883L_REG_STATUS
#define TEMPERATURE_MAX
#define QMC5883L_REG_CONTROL1
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define QMC5883L_REG_TEMP_LSB
#define QMC5883L_REG_CONTROL2
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define QMC5883L_ADDRESS
iic address definition
driver qmc5883l header file
uint8_t qmc5883l_set_full_scale(qmc5883l_handle_t *handle, qmc5883l_full_scale_t scale)
set the full scale
uint8_t qmc5883l_read(qmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
read data
uint8_t qmc5883l_get_pointer_roll_over(qmc5883l_handle_t *handle, qmc5883l_bool_t *enable)
get pointer roll over status
uint8_t qmc5883l_get_over_sample(qmc5883l_handle_t *handle, qmc5883l_over_sample_t *sample)
get the over sample
uint8_t qmc5883l_get_full_scale(qmc5883l_handle_t *handle, qmc5883l_full_scale_t *scale)
get the full scale
uint8_t qmc5883l_get_period(qmc5883l_handle_t *handle, uint8_t *fbr)
get period
uint8_t qmc5883l_set_over_sample(qmc5883l_handle_t *handle, qmc5883l_over_sample_t sample)
set the over sample
uint8_t qmc5883l_get_interrupt(qmc5883l_handle_t *handle, qmc5883l_bool_t *enable)
get interrupt status
uint8_t qmc5883l_init(qmc5883l_handle_t *handle)
initialize the chip
qmc5883l_full_scale_t
qmc5883l full scale enumeration definition
uint8_t qmc5883l_set_pointer_roll_over(qmc5883l_handle_t *handle, qmc5883l_bool_t enable)
enable or disable pointer roll over
struct qmc5883l_handle_s qmc5883l_handle_t
qmc5883l handle structure definition
uint8_t qmc5883l_get_status(qmc5883l_handle_t *handle, uint8_t *status)
get status
uint8_t qmc5883l_set_interrupt(qmc5883l_handle_t *handle, qmc5883l_bool_t enable)
enable or disable interrupt
uint8_t qmc5883l_info(qmc5883l_info_t *info)
get chip's information
uint8_t qmc5883l_set_period(qmc5883l_handle_t *handle, uint8_t fbr)
set period
uint8_t qmc5883l_get_output_rate(qmc5883l_handle_t *handle, qmc5883l_output_rate_t *rate)
get the output rate
uint8_t qmc5883l_read_temperature(qmc5883l_handle_t *handle, int16_t *raw, float *deg)
read temperature
uint8_t qmc5883l_deinit(qmc5883l_handle_t *handle)
close the chip
uint8_t qmc5883l_set_output_rate(qmc5883l_handle_t *handle, qmc5883l_output_rate_t rate)
set the output rate
qmc5883l_bool_t
qmc5883l bool enumeration definition
struct qmc5883l_info_s qmc5883l_info_t
qmc5883l information structure definition
qmc5883l_output_rate_t
qmc5883l output rate enumeration definition
qmc5883l_mode_t
qmc5883l mode enumeration definition
qmc5883l_over_sample_t
qmc5883l over sample enumeration definition
uint8_t qmc5883l_soft_reset(qmc5883l_handle_t *handle)
soft reset
uint8_t qmc5883l_get_mode(qmc5883l_handle_t *handle, qmc5883l_mode_t *mode)
get the chip mode
uint8_t qmc5883l_set_mode(qmc5883l_handle_t *handle, qmc5883l_mode_t mode)
set the chip mode
uint8_t qmc5883l_set_reg(qmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t qmc5883l_get_reg(qmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get 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)
char manufacturer_name[32]