LibDriver SHT2X
Loading...
Searching...
No Matches
driver_sht2x.c
Go to the documentation of this file.
1
36
37#include "driver_sht2x.h"
38
42#define CHIP_NAME "Sensirion SHT2X"
43#define MANUFACTURER_NAME "Sensirion"
44#define SUPPLY_VOLTAGE_MIN 2.1f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.33f
47#define TEMPERATURE_MIN -50.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER 0xE3
55#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_HOLD_MASTER 0xE5
56#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER 0xF3
57#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_NO_HOLD_MASTER 0xF5
58#define SHT2X_COMMAND_WRITE_REG 0xE6
59#define SHT2X_COMMAND_READ_REG 0xE7
60#define SHT2X_COMMAND_SOFT_RESET 0xFE
61#define SHT2X_COMMAND_GET_SN1 0xFA0FU
62#define SHT2X_COMMAND_GET_SN2 0xFCC9U
63
67#define SHT2X_ADDRESS 0x80
68
79static uint8_t a_sht2x_write_command(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
80{
81 if (handle->iic_write_cmd(SHT2X_ADDRESS, data, len) != 0) /* iic write */
82 {
83 return 1; /* return error */
84 }
85
86 return 0; /* success return 0 */
87}
88
99static uint8_t a_sht2x_read_command(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
100{
101 if (handle->iic_read_cmd(SHT2X_ADDRESS, data, len) != 0) /* iic read */
102 {
103 return 1; /* return error */
104 }
105
106 return 0; /* success return 0 */
107}
108
120static uint8_t a_sht2x_write(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
121{
122 if (handle->iic_write(SHT2X_ADDRESS, reg, data, len) != 0) /* iic write */
123 {
124 return 1; /* return error */
125 }
126
127 return 0; /* success return 0 */
128}
129
141static uint8_t a_sht2x_read(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
142{
143 if (handle->iic_read(SHT2X_ADDRESS, reg, data, len) != 0) /* iic read */
144 {
145 return 1; /* return error */
146 }
147
148 return 0; /* success return 0 */
149}
150
162static uint8_t a_sht2x_read_hold(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
163{
164 if (handle->iic_read_with_wait(SHT2X_ADDRESS, reg, data, len) != 0) /* iic read */
165 {
166 return 1; /* return error */
167 }
168
169 return 0; /* success return 0 */
170}
171
183static uint8_t a_sht2x_read16(sht2x_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
184{
185 if (handle->iic_read_address16(SHT2X_ADDRESS, reg, data, len) != 0) /* iic read */
186 {
187 return 1; /* return error */
188 }
189
190 return 0; /* success return 0 */
191}
192
200static uint8_t a_sht2x_crc(uint8_t *data, uint16_t len)
201{
202 const uint16_t POLYNOMIAL = 0x131;
203 uint8_t crc = 0x00;
204 uint16_t i;
205 uint16_t j;
206
207 for (i = 0; i < len; i++) /* loop all */
208 {
209 crc ^= (data[i]); /* calc crc */
210 for (j= 8; j > 0; j--) /* loop */
211 {
212 if ((crc & 0x80) != 0) /* 1 */
213 {
214 crc = (crc << 1) ^ POLYNOMIAL; /* calc crc */
215 }
216 else /* 0 */
217 {
218 crc = (crc << 1); /* calc crc */
219 }
220 }
221 }
222
223 return crc; /* return crc */
224}
225
237{
238 uint8_t res;
239 uint8_t command;
240
241 if (handle == NULL) /* check handle */
242 {
243 return 2; /* return error */
244 }
245 if (handle->debug_print == NULL) /* check debug_print */
246 {
247 return 3; /* return error */
248 }
249 if (handle->iic_init == NULL) /* check iic_init */
250 {
251 handle->debug_print("sht2x: iic_init is null.\n"); /* iic_init is null */
252
253 return 3; /* return error */
254 }
255 if (handle->iic_deinit == NULL) /* check iic_deinit */
256 {
257 handle->debug_print("sht2x: iic_deinit is null.\n"); /* iic_deinit is null */
258
259 return 3; /* return error */
260 }
261 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
262 {
263 handle->debug_print("sht2x: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
264
265 return 3; /* return error */
266 }
267 if (handle->iic_read == NULL) /* check iic_read */
268 {
269 handle->debug_print("sht2x: iic_read is null.\n"); /* iic_read is null */
270
271 return 3; /* return error */
272 }
273 if (handle->iic_read_address16 == NULL) /* check iic_read_address16 */
274 {
275 handle->debug_print("sht2x: iic_read_address16 is null.\n"); /* iic_read_address16 is null */
276
277 return 3; /* return error */
278 }
279 if (handle->iic_read_with_wait == NULL) /* check iic_read_with_wait */
280 {
281 handle->debug_print("sht2x: iic_read_with_wait is null.\n"); /* iic_read_with_wait is null */
282
283 return 3; /* return error */
284 }
285 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
286 {
287 handle->debug_print("sht2x: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
288
289 return 3; /* return error */
290 }
291 if (handle->iic_write == NULL) /* check iic_write */
292 {
293 handle->debug_print("sht2x: iic_write is null.\n"); /* iic_write is null */
294
295 return 3; /* return error */
296 }
297 if (handle->delay_ms == NULL) /* check delay_ms */
298 {
299 handle->debug_print("sht2x: delay_ms is null.\n"); /* delay_ms is null */
300
301 return 3; /* return error */
302 }
303
304 if (handle->iic_init() != 0) /* iic init */
305 {
306 handle->debug_print("sht2x: iic init failed.\n"); /* iic init failed */
307
308 return 1; /* return error */
309 }
310
311 command = SHT2X_COMMAND_SOFT_RESET; /* set command */
312 res = a_sht2x_write_command(handle, &command, 1); /* write command */
313 if (res != 0) /* check result */
314 {
315 handle->debug_print("sht2x: write command failed.\n"); /* write command failed */
316 (void)handle->iic_deinit(); /* close iic */
317
318 return 1; /* return error */
319 }
320 handle->delay_ms(15); /* delay 15ms */
321 handle->inited = 1; /* flag finish initialization */
322
323 return 0; /* success return 0 */
324}
325
337{
338 uint8_t res;
339 uint8_t command;
340
341 if (handle == NULL) /* check handle */
342 {
343 return 2; /* return error */
344 }
345 if (handle->inited != 1) /* check handle initialization */
346 {
347 return 3; /* return error */
348 }
349
350 command = SHT2X_COMMAND_SOFT_RESET; /* set command */
351 res = a_sht2x_write_command(handle, &command, 1); /* write command */
352 if (res != 0) /* check result */
353 {
354 handle->debug_print("sht2x: write command failed.\n"); /* write command failed */
355
356 return 1; /* return error */
357 }
358 handle->delay_ms(15); /* delay 15ms */
359 if (handle->iic_deinit() != 0) /* iic deinit */
360 {
361 handle->debug_print("sht2x: iic deinit failed.\n"); /* iic deinit failed */
362
363 return 1; /* return error */
364 }
365 handle->inited = 0; /* flag close */
366
367 return 0; /* success return 0 */
368}
369
381{
382 uint8_t res;
383 uint8_t command;
384
385 if (handle == NULL) /* check handle */
386 {
387 return 2; /* return error */
388 }
389 if (handle->inited != 1) /* check handle initialization */
390 {
391 return 3; /* return error */
392 }
393
394 command = SHT2X_COMMAND_SOFT_RESET; /* set command */
395 res = a_sht2x_write_command(handle, &command, 1); /* write command */
396 if (res != 0) /* check result */
397 {
398 handle->debug_print("sht2x: write command failed.\n"); /* write command failed */
399
400 return 1; /* return error */
401 }
402 handle->delay_ms(15); /* delay 15ms */
403
404 return 0; /* success return 0 */
405}
406
419{
420 uint8_t res;
421 uint8_t command;
422 uint8_t prev;
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 command = SHT2X_COMMAND_READ_REG; /* set command */
434 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
435 if (res != 0) /* check result */
436 {
437 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
438
439 return 1; /* return error */
440 }
441 prev &= ~(1 << 7); /* clear bit7 */
442 prev &= ~(1 << 0); /* clear bit0 */
443 prev |= (((uint8_t)(resolution) >> 1) & 0x01) << 7; /* set resolution */
444 prev |= (((uint8_t)(resolution) >> 0) & 0x01) << 0; /* set resolution */
445 command = SHT2X_COMMAND_WRITE_REG; /* set command */
446 res = a_sht2x_write(handle, command, &prev, 1); /* write reg */
447 if (res != 0) /* check result */
448 {
449 handle->debug_print("sht2x: write reg failed.\n"); /* write reg failed */
450
451 return 1; /* return error */
452 }
453 handle->resolution = resolution; /* set resolution */
454
455 return 0; /* success return 0 */
456}
457
470{
471 uint8_t res;
472 uint8_t command;
473 uint8_t prev;
474 uint8_t r;
475
476 if (handle == NULL) /* check handle */
477 {
478 return 2; /* return error */
479 }
480 if (handle->inited != 1) /* check handle initialization */
481 {
482 return 3; /* return error */
483 }
484
485 command = SHT2X_COMMAND_READ_REG; /* set command */
486 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
487 if (res != 0) /* check result */
488 {
489 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
490
491 return 1; /* return error */
492 }
493 r = (((prev >> 7) & 0x01) << 1) | (((prev >> 0) & 0x01) << 0); /* get data*/
494 *resolution = (sht2x_resolution_t)(r);
495
496 return 0; /* success return 0 */
497}
498
511{
512 uint8_t res;
513 uint8_t command;
514 uint8_t prev;
515
516 if (handle == NULL) /* check handle */
517 {
518 return 2; /* return error */
519 }
520 if (handle->inited != 1) /* check handle initialization */
521 {
522 return 3; /* return error */
523 }
524
525 command = SHT2X_COMMAND_READ_REG; /* set command */
526 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
527 if (res != 0) /* check result */
528 {
529 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
530
531 return 1; /* return error */
532 }
533 prev &= ~(1 << 2); /* clear settings */
534 prev |= enable << 2; /* set bool */
535 command = SHT2X_COMMAND_WRITE_REG; /* set command */
536 res = a_sht2x_write(handle, command, &prev, 1); /* write reg */
537 if (res != 0) /* check result */
538 {
539 handle->debug_print("sht2x: write reg failed.\n"); /* write reg failed */
540
541 return 1; /* return error */
542 }
543
544 return 0; /* success return 0 */
545}
546
559{
560 uint8_t res;
561 uint8_t command;
562 uint8_t prev;
563
564 if (handle == NULL) /* check handle */
565 {
566 return 2; /* return error */
567 }
568 if (handle->inited != 1) /* check handle initialization */
569 {
570 return 3; /* return error */
571 }
572
573 command = SHT2X_COMMAND_READ_REG; /* set command */
574 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
575 if (res != 0) /* check result */
576 {
577 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
578
579 return 1; /* return error */
580 }
581 *enable = (sht2x_bool_t)((prev >> 2) & 0x01); /* get bool */
582
583 return 0; /* success return 0 */
584}
585
598{
599 uint8_t res;
600 uint8_t command;
601 uint8_t prev;
602
603 if (handle == NULL) /* check handle */
604 {
605 return 2; /* return error */
606 }
607 if (handle->inited != 1) /* check handle initialization */
608 {
609 return 3; /* return error */
610 }
611
612 command = SHT2X_COMMAND_READ_REG; /* set command */
613 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
614 if (res != 0) /* check result */
615 {
616 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
617
618 return 1; /* return error */
619 }
620 prev &= ~(1 << 1); /* clear settings */
621 prev |= enable << 1; /* set bool */
622 command = SHT2X_COMMAND_WRITE_REG; /* set command */
623 res = a_sht2x_write(handle, command, &prev, 1); /* write reg */
624 if (res != 0) /* check result */
625 {
626 handle->debug_print("sht2x: write reg failed.\n"); /* write reg failed */
627
628 return 1; /* return error */
629 }
630
631 return 0; /* success return 0 */
632}
633
646{
647 uint8_t res;
648 uint8_t command;
649 uint8_t prev;
650
651 if (handle == NULL) /* check handle */
652 {
653 return 2; /* return error */
654 }
655 if (handle->inited != 1) /* check handle initialization */
656 {
657 return 3; /* return error */
658 }
659
660 command = SHT2X_COMMAND_READ_REG; /* set command */
661 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
662 if (res != 0) /* check result */
663 {
664 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
665
666 return 1; /* return error */
667 }
668 *enable = (sht2x_bool_t)((prev >> 1) & 0x01); /* set bool */
669
670 return 0; /* success return 0 */
671}
672
685{
686 uint8_t res;
687 uint8_t command;
688 uint8_t prev;
689
690 if (handle == NULL) /* check handle */
691 {
692 return 2; /* return error */
693 }
694 if (handle->inited != 1) /* check handle initialization */
695 {
696 return 3; /* return error */
697 }
698
699 command = SHT2X_COMMAND_READ_REG; /* set command */
700 res = a_sht2x_read(handle, command, &prev, 1); /* read reg */
701 if (res != 0) /* check result */
702 {
703 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
704
705 return 1; /* return error */
706 }
707 *status = (sht2x_status_t)((prev >> 6) & 0x01); /* set status */
708
709 return 0; /* success return 0 */
710}
711
723{
724 if (handle == NULL) /* check handle */
725 {
726 return 2; /* return error */
727 }
728 if (handle->inited != 1) /* check handle initialization */
729 {
730 return 3; /* return error */
731 }
732
733 handle->mode = (uint8_t)mode; /* set mode */
734
735 return 0; /* success return 0 */
736}
737
749{
750 if (handle == NULL) /* check handle */
751 {
752 return 2; /* return error */
753 }
754 if (handle->inited != 1) /* check handle initialization */
755 {
756 return 3; /* return error */
757 }
758
759 *mode = (sht2x_mode_t)(handle->mode); /* get mode */
760
761 return 0; /* success return 0 */
762}
763
779uint8_t sht2x_read(sht2x_handle_t *handle, uint16_t *temperature_raw, float *temperature_s,
780 uint16_t *humidity_raw, float *humidity_s)
781{
782 uint8_t res;
783 uint8_t command;
784 uint8_t data[3];
785
786 if (handle == NULL) /* check handle */
787 {
788 return 2; /* return error */
789 }
790 if (handle->inited != 1) /* check handle initialization */
791 {
792 return 3; /* return error */
793 }
794
795 if(handle->mode == (uint8_t)(SHT2X_MODE_HOLD_MASTER)) /* hold master */
796 {
797 command = SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER; /* set command */
798 res = a_sht2x_read_hold(handle, command, data, 3); /* read hold */
799 if (res != 0) /* check result */
800 {
801 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
802
803 return 1; /* return error */
804 }
805 if (a_sht2x_crc(data, 2) != data[2]) /* check crc */
806 {
807 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
808
809 return 4; /* return error */
810 }
811 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* set raw temperature */
812 (*temperature_raw) &= ~0x0003; /* clear status bits */
813 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f; /* convert raw temperature */
814
815 command = SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_HOLD_MASTER; /* set command */
816 res = a_sht2x_read_hold(handle, command, data, 3); /* read hold */
817 if (res != 0) /* check result */
818 {
819 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
820
821 return 1; /* return error */
822 }
823 if (a_sht2x_crc(data, 2) != data[2]) /* check crc */
824 {
825 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
826
827 return 4; /* return error */
828 }
829 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* set raw humidity */
830 (*humidity_raw) &= ~0x0003; /* clear status bits */
831 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f; /* convert raw humidity */
832 }
833 else /* no hold master */
834 {
835 command = SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER; /* set command */
836 res = a_sht2x_write_command(handle, &command, 1); /* write command */
837 if (res != 0) /* check result */
838 {
839 handle->debug_print("sht2x: write reg failed.\n"); /* write reg failed */
840
841 return 1; /* return error */
842 }
843 if (handle->resolution == SHT2X_RESOLUTION_RH_12BIT_T_14BIT) /* 14bit */
844 {
845 handle->delay_ms(86); /* delay 86ms */
846 }
847 else if (handle->resolution == SHT2X_RESOLUTION_RH_8BIT_T_12BIT) /* 12bit */
848 {
849 handle->delay_ms(23); /* delay 23ms */
850 }
851 else if (handle->resolution == SHT2X_RESOLUTION_RH_10BIT_T_13BIT) /* 13bit */
852 {
853 handle->delay_ms(44); /* delay 44ms */
854 }
855 else /* 11bit */
856 {
857 handle->delay_ms(12); /* delay 12ms */
858 }
859 res = a_sht2x_read_command(handle, data, 3); /* read command */
860 if (res != 0) /* check result */
861 {
862 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
863
864 return 1; /* return error */
865 }
866 if (a_sht2x_crc(data, 2) != data[2]) /* check crc */
867 {
868 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
869
870 return 4; /* return error */
871 }
872 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* set raw temperature */
873 (*temperature_raw) &= ~0x0003; /* clear status bits */
874 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f; /* convert raw temperature */
875
876 command = SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_NO_HOLD_MASTER; /* set command */
877 res = a_sht2x_write_command(handle, &command, 1); /* write command */
878 if (res != 0) /* check result */
879 {
880 handle->debug_print("sht2x: write reg failed.\n"); /* write reg failed */
881
882 return 1; /* return error */
883 }
884 if (handle->resolution == SHT2X_RESOLUTION_RH_12BIT_T_14BIT) /* 12bit */
885 {
886 handle->delay_ms(30); /* delay 30ms */
887 }
888 else if (handle->resolution == SHT2X_RESOLUTION_RH_8BIT_T_12BIT) /* 18bit */
889 {
890 handle->delay_ms(5); /* delay 5ms */
891 }
892 else if (handle->resolution == SHT2X_RESOLUTION_RH_10BIT_T_13BIT) /* 10bit */
893 {
894 handle->delay_ms(10); /* delay 10ms */
895 }
896 else /* 11bit */
897 {
898 handle->delay_ms(16); /* delay 16ms */
899 }
900 res = a_sht2x_read_command(handle, data, 3); /* read command */
901 if (res != 0) /* check result */
902 {
903 handle->debug_print("sht2x: read reg failed.\n"); /* read reg failed */
904
905 return 1; /* return error */
906 }
907 if (a_sht2x_crc(data, 2) != data[2]) /* check crc */
908 {
909 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
910
911 return 4; /* return error */
912 }
913 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]); /* set raw humidity */
914 (*humidity_raw) &= ~0x0003; /* clear status bits */
915 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f; /* convert raw humidity */
916 }
917
918 return 0; /* success return 0 */
919}
920
933uint8_t sht2x_get_serial_number(sht2x_handle_t *handle, uint8_t sn[8])
934{
935 uint8_t res;
936 uint16_t command;
937 uint8_t data[8];
938
939 if (handle == NULL) /* check handle */
940 {
941 return 2; /* return error */
942 }
943 if (handle->inited != 1) /* check handle initialization */
944 {
945 return 3; /* return error */
946 }
947
948 command = SHT2X_COMMAND_GET_SN1; /* get serial number command */
949 res = a_sht2x_read16(handle, command, (uint8_t *)data, 8); /* read data */
950 if (res != 0) /* check result */
951 {
952 handle->debug_print("sht2x: read data failed.\n"); /* read data failed */
953
954 return 1; /* return error */
955 }
956 if (a_sht2x_crc((uint8_t *)&data[0], 1) != data[1]) /* check crc */
957 {
958 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
959
960 return 4; /* return error */
961 }
962 if (a_sht2x_crc((uint8_t *)&data[2], 1) != data[3]) /* check crc */
963 {
964 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
965
966 return 4; /* return error */
967 }
968 if (a_sht2x_crc((uint8_t *)&data[4], 1) != data[5]) /* check crc */
969 {
970 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
971
972 return 4; /* return error */
973 }
974 if (a_sht2x_crc((uint8_t *)&data[6], 1) != data[7]) /* check crc */
975 {
976 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
977
978 return 4; /* return error */
979 }
980 sn[5] = data[0]; /* set sn5 */
981 sn[4] = data[2]; /* set sn4 */
982 sn[3] = data[4]; /* set sn3 */
983 sn[2] = data[6]; /* set sn2 */
984
985 command = SHT2X_COMMAND_GET_SN2; /* get serial number command */
986 res = a_sht2x_read16(handle, command, (uint8_t *)data, 6); /* read data */
987 if (res != 0) /* check result */
988 {
989 handle->debug_print("sht2x: read data failed.\n"); /* read data failed */
990
991 return 1; /* return error */
992 }
993 if (a_sht2x_crc((uint8_t *)data, 2) != data[2]) /* check crc */
994 {
995 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
996
997 return 4; /* return error */
998 }
999 if (a_sht2x_crc((uint8_t *)&data[3], 2) != data[5]) /* check crc */
1000 {
1001 handle->debug_print("sht2x: crc check failed.\n"); /* crc check failed */
1002
1003 return 4; /* return error */
1004 }
1005 sn[1] = data[0]; /* set sn1 */
1006 sn[0] = data[1]; /* set sn0 */
1007 sn[7] = data[3]; /* set sn7 */
1008 sn[6] = data[4]; /* set sn6 */
1009
1010 return 0; /* success return 0 */
1011}
1012
1026uint8_t sht2x_set_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
1027{
1028 if (handle == NULL) /* check handle */
1029 {
1030 return 2; /* return error */
1031 }
1032 if (handle->inited != 1) /* check handle initialization */
1033 {
1034 return 3; /* return error */
1035 }
1036
1037 return a_sht2x_write(handle, reg, data, len); /* write command */
1038}
1039
1053uint8_t sht2x_get_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
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 return a_sht2x_read(handle, reg, data, len); /* read data */
1065}
1066
1080uint8_t sht2x_get_reg16(sht2x_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
1081{
1082 if (handle == NULL) /* check handle */
1083 {
1084 return 2; /* return error */
1085 }
1086 if (handle->inited != 1) /* check handle initialization */
1087 {
1088 return 3; /* return error */
1089 }
1090
1091 return a_sht2x_read16(handle, reg, data, len); /* read data */
1092}
1093
1106uint8_t sht2x_set_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
1107{
1108 if (handle == NULL) /* check handle */
1109 {
1110 return 2; /* return error */
1111 }
1112 if (handle->inited != 1) /* check handle initialization */
1113 {
1114 return 3; /* return error */
1115 }
1116
1117 return a_sht2x_write_command(handle, data, len); /* write command */
1118}
1119
1132uint8_t sht2x_get_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
1133{
1134 if (handle == NULL) /* check handle */
1135 {
1136 return 2; /* return error */
1137 }
1138 if (handle->inited != 1) /* check handle initialization */
1139 {
1140 return 3; /* return error */
1141 }
1142
1143 return a_sht2x_read_command(handle, data, len); /* read data */
1144}
1145
1155{
1156 if (info == NULL) /* check handle */
1157 {
1158 return 2; /* return error */
1159 }
1160
1161 memset(info, 0, sizeof(sht2x_info_t)); /* initialize sht2x info structure */
1162 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1163 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1164 strncpy(info->interface, "IIC", 8); /* copy interface name */
1165 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1166 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1167 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1168 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1169 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1170 info->driver_version = DRIVER_VERSION; /* set driver version */
1171
1172 return 0; /* success return 0 */
1173}
#define SHT2X_COMMAND_GET_SN2
#define SHT2X_COMMAND_SOFT_RESET
#define MAX_CURRENT
#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define SHT2X_ADDRESS
chip address definition
#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER
#define SHT2X_COMMAND_GET_SN1
#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_NO_HOLD_MASTER
#define SHT2X_COMMAND_WRITE_REG
#define CHIP_NAME
chip information definition
#define SHT2X_COMMAND_READ_REG
#define DRIVER_VERSION
#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_HOLD_MASTER
driver sht2x header file
struct sht2x_info_s sht2x_info_t
sht2x information structure definition
uint8_t sht2x_set_heater(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable heater
uint8_t sht2x_read(sht2x_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data
uint8_t sht2x_get_status(sht2x_handle_t *handle, sht2x_status_t *status)
get status
sht2x_mode_t
sht2x mode enumeration definition
uint8_t sht2x_init(sht2x_handle_t *handle)
initialize the chip
uint8_t sht2x_soft_reset(sht2x_handle_t *handle)
soft reset the chip
uint8_t sht2x_get_mode(sht2x_handle_t *handle, sht2x_mode_t *mode)
get chip mode
uint8_t sht2x_get_disable_otp_reload(sht2x_handle_t *handle, sht2x_bool_t *enable)
get disable otp reload status
uint8_t sht2x_info(sht2x_info_t *info)
get chip's information
uint8_t sht2x_get_resolution(sht2x_handle_t *handle, sht2x_resolution_t *resolution)
get resolution
uint8_t sht2x_set_resolution(sht2x_handle_t *handle, sht2x_resolution_t resolution)
set resolution
struct sht2x_handle_s sht2x_handle_t
sht2x handle structure definition
uint8_t sht2x_set_disable_otp_reload(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable disable otp reload
uint8_t sht2x_get_serial_number(sht2x_handle_t *handle, uint8_t sn[8])
get serial number
sht2x_status_t
sht2x status enumeration definition
uint8_t sht2x_deinit(sht2x_handle_t *handle)
close the chip
uint8_t sht2x_set_mode(sht2x_handle_t *handle, sht2x_mode_t mode)
set chip mode
sht2x_resolution_t
sht2x resolution enumeration definition
uint8_t sht2x_get_heater(sht2x_handle_t *handle, sht2x_bool_t *enable)
get heater status
sht2x_bool_t
sht2x bool enumeration definition
@ SHT2X_MODE_HOLD_MASTER
@ SHT2X_RESOLUTION_RH_12BIT_T_14BIT
@ SHT2X_RESOLUTION_RH_10BIT_T_13BIT
@ SHT2X_RESOLUTION_RH_8BIT_T_12BIT
uint8_t sht2x_get_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
get command
uint8_t sht2x_get_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
get the chip register
uint8_t sht2x_get_reg16(sht2x_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
get the chip register16
uint8_t sht2x_set_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
set command
uint8_t sht2x_set_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_with_wait)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
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)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
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]