LibDriver TCS34725
Loading...
Searching...
No Matches
driver_tcs34725.c
Go to the documentation of this file.
1
37
38#include "driver_tcs34725.h"
39
43#define CHIP_NAME "AMS TCS34725"
44#define MANUFACTURER_NAME "AMS"
45#define SUPPLY_VOLTAGE_MIN 2.7f
46#define SUPPLY_VOLTAGE_MAX 3.6f
47#define MAX_CURRENT 20.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define TCS34725_REG_ENABLE 0x80
56#define TCS34725_REG_ATIME 0x81
57#define TCS34725_REG_WTIME 0x83
58#define TCS34725_REG_AILTL 0xA4
59#define TCS34725_REG_AILTH 0xA5
60#define TCS34725_REG_AIHTL 0xA6
61#define TCS34725_REG_AIHTH 0xA7
62#define TCS34725_REG_PERS 0x8C
63#define TCS34725_REG_CONFIG 0x8D
64#define TCS34725_REG_CONTROL 0x8F
65#define TCS34725_REG_ID 0x92
66#define TCS34725_REG_STATUS 0x93
67#define TCS34725_REG_CDATAL 0xB4
68#define TCS34725_REG_CDATAH 0xB5
69#define TCS34725_REG_RDATAL 0xB6
70#define TCS34725_REG_RDATAH 0xB7
71#define TCS34725_REG_GDATAL 0xB8
72#define TCS34725_REG_GDATAH 0xB9
73#define TCS34725_REG_BDATAL 0xBA
74#define TCS34725_REG_BDATAH 0xBB
75#define TCS34725_REG_CLEAR 0xE6
76
80#define TCS34725_ADDRESS (0x29 << 1)
81
94{
95 uint8_t res, prev;
96
97 if (handle == NULL) /* check handle */
98 {
99 return 2; /* return error */
100 }
101 if (handle->inited != 1) /* check handle initialization */
102 {
103 return 3; /* return error */
104 }
105
106 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable config */
107 if (res != 0) /* check the result */
108 {
109 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
110
111 return 1; /* return error */
112 }
113 prev &= ~(1 << 4); /* clear interrupt */
114 prev |= enable << 4; /* set enable */
115 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* write config */
116 if (res != 0) /* check the result */
117 {
118 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
119
120 return 1; /* return error */
121 }
122
123 return 0; /* success return 0 */
124}
125
138{
139 uint8_t res, prev;
140
141 if (handle == NULL) /* check handle */
142 {
143 return 2; /* return error */
144 }
145 if (handle->inited != 1) /* check handle initialization */
146 {
147 return 3; /* return error */
148 }
149
150 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable config */
151 if (res != 0) /* check the result */
152 {
153 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
154
155 return 1; /* return error */
156 }
157 prev &= 1 << 4; /* get interrupt */
158 *enable = (tcs34725_bool_t)((prev >> 4) & 0x01); /* set interrupt */
159
160 return 0; /* success return 0 */
161}
162
175{
176 uint8_t res, prev;
177
178 if (handle == NULL) /* check handle */
179 {
180 return 2; /* return error */
181 }
182 if (handle->inited != 1) /* check handle initialization */
183 {
184 return 3; /* return error */
185 }
186
187 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable config */
188 if (res != 0) /* check result */
189 {
190 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
191
192 return 1; /* return error */
193 }
194 prev &= ~(1 << 3); /* clear enable bit */
195 prev |= enable << 3; /* set enable */
196 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* write config */
197 if (res != 0) /* check the result */
198 {
199 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
200
201 return 1; /* return error */
202 }
203
204 return 0; /* success return 0 */
205}
206
219{
220 uint8_t res, 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 = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read config */
232 if (res != 0) /* check result */
233 {
234 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
235
236 return 1; /* return error */
237 }
238 prev &= 1 << 3; /* get wait bit */
239 *enable = (tcs34725_bool_t)((prev >> 3) & 0x01); /* get wait */
240
241 return 0; /* success return 0 */
242}
243
256{
257 uint8_t res, prev;
258
259 if (handle == NULL) /* check handle */
260 {
261 return 2; /* return error */
262 }
263 if (handle->inited != 1) /* check handle initialization */
264 {
265 return 3; /* return error */
266 }
267
268 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read config */
269 if (res != 0) /* check result */
270 {
271 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
272
273 return 1; /* return error */
274 }
275 prev &= ~(1 << 1); /* clear enable bit */
276 prev |= enable << 1; /* set enable */
277 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* write config */
278 if (res != 0) /* check the result */
279 {
280 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
281
282 return 1; /* return error */
283 }
284
285 return 0; /* success return 0 */
286}
287
300{
301 uint8_t res, prev;
302
303 if (handle == NULL) /* check handle */
304 {
305 return 2; /* return error */
306 }
307 if (handle->inited != 1) /* check handle initialization */
308 {
309 return 3; /* return error */
310 }
311
312 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable config */
313 if (res != 0) /* check result */
314 {
315 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
316
317 return 1; /* return error */
318 }
319 prev &= 1 << 1; /* get rgbc bit */
320 *enable = (tcs34725_bool_t)((prev >> 1) & 0x01); /* get enable */
321
322 return 0; /* success return 0 */
323}
324
337{
338 uint8_t res, prev;
339
340 if (handle == NULL) /* check handle */
341 {
342 return 2; /* return error */
343 }
344 if (handle->inited != 1) /* check handle initialization */
345 {
346 return 3; /* return error */
347 }
348
349 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read config */
350 if (res != 0) /* check result */
351 {
352 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
353
354 return 1; /* return error */
355 }
356 prev &= ~(1 << 0); /* clear enable bit */
357 prev |= enable << 0; /* set enable */
358 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* write config */
359 if (res != 0) /* check the result */
360 {
361 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
362
363 return 1; /* return error */
364 }
365
366 return 0; /* success return 0 */
367}
368
381{
382 uint8_t res, prev;
383
384 if (handle == NULL) /* check handle */
385 {
386 return 2; /* return error */
387 }
388 if (handle->inited != 1) /* check handle initialization */
389 {
390 return 3; /* return error */
391 }
392
393 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable config */
394 if (res != 0) /* check result */
395 {
396 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
397
398 return 1; /* return error */
399 }
400 prev &= 1 << 0; /* get enable bit */
401 *enable = (tcs34725_bool_t)((prev >> 0) & 0x01); /* get enable */
402
403 return 0; /* success return 0 */
404}
405
418{
419 uint8_t res;
420
421 if (handle == NULL) /* check handle */
422 {
423 return 2; /* return error */
424 }
425 if (handle->inited != 1) /* check handle initialization */
426 {
427 return 3; /* return error */
428 }
429
430 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ATIME, (uint8_t *)&t, 1); /* write config */
431 if (res != 0) /* check the result */
432 {
433 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
434
435 return 1; /* return error */
436 }
437
438 return 0; /* success return 0 */
439}
440
453{
454 uint8_t res;
455
456 if (handle == NULL) /* check handle */
457 {
458 return 2; /* return error */
459 }
460 if (handle->inited != 1) /* check handle initialization */
461 {
462 return 3; /* return error */
463 }
464
465 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ATIME, (uint8_t *)t, 1); /* read config */
466 if (res != 0) /* check the result */
467 {
468 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
469
470 return 1; /* return error */
471 }
472
473 return 0; /* success return 0 */
474}
475
488{
489 uint8_t res, prev, bit;
490
491 if (handle == NULL) /* check handle */
492 {
493 return 2; /* return error */
494 }
495 if (handle->inited != 1) /* check handle initialization */
496 {
497 return 3; /* return error */
498 }
499
500 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
501 if (res != 0) /* check result */
502 {
503 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
504
505 return 1; /* return error */
506 }
507 bit = (t & 0x100) >> 8; /* get bit */
508 prev &= ~(1 << 1); /* clear wait time bit */
509 prev |= bit << 1; /* set bit */
510 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_CONFIG, (uint8_t *)&prev, 1); /* write config */
511 if (res != 0) /* check result */
512 {
513 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
514
515 return 1; /* return error */
516 }
517 prev = t & 0xFF; /* get time */
518 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_WTIME, (uint8_t *)&prev, 1); /* write config */
519 if (res != 0) /* check the result */
520 {
521 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
522
523 return 1; /* return error */
524 }
525
526 return 0; /* success return 0 */
527}
528
541{
542 uint8_t res, prev, bit;
543
544 if (handle == NULL) /* check handle */
545 {
546 return 2; /* return error */
547 }
548 if (handle->inited != 1) /* check handle initialization */
549 {
550 return 3; /* return error */
551 }
552
553 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CONFIG, (uint8_t *)&prev, 1); /* read config */
554 if (res != 0) /* check result */
555 {
556 handle->debug_print("tcs34725: read register failed.\n"); /* read failed */
557
558 return 1; /* return error */
559 }
560 prev &= 1 << 1; /* get wait time bit */
561 bit = (prev >> 1) & 0x01; /* get wait time */
562 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_WTIME, (uint8_t *)&prev, 1); /* read config */
563 if (res != 0) /* check result */
564 {
565 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
566
567 return 1; /* return error */
568 }
569 *t = (tcs34725_wait_time_t)((bit << 8) | prev); /* get time */
570
571 return 0; /* success return 0 */
572}
573
586{
587 uint8_t res;
588 uint8_t buf[2];
589
590 if (handle == NULL) /* check handle */
591 {
592 return 2; /* return error */
593 }
594 if (handle->inited != 1) /* check handle initialization */
595 {
596 return 3; /* return error */
597 }
598
599 buf[0] = threshold & 0xFF; /* get threshold LSB */
600 buf[1] = (threshold >> 8) & 0xFF; /* get threshold MSB */
601 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_AILTL, (uint8_t *)buf, 2); /* write config */
602 if (res != 0) /* check the result */
603 {
604 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
605
606 return 1; /* return error */
607 }
608
609 return 0; /* success return 0 */
610}
611
624{
625 uint8_t res, buf[2];
626
627 if (handle == NULL) /* check handle */
628 {
629 return 2; /* return error */
630 }
631 if (handle->inited != 1) /* check handle initialization */
632 {
633 return 3; /* return error */
634 }
635
636 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
637 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_AILTL, (uint8_t *)buf, 2); /* read ailtl */
638 if (res != 0) /* check result */
639 {
640 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
641
642 return 1; /* return error */
643 }
644 *threshold = ((uint16_t)buf[1] << 8) | buf[0]; /* get threshold */
645
646 return 0; /* success return 0 */
647}
648
661{
662 uint8_t res;
663 uint8_t buf[2];
664
665 if (handle == NULL) /* check handle */
666 {
667 return 2; /* return error */
668 }
669 if (handle->inited != 1) /* check handle initialization */
670 {
671 return 3; /* return error */
672 }
673
674 buf[0] = threshold & 0xFF; /* get threshold LSB */
675 buf[1] = (threshold >> 8) & 0xFF; /* get threshold MSB */
676 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_AIHTL, (uint8_t *)buf, 2); /* write config */
677 if (res != 0) /* check the result */
678 {
679 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
680
681 return 1; /* return error */
682 }
683
684 return 0; /* success return 0 */
685}
686
699{
700 uint8_t res, buf[2];
701
702 if (handle == NULL) /* check handle */
703 {
704 return 2; /* return error */
705 }
706 if (handle->inited != 1) /* check handle initialization */
707 {
708 return 3; /* return error */
709 }
710
711 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
712 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_AIHTL, (uint8_t *)buf, 2); /* read aihtl */
713 if (res != 0) /* check result */
714 {
715 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
716
717 return 1; /* return error */
718 }
719 *threshold = ((uint16_t)buf[1] << 8) | buf[0]; /* get threshold */
720
721 return 0; /* success return 0 */
722}
723
736{
737 uint8_t res, prev;
738
739 if (handle == NULL) /* check handle */
740 {
741 return 2; /* return error */
742 }
743 if (handle->inited != 1) /* check handle initialization */
744 {
745 return 3; /* return error */
746 }
747
748 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_PERS, (uint8_t *)&prev, 1); /* read pers */
749 if (res != 0) /* check result */
750 {
751 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
752
753 return 1; /* return error */
754 }
755 prev &= ~0x0F; /* clear mode bit */
756 prev |= mode; /* set mode */
757 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_PERS, (uint8_t *)&prev, 1); /* write config */
758 if (res != 0) /* check result */
759 {
760 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
761
762 return 1; /* return error */
763 }
764
765 return 0; /* success return 0 */
766}
767
780{
781 uint8_t res, prev;
782
783 if (handle == NULL) /* check handle */
784 {
785 return 2; /* return error */
786 }
787 if (handle->inited != 1) /* check handle initialization */
788 {
789 return 3; /* return error */
790 }
791
792 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_PERS, (uint8_t *)&prev, 1); /* read pers */
793 if (res != 0) /* check result */
794 {
795 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
796
797 return 1; /* return error */
798 }
799 prev &= 0x0F; /* get interrupt mode bits */
800 *mode = (tcs34725_interrupt_mode_t)(prev & 0x0F); /* get interrupt mode */
801
802 return 0; /* success return 0 */
803}
804
817{
818 uint8_t res, prev;
819
820 if (handle == NULL) /* check handle */
821 {
822 return 2; /* return error */
823 }
824 if (handle->inited != 1) /* check handle initialization */
825 {
826 return 3; /* return error */
827 }
828
829 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CONTROL, (uint8_t *)&prev, 1); /* read control */
830 if (res != 0) /* check result */
831 {
832 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
833
834 return 1; /* return error */
835 }
836 prev &= ~0x03; /* get gain bits */
837 prev |= gain; /* set gain */
838 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_CONTROL, (uint8_t *)&prev, 1); /* write config */
839 if (res != 0) /* check result */
840 {
841 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
842
843 return 1; /* return error */
844 }
845
846 return 0; /* success return 0 */
847}
848
861{
862 uint8_t res, prev;
863
864 if (handle == NULL) /* check handle */
865 {
866 return 2; /* return error */
867 }
868 if (handle->inited != 1) /* check handle initialization */
869 {
870 return 3; /* return error */
871 }
872
873 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CONTROL, (uint8_t *)&prev, 1); /* read config */
874 if (res != 0) /* check result */
875 {
876 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
877
878 return 1; /* return error */
879 }
880 prev &= 0x03; /* get gain bits */
881 *gain = (tcs34725_gain_t)(prev & 0x03); /* get gain */
882
883 return 0; /* success return 0 */
884}
885
900uint8_t tcs34725_read_rgbc(tcs34725_handle_t *handle, uint16_t *red, uint16_t *green, uint16_t *blue, uint16_t *clear)
901{
902 uint8_t res, prev;
903 uint8_t buf[8];
904
905 if (handle == NULL) /* check handle */
906 {
907 return 2; /* return error */
908 }
909 if (handle->inited != 1) /* check handle initialization */
910 {
911 return 3; /* return error */
912 }
913
914 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_STATUS, (uint8_t *)&prev, 1); /* read status */
915 if (res != 0) /* check result */
916 {
917 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
918
919 return 1; /* return error */
920 }
921 if ((prev & (1 << 4)) != 0) /* find interrupt */
922 {
923 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_CLEAR, NULL, 0); /* clear interrupt */
924 if (res != 0) /* check result */
925 {
926 handle->debug_print("tcs34725: clear interrupt failed.\n"); /* clear interrupt failed */
927
928 return 1; /* return error */
929 }
930 }
931 if ((prev & 0x01) != 0) /* if data ready */
932 {
933 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CDATAL, (uint8_t *)buf, 8); /* read data */
934 if (res != 0) /* check result */
935 {
936 handle->debug_print("tcs34725: read failed.\n"); /* read failed */
937
938 return 1; /* return error */
939 }
940 *clear = ((uint16_t)buf[1] << 8) | buf[0]; /* get clear */
941 *red = ((uint16_t)buf[3] << 8) | buf[2]; /* get red */
942 *green = ((uint16_t)buf[5] << 8) | buf[4]; /* get green */
943 *blue = ((uint16_t)buf[7] << 8) | buf[6]; /* get blue */
944
945 return 0; /* success return 0 */
946 }
947 else
948 {
949 handle->debug_print("tcs34725: data not ready.\n"); /* data not ready */
950
951 return 1; /* return error */
952 }
953}
954
968uint8_t tcs34725_read_rgb(tcs34725_handle_t *handle, uint16_t *red, uint16_t *green, uint16_t *blue)
969{
970 uint8_t res, prev;
971 uint8_t buf[8];
972
973 if (handle == NULL) /* check handle */
974 {
975 return 2; /* return error */
976 }
977 if (handle->inited != 1) /* check handle initialization */
978 {
979 return 3; /* return error */
980 }
981
982 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_STATUS, (uint8_t *)&prev, 1); /* read config */
983 if (res != 0) /* check result */
984 {
985 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
986
987 return 1; /* return error */
988 }
989 if ((prev & (1 << 4)) != 0) /* find interrupt */
990 {
991 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_CLEAR, NULL, 0); /* clear interrupt */
992 if (res != 0) /* check result */
993 {
994 handle->debug_print("tcs34725: clear interrupt failed.\n"); /* clear interrupt failed */
995
996 return 1; /* return error */
997 }
998 }
999 if ((prev & 0x01) != 0) /* if data ready */
1000 {
1001 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CDATAL, (uint8_t *)buf, 8); /* read data */
1002 if (res != 0) /* check result */
1003 {
1004 handle->debug_print("tcs34725: read failed.\n"); /* read data failed */
1005
1006 return 1; /* return error */
1007 }
1008 *red = ((uint16_t)buf[3] << 8) | buf[2]; /* get red */
1009 *green = ((uint16_t)buf[5] << 8) | buf[4]; /* get green */
1010 *blue = ((uint16_t)buf[7] << 8) | buf[6]; /* get blue */
1011
1012 return 0; /* success return 0 */
1013 }
1014 else
1015 {
1016 handle->debug_print("tcs34725: data not ready.\n"); /* data not ready */
1017
1018 return 1; /* return error */
1019 }
1020}
1021
1033uint8_t tcs34725_read_c(tcs34725_handle_t *handle, uint16_t *clear)
1034{
1035 uint8_t res, prev;
1036 uint8_t buf[8];
1037
1038 if (handle == NULL) /* check handle */
1039 {
1040 return 2; /* return error */
1041 }
1042 if (handle->inited != 1) /* check handle initialization */
1043 {
1044 return 3; /* return error */
1045 }
1046
1047 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_STATUS, (uint8_t *)&prev, 1); /* read status */
1048 if (res != 0) /* check result */
1049 {
1050 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
1051
1052 return 1; /* return error */
1053 }
1054 if ((prev & (1 << 4)) != 0) /* find interrupt */
1055 {
1056 res = handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_CLEAR, NULL, 0); /* clear interrupt */
1057 if (res != 0) /* check result */
1058 {
1059 handle->debug_print("tcs34725: clear interrupt failed.\n"); /* clear interrupt failed */
1060
1061 return 1; /* return error */
1062 }
1063 }
1064 if ((prev & 0x01) != 0) /* if data ready */
1065 {
1066 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_CDATAL, (uint8_t *)buf, 8); /* read data */
1067 if (res != 0) /* check result */
1068 {
1069 handle->debug_print("tcs34725: read failed.\n"); /* read failed */
1070
1071 return 1; /* return error */
1072 }
1073 *clear = ((uint16_t)buf[1] << 8) | buf[0]; /* get clear */
1074
1075 return 0; /* success return 0 */
1076 }
1077 else
1078 {
1079 handle->debug_print("tcs34725: data not ready.\n"); /* data not ready */
1080
1081 return 1; /* return error */
1082 }
1083}
1084
1096{
1097 uint8_t res, id;
1098
1099 if (handle == NULL) /* check handle */
1100 {
1101 return 2; /* return error */
1102 }
1103 if (handle->debug_print == NULL) /* check debug_print */
1104 {
1105 return 3; /* return error */
1106 }
1107 if (handle->iic_init == NULL) /* check iic_init */
1108 {
1109 handle->debug_print("tcs34725: iic_init is null.\n"); /* iic_init is null */
1110
1111 return 3; /* return error */
1112 }
1113 if (handle->iic_deinit == NULL) /* check iic_init */
1114 {
1115 handle->debug_print("tcs34725: iic_deinit is null.\n"); /* iic_deinit is null */
1116
1117 return 3; /* return error */
1118 }
1119 if (handle->iic_read == NULL) /* check iic_read */
1120 {
1121 handle->debug_print("tcs34725: iic_read is null.\n"); /* iic_read is null */
1122
1123 return 3; /* return error */
1124 }
1125 if (handle->iic_write == NULL) /* check iic_write */
1126 {
1127 handle->debug_print("tcs34725: iic_write is null.\n"); /* iic_write is null */
1128
1129 return 3; /* return error */
1130 }
1131 if (handle->delay_ms == NULL) /* check delay_ms */
1132 {
1133 handle->debug_print("tcs34725: delay_ms is null.\n"); /* delay_ms is null */
1134
1135 return 3; /* return error */
1136 }
1137
1138 if (handle->iic_init() != 0) /* iic init */
1139 {
1140 handle->debug_print("tcs34725: iic init failed.\n"); /* iic init failed */
1141
1142 return 1; /* return error */
1143 }
1144 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ID, (uint8_t *)&id, 1); /* read id */
1145 if (res != 0) /* check result */
1146 {
1147 handle->debug_print("tcs34725: read id failed.\n"); /* read id failed */
1148 (void)handle->iic_deinit(); /* iic deinit */
1149
1150 return 1; /* return error */
1151 }
1152 if ((id != 0x44) && (id != 0x4D)) /* check id */
1153 {
1154 handle->debug_print("tcs34725: id is error.\n"); /* id is error */
1155 (void)handle->iic_deinit(); /* iic deinit */
1156
1157 return 1; /* return error */
1158 }
1159 handle->inited = 1; /* flag finish initialization */
1160
1161 return 0; /* success return 0 */
1162}
1163
1175{
1176 uint8_t res, prev;
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 res = handle->iic_read(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1); /* read enable */
1188 if (res != 0) /* check result */
1189 {
1190 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
1191
1192 return 1; /* return error */
1193 }
1194 prev &= ~(1 << 0); /* disable */
1195 if (handle->iic_write(TCS34725_ADDRESS, TCS34725_REG_ENABLE, (uint8_t *)&prev, 1) != 0) /* write enable */
1196 {
1197 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
1198
1199 return 1; /* return error */
1200 }
1201 if (handle->iic_deinit() != 0) /* iic deinit */
1202 {
1203 handle->debug_print("tcs34725: iic deinit failed.\n"); /* iic deinit failed */
1204
1205 return 1; /* return error */
1206 }
1207 handle->inited = 0; /* flag close */
1208
1209 return 0; /* success return 0 */
1210}
1211
1225uint8_t tcs34725_set_reg(tcs34725_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1226{
1227 uint8_t res;
1228
1229 if (handle == NULL) /* check handle */
1230 {
1231 return 2; /* return error */
1232 }
1233 if (handle->inited != 1) /* check handle initialization */
1234 {
1235 return 3; /* return error */
1236 }
1237
1238 res = handle->iic_write(TCS34725_ADDRESS, reg, buf, len); /* write data */
1239 if (res != 0) /* check result */
1240 {
1241 handle->debug_print("tcs34725: write register failed.\n"); /* write register failed */
1242
1243 return 1; /* return error */
1244 }
1245
1246 return 0; /* success return 0 */
1247}
1248
1262uint8_t tcs34725_get_reg(tcs34725_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1263{
1264 uint8_t res;
1265
1266 if (handle == NULL) /* check handle */
1267 {
1268 return 2; /* return error */
1269 }
1270 if (handle->inited != 1) /* check handle initialization */
1271 {
1272 return 3; /* return error */
1273 }
1274
1275 res = handle->iic_read(TCS34725_ADDRESS, reg, buf, len); /* read data */
1276 if (res != 0) /* check result */
1277 {
1278 handle->debug_print("tcs34725: read register failed.\n"); /* read register failed */
1279
1280 return 1; /* return error */
1281 }
1282
1283 return 0; /* success return 0 */
1284}
1285
1295{
1296 if (info == NULL) /* check handle */
1297 {
1298 return 2; /* return error */
1299 }
1300
1301 memset(info, 0, sizeof(tcs34725_info_t)); /* initialize tcs34725 info structure */
1302 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1303 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1304 strncpy(info->interface, "IIC", 8); /* copy interface name */
1305 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1306 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1307 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1308 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1309 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1310 info->driver_version = DRIVER_VERSION; /* set driver version */
1311
1312 return 0; /* success return 0 */
1313}
#define TCS34725_REG_ID
#define TCS34725_REG_CONFIG
#define TCS34725_REG_ENABLE
chip register definition
#define TCS34725_REG_STATUS
#define TCS34725_REG_CDATAL
#define MAX_CURRENT
#define TCS34725_REG_WTIME
#define TCS34725_REG_AIHTL
#define SUPPLY_VOLTAGE_MAX
#define TCS34725_REG_PERS
#define TCS34725_REG_CONTROL
#define TCS34725_ADDRESS
iic address definition
#define TCS34725_REG_ATIME
#define TEMPERATURE_MAX
#define TCS34725_REG_CLEAR
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define TCS34725_REG_AILTL
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver tcs34725 header file
uint8_t tcs34725_set_rgbc_integration_time(tcs34725_handle_t *handle, tcs34725_integration_time_t t)
set the rgbc adc integration time
uint8_t tcs34725_get_power_on(tcs34725_handle_t *handle, tcs34725_bool_t *enable)
get the power status
uint8_t tcs34725_deinit(tcs34725_handle_t *handle)
close the chip
struct tcs34725_info_s tcs34725_info_t
tcs34725 information structure definition
uint8_t tcs34725_set_gain(tcs34725_handle_t *handle, tcs34725_gain_t gain)
set the adc gain
uint8_t tcs34725_set_rgbc(tcs34725_handle_t *handle, tcs34725_bool_t enable)
enable or disable the rgbc adc
uint8_t tcs34725_set_wait_time(tcs34725_handle_t *handle, tcs34725_wait_time_t t)
set the wait time
tcs34725_bool_t
tcs34725 bool enumeration definition
uint8_t tcs34725_set_power_on(tcs34725_handle_t *handle, tcs34725_bool_t enable)
enable or disable the power
uint8_t tcs34725_get_wait_time(tcs34725_handle_t *handle, tcs34725_wait_time_t *t)
get the wait time
uint8_t tcs34725_get_wait(tcs34725_handle_t *handle, tcs34725_bool_t *enable)
get the wait time
uint8_t tcs34725_get_rgbc(tcs34725_handle_t *handle, tcs34725_bool_t *enable)
get the rgbc status
uint8_t tcs34725_info(tcs34725_info_t *info)
get chip information
uint8_t tcs34725_get_gain(tcs34725_handle_t *handle, tcs34725_gain_t *gain)
get the adc gain
uint8_t tcs34725_read_rgb(tcs34725_handle_t *handle, uint16_t *red, uint16_t *green, uint16_t *blue)
read the rgb data
struct tcs34725_handle_s tcs34725_handle_t
tcs34725 handle structure definition
tcs34725_wait_time_t
tcs34725 wait time enumeration definition
tcs34725_gain_t
tcs34725 gain enumeration definition
uint8_t tcs34725_init(tcs34725_handle_t *handle)
initialize the chip
uint8_t tcs34725_read_rgbc(tcs34725_handle_t *handle, uint16_t *red, uint16_t *green, uint16_t *blue, uint16_t *clear)
read the rgbc data
tcs34725_integration_time_t
tcs34725 integration time enumeration definition
uint8_t tcs34725_set_wait(tcs34725_handle_t *handle, tcs34725_bool_t enable)
enable or disable the wait time
uint8_t tcs34725_read_c(tcs34725_handle_t *handle, uint16_t *clear)
read the clear data
uint8_t tcs34725_get_rgbc_integration_time(tcs34725_handle_t *handle, tcs34725_integration_time_t *t)
get the rgbc adc integration time
uint8_t tcs34725_get_reg(tcs34725_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t tcs34725_set_reg(tcs34725_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t tcs34725_get_rgbc_clear_high_interrupt_threshold(tcs34725_handle_t *handle, uint16_t *threshold)
get the rgbc clear high interrupt threshold
tcs34725_interrupt_mode_t
tcs34725 interrupt mode enumeration definition
uint8_t tcs34725_set_rgbc_clear_high_interrupt_threshold(tcs34725_handle_t *handle, uint16_t threshold)
set the rgbc clear high interrupt threshold
uint8_t tcs34725_get_rgbc_clear_low_interrupt_threshold(tcs34725_handle_t *handle, uint16_t *threshold)
get the rgbc clear low interrupt threshold
uint8_t tcs34725_get_rgbc_interrupt(tcs34725_handle_t *handle, tcs34725_bool_t *enable)
get the rgbc interrupt
uint8_t tcs34725_get_interrupt_mode(tcs34725_handle_t *handle, tcs34725_interrupt_mode_t *mode)
get the interrupt mode
uint8_t tcs34725_set_rgbc_interrupt(tcs34725_handle_t *handle, tcs34725_bool_t enable)
enable or disable the rgbc interrupt
uint8_t tcs34725_set_rgbc_clear_low_interrupt_threshold(tcs34725_handle_t *handle, uint16_t threshold)
set the rgbc clear low interrupt threshold
uint8_t tcs34725_set_interrupt_mode(tcs34725_handle_t *handle, tcs34725_interrupt_mode_t mode)
set the interrupt mode
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
char manufacturer_name[32]