LibDriver CCS811
Loading...
Searching...
No Matches
driver_ccs811.c
Go to the documentation of this file.
1
36
37#include "driver_ccs811.h"
38
42#define CHIP_NAME "AMS CCS811"
43#define MANUFACTURER_NAME "AMS"
44#define SUPPLY_VOLTAGE_MIN 1.8f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 54.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define CCS811_REG_STATUS 0x00
55#define CCS811_REG_MEAS_MODE 0x01
56#define CCS811_REG_ALG_RESULT_DATA 0x02
57#define CCS811_REG_RAW_DATA 0x03
58#define CCS811_REG_ENV_DATA 0x05
59#define CCS811_REG_THRESHOLDS 0x10
60#define CCS811_REG_BASELINE 0x11
61#define CCS811_REG_APP_ERASE 0xF1
62#define CCS811_REG_APP_DATA 0xF2
63#define CCS811_REG_APP_VERIFY 0xF3
64#define CCS811_REG_APP_START 0xF4
65#define CCS811_REG_HW_ID 0x20
66#define CCS811_REG_HW_VERSION 0x21
67#define CCS811_REG_FW_BOOT_VERSION 0x23
68#define CCS811_REG_FW_APP_VERSION 0x24
69#define CCS811_REG_ERROR_ID 0xE0
70#define CCS811_REG_SW_RESET 0xFF
71
83static uint8_t a_ccs811_iic_read(ccs811_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
84{
85 if (handle->iic_read(handle->iic_addr, reg, data, len) != 0) /* read data */
86 {
87 return 1; /* return error */
88 }
89
90 return 0; /* success return 0 */
91}
92
104static uint8_t a_ccs811_iic_write(ccs811_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
105{
106 if (handle->iic_write(handle->iic_addr, reg, data, len) != 0) /* write data */
107 {
108 return 1; /* return error */
109 }
110
111 return 0; /* success return 0 */
112}
113
124{
125 if (handle == NULL) /* check handle */
126 {
127 return 2; /* return error */
128 }
129
130 handle->iic_addr = (uint8_t)addr_pin; /* set iic address */
131
132 return 0; /* success return 0 */
133}
134
145{
146 if (handle == NULL) /* check handle */
147 {
148 return 2; /* return error */
149 }
150
151 *addr_pin = (ccs811_address_t)(handle->iic_addr); /* set iic address */
152
153 return 0; /* success return 0 */
154}
155
167uint8_t ccs811_get_status(ccs811_handle_t *handle, uint8_t *status)
168{
169 uint8_t res;
170
171 if (handle == NULL) /* check handle */
172 {
173 return 2; /* return error */
174 }
175 if (handle->inited != 1) /* check handle initialization */
176 {
177 return 3; /* return error */
178 }
179
180 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, status, 1); /* get status */
181 if (res != 0) /* check result */
182 {
183 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
184
185 return 1; /* return error */
186 }
187
188 return 0; /* success return 0 */
189}
190
203{
204 uint8_t res;
205 uint8_t prev;
206
207 if (handle == NULL) /* check handle */
208 {
209 return 2; /* return error */
210 }
211 if (handle->inited != 1) /* check handle initialization */
212 {
213 return 3; /* return error */
214 }
215
216 res = a_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
217 if (res != 0) /* check result */
218 {
219 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
220
221 return 1; /* return error */
222 }
223 prev &= ~(7 << 4); /* clear settings */
224 prev |= mode << 4; /* set mode */
225 res = a_ccs811_iic_write(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* set meas mode */
226 if (res != 0) /* check result */
227 {
228 handle->debug_print("ccs811: set meas mode failed.\n"); /* set meas mode failed */
229
230 return 1; /* return error */
231 }
232
233 return 0; /* success return 0 */
234}
235
248{
249 uint8_t res;
250 uint8_t prev;
251
252 if (handle == NULL) /* check handle */
253 {
254 return 2; /* return error */
255 }
256 if (handle->inited != 1) /* check handle initialization */
257 {
258 return 3; /* return error */
259 }
260
261 res = a_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
262 if (res != 0) /* check result */
263 {
264 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
265
266 return 1; /* return error */
267 }
268 *mode = (ccs811_mode_t)((prev >> 4) & 0x07); /* set mode */
269
270 return 0; /* success return 0 */
271}
272
285{
286 uint8_t res;
287 uint8_t prev;
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_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
299 if (res != 0) /* check result */
300 {
301 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
302
303 return 1; /* return error */
304 }
305 prev &= ~(1 << 3); /* clear settings */
306 prev |= enable << 3; /* set bool */
307 res = a_ccs811_iic_write(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* set meas mode */
308 if (res != 0) /* check result */
309 {
310 handle->debug_print("ccs811: set meas mode failed.\n"); /* set meas mode failed */
311
312 return 1; /* return error */
313 }
314
315 return 0; /* success return 0 */
316}
317
330{
331 uint8_t res;
332 uint8_t prev;
333
334 if (handle == NULL) /* check handle */
335 {
336 return 2; /* return error */
337 }
338 if (handle->inited != 1) /* check handle initialization */
339 {
340 return 3; /* return error */
341 }
342
343 res = a_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
344 if (res != 0) /* check result */
345 {
346 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
347
348 return 1; /* return error */
349 }
350 *enable = (ccs811_bool_t)((prev >> 3) & 0x01); /* set bool */
351
352 return 0; /* success return 0 */
353}
354
367{
368 uint8_t res;
369 uint8_t prev;
370
371 if (handle == NULL) /* check handle */
372 {
373 return 2; /* return error */
374 }
375 if (handle->inited != 1) /* check handle initialization */
376 {
377 return 3; /* return error */
378 }
379
380 res = a_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
381 if (res != 0) /* check result */
382 {
383 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
384
385 return 1; /* return error */
386 }
387 prev &= ~(1 << 2); /* clear settings */
388 prev |= enable << 2; /* set bool */
389 res = a_ccs811_iic_write(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* set meas mode */
390 if (res != 0) /* check result */
391 {
392 handle->debug_print("ccs811: set meas mode failed.\n"); /* set meas mode failed */
393
394 return 1; /* return error */
395 }
396
397 return 0; /* success return 0 */
398}
399
412{
413 uint8_t res;
414 uint8_t prev;
415
416 if (handle == NULL) /* check handle */
417 {
418 return 2; /* return error */
419 }
420 if (handle->inited != 1) /* check handle initialization */
421 {
422 return 3; /* return error */
423 }
424
425 res = a_ccs811_iic_read(handle, CCS811_REG_MEAS_MODE, &prev, 1); /* get meas mode */
426 if (res != 0) /* check result */
427 {
428 handle->debug_print("ccs811: get meas mode failed.\n"); /* get meas mode failed */
429
430 return 1; /* return error */
431 }
432 *enable = (ccs811_bool_t)((prev >> 2) & 0x01); /* set bool */
433
434 return 0; /* success return 0 */
435}
436
452uint8_t ccs811_read(ccs811_handle_t *handle, uint16_t *eco2_ppm, uint16_t *tvoc_ppb, uint16_t *raw)
453{
454 uint8_t res;
455 uint8_t buf[8];
456
457 if (handle == NULL) /* check handle */
458 {
459 return 2; /* return error */
460 }
461 if (handle->inited != 1) /* check handle initialization */
462 {
463 return 3; /* return error */
464 }
465
466 res = a_ccs811_iic_read(handle, CCS811_REG_ALG_RESULT_DATA, buf, 8); /* read data */
467 if (res != 0) /* check result */
468 {
469 handle->debug_print("ccs811: read failed.\n"); /* read failed */
470
471 return 1; /* return error */
472 }
473 if ((buf[4] & CCS811_STATUS_ERROR) != 0) /* check error */
474 {
475 handle->debug_print("ccs811: find error.\n"); /* find error */
476
477 return 4; /* return error */
478 }
479 if ((buf[4] & CCS811_STATUS_DATA_READY) != 0) /* check data ready */
480 {
481 *eco2_ppm = (((uint16_t)buf[0]) << 8) | buf[1]; /* save eco2 */
482 *tvoc_ppb = (((uint16_t)buf[2]) << 8) | buf[3]; /* save tvoc */
483 *raw = (((uint16_t)buf[6]) << 8) | buf[7]; /* save raw */
484 }
485 else
486 {
487 handle->debug_print("ccs811: data is not ready.\n"); /* data is not ready */
488
489 return 5; /* return error */
490 }
491
492 return 0; /* success return 0 */
493}
494
511uint8_t ccs811_get_raw_data(ccs811_handle_t *handle, uint16_t *raw, uint8_t *ua, uint16_t *adc_raw, float *adc_v)
512{
513 uint8_t res;
514 uint8_t status;
515 uint8_t buf[2];
516
517 if (handle == NULL) /* check handle */
518 {
519 return 2; /* return error */
520 }
521 if (handle->inited != 1) /* check handle initialization */
522 {
523 return 3; /* return error */
524 }
525
526 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
527 if (res != 0) /* check result */
528 {
529 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
530
531 return 1; /* return error */
532 }
533 if ((status & CCS811_STATUS_ERROR) != 0) /* check error */
534 {
535 handle->debug_print("ccs811: find error.\n"); /* find error */
536
537 return 4; /* return error */
538 }
539 if ((status & CCS811_STATUS_DATA_READY) != 0) /* check data ready */
540 {
541 res = a_ccs811_iic_read(handle, CCS811_REG_RAW_DATA, buf, 2); /* read data */
542 if (res != 0) /* check result */
543 {
544 handle->debug_print("ccs811: read failed.\n"); /* read failed */
545
546 return 1; /* return error */
547 }
548 *raw = (((uint16_t)buf[0]) << 8) | buf[1]; /* save raw */
549 *ua = buf[0] >> 2; /* set ua */
550 *adc_raw = (uint16_t)(((uint16_t)(buf[0] & 0x03)) << 8) | buf[1]; /* set adc raw */
551 *adc_v = (*adc_raw) * 1.65f / 1023.0f; /* set adc voltage */
552 }
553 else
554 {
555 handle->debug_print("ccs811: data is not ready.\n"); /* data is not ready */
556
557 return 5; /* return error */
558 }
559
560 return 0; /* success return 0 */
561}
562
575uint8_t ccs811_set_environment_data(ccs811_handle_t *handle, uint16_t humidity_raw, uint16_t temperature_raw)
576{
577 uint8_t res;
578 uint8_t buf[4];
579
580 if (handle == NULL) /* check handle */
581 {
582 return 2; /* return error */
583 }
584 if (handle->inited != 1) /* check handle initialization */
585 {
586 return 3; /* return error */
587 }
588
589 buf[0] = (humidity_raw >> 8) & 0xFF; /* set humidity raw high */
590 buf[1] = (humidity_raw >> 0) & 0xFF; /* set humidity raw low */
591 buf[2] = (temperature_raw >> 8) & 0xFF; /* set temperature raw high */
592 buf[3] = (temperature_raw >> 0) & 0xFF; /* set temperature raw low */
593 res = a_ccs811_iic_write(handle, CCS811_REG_ENV_DATA, buf, 4); /* set environment data */
594 if (res != 0) /* check result */
595 {
596 handle->debug_print("ccs811: set environment data failed.\n"); /* set environment data failed */
597
598 return 1; /* return error */
599 }
600
601 return 0; /* success return 0 */
602}
603
617uint8_t ccs811_set_eco2_threshold(ccs811_handle_t *handle, uint16_t low_medium_ppm, uint16_t medium_high_ppm, uint8_t hysteresis)
618{
619 uint8_t res;
620 uint8_t buf[5];
621
622 if (handle == NULL) /* check handle */
623 {
624 return 2; /* return error */
625 }
626 if (handle->inited != 1) /* check handle initialization */
627 {
628 return 3; /* return error */
629 }
630
631 buf[0] = (low_medium_ppm >> 8) & 0xFF; /* set low medium high */
632 buf[1] = (low_medium_ppm >> 0) & 0xFF; /* set low medium low */
633 buf[2] = (medium_high_ppm >> 8) & 0xFF; /* set medium high ppm high */
634 buf[3] = (medium_high_ppm >> 0) & 0xFF; /* set medium high ppm low */
635 buf[4] = hysteresis; /* set hysteresis */
636 res = a_ccs811_iic_write(handle, CCS811_REG_THRESHOLDS, buf, 5); /* set threshold data */
637 if (res != 0) /* check result */
638 {
639 handle->debug_print("ccs811: set threshold failed.\n"); /* set threshold failed */
640
641 return 1; /* return error */
642 }
643
644 return 0; /* success return 0 */
645}
646
658uint8_t ccs811_set_baseline(ccs811_handle_t *handle, uint16_t baseline)
659{
660 uint8_t res;
661 uint8_t buf[2];
662
663 if (handle == NULL) /* check handle */
664 {
665 return 2; /* return error */
666 }
667 if (handle->inited != 1) /* check handle initialization */
668 {
669 return 3; /* return error */
670 }
671
672 buf[0] = (baseline >> 8) & 0xFF; /* set baseline high */
673 buf[1] = (baseline >> 0) & 0xFF; /* set baseline low */
674 res = a_ccs811_iic_write(handle, CCS811_REG_BASELINE, buf, 2); /* set baseline data */
675 if (res != 0) /* check result */
676 {
677 handle->debug_print("ccs811: set baseline failed.\n"); /* set baseline failed */
678
679 return 1; /* return error */
680 }
681
682 return 0; /* success return 0 */
683}
684
696uint8_t ccs811_get_baseline(ccs811_handle_t *handle, uint16_t *baseline)
697{
698 uint8_t res;
699 uint8_t buf[2];
700
701 if (handle == NULL) /* check handle */
702 {
703 return 2; /* return error */
704 }
705 if (handle->inited != 1) /* check handle initialization */
706 {
707 return 3; /* return error */
708 }
709
710 res = a_ccs811_iic_read(handle, CCS811_REG_BASELINE, buf, 2); /* get baseline data */
711 if (res != 0) /* check result */
712 {
713 handle->debug_print("ccs811: get baseline failed.\n"); /* get baseline failed */
714
715 return 1; /* return error */
716 }
717 *baseline = ((uint16_t)buf[0]) << 8 | buf[1]; /* save baseline */
718
719 return 0; /* success return 0 */
720}
721
733uint8_t ccs811_get_hardware_id(ccs811_handle_t *handle, uint8_t *id)
734{
735 uint8_t res;
736
737 if (handle == NULL) /* check handle */
738 {
739 return 2; /* return error */
740 }
741 if (handle->inited != 1) /* check handle initialization */
742 {
743 return 3; /* return error */
744 }
745
746 res = a_ccs811_iic_read(handle, CCS811_REG_HW_ID, id, 1); /* get hardware id data */
747 if (res != 0) /* check result */
748 {
749 handle->debug_print("ccs811: get hardware id failed.\n"); /* get hardware id failed */
750
751 return 1; /* return error */
752 }
753
754 return 0; /* success return 0 */
755}
756
768uint8_t ccs811_get_hardware_version(ccs811_handle_t *handle, uint8_t *version)
769{
770 uint8_t res;
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 res = a_ccs811_iic_read(handle, CCS811_REG_HW_VERSION, version, 1); /* get hardware version data */
782 if (res != 0) /* check result */
783 {
784 handle->debug_print("ccs811: get hardware version failed.\n"); /* get hardware version failed */
785
786 return 1; /* return error */
787 }
788
789 return 0; /* success return 0 */
790}
791
805uint8_t ccs811_get_firmware_bootloader_version(ccs811_handle_t *handle, uint8_t *major, uint8_t *minor, uint8_t *trivial)
806{
807 uint8_t res;
808 uint8_t buf[2];
809
810 if (handle == NULL) /* check handle */
811 {
812 return 2; /* return error */
813 }
814 if (handle->inited != 1) /* check handle initialization */
815 {
816 return 3; /* return error */
817 }
818
819 res = a_ccs811_iic_read(handle, CCS811_REG_FW_BOOT_VERSION, buf, 2); /* get version data */
820 if (res != 0) /* check result */
821 {
822 handle->debug_print("ccs811: get version failed.\n"); /* get version failed */
823
824 return 1; /* return error */
825 }
826 *major = (buf[0] >> 4) & 0xFF; /* set major */
827 *minor = (buf[0] >> 0) & 0xFF; /* set minor */
828 *trivial = buf[1]; /* set trivial */
829
830 return 0; /* success return 0 */
831}
832
846uint8_t ccs811_get_firmware_application_version(ccs811_handle_t *handle, uint8_t *major, uint8_t *minor, uint8_t *trivial)
847{
848 uint8_t res;
849 uint8_t buf[2];
850
851 if (handle == NULL) /* check handle */
852 {
853 return 2; /* return error */
854 }
855 if (handle->inited != 1) /* check handle initialization */
856 {
857 return 3; /* return error */
858 }
859
860 res = a_ccs811_iic_read(handle, CCS811_REG_FW_APP_VERSION, buf, 2); /* get version data */
861 if (res != 0) /* check result */
862 {
863 handle->debug_print("ccs811: get version failed.\n"); /* get version failed */
864
865 return 1; /* return error */
866 }
867 *major = (buf[0] >> 4) & 0xFF; /* set major */
868 *minor = (buf[0] >> 0) & 0xFF; /* set minor */
869 *trivial = buf[1]; /* set trivial */
870
871 return 0; /* success return 0 */
872}
873
885uint8_t ccs811_get_error_id(ccs811_handle_t *handle, uint8_t *id)
886{
887 uint8_t res;
888
889 if (handle == NULL) /* check handle */
890 {
891 return 2; /* return error */
892 }
893 if (handle->inited != 1) /* check handle initialization */
894 {
895 return 3; /* return error */
896 }
897
898 res = a_ccs811_iic_read(handle, CCS811_REG_ERROR_ID, id, 1); /* get error id */
899 if (res != 0) /* check result */
900 {
901 handle->debug_print("ccs811: get error id failed.\n"); /* get error id failed */
902
903 return 1; /* return error */
904 }
905
906 return 0; /* success return 0 */
907}
908
920{
921 uint8_t res;
922 uint8_t cmd[] = {0x11, 0xE5, 0x72, 0x8A};
923
924 if (handle == NULL) /* check handle */
925 {
926 return 2; /* return error */
927 }
928 if (handle->inited != 1) /* check handle initialization */
929 {
930 return 3; /* return error */
931 }
932
933 res = a_ccs811_iic_write(handle, CCS811_REG_SW_RESET, cmd, 4); /* soft reset */
934 if (res != 0) /* check result */
935 {
936 handle->debug_print("ccs811: soft reset failed.\n"); /* soft reset failed */
937
938 return 1; /* return error */
939 }
940 handle->delay_ms(20); /* wait 20 ms */
941
942 return 0; /* success return 0 */
943}
944
957{
958 uint8_t res;
959 uint8_t status;
960 uint32_t i;
961 uint32_t timeout_ms = 5000;
962 uint8_t cmd[] = {0xE7, 0xA7, 0xE6, 0x09};
963
964 if (handle == NULL) /* check handle */
965 {
966 return 2; /* return error */
967 }
968 if (handle->inited != 1) /* check handle initialization */
969 {
970 return 3; /* return error */
971 }
972
973 res = a_ccs811_iic_write(handle, CCS811_REG_APP_ERASE, cmd, 4); /* app erase */
974 if (res != 0) /* check result */
975 {
976 handle->debug_print("ccs811: app erase failed.\n"); /* app erase failed */
977
978 return 1; /* return error */
979 }
980 handle->delay_ms(500); /* delay 500ms */
981 for (i = 0; i < timeout_ms; i++) /* check status */
982 {
983 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
984 if (res != 0) /* check result */
985 {
986 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
987
988 return 1; /* return error */
989 }
990 if ((status & (1 << 6)) != 0) /* check flag */
991 {
992 return 0; /* success return 0 */
993 }
994 handle->delay_ms(1); /* delay 1ms */
995 }
996 handle->debug_print("ccs811: erase timeout.\n"); /* erase timeout */
997
998 return 4; /* timeout */
999}
1000
1013{
1014 uint8_t res;
1015 uint8_t status;
1016 uint32_t i;
1017 uint32_t timeout_ms = 5000;
1018
1019 if (handle == NULL) /* check handle */
1020 {
1021 return 2; /* return error */
1022 }
1023 if (handle->inited != 1) /* check handle initialization */
1024 {
1025 return 3; /* return error */
1026 }
1027
1028 res = a_ccs811_iic_write(handle, CCS811_REG_APP_VERIFY, NULL, 0); /* app verify */
1029 if (res != 0) /* check result */
1030 {
1031 handle->debug_print("ccs811: app verify failed.\n"); /* app verify failed */
1032
1033 return 1; /* return error */
1034 }
1035 handle->delay_ms(100); /* delay 100ms */
1036 for (i = 0; i < timeout_ms; i++) /* check status */
1037 {
1038 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
1039 if (res != 0) /* check result */
1040 {
1041 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
1042
1043 return 1; /* return error */
1044 }
1045 if ((status & (1 << 5)) != 0) /* check flag */
1046 {
1047 return 0; /* success return 0 */
1048 }
1049 handle->delay_ms(1); /* delay 1ms */
1050 }
1051 handle->debug_print("ccs811: verify timeout.\n"); /* verify timeout */
1052
1053 return 4; /* timeout */
1054}
1055
1068{
1069 uint8_t res;
1070 uint8_t status;
1071 uint32_t i;
1072 uint32_t timeout_ms = 1000;
1073
1074 if (handle == NULL) /* check handle */
1075 {
1076 return 2; /* return error */
1077 }
1078 if (handle->inited != 1) /* check handle initialization */
1079 {
1080 return 3; /* return error */
1081 }
1082
1083 res = a_ccs811_iic_write(handle, CCS811_REG_APP_START, NULL, 0); /* app start */
1084 if (res != 0) /* check result */
1085 {
1086 handle->debug_print("ccs811: app start failed.\n"); /* app start failed */
1087
1088 return 1; /* return error */
1089 }
1090 handle->delay_ms(10); /* delay 10ms */
1091 for (i = 0; i < timeout_ms; i++) /* check status */
1092 {
1093 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
1094 if (res != 0) /* check result */
1095 {
1096 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
1097
1098 return 1; /* return error */
1099 }
1100 if ((status & (1 << 4)) != 0) /* check flag */
1101 {
1102 return 0; /* success return 0 */
1103 }
1104 handle->delay_ms(1); /* delay 1ms */
1105 }
1106 handle->debug_print("ccs811: start timeout.\n"); /* start timeout */
1107
1108 return 4; /* timeout */
1109}
1110
1124uint8_t ccs811_app_program(ccs811_handle_t *handle, uint8_t *rom, uint32_t len)
1125{
1126 uint8_t res;
1127 uint32_t i;
1128
1129 if (handle == NULL) /* check handle */
1130 {
1131 return 2; /* return error */
1132 }
1133 if (handle->inited != 1) /* check handle initialization */
1134 {
1135 return 3; /* return error */
1136 }
1137 if ((len % 8) != 0) /* check length */
1138 {
1139 handle->debug_print("ccs811: len is invalid.\n"); /* len is invalid */
1140
1141 return 4; /* return error */
1142 }
1143
1144 for (i = 0; i < len; i += 8)
1145 {
1146 res = a_ccs811_iic_write(handle, CCS811_REG_APP_DATA, rom + i, 8); /* app program */
1147 if (res != 0) /* check result */
1148 {
1149 handle->debug_print("ccs811: app program failed.\n"); /* app program failed */
1150
1151 return 1; /* return error */
1152 }
1153 handle->delay_ms(50); /* delay 50ms */
1154 }
1155
1156 return 0; /* success return 0 */
1157}
1158
1171{
1172 uint8_t res;
1173
1174 if (handle == NULL) /* check handle */
1175 {
1176 return 2; /* return error */
1177 }
1178 if (handle->inited != 1) /* check handle initialization */
1179 {
1180 return 3; /* return error */
1181 }
1182
1183 if (enable != 0) /* enable */
1184 {
1185 res = handle->gpio_wake_write(0); /* set low */
1186 if (res != 0) /* check result */
1187 {
1188 handle->debug_print("ccs811: wake failed.\n"); /* wake failed */
1189
1190 return 1; /* return error */
1191 }
1192 }
1193 else /* disable */
1194 {
1195 res = handle->gpio_wake_write(1); /* set high */
1196 if (res != 0) /* check result */
1197 {
1198 handle->debug_print("ccs811: wake failed.\n"); /* wake failed */
1199
1200 return 1; /* return error */
1201 }
1202 }
1203
1204 return 0; /* success return 0 */
1205}
1206
1218{
1219 uint8_t res;
1220
1221 if (handle == NULL) /* check handle */
1222 {
1223 return 2; /* return error */
1224 }
1225 if (handle->inited != 1) /* check handle initialization */
1226 {
1227 return 3; /* return error */
1228 }
1229
1230 res = handle->gpio_reset_write(0); /* set low */
1231 if (res != 0) /* check result */
1232 {
1233 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1234
1235 return 1; /* return error */
1236 }
1237 handle->delay_ms(20); /* delay 20ms */
1238 res = handle->gpio_reset_write(1); /* set high */
1239 if (res != 0) /* check result */
1240 {
1241 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1242
1243 return 1; /* return error */
1244 }
1245
1246 return 0; /* success return 0 */
1247}
1248
1260uint8_t ccs811_environment_humidity_convert_to_register(ccs811_handle_t *handle, float humidity, uint16_t *reg)
1261{
1262 if (handle == NULL) /* check handle */
1263 {
1264 return 2; /* return error */
1265 }
1266 if (handle->inited != 1) /* check handle initialization */
1267 {
1268 return 3; /* return error */
1269 }
1270
1271 *reg = (uint16_t)(humidity * 512.0f); /* convert real data to register data */
1272
1273 return 0; /* success return 0 */
1274}
1275
1287uint8_t ccs811_environment_humidity_convert_to_data(ccs811_handle_t *handle, uint16_t reg, float *humidity)
1288{
1289 if (handle == NULL) /* check handle */
1290 {
1291 return 2; /* return error */
1292 }
1293 if (handle->inited != 1) /* check handle initialization */
1294 {
1295 return 3; /* return error */
1296 }
1297
1298 *humidity = (float)(reg) / 512.0f; /* convert raw data to real data */
1299
1300 return 0; /* success return 0 */
1301}
1302
1314uint8_t ccs811_environment_temperature_convert_to_register(ccs811_handle_t *handle, float temperature, uint16_t *reg)
1315{
1316 if (handle == NULL) /* check handle */
1317 {
1318 return 2; /* return error */
1319 }
1320 if (handle->inited != 1) /* check handle initialization */
1321 {
1322 return 3; /* return error */
1323 }
1324
1325 *reg = (uint16_t)((temperature + 25.0f) * 512.0f); /* convert real data to register data */
1326
1327 return 0; /* success return 0 */
1328}
1329
1341uint8_t ccs811_environment_temperature_convert_to_data(ccs811_handle_t *handle, uint16_t reg, float *temperature)
1342{
1343 if (handle == NULL) /* check handle */
1344 {
1345 return 2; /* return error */
1346 }
1347 if (handle->inited != 1) /* check handle initialization */
1348 {
1349 return 3; /* return error */
1350 }
1351
1352 *temperature = (float)(reg) / 512.0f - 25.0f; /* convert raw data to real data */
1353
1354 return 0; /* success return 0 */
1355}
1356
1371{
1372 uint8_t res;
1373 uint8_t done;
1374 uint8_t status;
1375 uint32_t i;
1376 uint32_t j;
1377 uint32_t timeout_ms;
1378 uint32_t size;
1379 uint16_t len;
1380 uint8_t cmd_erase[] = {0xE7, 0xA7, 0xE6, 0x09};
1381
1382 if (handle == NULL) /* check handle */
1383 {
1384 return 2; /* return error */
1385 }
1386 if (handle->inited != 1) /* check handle initialization */
1387 {
1388 return 3; /* return error */
1389 }
1390
1391 res = handle->bin_read_init(path, &size); /* bin read init */
1392 if (res != 0) /* check result */
1393 {
1394 handle->debug_print("ccs811: bin init failed.\n"); /* bin init failed */
1395
1396 return 1; /* return error */
1397 }
1398 if ((size % 8) != 0) /* check size */
1399 {
1400 handle->debug_print("ccs811: size is invalid.\n"); /* size is invalid */
1401 (void)handle->bin_read_deinit(); /* bin read deinit */
1402
1403 return 4; /* return error */
1404 }
1405
1406 timeout_ms = 5000; /* set timeout */
1407 done= 0; /* init 0 */
1408 res = a_ccs811_iic_write(handle, CCS811_REG_APP_ERASE, cmd_erase, 4); /* app erase */
1409 if (res != 0) /* check result */
1410 {
1411 handle->debug_print("ccs811: app erase failed.\n"); /* app erase failed */
1412 (void)handle->bin_read_deinit(); /* bin read deinit */
1413
1414 return 1; /* return error */
1415 }
1416 handle->delay_ms(500); /* delay 500ms */
1417 for (i = 0; i < timeout_ms; i++) /* check status */
1418 {
1419 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
1420 if (res != 0) /* check result */
1421 {
1422 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
1423 (void)handle->bin_read_deinit(); /* bin read deinit */
1424
1425 return 1; /* return error */
1426 }
1427 if ((status & (1 << 6)) != 0) /* check flag */
1428 {
1429 done = 1; /* set done */
1430
1431 break; /* break */
1432 }
1433 handle->delay_ms(1); /* delay 1ms */
1434 }
1435 if (done == 0) /* check done */
1436 {
1437 handle->debug_print("ccs811: erase timeout.\n"); /* erase timeout */
1438 (void)handle->bin_read_deinit(); /* bin read deinit */
1439
1440 return 5; /* timeout */
1441 }
1442
1443 for (i = 0; i < size; i += 256) /* loop */
1444 {
1445 len = ((size - i) >= 256) ? 256 : (size - i); /* get length */
1446 res = handle->bin_read(i, len, handle->buf); /* bin read */
1447 if (res != 0) /* check result */
1448 {
1449 handle->debug_print("ccs811: bin read failed.\n"); /* bin read failed */
1450 (void)handle->bin_read_deinit(); /* bin read deinit */
1451
1452 return 1; /* return error */
1453 }
1454
1455 for (j = 0; j < len; j += 8) /* program */
1456 {
1457 res = a_ccs811_iic_write(handle, CCS811_REG_APP_DATA, handle->buf + j, 8); /* app program */
1458 if (res != 0) /* check result */
1459 {
1460 handle->debug_print("ccs811: app program failed.\n"); /* app program failed */
1461 (void)handle->bin_read_deinit(); /* bin read deinit */
1462
1463 return 1; /* return error */
1464 }
1465 handle->delay_ms(50); /* delay 50ms */
1466 }
1467 }
1468
1469 timeout_ms = 5000; /* set timeout */
1470 done= 0; /* init 0 */
1471 res = a_ccs811_iic_write(handle, CCS811_REG_APP_VERIFY, NULL, 0); /* app verify */
1472 if (res != 0) /* check result */
1473 {
1474 handle->debug_print("ccs811: app verify failed.\n"); /* app verify failed */
1475 (void)handle->bin_read_deinit(); /* bin read deinit */
1476
1477 return 1; /* return error */
1478 }
1479 handle->delay_ms(100); /* delay 100ms */
1480 for (i = 0; i < timeout_ms; i++) /* check status */
1481 {
1482 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
1483 if(res != 0) /* check result */
1484 {
1485 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
1486 (void)handle->bin_read_deinit(); /* bin read deinit */
1487
1488 return 1; /* return error */
1489 }
1490 if ((status & (1 << 5)) != 0) /* check flag */
1491 {
1492 done = 1; /* set done */
1493
1494 break; /* break */
1495 }
1496 handle->delay_ms(1); /* delay 1ms */
1497 }
1498 if (done == 0) /* check done */
1499 {
1500 handle->debug_print("ccs811: erase timeout.\n"); /* erase timeout */
1501 (void)handle->bin_read_deinit(); /* bin read deinit */
1502
1503 return 5; /* timeout */
1504 }
1505 res = handle->bin_read_deinit(); /* bin read deinit */
1506 if (res != 0) /* check result */
1507 {
1508 handle->debug_print("ccs811: bin deinit failed.\n"); /* bin deinit failed */
1509
1510 return 1; /* return error */
1511 }
1512
1513 return 0; /* success return 0 */
1514}
1515
1529{
1530 uint8_t res;
1531 uint8_t id;
1532 uint8_t status;
1533 uint16_t i;
1534 uint8_t cmd_reset[] = {0x11, 0xE5, 0x72, 0x8A};
1535
1536 if (handle == NULL) /* check handle */
1537 {
1538 return 2; /* return error */
1539 }
1540 if (handle->debug_print == NULL) /* check debug_print */
1541 {
1542 return 3; /* return error */
1543 }
1544 if (handle->iic_init == NULL) /* check iic_init */
1545 {
1546 handle->debug_print("ccs811: iic_init is null.\n"); /* iic_init is null */
1547
1548 return 3; /* return error */
1549 }
1550 if (handle->iic_deinit == NULL) /* check iic_deinit */
1551 {
1552 handle->debug_print("ccs811: iic_deinit is null.\n"); /* iic_deinit is null */
1553
1554 return 3; /* return error */
1555 }
1556 if (handle->iic_write == NULL) /* check iic_write */
1557 {
1558 handle->debug_print("ccs811: iic_write is null.\n"); /* iic_write is null */
1559
1560 return 3; /* return error */
1561 }
1562 if (handle->iic_read == NULL) /* check iic_read */
1563 {
1564 handle->debug_print("ccs811: iic_read is null.\n"); /* iic_read is null */
1565
1566 return 3; /* return error */
1567 }
1568 if (handle->gpio_wake_init == NULL) /* check gpio_wake_init */
1569 {
1570 handle->debug_print("ccs811: gpio_wake_init is null.\n"); /* gpio_wake_init is null */
1571
1572 return 3; /* return error */
1573 }
1574 if (handle->gpio_wake_deinit == NULL) /* check gpio_wake_deinit */
1575 {
1576 handle->debug_print("ccs811: gpio_wake_deinit is null.\n"); /* gpio_wake_deinit is null */
1577
1578 return 3; /* return error */
1579 }
1580 if (handle->gpio_wake_write == NULL) /* check gpio_wake_write */
1581 {
1582 handle->debug_print("ccs811: gpio_wake_write is null.\n"); /* gpio_wake_write is null */
1583
1584 return 3; /* return error */
1585 }
1586 if (handle->gpio_reset_init == NULL) /* check gpio_reset_init */
1587 {
1588 handle->debug_print("ccs811: gpio_reset_init is null.\n"); /* gpio_reset_init is null */
1589
1590 return 3; /* return error */
1591 }
1592 if (handle->gpio_reset_deinit == NULL) /* check gpio_reset_deinit */
1593 {
1594 handle->debug_print("ccs811: gpio_reset_deinit is null.\n"); /* gpio_reset_deinit is null */
1595
1596 return 3; /* return error */
1597 }
1598 if (handle->gpio_reset_write == NULL) /* check gpio_reset_write */
1599 {
1600 handle->debug_print("ccs811: gpio_reset_write is null.\n"); /* gpio_reset_write is null */
1601
1602 return 3; /* return error */
1603 }
1604 if (handle->bin_read_init == NULL) /* check bin_read_init */
1605 {
1606 handle->debug_print("ccs811: bin_read_init is null.\n"); /* bin_read_init is null */
1607
1608 return 3; /* return error */
1609 }
1610 if (handle->bin_read == NULL) /* check bin_read */
1611 {
1612 handle->debug_print("ccs811: bin_read is null.\n"); /* bin_read is null */
1613
1614 return 3; /* return error */
1615 }
1616 if (handle->bin_read_deinit == NULL) /* check bin_read_deinit */
1617 {
1618 handle->debug_print("ccs811: bin_read_deinit is null.\n"); /* bin_read_deinit is null */
1619
1620 return 3; /* return error */
1621 }
1622 if (handle->delay_ms == NULL) /* check delay_ms */
1623 {
1624 handle->debug_print("ccs811: delay_ms is null.\n"); /* delay_ms is null */
1625
1626 return 3; /* return error */
1627 }
1628
1629 if (handle->iic_init() != 0) /* iic init */
1630 {
1631 handle->debug_print("ccs811: iic init failed.\n"); /* iic init failed */
1632
1633 return 3; /* return error */
1634 }
1635 if (handle->gpio_wake_init() != 0) /* gpio wake init */
1636 {
1637 handle->debug_print("ccs811: gpio wake init failed.\n"); /* gpio wake init failed */
1638 (void)handle->iic_deinit(); /* iic deinit */
1639
1640 return 3; /* return error */
1641 }
1642 if (handle->gpio_reset_init() != 0) /* gpio reset init */
1643 {
1644 handle->debug_print("ccs811: gpio reset init failed.\n"); /* gpio reset init failed */
1645 (void)handle->iic_deinit(); /* iic deinit */
1646 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1647
1648 return 3; /* return error */
1649 }
1650 res = handle->gpio_wake_write(0); /* wake up */
1651 if (res != 0) /* check result */
1652 {
1653 handle->debug_print("ccs811: wake up failed.\n"); /* wake up failed */
1654 (void)handle->iic_deinit(); /* iic deinit */
1655 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1656 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1657
1658 return 3; /* return error */
1659 }
1660 handle->delay_ms(5); /* delay 5ms */
1661
1662 res = handle->gpio_reset_write(0); /* set low */
1663 if (res != 0) /* check result */
1664 {
1665 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1666 (void)handle->iic_deinit(); /* iic deinit */
1667 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1668 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1669
1670 return 3; /* return error */
1671 }
1672 handle->delay_ms(20); /* delay 20ms */
1673 res = handle->gpio_reset_write(1); /* set high */
1674 if (res != 0) /* check result */
1675 {
1676 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1677 (void)handle->iic_deinit(); /* iic deinit */
1678 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1679 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1680
1681 return 3; /* return error */
1682 }
1683 handle->delay_ms(5); /* delay 5ms */
1684
1685 res = a_ccs811_iic_read(handle, CCS811_REG_HW_ID, &id, 1); /* get hardware id data */
1686 if (res != 0) /* check result */
1687 {
1688 handle->debug_print("ccs811: get hardware id failed.\n"); /* get hardware id failed */
1689 (void)handle->iic_deinit(); /* iic deinit */
1690 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1691 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1692
1693 return 3; /* return error */
1694 }
1695 if (id != 0x81) /* check id */
1696 {
1697 handle->debug_print("ccs811: id is error.\n"); /* id is error */
1698 (void)handle->iic_deinit(); /* iic deinit */
1699 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1700 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1701
1702 return 4; /* return error */
1703 }
1704
1705 res = a_ccs811_iic_write(handle, CCS811_REG_SW_RESET, cmd_reset, 4); /* soft reset */
1706 if (res != 0) /* check result */
1707 {
1708 handle->debug_print("ccs811: soft reset failed.\n"); /* soft reset failed */
1709 (void)handle->iic_deinit(); /* iic deinit */
1710 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1711 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1712
1713 return 1; /* return error */
1714 }
1715 handle->delay_ms(20); /* wait 20 ms */
1716 for (i = 0; i < 1000; i++) /* check status */
1717 {
1718 res = a_ccs811_iic_read(handle, CCS811_REG_STATUS, &status, 1); /* get status */
1719 if (res != 0) /* check result */
1720 {
1721 handle->debug_print("ccs811: get status failed.\n"); /* get status failed */
1722 (void)handle->iic_deinit(); /* iic deinit */
1723 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1724 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1725
1726 return 1; /* return error */
1727 }
1728 if ((status & (1 << 4)) != 0) /* check flag */
1729 {
1730 break; /* break */
1731 }
1732 handle->delay_ms(1); /* delay 1ms */
1733 }
1734 if (i == 1000) /* check timeout */
1735 {
1736 handle->debug_print("ccs811: firmware loaded failed.\n"); /* firmware loaded failed */
1737 (void)handle->iic_deinit(); /* iic deinit */
1738 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1739 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1740
1741 return 5; /* return error */
1742 }
1743 handle->inited = 1; /* flag finish initialization */
1744
1745 return 0; /* success return 0 */
1746}
1747
1760{
1761 uint8_t res;
1762 uint8_t id;
1763 uint8_t cmd_reset[] = {0x11, 0xE5, 0x72, 0x8A};
1764
1765 if (handle == NULL) /* check handle */
1766 {
1767 return 2; /* return error */
1768 }
1769 if (handle->debug_print == NULL) /* check debug_print */
1770 {
1771 return 3; /* return error */
1772 }
1773 if (handle->iic_init == NULL) /* check iic_init */
1774 {
1775 handle->debug_print("ccs811: iic_init is null.\n"); /* iic_init is null */
1776
1777 return 3; /* return error */
1778 }
1779 if (handle->iic_deinit == NULL) /* check iic_deinit */
1780 {
1781 handle->debug_print("ccs811: iic_deinit is null.\n"); /* iic_deinit is null */
1782
1783 return 3; /* return error */
1784 }
1785 if (handle->iic_write == NULL) /* check iic_write */
1786 {
1787 handle->debug_print("ccs811: iic_write is null.\n"); /* iic_write is null */
1788
1789 return 3; /* return error */
1790 }
1791 if (handle->iic_read == NULL) /* check iic_read */
1792 {
1793 handle->debug_print("ccs811: iic_read is null.\n"); /* iic_read is null */
1794
1795 return 3; /* return error */
1796 }
1797 if (handle->gpio_wake_init == NULL) /* check gpio_wake_init */
1798 {
1799 handle->debug_print("ccs811: gpio_wake_init is null.\n"); /* gpio_wake_init is null */
1800
1801 return 3; /* return error */
1802 }
1803 if (handle->gpio_wake_deinit == NULL) /* check gpio_wake_deinit */
1804 {
1805 handle->debug_print("ccs811: gpio_wake_deinit is null.\n"); /* gpio_wake_deinit is null */
1806
1807 return 3; /* return error */
1808 }
1809 if (handle->gpio_wake_write == NULL) /* check gpio_wake_write */
1810 {
1811 handle->debug_print("ccs811: gpio_wake_write is null.\n"); /* gpio_wake_write is null */
1812
1813 return 3; /* return error */
1814 }
1815 if (handle->gpio_reset_init == NULL) /* check gpio_reset_init */
1816 {
1817 handle->debug_print("ccs811: gpio_reset_init is null.\n"); /* gpio_reset_init is null */
1818
1819 return 3; /* return error */
1820 }
1821 if (handle->gpio_reset_deinit == NULL) /* check gpio_reset_deinit */
1822 {
1823 handle->debug_print("ccs811: gpio_reset_deinit is null.\n"); /* gpio_reset_deinit is null */
1824
1825 return 3; /* return error */
1826 }
1827 if (handle->gpio_reset_write == NULL) /* check gpio_reset_write */
1828 {
1829 handle->debug_print("ccs811: gpio_reset_write is null.\n"); /* gpio_reset_write is null */
1830
1831 return 3; /* return error */
1832 }
1833 if (handle->bin_read_init == NULL) /* check bin_read_init */
1834 {
1835 handle->debug_print("ccs811: bin_read_init is null.\n"); /* bin_read_init is null */
1836
1837 return 3; /* return error */
1838 }
1839 if (handle->bin_read == NULL) /* check bin_read */
1840 {
1841 handle->debug_print("ccs811: bin_read is null.\n"); /* bin_read is null */
1842
1843 return 3; /* return error */
1844 }
1845 if (handle->bin_read_deinit == NULL) /* check bin_read_deinit */
1846 {
1847 handle->debug_print("ccs811: bin_read_deinit is null.\n"); /* bin_read_deinit is null */
1848
1849 return 3; /* return error */
1850 }
1851 if (handle->delay_ms == NULL) /* check delay_ms */
1852 {
1853 handle->debug_print("ccs811: delay_ms is null.\n"); /* delay_ms is null */
1854
1855 return 3; /* return error */
1856 }
1857
1858 if (handle->iic_init() != 0) /* iic init */
1859 {
1860 handle->debug_print("ccs811: iic init failed.\n"); /* iic init failed */
1861
1862 return 3; /* return error */
1863 }
1864 if (handle->gpio_wake_init() != 0) /* gpio wake init */
1865 {
1866 handle->debug_print("ccs811: gpio wake init failed.\n"); /* gpio wake init failed */
1867 (void)handle->iic_deinit(); /* iic deinit */
1868
1869 return 3; /* return error */
1870 }
1871 if (handle->gpio_reset_init() != 0) /* gpio reset init */
1872 {
1873 handle->debug_print("ccs811: gpio reset init failed.\n"); /* gpio reset init failed */
1874 (void)handle->iic_deinit(); /* iic deinit */
1875 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1876
1877 return 3; /* return error */
1878 }
1879 res = handle->gpio_wake_write(0); /* wake up */
1880 if (res != 0) /* check result */
1881 {
1882 handle->debug_print("ccs811: wake up failed.\n"); /* wake up failed */
1883 (void)handle->iic_deinit(); /* iic deinit */
1884 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1885 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1886
1887 return 3; /* return error */
1888 }
1889 handle->delay_ms(5); /* delay 5ms */
1890
1891 res = handle->gpio_reset_write(0); /* set low */
1892 if (res != 0) /* check result */
1893 {
1894 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1895 (void)handle->iic_deinit(); /* iic deinit */
1896 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1897 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1898
1899 return 3; /* return error */
1900 }
1901 handle->delay_ms(20); /* delay 20ms */
1902 res = handle->gpio_reset_write(1); /* set high */
1903 if (res != 0) /* check result */
1904 {
1905 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1906 (void)handle->iic_deinit(); /* iic deinit */
1907 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1908 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1909
1910 return 3; /* return error */
1911 }
1912 handle->delay_ms(5); /* delay 5ms */
1913
1914 res = a_ccs811_iic_read(handle, CCS811_REG_HW_ID, &id, 1); /* get hardware id data */
1915 if (res != 0) /* check result */
1916 {
1917 handle->debug_print("ccs811: get hardware id failed.\n"); /* get hardware id failed */
1918 (void)handle->iic_deinit(); /* iic deinit */
1919 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1920 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1921
1922 return 3; /* return error */
1923 }
1924 if (id != 0x81) /* check id */
1925 {
1926 handle->debug_print("ccs811: id is error.\n"); /* id is error */
1927 (void)handle->iic_deinit(); /* iic deinit */
1928 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1929 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1930
1931 return 4; /* return error */
1932 }
1933
1934 res = a_ccs811_iic_write(handle, CCS811_REG_SW_RESET, cmd_reset, 4); /* soft reset */
1935 if (res != 0) /* check result */
1936 {
1937 handle->debug_print("ccs811: soft reset failed.\n"); /* soft reset failed */
1938 (void)handle->iic_deinit(); /* iic deinit */
1939 (void)handle->gpio_wake_deinit(); /* gpio wake deinit */
1940 (void)handle->gpio_reset_deinit(); /* gpio reset deinit */
1941
1942 return 1; /* return error */
1943 }
1944 handle->delay_ms(20); /* wait 20 ms */
1945 handle->inited = 1; /* flag finish initialization */
1946
1947 return 0; /* success return 0 */
1948}
1949
1962{
1963 uint8_t res;
1964
1965 if (handle == NULL) /* check handle */
1966 {
1967 return 2; /* return error */
1968 }
1969 if (handle->inited != 1) /* check handle initialization */
1970 {
1971 return 3; /* return error */
1972 }
1973
1974 res = handle->gpio_reset_write(0); /* set low */
1975 if (res != 0) /* check result */
1976 {
1977 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1978
1979 return 4; /* return error */
1980 }
1981 handle->delay_ms(20); /* delay 20ms */
1982 res = handle->gpio_reset_write(1); /* set high */
1983 if (res != 0) /* check result */
1984 {
1985 handle->debug_print("ccs811: reset failed.\n"); /* reset failed */
1986
1987 return 4; /* return error */
1988 }
1989 if (handle->gpio_reset_deinit() != 0) /* gpio reset deinit */
1990 {
1991 handle->debug_print("ccs811: gpio reset deinit failed.\n"); /* gpio reset deinit failed */
1992
1993 return 1; /* return error */
1994 }
1995 if (handle->gpio_wake_deinit() != 0) /* gpio wake deinit */
1996 {
1997 handle->debug_print("ccs811: gpio wake deinit failed.\n"); /* gpio wake deinit failed */
1998
1999 return 1; /* return error */
2000 }
2001 if (handle->iic_deinit() != 0) /* iic deinit */
2002 {
2003 handle->debug_print("ccs811: iic close failed.\n"); /* iic close failed */
2004
2005 return 1; /* return error */
2006 }
2007 handle->inited = 0; /* flag close initialization */
2008
2009 return 0; /* success return 0 */
2010}
2011
2025uint8_t ccs811_set_reg(ccs811_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2026{
2027 if (handle == NULL) /* check handle */
2028 {
2029 return 2; /* return error */
2030 }
2031 if (handle->inited != 1) /* check handle initialization */
2032 {
2033 return 3; /* return error */
2034 }
2035
2036 return a_ccs811_iic_write(handle, reg, buf, len); /* write data */
2037}
2038
2052uint8_t ccs811_get_reg(ccs811_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2053{
2054 if (handle == NULL) /* check handle */
2055 {
2056 return 2; /* return error */
2057 }
2058 if (handle->inited != 1) /* check handle initialization */
2059 {
2060 return 3; /* return error */
2061 }
2062
2063 return a_ccs811_iic_read(handle, reg, buf, len); /* read data */
2064}
2065
2075{
2076 if (info == NULL) /* check handle */
2077 {
2078 return 2; /* return error */
2079 }
2080
2081 memset(info, 0, sizeof(ccs811_info_t)); /* initialize ccs811 info structure */
2082 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2083 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2084 strncpy(info->interface, "IIC", 8); /* copy interface name */
2085 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2086 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2087 info->max_current_ma = MAX_CURRENT; /* set maximum current */
2088 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2089 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2090 info->driver_version = DRIVER_VERSION; /* set driver version */
2091
2092 return 0; /* success return 0 */
2093}
#define CCS811_REG_ENV_DATA
#define MAX_CURRENT
#define CCS811_REG_APP_DATA
#define CCS811_REG_HW_VERSION
#define CCS811_REG_SW_RESET
#define CCS811_REG_ALG_RESULT_DATA
#define CCS811_REG_FW_APP_VERSION
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define CCS811_REG_ERROR_ID
#define CCS811_REG_HW_ID
#define CCS811_REG_THRESHOLDS
#define CCS811_REG_FW_BOOT_VERSION
#define CCS811_REG_APP_START
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CCS811_REG_MEAS_MODE
#define CCS811_REG_BASELINE
#define CCS811_REG_APP_VERIFY
#define CHIP_NAME
chip information definition
#define CCS811_REG_APP_ERASE
#define CCS811_REG_STATUS
chip register definition
#define DRIVER_VERSION
#define CCS811_REG_RAW_DATA
driver ccs811 header file
uint8_t ccs811_app_firmware_update(ccs811_handle_t *handle, char *path)
app firmware update
uint8_t ccs811_app_program(ccs811_handle_t *handle, uint8_t *rom, uint32_t len)
app program
uint8_t ccs811_app_start(ccs811_handle_t *handle)
app start
uint8_t ccs811_app_erase(ccs811_handle_t *handle)
app erase
uint8_t ccs811_app_verify(ccs811_handle_t *handle)
app verify
uint8_t ccs811_app_firmware_init(ccs811_handle_t *handle)
app firmware initialize
struct ccs811_info_s ccs811_info_t
ccs811 information structure definition
uint8_t ccs811_set_addr_pin(ccs811_handle_t *handle, ccs811_address_t addr_pin)
set address pin
struct ccs811_handle_s ccs811_handle_t
ccs811 handle structure definition
uint8_t ccs811_get_addr_pin(ccs811_handle_t *handle, ccs811_address_t *addr_pin)
get address pin
uint8_t ccs811_set_eco2_threshold(ccs811_handle_t *handle, uint16_t low_medium_ppm, uint16_t medium_high_ppm, uint8_t hysteresis)
set eco2 threshold
ccs811_address_t
ccs811 address enumeration definition
uint8_t ccs811_get_interrupt_threshold(ccs811_handle_t *handle, ccs811_bool_t *enable)
get interrupt threshold status
uint8_t ccs811_hard_reset(ccs811_handle_t *handle)
hard reset
uint8_t ccs811_environment_temperature_convert_to_register(ccs811_handle_t *handle, float temperature, uint16_t *reg)
convert real data format to register data format
uint8_t ccs811_get_hardware_version(ccs811_handle_t *handle, uint8_t *version)
get hardware version
uint8_t ccs811_environment_humidity_convert_to_data(ccs811_handle_t *handle, uint16_t reg, float *humidity)
convert register data to real data format
uint8_t ccs811_set_baseline(ccs811_handle_t *handle, uint16_t baseline)
set baseline
uint8_t ccs811_environment_humidity_convert_to_register(ccs811_handle_t *handle, float humidity, uint16_t *reg)
convert real data format to register data format
uint8_t ccs811_get_error_id(ccs811_handle_t *handle, uint8_t *id)
get error id
uint8_t ccs811_set_mode(ccs811_handle_t *handle, ccs811_mode_t mode)
set mode
uint8_t ccs811_get_firmware_bootloader_version(ccs811_handle_t *handle, uint8_t *major, uint8_t *minor, uint8_t *trivial)
get firmware bootloader version
uint8_t ccs811_soft_reset(ccs811_handle_t *handle)
soft reset
ccs811_bool_t
ccs811 bool enumeration definition
uint8_t ccs811_get_interrupt_data_ready(ccs811_handle_t *handle, ccs811_bool_t *enable)
get interrupt data ready status
uint8_t ccs811_get_baseline(ccs811_handle_t *handle, uint16_t *baseline)
get baseline
uint8_t ccs811_set_environment_data(ccs811_handle_t *handle, uint16_t humidity_raw, uint16_t temperature_raw)
set environment data
uint8_t ccs811_set_interrupt_data_ready(ccs811_handle_t *handle, ccs811_bool_t enable)
enable or disable interrupt data ready
uint8_t ccs811_read(ccs811_handle_t *handle, uint16_t *eco2_ppm, uint16_t *tvoc_ppb, uint16_t *raw)
read data
uint8_t ccs811_get_firmware_application_version(ccs811_handle_t *handle, uint8_t *major, uint8_t *minor, uint8_t *trivial)
get firmware application version
uint8_t ccs811_environment_temperature_convert_to_data(ccs811_handle_t *handle, uint16_t reg, float *temperature)
convert register data to real data format
uint8_t ccs811_set_interrupt_threshold(ccs811_handle_t *handle, ccs811_bool_t enable)
enable or disable interrupt threshold
uint8_t ccs811_info(ccs811_info_t *info)
get chip information
uint8_t ccs811_get_hardware_id(ccs811_handle_t *handle, uint8_t *id)
get hardware id
uint8_t ccs811_init(ccs811_handle_t *handle)
initialize the chip
uint8_t ccs811_deinit(ccs811_handle_t *handle)
close the chip
uint8_t ccs811_get_mode(ccs811_handle_t *handle, ccs811_mode_t *mode)
get mode
ccs811_mode_t
ccs811 mode enumeration definition
uint8_t ccs811_get_status(ccs811_handle_t *handle, uint8_t *status)
get status
uint8_t ccs811_get_raw_data(ccs811_handle_t *handle, uint16_t *raw, uint8_t *ua, uint16_t *adc_raw, float *adc_v)
get raw data
uint8_t ccs811_wake_up(ccs811_handle_t *handle, ccs811_bool_t enable)
wake up
@ CCS811_STATUS_ERROR
@ CCS811_STATUS_DATA_READY
uint8_t ccs811_set_reg(ccs811_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t ccs811_get_reg(ccs811_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t(* gpio_reset_deinit)(void)
uint8_t buf[256]
uint8_t(* gpio_wake_init)(void)
uint8_t(* gpio_wake_write)(uint8_t level)
uint8_t(* gpio_wake_deinit)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* bin_read_init)(char *name, uint32_t *size)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* bin_read)(uint32_t addr, uint16_t size, uint8_t *buffer)
uint8_t(* gpio_reset_write)(uint8_t level)
uint8_t(* bin_read_deinit)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* gpio_reset_init)(void)
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]