LibDriver HTU21D
Loading...
Searching...
No Matches
driver_htu21d.c
Go to the documentation of this file.
1
36
37#include "driver_htu21d.h"
38
42#define CHIP_NAME "TE HTU21D"
43#define MANUFACTURER_NAME "TE"
44#define SUPPLY_VOLTAGE_MIN 1.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.5f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define HTU21D_ADDRESS 0x80
55
59#define HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER 0xE3
60#define HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER 0xE5
61#define HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER 0xF3
62#define HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER 0xF5
63#define HTU21D_COMMAND_WRITE_USER_REGISTER 0xE6
64#define HTU21D_COMMAND_READ_USER_REGISTER 0xE7
65#define HTU21D_COMMAND_SOFT_RESET 0xFE
66
77static uint8_t a_htu21d_write_cmd(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
78{
79 if (handle->iic_write_cmd(HTU21D_ADDRESS, buf, len) != 0) /* iic write */
80 {
81 return 1; /* return error */
82 }
83 else
84 {
85 return 0; /* success return 0 */
86 }
87}
88
99static uint8_t a_htu21d_read_cmd(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
100{
101 if (handle->iic_read_cmd(HTU21D_ADDRESS, buf, len) != 0) /* iic read */
102 {
103 return 1; /* return error */
104 }
105 else
106 {
107 return 0; /* success return 0 */
108 }
109}
110
122static uint8_t a_htu21d_write(htu21d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
123{
124 if (handle->iic_write(HTU21D_ADDRESS, reg, buf, len) != 0) /* iic write */
125 {
126 return 1; /* return error */
127 }
128 else
129 {
130 return 0; /* success return 0 */
131 }
132}
133
146static uint8_t a_htu21d_read(htu21d_handle_t *handle, uint8_t mode, uint8_t reg, uint8_t *buf, uint16_t len)
147{
148 if (mode == HTU21D_MODE_HOLD_MASTER) /* hold master */
149 {
150 if (handle->iic_read_with_scl(HTU21D_ADDRESS, reg, buf, len) != 0) /* iic read */
151 {
152 return 1; /* return error */
153 }
154 else
155 {
156 return 0; /* success return 0 */
157 }
158 }
159 else /* no hold master */
160 {
161 if (handle->iic_read(HTU21D_ADDRESS, reg, buf, len) != 0) /* iic read */
162 {
163 return 1; /* return error */
164 }
165 else
166 {
167 return 0; /* success return 0 */
168 }
169 }
170}
171
181static uint8_t a_htu21d_crc(uint16_t value, uint32_t crc)
182{
183 uint32_t polynom = 0x988000U;
184 uint32_t msb = 0x800000U;
185 uint32_t mask = 0xFF8000U;
186 uint32_t result = (uint32_t)value << 8;
187
188 while (msb != 0x80) /* check the msb */
189 {
190 if ((result & msb) != 0) /* check the result */
191 {
192 result = ((result ^ polynom) & mask) | (result & (~mask)); /* get the new result */
193 }
194 msb >>= 1; /* right shift 1 */
195 mask >>= 1; /* right shift 1 */
196 polynom >>= 1; /* right shift 1 */
197 }
198 if (result == crc) /* check the result */
199 {
200 return 0; /* success return 0 */
201 }
202 else
203 {
204 return 1; /* return error */
205 }
206}
207
220{
221 if (handle == NULL) /* check handle */
222 {
223 return 2; /* return error */
224 }
225 if (handle->inited != 1) /* check handle initialization */
226 {
227 return 3; /* return error */
228 }
229
230 handle->mode = mode; /* set the mode */
231
232 return 0; /* success return 0 */
233}
234
247{
248 if (handle == NULL) /* check handle */
249 {
250 return 2; /* return error */
251 }
252 if (handle->inited != 1) /* check handle initialization */
253 {
254 return 3; /* return error */
255 }
256
257 *mode = (htu21d_mode_t)(handle->mode); /* get the mode */
258
259 return 0; /* success return 0 */
260}
261
273{
274 uint8_t res;
275
276 if (handle == NULL) /* check handle */
277 {
278 return 2; /* return error */
279 }
280 if (handle->inited != 1) /* check handle initialization */
281 {
282 return 3; /* return error */
283 }
284
285 res = a_htu21d_write(handle, HTU21D_COMMAND_SOFT_RESET, NULL, 0); /* soft reset */
286 if (res != 0) /* check the result */
287 {
288 handle->debug_print("htu21d: write failed.\n"); /* write failed */
289
290 return 1; /* return error */
291 }
292 handle->delay_ms(15); /* delay 15 ms */
293
294 return 0; /* success return 0 */
295}
296
314 uint16_t *temperature_raw, float *temperature_s,
315 uint16_t *humidity_raw, float *humidity_s
316 )
317{
318 uint8_t res;
319 uint8_t buf[3];
320 uint8_t status;
321 uint16_t data;
322
323 if (handle == NULL) /* check handle */
324 {
325 return 2; /* return error */
326 }
327 if (handle->inited != 1) /* check handle initialization */
328 {
329 return 3; /* return error */
330 }
331
332 if (handle->mode == HTU21D_MODE_HOLD_MASTER) /* hold master mode */
333 {
334 res = a_htu21d_read(handle, HTU21D_MODE_HOLD_MASTER,
335 HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER, buf, 3); /* read temperature */
336 if (res != 0) /* check the result */
337 {
338 handle->debug_print("htu21d: read failed.\n"); /* read failed */
339
340 return 1; /* return error */
341 }
342 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
343 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
344 {
345 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
346
347 return 4; /* return error */
348 }
349 status = data & 0x3; /* get the status */
350 if (status != 0x00) /* check the status */
351 {
352 handle->debug_print("htu21d: status is error.\n"); /* status is error */
353
354 return 5; /* return error */
355 }
356 *temperature_raw = data; /* copy data */
357
358 res = a_htu21d_read(handle, HTU21D_MODE_HOLD_MASTER,
359 HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER, buf, 3); /* read humidity */
360 if (res != 0) /* check the result */
361 {
362 handle->debug_print("htu21d: read failed.\n"); /* read failed */
363
364 return 1; /* return error */
365 }
366 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
367 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
368 {
369 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
370
371 return 4; /* return error */
372 }
373 status = data & 0x3; /* get the status */
374 if (status != 0x02) /* check the status */
375 {
376 handle->debug_print("htu21d: status is error.\n"); /* status is error */
377
378 return 5; /* return error */
379 }
380 *humidity_raw = data; /* copy data */
381 }
382 else /* no hold master mode */
383 {
384 uint8_t cmd;
385
386 cmd = HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER; /* read temperature */
387 res = a_htu21d_write_cmd(handle, &cmd, 1); /* write the command */
388 if (res != 0) /* check the result */
389 {
390 handle->debug_print("htu21d: write cmd failed.\n"); /* write cmd failed */
391
392 return 1; /* return error */
393 }
394 if (handle->resolution == HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS) /* temp 11 bits and rh 11 bits */
395 {
396 handle->delay_ms(7); /* delay 7 ms */
397 }
398 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS) /* temp 12 bits and rh 8 bits */
399 {
400 handle->delay_ms(13); /* delay 13 ms */
401 }
402 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS) /* temp 13 bits and rh 10 bits */
403 {
404 handle->delay_ms(25); /* delay 25 ms */
405 }
406 else /* temp 14 bits and rh 12 bits */
407 {
408 handle->delay_ms(50); /* delay 50 ms */
409 }
410 res = a_htu21d_read_cmd(handle, buf, 3);
411 if (res != 0) /* check the result */
412 {
413 handle->debug_print("htu21d: read cmd failed.\n"); /* read cmd failed */
414
415 return 1; /* return error */
416 }
417 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
418 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
419 {
420 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
421
422 return 4; /* return error */
423 }
424 status = data & 0x3; /* get the status */
425 if (status != 0x00) /* check the status */
426 {
427 handle->debug_print("htu21d: status is error.\n"); /* status is error */
428
429 return 5; /* return error */
430 }
431 *temperature_raw = data; /* copy data */
432
433 cmd = HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER; /* read humidity */
434 res = a_htu21d_write_cmd(handle, &cmd, 1); /* write the command */
435 if (res != 0) /* check the result */
436 {
437 handle->debug_print("htu21d: write cmd failed.\n"); /* write cmd failed */
438
439 return 1; /* return error */
440 }
441 if (handle->resolution == HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS) /* temp 11 bits and rh 11 bits */
442 {
443 handle->delay_ms(7); /* delay 7 ms */
444 }
445 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS) /* temp 12 bits and rh 8 bits */
446 {
447 handle->delay_ms(13); /* delay 13 ms */
448 }
449 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS) /* temp 13 bits and rh 10 bits */
450 {
451 handle->delay_ms(25); /* delay 25 ms */
452 }
453 else /* temp 14 bits and rh 12 bits */
454 {
455 handle->delay_ms(50); /* delay 50 ms */
456 }
457 res = a_htu21d_read_cmd(handle, buf, 3);
458 if (res != 0) /* check the result */
459 {
460 handle->debug_print("htu21d: read cmd failed.\n"); /* read cmd failed */
461
462 return 1; /* return error */
463 }
464 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
465 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
466 {
467 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
468
469 return 4; /* return error */
470 }
471 status = data & 0x3; /* get the status */
472 if (status != 0x02) /* check the status */
473 {
474 handle->debug_print("htu21d: status is error.\n"); /* status is error */
475
476 return 5; /* return error */
477 }
478 *humidity_raw = data; /* copy data */
479 }
480
481 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f; /* convert raw temperature */
482 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f; /* convert raw humidity */
483
484 return 0; /* success return 0 */
485}
486
501uint8_t htu21d_read_temperature(htu21d_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
502{
503 uint8_t res;
504 uint8_t buf[3];
505 uint8_t status;
506 uint16_t data;
507
508 if (handle == NULL) /* check handle */
509 {
510 return 2; /* return error */
511 }
512 if (handle->inited != 1) /* check handle initialization */
513 {
514 return 3; /* return error */
515 }
516
517 if (handle->mode == HTU21D_MODE_HOLD_MASTER) /* hold master mode */
518 {
519 res = a_htu21d_read(handle, HTU21D_MODE_HOLD_MASTER,
520 HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER, buf, 3); /* read temperature */
521 if (res != 0) /* check the result */
522 {
523 handle->debug_print("htu21d: read failed.\n"); /* read failed */
524
525 return 1; /* return error */
526 }
527 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
528 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
529 {
530 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
531
532 return 4; /* return error */
533 }
534 status = data & 0x3; /* get the status */
535 if (status != 0x00) /* check the status */
536 {
537 handle->debug_print("htu21d: status is error.\n"); /* status is error */
538
539 return 5; /* return error */
540 }
541 *temperature_raw = data; /* copy data */
542 }
543 else /* no hold master mode */
544 {
545 uint8_t cmd;
546
547 cmd = HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER; /* read temperature */
548 res = a_htu21d_write_cmd(handle, &cmd, 1); /* write the command */
549 if (res != 0) /* check the result */
550 {
551 handle->debug_print("htu21d: write cmd failed.\n"); /* write cmd failed */
552
553 return 1; /* return error */
554 }
555 if (handle->resolution == HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS) /* temp 11 bits and rh 11 bits */
556 {
557 handle->delay_ms(7); /* delay 7 ms */
558 }
559 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS) /* temp 12 bits and rh 8 bits */
560 {
561 handle->delay_ms(13); /* delay 13 ms */
562 }
563 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS) /* temp 13 bits and rh 10 bits */
564 {
565 handle->delay_ms(25); /* delay 25 ms */
566 }
567 else /* temp 14 bits and rh 12 bits */
568 {
569 handle->delay_ms(50); /* delay 50 ms */
570 }
571 res = a_htu21d_read_cmd(handle, buf, 3);
572 if (res != 0) /* check the result */
573 {
574 handle->debug_print("htu21d: read cmd failed.\n"); /* read cmd failed */
575
576 return 1; /* return error */
577 }
578 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
579 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
580 {
581 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
582
583 return 4; /* return error */
584 }
585 status = data & 0x3; /* get the status */
586 if (status != 0x00) /* check the status */
587 {
588 handle->debug_print("htu21d: status is error.\n"); /* status is error */
589
590 return 5; /* return error */
591 }
592 *temperature_raw = data; /* copy data */
593 }
594
595 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f; /* convert raw temperature */
596
597 return 0; /* success return 0 */
598}
599
614uint8_t htu21d_read_humidity(htu21d_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
615{
616 uint8_t res;
617 uint8_t buf[3];
618 uint8_t status;
619 uint16_t data;
620
621 if (handle == NULL) /* check handle */
622 {
623 return 2; /* return error */
624 }
625 if (handle->inited != 1) /* check handle initialization */
626 {
627 return 3; /* return error */
628 }
629
630 if (handle->mode == HTU21D_MODE_HOLD_MASTER) /* hold master mode */
631 {
632 res = a_htu21d_read(handle, HTU21D_MODE_HOLD_MASTER,
633 HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER, buf, 3); /* read humidity */
634 if (res != 0) /* check the result */
635 {
636 handle->debug_print("htu21d: read failed.\n"); /* read failed */
637
638 return 1; /* return error */
639 }
640 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
641 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
642 {
643 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
644
645 return 4; /* return error */
646 }
647 status = data & 0x3; /* get the status */
648 if (status != 0x02) /* check the status */
649 {
650 handle->debug_print("htu21d: status is error.\n"); /* status is error */
651
652 return 5; /* return error */
653 }
654 *humidity_raw = data; /* copy data */
655 }
656 else /* no hold master mode */
657 {
658 uint8_t cmd;
659
660 cmd = HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER; /* read humidity */
661 res = a_htu21d_write_cmd(handle, &cmd, 1); /* write the command */
662 if (res != 0) /* check the result */
663 {
664 handle->debug_print("htu21d: write cmd failed.\n"); /* write cmd failed */
665
666 return 1; /* return error */
667 }
668 if (handle->resolution == HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS) /* temp 11 bits and rh 11 bits */
669 {
670 handle->delay_ms(7); /* delay 7 ms */
671 }
672 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS) /* temp 12 bits and rh 8 bits */
673 {
674 handle->delay_ms(13); /* delay 13 ms */
675 }
676 else if (handle->resolution == HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS) /* temp 13 bits and rh 10 bits */
677 {
678 handle->delay_ms(25); /* delay 25 ms */
679 }
680 else /* temp 14 bits and rh 12 bits */
681 {
682 handle->delay_ms(50); /* delay 50 ms */
683 }
684 res = a_htu21d_read_cmd(handle, buf, 3);
685 if (res != 0) /* check the result */
686 {
687 handle->debug_print("htu21d: read cmd failed.\n"); /* read cmd failed */
688
689 return 1; /* return error */
690 }
691 data = ((uint16_t)buf[0] << 8) | buf[1]; /* get the raw data */
692 if (a_htu21d_crc(data, buf[2]) != 0) /* check the crc */
693 {
694 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
695
696 return 4; /* return error */
697 }
698 status = data & 0x3; /* get the status */
699 if (status != 0x02) /* check the status */
700 {
701 handle->debug_print("htu21d: status is error.\n"); /* status is error */
702
703 return 5; /* return error */
704 }
705 *humidity_raw = data; /* copy data */
706 }
707
708 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f; /* convert raw humidity */
709
710 return 0; /* success return 0 */
711}
712
725{
726 uint8_t res;
727 uint8_t prev;
728
729 if (handle == NULL) /* check handle */
730 {
731 return 2; /* return error */
732 }
733 if (handle->inited != 1) /* check handle initialization */
734 {
735 return 3; /* return error */
736 }
737
738 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
739 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
740 if (res != 0) /* check the result */
741 {
742 handle->debug_print("htu21d: read failed.\n"); /* read failed */
743
744 return 1; /* return error */
745 }
746 prev &= ~(1 << 7); /* clear bit 7 */
747 prev &= ~(1 << 0); /* clear bit 0 */
748 prev |= ((resolution >> 1) & 0x01) << 7; /* set bit 7 */
749 prev |= ((resolution >> 0) & 0x01) << 0; /* set bit 0 */
750 res = a_htu21d_write(handle, HTU21D_COMMAND_WRITE_USER_REGISTER, &prev, 1); /* write config */
751 if (res != 0) /* check the result */
752 {
753 handle->debug_print("htu21d: write failed.\n"); /* write failed */
754
755 return 1; /* return error */
756 }
757
758 return 0; /* success return 0 */
759}
760
773{
774 uint8_t res;
775 uint8_t prev;
776
777 if (handle == NULL) /* check handle */
778 {
779 return 2; /* return error */
780 }
781 if (handle->inited != 1) /* check handle initialization */
782 {
783 return 3; /* return error */
784 }
785
786 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
787 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
788 if (res != 0) /* check the result */
789 {
790 handle->debug_print("htu21d: read failed.\n"); /* read failed */
791
792 return 1; /* return error */
793 }
794 *resolution = (htu21d_resolution_t)((((prev >> 7) & 0x01) << 1)
795 | (((prev >> 0) & 0x01) << 0)); /* get the resolution */
796 handle->resolution = *resolution; /* save the resolution */
797
798 return 0; /* success return 0 */
799}
800
813{
814 uint8_t res;
815 uint8_t prev;
816
817 if (handle == NULL) /* check handle */
818 {
819 return 2; /* return error */
820 }
821 if (handle->inited != 1) /* check handle initialization */
822 {
823 return 3; /* return error */
824 }
825
826 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
827 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
828 if (res != 0) /* check the result */
829 {
830 handle->debug_print("htu21d: read failed.\n"); /* read failed */
831
832 return 1; /* return error */
833 }
834 *status = (htu21d_status_t)((prev >> 6) & 0x01); /* get the status */
835
836 return 0; /* success return 0 */
837}
838
851{
852 uint8_t res;
853 uint8_t prev;
854
855 if (handle == NULL) /* check handle */
856 {
857 return 2; /* return error */
858 }
859 if (handle->inited != 1) /* check handle initialization */
860 {
861 return 3; /* return error */
862 }
863
864 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
865 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
866 if (res != 0) /* check the result */
867 {
868 handle->debug_print("htu21d: read failed.\n"); /* read failed */
869
870 return 1; /* return error */
871 }
872 prev &= ~(1 << 2); /* clear bit 2 */
873 prev |= enable << 2; /* set the bool */
874 res = a_htu21d_write(handle, HTU21D_COMMAND_WRITE_USER_REGISTER, &prev, 1); /* write config */
875 if (res != 0) /* check the result */
876 {
877 handle->debug_print("htu21d: write failed.\n"); /* write failed */
878
879 return 1; /* return error */
880 }
881
882 return 0; /* success return 0 */
883}
884
897{
898 uint8_t res;
899 uint8_t prev;
900
901 if (handle == NULL) /* check handle */
902 {
903 return 2; /* return error */
904 }
905 if (handle->inited != 1) /* check handle initialization */
906 {
907 return 3; /* return error */
908 }
909
910 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
911 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
912 if (res != 0) /* check the result */
913 {
914 handle->debug_print("htu21d: read failed.\n"); /* read failed */
915
916 return 1; /* return error */
917 }
918 *enable = (htu21d_bool_t)((prev >> 2) & 0x01); /* set the bool */
919
920 return 0; /* success return 0 */
921}
922
935{
936 uint8_t res;
937 uint8_t prev;
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 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
949 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
950 if (res != 0) /* check the result */
951 {
952 handle->debug_print("htu21d: read failed.\n"); /* read failed */
953
954 return 1; /* return error */
955 }
956 prev &= ~(1 << 1); /* clear bit 1 */
957 prev |= enable << 1; /* set the bool */
958 res = a_htu21d_write(handle, HTU21D_COMMAND_WRITE_USER_REGISTER, &prev, 1); /* write config */
959 if (res != 0) /* check the result */
960 {
961 handle->debug_print("htu21d: write failed.\n"); /* write failed */
962
963 return 1; /* return error */
964 }
965
966 return 0; /* success return 0 */
967}
968
981{
982 uint8_t res;
983 uint8_t prev;
984
985 if (handle == NULL) /* check handle */
986 {
987 return 2; /* return error */
988 }
989 if (handle->inited != 1) /* check handle initialization */
990 {
991 return 3; /* return error */
992 }
993
994 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
995 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
996 if (res != 0) /* check the result */
997 {
998 handle->debug_print("htu21d: read failed.\n"); /* read failed */
999
1000 return 1; /* return error */
1001 }
1002 *enable = (htu21d_bool_t)((prev >> 1) & 0x01); /* set the bool */
1003
1004 return 0; /* success return 0 */
1005}
1006
1019uint8_t htu21d_get_serial_number(htu21d_handle_t *handle, uint64_t *number)
1020{
1021 uint8_t i;
1022 uint8_t res;
1023 uint8_t cmd[2];
1024 uint8_t data[14];
1025
1026 if (handle == NULL) /* check handle */
1027 {
1028 return 2; /* return error */
1029 }
1030 if (handle->inited != 1) /* check handle initialization */
1031 {
1032 return 3; /* return error */
1033 }
1034
1035 cmd[0] = 0xFA; /* command 0 */
1036 cmd[1] = 0x0F; /* command 1 */
1037 res = a_htu21d_write_cmd(handle, cmd, 2); /* write command */
1038 if (res != 0) /* check the result */
1039 {
1040 handle->debug_print("htu21d: write command failed.\n"); /* write command failed */
1041
1042 return 1; /* return error */
1043 }
1044 res = a_htu21d_read_cmd(handle, data, 8); /* read command */
1045 if (res != 0) /* check the result */
1046 {
1047 handle->debug_print("htu21d: read command failed.\n"); /* read command failed */
1048
1049 return 1; /* return error */
1050 }
1051 cmd[0] = 0xFC; /* command 0 */
1052 cmd[1] = 0xC9; /* command 1 */
1053 res = a_htu21d_write_cmd(handle, cmd, 2); /* write command */
1054 if (res != 0) /* check the result */
1055 {
1056 handle->debug_print("htu21d: write command failed.\n"); /* write command failed */
1057
1058 return 1; /* return error */
1059 }
1060 res = a_htu21d_read_cmd(handle, data + 8, 6); /* read command */
1061 if (res != 0) /* check the result */
1062 {
1063 handle->debug_print("htu21d: read command failed.\n"); /* read command failed */
1064
1065 return 1; /* return error */
1066 }
1067
1068 for (i = 0; i < 8; i += 2) /* check first data */
1069 {
1070 if (a_htu21d_crc(data[i], data[i + 1]) != 0) /* check the crc */
1071 {
1072 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
1073
1074 return 4; /* return error */
1075 }
1076 }
1077 for (i = 8; i < 14; i += 3) /* check last data */
1078 {
1079 if (a_htu21d_crc(((uint16_t)data[i] << 8) | data[i + 1], data[i + 2]) != 0) /* check the crc */
1080 {
1081 handle->debug_print("htu21d: crc is error.\n"); /* crc is error */
1082
1083 return 4; /* return error */
1084 }
1085 }
1086
1087 *number = ((uint64_t)data[0] << 56) | ((uint64_t)data[2] << 48)
1088 | ((uint64_t)data[4] << 40) | ((uint64_t)data[6] << 32)
1089 | ((uint64_t)data[8] << 24) | ((uint64_t)data[9] << 16)
1090 | ((uint64_t)data[11] << 8) | ((uint64_t)data[12] << 0); /* set the number */
1091
1092 return 0; /* success return 0 */
1093}
1094
1108{
1109 uint8_t res;
1110 uint8_t prev;
1111
1112 if (handle == NULL) /* check handle */
1113 {
1114 return 2; /* return error */
1115 }
1116 if (handle->debug_print == NULL) /* check debug_print */
1117 {
1118 return 3; /* return error */
1119 }
1120 if (handle->iic_init == NULL) /* check iic_init */
1121 {
1122 handle->debug_print("htu21d: iic_init is null.\n"); /* iic_init is null */
1123
1124 return 3; /* return error */
1125 }
1126 if (handle->iic_deinit == NULL) /* check iic_deinit */
1127 {
1128 handle->debug_print("htu21d: iic_deinit is null.\n"); /* iic_deinit is null */
1129
1130 return 3; /* return error */
1131 }
1132 if (handle->iic_read == NULL) /* check iic_read */
1133 {
1134 handle->debug_print("htu21d: iic_read is null.\n"); /* iic_read is null */
1135
1136 return 3; /* return error */
1137 }
1138 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
1139 {
1140 handle->debug_print("htu21d: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
1141
1142 return 3; /* return error */
1143 }
1144 if (handle->iic_read_with_scl == NULL) /* check iic_read_with_scl */
1145 {
1146 handle->debug_print("htu21d: iic_read_with_scl is null.\n"); /* iic_read_with_scl is null */
1147
1148 return 3; /* return error */
1149 }
1150 if (handle->iic_write == NULL) /* check iic_write */
1151 {
1152 handle->debug_print("htu21d: iic_write is null.\n"); /* iic_write is null */
1153
1154 return 3; /* return error */
1155 }
1156 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
1157 {
1158 handle->debug_print("htu21d: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
1159
1160 return 3; /* return error */
1161 }
1162 if (handle->delay_ms == NULL) /* check delay_ms */
1163 {
1164 handle->debug_print("htu21d: delay_ms is null.\n"); /* delay_ms is null */
1165
1166 return 3; /* return error */
1167 }
1168
1169 if (handle->iic_init() != 0) /* iic init */
1170 {
1171 handle->debug_print("htu21d: iic init failed.\n"); /* iic init failed */
1172
1173 return 1; /* return error */
1174 }
1175 res = a_htu21d_write(handle, HTU21D_COMMAND_SOFT_RESET, NULL, 0); /* soft reset */
1176 if (res != 0) /* check the result */
1177 {
1178 handle->debug_print("htu21d: soft reset failed.\n"); /* soft reset failed */
1179 (void)handle->iic_deinit(); /* iic deinit */
1180
1181 return 4; /* return error */
1182 }
1183 handle->delay_ms(15); /* delay 15 ms */
1184 res = a_htu21d_read(handle, HTU21D_MODE_NO_HOLD_MASTER,
1185 HTU21D_COMMAND_READ_USER_REGISTER, &prev, 1); /* read config */
1186 if (res != 0) /* check the result */
1187 {
1188 handle->debug_print("htu21d: read config failed.\n"); /* read config failed */
1189 (void)handle->iic_deinit(); /* iic deinit */
1190
1191 return 5; /* return error */
1192 }
1193 handle->resolution = (htu21d_resolution_t)((((prev >> 7) & 0x01) << 1)
1194 | (((prev >> 0) & 0x01) << 0)); /* save the resolution */
1195 handle->mode = HTU21D_MODE_NO_HOLD_MASTER; /* set no hold master */
1196
1197 handle->inited = 1; /* flag finish initialization */
1198
1199 return 0; /* success return 0 */
1200}
1201
1214{
1215 uint8_t res;
1216
1217 if (handle == NULL) /* check handle */
1218 {
1219 return 2; /* return error */
1220 }
1221 if (handle->inited != 1) /* check handle initialization */
1222 {
1223 return 3; /* return error */
1224 }
1225
1226 res = a_htu21d_write(handle, HTU21D_COMMAND_SOFT_RESET, NULL, 0); /* soft reset */
1227 if (res != 0) /* check the result */
1228 {
1229 handle->debug_print("htu21d: soft reset failed.\n"); /* soft reset failed */
1230
1231 return 4; /* return error */
1232 }
1233 handle->delay_ms(15); /* delay 15 ms */
1234 if (handle->iic_deinit() != 0) /* iic deinit */
1235 {
1236 handle->debug_print("htu21d: iic deinit failed.\n"); /* iic deinit failed */
1237
1238 return 1; /* return error */
1239 }
1240 handle->inited = 0; /* flag close */
1241
1242 return 0; /* success return 0 */
1243}
1244
1257uint8_t htu21d_set_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
1258{
1259 if (handle == NULL) /* check handle */
1260 {
1261 return 2; /* return error */
1262 }
1263 if (handle->inited != 1) /* check handle initialization */
1264 {
1265 return 3; /* return error */
1266 }
1267
1268 return a_htu21d_write_cmd(handle, buf, len); /* write command */
1269}
1270
1283uint8_t htu21d_get_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
1284{
1285 if (handle == NULL) /* check handle */
1286 {
1287 return 2; /* return error */
1288 }
1289 if (handle->inited != 1) /* check handle initialization */
1290 {
1291 return 3; /* return error */
1292 }
1293
1294 return a_htu21d_read_cmd(handle, buf, len); /* read command */
1295}
1296
1306{
1307 if (info == NULL) /* check handle */
1308 {
1309 return 2; /* return error */
1310 }
1311
1312 memset(info, 0, sizeof(htu21d_info_t)); /* initialize htu21d info structure */
1313 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1314 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1315 strncpy(info->interface, "IIC", 8); /* copy interface name */
1316 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1317 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1318 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1319 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1320 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1321 info->driver_version = DRIVER_VERSION; /* set driver version */
1322
1323 return 0; /* success return 0 */
1324}
#define HTU21D_COMMAND_READ_USER_REGISTER
#define MAX_CURRENT
#define HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER
#define SUPPLY_VOLTAGE_MAX
#define HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define HTU21D_COMMAND_WRITE_USER_REGISTER
#define HTU21D_COMMAND_SOFT_RESET
#define HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER
chip command definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER
#define HTU21D_ADDRESS
chip address definition
driver htu21d header file
uint8_t htu21d_get_disable_otp_reload(htu21d_handle_t *handle, htu21d_bool_t *enable)
get the disable otp reload status
uint8_t htu21d_set_disable_otp_reload(htu21d_handle_t *handle, htu21d_bool_t enable)
enable or disable otp reload
uint8_t htu21d_read_temperature_humidity(htu21d_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 htu21d_get_heater(htu21d_handle_t *handle, htu21d_bool_t *enable)
get the heater status
uint8_t htu21d_soft_reset(htu21d_handle_t *handle)
soft reset
uint8_t htu21d_get_resolution(htu21d_handle_t *handle, htu21d_resolution_t *resolution)
get the resolution
uint8_t htu21d_info(htu21d_info_t *info)
get chip's information
uint8_t htu21d_set_mode(htu21d_handle_t *handle, htu21d_mode_t mode)
set the chip mode
uint8_t htu21d_init(htu21d_handle_t *handle)
initialize the chip
htu21d_resolution_t
htu21d resolution enumeration definition
htu21d_mode_t
htu21d mode enumeration definition
uint8_t htu21d_set_resolution(htu21d_handle_t *handle, htu21d_resolution_t resolution)
set the resolution
uint8_t htu21d_set_heater(htu21d_handle_t *handle, htu21d_bool_t enable)
enable or disable the heater
struct htu21d_handle_s htu21d_handle_t
htu21d handle structure definition
uint8_t htu21d_read_temperature(htu21d_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
read the temperature data
struct htu21d_info_s htu21d_info_t
htu21d information structure definition
uint8_t htu21d_get_mode(htu21d_handle_t *handle, htu21d_mode_t *mode)
get the chip mode
uint8_t htu21d_get_serial_number(htu21d_handle_t *handle, uint64_t *number)
get the serial number
uint8_t htu21d_get_battery_status(htu21d_handle_t *handle, htu21d_status_t *status)
get the battery status
uint8_t htu21d_read_humidity(htu21d_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
read the humidity data
htu21d_status_t
htu21d status enumeration definition
htu21d_bool_t
htu21d bool enumeration definition
uint8_t htu21d_deinit(htu21d_handle_t *handle)
close the chip
@ HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS
@ HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS
@ HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS
@ HTU21D_MODE_HOLD_MASTER
@ HTU21D_MODE_NO_HOLD_MASTER
uint8_t htu21d_get_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t htu21d_set_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_with_scl)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
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 supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]