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->inited = 1; /* flag finish initialization */
235
236 return 0; /* success return 0 */
237}
238
250{
251 uint8_t res;
252 uint16_t command;
253
254 if (handle == NULL) /* check handle */
255 {
256 return 2; /* return error */
257 }
258 if (handle->inited != 1) /* check handle initialization */
259 {
260 return 3; /* return error */
261 }
262
263 command = SHT30_COMMAND_BREAK; /* set command */
264 res = a_sht30_write(handle, command); /* write command */
265 if (res != 0) /* check result */
266 {
267 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
268
269 return 1; /* return error */
270 }
271 if (handle->iic_deinit() != 0) /* iic deinit */
272 {
273 handle->debug_print("sht30: iic deinit failed.\n"); /* iic deinit failed */
274
275 return 1; /* return error */
276 }
277 handle->inited = 0; /* flag close */
278
279 return 0; /* success return 0 */
280}
281
293{
294 if (handle == NULL) /* check handle */
295 {
296 return 2; /* return error */
297 }
298
299 handle->iic_addr = (uint8_t)addr_pin; /* set address pin */
300
301 return 0; /* success return 0 */
302}
303
315{
316 if (handle == NULL) /* check handle */
317 {
318 return 2; /* return error */
319 }
320
321 *addr_pin = (sht30_address_t)(handle->iic_addr); /* get address pin */
322
323 return 0; /* success return 0 */
324}
325
337uint8_t sht30_get_status(sht30_handle_t *handle, uint16_t *status)
338{
339 uint8_t res;
340 uint8_t data[3];
341 uint16_t command;
342
343 if (handle == NULL) /* check handle */
344 {
345 return 2; /* return error */
346 }
347 if (handle->inited != 1) /* check handle initialization */
348 {
349 return 3; /* return error */
350 }
351
352 memset(data, 0, sizeof(uint8_t) * 3); /* clear the buffer */
353 command = SHT30_COMMAND_READ_STATUS; /* set command */
354 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read status */
355 if (res != 0) /* check result */
356 {
357 handle->debug_print("sht30: read status failed.\n"); /* read status failed */
358
359 return 1; /* return error */
360 }
361 if (a_sht30_crc((uint8_t *)data, 2) == data[2]) /* check crc */
362 {
363 *status = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get status */
364
365 return 0; /* success return 0 */
366 }
367 else
368 {
369 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
370
371 return 1; /* return error */
372 }
373}
374
386{
387 uint8_t res;
388 uint16_t command;
389
390 if (handle == NULL) /* check handle */
391 {
392 return 2; /* return error */
393 }
394 if (handle->inited != 1) /* check handle initialization */
395 {
396 return 3; /* return error */
397 }
398
399 command = SHT30_COMMAND_CLEAR_STATUS; /* set command */
400 res = a_sht30_write(handle, command); /* write command */
401 if (res != 0) /* check result */
402 {
403 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
404
405 return 1; /* return error */
406 }
407
408 return 0; /* success return 0 */
409}
410
422{
423 if (handle == NULL) /* check handle */
424 {
425 return 2; /* return error */
426 }
427 if (handle->inited != 1) /* check handle initialization */
428 {
429 return 3; /* return error */
430 }
431
432 handle->repeatability = (uint8_t)repeatability; /* set repeatability */
433
434 return 0; /* success return 0 */
435}
436
448{
449 if (handle == NULL) /* check handle */
450 {
451 return 2; /* return error */
452 }
453 if (handle->inited != 1) /* check handle initialization */
454 {
455 return 3; /* return error */
456 }
457
458 *repeatability = (sht30_repeatability_t)(handle->repeatability); /* get repeatability */
459
460 return 0; /* success return 0 */
461}
462
478uint8_t sht30_single_read(sht30_handle_t *handle, sht30_bool_t clock_stretching_enable,
479 uint16_t *temperature_raw, float *temperature_s,
480 uint16_t *humidity_raw, float *humidity_s
481 )
482{
483 uint8_t res;
484 uint16_t command;
485 uint8_t data[6];
486
487 if (handle == NULL) /* check handle */
488 {
489 return 2; /* return error */
490 }
491 if (handle->inited != 1) /* check handle initialization */
492 {
493 return 3; /* return error */
494 }
495
496 if (handle->repeatability == SHT30_REPEATABILITY_HIGH) /* repeatability high */
497 {
498 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
499 {
500 command = 0x2400U; /* set disable high */
501 }
502 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
503 {
504 command = 0x2C06U; /* set enable high */
505 }
506 else
507 {
508 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
509
510 return 1; /* return error */
511 }
512 }
513 else if (handle->repeatability == SHT30_REPEATABILITY_MEDIUM) /* repeatability medium */
514 {
515 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
516 {
517 command = 0x240BU; /* set disable medium */
518 }
519 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
520 {
521 command = 0x2C0DU; /* set enable medium */
522 }
523 else
524 {
525 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
526
527 return 1; /* return error */
528 }
529 }
530 else if (handle->repeatability == SHT30_REPEATABILITY_LOW) /* repeatability low */
531 {
532 if (clock_stretching_enable == SHT30_BOOL_FALSE) /* if disable clock stretching */
533 {
534 command = 0x2416U; /* set disable low */
535 }
536 else if (clock_stretching_enable == SHT30_BOOL_TRUE) /* if enable clock stretching */
537 {
538 command = 0x2C10U; /* set enable low */
539 }
540 else
541 {
542 handle->debug_print("sht30: clock stretching is invalid.\n"); /* clock stretching is invalid */
543
544 return 1; /* return error */
545 }
546 }
547 else
548 {
549 handle->debug_print("sht30: repeatability is invalid.\n"); /* repeatability is invalid */
550
551 return 1; /* return error */
552 }
553 memset(data, 0, sizeof(uint8_t) * 6); /* clear the buffer */
554 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
555 if (res != 0) /* check result */
556 {
557 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
558
559 return 1; /* return error */
560 }
561 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
562 {
563 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
564
565 return 1; /* return error */
566 }
567 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
568 {
569 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
570
571 return 1; /* return error */
572 }
573 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
574 *humidity_raw = (uint16_t)((((uint16_t)data[3]) << 8) | data[4]); /* get raw humidity */
575 *temperature_s = (float)(*temperature_raw) / 65535.0f * 175.0f - 45.0f; /* convert raw temperature */
576 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
577
578 return 0; /* success return 0 */
579}
580
593{
594 uint8_t res;
595 uint16_t command;
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 if (handle->repeatability == SHT30_REPEATABILITY_HIGH) /* repeatability high */
607 {
608 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
609 {
610 command = 0x2032U; /* set 0.5Hz high */
611 }
612 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
613 {
614 command = 0x2130U; /* set 1Hz high */
615 }
616 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
617 {
618 command = 0x2236U; /* set 2Hz high */
619 }
620 else if (rate == SHT30_RATE_4HZ) /* 4Hz */
621 {
622 command = 0x2334U; /* set 4Hz high */
623 }
624 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
625 {
626 command = 0x2737U; /* set 10Hz high */
627 }
628 else
629 {
630 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
631
632 return 1; /* return error */
633 }
634 }
635 else if (handle->repeatability == SHT30_REPEATABILITY_MEDIUM) /* repeatability medium */
636 {
637 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
638 {
639 command = 0x2024U; /* set 0.5Hz medium */
640 }
641 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
642 {
643 command = 0x2126U; /* set 1Hz medium */
644 }
645 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
646 {
647 command = 0x2220U; /* set 2Hz medium */
648 }
649 else if (rate == SHT30_RATE_4HZ) /* 4Hz */
650 {
651 command = 0x2322U; /* set 4Hz medium */
652 }
653 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
654 {
655 command = 0x2721U; /* set 10Hz medium */
656 }
657 else
658 {
659 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
660
661 return 1; /* return error */
662 }
663 }
664 else if (handle->repeatability == SHT30_REPEATABILITY_LOW) /* repeatability low */
665 {
666 if (rate == SHT30_RATE_0P5HZ) /* 0.5Hz */
667 {
668 command = 0x202FU; /* set 0.5Hz low */
669 }
670 else if (rate == SHT30_RATE_1HZ) /* 1Hz */
671 {
672 command = 0x212DU; /* set 1Hz low */
673 }
674 else if (rate == SHT30_RATE_2HZ) /* 2Hz */
675 {
676 command = 0x222BU; /* set 2Hz low */
677 }
678 else if (rate == SHT30_RATE_4HZ) /* set 4Hz */
679 {
680 command = 0x2329U; /* set 4Hz low */
681 }
682 else if (rate == SHT30_RATE_10HZ) /* 10Hz */
683 {
684 command = 0x272AU; /* set 10Hz low */
685 }
686 else
687 {
688 handle->debug_print("sht30: rate is invalid.\n"); /* rate is invalid */
689
690 return 1; /* return error */
691 }
692 }
693 else
694 {
695 handle->debug_print("sht30: repeatability is invalid.\n"); /* repeatability is invalid */
696
697 return 1; /* return error */
698 }
699 res = a_sht30_write(handle, command); /* write command */
700 if (res != 0) /* check result */
701 {
702 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
703
704 return 1; /* return error */
705 }
706
707 return 0; /* success return 0 */
708}
709
721{
722 uint8_t res;
723 uint16_t command;
724
725 if (handle == NULL) /* check handle */
726 {
727 return 2; /* return error */
728 }
729 if (handle->inited != 1) /* check handle initialization */
730 {
731 return 3; /* return error */
732 }
733
734 command = SHT30_COMMAND_BREAK; /* set command */
735 res = a_sht30_write(handle, command); /* write command */
736 if (res != 0) /* check result */
737 {
738 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
739
740 return 1; /* return error */
741 }
742
743 return 0; /* success return 0 */
744}
745
761 uint16_t *temperature_raw, float *temperature_s,
762 uint16_t *humidity_raw, float *humidity_s)
763{
764 uint8_t res;
765 uint16_t command;
766 uint8_t data[6];
767
768 if (handle == NULL) /* check handle */
769 {
770 return 2; /* return error */
771 }
772 if (handle->inited != 1) /* check handle initialization */
773 {
774 return 3; /* return error */
775 }
776
777 command = SHT30_COMMAND_FETCH_DATA; /* set fetch data */
778 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
779 if (res != 0) /* check result */
780 {
781 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
782
783 return 1; /* return error */
784 }
785 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
786 {
787 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
788
789 return 1; /* return error */
790 }
791 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
792 {
793 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
794
795 return 1; /* return error */
796 }
797 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get raw temperature */
798 *humidity_raw = (uint16_t)((((uint16_t)data[3]) << 8) | data[4]); /* get raw humidity */
799 *temperature_s = (float)(*temperature_raw) / 65535.0f * 175.0f - 45.0f; /* convert raw temperature */
800 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
801
802 return 0; /* success return 0 */
803}
804
816{
817 uint8_t res;
818 uint16_t command;
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 command = SHT30_COMMAND_ART; /* set command */
830 res = a_sht30_write(handle, command); /* write command */
831 if (res != 0) /* check result */
832 {
833 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
834
835 return 1; /* return error */
836 }
837
838 return 0; /* success return 0 */
839}
840
852{
853 uint8_t res;
854 uint16_t command;
855
856 if (handle == NULL) /* check handle */
857 {
858 return 2; /* return error */
859 }
860 if (handle->inited != 1) /* check handle initialization */
861 {
862 return 3; /* return error */
863 }
864
865 command = SHT30_COMMAND_SOFT_RESET; /* set command */
866 res = a_sht30_write(handle, command); /* write command */
867 if (res != 0) /* check result */
868 {
869 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
870
871 return 1; /* return error */
872 }
873
874 return 0; /* success return 0 */
875}
876
889{
890 uint8_t res;
891 uint16_t command;
892
893 if (handle == NULL) /* check handle */
894 {
895 return 2; /* return error */
896 }
897 if (handle->inited != 1) /* check handle initialization */
898 {
899 return 3; /* return error */
900 }
901
902 if (enable == SHT30_BOOL_TRUE) /* enable heater */
903 {
904 command = SHT30_COMMAND_HEATER_ENABLE; /* set enable */
905 }
906 else if (enable == SHT30_BOOL_FALSE) /* disable heater */
907 {
908 command = SHT30_COMMAND_HEATER_DISABLE; /* set disable */
909 }
910 else
911 {
912 handle->debug_print("sht30: bool is invalid.\n"); /* bool is invalid */
913
914 return 1; /* return error */
915 }
916 res = a_sht30_write(handle, command); /* write command */
917 if (res != 0) /* check result */
918 {
919 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
920
921 return 1; /* return error */
922 }
923
924 return 0; /* success return 0 */
925}
926
938uint8_t sht30_get_serial_number(sht30_handle_t *handle, uint8_t sn[4])
939{
940 uint8_t res;
941 uint16_t command;
942 uint8_t data[6];
943
944 if (handle == NULL) /* check handle */
945 {
946 return 2; /* return error */
947 }
948 if (handle->inited != 1) /* check handle initialization */
949 {
950 return 3; /* return error */
951 }
952
953 command = SHT30_COMMAND_GET_SERIAL_NUMBER; /* get serial number command */
954 res = a_sht30_read(handle, command, (uint8_t *)data, 6); /* read data */
955 if (res != 0) /* check result */
956 {
957 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
958
959 return 1; /* return error */
960 }
961 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
962 {
963 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
964
965 return 1; /* return error */
966 }
967 if (a_sht30_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
968 {
969 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
970
971 return 1; /* return error */
972 }
973 sn[0] = data[4]; /* set sn0 */
974 sn[1] = data[3]; /* set sn1 */
975 sn[2] = data[1]; /* set sn2 */
976 sn[3] = data[0]; /* set sn3 */
977
978 return 0; /* success return 0 */
979}
980
993uint8_t sht30_set_high_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
994{
995 uint8_t res;
996 uint16_t command;
997 uint8_t data[3];
998
999 if (handle == NULL) /* check handle */
1000 {
1001 return 2; /* return error */
1002 }
1003 if (handle->inited != 1) /* check handle initialization */
1004 {
1005 return 3; /* return error */
1006 }
1007
1008 command = SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_SET; /* write high alert limit set command */
1009 data[0] = (set >> 8) & 0xFF; /* set data 0 */
1010 data[1] = (set >> 0) & 0xFF; /* set data 1 */
1011 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1012 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1013 if (res != 0) /* check result */
1014 {
1015 handle->debug_print("sht30: write failed.\n"); /* write failed */
1016
1017 return 1; /* return error */
1018 }
1019 handle->delay_ms(10); /* delay 10ms */
1020 command = SHT30_COMMAND_WRITE_HIGH_ALERT_LIMIT_CLEAR; /* write high alert limit clear command */
1021 data[0] = (clear >> 8) & 0xFF; /* set data 0 */
1022 data[1] = (clear >> 0) & 0xFF; /* set data 1 */
1023 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1024 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1025 if (res != 0) /* check result */
1026 {
1027 handle->debug_print("sht30: write failed.\n"); /* write failed */
1028
1029 return 1; /* return error */
1030 }
1031
1032 return 0; /* success return 0 */
1033}
1034
1047uint8_t sht30_get_high_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
1048{
1049 uint8_t res;
1050 uint16_t command;
1051 uint8_t data[3];
1052
1053 if (handle == NULL) /* check handle */
1054 {
1055 return 2; /* return error */
1056 }
1057 if (handle->inited != 1) /* check handle initialization */
1058 {
1059 return 3; /* return error */
1060 }
1061
1062 command = SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_SET; /* read high alert limit set command */
1063 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1064 if (res != 0) /* check result */
1065 {
1066 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1067
1068 return 1; /* return error */
1069 }
1070 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1071 {
1072 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1073
1074 return 1; /* return error */
1075 }
1076 *set = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1077
1078 command = SHT30_COMMAND_READ_HIGH_ALERT_LIMIT_CLEAR; /* read high alert limit clear command */
1079 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1080 if (res != 0) /* check result */
1081 {
1082 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1083
1084 return 1; /* return error */
1085 }
1086 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1087 {
1088 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1089
1090 return 1; /* return error */
1091 }
1092 *clear = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1093
1094 return 0; /* success return 0 */
1095}
1096
1109uint8_t sht30_set_low_alert_limit(sht30_handle_t *handle, uint16_t set, uint16_t clear)
1110{
1111 uint8_t res;
1112 uint16_t command;
1113 uint8_t data[3];
1114
1115 if (handle == NULL) /* check handle */
1116 {
1117 return 2; /* return error */
1118 }
1119 if (handle->inited != 1) /* check handle initialization */
1120 {
1121 return 3; /* return error */
1122 }
1123
1124 command = SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_SET; /* write low alert limit set command */
1125 data[0] = (set >> 8) & 0xFF; /* set data 0 */
1126 data[1] = (set >> 0) & 0xFF; /* set data 1 */
1127 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1128 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1129 if (res != 0) /* check result */
1130 {
1131 handle->debug_print("sht30: write failed.\n"); /* write failed */
1132
1133 return 1; /* return error */
1134 }
1135 handle->delay_ms(10); /* delay 10ms */
1136 command = SHT30_COMMAND_WRITE_LOW_ALERT_LIMIT_CLEAR; /* write low alert limit clear command */
1137 data[0] = (clear >> 8) & 0xFF; /* set data 0 */
1138 data[1] = (clear >> 0) & 0xFF; /* set data 1 */
1139 data[2] = a_sht30_crc((uint8_t *)data, 2); /* set data crc */
1140 res = a_sht30_write_bytes(handle, command, (uint8_t *)data, 3); /* write data */
1141 if (res != 0) /* check result */
1142 {
1143 handle->debug_print("sht30: write failed.\n"); /* write failed */
1144
1145 return 1; /* return error */
1146 }
1147
1148 return 0; /* success return 0 */
1149}
1150
1163uint8_t sht30_get_low_alert_limit(sht30_handle_t *handle, uint16_t *set, uint16_t *clear)
1164{
1165 uint8_t res;
1166 uint16_t command;
1167 uint8_t data[3];
1168
1169 if (handle == NULL) /* check handle */
1170 {
1171 return 2; /* return error */
1172 }
1173 if (handle->inited != 1) /* check handle initialization */
1174 {
1175 return 3; /* return error */
1176 }
1177
1178 command = SHT30_COMMAND_READ_LOW_ALERT_LIMIT_SET; /* read low alert limit set command */
1179 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1180 if (res != 0) /* check result */
1181 {
1182 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1183
1184 return 1; /* return error */
1185 }
1186 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1187 {
1188 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1189
1190 return 1; /* return error */
1191 }
1192 *set = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1193
1194 command = SHT30_COMMAND_READ_LOW_ALERT_LIMIT_CLEAR; /* read low alert limit clear command */
1195 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read data */
1196 if (res != 0) /* check result */
1197 {
1198 handle->debug_print("sht30: read data failed.\n"); /* read data failed */
1199
1200 return 1; /* return error */
1201 }
1202 if (a_sht30_crc((uint8_t *)data, 2) != data[2]) /* check crc */
1203 {
1204 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1205
1206 return 1; /* return error */
1207 }
1208 *clear = ((uint16_t)data[0] << 8) | data[1]; /* set data */
1209
1210 return 0; /* success return 0 */
1211}
1212
1225uint8_t sht30_alert_limit_convert_to_register(sht30_handle_t *handle, float temperature, float humidity, uint16_t *reg)
1226{
1227 uint16_t r_t;
1228 uint16_t r_h;
1229
1230 if (handle == NULL) /* check handle */
1231 {
1232 return 2; /* return error */
1233 }
1234 if (handle->inited != 1) /* check handle initialization */
1235 {
1236 return 3; /* return error */
1237 }
1238
1239 r_t = (uint16_t)(((temperature + 45.0f) / 175.0f) * 65535.0f); /* convert to raw */
1240 r_t = r_t >> 7; /* convert temperature */
1241 r_h = (uint16_t)((humidity / 100.0f) * 65535.0f); /* convert to raw */
1242 r_h = r_h >> 9; /* convert humidity */
1243 *reg = (r_h << 9) | r_t; /* set register */
1244
1245 return 0; /* success return 0 */
1246}
1247
1259{
1260 uint8_t res;
1261 uint8_t data[3];
1262 uint16_t status;
1263 uint16_t command;
1264
1265 if (handle == NULL) /* check handle */
1266 {
1267 return 2; /* return error */
1268 }
1269 if (handle->inited != 1) /* check handle initialization */
1270 {
1271 return 3; /* return error */
1272 }
1273
1274 memset(data, 0, sizeof(uint8_t) * 3); /* clear the buffer */
1275 command = SHT30_COMMAND_READ_STATUS; /* set command */
1276 res = a_sht30_read(handle, command, (uint8_t *)data, 3); /* read status */
1277 if (res != 0) /* check result */
1278 {
1279 handle->debug_print("sht30: read status failed.\n"); /* read status failed */
1280
1281 return 1; /* return error */
1282 }
1283 if (a_sht30_crc((uint8_t *)data, 2) == data[2]) /* check crc */
1284 {
1285 status = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* get status */
1286 command = SHT30_COMMAND_CLEAR_STATUS; /* set command */
1287 res = a_sht30_write(handle, command); /* write command */
1288 if (res != 0) /* check result */
1289 {
1290 handle->debug_print("sht30: write command failed.\n"); /* write command failed */
1291
1292 return 1; /* return error */
1293 }
1294
1295 if ((status & SHT30_STATUS_ALERT_PENDING_STATUS) != 0) /* check alert pending status */
1296 {
1297 if (handle->receive_callback != NULL) /* check the receive callback */
1298 {
1299 handle->receive_callback(SHT30_STATUS_ALERT_PENDING_STATUS); /* run the callback */
1300 }
1301 }
1302 if ((status & SHT30_STATUS_HUMIDITY_ALERT) != 0) /* check humidity alert */
1303 {
1304 if (handle->receive_callback != NULL) /* check the receive callback */
1305 {
1306 handle->receive_callback(SHT30_STATUS_HUMIDITY_ALERT); /* run the callback */
1307 }
1308 }
1309 if ((status & SHT30_STATUS_TEMPERATURE_ALERT) != 0) /* check temperature alert */
1310 {
1311 if (handle->receive_callback != NULL) /* check the receive callback */
1312 {
1313 handle->receive_callback(SHT30_STATUS_TEMPERATURE_ALERT); /* run the callback */
1314 }
1315 }
1316
1317 return 0; /* success return 0 */
1318 }
1319 else
1320 {
1321 handle->debug_print("sht30: crc check failed.\n"); /* crc check failed */
1322
1323 return 1; /* return error */
1324 }
1325}
1326
1338uint8_t sht30_set_reg(sht30_handle_t *handle, uint16_t command)
1339{
1340 if (handle == NULL) /* check handle */
1341 {
1342 return 2; /* return error */
1343 }
1344 if (handle->inited != 1) /* check handle initialization */
1345 {
1346 return 3; /* return error */
1347 }
1348
1349 return a_sht30_write(handle, command); /* write command */
1350}
1351
1365uint8_t sht30_get_reg(sht30_handle_t *handle, uint16_t command, uint8_t *buf, uint16_t len)
1366{
1367 if (handle == NULL) /* check handle */
1368 {
1369 return 2; /* return error */
1370 }
1371 if (handle->inited != 1) /* check handle initialization */
1372 {
1373 return 3; /* return error */
1374 }
1375
1376 return a_sht30_read(handle, command, buf, len); /* read data */
1377}
1378
1388{
1389 if (info == NULL) /* check handle */
1390 {
1391 return 2; /* return error */
1392 }
1393
1394 memset(info, 0, sizeof(sht30_info_t)); /* initialize sht30 info structure */
1395 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1396 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1397 strncpy(info->interface, "IIC", 8); /* copy interface name */
1398 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1399 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1400 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1401 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1402 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1403 info->driver_version = DRIVER_VERSION; /* set driver version */
1404
1405 return 0; /* success return 0 */
1406}
#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]