LibDriver SHT30
Loading...
Searching...
No Matches
driver_sht30.c
Go to the documentation of this file.
1
37
38#include "driver_sht30.h"
39
43#define CHIP_NAME "Sensirion SHT30"
44#define MANUFACTURER_NAME "Sensirion"
45#define SUPPLY_VOLTAGE_MIN 2.4f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 1.5f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 125.0f
50#define DRIVER_VERSION 2000
51
55#define SHT30_COMMAND_FETCH_DATA 0xE000U
56#define SHT30_COMMAND_ART 0x2B32U
57#define SHT30_COMMAND_BREAK 0x3093U
58#define SHT30_COMMAND_SOFT_RESET 0x30A2U
59#define SHT30_COMMAND_HEATER_ENABLE 0x306DU
60#define SHT30_COMMAND_HEATER_DISABLE 0x3066U
61#define SHT30_COMMAND_READ_STATUS 0xF32DU
62#define SHT30_COMMAND_CLEAR_STATUS 0x3041U
63#define SHT30_COMMAND_GET_SERIAL_NUMBER 0x3682U
64#define SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_SET 0xE11FU
65#define SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_CLEAR 0xE114U
66#define SHT30_COMMAND_READ_LOW_ALERT_LIMIT_SET 0xE102U
67#define SHT30_COMMAND_READ_LOW_ALERT_LIMIT_CLEAR 0xE109U
68#define SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_SET 0x611DU
69#define SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_CLEAR 0x6116U
70#define SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_SET 0x6100U
71#define SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_CLEAR 0x610BU
72
82static uint8_t a_sht30_write(sht30_handle_t *handle, uint16_t cmd)
83{
84 if (handle->iic_write_address16(handle->iic_addr, cmd, NULL, 0) != 0) /* iic write */
85 {
86 return 1; /* return error */
87 }
88 else
89 {
90 return 0; /* success return 0 */
91 }
92}
93
105static uint8_t a_sht30_write_bytes(sht30_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
106{
107 if (handle->iic_write_address16(handle->iic_addr, reg, data, len) != 0) /* iic write */
108 {
109 return 1; /* return error */
110 }
111 else
112 {
113 return 0; /* success return 0 */
114 }
115}
116
128static uint8_t a_sht30_read(sht30_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
129{
130 if (handle->iic_read_address16(handle->iic_addr, reg, data, len) != 0) /* iic read */
131 {
132 return 1; /* return error */
133 }
134 else
135 {
136 return 0; /* success return 0 */
137 }
138}
139
147static uint8_t a_sht30_crc(uint8_t *data, uint16_t len)
148{
149 const uint8_t POLYNOMIAL = 0x31;
150 uint8_t crc = 0xFF;
151 uint16_t i, j;
152
153 for (j = len; j != 0; --j) /* length-- */
154 {
155 crc ^= *data++; /* xor */
156 for (i = 8; i != 0; --i) /* 8 times */
157 {
158 crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc<<1); /* calculate crc */
159 }
160 }
161
162 return crc; /* return crc */
163}
164
176{
177 uint8_t res;
178 uint16_t command;
179
180 if (handle == NULL) /* check handle */
181 {
182 return 2; /* return error */
183 }
184 if (handle->debug_print == NULL) /* check debug_print */
185 {
186 return 3; /* return error */
187 }
188 if (handle->iic_init == NULL) /* check iic_init */
189 {
190 handle->debug_print("sht30: iic_init is null.\n"); /* iic_init is null */
191
192 return 3; /* return error */
193 }
194 if (handle->iic_deinit == NULL) /* check iic_deinit */
195 {
196 handle->debug_print("sht30: iic_deinit is null.\n"); /* iic_deinit is null */
197
198 return 3; /* return error */
199 }
200 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
201 {
202 handle->debug_print("sht30: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
203
204 return 3; /* return error */
205 }
206 if (handle->iic_write_address16 == NULL) /* check iic_write_address16 */
207 {
208 handle->debug_print("sht30: iic_write_address16 is null.\n"); /* iic_write_address16 is null */
209
210 return 3; /* return error */
211 }
212 if (handle->delay_ms == NULL) /* check delay_ms */
213 {
214 handle->debug_print("sht30: delay_ms is null.\n"); /* delay_ms is null */
215
216 return 3; /* return error */
217 }
218
219 if (handle->iic_init() != 0) /* iic init */
220 {
221 handle->debug_print("sht30: iic init failed.\n"); /* iic init failed */
222
223 return 1; /* return error */
224 }
225 command = SHT30_COMMAND_SOFT_RESET; /* set command */
226 res = a_sht30_write(handle, command); /* write command */
227 if (res != 0) /* check result */
228 {
229 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
230 (void)handle->iic_deinit(); /* close iic */
231
232 return 1; /* return error */
233 }
234 handle->delay_ms(2); /* delay 2ms */
235 handle->inited = 1; /* flag finish initialization */
236
237 return 0; /* success return 0 */
238}
239
251{
252 uint8_t res;
253 uint16_t command;
254
255 if (handle == NULL) /* check handle */
256 {
257 return 2; /* return error */
258 }
259 if (handle->inited != 1) /* check handle initialization */
260 {
261 return 3; /* return error */
262 }
263
264 command = SHT30_COMMAND_BREAK; /* set command */
265 res = a_sht30_write(handle, command); /* write command */
266 if (res != 0) /* check result */
267 {
268 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
269
270 return 1; /* return error */
271 }
272 if (handle->iic_deinit() != 0) /* iic deinit */
273 {
274 handle->debug_print("sht30: iic deinit failed.\n"); /* iic deinit failed */
275
276 return 1; /* return error */
277 }
278 handle->inited = 0; /* flag close */
279
280 return 0; /* success return 0 */
281}
282
294{
295 if (handle == NULL) /* check handle */
296 {
297 return 2; /* return error */
298 }
299
300 handle->iic_addr = (uint8_t)addr_pin; /* set address pin */
301
302 return 0; /* success return 0 */
303}
304
316{
317 if (handle == NULL) /* check handle */
318 {
319 return 2; /* return error */
320 }
321
322 *addr_pin = (sht30_address_t)(handle->iic_addr); /* get address pin */
323
324 return 0; /* success return 0 */
325}
326
338uint8_t sht30_get_status(sht30_handle_t *handle, uint16_t *status)
339{
340 uint8_t res;
341 uint8_t data[3];
342 uint16_t command;
343
344 if (handle == NULL) /* check handle */
345 {
346 return 2; /* return error */
347 }
348 if (handle->inited != 1) /* check handle initialization */
349 {
350 return 3; /* return error */
351 }
352
353 memset(data, 0, sizeof(uint8_t) * 3); /* clear the buffer */
354 command = SHT30_COMMAND_READ_STATUS; /* set command */
355 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read status */
356 if (res != 0) /* check result */
357 {
358 handle->debug_print("sht30: read status failed.\n"); /* read status failed */
359
360 return 1; /* return error */
361 }
362 if (a_sht30_crc((uint8_t *)data, 2) == data[2]) /* check crc */
363 {
364 *status = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get status */
365
366 return 0; /* success return 0 */
367 }
368 else
369 {
370 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
371
372 return 1; /* return error */
373 }
374}
375
387{
388 uint8_t res;
389 uint16_t command;
390
391 if (handle == NULL) /* check handle */
392 {
393 return 2; /* return error */
394 }
395 if (handle->inited != 1) /* check handle initialization */
396 {
397 return 3; /* return error */
398 }
399
400 command = SHT30_COMMAND_CLEAR_STATUS; /* set command */
401 res = a_sht30_write(handle, command); /* write command */
402 if (res != 0) /* check result */
403 {
404 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
405
406 return 1; /* return error */
407 }
408
409 return 0; /* success return 0 */
410}
411
423{
424 if (handle == NULL) /* check handle */
425 {
426 return 2; /* return error */
427 }
428 if (handle->inited != 1) /* check handle initialization */
429 {
430 return 3; /* return error */
431 }
432
433 handle->repeatability = (uint8_t)repeatability; /* set repeatability */
434
435 return 0; /* success return 0 */
436}
437
449{
450 if (handle == NULL) /* check handle */
451 {
452 return 2; /* return error */
453 }
454 if (handle->inited != 1) /* check handle initialization */
455 {
456 return 3; /* return error */
457 }
458
459 *repeatability = (sht30_repeatability_t)(handle->repeatability); /* get repeatability */
460
461 return 0; /* success return 0 */
462}
463
479uint8_t sht30_single_read(sht30_handle_t *handle, sht30_bool_t clock_stretching_enable,
480 uint16_t *temperature_raw, float *temperature_s,
481 uint16_t *humidity_raw, float *humidity_s
482 )
483{
484 uint8_t res;
485 uint16_t command;
486 uint8_t data[6];
487
488 if (handle == NULL) /* check handle */
489 {
490 return 2; /* return error */
491 }
492 if (handle->inited != 1) /* check handle initialization */
493 {
494 return 3; /* return error */
495 }
496
497 if (handle->repeatability == SHT30_REPEATABILITY_HIGH) /* repeatability high */
498 {
499 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
500 {
501 command = 0x2400U; /* set disable high */
502 }
503 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
504 {
505 command = 0x2C06U; /* set enable high */
506 }
507 else
508 {
509 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
510
511 return 1; /* return error */
512 }
513 }
514 else if (handle->repeatability == SHT30_REPEATABILITY_MEDIUM) /* repeatability medium */
515 {
516 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
517 {
518 command = 0x240BU; /* set disable medium */
519 }
520 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
521 {
522 command = 0x2C0DU; /* set enable medium */
523 }
524 else
525 {
526 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
527
528 return 1; /* return error */
529 }
530 }
531 else if (handle->repeatability == SHT30_REPEATABILITY_LOW) /* repeatability low */
532 {
533 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
534 {
535 command = 0x2416U; /* set disable low */
536 }
537 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
538 {
539 command = 0x2C10U; /* set enable low */
540 }
541 else
542 {
543 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
544
545 return 1; /* return error */
546 }
547 }
548 else
549 {
550 handle->debug_print("sht30: repeatability is invalid.\n"); /* repeatability is invalid */
551
552 return 1; /* return error */
553 }
554 memset(data, 0, sizeof(uint8_t) * 6); /* clear the buffer */
555 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
556 if (res != 0) /* check result */
557 {
558 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
559
560 return 1; /* return error */
561 }
562 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
563 {
564 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
565
566 return 1; /* return error */
567 }
568 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
569 {
570 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
571
572 return 1; /* return error */
573 }
574 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
575 *humidity_raw = (uint16_t)((((uint16_t)data[3]) << 8) | data[4]); /* get raw humidity */
576 *temperature_s = (float)(*temperature_raw) / 65535.0f * 175.0f - 45.0f; /* convert raw temperature */
577 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
578
579 return 0; /* success return 0 */
580}
581
594{
595 uint8_t res;
596 uint16_t command;
597
598 if (handle == NULL) /* check handle */
599 {
600 return 2; /* return error */
601 }
602 if (handle->inited != 1) /* check handle initialization */
603 {
604 return 3; /* return error */
605 }
606
607 if (handle->repeatability == SHT30_REPEATABILITY_HIGH) /* repeatability high */
608 {
609 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
610 {
611 command = 0x2032U; /* set 0.5Hz high */
612 }
613 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
614 {
615 command = 0x2130U; /* set 1Hz high */
616 }
617 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
618 {
619 command = 0x2236U; /* set 2Hz high */
620 }
621 else if (rate == SHT30_RATE_4HZ) /* 4Hz */
622 {
623 command = 0x2334U; /* set 4Hz high */
624 }
625 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
626 {
627 command = 0x2737U; /* set 10Hz high */
628 }
629 else
630 {
631 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
632
633 return 1; /* return error */
634 }
635 }
636 else if (handle->repeatability == SHT30_REPEATABILITY_MEDIUM) /* repeatability medium */
637 {
638 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
639 {
640 command = 0x2024U; /* set 0.5Hz medium */
641 }
642 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
643 {
644 command = 0x2126U; /* set 1Hz medium */
645 }
646 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
647 {
648 command = 0x2220U; /* set 2Hz medium */
649 }
650 else if (rate == SHT30_RATE_4HZ) /* 4Hz */
651 {
652 command = 0x2322U; /* set 4Hz medium */
653 }
654 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
655 {
656 command = 0x2721U; /* set 10Hz medium */
657 }
658 else
659 {
660 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
661
662 return 1; /* return error */
663 }
664 }
665 else if (handle->repeatability == SHT30_REPEATABILITY_LOW) /* repeatability low */
666 {
667 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
668 {
669 command = 0x202FU; /* set 0.5Hz low */
670 }
671 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
672 {
673 command = 0x212DU; /* set 1Hz low */
674 }
675 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
676 {
677 command = 0x222BU; /* set 2Hz low */
678 }
679 else if (rate == SHT30_RATE_4HZ) /* set 4Hz */
680 {
681 command = 0x2329U; /* set 4Hz low */
682 }
683 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
684 {
685 command = 0x272AU; /* set 10Hz low */
686 }
687 else
688 {
689 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
690
691 return 1; /* return error */
692 }
693 }
694 else
695 {
696 handle->debug_print("sht30: repeatability is invalid.\n"); /* repeatability is invalid */
697
698 return 1; /* return error */
699 }
700 res = a_sht30_write(handle, command); /* write command */
701 if (res != 0) /* check result */
702 {
703 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
704
705 return 1; /* return error */
706 }
707
708 return 0; /* success return 0 */
709}
710
722{
723 uint8_t res;
724 uint16_t command;
725
726 if (handle == NULL) /* check handle */
727 {
728 return 2; /* return error */
729 }
730 if (handle->inited != 1) /* check handle initialization */
731 {
732 return 3; /* return error */
733 }
734
735 command = SHT30_COMMAND_BREAK; /* set command */
736 res = a_sht30_write(handle, command); /* write command */
737 if (res != 0) /* check result */
738 {
739 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
740
741 return 1; /* return error */
742 }
743
744 return 0; /* success return 0 */
745}
746
762 uint16_t *temperature_raw, float *temperature_s,
763 uint16_t *humidity_raw, float *humidity_s)
764{
765 uint8_t res;
766 uint16_t command;
767 uint8_t data[6];
768
769 if (handle == NULL) /* check handle */
770 {
771 return 2; /* return error */
772 }
773 if (handle->inited != 1) /* check handle initialization */
774 {
775 return 3; /* return error */
776 }
777
778 command = SHT30_COMMAND_FETCH_DATA; /* set fetch data */
779 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
780 if (res != 0) /* check result */
781 {
782 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
783
784 return 1; /* return error */
785 }
786 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
787 {
788 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
789
790 return 1; /* return error */
791 }
792 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
793 {
794 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
795
796 return 1; /* return error */
797 }
798 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
799 *humidity_raw = (uint16_t)((((uint16_t)data[3]) << 8) | data[4]); /* get raw humidity */
800 *temperature_s = (float)(*temperature_raw) / 65535.0f * 175.0f - 45.0f; /* convert raw temperature */
801 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
802
803 return 0; /* success return 0 */
804}
805
817{
818 uint8_t res;
819 uint16_t command;
820
821 if (handle == NULL) /* check handle */
822 {
823 return 2; /* return error */
824 }
825 if (handle->inited != 1) /* check handle initialization */
826 {
827 return 3; /* return error */
828 }
829
830 command = SHT30_COMMAND_ART; /* set command */
831 res = a_sht30_write(handle, command); /* write command */
832 if (res != 0) /* check result */
833 {
834 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
835
836 return 1; /* return error */
837 }
838
839 return 0; /* success return 0 */
840}
841
853{
854 uint8_t res;
855 uint16_t command;
856
857 if (handle == NULL) /* check handle */
858 {
859 return 2; /* return error */
860 }
861 if (handle->inited != 1) /* check handle initialization */
862 {
863 return 3; /* return error */
864 }
865
866 command = SHT30_COMMAND_SOFT_RESET; /* set command */
867 res = a_sht30_write(handle, command); /* write command */
868 if (res != 0) /* check result */
869 {
870 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
871
872 return 1; /* return error */
873 }
874 handle->delay_ms(2); /* delay 2ms */
875
876 return 0; /* success return 0 */
877}
878
891{
892 uint8_t res;
893 uint16_t command;
894
895 if (handle == NULL) /* check handle */
896 {
897 return 2; /* return error */
898 }
899 if (handle->inited != 1) /* check handle initialization */
900 {
901 return 3; /* return error */
902 }
903
904 if (enable == SHT30_BOOL_TRUE) /* enable heater */
905 {
906 command = SHT30_COMMAND_HEATER_ENABLE; /* set enable */
907 }
908 else if (enable == SHT30_BOOL_FALSE) /* disable heater */
909 {
910 command = SHT30_COMMAND_HEATER_DISABLE; /* set disable */
911 }
912 else
913 {
914 handle->debug_print("sht30: bool is invalid.\n"); /* bool is invalid */
915
916 return 1; /* return error */
917 }
918 res = a_sht30_write(handle, command); /* write command */
919 if (res != 0) /* check result */
920 {
921 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
922
923 return 1; /* return error */
924 }
925
926 return 0; /* success return 0 */
927}
928
940uint8_t sht30_get_serial_number(sht30_handle_t *handle, uint8_t sn[4])
941{
942 uint8_t res;
943 uint16_t command;
944 uint8_t data[6];
945
946 if (handle == NULL) /* check handle */
947 {
948 return 2; /* return error */
949 }
950 if (handle->inited != 1) /* check handle initialization */
951 {
952 return 3; /* return error */
953 }
954
955 command = SHT30_COMMAND_GET_SERIAL_NUMBER; /* get serial number command */
956 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
957 if (res != 0) /* check result */
958 {
959 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
960
961 return 1; /* return error */
962 }
963 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
964 {
965 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
966
967 return 1; /* return error */
968 }
969 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
970 {
971 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
972
973 return 1; /* return error */
974 }
975 sn[0] = data[4]; /* set sn0 */
976 sn[1] = data[3]; /* set sn1 */
977 sn[2] = data[1]; /* set sn2 */
978 sn[3] = data[0]; /* set sn3 */
979
980 return 0; /* success return 0 */
981}
982
995uint8_t sht30_set_high_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
996{
997 uint8_t res;
998 uint16_t command;
999 uint8_t data[3];
1000
1001 if (handle == NULL) /* check handle */
1002 {
1003 return 2; /* return error */
1004 }
1005 if (handle->inited != 1) /* check handle initialization */
1006 {
1007 return 3; /* return error */
1008 }
1009
1010 command = SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_SET; /* write high alert limit set command */
1011 data[0] = (set >> 8) & 0xFF; /* set data 0 */
1012 data[1] = (set >> 0) & 0xFF; /* set data 1 */
1013 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1014 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1015 if (res != 0) /* check result */
1016 {
1017 handle->debug_print("sht30: write failed.\n"); /* write failed */
1018
1019 return 1; /* return error */
1020 }
1021 handle->delay_ms(10); /* delay 10ms */
1022 command = SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_CLEAR; /* write high alert limit clear command */
1023 data[0] = (clear >> 8) & 0xFF; /* set data 0 */
1024 data[1] = (clear >> 0) & 0xFF; /* set data 1 */
1025 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1026 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1027 if (res != 0) /* check result */
1028 {
1029 handle->debug_print("sht30: write failed.\n"); /* write failed */
1030
1031 return 1; /* return error */
1032 }
1033
1034 return 0; /* success return 0 */
1035}
1036
1049uint8_t sht30_get_high_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
1050{
1051 uint8_t res;
1052 uint16_t command;
1053 uint8_t data[3];
1054
1055 if (handle == NULL) /* check handle */
1056 {
1057 return 2; /* return error */
1058 }
1059 if (handle->inited != 1) /* check handle initialization */
1060 {
1061 return 3; /* return error */
1062 }
1063
1064 command = SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_SET; /* read high alert limit set command */
1065 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1066 if (res != 0) /* check result */
1067 {
1068 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1069
1070 return 1; /* return error */
1071 }
1072 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1073 {
1074 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1075
1076 return 1; /* return error */
1077 }
1078 *set = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1079
1080 command = SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_CLEAR; /* read high alert limit clear command */
1081 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1082 if (res != 0) /* check result */
1083 {
1084 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1085
1086 return 1; /* return error */
1087 }
1088 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1089 {
1090 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1091
1092 return 1; /* return error */
1093 }
1094 *clear = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1095
1096 return 0; /* success return 0 */
1097}
1098
1111uint8_t sht30_set_low_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
1112{
1113 uint8_t res;
1114 uint16_t command;
1115 uint8_t data[3];
1116
1117 if (handle == NULL) /* check handle */
1118 {
1119 return 2; /* return error */
1120 }
1121 if (handle->inited != 1) /* check handle initialization */
1122 {
1123 return 3; /* return error */
1124 }
1125
1126 command = SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_SET; /* write low alert limit set command */
1127 data[0] = (set >> 8) & 0xFF; /* set data 0 */
1128 data[1] = (set >> 0) & 0xFF; /* set data 1 */
1129 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1130 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1131 if (res != 0) /* check result */
1132 {
1133 handle->debug_print("sht30: write failed.\n"); /* write failed */
1134
1135 return 1; /* return error */
1136 }
1137 handle->delay_ms(10); /* delay 10ms */
1138 command = SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_CLEAR; /* write low alert limit clear command */
1139 data[0] = (clear >> 8) & 0xFF; /* set data 0 */
1140 data[1] = (clear >> 0) & 0xFF; /* set data 1 */
1141 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1142 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1143 if (res != 0) /* check result */
1144 {
1145 handle->debug_print("sht30: write failed.\n"); /* write failed */
1146
1147 return 1; /* return error */
1148 }
1149
1150 return 0; /* success return 0 */
1151}
1152
1165uint8_t sht30_get_low_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
1166{
1167 uint8_t res;
1168 uint16_t command;
1169 uint8_t data[3];
1170
1171 if (handle == NULL) /* check handle */
1172 {
1173 return 2; /* return error */
1174 }
1175 if (handle->inited != 1) /* check handle initialization */
1176 {
1177 return 3; /* return error */
1178 }
1179
1180 command = SHT30_COMMAND_READ_LOW_ALERT_LIMIT_SET; /* read low alert limit set command */
1181 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1182 if (res != 0) /* check result */
1183 {
1184 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1185
1186 return 1; /* return error */
1187 }
1188 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1189 {
1190 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1191
1192 return 1; /* return error */
1193 }
1194 *set = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1195
1196 command = SHT30_COMMAND_READ_LOW_ALERT_LIMIT_CLEAR; /* read low alert limit clear command */
1197 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1198 if (res != 0) /* check result */
1199 {
1200 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1201
1202 return 1; /* return error */
1203 }
1204 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1205 {
1206 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1207
1208 return 1; /* return error */
1209 }
1210 *clear = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1211
1212 return 0; /* success return 0 */
1213}
1214
1227uint8_t sht30_alert_limit_convert_to_register(sht30_handle_t *handle, float temperature, float humidity, uint16_t *reg)
1228{
1229 uint16_t r_t;
1230 uint16_t r_h;
1231
1232 if (handle == NULL) /* check handle */
1233 {
1234 return 2; /* return error */
1235 }
1236 if (handle->inited != 1) /* check handle initialization */
1237 {
1238 return 3; /* return error */
1239 }
1240
1241 r_t = (uint16_t)(((temperature + 45.0f) / 175.0f) * 65535.0f); /* convert to raw */
1242 r_t = r_t >> 7; /* convert temperature */
1243 r_h = (uint16_t)((humidity / 100.0f) * 65535.0f); /* convert to raw */
1244 r_h = r_h >> 9; /* convert humidity */
1245 *reg = (r_h << 9) | r_t; /* set register */
1246
1247 return 0; /* success return 0 */
1248}
1249
1261{
1262 uint8_t res;
1263 uint8_t data[3];
1264 uint16_t status;
1265 uint16_t command;
1266
1267 if (handle == NULL) /* check handle */
1268 {
1269 return 2; /* return error */
1270 }
1271 if (handle->inited != 1) /* check handle initialization */
1272 {
1273 return 3; /* return error */
1274 }
1275
1276 memset(data, 0, sizeof(uint8_t) * 3); /* clear the buffer */
1277 command = SHT30_COMMAND_READ_STATUS; /* set command */
1278 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read status */
1279 if (res != 0) /* check result */
1280 {
1281 handle->debug_print("sht30: read status failed.\n"); /* read status failed */
1282
1283 return 1; /* return error */
1284 }
1285 if (a_sht30_crc((uint8_t *)data, 2) == data[2]) /* check crc */
1286 {
1287 status = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get status */
1288 command = SHT30_COMMAND_CLEAR_STATUS; /* set command */
1289 res = a_sht30_write(handle, command); /* write command */
1290 if (res != 0) /* check result */
1291 {
1292 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
1293
1294 return 1; /* return error */
1295 }
1296
1297 if ((status & SHT30_STATUS_ALERT_PENDING_STATUS) != 0) /* check alert pending status */
1298 {
1299 if (handle->receive_callback != NULL) /* check the receive callback */
1300 {
1301 handle->receive_callback(SHT30_STATUS_ALERT_PENDING_STATUS); /* run the callback */
1302 }
1303 }
1304 if ((status & SHT30_STATUS_HUMIDITY_ALERT) != 0) /* check humidity alert */
1305 {
1306 if (handle->receive_callback != NULL) /* check the receive callback */
1307 {
1308 handle->receive_callback(SHT30_STATUS_HUMIDITY_ALERT); /* run the callback */
1309 }
1310 }
1311 if ((status & SHT30_STATUS_TEMPERATURE_ALERT) != 0) /* check temperature alert */
1312 {
1313 if (handle->receive_callback != NULL) /* check the receive callback */
1314 {
1315 handle->receive_callback(SHT30_STATUS_TEMPERATURE_ALERT); /* run the callback */
1316 }
1317 }
1318
1319 return 0; /* success return 0 */
1320 }
1321 else
1322 {
1323 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1324
1325 return 1; /* return error */
1326 }
1327}
1328
1340uint8_t sht30_set_reg(sht30_handle_t *handle, uint16_t command)
1341{
1342 if (handle == NULL) /* check handle */
1343 {
1344 return 2; /* return error */
1345 }
1346 if (handle->inited != 1) /* check handle initialization */
1347 {
1348 return 3; /* return error */
1349 }
1350
1351 return a_sht30_write(handle, command); /* write command */
1352}
1353
1367uint8_t sht30_get_reg(sht30_handle_t *handle, uint16_t command, uint8_t *buf, uint16_t len)
1368{
1369 if (handle == NULL) /* check handle */
1370 {
1371 return 2; /* return error */
1372 }
1373 if (handle->inited != 1) /* check handle initialization */
1374 {
1375 return 3; /* return error */
1376 }
1377
1378 return a_sht30_read(handle, command, buf, len); /* read data */
1379}
1380
1390{
1391 if (info == NULL) /* check handle */
1392 {
1393 return 2; /* return error */
1394 }
1395
1396 memset(info, 0, sizeof(sht30_info_t)); /* initialize sht30 info structure */
1397 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1398 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1399 strncpy(info->interface, "IIC", 8); /* copy interface name */
1400 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1401 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1402 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1403 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1404 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1405 info->driver_version = DRIVER_VERSION; /* set driver version */
1406
1407 return 0; /* success return 0 */
1408}
#define SHT30_COMMAND_READ_STATUS
#define SHT30_COMMAND_BREAK
#define MAX_CURRENT
#define SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_CLEAR
#define SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_CLEAR
#define SUPPLY_VOLTAGE_MAX
#define SHT30_COMMAND_READ_LOW_ALERT_LIMIT_CLEAR
#define SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_SET
#define TEMPERATURE_MAX
#define SHT30_COMMAND_SOFT_RESET
#define SHT30_COMMAND_GET_SERIAL_NUMBER
#define SHT30_COMMAND_FETCH_DATA
chip command definition
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_CLEAR
#define SHT30_COMMAND_HEATER_DISABLE
#define SHT30_COMMAND_ART
#define SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_SET
#define CHIP_NAME
chip information definition
#define SHT30_COMMAND_HEATER_ENABLE
#define DRIVER_VERSION
#define SHT30_COMMAND_READ_LOW_ALERT_LIMIT_SET
#define SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_SET
#define SHT30_COMMAND_CLEAR_STATUS
driver sht30 header file
uint8_t sht30_stop_continuous_read(sht30_handle_t *handle)
stop reading
uint8_t sht30_info(sht30_info_t *info)
get chip's information
uint8_t sht30_start_continuous_read(sht30_handle_t *handle, sht30_rate_t rate)
start reading
uint8_t sht30_set_repeatability(sht30_handle_t *handle, sht30_repeatability_t repeatability)
set the measurement repeatability
sht30_rate_t
sht30 rate enumeration definition
sht30_repeatability_t
sht30 repeatability enumeration definition
uint8_t sht30_get_addr_pin(sht30_handle_t *handle, sht30_address_t *addr_pin)
get the iic address pin
uint8_t sht30_single_read(sht30_handle_t *handle, sht30_bool_t clock_stretching_enable, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data once
uint8_t sht30_init(sht30_handle_t *handle)
initialize the chip
uint8_t sht30_soft_reset(sht30_handle_t *handle)
soft reset the chip
uint8_t sht30_get_low_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
get low alert limit
sht30_bool_t
sht30 bool enumeration definition
uint8_t sht30_set_art(sht30_handle_t *handle)
set the chip art
sht30_address_t
sht30 address enumeration definition
struct sht30_info_s sht30_info_t
sht30 information structure definition
uint8_t sht30_alert_limit_convert_to_register(sht30_handle_t *handle, float temperature, float humidity, uint16_t *reg)
alert limit convert to register raw data
uint8_t sht30_set_heater(sht30_handle_t *handle, sht30_bool_t enable)
enable or disable the chip heater
uint8_t sht30_get_status(sht30_handle_t *handle, uint16_t *status)
get the current status
uint8_t sht30_irq_handler(sht30_handle_t *handle)
irq handler
uint8_t sht30_set_addr_pin(sht30_handle_t *handle, sht30_address_t addr_pin)
set the iic address pin
uint8_t sht30_continuous_read(sht30_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data continuously
uint8_t sht30_set_high_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
set high alert limit
struct sht30_handle_s sht30_handle_t
sht30 handle structure definition
uint8_t sht30_get_high_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
get high alert limit
uint8_t sht30_get_repeatability(sht30_handle_t *handle, sht30_repeatability_t *repeatability)
get the measurement repeatability
uint8_t sht30_get_serial_number(sht30_handle_t *handle, uint8_t sn[4])
get serial number
uint8_t sht30_clear_status(sht30_handle_t *handle)
clear the current status
uint8_t sht30_set_low_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
set low alert limit
uint8_t sht30_deinit(sht30_handle_t *handle)
close the chip
@ SHT30_RATE_4HZ
@ SHT30_RATE_10HZ
@ SHT30_RATE_2HZ
@ SHT30_RATE_0P5HZ
@ SHT30_RATE_1HZ
@ SHT30_REPEATABILITY_MEDIUM
@ SHT30_REPEATABILITY_LOW
@ SHT30_REPEATABILITY_HIGH
@ SHT30_BOOL_TRUE
@ SHT30_BOOL_FALSE
@ SHT30_STATUS_ALERT_PENDING_STATUS
@ SHT30_STATUS_HUMIDITY_ALERT
@ SHT30_STATUS_TEMPERATURE_ALERT
uint8_t sht30_get_reg(sht30_handle_t *handle, uint16_t command, uint8_t *buf, uint16_t len)
get the chip register
uint8_t sht30_set_reg(sht30_handle_t *handle, uint16_t command)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t repeatability
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
void(* receive_callback)(uint16_t type)
uint8_t(* iic_init)(void)
uint8_t(* iic_write_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]