-
Notifications
You must be signed in to change notification settings - Fork 0
/
bme69x.c
1761 lines (1501 loc) · 58.4 KB
/
bme69x.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
/**
* Copyright (c) 2024 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme69x.c
* @date 2024-05-14
* @version v1.0.1
*
*/
#include "bme69x.h"
#include <stdio.h>
/* This internal API is used to read the calibration coefficients */
static int8_t get_calib_data(struct bme69x_dev *dev);
/* This internal API is used to read variant ID information register status */
static int8_t read_variant_id(struct bme69x_dev *dev);
/* This internal API is used to calculate the gas wait */
static uint8_t calc_gas_wait(uint16_t dur);
#ifndef BME69X_USE_FPU
/* This internal API is used to calculate the temperature in integer */
static int16_t calc_temperature(uint32_t temp_adc, struct bme69x_dev *dev, uint32_t *t_lin);
/* This internal API is used to calculate the pressure in integer */
static uint32_t calc_pressure(uint32_t pres_adc, uint32_t t_lin, const struct bme69x_dev *dev);
/* This internal API is used to calculate the humidity in integer */
static uint32_t calc_humidity(uint16_t hum_adc, int16_t comp_temperature, const struct bme69x_dev *dev);
/* This internal API is used to calculate the gas resistance for BME69x variant */
static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range);
/* This internal API is used to calculate the heater resistance using integer */
static uint8_t calc_res_heat(uint16_t temp, const struct bme69x_dev *dev);
#else
/* This internal API is used to calculate the temperature value in float */
static float calc_temperature(uint32_t temp_adc, const struct bme69x_dev *dev);
/* This internal API is used to calculate the pressure value in float */
static float calc_pressure(uint32_t pres_adc, float comp_temperature, const struct bme69x_dev *dev);
/* This internal API is used to calculate the humidity value in float */
static float calc_humidity(uint16_t hum_adc, float comp_temperature, const struct bme69x_dev *dev);
/* This internal API is used to calculate the gas for BME69x variant in float */
static float calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range);
/* This internal API is used to calculate the heater resistance value using float */
static uint8_t calc_res_heat(uint16_t temp, const struct bme69x_dev *dev);
#endif
/* This internal API is used to read a single data of the sensor */
static int8_t read_field_data(uint8_t index, struct bme69x_data *data, struct bme69x_dev *dev);
/* This internal API is used to read all data fields of the sensor */
static int8_t read_all_field_data(struct bme69x_data * const data[], struct bme69x_dev *dev);
/* This internal API is used to switch between SPI memory pages */
static int8_t set_mem_page(uint8_t reg_addr, struct bme69x_dev *dev);
/* This internal API is used to get the current SPI memory page */
static int8_t get_mem_page(struct bme69x_dev *dev);
/* This internal API is used to check the bme69x_dev for null pointers */
static int8_t null_ptr_check(const struct bme69x_dev *dev);
/* This internal API is used to set heater configurations */
static int8_t set_conf(const struct bme69x_heatr_conf *conf, uint8_t op_mode, uint8_t *nb_conv, struct bme69x_dev *dev);
/* This internal API is used to limit the max value of a parameter */
static int8_t boundary_check(uint8_t *value, uint8_t max, struct bme69x_dev *dev);
/* This internal API is used to calculate the register value for
* shared heater duration */
static uint8_t calc_heatr_dur_shared(uint16_t dur);
/* This internal API is used to swap two fields */
static void swap_fields(uint8_t index1, uint8_t index2, struct bme69x_data *field[]);
/* This internal API is used sort the sensor data */
static void sort_sensor_data(uint8_t low_index, uint8_t high_index, struct bme69x_data *field[]);
/*
* @brief Function to analyze the sensor data
*
* @param[in] data Array of measurement data
* @param[in] n_meas Number of measurements
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
*/
static int8_t analyze_sensor_data(const struct bme69x_data *data, uint8_t n_meas);
/******************************************************************************************/
/* Global API definitions */
/******************************************************************************************/
/* @brief This API reads the chip-id of the sensor which is the first step to
* verify the sensor and also calibrates the sensor
* As this API is the entry point, call this API before using other APIs.
*/
int8_t bme69x_init(struct bme69x_dev *dev)
{
int8_t rslt;
(void) bme69x_soft_reset(dev);
rslt = bme69x_get_regs(BME69X_REG_CHIP_ID, &dev->chip_id, 1, dev);
if (rslt == BME69X_OK)
{
if (dev->chip_id == BME69X_CHIP_ID)
{
/* Read Variant ID */
rslt = read_variant_id(dev);
if (rslt == BME69X_OK)
{
/* Get the Calibration data */
rslt = get_calib_data(dev);
}
}
else
{
rslt = BME69X_E_DEV_NOT_FOUND;
}
}
return rslt;
}
/*
* @brief This API writes the given data to the register address of the sensor
*/
int8_t bme69x_set_regs(const uint8_t *reg_addr, const uint8_t *reg_data, uint32_t len, struct bme69x_dev *dev)
{
int8_t rslt;
/* Length of the temporary buffer is 2*(length of register)*/
uint8_t tmp_buff[BME69X_LEN_INTERLEAVE_BUFF] = { 0 };
uint16_t index;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if ((rslt == BME69X_OK) && reg_addr && reg_data)
{
if ((len > 0) && (len <= (BME69X_LEN_INTERLEAVE_BUFF / 2)))
{
/* Interleave the 2 arrays */
for (index = 0; index < len; index++)
{
if (dev->intf == BME69X_SPI_INTF)
{
/* Set the memory page */
rslt = set_mem_page(reg_addr[index], dev);
tmp_buff[(2 * index)] = reg_addr[index] & BME69X_SPI_WR_MSK;
}
else
{
tmp_buff[(2 * index)] = reg_addr[index];
}
tmp_buff[(2 * index) + 1] = reg_data[index];
}
/* Write the interleaved array */
if (rslt == BME69X_OK)
{
dev->intf_rslt = dev->write(tmp_buff[0], &tmp_buff[1], (2 * len) - 1, dev->intf_ptr);
if (dev->intf_rslt != 0)
{
rslt = BME69X_E_COM_FAIL;
}
}
}
else
{
rslt = BME69X_E_INVALID_LENGTH;
}
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*
* @brief This API reads the data from the given register address of sensor.
*/
int8_t bme69x_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, struct bme69x_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if ((rslt == BME69X_OK) && reg_data)
{
if (dev->intf == BME69X_SPI_INTF)
{
/* Set the memory page */
rslt = set_mem_page(reg_addr, dev);
if (rslt == BME69X_OK)
{
reg_addr = reg_addr | BME69X_SPI_RD_MSK;
}
}
dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);
if (dev->intf_rslt != 0)
{
rslt = BME69X_E_COM_FAIL;
}
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*
* @brief This API soft-resets the sensor.
*/
int8_t bme69x_soft_reset(struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME69X_REG_SOFT_RESET;
/* 0xb6 is the soft reset command */
uint8_t soft_rst_cmd = BME69X_SOFT_RESET_CMD;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if (rslt == BME69X_OK)
{
if (dev->intf == BME69X_SPI_INTF)
{
rslt = get_mem_page(dev);
}
/* Reset the device */
if (rslt == BME69X_OK)
{
rslt = bme69x_set_regs(®_addr, &soft_rst_cmd, 1, dev);
if (rslt == BME69X_OK)
{
/* Wait for 5ms */
dev->delay_us(BME69X_PERIOD_RESET, dev->intf_ptr);
/* After reset get the memory page */
if (dev->intf == BME69X_SPI_INTF)
{
rslt = get_mem_page(dev);
}
}
}
}
return rslt;
}
/*
* @brief This API is used to set the oversampling, filter and odr configuration
*/
int8_t bme69x_set_conf(struct bme69x_conf *conf, struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t odr20 = 0, odr3 = 1;
uint8_t current_op_mode;
/* Register data starting from BME69X_REG_CTRL_GAS_1(0x71) up to BME69X_REG_CONFIG(0x75) */
uint8_t reg_array[BME69X_LEN_CONFIG] = { 0x71, 0x72, 0x73, 0x74, 0x75 };
uint8_t data_array[BME69X_LEN_CONFIG] = { 0 };
rslt = bme69x_get_op_mode(¤t_op_mode, dev);
if (rslt == BME69X_OK)
{
/* Configure only in the sleep mode */
rslt = bme69x_set_op_mode(BME69X_SLEEP_MODE, dev);
}
if (conf == NULL)
{
rslt = BME69X_E_NULL_PTR;
}
else if (rslt == BME69X_OK)
{
/* Read the whole configuration and write it back once later */
rslt = bme69x_get_regs(reg_array[0], data_array, BME69X_LEN_CONFIG, dev);
dev->info_msg = BME69X_OK;
if (rslt == BME69X_OK)
{
rslt = boundary_check(&conf->filter, BME69X_FILTER_SIZE_127, dev);
}
if (rslt == BME69X_OK)
{
rslt = boundary_check(&conf->os_temp, BME69X_OS_16X, dev);
}
if (rslt == BME69X_OK)
{
rslt = boundary_check(&conf->os_pres, BME69X_OS_16X, dev);
}
if (rslt == BME69X_OK)
{
rslt = boundary_check(&conf->os_hum, BME69X_OS_16X, dev);
}
if (rslt == BME69X_OK)
{
rslt = boundary_check(&conf->odr, BME69X_ODR_NONE, dev);
}
if (rslt == BME69X_OK)
{
data_array[4] = BME69X_SET_BITS(data_array[4], BME69X_FILTER, conf->filter);
data_array[3] = BME69X_SET_BITS(data_array[3], BME69X_OST, conf->os_temp);
data_array[3] = BME69X_SET_BITS(data_array[3], BME69X_OSP, conf->os_pres);
data_array[1] = BME69X_SET_BITS_POS_0(data_array[1], BME69X_OSH, conf->os_hum);
if (conf->odr != BME69X_ODR_NONE)
{
odr20 = conf->odr;
odr3 = 0;
}
data_array[4] = BME69X_SET_BITS(data_array[4], BME69X_ODR20, odr20);
data_array[0] = BME69X_SET_BITS(data_array[0], BME69X_ODR3, odr3);
}
}
if (rslt == BME69X_OK)
{
rslt = bme69x_set_regs(reg_array, data_array, BME69X_LEN_CONFIG, dev);
}
if ((current_op_mode != BME69X_SLEEP_MODE) && (rslt == BME69X_OK))
{
rslt = bme69x_set_op_mode(current_op_mode, dev);
}
return rslt;
}
/*
* @brief This API is used to get the oversampling, filter and odr
*/
int8_t bme69x_get_conf(struct bme69x_conf *conf, struct bme69x_dev *dev)
{
int8_t rslt;
/* starting address of the register array for burst read*/
uint8_t reg_addr = BME69X_REG_CTRL_GAS_1;
uint8_t data_array[BME69X_LEN_CONFIG];
rslt = bme69x_get_regs(reg_addr, data_array, 5, dev);
if (!conf)
{
rslt = BME69X_E_NULL_PTR;
}
else if (rslt == BME69X_OK)
{
conf->os_hum = BME69X_GET_BITS_POS_0(data_array[1], BME69X_OSH);
conf->filter = BME69X_GET_BITS(data_array[4], BME69X_FILTER);
conf->os_temp = BME69X_GET_BITS(data_array[3], BME69X_OST);
conf->os_pres = BME69X_GET_BITS(data_array[3], BME69X_OSP);
if (BME69X_GET_BITS(data_array[0], BME69X_ODR3))
{
conf->odr = BME69X_ODR_NONE;
}
else
{
conf->odr = BME69X_GET_BITS(data_array[4], BME69X_ODR20);
}
}
return rslt;
}
/*
* @brief This API is used to set the operation mode of the sensor
*/
int8_t bme69x_set_op_mode(const uint8_t op_mode, struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t tmp_pow_mode;
uint8_t pow_mode = 0;
uint8_t reg_addr = BME69X_REG_CTRL_MEAS;
/* Call until in sleep */
do
{
rslt = bme69x_get_regs(BME69X_REG_CTRL_MEAS, &tmp_pow_mode, 1, dev);
if (rslt == BME69X_OK)
{
/* Put to sleep before changing mode */
pow_mode = (tmp_pow_mode & BME69X_MODE_MSK);
if (pow_mode != BME69X_SLEEP_MODE)
{
tmp_pow_mode &= ~BME69X_MODE_MSK; /* Set to sleep */
rslt = bme69x_set_regs(®_addr, &tmp_pow_mode, 1, dev);
dev->delay_us(BME69X_PERIOD_POLL, dev->intf_ptr);
}
}
} while ((pow_mode != BME69X_SLEEP_MODE) && (rslt == BME69X_OK));
/* Already in sleep */
if ((op_mode != BME69X_SLEEP_MODE) && (rslt == BME69X_OK))
{
tmp_pow_mode = (tmp_pow_mode & ~BME69X_MODE_MSK) | (op_mode & BME69X_MODE_MSK);
rslt = bme69x_set_regs(®_addr, &tmp_pow_mode, 1, dev);
}
return rslt;
}
/*
* @brief This API is used to get the operation mode of the sensor.
*/
int8_t bme69x_get_op_mode(uint8_t *op_mode, struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t mode;
if (op_mode)
{
rslt = bme69x_get_regs(BME69X_REG_CTRL_MEAS, &mode, 1, dev);
/* Masking the other register bit info*/
*op_mode = mode & BME69X_MODE_MSK;
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*
* @brief This API is used to get the remaining duration that can be used for heating.
*/
uint32_t bme69x_get_meas_dur(const uint8_t op_mode, struct bme69x_conf *conf, struct bme69x_dev *dev)
{
int8_t rslt;
uint32_t meas_dur = 0; /* Calculate in us */
uint32_t meas_cycles;
uint8_t os_to_meas_cycles[6] = { 0, 1, 2, 4, 8, 16 };
if (conf != NULL)
{
/* Boundary check for temperature oversampling */
rslt = boundary_check(&conf->os_temp, BME69X_OS_16X, dev);
if (rslt == BME69X_OK)
{
/* Boundary check for pressure oversampling */
rslt = boundary_check(&conf->os_pres, BME69X_OS_16X, dev);
}
if (rslt == BME69X_OK)
{
/* Boundary check for humidity oversampling */
rslt = boundary_check(&conf->os_hum, BME69X_OS_16X, dev);
}
if (rslt == BME69X_OK)
{
meas_cycles = os_to_meas_cycles[conf->os_temp];
meas_cycles += os_to_meas_cycles[conf->os_pres];
meas_cycles += os_to_meas_cycles[conf->os_hum];
/* TPH measurement duration */
meas_dur = meas_cycles * UINT32_C(1963);
meas_dur += UINT32_C(477 * 4); /* TPH switching duration */
meas_dur += UINT32_C(477 * 5); /* Gas measurement duration */
if (op_mode != BME69X_PARALLEL_MODE)
{
meas_dur += UINT32_C(1000); /* Wake up duration of 1ms */
}
}
}
return meas_dur;
}
/*
* @brief This API reads the pressure, temperature and humidity and gas data
* from the sensor, compensates the data and store it in the bme69x_data
* structure instance passed by the user.
*/
int8_t bme69x_get_data(uint8_t op_mode, struct bme69x_data *data, uint8_t *n_data, struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t i = 0, j = 0, new_fields = 0;
struct bme69x_data *field_ptr[3] = { 0 };
struct bme69x_data field_data[3] = { { 0 } };
field_ptr[0] = &field_data[0];
field_ptr[1] = &field_data[1];
field_ptr[2] = &field_data[2];
rslt = null_ptr_check(dev);
if ((rslt == BME69X_OK) && (data != NULL))
{
/* Reading the sensor data in forced mode only */
if (op_mode == BME69X_FORCED_MODE)
{
rslt = read_field_data(0, data, dev);
if (rslt == BME69X_OK)
{
if (data->status & BME69X_NEW_DATA_MSK)
{
new_fields = 1;
}
else
{
new_fields = 0;
rslt = BME69X_W_NO_NEW_DATA;
}
}
}
else if ((op_mode == BME69X_PARALLEL_MODE) || (op_mode == BME69X_SEQUENTIAL_MODE))
{
/* Read the 3 fields and count the number of new data fields */
rslt = read_all_field_data(field_ptr, dev);
new_fields = 0;
for (i = 0; (i < 3) && (rslt == BME69X_OK); i++)
{
if (field_ptr[i]->status & BME69X_NEW_DATA_MSK)
{
new_fields++;
}
}
/* Sort the sensor data in parallel & sequential modes*/
for (i = 0; (i < 2) && (rslt == BME69X_OK); i++)
{
for (j = i + 1; j < 3; j++)
{
sort_sensor_data(i, j, field_ptr);
}
}
/* Copy the sorted data */
for (i = 0; ((i < 3) && (rslt == BME69X_OK)); i++)
{
data[i] = *field_ptr[i];
}
if (new_fields == 0)
{
rslt = BME69X_W_NO_NEW_DATA;
}
}
else
{
rslt = BME69X_W_DEFINE_OP_MODE;
}
if (n_data == NULL)
{
rslt = BME69X_E_NULL_PTR;
}
else
{
*n_data = new_fields;
}
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*
* @brief This API is used to set the gas configuration of the sensor.
*/
int8_t bme69x_set_heatr_conf(uint8_t op_mode, const struct bme69x_heatr_conf *conf, struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t nb_conv = 0;
uint8_t hctrl, run_gas = 0;
uint8_t ctrl_gas_data[2];
uint8_t ctrl_gas_addr[2] = { BME69X_REG_CTRL_GAS_0, BME69X_REG_CTRL_GAS_1 };
if (conf != NULL)
{
rslt = bme69x_set_op_mode(BME69X_SLEEP_MODE, dev);
if (rslt == BME69X_OK)
{
rslt = set_conf(conf, op_mode, &nb_conv, dev);
}
if (rslt == BME69X_OK)
{
rslt = bme69x_get_regs(BME69X_REG_CTRL_GAS_0, ctrl_gas_data, 2, dev);
if (rslt == BME69X_OK)
{
if (conf->enable == BME69X_ENABLE)
{
hctrl = BME69X_ENABLE_HEATER;
run_gas = BME69X_ENABLE_GAS_MEAS;
}
else
{
hctrl = BME69X_DISABLE_HEATER;
run_gas = BME69X_DISABLE_GAS_MEAS;
}
ctrl_gas_data[0] = BME69X_SET_BITS(ctrl_gas_data[0], BME69X_HCTRL, hctrl);
ctrl_gas_data[1] = BME69X_SET_BITS_POS_0(ctrl_gas_data[1], BME69X_NBCONV, nb_conv);
ctrl_gas_data[1] = BME69X_SET_BITS(ctrl_gas_data[1], BME69X_RUN_GAS, run_gas);
rslt = bme69x_set_regs(ctrl_gas_addr, ctrl_gas_data, 2, dev);
}
}
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to get the gas configuration of the sensor.
*/
int8_t bme69x_get_heatr_conf(const struct bme69x_heatr_conf *conf, struct bme69x_dev *dev)
{
int8_t rslt = BME69X_OK;
uint8_t data_array[10] = { 0 };
uint8_t i;
if ((conf != NULL) && (conf->heatr_dur_prof != NULL) && (conf->heatr_temp_prof != NULL))
{
/* FIXME: Add conversion to deg C and ms and add the other parameters */
rslt = bme69x_get_regs(BME69X_REG_RES_HEAT0, data_array, 10, dev);
if (rslt == BME69X_OK)
{
for (i = 0; i < conf->profile_len; i++)
{
conf->heatr_temp_prof[i] = data_array[i];
}
rslt = bme69x_get_regs(BME69X_REG_GAS_WAIT0, data_array, 10, dev);
if (rslt == BME69X_OK)
{
for (i = 0; i < conf->profile_len; i++)
{
conf->heatr_dur_prof[i] = data_array[i];
}
}
}
}
else
{
rslt = BME69X_E_NULL_PTR;
}
return rslt;
}
/*
* @brief This API performs Self-test of low and high gas variants of BME69X
*/
int8_t bme69x_selftest_check(const struct bme69x_dev *dev)
{
int8_t rslt;
uint8_t n_fields;
uint8_t i = 0;
struct bme69x_data data[BME69X_N_MEAS] = { { 0 } };
struct bme69x_dev t_dev;
struct bme69x_conf conf;
struct bme69x_heatr_conf heatr_conf;
rslt = null_ptr_check(dev);
if (rslt == BME69X_OK)
{
/* Copy required parameters from reference bme69x_dev struct */
t_dev.amb_temp = 25;
t_dev.read = dev->read;
t_dev.write = dev->write;
t_dev.intf = dev->intf;
t_dev.delay_us = dev->delay_us;
t_dev.intf_ptr = dev->intf_ptr;
rslt = bme69x_init(&t_dev);
}
if (rslt == BME69X_OK)
{
/* Set the temperature, pressure and humidity & filter settings */
conf.os_hum = BME69X_OS_1X;
conf.os_pres = BME69X_OS_16X;
conf.os_temp = BME69X_OS_2X;
/* Set the remaining gas sensor settings and link the heating profile */
heatr_conf.enable = BME69X_ENABLE;
heatr_conf.heatr_dur = BME69X_HEATR_DUR1;
heatr_conf.heatr_temp = BME69X_HIGH_TEMP;
rslt = bme69x_set_heatr_conf(BME69X_FORCED_MODE, &heatr_conf, &t_dev);
if (rslt == BME69X_OK)
{
rslt = bme69x_set_conf(&conf, &t_dev);
if (rslt == BME69X_OK)
{
rslt = bme69x_set_op_mode(BME69X_FORCED_MODE, &t_dev); /* Trigger a measurement */
if (rslt == BME69X_OK)
{
/* Wait for the measurement to complete */
t_dev.delay_us(BME69X_HEATR_DUR1_DELAY, t_dev.intf_ptr);
rslt = bme69x_get_data(BME69X_FORCED_MODE, &data[0], &n_fields, &t_dev);
if (rslt == BME69X_OK)
{
if ((data[0].idac != 0x00) && (data[0].idac != 0xFF) &&
(data[0].status & BME69X_GASM_VALID_MSK))
{
rslt = BME69X_OK;
}
else
{
rslt = BME69X_E_SELF_TEST;
}
}
}
}
}
heatr_conf.heatr_dur = BME69X_HEATR_DUR2;
while ((rslt == BME69X_OK) && (i < BME69X_N_MEAS))
{
if (i % 2 == 0)
{
heatr_conf.heatr_temp = BME69X_HIGH_TEMP; /* Higher temperature */
}
else
{
heatr_conf.heatr_temp = BME69X_LOW_TEMP; /* Lower temperature */
}
rslt = bme69x_set_heatr_conf(BME69X_FORCED_MODE, &heatr_conf, &t_dev);
if (rslt == BME69X_OK)
{
rslt = bme69x_set_conf(&conf, &t_dev);
if (rslt == BME69X_OK)
{
rslt = bme69x_set_op_mode(BME69X_FORCED_MODE, &t_dev); /* Trigger a measurement */
if (rslt == BME69X_OK)
{
/* Wait for the measurement to complete */
t_dev.delay_us(BME69X_HEATR_DUR2_DELAY, t_dev.intf_ptr);
rslt = bme69x_get_data(BME69X_FORCED_MODE, &data[i], &n_fields, &t_dev);
}
}
}
i++;
}
if (rslt == BME69X_OK)
{
rslt = analyze_sensor_data(data, BME69X_N_MEAS);
}
}
return rslt;
}
/*****************************INTERNAL APIs***********************************************/
#ifndef BME69X_USE_FPU
/* @brief This internal API is used to calculate the temperature value. */
static int16_t calc_temperature(uint32_t temp_adc, struct bme69x_dev *dev, uint32_t *t_lin)
{
int64_t partial_data1;
int64_t partial_data2;
int64_t partial_data3;
int64_t partial_data4;
int64_t partial_data5;
int64_t partial_data6;
int64_t tem_comp;
partial_data1 = (int64_t)(temp_adc - (256U * dev->calib.par_t1));
partial_data2 = (int64_t)(partial_data1 * (int64_t)dev->calib.par_t2);
partial_data3 = (int64_t)(partial_data1 * partial_data1);
partial_data4 = (int64_t)(partial_data3 * (int64_t)dev->calib.par_t3);
partial_data5 = (int64_t)((int64_t)(partial_data2 * 262144UL) + partial_data4);
partial_data6 = (int64_t)(partial_data5 / 4294967296ULL);
*t_lin = (uint32_t)partial_data6;
tem_comp = (int64_t)((partial_data6 * 25U) / 16384UL);
return (int16_t)(tem_comp);
}
/* @brief This internal API is used to calculate the pressure value. */
static uint32_t calc_pressure(uint32_t pres_adc, uint32_t t_lin, const struct bme69x_dev *dev)
{
int64_t partial_data1;
int64_t partial_data2;
int64_t partial_data3;
int64_t partial_data4;
int64_t partial_data5;
int64_t partial_data6;
int64_t offset;
int64_t sensitivity;
int64_t press_comp;
int64_t t_lin_64;
t_lin_64 = (int64_t)t_lin;
partial_data1 = t_lin_64 * t_lin_64;
partial_data2 = partial_data1 / 64;
partial_data3 = partial_data2 * t_lin_64 / 256;
partial_data4 = dev->calib.par_p4 * partial_data3 / 32;
partial_data5 = dev->calib.par_p3 * partial_data1 * 16;
partial_data6 = dev->calib.par_p2 * t_lin_64 * (1 << 22);
offset = dev->calib.par_p1 * ((int64_t)1 << 47) + partial_data4 + partial_data5 + partial_data6;
partial_data2 = (dev->calib.par_p8 * partial_data3) / (1 << 5);
partial_data4 = dev->calib.par_p7 * partial_data1 * (1 << 2);
partial_data5 = (dev->calib.par_p6 - 16384) * t_lin_64 * (1 << 21);
sensitivity = (dev->calib.par_p5 - 16384) * ((int64_t)1 << 46) + partial_data2 + partial_data4 + partial_data5;
partial_data1 = sensitivity / (1 << 24) * pres_adc;
partial_data2 = dev->calib.par_p10 * t_lin_64;
partial_data3 = partial_data2 + dev->calib.par_p9 * (1 << 16);
partial_data4 = partial_data3 * pres_adc / (1 << 13);
partial_data5 = (pres_adc * partial_data4 / 10) / (1 << 9);
partial_data5 = partial_data5 * 10;
partial_data6 = pres_adc * pres_adc;
partial_data2 = dev->calib.par_p11 * partial_data6 / (1 << 16);
partial_data3 = partial_data2 * pres_adc / (1 << 7);
partial_data4 = offset / 4 + partial_data1 + partial_data5 + partial_data3;
press_comp = (partial_data4 / ((int64_t)1 << 40)) * 25;
return (uint32_t)(press_comp / 100);
}
/* This internal API is used to calculate the humidity in integer */
static uint32_t calc_humidity(uint16_t hum_adc, int16_t comp_temperature, const struct bme69x_dev *dev)
{
uint32_t hum_comp;
int64_t hum_64 = hum_adc;
int64_t t_comp = comp_temperature;
int64_t t_fine = (t_comp * 256 - 128) / 5;
int64_t var_H = t_fine - 76800UL;
var_H =
(((((hum_64 * 16384UL) - (dev->calib.par_h1 * 1048576UL) - (dev->calib.par_h2 * var_H)) + 16384UL) / 32768UL) *
((((((var_H * dev->calib.par_h4) / 1024UL) * ((var_H * dev->calib.par_h3) / 2048UL + 32768UL)) / 1024UL) +
2097152ULL) * dev->calib.par_h5 + 8192UL) / 16384UL);
var_H = var_H - (((((var_H / 32768UL) * (var_H / 32768UL)) / 128UL) * dev->calib.par_h6) / 16UL);
hum_comp = (uint32_t)(var_H / 4096UL);
return hum_comp;
}
/* This internal API is used to calculate the gas resistance */
static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range)
{
uint32_t calc_gas_res;
uint32_t var1 = UINT32_C(262144) >> gas_range;
int32_t var2 = (int32_t)gas_res_adc - INT32_C(512);
var2 *= INT32_C(3);
var2 = INT32_C(4096) + var2;
/* multiplying 10000 then dividing then multiplying by 100 instead of multiplying by 1000000 to prevent overflow */
calc_gas_res = (UINT32_C(10000) * var1) / (uint32_t)var2;
calc_gas_res = calc_gas_res * 100;
return calc_gas_res;
}
/* This internal API is used to calculate the heater resistance value using integer */
static uint8_t calc_res_heat(uint16_t temp, const struct bme69x_dev *dev)
{
uint8_t heatr_res;
int32_t var1;
int32_t var2;
int32_t var3;
int32_t var4;
int32_t var5;
int32_t heatr_res_x100;
if (temp > 400) /* Cap temperature */
{
temp = 400;
}
var1 = (((int32_t)dev->amb_temp * dev->calib.par_g3) / 1000U) * 256; /* par_g1 */
var2 = (dev->calib.par_g1 + 784) * (((((dev->calib.par_g2 + 154009UL) * temp * 5) / 100) + 3276800ULL) / 10); /* par_g2,
* par_g3 */
var3 = var1 + (var2 >> 1);
var4 = (var3 / (dev->calib.res_heat_range + 4));
var5 = (131 * dev->calib.res_heat_val) + 65536UL;
heatr_res_x100 = (int32_t)(((var4 / var5) - 250) * 34);
heatr_res = (uint8_t)((heatr_res_x100 + 50) / 100);
return heatr_res;
}
#else
/* @brief This internal API is used to calculate the temperature value. */
static float calc_temperature(uint32_t temp_adc, const struct bme69x_dev *dev)
{
uint32_t do1, cf;
double dtk1, dtk2, temp1, temp2;
double calc_temp;
do1 = (uint32_t)dev->calib.par_t1 << 8;
dtk1 = (double)dev->calib.par_t2 / (double)(1ULL << 30);
dtk2 = (double)dev->calib.par_t3 / (double)(1ULL << 48);
cf = temp_adc - do1;
temp1 = (double)(cf * dtk1);
temp2 = (double)cf * (double)cf * dtk2;
calc_temp = temp1 + temp2;
return (float)calc_temp;
}
/* @brief This internal API is used to calculate the pressure value. */
static float calc_pressure(uint32_t pres_adc, float comp_temperature, const struct bme69x_dev *dev)
{
uint32_t o;
double tk10, tk20, tk30;
double s;
double tk1s, tk2s, tk3s;
double nls, tknls, nls3;
double calc_pres, tmp1, tmp2, tmp3, tmp4;
o = (uint32_t)dev->calib.par_p1 * (uint32_t)(1ULL << 3);
tk10 = (double)dev->calib.par_p2 / (double)(1ULL << 6);
tk20 = (double)dev->calib.par_p3 / (double)(1ULL << 8);
tk30 = (double)dev->calib.par_p4 / (double)(1ULL << 15);
s = ((double)dev->calib.par_p5 - (double)(1ULL << 14)) / (double)(1ULL << 20);
tk1s = ((double)dev->calib.par_p6 - (double)(1ULL << 14)) / (double)(1ULL << 29);
tk2s = (double)dev->calib.par_p7 / (double)(1ULL << 32);
tk3s = (double)dev->calib.par_p8 / (double)(1ULL << 37);
nls = (double)dev->calib.par_p9 / (double)(1ULL << 48);