LibDriver STTS22H
Loading...
Searching...
No Matches
driver_stts22h.c
Go to the documentation of this file.
1
36
37#include "driver_stts22h.h"
38
42#define CHIP_NAME "STMicroelectronic STTS22H"
43#define MANUFACTURER_NAME "STMicroelectronic"
44#define SUPPLY_VOLTAGE_MIN 1.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.18f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define STTS22H_REG_WHOAMI 0x01
55#define STTS22H_REG_TEMP_H_LIMIT 0x02
56#define STTS22H_REG_TEMP_L_LIMIT 0x03
57#define STTS22H_REG_CTRL 0x04
58#define STTS22H_REG_STATUS 0x05
59#define STTS22H_REG_TEMP_L_OUT 0x06
60#define STTS22H_REG_TEMP_H_OUT 0x07
61
73static uint8_t a_stts22h_read(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
74{
75 if (handle->iic_addr_auto_inc != 0) /* address auto inc */
76 {
77 if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* read data */
78 {
79 return 1; /* return error */
80 }
81 }
82 else
83 {
84 uint16_t i;
85
86 for (i = 0; i < len; i++) /* read all data */
87 {
88 if (handle->iic_read(handle->iic_addr, (uint8_t)(reg + i), &buf[i], 1) != 0) /* read data */
89 {
90 return 1; /* return error */
91 }
92 }
93 }
94
95 return 0; /* success return 0 */
96}
97
109static uint8_t a_stts22h_write(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
110{
111 if (handle->iic_addr_auto_inc != 0) /* address auto inc */
112 {
113 if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* write data */
114 {
115 return 1; /* return error */
116 }
117 }
118 else
119 {
120 uint16_t i;
121
122 for (i = 0; i < len; i++) /* write all data */
123 {
124 if (handle->iic_write(handle->iic_addr, (uint8_t)(reg + i), &buf[i], 1) != 0) /* write data */
125 {
126 return 1; /* return error */
127 }
128 }
129 }
130
131 return 0; /* success return 0 */
132}
133
144{
145 if (handle == NULL) /* check handle */
146 {
147 return 2; /* return error */
148 }
149
150 handle->iic_addr = (uint8_t)addr_pin; /* set pin */
151
152 return 0; /* success return 0 */
153}
154
165{
166 if (handle == NULL) /* check handle */
167 {
168 return 2; /* return error */
169 }
170
171 *addr_pin = (stts22h_address_t)(handle->iic_addr); /* get pin */
172
173 return 0; /* success return 0 */
174}
175
188{
189 uint8_t res;
190 uint8_t prev;
191
192 if (handle == NULL) /* check handle */
193 {
194 return 2; /* return error */
195 }
196 if (handle->inited != 1) /* check handle initialization */
197 {
198 return 3; /* return error */
199 }
200
201 prev = raw; /* set raw data */
202 res = a_stts22h_write(handle, STTS22H_REG_TEMP_H_LIMIT, &prev, 1); /* write limit */
203 if (res != 0) /* check the result */
204 {
205 handle->debug_print("stts22h: write temperature high limit failed.\n"); /* write temperature high limit failed */
206
207 return 1; /* return error */
208 }
209
210 return 0; /* success return 0 */
211}
212
225{
226 uint8_t res;
227 uint8_t prev;
228
229 if (handle == NULL) /* check handle */
230 {
231 return 2; /* return error */
232 }
233 if (handle->inited != 1) /* check handle initialization */
234 {
235 return 3; /* return error */
236 }
237
238 res = a_stts22h_read(handle, STTS22H_REG_TEMP_H_LIMIT, &prev, 1); /* read limit */
239 if (res != 0) /* check the result */
240 {
241 handle->debug_print("stts22h: read temperature high limit failed.\n"); /* read temperature high limit failed */
242
243 return 1; /* return error */
244 }
245 *raw = prev; /* set raw data */
246
247 return 0; /* success return 0 */
248}
249
262{
263 uint8_t res;
264 uint8_t prev;
265
266 if (handle == NULL) /* check handle */
267 {
268 return 2; /* return error */
269 }
270 if (handle->inited != 1) /* check handle initialization */
271 {
272 return 3; /* return error */
273 }
274
275 prev = raw; /* set raw data */
276 res = a_stts22h_write(handle, STTS22H_REG_TEMP_L_LIMIT, &prev, 1); /* write limit */
277 if (res != 0) /* check the result */
278 {
279 handle->debug_print("stts22h: write temperature low limit failed.\n"); /* write temperature low limit failed */
280
281 return 1; /* return error */
282 }
283
284 return 0; /* success return 0 */
285}
286
299{
300 uint8_t res;
301 uint8_t prev;
302
303 if (handle == NULL) /* check handle */
304 {
305 return 2; /* return error */
306 }
307 if (handle->inited != 1) /* check handle initialization */
308 {
309 return 3; /* return error */
310 }
311
312 res = a_stts22h_read(handle, STTS22H_REG_TEMP_L_LIMIT, &prev, 1); /* read limit */
313 if (res != 0) /* check the result */
314 {
315 handle->debug_print("stts22h: read temperature low limit failed.\n"); /* read temperature low limit failed */
316
317 return 1; /* return error */
318 }
319 *raw = prev; /* set raw data */
320
321 return 0; /* success return 0 */
322}
323
336{
337 uint8_t res;
338 uint8_t prev;
339
340 if (handle == NULL) /* check handle */
341 {
342 return 2; /* return error */
343 }
344 if (handle->inited != 1) /* check handle initialization */
345 {
346 return 3; /* return error */
347 }
348
349 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
350 if (res != 0) /* check the result */
351 {
352 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
353
354 return 1; /* return error */
355 }
356 if (rate == STTS22H_OUTPUT_DATA_RATE_1HZ) /* 1hz */
357 {
358 prev |= 1 << 7; /* set low power bit */
359 }
360 else
361 {
362 prev &= ~(1 << 7); /* clear low power bit */
363 prev &= ~(3 << 4); /* clear settings */
364 prev |= rate << 4; /* set rate */
365 }
366 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
367 if (res != 0) /* check the result */
368 {
369 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
370
371 return 1; /* return error */
372 }
373
374 return 0; /* success return 0 */
375}
376
389{
390 uint8_t res;
391 uint8_t prev;
392
393 if (handle == NULL) /* check handle */
394 {
395 return 2; /* return error */
396 }
397 if (handle->inited != 1) /* check handle initialization */
398 {
399 return 3; /* return error */
400 }
401
402 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
403 if (res != 0) /* check the result */
404 {
405 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
406
407 return 1; /* return error */
408 }
409
410 if (((prev >> 7) & 0x01) != 0) /* low power is clear */
411 {
412 *rate = STTS22H_OUTPUT_DATA_RATE_1HZ; /* set 1hz */
413 }
414 else
415 {
416 *rate = (stts22h_output_data_rate_t)((prev >> 4) & 0x03); /* set output data rate */
417 }
418
419 return 0; /* success return 0 */
420}
421
434{
435 uint8_t res;
436 uint8_t prev;
437
438 if (handle == NULL) /* check handle */
439 {
440 return 2; /* return error */
441 }
442 if (handle->inited != 1) /* check handle initialization */
443 {
444 return 3; /* return error */
445 }
446
447 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
448 if (res != 0) /* check the result */
449 {
450 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
451
452 return 1; /* return error */
453 }
454 prev &= ~(1 << 6); /* clear settings */
455 prev |= enable << 6; /* set bool */
456 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
457 if (res != 0) /* check the result */
458 {
459 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
460
461 return 1; /* return error */
462 }
463
464 return 0; /* success return 0 */
465}
466
479{
480 uint8_t res;
481 uint8_t prev;
482
483 if (handle == NULL) /* check handle */
484 {
485 return 2; /* return error */
486 }
487 if (handle->inited != 1) /* check handle initialization */
488 {
489 return 3; /* return error */
490 }
491
492 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
493 if (res != 0) /* check the result */
494 {
495 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
496
497 return 1; /* return error */
498 }
499 *enable = (stts22h_bool_t)((prev >> 6) & 0x01); /* set bool */
500
501 return 0; /* success return 0 */
502}
503
516{
517 uint8_t res;
518 uint8_t prev;
519
520 if (handle == NULL) /* check handle */
521 {
522 return 2; /* return error */
523 }
524 if (handle->inited != 1) /* check handle initialization */
525 {
526 return 3; /* return error */
527 }
528
529 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
530 if (res != 0) /* check the result */
531 {
532 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
533
534 return 1; /* return error */
535 }
536 prev &= ~(1 << 3); /* clear settings */
537 prev |= enable << 3; /* set bool */
538 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
539 if (res != 0) /* check the result */
540 {
541 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
542
543 return 1; /* return error */
544 }
545 handle->iic_addr_auto_inc = (uint8_t)enable; /* set bool */
546
547 return 0; /* success return 0 */
548}
549
562{
563 uint8_t res;
564 uint8_t prev;
565
566 if (handle == NULL) /* check handle */
567 {
568 return 2; /* return error */
569 }
570 if (handle->inited != 1) /* check handle initialization */
571 {
572 return 3; /* return error */
573 }
574
575 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
576 if (res != 0) /* check the result */
577 {
578 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
579
580 return 1; /* return error */
581 }
582 *enable = (stts22h_bool_t)((prev >> 3) & 0x01); /* set bool */
583
584 return 0; /* success return 0 */
585}
586
599{
600 uint8_t res;
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 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
613 if (res != 0) /* check the result */
614 {
615 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
616
617 return 1; /* return error */
618 }
619 prev &= ~(1 << 1); /* clear settings */
620 prev |= enable << 1; /* set bool */
621 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
622 if (res != 0) /* check the result */
623 {
624 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
625
626 return 1; /* return error */
627 }
628
629 return 0; /* success return 0 */
630}
631
644{
645 uint8_t res;
646 uint8_t prev;
647
648 if (handle == NULL) /* check handle */
649 {
650 return 2; /* return error */
651 }
652 if (handle->inited != 1) /* check handle initialization */
653 {
654 return 3; /* return error */
655 }
656
657 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
658 if (res != 0) /* check the result */
659 {
660 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
661
662 return 1; /* return error */
663 }
664 *enable = (stts22h_bool_t)((prev >> 1) & 0x01); /* set bool */
665
666 return 0; /* success return 0 */
667}
668
680uint8_t stts22h_get_status(stts22h_handle_t *handle, uint8_t *status)
681{
682 uint8_t res;
683 uint8_t prev;
684
685 if (handle == NULL) /* check handle */
686 {
687 return 2; /* return error */
688 }
689 if (handle->inited != 1) /* check handle initialization */
690 {
691 return 3; /* return error */
692 }
693
694 res = a_stts22h_read(handle, STTS22H_REG_STATUS, &prev, 1); /* read status */
695 if (res != 0) /* check the result */
696 {
697 handle->debug_print("stts22h: read status failed.\n"); /* read status failed */
698
699 return 1; /* return error */
700 }
701 *status = prev; /* set status */
702
703 return 0; /* success return 0 */
704}
705
719uint8_t stts22h_single_read(stts22h_handle_t *handle, int16_t *raw, float *celsius_deg)
720{
721 uint8_t res;
722 uint8_t prev;
723 uint8_t buf[2];
724 uint16_t timeout;
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 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
736 if (res != 0) /* check the result */
737 {
738 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
739
740 return 1; /* return error */
741 }
742 prev &= ~(1 << 7); /* clear low power */
743 prev &= ~(1 << 2); /* clear free run */
744 prev |= (1 << 0); /* set one shot */
745 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
746 if (res != 0) /* check the result */
747 {
748 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
749
750 return 1; /* return error */
751 }
752
753 timeout = 500; /* set timeout 5000ms */
754 while (timeout != 0)
755 {
756 res = a_stts22h_read(handle, STTS22H_REG_STATUS, &prev, 1); /* read status */
757 if (res != 0) /* check the result */
758 {
759 handle->debug_print("stts22h: read status failed.\n"); /* read status failed */
760
761 return 1; /* return error */
762 }
763 if ((prev & 0x01) == 0) /* conversion is completed */
764 {
765 break; /* break */
766 }
767 handle->delay_ms(10); /* delay 10ms */
768 timeout--; /* timeout-- */
769 }
770 if (timeout == 0) /* check timeout */
771 {
772 handle->debug_print("stts22h: read timeout.\n"); /* read timeout */
773
774 return 4; /* return error */
775 }
776
777 res = a_stts22h_read(handle, STTS22H_REG_TEMP_L_OUT, buf, 2); /* read temperature */
778 if (res != 0) /* check the result */
779 {
780 handle->debug_print("stts22h: read failed.\n"); /* read failed */
781
782 return 1; /* return error */
783 }
784 *raw = (int16_t)((uint16_t)((uint16_t)buf[1] << 8) | buf[0]); /* set raw data */
785 *celsius_deg = (float)(*raw) / 100.0f; /* convert data */
786
787 return 0; /* success return 0 */
788}
789
801{
802 uint8_t res;
803 uint8_t prev;
804
805 if (handle == NULL) /* check handle */
806 {
807 return 2; /* return error */
808 }
809 if (handle->inited != 1) /* check handle initialization */
810 {
811 return 3; /* return error */
812 }
813
814 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
815 if (res != 0) /* check the result */
816 {
817 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
818
819 return 1; /* return error */
820 }
821 prev |= 1 << 2; /* enable */
822 prev &= ~(1 << 0); /* clear one shot */
823 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
824 if (res != 0) /* check the result */
825 {
826 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
827
828 return 1; /* return error */
829 }
830
831 return 0; /* success return 0 */
832}
833
845{
846 uint8_t res;
847 uint8_t prev;
848
849 if (handle == NULL) /* check handle */
850 {
851 return 2; /* return error */
852 }
853 if (handle->inited != 1) /* check handle initialization */
854 {
855 return 3; /* return error */
856 }
857
858 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
859 if (res != 0) /* check the result */
860 {
861 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
862
863 return 1; /* return error */
864 }
865 prev &= ~(1 << 2); /* disable */
866 prev &= ~(1 << 0); /* clear one shot */
867 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
868 if (res != 0) /* check the result */
869 {
870 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
871
872 return 1; /* return error */
873 }
874
875 return 0; /* success return 0 */
876}
877
891uint8_t stts22h_continuous_read(stts22h_handle_t *handle, int16_t *raw, float *celsius_deg)
892{
893 uint8_t res;
894 uint8_t buf[2];
895
896 if (handle == NULL) /* check handle */
897 {
898 return 2; /* return error */
899 }
900 if (handle->inited != 1) /* check handle initialization */
901 {
902 return 3; /* return error */
903 }
904
905 res = a_stts22h_read(handle, STTS22H_REG_TEMP_L_OUT, buf, 2); /* read temperature */
906 if (res != 0) /* check the result */
907 {
908 handle->debug_print("stts22h: read failed.\n"); /* read failed */
909
910 return 1; /* return error */
911 }
912 *raw = (int16_t)((uint16_t)((uint16_t)buf[1] << 8) | buf[0]); /* set raw data */
913 *celsius_deg = (float)(*raw) / 100.0f; /* convert data */
914
915 return 0; /* success return 0 */
916}
917
929{
930 uint8_t res;
931 uint8_t prev;
932
933 if (handle == NULL) /* check handle */
934 {
935 return 2; /* return error */
936 }
937 if (handle->inited != 1) /* check handle initialization */
938 {
939 return 3; /* return error */
940 }
941
942 res = a_stts22h_read(handle, STTS22H_REG_STATUS, &prev, 1); /* read status */
943 if (res != 0) /* check the result */
944 {
945 handle->debug_print("stts22h: read status failed.\n"); /* read status failed */
946
947 return 1; /* return error */
948 }
949 if ((prev & STTS22H_STATUS_UNDER_LOW_LIMIT) != 0) /* check flag */
950 {
951 if (handle->receive_callback != NULL) /* not null */
952 {
953 handle->receive_callback(STTS22H_STATUS_UNDER_LOW_LIMIT); /* run the callback */
954 }
955 }
956 if ((prev & STTS22H_STATUS_OVER_HIGH_LIMIT) != 0) /* check flag */
957 {
958 if (handle->receive_callback != NULL) /* not null */
959 {
960 handle->receive_callback(STTS22H_STATUS_OVER_HIGH_LIMIT); /* run the callback */
961 }
962 }
963
964 return 0; /* success return 0 */
965}
966
978uint8_t stts22h_temperature_convert_to_register(stts22h_handle_t *handle, float celsius_deg, uint8_t *reg)
979{
980 if (handle == NULL) /* check handle */
981 {
982 return 2; /* return error */
983 }
984 if (handle->inited != 1) /* check handle initialization */
985 {
986 return 3; /* return error */
987 }
988
989 *reg = (uint8_t)(celsius_deg / 0.64f) + 63; /* convert real data to register data */
990
991 return 0; /* success return 0 */
992}
993
1005uint8_t stts22h_temperature_convert_to_data(stts22h_handle_t *handle, uint8_t reg, float *celsius_deg)
1006{
1007 if (handle == NULL) /* check handle */
1008 {
1009 return 2; /* return error */
1010 }
1011 if (handle->inited != 1) /* check handle initialization */
1012 {
1013 return 3; /* return error */
1014 }
1015
1016 *celsius_deg = ((float)(reg) - 63.0f) * 0.64f; /* convert raw data to real data */
1017
1018 return 0; /* success return 0 */
1019}
1020
1033{
1034 uint8_t res;
1035 uint8_t prev;
1036 uint8_t id;
1037
1038 if (handle == NULL) /* check handle */
1039 {
1040 return 2; /* return error */
1041 }
1042 if (handle->debug_print == NULL) /* check debug_print */
1043 {
1044 return 3; /* return error */
1045 }
1046 if (handle->iic_init == NULL) /* check iic_init */
1047 {
1048 handle->debug_print("stts22h: iic_init is null.\n"); /* iic_init is null */
1049
1050 return 3; /* return error */
1051 }
1052 if (handle->iic_deinit == NULL) /* check iic_deinit */
1053 {
1054 handle->debug_print("stts22h: iic_deinit is null.\n"); /* iic_deinit is null */
1055
1056 return 3; /* return error */
1057 }
1058 if (handle->iic_read == NULL) /* check iic_read */
1059 {
1060 handle->debug_print("stts22h: iic_read is null.\n"); /* iic_read is null */
1061
1062 return 3; /* return error */
1063 }
1064 if (handle->iic_write == NULL) /* check iic_write */
1065 {
1066 handle->debug_print("stts22h: iic_write is null.\n"); /* iic_write is null */
1067
1068 return 3; /* return error */
1069 }
1070 if (handle->delay_ms == NULL) /* check delay_ms */
1071 {
1072 handle->debug_print("stts22h: delay_ms is null.\n"); /* delay_ms is null */
1073
1074 return 3; /* return error */
1075 }
1076 if (handle->receive_callback == NULL) /* check receive_callback */
1077 {
1078 handle->debug_print("stts22h: receive_callback is null.\n"); /* receive_callback is null */
1079
1080 return 3; /* return error */
1081 }
1082
1083 if (handle->iic_init() != 0) /* iic init */
1084 {
1085 handle->debug_print("stts22h: iic init failed.\n"); /* iic init failed */
1086
1087 return 1; /* return error */
1088 }
1089
1090 res = a_stts22h_read(handle, STTS22H_REG_WHOAMI, &id, 1); /* read who am i */
1091 if (res != 0) /* check the result */
1092 {
1093 handle->debug_print("stts22h: read who am i failed.\n"); /* read who am i failed */
1094 (void)handle->iic_deinit(); /* iic deinit */
1095
1096 return 4; /* return error */
1097 }
1098 if (id != 0xA0) /* check id */
1099 {
1100 handle->debug_print("stts22h: id is invalid.\n"); /* id is invalid */
1101 (void)handle->iic_deinit(); /* iic deinit */
1102
1103 return 4; /* return error */
1104 }
1105
1106 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
1107 if (res != 0) /* check the result */
1108 {
1109 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
1110 (void)handle->iic_deinit(); /* iic deinit */
1111
1112 return 1; /* return error */
1113 }
1114 prev &= ~(1 << 3); /* clear settings */
1115 prev |= 1 << 3; /* set bool */
1116 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
1117 if (res != 0) /* check the result */
1118 {
1119 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
1120 (void)handle->iic_deinit(); /* iic deinit */
1121
1122 return 1; /* return error */
1123 }
1124 handle->iic_addr_auto_inc = 1; /* set enable */
1125 handle->inited = 1; /* flag finish initialization */
1126
1127 return 0; /* success return 0 */
1128}
1129
1142{
1143 uint8_t res;
1144 uint8_t prev;
1145
1146 if (handle == NULL) /* check handle */
1147 {
1148 return 2; /* return error */
1149 }
1150 if (handle->inited != 1) /* check handle initialization */
1151 {
1152 return 3; /* return error */
1153 }
1154
1155 res = a_stts22h_read(handle, STTS22H_REG_CTRL, &prev, 1); /* read control */
1156 if (res != 0) /* check the result */
1157 {
1158 handle->debug_print("stts22h: read control failed.\n"); /* read control failed */
1159
1160 return 4; /* return error */
1161 }
1162 prev &= ~(1 << 7); /* clear settings */
1163 prev &= ~(1 << 2); /* clear settings */
1164 prev &= ~(1 << 0); /* clear settings */
1165 res = a_stts22h_write(handle, STTS22H_REG_CTRL, &prev, 1); /* write control */
1166 if (res != 0) /* check the result */
1167 {
1168 handle->debug_print("stts22h: write control failed.\n"); /* write control failed */
1169
1170 return 4; /* return error */
1171 }
1172
1173 if (handle->iic_deinit() != 0) /* iic deinit */
1174 {
1175 handle->debug_print("stts22h: iic deinit failed.\n"); /* return error */
1176
1177 return 1; /* iic deinit failed */
1178 }
1179 handle->inited = 0; /* flag close */
1180
1181 return 0; /* success return 0 */
1182}
1183
1197uint8_t stts22h_set_reg(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1198{
1199 if (handle == NULL) /* check handle */
1200 {
1201 return 2; /* return error */
1202 }
1203 if (handle->inited != 1) /* check handle initialization */
1204 {
1205 return 3; /* return error */
1206 }
1207
1208 return a_stts22h_write(handle, reg, buf, len); /* write data */
1209}
1210
1224uint8_t stts22h_get_reg(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1225{
1226 if (handle == NULL) /* check handle */
1227 {
1228 return 2; /* return error */
1229 }
1230 if (handle->inited != 1) /* check handle initialization */
1231 {
1232 return 3; /* return error */
1233 }
1234
1235 return a_stts22h_read(handle, reg, buf, len); /* read data */
1236}
1237
1247{
1248 if (info == NULL) /* check handle */
1249 {
1250 return 2; /* return error */
1251 }
1252
1253 memset(info, 0, sizeof(stts22h_info_t)); /* initialize stts22h info structure */
1254 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1255 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1256 strncpy(info->interface, "IIC", 8); /* copy interface name */
1257 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1258 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1259 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1260 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1261 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1262 info->driver_version = DRIVER_VERSION; /* set driver version */
1263
1264 return 0; /* success return 0 */
1265}
#define STTS22H_REG_TEMP_L_OUT
#define STTS22H_REG_TEMP_H_LIMIT
#define STTS22H_REG_WHOAMI
chip register definition
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define STTS22H_REG_STATUS
#define STTS22H_REG_CTRL
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define STTS22H_REG_TEMP_L_LIMIT
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver stts22h header file
uint8_t stts22h_init(stts22h_handle_t *handle)
initialize the chip
uint8_t stts22h_set_temperature_low_limit(stts22h_handle_t *handle, uint8_t raw)
set temperature low limit
uint8_t stts22h_get_temperature_high_limit(stts22h_handle_t *handle, uint8_t *raw)
get temperature high limit
uint8_t stts22h_get_temperature_low_limit(stts22h_handle_t *handle, uint8_t *raw)
get temperature low limit
uint8_t stts22h_stop_continuous_read(stts22h_handle_t *handle)
stop the chip reading
uint8_t stts22h_get_status(stts22h_handle_t *handle, uint8_t *status)
get status
uint8_t stts22h_get_addr_pin(stts22h_handle_t *handle, stts22h_address_t *addr_pin)
get the iic address pin
stts22h_bool_t
stts22h bool enumeration definition
uint8_t stts22h_set_iic_address_auto_increment(stts22h_handle_t *handle, stts22h_bool_t enable)
enable or disable iic address auto increment
uint8_t stts22h_set_temperature_high_limit(stts22h_handle_t *handle, uint8_t raw)
set temperature high limit
struct stts22h_handle_s stts22h_handle_t
stts22h handle structure definition
uint8_t stts22h_get_disable_smbus_timeout(stts22h_handle_t *handle, stts22h_bool_t *enable)
get disable smbus timeout status
uint8_t stts22h_temperature_convert_to_data(stts22h_handle_t *handle, uint8_t reg, float *celsius_deg)
convert the register raw data to the temperature
uint8_t stts22h_set_addr_pin(stts22h_handle_t *handle, stts22h_address_t addr_pin)
set the iic address pin
uint8_t stts22h_set_output_data_rate(stts22h_handle_t *handle, stts22h_output_data_rate_t rate)
set output data rate
uint8_t stts22h_deinit(stts22h_handle_t *handle)
close the chip
uint8_t stts22h_get_iic_address_auto_increment(stts22h_handle_t *handle, stts22h_bool_t *enable)
get iic address auto increment status
uint8_t stts22h_get_block_data_update(stts22h_handle_t *handle, stts22h_bool_t *enable)
get block data update status
uint8_t stts22h_info(stts22h_info_t *info)
get chip's information
uint8_t stts22h_set_block_data_update(stts22h_handle_t *handle, stts22h_bool_t enable)
enable or disable block data update
uint8_t stts22h_irq_handler(stts22h_handle_t *handle)
irq handler
uint8_t stts22h_set_disable_smbus_timeout(stts22h_handle_t *handle, stts22h_bool_t enable)
enable or disable disable smbus timeout
uint8_t stts22h_continuous_read(stts22h_handle_t *handle, int16_t *raw, float *celsius_deg)
read data from the chip continuously
uint8_t stts22h_temperature_convert_to_register(stts22h_handle_t *handle, float celsius_deg, uint8_t *reg)
convert the temperature to the register raw data
stts22h_address_t
stts22h address enumeration definition
uint8_t stts22h_start_continuous_read(stts22h_handle_t *handle)
start the chip reading
uint8_t stts22h_single_read(stts22h_handle_t *handle, int16_t *raw, float *celsius_deg)
read data from the chip once
struct stts22h_info_s stts22h_info_t
stts22h information structure definition
uint8_t stts22h_get_output_data_rate(stts22h_handle_t *handle, stts22h_output_data_rate_t *rate)
get output data rate
stts22h_output_data_rate_t
stts22h output data rate enumeration definition
@ STTS22H_STATUS_OVER_HIGH_LIMIT
@ STTS22H_STATUS_UNDER_LOW_LIMIT
@ STTS22H_OUTPUT_DATA_RATE_1HZ
uint8_t stts22h_get_reg(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t stts22h_set_reg(stts22h_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* receive_callback)(uint8_t type)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint32_t driver_version
char manufacturer_name[32]