LibDriver MMA7660FC
Loading...
Searching...
No Matches
driver_mma7660fc.c
Go to the documentation of this file.
1
36
37#include "driver_mma7660fc.h"
38
42#define CHIP_NAME "NXP MMA7660FC"
43#define MANUFACTURER_NAME "NXP"
44#define SUPPLY_VOLTAGE_MIN 2.4f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.294f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define MMA7660FC_ADDRESS 0x98
55
59#define MMA7660FC_REG_XOUT 0x00
60#define MMA7660FC_REG_YOUT 0x01
61#define MMA7660FC_REG_ZOUT 0x02
62#define MMA7660FC_REG_TILT 0x03
63#define MMA7660FC_REG_SRST 0x04
64#define MMA7660FC_REG_SPCNT 0x05
65#define MMA7660FC_REG_INTSU 0x06
66#define MMA7660FC_REG_MODE 0x07
67#define MMA7660FC_REG_SR 0x08
68#define MMA7660FC_REG_PDET 0x09
69#define MMA7660FC_REG_PD 0x0A
70
82static uint8_t a_mma7660fc_iic_read(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
83{
84 if (handle->iic_read(MMA7660FC_ADDRESS, reg, buf, len) != 0) /* read */
85 {
86 return 1; /* return error */
87 }
88
89 return 0; /* success return 0 */
90}
91
103static uint8_t a_mma7660fc_iic_write(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
104{
105 if (handle->iic_write(MMA7660FC_ADDRESS, reg, buf, len) != 0) /* write */
106 {
107 return 1; /* return error */
108 }
109
110 return 0; /* success return 0 */
111}
112
124uint8_t mma7660fc_get_tilt_status(mma7660fc_handle_t *handle, uint8_t *status)
125{
126 uint8_t res;
127
128 if (handle == NULL) /* check handle */
129 {
130 return 2; /* return error */
131 }
132 if (handle->inited != 1) /* check handle initialization */
133 {
134 return 3; /* return error */
135 }
136
137 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_TILT, status, 1); /* read tilt */
138 if (res != 0) /* check the result */
139 {
140 handle->debug_print("mma7660fc: read tilt failed.\n"); /* read tilt failed */
141
142 return 1; /* return error */
143 }
144
145 return 0; /* success return 0 */
146}
147
160{
161 uint8_t res;
162 uint8_t prev;
163
164 if (handle == NULL) /* check handle */
165 {
166 return 2; /* return error */
167 }
168 if (handle->inited != 1) /* check handle initialization */
169 {
170 return 3; /* return error */
171 }
172
173 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SRST, &prev, 1); /* read srst */
174 if (res != 0) /* check the result */
175 {
176 handle->debug_print("mma7660fc: read srst failed.\n"); /* read srst failed */
177
178 return 1; /* return error */
179 }
180 *enable = (mma7660fc_bool_t)((prev >> 0) & 0x01); /* get bool */
181
182 return 0; /* success return 0 */
183}
184
197{
198 uint8_t res;
199 uint8_t prev;
200
201 if (handle == NULL) /* check handle */
202 {
203 return 2; /* return error */
204 }
205 if (handle->inited != 1) /* check handle initialization */
206 {
207 return 3; /* return error */
208 }
209
210 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SRST, &prev, 1); /* read srst */
211 if (res != 0) /* check the result */
212 {
213 handle->debug_print("mma7660fc: read srst failed.\n"); /* read srst failed */
214
215 return 1; /* return error */
216 }
217 *enable = (mma7660fc_bool_t)((prev >> 1) & 0x01); /* get bool */
218
219 return 0; /* success return 0 */
220}
221
233uint8_t mma7660fc_set_sleep_count(mma7660fc_handle_t *handle, uint8_t count)
234{
235 uint8_t res;
236 uint8_t prev;
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 prev = count; /* set sleep count */
248 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_SPCNT, &prev, 1); /* write spcnt */
249 if (res != 0) /* check the result */
250 {
251 handle->debug_print("mma7660fc: write spcnt failed.\n"); /* write spcnt failed */
252
253 return 1; /* return error */
254 }
255
256 return 0; /* success return 0 */
257}
258
270uint8_t mma7660fc_get_sleep_count(mma7660fc_handle_t *handle, uint8_t *count)
271{
272 uint8_t res;
273
274 if (handle == NULL) /* check handle */
275 {
276 return 2; /* return error */
277 }
278 if (handle->inited != 1) /* check handle initialization */
279 {
280 return 3; /* return error */
281 }
282
283 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SPCNT, count, 1); /* read spcnt */
284 if (res != 0) /* check the result */
285 {
286 handle->debug_print("mma7660fc: read spcnt failed.\n"); /* read spcnt failed */
287
288 return 1; /* return error */
289 }
290
291 return 0; /* success return 0 */
292}
293
306{
307 uint8_t res;
308 uint8_t prev;
309
310 if (handle == NULL) /* check handle */
311 {
312 return 2; /* return error */
313 }
314 if (handle->inited != 1) /* check handle initialization */
315 {
316 return 3; /* return error */
317 }
318
319 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
320 if (res != 0) /* check the result */
321 {
322 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
323
324 return 1; /* return error */
325 }
326 prev &= ~(1 << 0); /* clear setting bit */
327 prev |= (enable << 0); /* set bool */
328 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
329 if (res != 0) /* check the result */
330 {
331 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
332
333 return 1; /* return error */
334 }
335
336 return 0; /* success return 0 */
337}
338
351{
352 uint8_t res;
353 uint8_t prev;
354
355 if (handle == NULL) /* check handle */
356 {
357 return 2; /* return error */
358 }
359 if (handle->inited != 1) /* check handle initialization */
360 {
361 return 3; /* return error */
362 }
363
364 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
365 if (res != 0) /* check the result */
366 {
367 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
368
369 return 1; /* return error */
370 }
371 *enable = (mma7660fc_bool_t)((prev >> 0) & 0x01); /* get bool */
372
373 return 0; /* success return 0 */
374}
375
388{
389 uint8_t res;
390 uint8_t prev;
391
392 if (handle == NULL) /* check handle */
393 {
394 return 2; /* return error */
395 }
396 if (handle->inited != 1) /* check handle initialization */
397 {
398 return 3; /* return error */
399 }
400
401 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
402 if (res != 0) /* check the result */
403 {
404 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
405
406 return 1; /* return error */
407 }
408 prev &= ~(1 << 1); /* clear setting bit */
409 prev |= (enable << 1); /* set bool */
410 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
411 if (res != 0) /* check the result */
412 {
413 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
414
415 return 1; /* return error */
416 }
417
418 return 0; /* success return 0 */
419}
420
433{
434 uint8_t res;
435 uint8_t prev;
436
437 if (handle == NULL) /* check handle */
438 {
439 return 2; /* return error */
440 }
441 if (handle->inited != 1) /* check handle initialization */
442 {
443 return 3; /* return error */
444 }
445
446 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
447 if (res != 0) /* check the result */
448 {
449 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
450
451 return 1; /* return error */
452 }
453 *enable = (mma7660fc_bool_t)((prev >> 1) & 0x01); /* get bool */
454
455 return 0; /* success return 0 */
456}
457
470{
471 uint8_t res;
472 uint8_t prev;
473
474 if (handle == NULL) /* check handle */
475 {
476 return 2; /* return error */
477 }
478 if (handle->inited != 1) /* check handle initialization */
479 {
480 return 3; /* return error */
481 }
482
483 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
484 if (res != 0) /* check the result */
485 {
486 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
487
488 return 1; /* return error */
489 }
490 prev &= ~(1 << 2); /* clear setting bit */
491 prev |= (enable << 2); /* set bool */
492 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
493 if (res != 0) /* check the result */
494 {
495 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
496
497 return 1; /* return error */
498 }
499
500 return 0; /* success return 0 */
501}
502
515{
516 uint8_t res;
517 uint8_t prev;
518
519 if (handle == NULL) /* check handle */
520 {
521 return 2; /* return error */
522 }
523 if (handle->inited != 1) /* check handle initialization */
524 {
525 return 3; /* return error */
526 }
527
528 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
529 if (res != 0) /* check the result */
530 {
531 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
532
533 return 1; /* return error */
534 }
535 *enable = (mma7660fc_bool_t)((prev >> 2) & 0x01); /* get bool */
536
537 return 0; /* success return 0 */
538}
539
552{
553 uint8_t res;
554 uint8_t prev;
555
556 if (handle == NULL) /* check handle */
557 {
558 return 2; /* return error */
559 }
560 if (handle->inited != 1) /* check handle initialization */
561 {
562 return 3; /* return error */
563 }
564
565 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
566 if (res != 0) /* check the result */
567 {
568 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
569
570 return 1; /* return error */
571 }
572 prev &= ~(1 << 3); /* clear setting bit */
573 prev |= (enable << 3); /* set bool */
574 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
575 if (res != 0) /* check the result */
576 {
577 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
578
579 return 1; /* return error */
580 }
581
582 return 0; /* success return 0 */
583}
584
597{
598 uint8_t res;
599 uint8_t prev;
600
601 if (handle == NULL) /* check handle */
602 {
603 return 2; /* return error */
604 }
605 if (handle->inited != 1) /* check handle initialization */
606 {
607 return 3; /* return error */
608 }
609
610 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
611 if (res != 0) /* check the result */
612 {
613 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
614
615 return 1; /* return error */
616 }
617 *enable = (mma7660fc_bool_t)((prev >> 3) & 0x01); /* get bool */
618
619 return 0; /* success return 0 */
620}
621
634{
635 uint8_t res;
636 uint8_t prev;
637
638 if (handle == NULL) /* check handle */
639 {
640 return 2; /* return error */
641 }
642 if (handle->inited != 1) /* check handle initialization */
643 {
644 return 3; /* return error */
645 }
646
647 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
648 if (res != 0) /* check the result */
649 {
650 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
651
652 return 1; /* return error */
653 }
654 prev &= ~(1 << 4); /* clear setting bit */
655 prev |= (enable << 4); /* set bool */
656 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
657 if (res != 0) /* check the result */
658 {
659 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
660
661 return 1; /* return error */
662 }
663
664 return 0; /* success return 0 */
665}
666
679{
680 uint8_t res;
681 uint8_t 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 = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
693 if (res != 0) /* check the result */
694 {
695 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
696
697 return 1; /* return error */
698 }
699 *enable = (mma7660fc_bool_t)((prev >> 4) & 0x01); /* get bool */
700
701 return 0; /* success return 0 */
702}
703
716{
717 uint8_t res;
718 uint8_t prev;
719
720 if (handle == NULL) /* check handle */
721 {
722 return 2; /* return error */
723 }
724 if (handle->inited != 1) /* check handle initialization */
725 {
726 return 3; /* return error */
727 }
728
729 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
730 if (res != 0) /* check the result */
731 {
732 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
733
734 return 1; /* return error */
735 }
736 prev &= ~(1 << 7); /* clear setting bit */
737 prev |= (enable << 7); /* set bool */
738 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
739 if (res != 0) /* check the result */
740 {
741 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
742
743 return 1; /* return error */
744 }
745
746 return 0; /* success return 0 */
747}
748
761{
762 uint8_t res;
763 uint8_t prev;
764
765 if (handle == NULL) /* check handle */
766 {
767 return 2; /* return error */
768 }
769 if (handle->inited != 1) /* check handle initialization */
770 {
771 return 3; /* return error */
772 }
773
774 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
775 if (res != 0) /* check the result */
776 {
777 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
778
779 return 1; /* return error */
780 }
781 *enable = (mma7660fc_bool_t)((prev >> 7) & 0x01); /* get bool */
782
783 return 0; /* success return 0 */
784}
785
798{
799 uint8_t res;
800 uint8_t prev;
801
802 if (handle == NULL) /* check handle */
803 {
804 return 2; /* return error */
805 }
806 if (handle->inited != 1) /* check handle initialization */
807 {
808 return 3; /* return error */
809 }
810
811 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
812 if (res != 0) /* check the result */
813 {
814 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
815
816 return 1; /* return error */
817 }
818 prev &= ~(1 << 6); /* clear setting bit */
819 prev |= (enable << 6); /* set bool */
820 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
821 if (res != 0) /* check the result */
822 {
823 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
824
825 return 1; /* return error */
826 }
827
828 return 0; /* success return 0 */
829}
830
843{
844 uint8_t res;
845 uint8_t prev;
846
847 if (handle == NULL) /* check handle */
848 {
849 return 2; /* return error */
850 }
851 if (handle->inited != 1) /* check handle initialization */
852 {
853 return 3; /* return error */
854 }
855
856 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
857 if (res != 0) /* check the result */
858 {
859 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
860
861 return 1; /* return error */
862 }
863 *enable = (mma7660fc_bool_t)((prev >> 6) & 0x01); /* get bool */
864
865 return 0; /* success return 0 */
866}
867
880{
881 uint8_t res;
882 uint8_t prev;
883
884 if (handle == NULL) /* check handle */
885 {
886 return 2; /* return error */
887 }
888 if (handle->inited != 1) /* check handle initialization */
889 {
890 return 3; /* return error */
891 }
892
893 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
894 if (res != 0) /* check the result */
895 {
896 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
897
898 return 1; /* return error */
899 }
900 prev &= ~(1 << 5); /* clear setting bit */
901 prev |= (enable << 5); /* set bool */
902 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_INTSU, &prev, 1); /* write intsu */
903 if (res != 0) /* check the result */
904 {
905 handle->debug_print("mma7660fc: write intsu failed.\n"); /* write intsu failed */
906
907 return 1; /* return error */
908 }
909
910 return 0; /* success return 0 */
911}
912
925{
926 uint8_t res;
927 uint8_t prev;
928
929 if (handle == NULL) /* check handle */
930 {
931 return 2; /* return error */
932 }
933 if (handle->inited != 1) /* check handle initialization */
934 {
935 return 3; /* return error */
936 }
937
938 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_INTSU, &prev, 1); /* read intsu */
939 if (res != 0) /* check the result */
940 {
941 handle->debug_print("mma7660fc: read intsu failed.\n"); /* read intsu failed */
942
943 return 1; /* return error */
944 }
945 *enable = (mma7660fc_bool_t)((prev >> 5) & 0x01); /* get bool */
946
947 return 0; /* success return 0 */
948}
949
962{
963 uint8_t res;
964 uint8_t prev;
965
966 if (handle == NULL) /* check handle */
967 {
968 return 2; /* return error */
969 }
970 if (handle->inited != 1) /* check handle initialization */
971 {
972 return 3; /* return error */
973 }
974
975 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
976 if (res != 0) /* check the result */
977 {
978 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
979
980 return 1; /* return error */
981 }
982 prev &= ~(7 << 0); /* clear setting bit */
983 prev |= (mode << 0); /* set mode */
984 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
985 if (res != 0) /* check the result */
986 {
987 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
988
989 return 1; /* return error */
990 }
991
992 return 0; /* success return 0 */
993}
994
1007{
1008 uint8_t res;
1009 uint8_t prev;
1010
1011 if (handle == NULL) /* check handle */
1012 {
1013 return 2; /* return error */
1014 }
1015 if (handle->inited != 1) /* check handle initialization */
1016 {
1017 return 3; /* return error */
1018 }
1019
1020 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1021 if (res != 0) /* check the result */
1022 {
1023 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1024
1025 return 1; /* return error */
1026 }
1027 *mode = (mma7660fc_mode_t)(prev & 0x7); /* get the mode */
1028
1029 return 0; /* success return 0 */
1030}
1031
1044{
1045 uint8_t res;
1046 uint8_t prev;
1047
1048 if (handle == NULL) /* check handle */
1049 {
1050 return 2; /* return error */
1051 }
1052 if (handle->inited != 1) /* check handle initialization */
1053 {
1054 return 3; /* return error */
1055 }
1056
1057 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1058 if (res != 0) /* check the result */
1059 {
1060 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1061
1062 return 1; /* return error */
1063 }
1064 prev &= ~(1 << 3); /* clear setting bit */
1065 prev |= (enable << 3); /* set bool */
1066 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
1067 if (res != 0) /* check the result */
1068 {
1069 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
1070
1071 return 1; /* return error */
1072 }
1073
1074 return 0; /* success return 0 */
1075}
1076
1089{
1090 uint8_t res;
1091 uint8_t prev;
1092
1093 if (handle == NULL) /* check handle */
1094 {
1095 return 2; /* return error */
1096 }
1097 if (handle->inited != 1) /* check handle initialization */
1098 {
1099 return 3; /* return error */
1100 }
1101
1102 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1103 if (res != 0) /* check the result */
1104 {
1105 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1106
1107 return 1; /* return error */
1108 }
1109 *enable = (mma7660fc_bool_t)((prev >> 3) & 0x01); /* get bool */
1110
1111 return 0; /* success return 0 */
1112}
1113
1126{
1127 uint8_t res;
1128 uint8_t prev;
1129
1130 if (handle == NULL) /* check handle */
1131 {
1132 return 2; /* return error */
1133 }
1134 if (handle->inited != 1) /* check handle initialization */
1135 {
1136 return 3; /* return error */
1137 }
1138
1139 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1140 if (res != 0) /* check the result */
1141 {
1142 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1143
1144 return 1; /* return error */
1145 }
1146 prev &= ~(1 << 4); /* clear setting bit */
1147 prev |= (enable << 4); /* set bool */
1148 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
1149 if (res != 0) /* check the result */
1150 {
1151 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
1152
1153 return 1; /* return error */
1154 }
1155
1156 return 0; /* success return 0 */
1157}
1158
1171{
1172 uint8_t res;
1173 uint8_t prev;
1174
1175 if (handle == NULL) /* check handle */
1176 {
1177 return 2; /* return error */
1178 }
1179 if (handle->inited != 1) /* check handle initialization */
1180 {
1181 return 3; /* return error */
1182 }
1183
1184 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1185 if (res != 0) /* check the result */
1186 {
1187 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1188
1189 return 1; /* return error */
1190 }
1191 *enable = (mma7660fc_bool_t)((prev >> 4) & 0x01); /* get bool */
1192
1193 return 0; /* success return 0 */
1194}
1195
1208{
1209 uint8_t res;
1210 uint8_t prev;
1211
1212 if (handle == NULL) /* check handle */
1213 {
1214 return 2; /* return error */
1215 }
1216 if (handle->inited != 1) /* check handle initialization */
1217 {
1218 return 3; /* return error */
1219 }
1220
1221 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1222 if (res != 0) /* check the result */
1223 {
1224 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1225
1226 return 1; /* return error */
1227 }
1228 prev &= ~(1 << 5); /* clear setting bit */
1229 prev |= (prescaler << 5); /* set prescaler */
1230 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
1231 if (res != 0) /* check the result */
1232 {
1233 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
1234
1235 return 1; /* return error */
1236 }
1237
1238 return 0; /* success return 0 */
1239}
1240
1253{
1254 uint8_t res;
1255 uint8_t prev;
1256
1257 if (handle == NULL) /* check handle */
1258 {
1259 return 2; /* return error */
1260 }
1261 if (handle->inited != 1) /* check handle initialization */
1262 {
1263 return 3; /* return error */
1264 }
1265
1266 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1267 if (res != 0) /* check the result */
1268 {
1269 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1270
1271 return 1; /* return error */
1272 }
1273 *prescaler = (mma7660fc_sleep_counter_prescaler_t)((prev >> 5) & 0x01); /* get prescaler */
1274
1275 return 0; /* success return 0 */
1276}
1277
1290{
1291 uint8_t res;
1292 uint8_t prev;
1293
1294 if (handle == NULL) /* check handle */
1295 {
1296 return 2; /* return error */
1297 }
1298 if (handle->inited != 1) /* check handle initialization */
1299 {
1300 return 3; /* return error */
1301 }
1302
1303 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1304 if (res != 0) /* check the result */
1305 {
1306 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1307
1308 return 1; /* return error */
1309 }
1310 prev &= ~(1 << 6); /* clear setting bit */
1311 prev |= (type << 6); /* set type */
1312 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
1313 if (res != 0) /* check the result */
1314 {
1315 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
1316
1317 return 1; /* return error */
1318 }
1319
1320 return 0; /* success return 0 */
1321}
1322
1335{
1336 uint8_t res;
1337 uint8_t prev;
1338
1339 if (handle == NULL) /* check handle */
1340 {
1341 return 2; /* return error */
1342 }
1343 if (handle->inited != 1) /* check handle initialization */
1344 {
1345 return 3; /* return error */
1346 }
1347
1348 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1349 if (res != 0) /* check the result */
1350 {
1351 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1352
1353 return 1; /* return error */
1354 }
1355 *type = (mma7660fc_interrupt_pin_type_t)((prev >> 6) & 0x01); /* get type */
1356
1357 return 0; /* success return 0 */
1358}
1359
1372{
1373 uint8_t res;
1374 uint8_t prev;
1375
1376 if (handle == NULL) /* check handle */
1377 {
1378 return 2; /* return error */
1379 }
1380 if (handle->inited != 1) /* check handle initialization */
1381 {
1382 return 3; /* return error */
1383 }
1384
1385 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1386 if (res != 0) /* check the result */
1387 {
1388 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1389
1390 return 1; /* return error */
1391 }
1392 prev &= ~(1 << 7); /* clear setting bit */
1393 prev |= (level << 7); /* set level */
1394 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
1395 if (res != 0) /* check the result */
1396 {
1397 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
1398
1399 return 1; /* return error */
1400 }
1401
1402 return 0; /* success return 0 */
1403}
1404
1417{
1418 uint8_t res;
1419 uint8_t prev;
1420
1421 if (handle == NULL) /* check handle */
1422 {
1423 return 2; /* return error */
1424 }
1425 if (handle->inited != 1) /* check handle initialization */
1426 {
1427 return 3; /* return error */
1428 }
1429
1430 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
1431 if (res != 0) /* check the result */
1432 {
1433 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
1434
1435 return 1; /* return error */
1436 }
1437 *level = (mma7660fc_interrupt_active_level_t)((prev >> 7) & 0x01); /* get level */
1438
1439 return 0; /* success return 0 */
1440}
1441
1454{
1455 uint8_t res;
1456 uint8_t prev;
1457
1458 if (handle == NULL) /* check handle */
1459 {
1460 return 2; /* return error */
1461 }
1462 if (handle->inited != 1) /* check handle initialization */
1463 {
1464 return 3; /* return error */
1465 }
1466
1467 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1468 if (res != 0) /* check the result */
1469 {
1470 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1471
1472 return 1; /* return error */
1473 }
1474 prev &= ~(7 << 0); /* clear setting bit */
1475 prev |= (rate << 0); /* set rate */
1476 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_SR, &prev, 1); /* write sr */
1477 if (res != 0) /* check the result */
1478 {
1479 handle->debug_print("mma7660fc: write sr failed.\n"); /* write sr failed */
1480
1481 return 1; /* return error */
1482 }
1483
1484 return 0; /* success return 0 */
1485}
1486
1499{
1500 uint8_t res;
1501 uint8_t prev;
1502
1503 if (handle == NULL) /* check handle */
1504 {
1505 return 2; /* return error */
1506 }
1507 if (handle->inited != 1) /* check handle initialization */
1508 {
1509 return 3; /* return error */
1510 }
1511
1512 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1513 if (res != 0) /* check the result */
1514 {
1515 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1516
1517 return 1; /* return error */
1518 }
1519 *rate = (mma7660fc_auto_sleep_rate_t)(prev & 0x07); /* set rate */
1520
1521 return 0; /* success return 0 */
1522}
1523
1536{
1537 uint8_t res;
1538 uint8_t prev;
1539
1540 if (handle == NULL) /* check handle */
1541 {
1542 return 2; /* return error */
1543 }
1544 if (handle->inited != 1) /* check handle initialization */
1545 {
1546 return 3; /* return error */
1547 }
1548
1549 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1550 if (res != 0) /* check the result */
1551 {
1552 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1553
1554 return 1; /* return error */
1555 }
1556 prev &= ~(3 << 3); /* clear setting bit */
1557 prev |= (rate << 3); /* set rate */
1558 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_SR, &prev, 1); /* write sr */
1559 if (res != 0) /* check the result */
1560 {
1561 handle->debug_print("mma7660fc: write sr failed.\n"); /* write sr failed */
1562
1563 return 1; /* return error */
1564 }
1565
1566 return 0; /* success return 0 */
1567}
1568
1581{
1582 uint8_t res;
1583 uint8_t prev;
1584
1585 if (handle == NULL) /* check handle */
1586 {
1587 return 2; /* return error */
1588 }
1589 if (handle->inited != 1) /* check handle initialization */
1590 {
1591 return 3; /* return error */
1592 }
1593
1594 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1595 if (res != 0) /* check the result */
1596 {
1597 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1598
1599 return 1; /* return error */
1600 }
1601 *rate = (mma7660fc_auto_wake_rate_t)((prev >> 3) & 0x03); /* set rate */
1602
1603 return 0; /* success return 0 */
1604}
1605
1618{
1619 uint8_t res;
1620 uint8_t prev;
1621
1622 if (handle == NULL) /* check handle */
1623 {
1624 return 2; /* return error */
1625 }
1626 if (handle->inited != 1) /* check handle initialization */
1627 {
1628 return 3; /* return error */
1629 }
1630
1631 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1632 if (res != 0) /* check the result */
1633 {
1634 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1635
1636 return 1; /* return error */
1637 }
1638 prev &= ~(7 << 5); /* clear setting bit */
1639 prev |= (filter << 5); /* set filter */
1640 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_SR, &prev, 1); /* write sr */
1641 if (res != 0) /* check the result */
1642 {
1643 handle->debug_print("mma7660fc: write sr failed.\n"); /* write sr failed */
1644
1645 return 1; /* return error */
1646 }
1647
1648 return 0; /* success return 0 */
1649}
1650
1663{
1664 uint8_t res;
1665 uint8_t prev;
1666
1667 if (handle == NULL) /* check handle */
1668 {
1669 return 2; /* return error */
1670 }
1671 if (handle->inited != 1) /* check handle initialization */
1672 {
1673 return 3; /* return error */
1674 }
1675
1676 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SR, &prev, 1); /* read sr */
1677 if (res != 0) /* check the result */
1678 {
1679 handle->debug_print("mma7660fc: read sr failed.\n"); /* read sr failed */
1680
1681 return 1; /* return error */
1682 }
1683 *filter = (mma7660fc_tilt_debounce_filter_t)((prev >> 5) & 0x07); /* set filter */
1684
1685 return 0; /* success return 0 */
1686}
1687
1701{
1702 uint8_t res;
1703 uint8_t prev;
1704
1705 if (handle == NULL) /* check handle */
1706 {
1707 return 2; /* return error */
1708 }
1709 if (handle->inited != 1) /* check handle initialization */
1710 {
1711 return 3; /* return error */
1712 }
1713 if (threshold > 31) /* check threshold */
1714 {
1715 handle->debug_print("mma7660fc: threshold > 31.\n"); /* threshold > 31 */
1716
1717 return 4; /* return error */
1718 }
1719
1720 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1721 if (res != 0) /* check the result */
1722 {
1723 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1724
1725 return 1; /* return error */
1726 }
1727 prev &= ~(0x1F << 0); /* clear setting bit */
1728 prev |= (threshold << 0); /* set threshold */
1729 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_PDET, &prev, 1); /* write pdet */
1730 if (res != 0) /* check the result */
1731 {
1732 handle->debug_print("mma7660fc: write pdet failed.\n"); /* write pdet failed */
1733
1734 return 1; /* return error */
1735 }
1736
1737 return 0; /* success return 0 */
1738}
1739
1752{
1753 uint8_t res;
1754 uint8_t prev;
1755
1756 if (handle == NULL) /* check handle */
1757 {
1758 return 2; /* return error */
1759 }
1760 if (handle->inited != 1) /* check handle initialization */
1761 {
1762 return 3; /* return error */
1763 }
1764
1765 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1766 if (res != 0) /* check the result */
1767 {
1768 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1769
1770 return 1; /* return error */
1771 }
1772 *threshold = prev & 0x01F; /* set threshold */
1773
1774 return 0; /* success return 0 */
1775}
1776
1789{
1790 uint8_t res;
1791 uint8_t prev;
1792
1793 if (handle == NULL) /* check handle */
1794 {
1795 return 2; /* return error */
1796 }
1797 if (handle->inited != 1) /* check handle initialization */
1798 {
1799 return 3; /* return error */
1800 }
1801
1802 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1803 if (res != 0) /* check the result */
1804 {
1805 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1806
1807 return 1; /* return error */
1808 }
1809 prev &= ~(1 << 5); /* clear setting bit */
1810 prev |= ((!enable) << 5); /* set bool */
1811 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_PDET, &prev, 1); /* write pdet */
1812 if (res != 0) /* check the result */
1813 {
1814 handle->debug_print("mma7660fc: write pdet failed.\n"); /* write pdet failed */
1815
1816 return 1; /* return error */
1817 }
1818
1819 return 0; /* success return 0 */
1820}
1821
1834{
1835 uint8_t res;
1836 uint8_t prev;
1837
1838 if (handle == NULL) /* check handle */
1839 {
1840 return 2; /* return error */
1841 }
1842 if (handle->inited != 1) /* check handle initialization */
1843 {
1844 return 3; /* return error */
1845 }
1846
1847 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1848 if (res != 0) /* check the result */
1849 {
1850 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1851
1852 return 1; /* return error */
1853 }
1854 *enable = (mma7660fc_bool_t)(!((prev >> 5) & 0x01)); /* set bool */
1855
1856 return 0; /* success return 0 */
1857}
1858
1871{
1872 uint8_t res;
1873 uint8_t prev;
1874
1875 if (handle == NULL) /* check handle */
1876 {
1877 return 2; /* return error */
1878 }
1879 if (handle->inited != 1) /* check handle initialization */
1880 {
1881 return 3; /* return error */
1882 }
1883
1884 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1885 if (res != 0) /* check the result */
1886 {
1887 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1888
1889 return 1; /* return error */
1890 }
1891 prev &= ~(1 << 6); /* clear setting bit */
1892 prev |= ((!enable) << 6); /* set bool */
1893 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_PDET, &prev, 1); /* write pdet */
1894 if (res != 0) /* check the result */
1895 {
1896 handle->debug_print("mma7660fc: write pdet failed.\n"); /* write pdet failed */
1897
1898 return 1; /* return error */
1899 }
1900
1901 return 0; /* success return 0 */
1902}
1903
1916{
1917 uint8_t res;
1918 uint8_t prev;
1919
1920 if (handle == NULL) /* check handle */
1921 {
1922 return 2; /* return error */
1923 }
1924 if (handle->inited != 1) /* check handle initialization */
1925 {
1926 return 3; /* return error */
1927 }
1928
1929 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1930 if (res != 0) /* check the result */
1931 {
1932 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1933
1934 return 1; /* return error */
1935 }
1936 *enable = (mma7660fc_bool_t)(!((prev >> 6) & 0x01)); /* set bool */
1937
1938 return 0; /* success return 0 */
1939}
1940
1953{
1954 uint8_t res;
1955 uint8_t prev;
1956
1957 if (handle == NULL) /* check handle */
1958 {
1959 return 2; /* return error */
1960 }
1961 if (handle->inited != 1) /* check handle initialization */
1962 {
1963 return 3; /* return error */
1964 }
1965
1966 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
1967 if (res != 0) /* check the result */
1968 {
1969 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
1970
1971 return 1; /* return error */
1972 }
1973 prev &= ~(1 << 7); /* clear setting bit */
1974 prev |= ((!enable) << 7); /* set bool */
1975 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_PDET, &prev, 1); /* write pdet */
1976 if (res != 0) /* check the result */
1977 {
1978 handle->debug_print("mma7660fc: write pdet failed.\n"); /* write pdet failed */
1979
1980 return 1; /* return error */
1981 }
1982
1983 return 0; /* success return 0 */
1984}
1985
1998{
1999 uint8_t res;
2000 uint8_t prev;
2001
2002 if (handle == NULL) /* check handle */
2003 {
2004 return 2; /* return error */
2005 }
2006 if (handle->inited != 1) /* check handle initialization */
2007 {
2008 return 3; /* return error */
2009 }
2010
2011 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PDET, &prev, 1); /* read pdet */
2012 if (res != 0) /* check the result */
2013 {
2014 handle->debug_print("mma7660fc: read pdet failed.\n"); /* read pdet failed */
2015
2016 return 1; /* return error */
2017 }
2018 *enable = (mma7660fc_bool_t)(!((prev >> 7) & 0x01)); /* set bool */
2019
2020 return 0; /* success return 0 */
2021}
2022
2035{
2036 uint8_t res;
2037 uint8_t prev;
2038
2039 if (handle == NULL) /* check handle */
2040 {
2041 return 2; /* return error */
2042 }
2043 if (handle->inited != 1) /* check handle initialization */
2044 {
2045 return 3; /* return error */
2046 }
2047
2048 prev = count; /* set count */
2049 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_PD, &prev, 1); /* write pd */
2050 if (res != 0) /* check the result */
2051 {
2052 handle->debug_print("mma7660fc: write pd failed.\n"); /* write pd failed */
2053
2054 return 1; /* return error */
2055 }
2056
2057 return 0; /* success return 0 */
2058}
2059
2072{
2073 uint8_t res;
2074
2075 if (handle == NULL) /* check handle */
2076 {
2077 return 2; /* return error */
2078 }
2079 if (handle->inited != 1) /* check handle initialization */
2080 {
2081 return 3; /* return error */
2082 }
2083
2084 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_PD, count, 1); /* read pd */
2085 if (res != 0) /* check the result */
2086 {
2087 handle->debug_print("mma7660fc: read pd failed.\n"); /* read pd failed */
2088
2089 return 1; /* return error */
2090 }
2091
2092 return 0; /* success return 0 */
2093}
2094
2107{
2108 if (handle == NULL) /* check handle */
2109 {
2110 return 2; /* return error */
2111 }
2112 if (handle->inited != 1) /* check handle initialization */
2113 {
2114 return 3; /* return error */
2115 }
2116
2117 *reg = (uint8_t)((ms - 0.52f) / 0.26f); /* convert real data to register data */
2118
2119 return 0; /* success return 0 */
2120}
2121
2134{
2135 if (handle == NULL) /* check handle */
2136 {
2137 return 2; /* return error */
2138 }
2139 if (handle->inited != 1) /* check handle initialization */
2140 {
2141 return 3; /* return error */
2142 }
2143
2144 if (reg == 0) /* if reg = 0 */
2145 {
2146 *ms = 0.52f; /* set 0.52ms */
2147 }
2148 else /* common convert */
2149 {
2150 *ms = (float)(reg) * 0.26f + 0.52f; /* convert raw data to real data */
2151 }
2152
2153 return 0; /* success return 0 */
2154}
2155
2167{
2168 uint8_t res;
2169 uint8_t prev;
2170 uint8_t ba_fro;
2171 uint8_t pola;
2172
2173 if (handle == NULL) /* check handle */
2174 {
2175 return 2; /* return error */
2176 }
2177 if (handle->inited != 1) /* check handle initialization */
2178 {
2179 return 3; /* return error */
2180 }
2181
2182 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_TILT, &prev, 1); /* read tilt */
2183 if (res != 0) /* check the result */
2184 {
2185 handle->debug_print("mma7660fc: read tilt failed.\n"); /* read tilt failed */
2186
2187 return 1; /* return error */
2188 }
2189 ba_fro = prev & 0x03; /*get ba fro */
2190 pola = (prev >> 2) & 0x07; /* get pola */
2191 if (ba_fro == 0x01) /* if lying on its front */
2192 {
2193 if (handle->receive_callback != NULL) /* if receive callback */
2194 {
2195 handle->receive_callback(MMA7660FC_STATUS_FRONT); /* run callback */
2196 }
2197 }
2198 if (ba_fro == 0x02) /* if lying on its back */
2199 {
2200 if (handle->receive_callback != NULL) /* if receive callback */
2201 {
2202 handle->receive_callback(MMA7660FC_STATUS_BACK); /* run callback */
2203 }
2204 }
2205 if (pola == 0x01) /* if landscape mode to the left */
2206 {
2207 if (handle->receive_callback != NULL) /* if receive callback */
2208 {
2209 handle->receive_callback(MMA7660FC_STATUS_LEFT); /* run callback */
2210 }
2211 }
2212 if (pola == 0x02) /* if landscape mode to the right */
2213 {
2214 if (handle->receive_callback != NULL) /* if receive callback */
2215 {
2216 handle->receive_callback(MMA7660FC_STATUS_RIGHT); /* run callback */
2217 }
2218 }
2219 if (pola == 0x05) /* if standing vertically in inverted orientation */
2220 {
2221 if (handle->receive_callback != NULL) /* if receive callback */
2222 {
2223 handle->receive_callback(MMA7660FC_STATUS_DOWN); /* run callback */
2224 }
2225 }
2226 if (pola == 0x06) /* if standing vertically in normal orientation */
2227 {
2228 if (handle->receive_callback != NULL) /* if receive callback */
2229 {
2230 handle->receive_callback(MMA7660FC_STATUS_UP); /* run callback */
2231 }
2232 }
2233 if ((prev & (1 << 5)) != 0) /* if detected a tap */
2234 {
2235 if (handle->receive_callback != NULL) /* if receive callback */
2236 {
2237 handle->receive_callback(MMA7660FC_STATUS_TAP); /* run callback */
2238 }
2239 }
2240 if ((prev & (1 << 6)) != 0) /* if data is invalid */
2241 {
2242 if (handle->receive_callback != NULL) /* if receive callback */
2243 {
2244 handle->receive_callback(MMA7660FC_STATUS_UPDATE); /* run callback */
2245 }
2246 }
2247 if ((prev & (1 << 7)) != 0) /* if experiencing shake in one or more of the axes */
2248 {
2249 if (handle->receive_callback != NULL) /* if receive callback */
2250 {
2251 handle->receive_callback(MMA7660FC_STATUS_SHAKE); /* run callback */
2252 }
2253 }
2254 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_SRST, &prev, 1); /* read srst */
2255 if (res != 0) /* check the result */
2256 {
2257 handle->debug_print("mma7660fc: read srst failed.\n"); /* read srst failed */
2258
2259 return 1; /* return error */
2260 }
2261 if ((prev & (1 << 0)) != 0) /* if auto sleep */
2262 {
2263 if (handle->receive_callback != NULL) /* if receive callback */
2264 {
2265 handle->receive_callback(MMA7660FC_STATUS_AUTO_SLEEP); /* run callback */
2266 }
2267 }
2268 if ((prev & (1 << 1)) != 0) /* if wake up */
2269 {
2270 if (handle->receive_callback != NULL) /* if receive callback */
2271 {
2272 handle->receive_callback(MMA7660FC_STATUS_AUTO_WAKE_UP); /* run callback */
2273 }
2274 }
2275
2276 return 0; /* success return 0 */
2277}
2278
2290{
2291 if (handle == NULL) /* check handle */
2292 {
2293 return 2; /* return error */
2294 }
2295 if (handle->debug_print == NULL) /* check debug_print */
2296 {
2297 return 3; /* return error */
2298 }
2299 if (handle->iic_init == NULL) /* check iic_init */
2300 {
2301 handle->debug_print("mma7660fc: iic_init is null.\n"); /* iic_init is nul */
2302
2303 return 3; /* return error */
2304 }
2305 if (handle->iic_deinit == NULL) /* check iic_deinit */
2306 {
2307 handle->debug_print("mma7660fc: iic_deinit is null.\n"); /* iic_deinit is null */
2308
2309 return 3; /* return error */
2310 }
2311 if (handle->iic_read == NULL) /* check iic_read */
2312 {
2313 handle->debug_print("mma7660fc: iic_read is null.\n"); /* iic_read is null */
2314
2315 return 3; /* return error */
2316 }
2317 if (handle->iic_write == NULL) /* check iic_write */
2318 {
2319 handle->debug_print("mma7660fc: iic_write is null.\n"); /* iic_write is null */
2320
2321 return 3; /* return error */
2322 }
2323 if (handle->receive_callback == NULL) /* check receive_callback */
2324 {
2325 handle->debug_print("mma7660fc: receive_callback is null.\n"); /* receive_callback is null */
2326
2327 return 3; /* return error */
2328 }
2329 if (handle->delay_ms == NULL) /* check delay_ms */
2330 {
2331 handle->debug_print("mma7660fc: delay_ms is null.\n"); /* delay_ms is null */
2332
2333 return 3; /* return error */
2334 }
2335
2336 if (handle->iic_init() != 0) /* iic init */
2337 {
2338 handle->debug_print("mma7660fc: iic init failed.\n"); /* iic init failed */
2339
2340 return 1; /* return error */
2341 }
2342 handle->inited = 1; /* flag inited */
2343
2344 return 0; /* success return 0 */
2345}
2346
2359{
2360 uint8_t res;
2361 uint8_t prev;
2362
2363 if (handle == NULL) /* check handle */
2364 {
2365 return 2; /* return error */
2366 }
2367 if (handle->inited != 1) /* check handle initialization */
2368 {
2369 return 3; /* return error */
2370 }
2371
2372 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_MODE, &prev, 1); /* read mode */
2373 if (res != 0) /* check the result */
2374 {
2375 handle->debug_print("mma7660fc: read mode failed.\n"); /* read mode failed */
2376
2377 return 4; /* return error */
2378 }
2379 prev &= ~(7 << 0); /* clear setting bit */
2380 res = a_mma7660fc_iic_write(handle, MMA7660FC_REG_MODE, &prev, 1); /* write mode */
2381 if (res != 0) /* check the result */
2382 {
2383 handle->debug_print("mma7660fc: write mode failed.\n"); /* write mode failed */
2384
2385 return 4; /* return error */
2386 }
2387 if (handle->iic_deinit() != 0) /* iic deinit */
2388 {
2389 handle->debug_print("mma7660fc: iic deinit failed.\n"); /* iic deinit failed */
2390
2391 return 1; /* return error */
2392 }
2393 handle->inited = 0; /* flag close */
2394
2395 return 0; /* success return 0 */
2396}
2397
2411uint8_t mma7660fc_read(mma7660fc_handle_t *handle, int8_t raw[3], float g[3])
2412{
2413 uint8_t res;
2414 uint8_t buf[3];
2415
2416 if (handle == NULL) /* check handle */
2417 {
2418 return 2; /* return error */
2419 }
2420 if (handle->inited != 1) /* check handle initialization */
2421 {
2422 return 3; /* return error */
2423 }
2424
2425 res = a_mma7660fc_iic_read(handle, MMA7660FC_REG_XOUT, buf, 3); /* read data */
2426 if (res != 0) /* check the result */
2427 {
2428 handle->debug_print("mma7660fc: read data failed.\n"); /* read data failed */
2429
2430 return 1; /* return error */
2431 }
2432 buf[0] &= ~(1 << 7); /* clear bit 7 */
2433 buf[1] &= ~(1 << 7); /* clear bit 7 */
2434 buf[2] &= ~(1 << 7); /* clear bit 7 */
2435 if ((buf[0] & (1 << 6)) != 0) /* check alert bit */
2436 {
2437 handle->debug_print("mma7660fc: data is invalid.\n"); /* data is invalid */
2438
2439 return 4; /* return error */
2440 }
2441 if ((buf[1] & (1 << 6)) != 0) /* check alert bit */
2442 {
2443 handle->debug_print("mma7660fc: data is invalid.\n"); /* data is invalid */
2444
2445 return 4; /* return error */
2446 }
2447 if ((buf[2] & (1 << 6)) != 0) /* check alert bit */
2448 {
2449 handle->debug_print("mma7660fc: data is invalid.\n"); /* data is invalid */
2450
2451 return 4; /* return error */
2452 }
2453
2454 if ((buf[0] & (1 << 5)) != 0) /* check negative bit */
2455 {
2456 raw[0] = (int8_t)(buf[0] | (3 << 6)); /* extend to bit 6 and 7 */
2457 g[0] = (float)(raw[0] / 21.33f); /* convert to real data */
2458 }
2459 else
2460 {
2461 raw[0] = (int8_t)buf[0]; /* copy data */
2462 g[0] = (float)(raw[0] / 21.33f); /* convert to real data */
2463 }
2464 if ((buf[1] & (1 << 5)) != 0) /* check negative bit */
2465 {
2466 raw[1] = (int8_t)(buf[1] | (3 << 6)); /* extend to bit 6 and 7 */
2467 g[1] = (float)(raw[1] / 21.33f); /* convert to real data */
2468 }
2469 else
2470 {
2471 raw[1] = (int8_t)buf[1]; /* copy data */
2472 g[1] = (float)(raw[1] / 21.33f); /* convert to real data */
2473 }
2474 if ((buf[2] & (1 << 5)) != 0) /* check negative bit */
2475 {
2476 raw[2] = (int8_t)(buf[2] | (3 << 6)); /* extend to bit 6 and 7 */
2477 g[2] = (float)(raw[2] / 21.33f); /* convert to real data */
2478 }
2479 else
2480 {
2481 raw[2] = (int8_t)buf[2]; /* copy data */
2482 g[2] = (float)(raw[2] / 21.33f); /* convert to real data */
2483 }
2484
2485 return 0; /* success return 0 */
2486}
2487
2501uint8_t mma7660fc_set_reg(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2502{
2503 if (handle == NULL) /* check handle */
2504 {
2505 return 2; /* return error */
2506 }
2507 if (handle->inited != 1) /* check handle initialization */
2508 {
2509 return 3; /* return error */
2510 }
2511
2512 return a_mma7660fc_iic_write(handle, reg, buf, len); /* write register */
2513}
2514
2528uint8_t mma7660fc_get_reg(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2529{
2530 if (handle == NULL) /* check handle */
2531 {
2532 return 2; /* return error */
2533 }
2534 if (handle->inited != 1) /* check handle initialization */
2535 {
2536 return 3; /* return error */
2537 }
2538
2539 return a_mma7660fc_iic_read(handle, reg, buf, len); /* read register */
2540}
2541
2551{
2552 if (info == NULL) /* check handle */
2553 {
2554 return 2; /* return error */
2555 }
2556
2557 memset(info, 0, sizeof(mma7660fc_info_t)); /* initialize mma7660fc info structure */
2558 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2559 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2560 strncpy(info->interface, "IIC", 8); /* copy interface name */
2561 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2562 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2563 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2564 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2565 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2566 info->driver_version = DRIVER_VERSION; /* set driver version */
2567
2568 return 0; /* success return 0 */
2569}
#define MMA7660FC_REG_PDET
#define MMA7660FC_REG_MODE
#define MMA7660FC_REG_XOUT
chip register definition
#define MAX_CURRENT
#define MMA7660FC_REG_PD
#define MMA7660FC_REG_SRST
#define SUPPLY_VOLTAGE_MAX
#define MMA7660FC_REG_INTSU
#define TEMPERATURE_MAX
#define MMA7660FC_REG_SR
#define MANUFACTURER_NAME
#define MMA7660FC_REG_TILT
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define MMA7660FC_ADDRESS
chip address definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define MMA7660FC_REG_SPCNT
driver mma7660fc header file
uint8_t mma7660fc_get_sleep_count(mma7660fc_handle_t *handle, uint8_t *count)
get sleep count
uint8_t mma7660fc_set_auto_wake_up(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto wake up
uint8_t mma7660fc_get_auto_sleep_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get auto sleep interrupt status
uint8_t mma7660fc_init(mma7660fc_handle_t *handle)
initialize the chip
mma7660fc_interrupt_pin_type_t
mma7660fc interrupt pin type enumeration definition
mma7660fc_bool_t
mma7660fc bool enumeration definition
uint8_t mma7660fc_get_shake_x_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get shake x interrupt status
uint8_t mma7660fc_set_shake_x_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake x interrupt
uint8_t mma7660fc_get_shake_y_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get shake y interrupt status
uint8_t mma7660fc_get_tap_y_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get tap y detection status
uint8_t mma7660fc_get_shake_z_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get shake z interrupt status
uint8_t mma7660fc_get_auto_wake_up(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get auto wake up status
uint8_t mma7660fc_set_sleep_counter_prescaler(mma7660fc_handle_t *handle, mma7660fc_sleep_counter_prescaler_t prescaler)
set sleep counter prescaler
uint8_t mma7660fc_get_tilt_status(mma7660fc_handle_t *handle, uint8_t *status)
get tilt status
uint8_t mma7660fc_set_interrupt_pin_type(mma7660fc_handle_t *handle, mma7660fc_interrupt_pin_type_t type)
set interrupt pin type
uint8_t mma7660fc_tap_pulse_debounce_convert_to_data(mma7660fc_handle_t *handle, uint8_t reg, float *ms)
convert the register raw data to tap pulse debounce
uint8_t mma7660fc_get_auto_sleep(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get auto sleep status
uint8_t mma7660fc_read(mma7660fc_handle_t *handle, int8_t raw[3], float g[3])
read the data
uint8_t mma7660fc_set_tap_detection_threshold(mma7660fc_handle_t *handle, uint8_t threshold)
set tap detection threshold
uint8_t mma7660fc_deinit(mma7660fc_handle_t *handle)
close the chip
uint8_t mma7660fc_set_shake_z_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake z interrupt
uint8_t mma7660fc_tap_pulse_debounce_convert_to_register(mma7660fc_handle_t *handle, float ms, uint8_t *reg)
convert tap pulse debounce to the register raw data
uint8_t mma7660fc_get_sleep_counter_prescaler(mma7660fc_handle_t *handle, mma7660fc_sleep_counter_prescaler_t *prescaler)
get sleep counter prescaler
uint8_t mma7660fc_get_update_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get update interrupt status
uint8_t mma7660fc_info(mma7660fc_info_t *info)
get chip's information
uint8_t mma7660fc_set_tilt_debounce_filter(mma7660fc_handle_t *handle, mma7660fc_tilt_debounce_filter_t filter)
set tilt debounce filter
uint8_t mma7660fc_set_auto_wake_rate(mma7660fc_handle_t *handle, mma7660fc_auto_wake_rate_t rate)
set auto wake rate
struct mma7660fc_info_s mma7660fc_info_t
mma7660fc information structure definition
uint8_t mma7660fc_get_tap_pulse_debounce_count(mma7660fc_handle_t *handle, uint8_t *count)
get tap pulse debounce count
uint8_t mma7660fc_get_up_down_right_left_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get up down right left interrupt status
uint8_t mma7660fc_set_mode(mma7660fc_handle_t *handle, mma7660fc_mode_t mode)
set mode
uint8_t mma7660fc_set_auto_sleep_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto sleep interrupt
uint8_t mma7660fc_get_front_back_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get front back interrupt status
uint8_t mma7660fc_get_tilt_debounce_filter(mma7660fc_handle_t *handle, mma7660fc_tilt_debounce_filter_t *filter)
get tilt debounce filter
uint8_t mma7660fc_get_tap_detection_rate(mma7660fc_handle_t *handle, mma7660fc_auto_sleep_rate_t *rate)
get tap detection rate
struct mma7660fc_handle_s mma7660fc_handle_t
mma7660fc handle structure definition
mma7660fc_interrupt_active_level_t
mma7660fc interrupt active level enumeration definition
uint8_t mma7660fc_set_update_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable update interrupt status
uint8_t mma7660fc_set_front_back_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable front back interrupt
uint8_t mma7660fc_set_tap_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap interrupt
uint8_t mma7660fc_set_tap_x_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap x detection
uint8_t mma7660fc_get_tap_z_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get tap z detection status
uint8_t mma7660fc_set_tap_y_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap y detection
uint8_t mma7660fc_set_auto_sleep(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto sleep
mma7660fc_sleep_counter_prescaler_t
mma7660fc sleep counter prescaler enumeration definition
uint8_t mma7660fc_get_tap_detection_threshold(mma7660fc_handle_t *handle, uint8_t *threshold)
get tap detection threshold
uint8_t mma7660fc_set_interrupt_active_level(mma7660fc_handle_t *handle, mma7660fc_interrupt_active_level_t level)
set interrupt active level
uint8_t mma7660fc_set_tap_pulse_debounce_count(mma7660fc_handle_t *handle, uint8_t count)
set tap pulse debounce count
mma7660fc_auto_sleep_rate_t
mma7660fc auto sleep rate enumeration definition
uint8_t mma7660fc_irq_handler(mma7660fc_handle_t *handle)
irq handler
mma7660fc_tilt_debounce_filter_t
mma7660fc tilt debounce filter enumeration definition
uint8_t mma7660fc_get_mode(mma7660fc_handle_t *handle, mma7660fc_mode_t *mode)
get mode
mma7660fc_mode_t
mma7660fc mode enumeration definition
uint8_t mma7660fc_get_interrupt_active_level(mma7660fc_handle_t *handle, mma7660fc_interrupt_active_level_t *level)
get interrupt active level
uint8_t mma7660fc_get_auto_wake_rate(mma7660fc_handle_t *handle, mma7660fc_auto_wake_rate_t *rate)
get auto wake rate
uint8_t mma7660fc_set_tap_detection_rate(mma7660fc_handle_t *handle, mma7660fc_auto_sleep_rate_t rate)
set tap detection rate
uint8_t mma7660fc_get_auto_sleep_status(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get auto sleep status
uint8_t mma7660fc_set_shake_y_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake y interrupt
uint8_t mma7660fc_set_sleep_count(mma7660fc_handle_t *handle, uint8_t count)
set sleep count
uint8_t mma7660fc_get_interrupt_pin_type(mma7660fc_handle_t *handle, mma7660fc_interrupt_pin_type_t *type)
get interrupt pin type
uint8_t mma7660fc_get_auto_wake_up_status(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get auto wake up status
uint8_t mma7660fc_get_tap_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get tap interrupt status
uint8_t mma7660fc_get_tap_x_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t *enable)
get tap x detection status
uint8_t mma7660fc_set_tap_z_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap z detection
uint8_t mma7660fc_set_up_down_right_left_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable up down right left interrupt
mma7660fc_auto_wake_rate_t
mma7660fc auto wake rate enumeration definition
@ MMA7660FC_STATUS_LEFT
@ MMA7660FC_STATUS_TAP
@ MMA7660FC_STATUS_RIGHT
@ MMA7660FC_STATUS_FRONT
@ MMA7660FC_STATUS_SHAKE
@ MMA7660FC_STATUS_AUTO_SLEEP
@ MMA7660FC_STATUS_UP
@ MMA7660FC_STATUS_BACK
@ MMA7660FC_STATUS_AUTO_WAKE_UP
@ MMA7660FC_STATUS_DOWN
@ MMA7660FC_STATUS_UPDATE
uint8_t mma7660fc_set_reg(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t mma7660fc_get_reg(mma7660fc_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
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)