LibDriver TSL2561
Loading...
Searching...
No Matches
driver_tsl2561.c
Go to the documentation of this file.
1
37
38#include "driver_tsl2561.h"
39
43#define CHIP_NAME "AMS TSL2561"
44#define MANUFACTURER_NAME "AMS"
45#define SUPPLY_VOLTAGE_MIN 2.7f
46#define SUPPLY_VOLTAGE_MAX 3.6f
47#define MAX_CURRENT 0.60f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define TSL2561_REG_CONTROL 0x80
56#define TSL2561_REG_TIMING 0x81
57#define TSL2561_REG_THRESHLOWLOW 0xA2
58#define TSL2561_REG_THRESHLOWHIGH 0xA3
59#define TSL2561_REG_THRESHHIGHLOW 0xA4
60#define TSL2561_REG_THRESHHIGHHIGH 0xA5
61#define TSL2561_REG_INTERRUPT 0x86
62#define TSL2561_REG_ID 0x8A
63#define TSL2561_REG_DATA0LOW 0xEC
64#define TSL2561_REG_DATA0HIGH 0xED
65#define TSL2561_REG_DATA1LOW 0xEE
66#define TSL2561_REG_DATA1HIGH 0xEF
67
71#define TSL2561_CONTROL_POWERON 0x03
72#define TSL2561_CONTROL_POWEROFF 0x00
73#define TSL2561_LUX_LUXSCALE 14
74#define TSL2561_LUX_RATIOSCALE 9
75#define TSL2561_LUX_CHSCALE 10
76#define TSL2561_LUX_CHSCALE_TINT0 0x7517U
77#define TSL2561_LUX_CHSCALE_TINT1 0x0FE7U
78#define TSL2561_LUX_K1T 0x0040U
79#define TSL2561_LUX_B1T 0x01F2U
80#define TSL2561_LUX_M1T 0x01BEU
81#define TSL2561_LUX_K2T 0x0080U
82#define TSL2561_LUX_B2T 0x0214U
83#define TSL2561_LUX_M2T 0x02D1U
84#define TSL2561_LUX_K3T 0x00C0U
85#define TSL2561_LUX_B3T 0x023FU
86#define TSL2561_LUX_M3T 0x037BU
87#define TSL2561_LUX_K4T 0x0100U
88#define TSL2561_LUX_B4T 0x0270U
89#define TSL2561_LUX_M4T 0x03FEU
90#define TSL2561_LUX_K5T 0x0138U
91#define TSL2561_LUX_B5T 0x016FU
92#define TSL2561_LUX_M5T 0x01FCU
93#define TSL2561_LUX_K6T 0x019AU
94#define TSL2561_LUX_B6T 0x00D2U
95#define TSL2561_LUX_M6T 0x00FBU
96#define TSL2561_LUX_K7T 0x029AU
97#define TSL2561_LUX_B7T 0x0018U
98#define TSL2561_LUX_M7T 0x0012U
99#define TSL2561_LUX_K8T 0x029AU
100#define TSL2561_LUX_B8T 0x0000U
101#define TSL2561_LUX_M8T 0x0000U
102#define TSL2561_GAIN_0X 0x00
103#define TSL2561_GAIN_16X 0x10
104
114static uint32_t a_tsl2561_calculate_lux(uint16_t gain, uint16_t t, uint16_t ch0, uint16_t ch1)
115{
116 uint32_t ch_scale;
117 uint32_t channel_1;
118 uint32_t channel_0;
119 uint32_t temp;
120 uint32_t ratio1 = 0;
121 uint32_t ratio;
122 uint32_t lux;
123 uint16_t b,m;
124
125 switch (t) /* choose integration time */
126 {
127 case 0 :
128 {
129 ch_scale = TSL2561_LUX_CHSCALE_TINT0; /* 13.7ms */
130
131 break; /* break */
132 }
133 case 1 :
134 {
135 ch_scale = TSL2561_LUX_CHSCALE_TINT1; /* 101ms */
136
137 break; /* break */
138 }
139 default :
140 {
141 ch_scale = 1 << TSL2561_LUX_CHSCALE; /* assume no scaling */
142
143 break; /* break */
144 }
145 }
146 if (!gain) /* check gain */
147 {
148 ch_scale = ch_scale << 4; /* cale 1X to 16X */
149 }
150 channel_0 = (ch0 * ch_scale) >> TSL2561_LUX_CHSCALE; /* set channel 0 */
151 channel_1 = (ch1 * ch_scale) >> TSL2561_LUX_CHSCALE; /* set channel 1 */
152 if (channel_0 != 0) /* check channel 0 */
153 {
154 ratio1 = (channel_1<<(TSL2561_LUX_RATIOSCALE+1)) / channel_0; /* get ratio */
155 }
156 ratio = (ratio1 + 1) >> 1; /* right shift 1 */
157 if ((ratio > 0) && (ratio <= TSL2561_LUX_K1T)) /* if K1T */
158 {
159 b = TSL2561_LUX_B1T; /* set B1T */
160 m = TSL2561_LUX_M1T; /* set M1T */
161 }
162 else if (ratio <= TSL2561_LUX_K2T) /* if K2T */
163 {
164 b = TSL2561_LUX_B2T; /* set B2T */
165 m = TSL2561_LUX_M2T; /* set M2T */
166 }
167 else if (ratio <= TSL2561_LUX_K3T) /* if K3T */
168 {
169 b = TSL2561_LUX_B3T; /* set B3T */
170 m = TSL2561_LUX_M3T; /* set M3T */
171 }
172 else if (ratio <= TSL2561_LUX_K4T) /* if K4T */
173 {
174 b = TSL2561_LUX_B4T; /* set B4T */
175 m = TSL2561_LUX_M4T; /* set M4T */
176 }
177 else if (ratio <= TSL2561_LUX_K5T) /* if K5T */
178 {
179 b = TSL2561_LUX_B5T; /* set B5T */
180 m = TSL2561_LUX_M5T; /* set M5T */
181 }
182 else if (ratio <= TSL2561_LUX_K6T) /* if K6T */
183 {
184 b = TSL2561_LUX_B6T; /* set B6T */
185 m = TSL2561_LUX_M6T; /* set M6T */
186 }
187 else if (ratio <= TSL2561_LUX_K7T) /* if K7T */
188 {
189 b = TSL2561_LUX_B7T; /* set B7T */
190 m = TSL2561_LUX_M7T; /* set M7T */
191 }
192 else /* if K8T */
193 {
194 b = TSL2561_LUX_B8T; /* set B8T */
195 m = TSL2561_LUX_M8T; /* set M8T */
196 }
197 temp = ((channel_0 * b) - (channel_1 * m)); /* get temp */
198 temp += (1 << (TSL2561_LUX_LUXSCALE - 1)); /* 2^(temp-1) */
199 lux = temp >> TSL2561_LUX_LUXSCALE; /* calculate lux */
200
201 return lux; /* return lux */
202}
203
215{
216 uint8_t res, id;
217
218 if (handle == NULL) /* check handle */
219 {
220 return 2; /* return error */
221 }
222 if (handle->debug_print == NULL) /* check debug_print */
223 {
224 return 3; /* return error */
225 }
226 if (handle->iic_init == NULL) /* check iic_init */
227 {
228 handle->debug_print("tsl2561: iic_init is null.\n"); /* iic_init is null */
229
230 return 3; /* return error */
231 }
232 if (handle->iic_deinit == NULL) /* check iic_deinit */
233 {
234 handle->debug_print("tsl2561: iic_deinit is null.\n"); /* iic_deinit is null */
235
236 return 3; /* return error */
237 }
238 if (handle->iic_read == NULL) /* check iic_read */
239 {
240 handle->debug_print("tsl2561: iic_read is null.\n"); /* iic_read is null */
241
242 return 3; /* return error */
243 }
244 if (handle->iic_write == NULL) /* check iic_write */
245 {
246 handle->debug_print("tsl2561: iic_write is null.\n"); /* iic_write is null */
247
248 return 3; /* return error */
249 }
250 if (handle->delay_ms == NULL) /* check delay_ms */
251 {
252 handle->debug_print("tsl2561: delay_ms is null.\n"); /* delay_ms is null */
253
254 return 3; /* return error */
255 }
256
257 if (handle->iic_init() != 0) /* iic init */
258 {
259 handle->debug_print("tsl2561: iic init failed.\n"); /* iic init failed */
260
261 return 1; /* return error */
262 }
263 res = handle->iic_read(handle->iic_addr, TSL2561_REG_ID, (uint8_t *)&id, 1); /* read id */
264 if (res != 0) /* check the result */
265 {
266 handle->debug_print("tsl2561: read id failed.\n"); /* read id failed */
267 (void)handle->iic_deinit(); /* iic deinit */
268
269 return 1; /* return error */
270 }
271 id &= ~0xF; /* get id part */
272 if (!(id == 0x10 || id == 0x50)) /* check id */
273 {
274 handle->debug_print("tsl2561: id is error.\n"); /* id is error */
275 (void)handle->iic_deinit(); /* iic deinit */
276
277 return 1; /* return error */
278 }
279 handle->inited = 1; /* flag finish initialization */
280
281 return 0; /* success return 0 */
282}
283
295{
296 uint8_t reg;
297
298 if (handle == NULL) /* check handle */
299 {
300 return 2; /* return error */
301 }
302 if (handle->inited != 1) /* check handle initialization */
303 {
304 return 3; /* return error */
305 }
306
307 reg = TSL2561_CONTROL_POWEROFF; /* set power down command */
308 if (handle->iic_write(handle->iic_addr, TSL2561_REG_CONTROL, (uint8_t *)&reg, 1) != 0) /* power down */
309 {
310 handle->debug_print("tsl2561: power down failed.\n"); /* power down failed */
311
312 return 1; /* return error */
313 }
314 if (handle->iic_deinit() != 0) /* iic deinit */
315 {
316 handle->debug_print("tsl2561: iic deinit failed.\n"); /* iic deinit failed */
317
318 return 1; /* return error */
319 }
320 handle->inited = 0; /* flag close */
321
322 return 0; /* success return 0 */
323}
324
336{
337 if (handle == NULL) /* check handle */
338 {
339 return 2; /* return error */
340 }
341
342 handle->iic_addr = (uint8_t)addr_pin; /* set iic address */
343
344 return 0; /* success return 0 */
345}
346
358{
359 if (handle == NULL) /* check handle */
360 {
361 return 2; /* return error */
362 }
363
364 *addr_pin = (tsl2561_address_t)handle->iic_addr; /* get iic address */
365
366 return 0; /* success return 0 */
367}
368
382uint8_t tsl2561_read(tsl2561_handle_t *handle, uint16_t *channel_0_raw, uint16_t *channel_1_raw, uint32_t *lux)
383{
384 uint8_t prev;
385 uint8_t buf[2];
386
387 if (handle == NULL) /* check handle */
388 {
389 return 2; /* return error */
390 }
391 if (handle->inited != 1) /* check handle initialization */
392 {
393 return 3; /* return error */
394 }
395
396 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
397 if (handle->iic_read(handle->iic_addr, TSL2561_REG_DATA0LOW, (uint8_t *)buf, 2) != 0) /* read data0 low */
398 {
399 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
400
401 return 1; /* return error */
402 }
403 *channel_0_raw = ((uint16_t)buf[1] << 8) | buf[0]; /* get channel 0 */
404 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
405 if (handle->iic_read(handle->iic_addr, TSL2561_REG_DATA1LOW, (uint8_t *)buf, 2) != 0) /* read data1 low */
406 {
407 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
408
409 return 1; /* return error */
410 }
411 *channel_1_raw = ((uint16_t)buf[1] << 8) | buf[0]; /* get channel 1 */
412 if (handle->iic_read(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1) != 0) /* read data */
413 {
414 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
415
416 return 1; /* return error */
417 }
418 *lux = a_tsl2561_calculate_lux((prev & 0x10) >> 4, prev & 0x03, *channel_0_raw, *channel_1_raw); /* calculate lux */
419
420 return 0; /* success return 0 */
421}
422
434{
435 uint8_t res, 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 = handle->iic_read(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1); /* read timing */
447 if (res != 0) /* check result */
448 {
449 handle->debug_print("tsl2561: read failed.\n"); /* read timing failed */
450
451 return 1; /* return error */
452 }
453 prev &= 0xEF; /* clear gain bit */
454 prev |= gain << 4; /* set gain */
455 if (handle->iic_write(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1) != 0) /* write command */
456 {
457 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
458
459 return 1; /* return error */
460 }
461 else
462 {
463 return 0; /* success return 0 */
464 }
465}
466
478{
479 uint8_t res, prev;
480
481 if (handle == NULL) /* check handle */
482 {
483 return 2; /* return error */
484 }
485 if (handle->inited != 1) /* check handle initialization */
486 {
487 return 3; /* return error */
488 }
489
490 res = handle->iic_read(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1); /* read timing */
491 if (res != 0) /* check result */
492 {
493 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
494
495 return 1; /* return error */
496 }
497 prev &= ~0xEF; /* get gain bits */
498 *gain = (tsl2561_gain_t)(prev >> 4); /* get gain */
499
500 return 0; /* success return 0 */
501}
502
514{
515 uint8_t res, prev;
516
517 if (handle == NULL) /* check handle */
518 {
519 return 2; /* return error */
520 }
521 if (handle->inited != 1) /* check handle initialization */
522 {
523 return 3; /* return error */
524 }
525
526 res = handle->iic_read(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1); /* read timing */
527 if (res != 0) /* check result */
528 {
529 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
530
531 return 1; /* return error */
532 }
533 prev &= ~0x03; /* clear integration time bits */
534 prev |= t; /* set integration bits */
535 if (handle->iic_write(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1) != 0) /* write config */
536 {
537 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
538
539 return 1; /* return error */
540 }
541 else
542 {
543 return 0; /* success return 0 */
544 }
545}
546
558{
559 uint8_t res, prev;
560
561 if (handle == NULL) /* check handle */
562 {
563 return 2; /* return error */
564 }
565 if (handle->inited != 1) /* check handle initialization */
566 {
567 return 3; /* return error */
568 }
569
570 res = handle->iic_read(handle->iic_addr, TSL2561_REG_TIMING, (uint8_t *)&prev, 1); /* read timing */
571 if (res != 0) /* check result */
572 {
573 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
574
575 return 1; /* return error */
576 }
577 prev &= 0x03; /* get integration time bits */
578 *t = (tsl2561_integration_time_t)prev; /* get integration time */
579
580 return 0; /* success return 0 */
581}
582
594{
595 uint8_t res, 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 = handle->iic_read(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1); /* read interrupt reg */
607 if (res != 0) /* check result */
608 {
609 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
610
611 return 1; /* return error */
612 }
613 prev &= ~0x0F; /* clear mode */
614 prev |= mode; /* set mode */
615 if (handle->iic_write(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1) != 0) /* write interrupt config */
616 {
617 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
618
619 return 1; /* return error */
620 }
621 else
622 {
623 return 0; /* success return 0 */
624 }
625}
626
638{
639 uint8_t res, prev;
640
641 if (handle == NULL) /* check handle */
642 {
643 return 2; /* return error */
644 }
645 if (handle->inited != 1) /* check handle initialization */
646 {
647 return 3; /* return error */
648 }
649
650 res = handle->iic_read(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1); /* read interrupt */
651 if (res != 0) /* check result */
652 {
653 handle->debug_print("tsl2561: read failed.\n"); /* read interrupt failed */
654
655 return 1; /* return error */
656 }
657 prev &= 0x0F; /* get mode bits */
658 *mode = (tsl2561_interrupt_mode_t)prev; /* get mode */
659
660 return 0; /* success return 0 */
661}
662
674{
675 uint8_t res, prev;
676
677 if (handle == NULL) /* check handle */
678 {
679 return 2; /* return error */
680 }
681 if (handle->inited != 1) /* check handle initialization */
682 {
683 return 3; /* return error */
684 }
685
686 res = handle->iic_read(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1); /* read interrupt */
687 if (res != 0) /* check result */
688 {
689 handle->debug_print("tsl2561: read failed.\n"); /* read interrupt failed */
690
691 return 1; /* return error */
692 }
693 prev &= ~0x30; /* clear interrupt bit */
694 prev |= enable << 4; /* set interrupt */
695 if (handle->iic_write(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1) != 0) /* write interrupt config */
696 {
697 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
698
699 return 1; /* return error */
700 }
701 else
702 {
703 return 0; /* success return 0 */
704 }
705}
706
718{
719 uint8_t res, prev;
720
721 if (handle == NULL) /* check handle */
722 {
723 return 2; /* return error */
724 }
725 if (handle->inited != 1) /* check handle initialization */
726 {
727 return 3; /* return error */
728 }
729
730 res = handle->iic_read(handle->iic_addr, TSL2561_REG_INTERRUPT, (uint8_t *)&prev, 1); /* read interrupt */
731 if (res != 0) /* check result */
732 {
733 handle->debug_print("tsl2561: read failed.\n"); /* read interrupt failed */
734
735 return 1; /* return error */
736 }
737 prev &= 0x30; /* get interrupt bit */
738 *enable = (tsl2561_bool_t)(prev >> 4); /* get interrupt */
739
740 return 0; /* success return 0 */
741}
742
754{
755 uint8_t buf[2];
756
757 if (handle == NULL) /* check handle */
758 {
759 return 2; /* return error */
760 }
761 if (handle->inited != 1) /* check handle initialization */
762 {
763 return 3; /* return error */
764 }
765
766 buf[0] = ch0_raw & 0xFF; /* set ch0 raw LSB */
767 buf[1] = (ch0_raw >> 8) & 0xFF; /* set ch0 raw MSB */
768 if (handle->iic_write(handle->iic_addr, TSL2561_REG_THRESHHIGHLOW, (uint8_t *)buf, 2) != 0) /* write config */
769 {
770 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
771
772 return 1; /* return error */
773 }
774 else
775 {
776 return 0; /* success return 0 */
777 }
778}
779
791{
792 uint8_t res;
793 uint8_t buf[2];
794
795 if (handle == NULL) /* check handle */
796 {
797 return 2; /* return error */
798 }
799 if (handle->inited != 1) /* check handle initialization */
800 {
801 return 3; /* return error */
802 }
803
804 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
805 res = handle->iic_read(handle->iic_addr, TSL2561_REG_THRESHHIGHLOW, (uint8_t *)buf, 2); /* read config */
806 if (res != 0) /*check the result */
807 {
808 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
809
810 return 1; /* return error */
811 }
812 *ch0_raw = ((uint16_t)buf[1] << 8) | buf[0]; /* get ch0 raw */
813
814 return 0; /* success return 0 */
815}
816
828{
829 uint8_t buf[2];
830
831 if (handle == NULL) /* check handle */
832 {
833 return 2; /* return error */
834 }
835 if (handle->inited != 1) /* check handle initialization */
836 {
837 return 3; /* return error */
838 }
839
840 buf[0] = ch0_raw & 0xFF; /* set ch0 raw LSB */
841 buf[1] = (ch0_raw >> 8) & 0xFF; /* set ch0 raw MSB */
842 if (handle->iic_write(handle->iic_addr, TSL2561_REG_THRESHLOWLOW, (uint8_t *)buf, 2) != 0) /* write config */
843 {
844 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
845
846 return 1; /* return error */
847 }
848 else
849 {
850 return 0; /* success return 0 */
851 }
852}
853
864uint8_t tsl2561_get_interrupt_low_threshold(tsl2561_handle_t *handle, uint16_t *ch0_raw)
865{
866 uint8_t res;
867 uint8_t buf[2];
868
869 if (handle == NULL) /* check handle */
870 {
871 return 2; /* return error */
872 }
873 if (handle->inited != 1) /* check handle initialization */
874 {
875 return 3; /* return error */
876 }
877
878 memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
879 res = handle->iic_read(handle->iic_addr, TSL2561_REG_THRESHLOWLOW, (uint8_t *)buf, 2); /* read config */
880 if (res != 0) /* check result */
881 {
882 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
883
884 return 1; /* return error */
885 }
886 *ch0_raw = ((uint16_t)buf[1] << 8) | buf[0]; /* get ch0 raw */
887
888 return 0; /* return error */
889}
890
902{
903 uint8_t reg;
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 reg = TSL2561_CONTROL_POWEROFF; /* set power off command */
915 if (handle->iic_write(handle->iic_addr, TSL2561_REG_CONTROL, (uint8_t *)&reg, 1) != 0) /* write control */
916 {
917 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
918
919 return 1; /* return error */
920 }
921 else
922 {
923 return 0; /* success return 0 */
924 }
925}
926
938{
939 uint8_t reg;
940
941 if (handle == NULL) /* check handle */
942 {
943 return 2; /* return error */
944 }
945 if (handle->inited != 1) /* check handle initialization */
946 {
947 return 3; /* return error */
948 }
949
950 reg = TSL2561_CONTROL_POWERON; /* set power on command */
951 if (handle->iic_write(handle->iic_addr, TSL2561_REG_CONTROL, (uint8_t *)&reg, 1) != 0) /* write control */
952 {
953 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
954
955 return 1; /* return error */
956 }
957 else
958 {
959 return 0; /* success return 0 */
960 }
961}
962
976uint8_t tsl2561_set_reg(tsl2561_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
977{
978 if (handle == NULL) /* check handle */
979 {
980 return 2; /* return error */
981 }
982 if (handle->inited != 1) /* check handle initialization */
983 {
984 return 3; /* return error */
985 }
986
987 if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* write data */
988 {
989 handle->debug_print("tsl2561: write failed.\n"); /* write failed */
990
991 return 1; /* return error */
992 }
993 else
994 {
995 return 0; /* success return 0 */
996 }
997}
998
1012uint8_t tsl2561_get_reg(tsl2561_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1013{
1014 if (handle == NULL) /* check handle */
1015 {
1016 return 2; /* return error */
1017 }
1018 if (handle->inited != 1) /* check handle initialization */
1019 {
1020 return 3; /* return error */
1021 }
1022
1023 if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* read data */
1024 {
1025 handle->debug_print("tsl2561: read failed.\n"); /* read failed */
1026
1027 return 1; /* return error */
1028 }
1029 else
1030 {
1031 return 0; /* success return 0 */
1032 }
1033}
1034
1044{
1045 if (info == NULL) /* check handle */
1046 {
1047 return 2; /* return error */
1048 }
1049
1050 memset(info, 0, sizeof(tsl2561_info_t)); /* initialize tsl2561 info structure */
1051 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1052 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1053 strncpy(info->interface, "IIC", 8); /* copy interface name */
1054 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1055 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1056 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1057 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1058 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1059 info->driver_version = DRIVER_VERSION; /* set driver version */
1060
1061 return 0; /* success return 0 */
1062}
#define TSL2561_LUX_K2T
#define TSL2561_LUX_B6T
#define TSL2561_LUX_M8T
#define TSL2561_REG_DATA0LOW
#define TSL2561_REG_THRESHLOWLOW
#define TSL2561_CONTROL_POWERON
command definition
#define TSL2561_LUX_M4T
#define TSL2561_LUX_M6T
#define MAX_CURRENT
#define TSL2561_LUX_RATIOSCALE
#define TSL2561_REG_DATA1LOW
#define TSL2561_REG_THRESHHIGHLOW
#define TSL2561_LUX_CHSCALE_TINT0
#define TSL2561_LUX_B3T
#define TSL2561_LUX_B1T
#define TSL2561_LUX_LUXSCALE
#define SUPPLY_VOLTAGE_MAX
#define TSL2561_LUX_M2T
#define TSL2561_LUX_K7T
#define TEMPERATURE_MAX
#define TSL2561_LUX_B4T
#define TSL2561_LUX_M5T
#define TSL2561_LUX_M7T
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define TSL2561_LUX_K5T
#define SUPPLY_VOLTAGE_MIN
#define TSL2561_LUX_B8T
#define TSL2561_LUX_CHSCALE_TINT1
#define TSL2561_REG_INTERRUPT
#define TSL2561_LUX_M1T
#define TSL2561_REG_TIMING
#define TSL2561_LUX_M3T
#define TSL2561_LUX_B2T
#define TSL2561_REG_CONTROL
chip register definition
#define TSL2561_LUX_K3T
#define TSL2561_REG_ID
#define TSL2561_LUX_B5T
#define TSL2561_LUX_K6T
#define CHIP_NAME
chip information definition
#define TSL2561_LUX_B7T
#define DRIVER_VERSION
#define TSL2561_LUX_K4T
#define TSL2561_CONTROL_POWEROFF
#define TSL2561_LUX_K1T
#define TSL2561_LUX_CHSCALE
driver tsl2561 header file
uint8_t tsl2561_read(tsl2561_handle_t *handle, uint16_t *channel_0_raw, uint16_t *channel_1_raw, uint32_t *lux)
read data from the chip
uint8_t tsl2561_deinit(tsl2561_handle_t *handle)
close the chip
uint8_t tsl2561_power_down(tsl2561_handle_t *handle)
power down the chip
uint8_t tsl2561_get_integration_time(tsl2561_handle_t *handle, tsl2561_integration_time_t *t)
get the integration time
uint8_t tsl2561_set_addr_pin(tsl2561_handle_t *handle, tsl2561_address_t addr_pin)
set the iic address pin
uint8_t tsl2561_init(tsl2561_handle_t *handle)
initialize the chip
tsl2561_bool_t
tsl2561 bool enumeration definition
uint8_t tsl2561_get_addr_pin(tsl2561_handle_t *handle, tsl2561_address_t *addr_pin)
get the iic address pin
uint8_t tsl2561_info(tsl2561_info_t *info)
get chip's information
tsl2561_address_t
tsl2561 address enumeration definition
uint8_t tsl2561_set_gain(tsl2561_handle_t *handle, tsl2561_gain_t gain)
set the adc gain
struct tsl2561_handle_s tsl2561_handle_t
tsl2561 handle structure definition
uint8_t tsl2561_get_gain(tsl2561_handle_t *handle, tsl2561_gain_t *gain)
get the adc gain
uint8_t tsl2561_set_integration_time(tsl2561_handle_t *handle, tsl2561_integration_time_t t)
set the integration time
tsl2561_gain_t
tsl2561 gain enumeration definition
struct tsl2561_info_s tsl2561_info_t
tsl2561 information structure definition
tsl2561_integration_time_t
tsl2561 integration time enumeration definition
uint8_t tsl2561_wake_up(tsl2561_handle_t *handle)
wake up the chip
uint8_t tsl2561_get_reg(tsl2561_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t tsl2561_set_reg(tsl2561_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t tsl2561_set_interrupt_mode(tsl2561_handle_t *handle, tsl2561_interrupt_mode_t mode)
set the interrupt mode
uint8_t tsl2561_set_interrupt_low_threshold(tsl2561_handle_t *handle, uint16_t ch0_raw)
set the interrupt low threshold
uint8_t tsl2561_set_interrupt_high_threshold(tsl2561_handle_t *handle, uint16_t ch0_raw)
set the interrupt high threshold
tsl2561_interrupt_mode_t
tsl2561 interrupt mode enumeration definition
uint8_t tsl2561_get_interrupt_low_threshold(tsl2561_handle_t *handle, uint16_t *ch0_raw)
get the interrupt low threshold
uint8_t tsl2561_get_interrupt(tsl2561_handle_t *handle, tsl2561_bool_t *enable)
get the chip interrupt
uint8_t tsl2561_set_interrupt(tsl2561_handle_t *handle, tsl2561_bool_t enable)
enable or disable the chip interrupt
uint8_t tsl2561_get_interrupt_mode(tsl2561_handle_t *handle, tsl2561_interrupt_mode_t *mode)
get the interrupt mode
uint8_t tsl2561_get_interrupt_high_threshold(tsl2561_handle_t *handle, uint16_t *ch0_raw)
get the interrupt high threshold
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)
uint32_t driver_version
char manufacturer_name[32]