-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsps30.c
2262 lines (2007 loc) · 141 KB
/
sps30.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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sps30.h"
#include "mcc_generated_files/drivers/i2c_simple_master.h"
#include "mcc_generated_files/uart2.h"
#include "mcc_generated_files/drivers/uart.h"
#define CHIP_NAME "Sensirion SPS30" /**< chip name */
#define MANUFACTURER_NAME "Sensirion" /**< manufacturer name */
#define SUPPLY_VOLTAGE_MIN 4.50f /**< chip min supply voltage */
#define SUPPLY_VOLTAGE_MAX 5.50f /**< chip max supply voltage */
#define MAX_CURRENT 80.0f /**< chip max current */
#define TEMPERATURE_MIN -10.0f /**< chip min operating temperature */
#define TEMPERATURE_MAX 60.0f /**< chip max operating temperature */
#define DRIVER_VERSION 1000 /**< driver version */
/**
* @brief chip address definition
*/
#define SPS30_ADDRESS (0x69 << 1) /**< chip iic address */
/**
* @brief chip command definition
*/
#define SPS30_IIC_COMMAND_START_MEASUREMENT 0x0010U /**< start measurement command */
#define SPS30_IIC_COMMAND_STOP_MEASUREMENT 0x0104U /**< stop measurement command */
#define SPS30_IIC_COMMAND_READ_DATA_READY_FLAG 0x0202U /**< read data ready flag command */
#define SPS30_IIC_COMMAND_READ_MEASURED_VALUES 0x0300U /**< read measured values command */
#define SPS30_IIC_COMMAND_SLEEP 0x1001U /**< sleep command */
#define SPS30_IIC_COMMAND_WAKE_UP 0x1103U /**< wake up command */
#define SPS30_IIC_COMMAND_START_FAN_CLEANING 0x5607U /**< start fan cleaning command */
#define SPS30_IIC_COMMAND_READ_WRITE_AUTO_CLEANING_INTERVAL 0x8004U /**< read/write auto cleaning interval command */
#define SPS30_IIC_COMMAND_READ_PRODUCT_TYPE 0xD002U /**< read product type command */
#define SPS30_IIC_COMMAND_READ_SERIAL_NUMBER 0xD033U /**< read serial number command */
#define SPS30_IIC_COMMAND_READ_VERSION 0xD100U /**< read version command */
#define SPS30_IIC_COMMAND_READ_DEVICE_STATUS_REG 0xD206U /**< read device status register command */
#define SPS30_IIC_COMMAND_CLEAR_DEVICE_STATUS_REG 0xD210U /**< clear device status register command */
#define SPS30_IIC_COMMAND_RESET 0xD304U /**< reset command */
#define SPS30_UART_COMMAND_START_MEASUREMENT 0x00 /**< start measurement command */
#define SPS30_UART_COMMAND_STOP_MEASUREMENT 0x01 /**< stop measurement command */
#define SPS30_UART_COMMAND_READ_MEASURED_VALUES 0x03 /**< read measured values command */
#define SPS30_UART_COMMAND_SLEEP 0x10 /**< sleep command */
#define SPS30_UART_COMMAND_WAKE_UP 0x11 /**< wake up command */
#define SPS30_UART_COMMAND_START_FAN_CLEANING 0x56 /**< start fan cleaning command */
#define SPS30_UART_COMMAND_READ_WRITE_AUTO_CLEANING_INTERVAL 0x80 /**< read/write auto cleaning interval command */
#define SPS30_UART_COMMAND_READ_PRODUCT_TYPE 0xD0 /**< read product type command */
#define SPS30_UART_COMMAND_READ_VERSION 0xD1 /**< read version command */
#define SPS30_UART_COMMAND_READ_DEVICE_STATUS_REG 0xD2 /**< read device status register command */
#define SPS30_UART_COMMAND_RESET 0xD3 /**< reset command */
/**
* @brief generate the crc
* @param[in] *handle points to an sps30 handle structure
* @param[in] *data points to a data buffer
* @param[in] count is the data length
* @return crc
* @note none
*/
static uint8_t a_sps30_generate_crc(sps30_handle_t *handle, uint8_t* data, uint8_t count)
{
if (handle->iic_uart == SPS30_INTERFACE_IIC)
{
uint8_t current_byte;
uint8_t crc = 0xFF;
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) ^ 0x31; /* xor */
}
else
{
crc = crc << 1; /* left shift 1 */
}
}
}
return crc; /* return crc */
}
else
{
uint8_t i;
uint32_t crc = 0x00;
for (i = 0; i < count; i++) /* sum */
{
crc += data[i]; /* sum */
}
return (uint8_t)(~(crc & 0xFF)); /* take the least significant byte of the result and invert it */
}
}
/**
* @brief read bytes
* @param[in] *handle points to an sps30 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_sps30_iic_read(sps30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len, uint16_t delay_ms)
{
uint8_t buf[2];
buf[0] = (reg >> 8) & 0xFF; /* set msb */
buf[1] = (reg >> 0) & 0xFF; /* set lsb */
if (handle->iic_write_cmd(addr, (uint8_t *)buf, 2) != 0) /* write data */
{
return 1; /* return error */
}
handle->delay_ms(delay_ms); /* delay ms */
if (handle->iic_read_cmd(addr, (uint8_t *)data, len) != 0) /* read data */
{
return 1; /* return error */
}
return 0; /* success return 0 */
}
/**
* @brief write bytes
* @param[in] *handle points to an sps30 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
* @param[in] delay_ms is the delay time in ms
* @return status code
* - 0 success
* - 1 write failed
* @note none
*/
static uint8_t a_sps30_iic_write(sps30_handle_t *handle, uint8_t addr, uint16_t reg, uint8_t *data, uint16_t len, uint16_t delay_ms)
{
uint8_t buf[16];
if (len > 14) /* check length */
{
return 1; /* return error */
}
buf[0] = (reg >> 8) & 0xFF; /* set msb */
buf[1] = (reg >> 0) & 0xFF; /* set lsb */
memcpy((uint8_t *)&buf[2], data, len); /* copy data */
if (handle->iic_write_cmd(addr, (uint8_t *)buf, len + 2) != 0) /* write data */
{
return 1; /* return error */
}
handle->delay_ms(delay_ms); /* delay ms */
return 0; /* success return 0 */
}
/**
* @brief uart get the rx frame
* @param[in] *handle points to an sps30 handle structure
* @param[in] len is the buffer length
* @param[out] *output points to an output buffer
* @param[in] out_len is the output length
* @return status code
* - 0 success
* - 1 uart get rx frame failed
* @note none
*/
static uint8_t a_sps30_uart_get_rx_frame(sps30_handle_t *handle, uint16_t len, uint8_t *output, uint16_t out_len)
{
uint16_t i, point;
output[0] = handle->buf[0]; /* set buf[0] */
point = 1; /* set point 1 */
for (i = 1; i < (len - 1); i++) /* run n -2 times */
{
if (point >= (out_len - 1)) /* check length */
{
return 1; /* return error */
}
if (((handle->buf[i] == 0x7D) && (handle->buf[i + 1]) == 0x5E) || /* check buffer */
((handle->buf[i] == 0x7D) && (handle->buf[i + 1]) == 0x5D) ||
((handle->buf[i] == 0x7D) && (handle->buf[i + 1]) == 0x31) ||
((handle->buf[i] == 0x7D) && (handle->buf[i + 1]) == 0x33)
)
{
switch (handle->buf[i + 1]) /* judge */
{
case 0x5E : /* 0x5E */
{
output[point] = 0x7E; /* set output */
point++; /* point++ */
i++; /* i++ */
break; /* break */
}
case 0x5D : /* 0x5D */
{
output[point] = 0x7D; /* set output */
point++; /* point++ */
i++; /* i++ */
break; /* break */
}
case 0x31 : /* 0x31 */
{
output[point] = 0x11; /* set output */
point++; /* point++ */
i++; /* i++ */
break; /* break */
}
case 0x33 : /* 0x33 */
{
output[point] = 0x13; /* set output */
point++; /* point++ */
i++; /* i++ */
break; /* break */
}
default :
{
break; /* break */
}
}
}
else
{
output[point] = handle->buf[i]; /* set output */
point++; /* point++ */
}
}
output[point] = handle->buf[len - 1]; /* set the end part */
point++; /* point++ */
if (point != out_len) /* check point */
{
return 1; /* return error */
}
return 0; /* success return 0 */
}
/**
* @brief uart set the tx frame
* @param[in] *handle points to an sps30 handle structure
* @param[in] *input points to an input buffer
* @param[in] in_len is the input length
* @param[out] *out_len points to an output length buffer
* @return status code
* - 0 success
* - 1 uart set tx frame failed
* @note none
*/
static uint8_t a_sps30_uart_set_tx_frame(sps30_handle_t *handle, uint8_t *input, uint16_t in_len, uint16_t *out_len)
{
uint16_t i;
memset(handle->buf, 0, sizeof(uint8_t) * 256); /* clear buffer */
handle->buf[0] = input[0]; /* set buf[0] */
*out_len = 1; /* set output length */
for (i = 1; i < (in_len - 1); i++)
{
if ((*out_len) >= 255) /* check output length */
{
return 1; /* return error */
}
if ((input[i] == 0x7E) || (input[i] == 0x7D) || /* check data */
(input[i] == 0x11) || (input[i] == 0x13)
)
{
switch (input[i]) /* judge input */
{
case 0x7E : /* 0x7E */
{
handle->buf[*out_len] = 0x7D; /* 0x7D */
(*out_len)++; /* output length++ */
handle->buf[*out_len] = 0x5E; /* 0x5E */
(*out_len)++; /* output length++ */
break; /* break */
}
case 0x7D : /* 0x7D */
{
handle->buf[*out_len] = 0x7D; /* set 0x7D */
(*out_len)++; /* output length++ */
handle->buf[*out_len] = 0x5D; /* set 0x5D */
(*out_len)++; /* output length++ */
break; /* break */
}
case 0x11 : /* 0x11 */
{
handle->buf[*out_len] = 0x7D; /* set 0x7D */
(*out_len)++; /* output length++ */
handle->buf[*out_len] = 0x31; /* set 0x31 */
(*out_len)++; /* output length++ */
break; /* break */
}
case 0x13 : /* 0x13 */
{
handle->buf[*out_len] = 0x7D; /* set 0x7D */
(*out_len)++; /* output length++ */
handle->buf[*out_len] = 0x33; /* set 0x33 */
(*out_len)++; /* output length++ */
break; /* break */
}
default :
{
break; /* break */
}
}
}
else
{
handle->buf[*out_len] = input[i]; /* set the buffer */
(*out_len)++; /* output length++ */
}
}
handle->buf[*out_len] = input[in_len - 1];
(*out_len)++; /* copy the end frame */
return 0; /* success return 0 */
}
/**
* @brief write read bytes
* @param[in] *handle points to an sps30 handle structure
* @param[in] *input points to an input buffer
* @param[in] in_len is the input length
* @param[in] delay_ms is the delay time in ms
* @param[out] *output points to an output buffer
* @param[in] out_len is the output length
* @return status code
* - 0 success
* - 1 write read failed
* @note none
*/
static uint8_t a_sps30_uart_write_read(sps30_handle_t *handle, uint8_t *input, uint16_t in_len,
uint16_t delay_ms, uint8_t *output, uint16_t out_len)
{
uint16_t len;
if (a_sps30_uart_set_tx_frame(handle, input, in_len, (uint16_t *)&len) != 0) /* set tx frame */
{
return 1; /* return error */
}
if (handle->uart_flush() != 0) /* uart flush */
{
return 1; /* return error */
}
if (handle->uart_write(handle->buf, len) != 0) /* write data */
{
return 1; /* return error */
}
handle->delay_ms(delay_ms); /* delay ms */
len = handle->uart_read(handle->buf, 256); /* read data */
if (a_sps30_uart_get_rx_frame(handle, len, output, out_len) != 0) /* get rx frame */
{
return 1; /* return error */
}
return 0; /* success return 0 */
}
/**
* @brief print error
* @param[in] *handle points to an sps30 handle structure
* @param[in] e is the error code
* @return error code
* @note none
*/
static uint8_t a_sps30_uart_error(sps30_handle_t *handle, uint8_t e)
{
switch (e)
{
case 0x00 :
{
break;
}
case 0x01 :
{
handle->debug_print("sps30: wrong data length for this command error.\n"); /* wrong data length for this command error */
break;
}
case 0x02 :
{
handle->debug_print("sps30: unknown command.\n"); /* unknown command */
break;
}
case 0x03 :
{
handle->debug_print("sps30: no access right for command.\n"); /* no access right for command */
break;
}
case 0x04 :
{
handle->debug_print("sps30: illegal command parameter or parameter "
"out of allowed range.\n"); /* illegal command parameter or parameter out of allowed range */
break;
}
case 0x28 :
{
handle->debug_print("sps30: internal function argument out of range.\n"); /* internal function argument out of range */
break;
}
case 0x43 :
{
handle->debug_print("sps30: command not allowed in current state.\n"); /* command not allowed in current state */
break;
}
default :
{
handle->debug_print("sps30: unknown code.\n"); /* unknown code */
break;
}
}
return e; /* return error code */
}
/**
* @brief set the chip interface
* @param[in] *handle points to an sps30 handle structure
* @param[in] interface is the chip interface
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t sps30_set_interface(sps30_handle_t *handle, sps30_interface_t interface)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
handle->iic_uart = (uint8_t)interface; /* set interface */
return 0; /* success return 0 */
}
/**
* @brief get the chip interface
* @param[in] *handle points to an sps30 handle structure
* @param[out] *interface points to a chip interface buffer
* @return status code
* - 0 success
* - 2 handle is NULL
* @note none
*/
uint8_t sps30_get_interface(sps30_handle_t *handle, sps30_interface_t *interface)
{
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
*interface = (sps30_interface_t)(handle->iic_uart); /* get interface */
return 0; /* success return 0 */
}
/**
* @brief start the measurement
* @param[in] *handle points to an sps30 handle structure
* @param[in] format is the data format
* @return status code
* - 0 success
* - 1 start measurement failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_start_measurement(sps30_handle_t *handle, sps30_format_t format)
{
uint8_t res;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 2];
uint8_t out_buf[7];
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_START_MEASUREMENT; /* set command */
input_buf[3] = 0x02; /* set length */
input_buf[4] = 0x01; /* set 0x01 */
input_buf[5] = format; /* set format */
handle->format = format; /* save format */
input_buf[6] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 5); /* set crc */
input_buf[7] = 0x7E; /* set stop */
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 8, 20, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
uint8_t buf[3];
buf[0] = format; /* set format */
handle->format = format; /* save format */
buf[1] = 0x00; /* set dummy */
buf[2] = a_sps30_generate_crc(handle, (uint8_t *)buf, 2); /* generate crc */
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_START_MEASUREMENT, (uint8_t *)buf, 3, 20); /* start measurement command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: start measurement failed.\n"); /* start measurement failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief stop the measurement
* @param[in] *handle points to an sps30 handle structure
* @return status code
* - 0 success
* - 1 stop measurement failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_stop_measurement(sps30_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 */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 0];
uint8_t out_buf[7];
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_STOP_MEASUREMENT; /* set command */
input_buf[3] = 0x00; /* set length */
input_buf[4] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 3); /* set crc */
input_buf[5] = 0x7E; /* set stop */
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 6, 20, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_STOP_MEASUREMENT, NULL, 0, 20); /* stop measurement command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: stop measurement failed.\n"); /* stop measurement failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief read the data read flag
* @param[in] *handle points to an sps30 handle structure
* @param[out] *flag points to a data ready flag buffer
* @return status code
* - 0 success
* - 1 read data read flag failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_read_data_flag(sps30_handle_t *handle, sps30_data_ready_flag_t *flag)
{
uint8_t res;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
if (handle->iic_uart != 0) /* uart */
{
handle->debug_print("sps30: uart has no command.\n"); /* uart has no command */
return 1; /* return error */
}
else /* iic */
{
uint8_t buf[3];
memset(buf, 0, sizeof(uint8_t) * 3); /* clear the buffer */
res = a_sps30_iic_read(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_READ_DATA_READY_FLAG, (uint8_t *)buf, 3, 0); /* read data ready flag command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: read data ready flag failed.\n"); /* read data ready flag failed */
return 1; /* return error */
}
if (buf[2] != a_sps30_generate_crc(handle, (uint8_t *)buf, 2)) /* check crc */
{
handle->debug_print("sps30: crc check failed.\n"); /* crc check failed */
return 1; /* return error */
}
*flag = (sps30_data_ready_flag_t)(buf[1] & 0x01); /* get the data ready flag */
}
return 0; /* success return 0 */
}
/**
* @brief enter the sleep mode
* @param[in] *handle points to an sps30 handle structure
* @return status code
* - 0 success
* - 1 sleep failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_sleep(sps30_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 */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 0];
uint8_t out_buf[7];
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_SLEEP; /* set command */
input_buf[3] = 0x00; /* set length */
input_buf[4] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 3); /* set crc */
input_buf[5] = 0x7E; /* set stop */
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 6, 5, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_SLEEP, NULL, 0, 5); /* sleep command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: sleep failed.\n"); /* sleep failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief wake up the chip
* @param[in] *handle points to an sps30 handle structure
* @return status code
* - 0 success
* - 1 wake up failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_wake_up(sps30_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 */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 0];
uint8_t out_buf[7];
uint8_t wake_up = 0xFF;
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_WAKE_UP; /* set command */
input_buf[3] = 0x00; /* set length */
input_buf[4] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 3); /* set crc */
input_buf[5] = 0x7E; /* set stop */
if (handle->uart_write((uint8_t *)&wake_up, 1) != 0) /* write data */
{
return 1; /* return error */
}
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 6, 100, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
(void)a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_WAKE_UP, NULL, 0, 0); /* wake up command */
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_WAKE_UP, NULL, 0, 100); /* wake up command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: wake up failed.\n"); /* wake up failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief start the fan cleaning
* @param[in] *handle points to an sps30 handle structure
* @return status code
* - 0 success
* - 1 start fan cleaning failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_start_fan_cleaning(sps30_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 */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 0];
uint8_t out_buf[7];
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_START_FAN_CLEANING; /* set command */
input_buf[3] = 0x00; /* set length */
input_buf[4] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 3); /* set crc */
input_buf[5] = 0x7E; /* set stop */
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 6, 20, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_START_FAN_CLEANING, NULL, 0, 5); /* start the fan cleaning command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: start the fan cleaning failed.\n"); /* start the fan cleaning failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief set the auto cleaning interval
* @param[in] *handle points to an sps30 handle structure
* @param[in] second is the interval
* @return status code
* - 0 success
* - 1 set auto cleaning interval failed
* - 2 handle is NULL
* - 3 handle is not initialized
* - 4 second is invalid
* @note 10 < second < 604800
*/
uint8_t sps30_set_auto_cleaning_interval(sps30_handle_t *handle, uint32_t second)
{
uint8_t res;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
if ((second < 10) || (second > 604800)) /* check second */
{
handle->debug_print("sps30: second is invalid.\n"); /* second is invalid */
return 4; /* return error */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 5];
uint8_t out_buf[7];
input_buf[0] = 0x7E; /* set start */
input_buf[1] = 0x00; /* set addr */
input_buf[2] = SPS30_UART_COMMAND_READ_WRITE_AUTO_CLEANING_INTERVAL; /* set command */
input_buf[3] = 0x05; /* set length */
input_buf[4] = 0x00; /* set 0x00 */
input_buf[5] = (second >> 24) & 0xFF; /* set 32 - 24 bits */
input_buf[6] = (second >> 16) & 0xFF; /* set 24 - 16 bits */
input_buf[7] = (second >> 8) & 0xFF; /* set 16 - 8 bits */
input_buf[8] = (second >> 0) & 0xFF; /* set 8 - 0 bits */
input_buf[9] = a_sps30_generate_crc(handle, (uint8_t *)&input_buf[1], 8); /* set crc */
input_buf[10] = 0x7E; /* set stop */
memset(out_buf, 0, sizeof(uint8_t) * 7); /* clear the buffer */
res = a_sps30_uart_write_read(handle, (uint8_t *)input_buf, 11, 20, (uint8_t *)out_buf, 7); /* write read frame */
if (res != 0) /* check result */
{
handle->debug_print("sps30: write read failed.\n"); /* write read failed */
return 1; /* return error */
}
if (out_buf[5] != a_sps30_generate_crc(handle, (uint8_t *)&out_buf[1], 4)) /* check crc */
{
handle->debug_print("sps30: crc check error.\n"); /* crc check error */
return 1; /* return error */
}
if (a_sps30_uart_error(handle, out_buf[3]) != 0) /* check status */
{
return 1; /* return error */
}
}
else /* iic */
{
uint8_t buf[6];
buf[0] = (second >> 24) & 0xFF; /* set byte 4 */
buf[1] = (second >> 16) & 0xFF; /* set byte 3 */
buf[2] = a_sps30_generate_crc(handle, (uint8_t *)&buf[0], 2); /* set crc */
buf[3] = (second >> 8) & 0xFF; /* set byte 2 */
buf[4] = (second >> 0) & 0xFF; /* set byte 1 */
buf[5] = a_sps30_generate_crc(handle, (uint8_t *)&buf[3], 2); /* set crc */
res = a_sps30_iic_write(handle, SPS30_ADDRESS, SPS30_IIC_COMMAND_READ_WRITE_AUTO_CLEANING_INTERVAL,
(uint8_t *)buf, 6, 20); /* set auto cleaning interval command */
if (res != 0) /* check result */
{
handle->debug_print("sps30: set auto cleaning interval failed.\n"); /* set auto cleaning interval failed */
return 1; /* return error */
}
}
return 0; /* success return 0 */
}
/**
* @brief get the auto cleaning interval
* @param[in] *handle points to an sps30 handle structure
* @param[out] *second points to an interval buffer
* @return status code
* - 0 success
* - 1 get auto cleaning interval failed
* - 2 handle is NULL
* - 3 handle is not initialized
* @note none
*/
uint8_t sps30_get_auto_cleaning_interval(sps30_handle_t *handle, uint32_t *second)
{
uint8_t res;
if (handle == NULL) /* check handle */
{
return 2; /* return error */
}
if (handle->inited != 1) /* check handle initialization */
{
return 3; /* return error */
}
if (handle->iic_uart != 0) /* uart */
{
uint8_t input_buf[6 + 1];
uint8_t out_buf[7 + 4];