LibDriver UVIS25
Loading...
Searching...
No Matches
driver_uvis25.c
Go to the documentation of this file.
1
37
38#include "driver_uvis25.h"
39
43#define CHIP_NAME "STMicroelectronics UVIS25"
44#define MANUFACTURER_NAME "STMicroelectronics"
45#define SUPPLY_VOLTAGE_MIN 1.7f
46#define SUPPLY_VOLTAGE_MAX 3.6f
47#define MAX_CURRENT 0.01f
48#define TEMPERATURE_MIN -20.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define UVIS25_REG_WHO_AM_I 0x0F
56#define UVIS25_REG_CTRL_REG1 0x20
57#define UVIS25_REG_CTRL_REG2 0x21
58#define UVIS25_REG_CTRL_REG3 0x22
59#define UVIS25_REG_INT_CFG 0x23
60#define UVIS25_REG_INT_SOURCE 0x24
61#define UVIS25_REG_THS_UV 0x25
62#define UVIS25_REG_STATUS_REG 0x27
63#define UVIS25_REG_UV_OUT_REG 0x28
64
68#define UVIS25_IIC_ADDRESS 0x8E
69
81static uint8_t a_uvis25_iic_spi_read(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
82{
83 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* iic interface */
84 {
85 if (len > 1) /* if length > 1 */
86 {
87 reg |= 1 << 7; /* set read more than 1 byte */
88 }
89
90 if (handle->iic_read(UVIS25_IIC_ADDRESS, reg, buf, len) != 0) /* read register */
91 {
92 return 1; /* return error */
93 }
94 else
95 {
96 return 0; /* success return 0 */
97 }
98 }
99 else /* spi interface */
100 {
101 if (len > 1) /* if length > 1 */
102 {
103 reg |= 1 << 6; /* set read more than 1 byte */
104 }
105 reg |= 1 << 7; /* set read mode */
106
107 if (handle->spi_read(reg, buf, len) != 0) /* read register */
108 {
109 return 1; /* return error */
110 }
111 else
112 {
113 return 0; /* success return 0 */
114 }
115 }
116}
117
129static uint8_t a_uvis25_iic_spi_write(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
130{
131 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* iic interface */
132 {
133 if (len > 1) /* if length > 1 */
134 {
135 reg |= 1 << 7; /* set write more than 1 byte */
136 }
137 if (handle->iic_write(UVIS25_IIC_ADDRESS, reg, buf, len) != 0) /* write register */
138 {
139 return 1; /* return error */
140 }
141 else
142 {
143 return 0; /* success return 0 */
144 }
145 }
146 else /* spi interface */
147 {
148 if (len > 1) /* if length > 1 */
149 {
150 reg |= 1 << 6; /* set write more than 1 byte */
151 }
152
153 if (handle->spi_write(reg, buf, len) != 0) /* write register */
154 {
155 return 1; /* return error */
156 }
157 else
158 {
159 return 0; /* success return 0 */
160 }
161 }
162}
163
174{
175 if (handle == NULL) /* check handle */
176 {
177 return 2; /* return error */
178 }
179
180 handle->iic_spi = (uint8_t)interface; /* set interface */
181
182 return 0; /* success return 0 */
183}
184
195{
196 if (handle == NULL) /* check handle */
197 {
198 return 2; /* return error */
199 }
200
201 *interface = (uvis25_interface_t)(handle->iic_spi); /* get interface */
202
203 return 0; /* success return 0 */
204}
205
218{
219 uint8_t res;
220 uint8_t prev;
221
222 if (handle == NULL) /* check handle */
223 {
224 return 2; /* return error */
225 }
226 if (handle->inited != 1) /* check handle initialization */
227 {
228 return 3; /* return error */
229 }
230
231 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* read ctrl reg1 */
232 if (res != 0) /* check result */
233 {
234 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
235
236 return 1; /* return error */
237 }
238 prev &= ~(1 << 1); /* clear enable bit */
239 prev |= enable << 1; /* set enable */
240
241 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* write config */
242}
243
256{
257 uint8_t res;
258 uint8_t prev;
259
260 if (handle == NULL) /* check handle */
261 {
262 return 2; /* return error */
263 }
264 if (handle->inited != 1) /* check handle initialization */
265 {
266 return 3; /* return error */
267 }
268
269 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* read reg */
270 if (res != 0) /* check result */
271 {
272 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
273
274 return 1; /* return error */
275 }
276 *enable = (uvis25_bool_t)((prev >> 1) & 0x01); /* get bool */
277
278 return 0; /* success return 0 */
279}
280
293{
294 uint8_t res;
295 uint8_t prev;
296
297 if (handle == NULL) /* check handle */
298 {
299 return 2; /* return error */
300 }
301 if (handle->inited != 1) /* check handle initialization */
302 {
303 return 3; /* return error */
304 }
305
306 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
307 if (res != 0) /* check result */
308 {
309 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
310
311 return 1; /* return error */
312 }
313 prev &= ~(1 << 7); /* clear boot mode bit */
314 prev |= mode << 7; /* set boot mode */
315
316 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write config */
317}
318
331{
332 uint8_t res;
333 uint8_t prev;
334
335 if (handle == NULL) /* check handle */
336 {
337 return 2; /* return error */
338 }
339 if (handle->inited != 1) /* check handle initialization */
340 {
341 return 3; /* return error */
342 }
343
344 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
345 if (res != 0) /* check result */
346 {
347 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
348
349 return 1; /* return error */
350 }
351 *mode = (uvis25_boot_mode_t)((prev >> 7) & 0x01); /* get mode */
352
353 return 0; /* success return 0 */
354}
355
368{
369 uint8_t res;
370 uint8_t prev;
371
372 if (handle == NULL) /* check handle */
373 {
374 return 2; /* return error */
375 }
376 if (handle->inited != 1) /* check handle initialization */
377 {
378 return 3; /* return error */
379 }
380
381 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
382 if (res != 0) /* check result */
383 {
384 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
385
386 return 1; /* return error */
387 }
388 prev &= ~(1 << 4); /* clear iic enable bit */
389 prev |= (!enable) << 4; /* set iic enable */
390
391 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write config */
392}
393
406{
407 uint8_t res;
408 uint8_t prev;
409
410 if (handle == NULL) /* check handle */
411 {
412 return 2; /* return error */
413 }
414 if (handle->inited != 1) /* check handle initialization */
415 {
416 return 3; /* return error */
417 }
418
419 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
420 if (res != 0) /* check result */
421 {
422 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
423
424 return 1; /* return error */
425 }
426 *enable = (uvis25_bool_t)!((prev >> 4) & 0x01); /* get bool */
427
428 return 0; /* success return 0 */
429}
430
443{
444 uint8_t res;
445 uint8_t prev;
446
447 if (handle == NULL) /* check handle */
448 {
449 return 2; /* return error */
450 }
451 if (handle->inited != 1) /* check handle initialization */
452 {
453 return 3; /* return error */
454 }
455
456 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
457 if (res != 0) /* check result */
458 {
459 handle->debug_print("uvis25: read register failed.\n"); /* read register failed */
460
461 return 1; /* return error */
462 }
463 prev &= ~(1 << 3); /* clear wire bit */
464 prev |= wire << 3; /* set wire */
465
466 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write config */
467}
468
481{
482 uint8_t res;
483 uint8_t prev;
484
485 if (handle == NULL) /* check handle */
486 {
487 return 2; /* return error */
488 }
489 if (handle->inited != 1) /* check handle initialization */
490 {
491 return 3; /* return error */
492 }
493
494 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
495 if (res != 0) /* check result */
496 {
497 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
498
499 return 1; /* return error */
500 }
501 *wire = (uvis25_spi_wire_t)((prev>>3) & 0x01); /* get bool */
502
503 return 0; /* success return 0 */
504}
505
518{
519 uint8_t res;
520 uint8_t prev;
521
522 if (handle == NULL) /* check handle */
523 {
524 return 2; /* return error */
525 }
526 if (handle->inited != 1) /* check handle initialization */
527 {
528 return 3; /* return error */
529 }
530
531 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
532 if (res != 0) /* check result */
533 {
534 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
535
536 return 1; /* return error */
537 }
538 prev &= ~(1 << 7); /* clear level bit */
539 prev |= level << 7; /* set level */
540
541 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* write config */
542}
543
556{
557 uint8_t res;
558 uint8_t prev;
559
560 if (handle == NULL) /* check handle */
561 {
562 return 2; /* return error */
563 }
564 if (handle->inited != 1) /* check handle initialization */
565 {
566 return 3; /* return error */
567 }
568
569 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
570 if (res != 0) /* check result */
571 {
572 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
573
574 return 1; /* return error */
575 }
576 *level = (uvis25_interrupt_active_level_t)((prev >> 7) & 0x01); /* get bool */
577
578 return 0; /* success return 0 */
579}
580
593{
594 uint8_t res;
595 uint8_t prev;
596
597 if (handle == NULL) /* check handle */
598 {
599 return 2; /* return error */
600 }
601 if (handle->inited != 1) /* check handle initialization */
602 {
603 return 3; /* return error */
604 }
605
606 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
607 if (res != 0) /* check result */
608 {
609 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
610
611 return 1; /* return error */
612 }
613 prev &= ~(1 << 6); /* clear pin type bit */
614 prev |= pin_type << 6; /* set pin type */
615
616 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* write config */
617}
618
631{
632 uint8_t res;
633 uint8_t prev;
634
635 if (handle == NULL) /* check handle */
636 {
637 return 2; /* return error */
638 }
639 if (handle->inited != 1) /* check handle initialization */
640 {
641 return 3; /* return error */
642 }
643
644 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
645 if (res != 0) /* check result */
646 {
647 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
648
649 return 1; /* return error */
650 }
651 *pin_type = (uvis25_interrupt_pin_type_t)((prev >> 6) & 0x01); /* get bool */
652
653 return 0; /* success return 0 */
654}
655
668{
669 uint8_t res;
670 uint8_t prev;
671
672 if (handle == NULL) /* check handle */
673 {
674 return 2; /* return error */
675 }
676 if (handle->inited != 1) /* check handle initialization */
677 {
678 return 3; /* return error */
679 }
680
681 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
682 if (res != 0) /* check result */
683 {
684 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
685
686 return 1; /* return error */
687 }
688 prev &= ~(0x03 << 0); /* clear interrupt type bits */
689 prev |= type << 0; /* set interrupt type */
690
691 return a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* write config */
692}
693
706{
707 uint8_t res;
708 uint8_t prev;
709
710 if (handle == NULL) /* check handle */
711 {
712 return 2; /* return error */
713 }
714 if (handle->inited != 1) /* check handle initialization */
715 {
716 return 3; /* return error */
717 }
718
719 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG3, (uint8_t *)&prev, 1); /* read ctrl reg3 */
720 if (res != 0) /* check result */
721 {
722 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
723
724 return 1; /* return error */
725 }
726 *type = (uvis25_interrupt_type_t)((prev >> 0) & 0x03); /* get interrupt type */
727
728 return 0; /* success return 0 */
729}
730
743{
744 uint8_t res;
745 uint8_t prev;
746
747 if (handle == NULL) /* check handle */
748 {
749 return 2; /* return error */
750 }
751 if (handle->inited != 1) /* check handle initialization */
752 {
753 return 3; /* return error */
754 }
755
756 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
757 if (res != 0) /* check result */
758 {
759 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
760
761 return 1; /* return error */
762 }
763 prev &= ~(0x01 << 3); /* clear interrupt enable bit */
764 prev |= enable << 3; /* set interrupt enable */
765
766 return a_uvis25_iic_spi_write(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* write config */
767}
768
781{
782 uint8_t res;
783 uint8_t prev;
784
785 if (handle == NULL) /* check handle */
786 {
787 return 2; /* return error */
788 }
789 if (handle->inited != 1) /* check handle initialization */
790 {
791 return 3; /* return error */
792 }
793
794 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
795 if (res != 0) /* check result */
796 {
797 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
798
799 return 1; /* return error */
800 }
801 *enable = (uvis25_bool_t)((prev >> 3) & 0x01); /* get value */
802
803 return 0; /* success return 0 */
804}
805
818{
819 uint8_t res;
820 uint8_t prev;
821
822 if (handle == NULL) /* check handle */
823 {
824 return 2; /* return error */
825 }
826 if (handle->inited != 1) /* check handle initialization */
827 {
828 return 3; /* return error */
829 }
830
831 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
832 if (res != 0) /* check result */
833 {
834 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
835
836 return 1; /* return error */
837 }
838 prev &= ~(0x01 << 2); /* clear latch interrupt enable bit */
839 prev |= enable << 2; /* set latch interrupt enable */
840
841 return a_uvis25_iic_spi_write(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* write config */
842}
843
856{
857 uint8_t res;
858 uint8_t prev;
859
860 if (handle == NULL) /* check handle */
861 {
862 return 2; /* return error */
863 }
864 if (handle->inited != 1) /* check handle initialization */
865 {
866 return 3; /* return error */
867 }
868
869 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
870 if (res != 0) /* check result */
871 {
872 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
873
874 return 1; /* return error */
875 }
876 *enable = (uvis25_bool_t)((prev >> 2) & 0x01); /* get value */
877
878 return 0; /* success return 0 */
879}
880
893{
894 uint8_t res;
895 uint8_t prev;
896
897 if (handle == NULL) /* check handle */
898 {
899 return 2; /* return error */
900 }
901 if (handle->inited != 1) /* check handle initialization */
902 {
903 return 3; /* return error */
904 }
905
906 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
907 if (res != 0) /* check result */
908 {
909 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
910
911 return 1; /* return error */
912 }
913 prev &= ~(0x01 << 1); /* clear interrupt low threshold bit */
914 prev |= enable << 1; /* set interrupt low threshold */
915
916 return a_uvis25_iic_spi_write(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* write config */
917}
918
931{
932 uint8_t res;
933 uint8_t prev;
934
935 if (handle == NULL) /* check handle */
936 {
937 return 2; /* return error */
938 }
939 if (handle->inited != 1) /* check handle initialization */
940 {
941 return 3; /* return error */
942 }
943
944 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
945 if (res != 0) /* check result */
946 {
947 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
948
949 return 1; /* return error */
950 }
951 *enable = (uvis25_bool_t)((prev >> 1) & 0x01); /* get value */
952
953 return 0; /* success return 0 */
954}
955
968{
969 uint8_t res;
970 uint8_t prev;
971
972 if (handle == NULL) /* check handle */
973 {
974 return 2; /* return error */
975 }
976 if (handle->inited != 1) /* check handle initialization */
977 {
978 return 3; /* return error */
979 }
980
981 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
982 if (res != 0) /* check result */
983 {
984 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
985
986 return 1; /* return error */
987 }
988 prev &= ~(0x01 << 0); /* clear interrupt high threshold enable bit */
989 prev |= enable << 0; /* set interrupt high threshold enable */
990
991 return a_uvis25_iic_spi_write(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* write config */
992}
993
1006{
1007 uint8_t res;
1008 uint8_t prev;
1009
1010 if (handle == NULL) /* check handle */
1011 {
1012 return 2; /* return error */
1013 }
1014 if (handle->inited != 1) /* check handle initialization */
1015 {
1016 return 3; /* return error */
1017 }
1018
1019 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_CFG, (uint8_t *)&prev, 1); /* read int cfg */
1020 if (res != 0) /* check result */
1021 {
1022 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1023
1024 return 1; /* return error */
1025 }
1026 *enable = (uvis25_bool_t)((prev >> 0) & 0x01); /* get value */
1027
1028 return 0; /* success return 0 */
1029}
1030
1042uint8_t uvis25_set_threshold(uvis25_handle_t *handle, uint8_t threshold)
1043{
1044 if (handle == NULL) /* check handle */
1045 {
1046 return 2; /* return error */
1047 }
1048 if (handle->inited != 1) /* check handle initialization */
1049 {
1050 return 3; /* return error */
1051 }
1052
1053 return a_uvis25_iic_spi_write(handle, UVIS25_REG_THS_UV, (uint8_t *)&threshold, 1); /* write threshold */
1054}
1055
1067uint8_t uvis25_get_threshold(uvis25_handle_t *handle, uint8_t *threshold)
1068{
1069 if (handle == NULL) /* check handle */
1070 {
1071 return 2; /* return error */
1072 }
1073 if (handle->inited != 1) /* check handle initialization */
1074 {
1075 return 3; /* return error */
1076 }
1077
1078 return a_uvis25_iic_spi_read(handle, UVIS25_REG_THS_UV, (uint8_t *)threshold, 1); /* read threshold */
1079}
1080
1092{
1093 uint8_t res, prev;
1094
1095 if (handle == NULL) /* check handle */
1096 {
1097 return 2; /* return error */
1098 }
1099 if (handle->inited != 1) /* check handle initialization */
1100 {
1101 return 3; /* return error */
1102 }
1103
1104 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_INT_SOURCE, (uint8_t *)&prev, 1); /* read int source */
1105 if (res != 0) /* check result */
1106 {
1107 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1108
1109 return 1; /* return error */
1110 }
1111 if ((prev & (1 << 2)) != 0) /* active */
1112 {
1113 if (handle->receive_callback != NULL) /* check the callback */
1114 {
1115 handle->receive_callback(UVIS25_INTERRUPT_ACTIVE); /* run callback */
1116 }
1117 }
1118 if ((prev & (1 << 1)) != 0) /* interrupt lower */
1119 {
1120 if (handle->receive_callback != NULL) /* check the callback */
1121 {
1122 handle->receive_callback(UVIS25_INTERRUPT_LOWER); /* run callback */
1123 }
1124 }
1125 if ((prev & (1 << 0)) != 0) /* interrupt higher */
1126 {
1127 if (handle->receive_callback != NULL) /* check the callback */
1128 {
1129 handle->receive_callback(UVIS25_INTERRUPT_HIGHER); /* run callback */
1130 }
1131 }
1132
1133 return 0; /* success return 0 */
1134}
1135
1148uint8_t uvis25_threshold_convert_to_register(uvis25_handle_t *handle, float uv, uint8_t *reg)
1149{
1150 if (handle == NULL) /* check handle */
1151 {
1152 return 2; /* return error */
1153 }
1154 if (handle->inited != 1) /* check handle initialization */
1155 {
1156 return 3; /* return error */
1157 }
1158
1159 *reg = (uint8_t )(uv * 16.0f); /* convert real data to register data */
1160
1161 return 0; /* success return 0 */
1162}
1163
1176uint8_t uvis25_threshold_convert_to_data(uvis25_handle_t *handle, uint8_t reg, float *uv)
1177{
1178 if (handle == NULL) /* check handle */
1179 {
1180 return 2; /* return error */
1181 }
1182 if (handle->inited != 1) /* check handle initialization */
1183 {
1184 return 3; /* return error */
1185 }
1186
1187 *uv = (float)reg / 16.0f; /* convert raw data to real data */
1188
1189 return 0; /* success return 0 */
1190}
1191
1203{
1204 uint8_t id;
1205
1206 if (handle == NULL) /* check handle */
1207 {
1208 return 2; /* return error */
1209 }
1210 if (handle->debug_print == NULL) /* check debug_print */
1211 {
1212 return 3; /* return error */
1213 }
1214 if (handle->iic_init == NULL) /* check iic_init */
1215 {
1216 handle->debug_print("uvis25: iic_init is null.\n"); /* iic_init is null */
1217
1218 return 3; /* return error */
1219 }
1220 if (handle->iic_deinit == NULL) /* check iic_deinit */
1221 {
1222 handle->debug_print("uvis25: iic_deinit is null.\n"); /* iic_deinit is null */
1223
1224 return 3; /* return error */
1225 }
1226 if (handle->iic_read == NULL) /* check iic_read */
1227 {
1228 handle->debug_print("uvis25: iic_read is null.\n"); /* iic_read is null */
1229
1230 return 3; /* return error */
1231 }
1232 if (handle->iic_write == NULL) /* check iic_write */
1233 {
1234 handle->debug_print("uvis25: iic_write is null.\n"); /* iic_write is null */
1235
1236 return 3; /* return error */
1237 }
1238 if (handle->spi_init == NULL) /* check spi_init */
1239 {
1240 handle->debug_print("uvis25: spi_init is null.\n"); /* spi_init is null */
1241
1242 return 3; /* return error */
1243 }
1244 if (handle->spi_deinit == NULL) /* check spi_deinit */
1245 {
1246 handle->debug_print("uvis25: spi_deinit is null.\n"); /* spi_deinit is null */
1247
1248 return 3; /* return error */
1249 }
1250 if (handle->spi_read == NULL) /* check spi_read */
1251 {
1252 handle->debug_print("uvis25: spi_read is null.\n"); /* spi_read is null */
1253
1254 return 3; /* return error */
1255 }
1256 if (handle->spi_write == NULL) /* check spi_write */
1257 {
1258 handle->debug_print("uvis25: spi_write is null.\n"); /* spi_write is null */
1259
1260 return 3; /* return error */
1261 }
1262 if (handle->delay_ms == NULL) /* check delay_ms */
1263 {
1264 handle->debug_print("uvis25: delay_ms is null.\n"); /* delay_ms is null */
1265
1266 return 3; /* return error */
1267 }
1268
1269 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* iic interface */
1270 {
1271 if (handle->iic_init() != 0) /* iic init */
1272 {
1273 handle->debug_print("uvis25: iic init failed.\n"); /* iic init failed */
1274
1275 return 1; /* return error */
1276 }
1277 }
1278 else /* spi interface */
1279 {
1280 if (handle->spi_init() != 0) /* spi init */
1281 {
1282 handle->debug_print("uvis25: spi init failed.\n"); /* spi init failed */
1283
1284 return 1; /* return error */
1285 }
1286 }
1287 if (a_uvis25_iic_spi_read(handle, UVIS25_REG_WHO_AM_I, (uint8_t *)&id, 1) != 0) /* read id */
1288 {
1289 handle->debug_print("uvis25: read id failed.\n"); /* read id failed */
1290 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* id iic interface */
1291 {
1292 (void)handle->iic_deinit(); /* iic deinit */
1293 }
1294 else
1295 {
1296 (void)handle->spi_deinit(); /* spi deinit */
1297 }
1298
1299 return 1; /* return error */
1300 }
1301 if (id != 0xCA) /* check id */
1302 {
1303 handle->debug_print("uvis25: id is invalid.\n"); /* id is invalid */
1304 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* if iic interface */
1305 {
1306 (void)handle->iic_deinit(); /* iic deinit */
1307 }
1308 else
1309 {
1310 (void)handle->spi_deinit(); /* spi deinit */
1311 }
1312
1313 return 1; /* return error */
1314 }
1315 handle->inited = 1; /* flag finish initialization */
1316
1317 return 0; /* success return 0 */
1318}
1319
1331{
1332 uint8_t res;
1333 uint8_t prev;
1334
1335 if (handle == NULL) /* check handle */
1336 {
1337 return 2; /* return error */
1338 }
1339 if (handle->inited != 1) /* check handle initialization */
1340 {
1341 return 3; /* return error */
1342 }
1343
1344 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
1345 if (res != 0) /* check result */
1346 {
1347 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1348
1349 return 1; /* return error */
1350 }
1351 prev &= ~(0x01 << 7); /* clear reboot bit */
1352 prev |= 1 << 7; /* set reboot */
1353 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write config */
1354 if (res != 0) /* check result */
1355 {
1356 handle->debug_print("uvis25: write register failed.\n"); /* write ctrl reg2 failed */
1357
1358 return 1; /* return error */
1359 }
1360 if (handle->iic_spi == UVIS25_INTERFACE_IIC) /* iic interface */
1361 {
1362 res = handle->iic_deinit(); /* iic deinit */
1363 if (res != 0) /* check result */
1364 {
1365 handle->debug_print("uvis25: iic deinit failed.\n"); /* iic deinit failed */
1366
1367 return 1; /* return error */
1368 }
1369 }
1370 else /* spi interface */
1371 {
1372 res = handle->spi_deinit(); /* spi deinit */
1373 if (res != 0) /* check result */
1374 {
1375 handle->debug_print("uvis25: spi deinit failed.\n"); /* spi deinit */
1376
1377 return 1; /* return error */
1378 }
1379 }
1380 handle->inited = 0; /* flag close */
1381
1382 return 0; /* success return 0 */
1383}
1384
1397uint8_t uvis25_single_read(uvis25_handle_t *handle, uint8_t *raw, float *uv)
1398{
1399 uint8_t res;
1400 uint8_t prev;
1401 uint8_t timeout;
1402
1403 if (handle == NULL) /* check handle */
1404 {
1405 return 2; /* return error */
1406 }
1407 if (handle->inited != 1) /* check handle initialization */
1408 {
1409 return 3; /* return error */
1410 }
1411
1412 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* read ctrl reg1 */
1413 if (res != 0) /* check result */
1414 {
1415 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1416
1417 return 1; /* return error */
1418 }
1419 prev &= ~(1 << 0); /* disable continuous reading */
1420 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* write ctrl reg1 */
1421 if (res != 0) /* check result */
1422 {
1423 handle->debug_print("uvis25: write register failed.\n"); /* write failed */
1424
1425 return 1; /* return error */
1426 }
1427 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
1428 if (res != 0) /* check result */
1429 {
1430 handle->debug_print("uvis25: read register failed.\n"); /* read ctrl reg2 failed */
1431
1432 return 1; /* return error */
1433 }
1434 prev &= ~(1 << 0); /* clear one shot bit */
1435 prev |= 1 << 0; /* set one shot */
1436 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write reg2 */
1437 if (res != 0) /* check result */
1438 {
1439 handle->debug_print("uvis25: write register failed.\n"); /* write ctrl reg2 failed */
1440
1441 return 1; /* return error */
1442 }
1443 timeout = 0; /* reset timeout times */
1444
1445 while (1) /* loop */
1446 {
1447 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_STATUS_REG, (uint8_t *)&prev, 1); /* read status */
1448 if (res != 0) /* check result */
1449 {
1450 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1451
1452 return 1; /* return error */
1453 }
1454 if ((prev & 0x01) != 0) /* if data ready */
1455 {
1456 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_UV_OUT_REG, (uint8_t *)raw, 1); /* read uv out */
1457 if (res != 0) /* check result */
1458 {
1459 handle->debug_print("uvis25: read failed.\n"); /* read failed */
1460
1461 return 1; /* return error */
1462 }
1463 *uv = (float)(*raw) / 16.0f; /* convert raw */
1464
1465 return 0; /* success return 0 */
1466 }
1467 else if (timeout < 100) /* if not timeout */
1468 {
1469 handle->delay_ms(100); /* delay 100 ms */
1470 timeout++; /* retry times++ */
1471
1472 continue; /* continue */
1473 }
1474 else
1475 {
1476 handle->debug_print("uvis25: data is not available.\n"); /* data is not available */
1477
1478 return 1; /* return error */
1479 }
1480 }
1481}
1482
1494{
1495 uint8_t res;
1496 uint8_t prev;
1497
1498 if (handle == NULL) /* check handle */
1499 {
1500 return 2; /* return error */
1501 }
1502 if (handle->inited != 1) /* check handle initialization */
1503 {
1504 return 3; /* return error */
1505 }
1506
1507 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* read ctrl reg1 */
1508 if (res != 0) /* check result */
1509 {
1510 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1511
1512 return 1; /* return error */
1513 }
1514 prev &= ~(1 << 0); /* clear continuous reading */
1515 prev |= 1 << 0; /* enable continuous reading */
1516 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* write ctrl reg1 */
1517 if (res != 0) /* check result */
1518 {
1519 handle->debug_print("uvis25: write register failed.\n"); /* write failed */
1520
1521 return 1; /* return error */
1522 }
1523 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
1524 if (res != 0) /* check result */
1525 {
1526 handle->debug_print("uvis25: read register failed.\n"); /* read ctrl reg2 failed */
1527
1528 return 1; /* return error */
1529 }
1530 prev &= ~(1 << 0); /* clear one shot bit */
1531 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write reg2 */
1532 if (res != 0) /* check result */
1533 {
1534 handle->debug_print("uvis25: write register failed.\n"); /* write ctrl reg2 failed */
1535
1536 return 1; /* return error */
1537 }
1538
1539 return 0; /* success return 0 */
1540}
1541
1553{
1554 uint8_t res;
1555 uint8_t prev;
1556
1557 if (handle == NULL) /* check handle */
1558 {
1559 return 2; /* return error */
1560 }
1561 if (handle->inited != 1) /* check handle initialization */
1562 {
1563 return 3; /* return error */
1564 }
1565
1566 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* read ctrl reg1 */
1567 if (res != 0) /* check result */
1568 {
1569 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1570
1571 return 1; /* return error */
1572 }
1573 prev &= ~(1 << 0); /* clear continuous reading */
1574 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG1, (uint8_t *)&prev, 1); /* write ctrl reg1 */
1575 if (res != 0) /* check result */
1576 {
1577 handle->debug_print("uvis25: write register failed.\n"); /* write failed */
1578
1579 return 1; /* return error */
1580 }
1581 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* read ctrl reg2 */
1582 if (res != 0) /* check result */
1583 {
1584 handle->debug_print("uvis25: read register failed.\n"); /* read ctrl reg2 failed */
1585
1586 return 1; /* return error */
1587 }
1588 prev &= ~(1 << 0); /* clear one shot bit */
1589 res = a_uvis25_iic_spi_write(handle, UVIS25_REG_CTRL_REG2, (uint8_t *)&prev, 1); /* write reg2 */
1590 if (res != 0) /* check result */
1591 {
1592 handle->debug_print("uvis25: write register failed.\n"); /* write ctrl reg2 failed */
1593
1594 return 1; /* return error */
1595 }
1596
1597 return 0; /* success return 0 */
1598}
1599
1612uint8_t uvis25_continuous_read(uvis25_handle_t *handle, uint8_t *raw, float *uv)
1613{
1614 uint8_t res;
1615 uint8_t prev;
1616
1617 if (handle == NULL) /* check handle */
1618 {
1619 return 2; /* return error */
1620 }
1621 if (handle->inited != 1) /* check handle initialization */
1622 {
1623 return 3; /* return error */
1624 }
1625
1626 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_STATUS_REG, (uint8_t *)&prev, 1); /* read status failed */
1627 if (res != 0) /* check result */
1628 {
1629 handle->debug_print("uvis25: read register failed.\n"); /* read failed */
1630
1631 return 1; /* return error */
1632 }
1633 if ((prev & 0x01) != 0) /* check status */
1634 {
1635 res = a_uvis25_iic_spi_read(handle, UVIS25_REG_UV_OUT_REG, (uint8_t *)raw, 1); /* read uv out reg */
1636 if (res != 0) /* check result */
1637 {
1638 handle->debug_print("uvis25: read failed.\n"); /* read uv out failed */
1639
1640 return 1; /* return error */
1641 }
1642 *uv = (float)(*raw) / 16.0f; /* convert raw data */
1643
1644 return 0; /* success return 0 */
1645 }
1646 else
1647 {
1648 handle->debug_print("uvis25: data is not available.\n"); /* data is not available */
1649
1650 return 1; /* return error */
1651 }
1652}
1653
1667uint8_t uvis25_set_reg(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1668{
1669 if (handle == NULL) /* check handle */
1670 {
1671 return 2; /* return error */
1672 }
1673 if (handle->inited != 1) /* check handle initialization */
1674 {
1675 return 3; /* return error */
1676 }
1677
1678 return a_uvis25_iic_spi_write(handle, reg, buf, len); /* write data */
1679}
1680
1694uint8_t uvis25_get_reg(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1695{
1696 if (handle == NULL) /* check handle */
1697 {
1698 return 2; /* return error */
1699 }
1700 if (handle->inited != 1) /* check handle initialization */
1701 {
1702 return 3; /* return error */
1703 }
1704
1705 return a_uvis25_iic_spi_read(handle, reg, buf, len); /* read data */
1706}
1707
1717{
1718 if (info == NULL) /* check handle */
1719 {
1720 return 2; /* return error */
1721 }
1722
1723 memset(info, 0, sizeof(uvis25_info_t)); /* initialize uvis25 info structure */
1724 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1725 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1726 strncpy(info->interface, "IIC SPI", 8); /* copy interface name */
1727 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1728 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1729 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1730 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1731 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1732 info->driver_version = DRIVER_VERSION; /* set driver version */
1733
1734 return 0; /* success return 0 */
1735}
#define UVIS25_REG_INT_SOURCE
#define UVIS25_REG_THS_UV
#define UVIS25_REG_UV_OUT_REG
#define MAX_CURRENT
#define UVIS25_REG_STATUS_REG
#define UVIS25_REG_CTRL_REG3
#define SUPPLY_VOLTAGE_MAX
#define UVIS25_REG_CTRL_REG1
#define UVIS25_REG_CTRL_REG2
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip register definition
#define UVIS25_IIC_ADDRESS
iic address definition
#define DRIVER_VERSION
#define UVIS25_REG_WHO_AM_I
chip register definition
#define UVIS25_REG_INT_CFG
driver uvis25 header file
uvis25_interface_t
uvis25 interface enumeration definition
uint8_t uvis25_get_iic(uvis25_handle_t *handle, uvis25_bool_t *enable)
get the chip iic status
uint8_t uvis25_set_boot(uvis25_handle_t *handle, uvis25_boot_mode_t mode)
set the boot mode
uint8_t uvis25_set_spi_wire(uvis25_handle_t *handle, uvis25_spi_wire_t wire)
set the spi wire
uint8_t uvis25_init(uvis25_handle_t *handle)
initialize the chip
uint8_t uvis25_set_iic(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable the chip iic
uint8_t uvis25_continuous_read(uvis25_handle_t *handle, uint8_t *raw, float *uv)
read data continuously
uint8_t uvis25_deinit(uvis25_handle_t *handle)
close the chip
uint8_t uvis25_irq_handler(uvis25_handle_t *handle)
irq handler
uint8_t uvis25_set_interface(uvis25_handle_t *handle, uvis25_interface_t interface)
set the chip interface
uint8_t uvis25_get_boot(uvis25_handle_t *handle, uvis25_boot_mode_t *mode)
get the boot mode
uvis25_boot_mode_t
uvis25 boot enumeration definition
uint8_t uvis25_get_interface(uvis25_handle_t *handle, uvis25_interface_t *interface)
get the chip interface
uint8_t uvis25_single_read(uvis25_handle_t *handle, uint8_t *raw, float *uv)
read data once
struct uvis25_info_s uvis25_info_t
uvis25 information structure definition
struct uvis25_handle_s uvis25_handle_t
uvis25 handle structure definition
uint8_t uvis25_info(uvis25_info_t *info)
get chip's information
uvis25_spi_wire_t
uvis25 spi wire enumeration definition
uint8_t uvis25_get_block_data_update(uvis25_handle_t *handle, uvis25_bool_t *enable)
get blocking data update status
uint8_t uvis25_set_block_data_update(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable blocking data update
uvis25_bool_t
uvis25 bool enumeration definition
uint8_t uvis25_start_continuous_read(uvis25_handle_t *handle)
start reading
uint8_t uvis25_stop_continuous_read(uvis25_handle_t *handle)
stop reading
uint8_t uvis25_get_spi_wire(uvis25_handle_t *handle, uvis25_spi_wire_t *wire)
get the spi wire
@ UVIS25_INTERFACE_IIC
uint8_t uvis25_set_reg(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t uvis25_get_reg(uvis25_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t uvis25_get_threshold(uvis25_handle_t *handle, uint8_t *threshold)
get the interrupt threshold
uint8_t uvis25_set_interrupt_low_threshold(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable the low threshold interrupt
uvis25_interrupt_pin_type_t
uvis25 interrupt pin type enumeration definition
uint8_t uvis25_set_interrupt_high_threshold(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable the high threshold interrupt
uint8_t uvis25_get_interrupt_type(uvis25_handle_t *handle, uvis25_interrupt_type_t *type)
get the interrupt type
uint8_t uvis25_get_interrupt_low_threshold(uvis25_handle_t *handle, uvis25_bool_t *enable)
get the low threshold interrupt status
uint8_t uvis25_set_interrupt_pin_type(uvis25_handle_t *handle, uvis25_interrupt_pin_type_t pin_type)
set the interrupt pin type
uint8_t uvis25_set_threshold(uvis25_handle_t *handle, uint8_t threshold)
set the interrupt threshold
uvis25_interrupt_active_level_t
uvis25 interrupt active level enumeration definition
uint8_t uvis25_set_interrupt_active_level(uvis25_handle_t *handle, uvis25_interrupt_active_level_t level)
set the interrupt active level
uint8_t uvis25_get_latch_interrupt(uvis25_handle_t *handle, uvis25_bool_t *enable)
get the latching interrupt status
uint8_t uvis25_threshold_convert_to_register(uvis25_handle_t *handle, float uv, uint8_t *reg)
convert a uv index to a raw register data
uint8_t uvis25_get_interrupt_pin_type(uvis25_handle_t *handle, uvis25_interrupt_pin_type_t *pin_type)
get the interrupt pin type
uint8_t uvis25_get_interrupt_high_threshold(uvis25_handle_t *handle, uvis25_bool_t *enable)
get the high threshold interrupt status
uint8_t uvis25_set_latch_interrupt(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable latching interrupt
uint8_t uvis25_get_interrupt(uvis25_handle_t *handle, uvis25_bool_t *enable)
get the chip interrupt status
uint8_t uvis25_set_interrupt_type(uvis25_handle_t *handle, uvis25_interrupt_type_t type)
set the interrupt type
uint8_t uvis25_threshold_convert_to_data(uvis25_handle_t *handle, uint8_t reg, float *uv)
convert a raw register data to a converted uv index
uint8_t uvis25_set_interrupt(uvis25_handle_t *handle, uvis25_bool_t enable)
enable or disable the chip interrupt
uvis25_interrupt_type_t
uvis25 interrupt type enumeration definition
uint8_t uvis25_get_interrupt_active_level(uvis25_handle_t *handle, uvis25_interrupt_active_level_t *level)
get the interrupt active level
@ UVIS25_INTERRUPT_LOWER
@ UVIS25_INTERRUPT_HIGHER
@ UVIS25_INTERRUPT_ACTIVE
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* receive_callback)(uint8_t type)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* spi_deinit)(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)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]