-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgp30.c
929 lines (813 loc) · 46.4 KB
/
sgp30.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#include "sgp30.h"
#include "mcc_generated_files/i2c1_driver.h"
#include "mcc_generated_files/drivers/i2c_simple_master.h"
/**
* @brief read bytes
* @param[in] *handle points to an sgp30 handle structure
* @param[in] addr is the iic device address
* @param[in] reg is the iic register address
* @param[out] *data points to a data buffer
* @param[in] len is the data length
* @param[in] delay_ms is the delay time in ms
* @return status code
* - 0 success
* - 1 read failed
* @note none
*/
static uint8_t a_sgp30_iic_read(sgp30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len, uint16_t delay_ms)
{
uint8_t buf[2];
memset(buf, 0, sizeof(uint8_t) * 2); /* clear the buffer */
buf[0] = (uint8_t)((reg >> 8) & 0xFF); /* set reg MSB */
buf[1] = (uint8_t)(reg & 0xFF); /* set reg LSB */
if (handle->iic_write_cmd(addr, (uint8_t *)buf, 2) != 0) /* write command */
{
return 1; /* return error */
}
handle->delay_ms(delay_ms); /* delay ms */
if (handle->iic_read_cmd(addr, data, len) != 0) /* read data */
{
return 1; /* write command */
}
else
{
return 0; /* success return 0 */
}
}
/**
* @brief write bytes
* @param[in] *handle points to an sgp30 handle structure
* @param[in] addr is the iic device address
* @param[in] reg is the iic register address
* @param[in] *data points to a data buffer
* @param[in] len is the data length
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
static uint8_t a_sgp30_iic_write(sgp30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len)
{
uint8_t buf[16];
uint16_t i;
if ((len + 2) > 16) /* check length */
{
return 1; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 16); /* clear the buffer */
buf[0] = (uint8_t)((reg >> 8) & 0xFF); /* set MSB of reg */
buf[1] = (uint8_t)(reg & 0xFF); /* set LSB of reg */
for (i = 0; i < len; i++)
{
buf[2 + i] = data[i]; /* copy write data */
}
if (handle->iic_write_cmd(addr, (uint8_t *)buf, len + 2) != 0) /* write iic command */
{
return 1; /* write command */
}
else
{
return 0; /* success return 0 */
}
}
/**
* @brief generate the crc
* @param[in] *data points to a data buffer
* @param[in] count is the data length
* @return crc
* @note none
*/
static uint8_t a_sgp30_generate_crc(uint8_t* data, uint8_t count)
{
uint8_t current_byte;
uint8_t crc = SGP30_CRC8_INIT;
uint8_t crc_bit;
for (current_byte = 0; current_byte < count; ++current_byte) /* calculate crc */
{
crc ^= (data[current_byte]); /* xor data */
for (crc_bit = 8; crc_bit > 0; --crc_bit) /* 8 bit */
{
if ((crc & 0x80) != 0) /* if 7th bit is 1 */
{
crc = (crc << 1) ^ SGP30_CRC8_POLYNOMIAL; /* xor */
}
else
{
crc = crc << 1; /* left shift 1 */
}
}
}
return crc; /* return crc */
}
/**
* @brief set the chip tvoc baseline
* @param[in] *handle points to an sgp30 handle structure
* @param[in] tvoc_baseline is the current tvoc baseline
* @return status code
* - 0 success
* - 1 set tvoc baseline failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_set_tvoc_baseline(sgp30_handle_t *handle, uint16_t tvoc_baseline)
{
uint8_t res;
uint8_t buf[3];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
buf[0] = (tvoc_baseline >> 8) & 0xFF; /* get high part */
buf[1] = (tvoc_baseline >> 0) & 0xFF; /* get low part */
buf[2] = a_sgp30_generate_crc((uint8_t *)buf, 2); /* generate crc */
res = a_sgp30_iic_write(handle, SGP30_ADDRESS, SGP30_COMMAND_SET_TVOC_BASELINE, (uint8_t *)buf, 3); /* write set tvoc baseline command */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: write tvoc baseline failed.\n"); /* write tvoc baseline failed */
return 1; /* return error */
}
handle->delay_ms(10); /* wait 10 ms */
return 0; /* success return 0 */
}
/**
* @brief get the chip tvoc inceptive baseline
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *tvoc_baseline points to a current tvoc baseline buffer
* @return status code
* - 0 success
* - 1 get tvoc inceptive baseline failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_tvoc_inceptive_baseline(sgp30_handle_t *handle, uint16_t *tvoc_baseline)
{
uint8_t res;
uint8_t buf[3];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_GET_TVOC_INCEPTIVE_BASELINE, (uint8_t *)buf, 3, 10); /* read tvoc inceptive baseline */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read tvoc baseline failed.\n"); /* read tvoc baseline failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check crc */
{
handle->debug_print("sgp30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
*tvoc_baseline = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* get data */
return 0; /* success return 0 */
}
/**
* @brief initialize the chip iaq
* @param[in] *handle points to an sgp30 handle structure
* @return status code
* - 0 success
* - 1 iaq init failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_iaq_init(sgp30_handle_t *handle)
{
uint8_t res;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
res = a_sgp30_iic_write(handle, SGP30_ADDRESS, SGP30_COMMAND_IAQ_INIT, NULL, 0); /* write iaq init command */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: write iaq init failed.\n"); /* write iaq init failed */
return 1; /* return error */
}
handle->delay_ms(10); /* wait 10 ms */
return 0; /* success return 0 */
}
/**
* @brief soft reset the chip
* @param[in] *handle points to an sgp30 handle structure
* @return status code
* - 0 success
* - 1 soft reset failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_soft_reset(sgp30_handle_t *handle)
{
uint8_t res;
uint8_t reg;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
reg = 0x06; /* soft reset command */
res = handle->iic_write_cmd(0x00, (uint8_t *)®, 1); /* write reset config */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: write soft reset failed.\n"); /* write soft reset failed */
return 1; /* return error */
}
return 0; /* success return 0 */
}
/**
* @brief get the chip serial id
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *id points to an id buffer
* @return status code
* - 0 success
* - 1 get serial id failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_serial_id(sgp30_handle_t *handle, uint16_t id[3])
{
uint8_t res;
uint8_t buf[9];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 9); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_GET_SERIAL_ID, (uint8_t *)buf, 9, 10); /* read config */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read serial id failed.\n"); /* read serial id failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)&buf[0], 2)) /* check 1st crc */
{
handle->debug_print("sgp30: crc 1 check failed.\n"); /* crc 1 check failed */
return 1; /* return error */
}
if (buf[5] != a_sgp30_generate_crc((uint8_t *)&buf[3], 2)) /* check 2nd crc */
{
handle->debug_print("sgp30: crc 2 check failed.\n"); /* crc 2 check failed */
return 1; /* return error */
}
if (buf[8] != a_sgp30_generate_crc((uint8_t *)&buf[6], 2)) /* check 3rd crc */
{
handle->debug_print("sgp30: crc 3 check failed.\n"); /* crc 3 check failed */
return 1; /* return error */
}
id[0] = (uint16_t)((((uint16_t)buf[0]) << 8) | buf[1]); /* set id 0 */
id[1] = (uint16_t)((((uint16_t)buf[3]) << 8) | buf[4]); /* set id 1 */
id[2] = (uint16_t)((((uint16_t)buf[6]) << 8) | buf[7]); /* set id 2 */
return 0; /* success return 0 */
}
/**
* @brief get the iaq measure result
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *co2_eq_ppm points to a co2 buffer
* @param[out] *tvoc_ppb points to a tvoc buffer
* @return status code
* - 0 success
* - 1 measure iaq failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_measure_iaq(sgp30_handle_t *handle, uint16_t *co2_eq_ppm, uint16_t *tvoc_ppb)
{
uint8_t res;
uint8_t buf[6];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 6); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_MEASURE_IAQ, (uint8_t *)buf, 6, 12); /* read measure iaq */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read measure iaq failed.\n"); /* read measure iaq failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* calculate crc */
{
handle->debug_print("sgp30: co2 eq crc check error.\n"); /* co2 eq crc check error */
return 1; /* return error */
}
if (buf[5] != a_sgp30_generate_crc((uint8_t *)&buf[3], 2)) /* calculate crc */
{
handle->debug_print("sgp30: tvoc crc check error.\n"); /* tvoc crc check error */
return 1; /* return error */
}
*co2_eq_ppm = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* get co2 eq ppm */
*tvoc_ppb = (uint16_t)(((uint16_t)buf[3]) << 8 | buf[4]); /* get tvoc ppb */
return 0; /* success return 0 */
}
/**
* @brief get the chip iaq baseline
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *tvoc points to a tvoc buffer
* @param[out] *co2_eq points to a co2 buffer
* @return status code
* - 0 success
* - 1 get iaq baseline failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_iaq_baseline(sgp30_handle_t *handle, uint16_t *tvoc, uint16_t *co2_eq)
{
uint8_t res;
uint8_t buf[6];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 6); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_GET_IAQ_BASELINE, (uint8_t *)buf, 6, 10); /* read iaq baseline */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read measure iaq failed.\n"); /* read measure iaq failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check co2 crc */
{
handle->debug_print("sgp30: co2 eq crc check error.\n"); /* co2 eq crc check error */
return 1; /* return error */
}
if (buf[5] != a_sgp30_generate_crc((uint8_t *)&buf[3], 2)) /* check tvoc crc */
{
handle->debug_print("sgp30: tvoc crc check error.\n"); /* tvoc crc check error */
return 1; /* return error */
}
*co2_eq = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* get co2 eq */
*tvoc = (uint16_t)(((uint16_t)buf[3]) << 8 | buf[4]); /* get tvoc */
return 0; /* success return 0 */
}
/**
* @brief set the chip iaq baseline
* @param[in] *handle points to an sgp30 handle structure
* @param[in] tvoc is the current tvoc
* @param[in] co2_eq is the current co2 eq
* @return status code
* - 0 success
* - 1 set iaq baseline failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_set_iaq_baseline(sgp30_handle_t *handle, uint16_t tvoc, uint16_t co2_eq)
{
uint8_t res;
uint8_t buf[6];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 6); /* clear the buffer */
buf[0] = (tvoc >> 8) & 0xFF; /* set tvoc high part */
buf[1] = tvoc & 0xFF; /* set tvoc low part */
buf[2] = a_sgp30_generate_crc((uint8_t *)buf, 2); /* generate tvoc crc */
buf[3] = (co2_eq >> 8) & 0xFF; /* set co2 eq high part */
buf[4] = co2_eq & 0xFF; /* set co2 eq low part */
buf[5] = a_sgp30_generate_crc((uint8_t *)&buf[3], 2); /* generate co2_eq crc */
res = a_sgp30_iic_write(handle, SGP30_ADDRESS, SGP30_COMMAND_SET_IAQ_BASELINE, (uint8_t *)buf, 6); /* write iaq baseline command */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: write iaq baseline failed.\n"); /* write iaq baseline failed */
return 1; /* return error */
}
handle->delay_ms(10); /* delay 10 ms */
return 0; /* success return 0 */
}
/**
* @brief set the chip absolute_humidity
* @param[in] *handle points to an sgp30 handle structure
* @param[in] humidity is the current absolute_humidity
* @return status code
* - 0 success
* - 1 set absolute_humidity failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_set_absolute_humidity(sgp30_handle_t *handle, uint16_t humidity)
{
uint8_t res;
uint8_t buf[3];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
buf[0] = (humidity >> 8) & 0xFF; /* set humidity high part */
buf[1] = (humidity >> 0) & 0xFF; /* set humidity low part */
buf[2] = a_sgp30_generate_crc((uint8_t *)buf, 2); /* generate crc */
res = a_sgp30_iic_write(handle, SGP30_ADDRESS, SGP30_COMMAND_SET_ABSOLUTE_HUMIDITY, (uint8_t *)buf, 3); /* write set absolute humidity command */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: write absolute humidity failed.\n"); /* write absolute humidity failed */
return 1; /* return error */
}
handle->delay_ms(10); /* delay 10 ms */
return 0; /* success return 0 */
}
/**
* @brief convert the absolute humidity to the register data
* @param[in] *handle points to an sgp30 handle structure
* @param[in] temp is the current temperature
* @param[in] rh is the current relative humidity
* @param[out] *reg points to a converted register data
* @return status code
* - 0 success
* - 1 absolute humidity convert to register failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_absolute_humidity_convert_to_register(sgp30_handle_t *handle, float temp, float rh, uint16_t *reg)
{
float absolute_humidity;
float intpart, fractpart;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
absolute_humidity = (rh / 100.0f * 6.112f * powf(17.62f * temp, 243.12f + temp)) / (273.15f + temp) * 216.7f; /* get absolute humidity */
fractpart = modff(absolute_humidity, (float *)&intpart); /* get intpart and fractpart */
*reg = (uint16_t)(intpart) << 8 | (uint8_t)(fractpart * 256); /* convert to register */
return 0; /* success return 0 */
}
/**
* @brief run the chip measure test
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *result points to a test result buffer
* @return status code
* - 0 success
* - 1 measure test failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_measure_test(sgp30_handle_t *handle, uint16_t *result)
{
uint8_t res;
uint8_t buf[3];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_MEASURE_TEST, (uint8_t *)buf, 3, 220); /* read measure test */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read measure test failed.\n"); /* read measure test failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check crc */
{
handle->debug_print("sgp30: measure test check error.\n"); /* measure test check error */
return 1; /* return error */
}
*result = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* combine data */
return 0; /* success return 0 */
}
/**
* @brief get the chip feature
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *product_type points to a product type buffer
* @param[out] *product_version points to a product version buffer
* @return status code
* - 0 success
* - 1 get feature set failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_feature_set(sgp30_handle_t *handle, uint8_t *product_type, uint8_t *product_version)
{
uint8_t res;
uint8_t buf[3];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_GET_FEATURE_SET, (uint8_t *)buf, 3, 10); /* read get feature set */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: get feature set failed.\n"); /* get feature set failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check crc */
{
handle->debug_print("sgp30: feature set check error.\n"); /* crc check error */
return 1; /* return error */
}
*product_type = (buf[0] >> 4) & 0xF; /* get product type */
*product_version = buf[1]; /* get product version */
return 0; /* success return 0 */
}
/**
* @brief get the iaq measure raw result
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *tvoc points to a tvoc buffer
* @param[out] *co2_eq points to a co2 buffer
* @return status code
* - 0 success
* - 1 get measure raw failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_measure_raw(sgp30_handle_t *handle, uint16_t *tvoc, uint16_t *co2_eq)
{
uint8_t res;
uint8_t buf[6];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 6); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_MEASURE_RAW, (uint8_t *)buf, 6, 25); /* read measure raw */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read measure raw failed.\n"); /* read measure failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check 1st crc */
{
handle->debug_print("sgp30: co2 eq crc check error.\n"); /* co2 eq crc check error */
return 1; /* return error */
}
if (buf[5] != a_sgp30_generate_crc((uint8_t *)&buf[3], 2)) /* check 2nd crc */
{
handle->debug_print("sgp30: tvoc crc check error.\n"); /* tvoc crc check error */
return 1; /* return error */
}
*tvoc = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* get tvoc data */
*co2_eq = (uint16_t)(((uint16_t)buf[3]) << 8 | buf[4]); /* get co2 eq data */
return 0; /* success return 0 */
}
/**
* @brief initialize the chip
* @param[in] *handle points to an sgp30 handle structure
* @return status code
* - 0 success
* - 1 iic initialization failed
* - 2 handle is NULL
* - 3 linked functions is NULL
* @note none
*/
uint8_t sgp30_init(sgp30_handle_t *handle)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->debug_print == NULL) /* check debug_print */
{
return 3; /* return error */
}
if (handle->iic_init == NULL) /* check iic_init */
{
handle->debug_print("sgp30: iic_init is null.\n"); /* iic_init is null */
return 3; /* return error */
}
if (handle->iic_deinit == NULL) /* check iic_deinit */
{
handle->debug_print("sgp30: iic_deinit is null.\n"); /* iic_deinit is null */
return 3; /* return error */
}
if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
{
handle->debug_print("sgp30: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
return 3; /* return error */
}
if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
{
handle->debug_print("sgp30: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
return 3; /* return error */
}
if (handle->delay_ms == NULL) /* check delay_ms */
{
handle->debug_print("sgp30: delay_ms is null.\n"); /* delay_ms is null */
return 3; /* return error */
}
if (handle->iic_init() != 0) /* iic init */
{
handle->debug_print("sgp30: iic init failed.\n"); /* iic init failed */
return 3; /* return error */
}
handle->inited = 1; /* flag finish initialization */
return 0; /* success return 0 */
}
/**
* @brief close the chip
* @param[in] *handle points to an sgp30 handle structure
* @return status code
* - 0 success
* - 1 iic deinit failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 soft reset failed
* @note none
*/
uint8_t sgp30_deinit(sgp30_handle_t *handle)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
if (sgp30_soft_reset(handle) != 0) /* reset chip */
{
handle->debug_print("sgp30: soft reset failed.\n"); /* soft reset failed */
return 4; /* return error */
}
if (handle->iic_deinit() != 0) /* iic deinit */
{
handle->debug_print("sgp30: iic close failed.\n"); /* iic close failed */
return 3; /* return error */
}
handle->inited = 0; /* flag close initialization */
return 0; /* success return 0 */
}
/**
* @brief read the iaq measure result
* @param[in] *handle points to an sgp30 handle structure
* @param[out] *co2_eq_ppm points to a co2 buffer
* @param[out] *tvoc_ppb points to a tvoc buffer
* @return status code
* - 0 success
* - 1 read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_read(sgp30_handle_t *handle, uint16_t *co2_eq_ppm, uint16_t *tvoc_ppb)
{
uint8_t res;
uint8_t buf[6];
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
memset(buf, 0, sizeof(uint8_t) * 6); /* clear the buffer */
res = a_sgp30_iic_read(handle, SGP30_ADDRESS, SGP30_COMMAND_MEASURE_IAQ, (uint8_t *)buf, 6, 12); /* read measure iaq */
if (res != 0) /* check result */
{
handle->debug_print("sgp30: read measure iaq failed.\n"); /* read measure iaq failed */
return 1; /* return error */
}
if (buf[2] != a_sgp30_generate_crc((uint8_t *)buf, 2)) /* check 1st crc */
{
handle->debug_print("sgp30: co2 eq crc check error.\n"); /* co2 eq crc check error */
return 1; /* return error */
}
if (buf[5] != a_sgp30_generate_crc((uint8_t *)&buf[3], 2)) /* check 2nd crc */
{
handle->debug_print("sgp30: tvoc crc check error.\n"); /* tvoc crc check error */
return 1; /* return error */
}
*co2_eq_ppm = (uint16_t)(((uint16_t)buf[0]) << 8 | buf[1]); /* get co2 eq ppm data */
*tvoc_ppb = (uint16_t)(((uint16_t)buf[3]) << 8 | buf[4]); /* get tvoc ppb data */
return 0; /* success return 0 */
}
/**
* @brief set the chip register
* @param[in] *handle points to an sgp30 handle structure
* @param[in] reg is the iic register address
* @param[in] *buf points to a data buffer
* @param[in] len is the data buffer length
* @return status code
* - 0 success
* - 1 write failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_set_reg(sgp30_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
return a_sgp30_iic_write(handle, SGP30_ADDRESS, reg, buf, len); /* write data */
}
/**
* @brief get the chip register
* @param[in] *handle points to an sgp30 handle structure
* @param[in] reg is the iic register address
* @param[out] *buf points to a data buffer
* @param[in] len is the data buffer length
* @return status code
* - 0 success
* - 1 read failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sgp30_get_reg(sgp30_handle_t *handle, uint16_t reg, uint8_t *buf, uint16_t len)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
return a_sgp30_iic_read(handle, SGP30_ADDRESS, reg, buf, len, 20); /* read data */
}
/**
* @brief get chip information
* @param[out] *info points to an sgp30 info structure
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t sgp30_info(sgp30_info_t *info)
{
if (info == NULL) /* check handle */
{
return 2; /* return error */
}
memset(info, 0, sizeof(sgp30_info_t)); /* initialize sgp30 info structure */
strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
strncpy(info->interface, "IIC", 8); /* copy interface name */
info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
info->max_current_ma = MAX_CURRENT; /* set maximum current */
info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
info->driver_version = DRIVER_VERSION; /* set driver version */
return 0; /* success return 0 */
}