LibDriver HMC5883L
Loading...
Searching...
No Matches
driver_hmc5883l.c
Go to the documentation of this file.
1
37
38#include "driver_hmc5883l.h"
39
43#define CHIP_NAME "Honeywell HMC5883L"
44#define MANUFACTURER_NAME "Honeywell"
45#define SUPPLY_VOLTAGE_MIN 2.16f
46#define SUPPLY_VOLTAGE_MAX 3.6f
47#define MAX_CURRENT 0.1f
48#define TEMPERATURE_MIN -30.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define HMC5883L_ADDRESS 0x3C
56
60#define HMC5883L_REG_CRA 0x00
61#define HMC5883L_REG_CRB 0x01
62#define HMC5883L_REG_MODE 0x02
63#define HMC5883L_REG_OUTXM 0x03
64#define HMC5883L_REG_OUTXL 0x04
65#define HMC5883L_REG_OUTYM 0x07
66#define HMC5883L_REG_OUTYL 0x08
67#define HMC5883L_REG_OUTZM 0x05
68#define HMC5883L_REG_OUTZL 0x06
69#define HMC5883L_REG_STATUS 0x09
70#define HMC5883L_REG_IDA 0x0A
71#define HMC5883L_REG_IDB 0x0B
72#define HMC5883L_REG_IDC 0x0C
73
82static uint8_t a_hmc5883l_test(hmc5883l_handle_t *handle)
83{
84 uint8_t reg, times, status;
85 uint8_t buf[6];
86 int16_t data;
87
88 reg = 0x71; /* set 0x71 */
89 if (handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&reg, 1) != 0) /* write cra reg */
90 {
91 handle->debug_print("hmc5883l: write failed.\n"); /* write cra failed */
92
93 return 1; /* return error */
94 }
95 reg = 0xA0; /* set 0xA0 */
96 if (handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&reg, 1) != 0) /* write crb reg */
97 {
98 handle->debug_print("hmc5883l: write failed.\n"); /* write crb failed */
99
100 return 1; /* return error */
101 }
102 reg = 0x00; /* set 0x00 */
103 if (handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&reg, 1) != 0) /* write mode reg */
104 {
105 handle->debug_print("hmc5883l: write failed.\n"); /* write mode failed */
106
107 return 1; /* return error */
108 }
109 handle->delay_ms(100); /* wait 100 ms */
110 times = 15; /* set try 15 times */
111
112 while (times != 0) /* check the times */
113 {
115 (uint8_t *)&status, 1) != 0) /* read status */
116 {
117 handle->debug_print("hmc5883l: read failed.\n"); /* read status failed */
118
119 return 1; /* return error */
120 }
121 status = status & 0x01; /* get status bit */
122 if (status == 0) /* check status */
123 {
124 times--; /* times-- */
125 }
126 else
127 {
128 break; /* break */
129 }
130 }
131 if (times == 0) /* if timeout */
132 {
133 handle->debug_print("hmc5883l: check failed.\n"); /* check failed */
134
135 return 1; /* return error */
136 }
137 if (handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_OUTXM, (uint8_t *)buf, 6) != 0) /* read raw data */
138 {
139 handle->debug_print("hmc5883l: read failed.\n"); /* read failed */
140
141 return 1; /* return error */
142 }
143 data = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get x data */
144 if ((data <= 243) || (data > 575)) /* check x data */
145 {
146 handle->debug_print("hmc5883l: x check failed.\n"); /* x data check failed */
147
148 return 1; /* return error */
149 }
150 data = (int16_t)(((uint16_t)buf[2] << 8) | buf[3]); /* get y data */
151 if ((data <= 243) || (data > 575)) /* check y data */
152 {
153 handle->debug_print("hmc5883l: y check failed.\n"); /* y data check failed */
154
155 return 1; /* return error */
156 }
157 data = (int16_t)(((uint16_t)buf[4] << 8) | buf[5]); /* get z data */
158 if ((data <= 243) || (data > 575)) /* check z data */
159 {
160 handle->debug_print("hmc5883l: z check failed.\n"); /* z data check failed */
161
162 return 1; /* return error */
163 }
164
165 return 0; /* success return 0 */
166}
167
179{
180 uint8_t id;
181
182 if (handle == NULL) /* check handle */
183 {
184 return 2; /* return error */
185 }
186 if (handle->debug_print == NULL) /* check debug_print */
187 {
188 return 3; /* return error */
189 }
190 if (handle->iic_init == NULL) /* check iic_init */
191 {
192 handle->debug_print("hmc5883l: iic_init is null.\n"); /* iic_init is null */
193
194 return 3; /* return error */
195 }
196 if (handle->iic_deinit == NULL) /* check iic_deinit */
197 {
198 handle->debug_print("hmc5883l: iic_deinit is null.\n"); /* iic_deinit is null */
199
200 return 3; /* return error */
201 }
202 if (handle->iic_read == NULL) /* check iic_read */
203 {
204 handle->debug_print("hmc5883l: iic_read is null.\n"); /* iic_read is null */
205
206 return 3; /* return error */
207 }
208 if (handle->iic_write == NULL) /* check iic_write */
209 {
210 handle->debug_print("hmc5883l: iic_write is null.\n"); /* iic_write is null */
211
212 return 3; /* return error */
213 }
214 if (handle->delay_ms == NULL) /* check delay_ms */
215 {
216 handle->debug_print("hmc5883l: delay_ms is null.\n"); /* delay_ms is null */
217
218 return 3; /* return error */
219 }
220
221 if (handle->iic_init() != 0) /* iic init */
222 {
223 handle->debug_print("hmc5883l: iic init failed.\n"); /* iic init failed */
224
225 return 1; /* return error */
226 }
227 if (handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_IDA, (uint8_t *)&id, 1) != 0) /* read ida failed */
228 {
229 handle->debug_print("hmc5883l: read failed.\n"); /* read failed */
230 (void)handle->iic_deinit(); /* iic deinit */
231
232 return 1; /* return error */
233 }
234 if (id != 'H') /* check id a */
235 {
236 handle->debug_print("hmc5883l: first id invalid.\n"); /* first id is invalid */
237 (void)handle->iic_deinit(); /* iic deinit */
238
239 return 1; /* return error */
240 }
241 if (handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_IDB, (uint8_t *)&id, 1) != 0) /* read idb failed */
242 {
243 handle->debug_print("hmc5883l: read failed.\n"); /* read failed */
244 (void)handle->iic_deinit(); /* iic deinit */
245
246 return 1; /* return error */
247 }
248 if (id != '4') /* check id b */
249 {
250 handle->debug_print("hmc5883l: second id invalid.\n"); /* second id is invalid */
251 (void)handle->iic_deinit(); /* iid deinit */
252
253 return 1; /* return error */
254 }
255 if (handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_IDC, (uint8_t *)&id, 1) != 0) /* read idc */
256 {
257 handle->debug_print("hmc5883l: read failed.\n"); /* read idc failed */
258 (void)handle->iic_deinit(); /* iic deinit */
259
260 return 1; /* return error */
261 }
262 if (id != '3') /* check id c */
263 {
264 handle->debug_print("hmc5883l: third id invalid.\n"); /* third id is invalid */
265 (void)handle->iic_deinit(); /* iic deinit */
266
267 return 1; /* return error */
268 }
269 if (a_hmc5883l_test(handle) != 0) /* run test */
270 {
271 handle->debug_print("hmc5883l: test is not passed.\n"); /* run test failed */
272 (void)handle->iic_deinit(); /* iic deinit */
273
274 return 1; /* return error */
275 }
276 handle->inited = 1; /* flag finish initialization */
277
278 return 0; /* success return 0 */
279}
280
293{
294 uint8_t res, prev;
295
296 if (handle == NULL) /* check handle */
297 {
298 return 2; /* return error */
299 }
300 if (handle->inited != 1) /* check handle initialization */
301 {
302 return 3; /* return error */
303 }
304
305 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode config */
306 if (res != 0) /* check result */
307 {
308 handle->debug_print("hmc5883l: read failed.\n"); /* read mode reg */
309
310 return 4; /* return error */
311 }
312 prev &= ~(0x7C); /* clear mode */
313 prev &= ~(0x03); /* clear config */
314 prev |= 0x02; /* set config */
315 res = handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write mode config */
316 if (res != 0) /* check result */
317 {
318 handle->debug_print("hmc5883l: write failed.\n"); /* write mode failed */
319
320 return 4; /* return error */
321 }
322 if (handle->iic_deinit() != 0) /* iic deinit */
323 {
324 handle->debug_print("hmc5883l: iic deinit failed.\n"); /* return error */
325
326 return 1; /* iic deinit failed */
327 }
328 handle->inited = 0; /* flag close */
329
330 return 0; /* success return 0 */
331}
332
345{
346 uint8_t res, prev;
347
348 if (handle == NULL) /* check handle */
349 {
350 return 2; /* return error */
351 }
352 if (handle->inited != 1) /* check handle initialization */
353 {
354 return 3; /* return error */
355 }
356
357 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
358 if (res != 0) /* check result */
359 {
360 handle->debug_print("hmc5883l: read failed.\n"); /* read cra failed */
361
362 return 1; /* return error */
363 }
364 prev &= ~(1<<7); /* clear cra 7 */
365 prev &= ~(3<<5); /* clear config */
366 prev |= average_sample << 5; /* set config */
367
368 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* write cra config */
369}
370
383{
384 uint8_t res, prev;
385
386 if (handle == NULL) /* check handle */
387 {
388 return 2; /* return error */
389 }
390 if (handle->inited != 1) /* check handle initialization */
391 {
392 return 3; /* return error */
393 }
394
395 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
396 if (res != 0) /* check result */
397 {
398 handle->debug_print("hmc5883l: read failed.\n"); /* return cra failed */
399
400 return 1; /* return error */
401 }
402 prev &= (3 << 5); /* get raw reg */
403 *average_sample = (hmc5883l_average_sample_t)(prev >> 5); /* get config */
404
405 return 0; /* success return 0 */
406}
407
420{
421 uint8_t res, prev;
422
423 if (handle == NULL) /* check handle */
424 {
425 return 2; /* return error */
426 }
427 if (handle->inited != 1) /* check handle initialization */
428 {
429 return 3; /* return error */
430 }
431
432 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
433 if (res != 0) /* check result */
434 {
435 handle->debug_print("hmc5883l: read failed.\n"); /* read cra failed */
436
437 return 1; /* return error */
438 }
439 prev &= ~(1 << 7); /* clear cra 7 */
440 prev &= ~(7 << 2); /* clear config */
441 prev |= data_rate << 2; /* set config */
442
443 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* write cra config */
444}
445
458{
459 uint8_t res, prev;
460
461 if (handle == NULL) /* check handle */
462 {
463 return 2; /* return error */
464 }
465 if (handle->inited != 1) /* check handle initialization */
466 {
467 return 3; /* return error */
468 }
469
470 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
471 if (res != 0) /* check result */
472 {
473 handle->debug_print("hmc5883l: read failed.\n"); /* read cra failed */
474
475 return 1; /* return error */
476 }
477 prev &= (7 << 2); /* get raw reg */
478 *data_rate = (hmc5883l_data_output_rate_t)(prev >> 2); /* get config */
479
480 return 0; /* success return 0 */
481}
482
495{
496 uint8_t res, prev;
497
498 if (handle == NULL) /* check handle */
499 {
500 return 2; /* return error */
501 }
502 if (handle->inited != 1) /* check handle initialization */
503 {
504 return 3; /* return error */
505 }
506
507 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
508 if (res != 0) /* check result */
509 {
510 handle->debug_print("hmc5883l: read failed.\n"); /* read cra failed */
511
512 return 1; /* return error */
513 }
514 prev &= ~(1 << 7); /* clear cra 7 */
515 prev &= ~(3 << 0); /* clear config */
516 prev |= mode << 0; /* set config */
517
518 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* write cra config */
519}
520
533{
534 uint8_t res, prev;
535
536 if (handle == NULL) /* check handle */
537 {
538 return 2; /* return error */
539 }
540 if (handle->inited != 1) /* check handle initialization */
541 {
542 return 3; /* return error */
543 }
544
545 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRA, (uint8_t *)&prev, 1); /* read cra config */
546 if (res != 0) /* check result */
547 {
548 handle->debug_print("hmc5883l: read failed.\n"); /* read cra failed */
549
550 return 1; /* return error */
551 }
552 prev &= 3 << 0; /* get raw reg */
553 *mode = (hmc5883l_mode_t)(prev >> 0); /* get config */
554
555 return 0; /* success return 0 */
556}
557
570{
571 uint8_t res, prev;
572
573 if (handle == NULL) /* check handle */
574 {
575 return 2; /* return error */
576 }
577 if (handle->inited != 1) /* check handle initialization */
578 {
579 return 3; /* return error */
580 }
581
582 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&prev, 1); /* read config */
583 if (res != 0) /* check result */
584 {
585 handle->debug_print("hmc5883l: read failed.\n"); /* read crb config failed */
586
587 return 1; /* return error */
588 }
589 prev &= ~(0x1F); /* clear crb */
590 prev &= ~(7 << 5); /* clear config */
591 prev |= gain << 5; /* set gain */
592
593 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&prev, 1); /* write config */
594}
595
608{
609 uint8_t res, prev;
610
611 if (handle == NULL) /* check handle */
612 {
613 return 2; /* return error */
614 }
615 if (handle->inited != 1) /* check handle initialization */
616 {
617 return 3; /* return error */
618 }
619
620 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&prev, 1); /* read config */
621 if (res != 0) /* check result */
622 {
623 handle->debug_print("hmc5883l: read failed.\n"); /* read crb config failed */
624
625 return 1; /* return error */
626 }
627 prev &= (7 << 5); /* get raw reg */
628 *gain = (hmc5883l_gain_t)(prev >> 5); /* get config */
629
630 return 0; /* success return 0 */
631}
632
644{
645 uint8_t res, 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(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode config */
657 if (res != 0) /* check result */
658 {
659 handle->debug_print("hmc5883l: read failed.\n"); /* read mode failed */
660
661 return 1; /* return error */
662 }
663 prev &= ~(0x7C); /* clear mode bit */
664 prev |= (1 << 7); /* enable high speed iic */
665
666 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write config */
667}
668
680{
681 uint8_t res, prev;
682
683 if (handle == NULL) /* check handle */
684 {
685 return 2; /* return error */
686 }
687 if (handle->inited != 1) /* check handle initialization */
688 {
689 return 3; /* return error */
690 }
691
692 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode config */
693 if (res != 0) /* check result */
694 {
695 handle->debug_print("hmc5883l: read failed.\n"); /* read mode failed */
696
697 return 1; /* return error */
698 }
699 prev &= ~(0x7C); /* clear mode bit */
700 prev &= ~(1 << 7); /* disable high speed iic */
701
702 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write config */
703}
704
717uint8_t hmc5883l_single_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
718{
719 uint8_t res, gain, status, prev;
720 uint16_t num = 5000;
721 uint8_t buf[6];
722 float resolution;
723
724 if (handle == NULL) /* check handle */
725 {
726 return 2; /* return error */
727 }
728 if (handle->inited != 1) /* check handle initialization */
729 {
730 return 3; /* return error */
731 }
732
733 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode register */
734 if (res != 0) /* check result */
735 {
736 handle->debug_print("hmc5883l: read failed.\n"); /* read mode failed */
737
738 return 1; /* return error */
739 }
740 prev &= ~(0x7C); /* clear mode bits */
741 prev &= ~(0x03); /* clear config */
742 prev |= 0x01; /* set config */
743 res = handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write mode register */
744 if (res != 0) /* check result */
745 {
746 handle->debug_print("hmc5883l: write failed.\n"); /* write mode failed */
747
748 return 1; /* return error */
749 }
750 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&gain, 1); /* read crb register */
751 if (res != 0) /* check result */
752 {
753 handle->debug_print("hmc5883l: read failed.\n"); /* read crb failed */
754
755 return 1; /* return error */
756 }
757 gain = gain >> 5; /* set gain */
758 switch (gain) /* choose resolution */
759 { /* check gain */
760 case 0x00 :
761 {
762 resolution = 0.73f; /* set resolution */
763
764 break; /* break */
765 }
766 case 0x01 :
767 {
768 resolution = 0.92f; /* set resolution */
769
770 break; /* break */
771 }
772 case 0x02 :
773 {
774 resolution = 1.22f; /* set resolution 1.22 */
775
776 break; /* break */
777 }
778 case 0x03 :
779 {
780 resolution = 1.52f; /* set resolution 1.52 */
781
782 break; /* break */
783 }
784 case 0x04 :
785 {
786 resolution = 2.27f; /* set resolution 2.27 */
787
788 break; /* break */
789 }
790 case 0x05 :
791 {
792 resolution = 2.56f; /* set resolution 2.56 */
793
794 break; /* break */
795 }
796 case 0x06 :
797 {
798 resolution = 3.03f; /* set resolution 3.03 */
799
800 break; /* break */
801 }
802 case 0x07 :
803 {
804 resolution = 4.35f; /* set resolution 4.35 */
805
806 break; /* break */
807 }
808 default : /* unknown code */
809 {
810 resolution = 0.00f; /* set resolution 0.00 */
811
812 break; /* break */
813 }
814 }
815 while (num != 0) /* check num */
816 {
817 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_STATUS, (uint8_t *)&status, 1); /* read status register */
818 if (res != 0) /* check result */
819 {
820 handle->debug_print("hmc5883l: read failed.\n"); /* read status failed */
821
822 return 1; /* return error */
823 }
824 if ((status & 0x01) != 0) /* check status */
825 {
826 break; /* break loop */
827 }
828 handle->delay_ms(10); /* check 10 ms */
829 num--; /* retry times-- */
830 if (num == 0) /* if timeout */
831 {
832 handle->debug_print("hmc5883l: ready bit not be set.\n"); /* timeout */
833
834 return 1; /* return error */
835 }
836 }
837 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_OUTXM, (uint8_t *)buf, 6); /* read raw data */
838 if (res != 0) /* check result */
839 {
840 handle->debug_print("hmc5883l: read failed.\n"); /* read out failed */
841
842 return 1; /* return error */
843 }
844 raw[0] = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get x raw */
845 raw[1] = (int16_t)(((uint16_t)buf[2] << 8) | buf[3]); /* get y raw */
846 raw[2] = (int16_t)(((uint16_t)buf[4] << 8) | buf[5]); /* get z raw */
847 m_gauss[0] = (float)(raw[0]) * resolution; /* calculate x */
848 m_gauss[1] = (float)(raw[1]) * resolution; /* calculate y */
849 m_gauss[2] = (float)(raw[2]) * resolution; /* calculate z */
850
851 return 0; /* success return 0 */
852}
853
865{
866 uint8_t res, prev;
867
868 if (handle == NULL) /* check handle */
869 {
870 return 2; /* return error */
871 }
872 if (handle->inited != 1) /* check handle initialization */
873 {
874 return 3; /* return error */
875 }
876
877 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode register */
878 if (res != 0) /* check result */
879 {
880 handle->debug_print("hmc5883l: read failed.\n"); /* read mode failed */
881
882 return 1; /* return error */
883 }
884 prev &= ~(0x7C); /* clear mode bits */
885 prev &= ~(0x03); /* clear config */
886
887 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write config */
888}
889
901{
902 uint8_t res, prev;
903
904 if (handle == NULL) /* check handle */
905 {
906 return 2; /* return error */
907 }
908 if (handle->inited != 1) /* check handle initialization */
909 {
910 return 3; /* return error */
911 }
912
913 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* read mode register */
914 if (res != 0) /* check result */
915 {
916 handle->debug_print("hmc5883l: read failed.\n"); /* read mode failed */
917
918 return 1; /* return error */
919 }
920 prev &= ~(0x7C); /* clear mode bits */
921 prev &= ~(0x03); /* clear config */
922 prev |= 1 << 1; /* set config */
923
924 return handle->iic_write(HMC5883L_ADDRESS, HMC5883L_REG_MODE, (uint8_t *)&prev, 1); /* write config */
925}
926
939uint8_t hmc5883l_continuous_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
940{
941 uint8_t res, gain, status;
942 uint16_t num = 5000;
943 uint8_t buf[6];
944 float resolution;
945
946 if (handle == NULL) /* check handle */
947 {
948 return 2; /* return error */
949 }
950 if (handle->inited != 1) /* check handle initialization */
951 {
952 return 3; /* return error */
953 }
954
955 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_CRB, (uint8_t *)&gain, 1); /* read crb register failed */
956 if (res != 0) /* check result */
957 {
958 handle->debug_print("hmc5883l: read failed.\n"); /* read crb failed */
959
960 return 1; /* return error */
961 }
962 gain = gain >> 5; /* set gain */
963 switch (gain) /* choose resolution */
964 {
965 case 0x00 :
966 {
967 resolution = 0.73f; /* set resolution 0.73 */
968
969 break; /* break */
970 }
971 case 0x01 :
972 {
973 resolution = 0.92f; /* set resolution 0.92 */
974
975 break; /* break */
976 }
977 case 0x02 :
978 {
979 resolution = 1.22f; /* set resolution 1.22 */
980
981 break; /* break */
982 }
983 case 0x03 :
984 {
985 resolution = 1.52f; /* set resolution 1.52 */
986
987 break; /* break */
988 }
989 case 0x04 :
990 {
991 resolution = 2.27f; /* set resolution 2.27 */
992
993 break; /* break */
994 }
995 case 0x05 :
996 {
997 resolution = 2.56f; /* set resolution 2.56 */
998
999 break; /* break */
1000 }
1001 case 0x06 :
1002 {
1003 resolution = 3.03f; /* set resolution 3.03 */
1004
1005 break; /* break */
1006 }
1007 case 0x07 :
1008 {
1009 resolution = 4.35f; /* set resolution 4.35 */
1010
1011 break; /* break */
1012 }
1013 default : /* unknown code */
1014 {
1015 resolution = 0.00f; /* set resolution 0.00 */
1016
1017 break; /* break */
1018 }
1019 }
1020 while (num != 0) /* check num */
1021 {
1022 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_STATUS, (uint8_t *)&status, 1); /* read status register */
1023 if (res != 0) /* check result */
1024 {
1025 handle->debug_print("hmc5883l: read failed.\n"); /* read status failed */
1026
1027 return 1; /* return error */
1028 }
1029 if ((status & 0x01) != 0) /* check status */
1030 {
1031 break; /* break loop */
1032 }
1033 handle->delay_ms(10); /* check 10 ms */
1034 num--;
1035 if (num == 0) /* if timeout */
1036 {
1037 handle->debug_print("hmc5883l: ready bit not be set.\n"); /* timeout */
1038
1039 return 1; /* return error */
1040 }
1041 }
1042 res = handle->iic_read(HMC5883L_ADDRESS, HMC5883L_REG_OUTXM, (uint8_t *)buf, 6); /* read raw data */
1043 if (res != 0) /* check result */
1044 {
1045 handle->debug_print("hmc5883l: read failed.\n"); /* read out failed */
1046
1047 return 1; /* return error */
1048 }
1049 raw[0] = (int16_t)(((uint16_t)buf[0] << 8) | buf[1]); /* get x raw */
1050 raw[1] = (int16_t)(((uint16_t)buf[2] << 8) | buf[3]); /* get y raw */
1051 raw[2] = (int16_t)(((uint16_t)buf[4] << 8) | buf[5]); /* get z raw */
1052 m_gauss[0] = (float)(raw[0]) * resolution; /* calculate x */
1053 m_gauss[1] = (float)(raw[1]) * resolution; /* calculate y */
1054 m_gauss[2] = (float)(raw[2]) * resolution; /* calculate z */
1055
1056 return 0; /* success return 0 */
1057}
1058
1072uint8_t hmc5883l_set_reg(hmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1073{
1074 if (handle == NULL) /* check handle */
1075 {
1076 return 2; /* return error */
1077 }
1078 if (handle->inited != 1) /* check handle initialization */
1079 {
1080 return 3; /* return error */
1081 }
1082
1083 return handle->iic_write(HMC5883L_ADDRESS, reg, buf, len); /* write data */
1084}
1085
1099uint8_t hmc5883l_get_reg(hmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1100{
1101 if (handle == NULL) /* check handle */
1102 {
1103 return 2; /* return error */
1104 }
1105 if (handle->inited != 1) /* check handle initialization */
1106 {
1107 return 3; /* return error */
1108 }
1109
1110 return handle->iic_read(HMC5883L_ADDRESS, reg, buf, len); /* read data */
1111}
1112
1122{
1123 if (info == NULL) /* check handle */
1124 {
1125 return 2; /* return error */
1126 }
1127
1128 memset(info, 0, sizeof(hmc5883l_info_t)); /* initialize hmc5883l info structure */
1129 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1130 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1131 strncpy(info->interface, "IIC", 8); /* copy interface name */
1132 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1133 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1134 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1135 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1136 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1137 info->driver_version = DRIVER_VERSION; /* set driver version */
1138
1139 return 0; /* success return 0 */
1140}
#define HMC5883L_REG_IDB
#define HMC5883L_REG_CRA
chip register definition
#define HMC5883L_REG_IDC
#define HMC5883L_REG_STATUS
#define MAX_CURRENT
#define HMC5883L_REG_IDA
#define HMC5883L_REG_CRB
#define SUPPLY_VOLTAGE_MAX
#define HMC5883L_ADDRESS
iic address definition
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define HMC5883L_REG_OUTXM
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define HMC5883L_REG_MODE
driver hmc5883l header file
uint8_t hmc5883l_set_mode(hmc5883l_handle_t *handle, hmc5883l_mode_t mode)
set the chip mode
hmc5883l_average_sample_t
hmc5883l average sample enumeration definition
hmc5883l_mode_t
hmc5883l mode enumeration definition
uint8_t hmc5883l_disable_high_speed_iic(hmc5883l_handle_t *handle)
disable the high speed iic
uint8_t hmc5883l_get_mode(hmc5883l_handle_t *handle, hmc5883l_mode_t *mode)
get the chip mode
uint8_t hmc5883l_set_gain(hmc5883l_handle_t *handle, hmc5883l_gain_t gain)
set the chip gain
uint8_t hmc5883l_set_average_sample(hmc5883l_handle_t *handle, hmc5883l_average_sample_t average_sample)
set the average sample rate
uint8_t hmc5883l_init(hmc5883l_handle_t *handle)
initialize the chip
uint8_t hmc5883l_continuous_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
read data continuously
uint8_t hmc5883l_set_data_output_rate(hmc5883l_handle_t *handle, hmc5883l_data_output_rate_t data_rate)
set the data output rate
uint8_t hmc5883l_info(hmc5883l_info_t *info)
get chip's information
uint8_t hmc5883l_deinit(hmc5883l_handle_t *handle)
close the chip
struct hmc5883l_handle_s hmc5883l_handle_t
hmc5883l handle structure definition
struct hmc5883l_info_s hmc5883l_info_t
hmc5883l information structure definition
hmc5883l_gain_t
hmc5883l gain enumeration definition
uint8_t hmc5883l_get_gain(hmc5883l_handle_t *handle, hmc5883l_gain_t *gain)
get the chip gain
hmc5883l_data_output_rate_t
hmc5883l data output rate enumeration definition
uint8_t hmc5883l_get_average_sample(hmc5883l_handle_t *handle, hmc5883l_average_sample_t *average_sample)
get the average sample rate
uint8_t hmc5883l_single_read(hmc5883l_handle_t *handle, int16_t raw[3], float m_gauss[3])
read data once
uint8_t hmc5883l_get_data_output_rate(hmc5883l_handle_t *handle, hmc5883l_data_output_rate_t *data_rate)
get the data output rate
uint8_t hmc5883l_start_continuous_read(hmc5883l_handle_t *handle)
start reading data
uint8_t hmc5883l_enable_high_speed_iic(hmc5883l_handle_t *handle)
enable the high speed iic
uint8_t hmc5883l_stop_continuous_read(hmc5883l_handle_t *handle)
stop reading data
uint8_t hmc5883l_get_reg(hmc5883l_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t hmc5883l_set_reg(hmc5883l_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)
char manufacturer_name[32]