LibDriver HTU31D
Loading...
Searching...
No Matches
driver_htu31d.c
Go to the documentation of this file.
1
36
37#include "driver_htu31d.h"
38
42#define CHIP_NAME "TE HTU31D"
43#define MANUFACTURER_NAME "TE"
44#define SUPPLY_VOLTAGE_MIN 3.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.414f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define HTU31D_COMMAND_CONVERSION (0x1 << 6)
55#define HTU31D_COMMAND_READ_T_RH (0x00)
56#define HTU31D_COMMAND_READ_RH (0x1 << 4)
57#define HTU31D_COMMAND_RESET (0x1E)
58#define HTU31D_COMMAND_HEATER_ON (0x1 << 2)
59#define HTU31D_COMMAND_HEATER_OFF (0x1 << 1)
60#define HTU31D_COMMAND_READ_SERIAL_NUMBER (0x0A)
61#define HTU31D_COMMAND_READ_DIAGNOSTIC (1 << 3)
62
74static uint8_t a_htu31d_write(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
75{
76 if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* iic write */
77 {
78 return 1; /* return error */
79 }
80
81 return 0; /* success return 0 */
82}
83
95static uint8_t a_htu31d_read(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
96{
97 if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* iic read */
98 {
99 return 1; /* return error */
100 }
101
102 return 0; /* success return 0 */
103}
104
115static uint8_t a_htu31d_crc(uint8_t *buf, uint8_t len, uint32_t crc)
116{
117 uint32_t polynom = 0x98800000UL;
118 uint32_t msb = 0x80000000UL;
119 uint32_t mask = 0xFF800000UL;
120 uint32_t result = 0;
121
122 if (len == 1) /* if len == 1 */
123 {
124 result = (uint32_t)buf[0] << 8; /* set to buffer */
125 }
126 else if (len == 2) /* if len == 2 */
127 {
128 result = (uint32_t)buf[0] << 16; /* set part 0 */
129 result |= (uint32_t)buf[1] << 8; /* set part 1 */
130 }
131 else if (len == 3) /* if len == 3 */
132 {
133 result = buf[0]; /* set buf 0 */
134 result <<= 8; /* left shift 8 */
135 result |= buf[1]; /* set buf 1 */
136 result <<= 8; /* left shift 8 */
137 result |= buf[2]; /* set buf 2 */
138 result <<= 8; /* left shift 8 */
139 }
140 else
141 {
142 return 1; /* return error */
143 }
144 while (msb != 0x80) /* check the msb */
145 {
146 if ((result & msb) != 0) /* check the result */
147 {
148 result = ((result ^ polynom) & mask) | (result & (~mask)); /* get the new result */
149 }
150 msb >>= 1; /* right shift 1 */
151 mask >>= 1; /* right shift 1 */
152 polynom >>= 1; /* right shift 1 */
153 }
154 if ((result & 0xFF) == (crc & 0xFF)) /* check the result */
155 {
156 return 0; /* success return 0 */
157 }
158 else
159 {
160 return 1; /* return error */
161 }
162}
163
174{
175 if (handle == NULL) /* check handle */
176 {
177 return 2; /* return error */
178 }
179
180 handle->iic_addr = addr_pin; /* set the addr pin */
181
182 return 0; /* success return 0 */
183}
184
195{
196 if (handle == NULL) /* check handle */
197 {
198 return 2; /* return error */
199 }
200
201 *addr_pin = (htu31d_addr_pin_t)(handle->iic_addr); /* get the addr pin */
202
203 return 0; /* success return 0 */
204}
205
217{
218 uint8_t res;
219
220 if (handle == NULL) /* check handle */
221 {
222 return 2; /* return error */
223 }
224 if (handle->inited != 1) /* check handle initialization */
225 {
226 return 3; /* return error */
227 }
228
229 res = a_htu31d_write(handle, HTU31D_COMMAND_RESET, NULL, 0); /* soft reset */
230 if (res != 0) /* check the result */
231 {
232 handle->debug_print("htu31d: write failed.\n"); /* write failed */
233
234 return 1; /* return error */
235 }
236 handle->delay_ms(15); /* delay 15 ms */
237
238 return 0; /* success return 0 */
239}
240
252{
253 uint8_t res;
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 res = a_htu31d_write(handle, HTU31D_COMMAND_HEATER_ON, NULL, 0); /* heater on */
265 if (res != 0) /* check the result */
266 {
267 handle->debug_print("htu31d: write failed.\n"); /* write failed */
268
269 return 1; /* return error */
270 }
271
272 return 0; /* success return 0 */
273}
274
286{
287 uint8_t res;
288
289 if (handle == NULL) /* check handle */
290 {
291 return 2; /* return error */
292 }
293 if (handle->inited != 1) /* check handle initialization */
294 {
295 return 3; /* return error */
296 }
297
298 res = a_htu31d_write(handle, HTU31D_COMMAND_HEATER_OFF, NULL, 0); /* heater off */
299 if (res != 0) /* check the result */
300 {
301 handle->debug_print("htu31d: write failed.\n"); /* write failed */
302
303 return 1; /* return error */
304 }
305
306 return 0; /* success return 0 */
307}
308
321uint8_t htu31d_get_serial_number(htu31d_handle_t *handle, uint8_t number[3])
322{
323 uint8_t res;
324 uint8_t buf[4];
325
326 if (handle == NULL) /* check handle */
327 {
328 return 2; /* return error */
329 }
330 if (handle->inited != 1) /* check handle initialization */
331 {
332 return 3; /* return error */
333 }
334
335 res = a_htu31d_read(handle, HTU31D_COMMAND_READ_SERIAL_NUMBER, buf, 4); /* get serial number */
336 if (res != 0) /* check the result */
337 {
338 handle->debug_print("htu31d: get serial number failed.\n"); /* get serial number failed */
339
340 return 1; /* return error */
341 }
342 if (a_htu31d_crc(buf, 3, buf[3]) != 0) /* check the crc */
343 {
344 handle->debug_print("htu31d: crc checked error.\n"); /* crc checked error */
345
346 return 4; /* return error */
347 }
348 memcpy(number, buf, sizeof(uint8_t) * 3); /* copy to number */
349
350 return 0; /* success return 0 */
351}
352
365uint8_t htu31d_get_diagnostic(htu31d_handle_t *handle, uint8_t *diagnostic)
366{
367 uint8_t res;
368 uint8_t buf[2];
369
370 if (handle == NULL) /* check handle */
371 {
372 return 2; /* return error */
373 }
374 if (handle->inited != 1) /* check handle initialization */
375 {
376 return 3; /* return error */
377 }
378
379 res = a_htu31d_read(handle, HTU31D_COMMAND_READ_DIAGNOSTIC, buf, 2); /* get diagnostic */
380 if (res != 0) /* check the result */
381 {
382 handle->debug_print("htu31d: get diagnostic failed.\n"); /* get diagnostic failed */
383
384 return 1; /* return error */
385 }
386 if (a_htu31d_crc(buf, 1, buf[1]) != 0) /* check the crc */
387 {
388 handle->debug_print("htu31d: crc checked error.\n"); /* crc checked error */
389
390 return 4; /* return error */
391 }
392 *diagnostic = buf[1]; /* copy to diagnostic */
393
394 return 0; /* success return 0 */
395}
396
414 uint16_t *temperature_raw, float *temperature_s,
415 uint16_t *humidity_raw, float *humidity_s
416 )
417{
418 uint8_t res;
419 uint8_t prev;
420 uint8_t buf[6];
421
422 if (handle == NULL) /* check handle */
423 {
424 return 2; /* return error */
425 }
426 if (handle->inited != 1) /* check handle initialization */
427 {
428 return 3; /* return error */
429 }
430
432 | (handle->humidity_osr & 0x3) << 3
433 | (handle->temperature_osr & 0x3) << 1; /* set the command */
434 res = a_htu31d_write(handle, prev, NULL, 0); /* conversion */
435 if (res != 0) /* check the result */
436 {
437 handle->debug_print("htu31d: write failed.\n"); /* write failed */
438
439 return 1; /* return error */
440 }
441 if (handle->humidity_osr == 0) /* osr 0 */
442 {
443 handle->delay_ms(2); /* delay 2ms */
444 }
445 else if (handle->humidity_osr == 1) /* osr 1 */
446 {
447 handle->delay_ms(3); /* delay 3ms */
448 }
449 else if (handle->humidity_osr == 2) /* osr 2 */
450 {
451 handle->delay_ms(5); /* delay 5ms */
452 }
453 else if (handle->humidity_osr == 3) /* osr 3 */
454 {
455 handle->delay_ms(9); /* delay 9ms */
456 }
457 else
458 {
459 handle->debug_print("htu31d: param is error.\n"); /* param is error */
460
461 return 5; /* return error */
462 }
463 if (handle->temperature_osr == 0) /* osr 0 */
464 {
465 handle->delay_ms(2); /* delay 2ms */
466 }
467 else if (handle->temperature_osr == 1) /* osr 1 */
468 {
469 handle->delay_ms(4); /* delay 4ms */
470 }
471 else if (handle->temperature_osr == 2) /* osr 2 */
472 {
473 handle->delay_ms(7); /* delay 7ms */
474 }
475 else if (handle->temperature_osr == 3) /* osr 3 */
476 {
477 handle->delay_ms(13); /* delay 13ms */
478 }
479 else
480 {
481 handle->debug_print("htu31d: param is error.\n"); /* param is error */
482
483 return 5; /* return error */
484 }
485
486 res = a_htu31d_read(handle, HTU31D_COMMAND_READ_T_RH, buf, 6); /* read t && rh */
487 if (res != 0) /* check the result */
488 {
489 handle->debug_print("htu31d: read t && rh failed.\n"); /* read t && rh failed */
490
491 return 1; /* return error */
492 }
493 if (a_htu31d_crc(buf, 2, buf[2]) != 0) /* check the crc */
494 {
495 handle->debug_print("htu31d: crc checked error.\n"); /* crc checked error */
496
497 return 4; /* return error */
498 }
499 if (a_htu31d_crc(buf + 3, 2, buf[5]) != 0) /* check the crc */
500 {
501 handle->debug_print("htu31d: crc checked error.\n"); /* crc checked error */
502
503 return 4; /* return error */
504 }
505 *temperature_raw = ((uint16_t)buf[0] << 8) | buf[1]; /* set temperature raw */
506 *humidity_raw = ((uint16_t)buf[3] << 8) | buf[4]; /* set humidity raw */
507
508 *temperature_s = (float)(*temperature_raw) / 65535.0f * 165.0f - 40.0f; /* convert raw temperature */
509 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
510
511 return 0; /* success return 0 */
512}
513
528uint8_t htu31d_read_humidity(htu31d_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
529{
530 uint8_t res;
531 uint8_t prev;
532 uint8_t buf[3];
533
534 if (handle == NULL) /* check handle */
535 {
536 return 2; /* return error */
537 }
538 if (handle->inited != 1) /* check handle initialization */
539 {
540 return 3; /* return error */
541 }
542
544 | (handle->humidity_osr & 0x3) << 3
545 | (handle->temperature_osr & 0x3) << 1; /* set the command */
546 res = a_htu31d_write(handle, prev, NULL, 0); /* conversion */
547 if (res != 0) /* check the result */
548 {
549 handle->debug_print("htu31d: write failed.\n"); /* write failed */
550
551 return 1; /* return error */
552 }
553 if (handle->humidity_osr == 0) /* osr 0 */
554 {
555 handle->delay_ms(2); /* delay 2ms */
556 }
557 else if (handle->humidity_osr == 1) /* osr 1 */
558 {
559 handle->delay_ms(3); /* delay 3ms */
560 }
561 else if (handle->humidity_osr == 2) /* osr 2 */
562 {
563 handle->delay_ms(5); /* delay 5ms */
564 }
565 else if (handle->humidity_osr == 3) /* osr 3 */
566 {
567 handle->delay_ms(9); /* delay 9ms */
568 }
569 else
570 {
571 handle->debug_print("htu31d: param is error.\n"); /* param is error */
572
573 return 5; /* return error */
574 }
575 if (handle->temperature_osr == 0) /* osr 0 */
576 {
577 handle->delay_ms(2); /* delay 2ms */
578 }
579 else if (handle->temperature_osr == 1) /* osr 1 */
580 {
581 handle->delay_ms(4); /* delay 4ms */
582 }
583 else if (handle->temperature_osr == 2) /* osr 2 */
584 {
585 handle->delay_ms(7); /* delay 7ms */
586 }
587 else if (handle->temperature_osr == 3) /* osr 3 */
588 {
589 handle->delay_ms(13); /* delay 13ms */
590 }
591 else
592 {
593 handle->debug_print("htu31d: param is error.\n"); /* param is error */
594
595 return 5; /* return error */
596 }
597
598 res = a_htu31d_read(handle, HTU31D_COMMAND_READ_RH, buf, 3); /* read rh */
599 if (res != 0) /* check the result */
600 {
601 handle->debug_print("htu31d: read rh failed.\n"); /* read rh failed */
602
603 return 1; /* return error */
604 }
605 if (a_htu31d_crc(buf, 2, buf[2]) != 0) /* check the crc */
606 {
607 handle->debug_print("htu31d: crc checked error.\n"); /* crc checked error */
608
609 return 4; /* return error */
610 }
611 *humidity_raw = ((uint16_t)buf[0] << 8) | buf[1]; /* set humidity raw */
612 *humidity_s = (float)(*humidity_raw) / 65535.0f * 100.0f; /* convert raw humidity */
613
614 return 0; /* success return 0 */
615}
616
629{
630 uint8_t res;
631
632 if (handle == NULL) /* check handle */
633 {
634 return 2; /* return error */
635 }
636 if (handle->debug_print == NULL) /* check debug_print */
637 {
638 return 3; /* return error */
639 }
640 if (handle->iic_init == NULL) /* check iic_init */
641 {
642 handle->debug_print("htu31d: iic_init is null.\n"); /* iic_init is null */
643
644 return 3; /* return error */
645 }
646 if (handle->iic_deinit == NULL) /* check iic_deinit */
647 {
648 handle->debug_print("htu31d: iic_deinit is null.\n"); /* iic_deinit is null */
649
650 return 3; /* return error */
651 }
652 if (handle->iic_read == NULL) /* check iic_read */
653 {
654 handle->debug_print("htu31d: iic_read is null.\n"); /* iic_read is null */
655
656 return 3; /* return error */
657 }
658 if (handle->iic_write == NULL) /* check iic_write */
659 {
660 handle->debug_print("htu31d: iic_write is null.\n"); /* iic_write is null */
661
662 return 3; /* return error */
663 }
664 if (handle->delay_ms == NULL) /* check delay_ms */
665 {
666 handle->debug_print("htu31d: delay_ms is null.\n"); /* delay_ms is null */
667
668 return 3; /* return error */
669 }
670
671 if (handle->iic_init() != 0) /* iic init */
672 {
673 handle->debug_print("htu31d: iic init failed.\n"); /* iic init failed */
674
675 return 1; /* return error */
676 }
677 res = a_htu31d_write(handle, HTU31D_COMMAND_RESET, NULL, 0); /* soft reset */
678 if (res != 0) /* check the result */
679 {
680 handle->debug_print("htu31d: soft reset failed.\n"); /* soft reset failed */
681 (void)handle->iic_deinit(); /* iic deinit */
682
683 return 4; /* return error */
684 }
685 handle->delay_ms(15); /* delay 15 ms */
686 handle->inited = 1; /* flag finish initialization */
687
688 return 0; /* success return 0 */
689}
690
703{
704 uint8_t res;
705
706 if (handle == NULL) /* check handle */
707 {
708 return 2; /* return error */
709 }
710 if (handle->inited != 1) /* check handle initialization */
711 {
712 return 3; /* return error */
713 }
714
715 res = a_htu31d_write(handle, HTU31D_COMMAND_RESET, NULL, 0); /* soft reset */
716 if (res != 0) /* check the result */
717 {
718 handle->debug_print("htu31d: soft reset failed.\n"); /* soft reset failed */
719
720 return 4; /* return error */
721 }
722 handle->delay_ms(15); /* delay 15 ms */
723 if (handle->iic_deinit() != 0) /* iic deinit */
724 {
725 handle->debug_print("htu31d: iic deinit failed.\n"); /* iic deinit failed */
726
727 return 1; /* return error */
728 }
729 handle->inited = 0; /* flag close */
730
731 return 0; /* success return 0 */
732}
733
745{
746 if (handle == NULL) /* check handle */
747 {
748 return 2; /* return error */
749 }
750 if (handle->inited != 1) /* check handle initialization */
751 {
752 return 3; /* return error */
753 }
754
755 handle->humidity_osr = osr; /* set osr */
756
757 return 0; /* success return 0 */
758}
759
771{
772 if (handle == NULL) /* check handle */
773 {
774 return 2; /* return error */
775 }
776 if (handle->inited != 1) /* check handle initialization */
777 {
778 return 3; /* return error */
779 }
780
781 *osr = (htu31d_humidity_osr_t)(handle->humidity_osr); /* get osr */
782
783 return 0; /* success return 0 */
784}
785
797{
798 if (handle == NULL) /* check handle */
799 {
800 return 2; /* return error */
801 }
802 if (handle->inited != 1) /* check handle initialization */
803 {
804 return 3; /* return error */
805 }
806
807 handle->temperature_osr = osr; /* set osr */
808
809 return 0; /* success return 0 */
810}
811
823{
824 if (handle == NULL) /* check handle */
825 {
826 return 2; /* return error */
827 }
828 if (handle->inited != 1) /* check handle initialization */
829 {
830 return 3; /* return error */
831 }
832
833 *osr = (htu31d_temperature_osr_t)(handle->temperature_osr); /* get osr */
834
835 return 0; /* success return 0 */
836}
837
851uint8_t htu31d_set_reg(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
852{
853 if (handle == NULL) /* check handle */
854 {
855 return 2; /* return error */
856 }
857 if (handle->inited != 1) /* check handle initialization */
858 {
859 return 3; /* return error */
860 }
861
862 return a_htu31d_write(handle, reg, buf, len); /* write command */
863}
864
878uint8_t htu31d_get_reg(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
879{
880 if (handle == NULL) /* check handle */
881 {
882 return 2; /* return error */
883 }
884 if (handle->inited != 1) /* check handle initialization */
885 {
886 return 3; /* return error */
887 }
888
889 return a_htu31d_read(handle, reg, buf, len); /* read command */
890}
891
901{
902 if (info == NULL) /* check handle */
903 {
904 return 2; /* return error */
905 }
906
907 memset(info, 0, sizeof(htu31d_info_t)); /* initialize htu31d info structure */
908 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
909 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
910 strncpy(info->interface, "IIC", 8); /* copy interface name */
911 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
912 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
913 info->max_current_ma = MAX_CURRENT; /* set maximum current */
914 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
915 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
916 info->driver_version = DRIVER_VERSION; /* set driver version */
917
918 return 0; /* success return 0 */
919}
#define HTU31D_COMMAND_RESET
#define HTU31D_COMMAND_READ_RH
#define HTU31D_COMMAND_HEATER_ON
#define MAX_CURRENT
#define HTU31D_COMMAND_READ_T_RH
#define HTU31D_COMMAND_CONVERSION
chip command definition
#define HTU31D_COMMAND_HEATER_OFF
#define SUPPLY_VOLTAGE_MAX
#define HTU31D_COMMAND_READ_SERIAL_NUMBER
#define TEMPERATURE_MAX
#define HTU31D_COMMAND_READ_DIAGNOSTIC
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver htu31d header file
uint8_t htu31d_soft_reset(htu31d_handle_t *handle)
soft reset
uint8_t htu31d_set_addr_pin(htu31d_handle_t *handle, htu31d_addr_pin_t addr_pin)
set the address pin
uint8_t htu31d_get_serial_number(htu31d_handle_t *handle, uint8_t number[3])
get the serial number
struct htu31d_info_s htu31d_info_t
htu31d information structure definition
uint8_t htu31d_deinit(htu31d_handle_t *handle)
close the chip
uint8_t htu31d_get_humidity_osr(htu31d_handle_t *handle, htu31d_humidity_osr_t *osr)
get humidity osr
htu31d_humidity_osr_t
htu31d humidity osr enumeration definition
uint8_t htu31d_read_humidity(htu31d_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
read the humidity data
htu31d_temperature_osr_t
htu31d temperature osr enumeration definition
htu31d_addr_pin_t
htu31d addr pin enumeration definition
uint8_t htu31d_init(htu31d_handle_t *handle)
initialize the chip
uint8_t htu31d_set_heater_on(htu31d_handle_t *handle)
enable heater
uint8_t htu31d_set_heater_off(htu31d_handle_t *handle)
disable heater
uint8_t htu31d_read_temperature_humidity(htu31d_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read the temperature and humidity data
uint8_t htu31d_info(htu31d_info_t *info)
get chip's information
uint8_t htu31d_get_temperature_osr(htu31d_handle_t *handle, htu31d_temperature_osr_t *osr)
get temperature osr
uint8_t htu31d_set_temperature_osr(htu31d_handle_t *handle, htu31d_temperature_osr_t osr)
set temperature osr
uint8_t htu31d_get_addr_pin(htu31d_handle_t *handle, htu31d_addr_pin_t *addr_pin)
get the address pin
uint8_t htu31d_get_diagnostic(htu31d_handle_t *handle, uint8_t *diagnostic)
get the diagnostic
struct htu31d_handle_s htu31d_handle_t
htu31d handle structure definition
uint8_t htu31d_set_humidity_osr(htu31d_handle_t *handle, htu31d_humidity_osr_t osr)
set humidity osr
uint8_t htu31d_set_reg(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t htu31d_get_reg(htu31d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t temperature_osr
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)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]