-
Notifications
You must be signed in to change notification settings - Fork 80
/
bme680.c
2535 lines (2312 loc) · 73.8 KB
/
bme680.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
/** \mainpage
****************************************************************************
* Copyright (C) 2015 Bosch Sensortec GmbH
*
* File : bme680.c
*
* Date : 2016/06/10
*
* Revision: 2.0.0
*
* Usage: Sensor Driver for BME680 sensor
*
****************************************************************************
* Section Disclaimer
*
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of the copyright holder nor the names of the
* 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 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
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/
/*! \file bme680.c
\brief BME680 Sensor Driver Support source File */
/***************************************************************************
Header files
****************************************************************************/
#include "bme680.h"
#include "bme680_calculations.h"
#include "bme680_internal.h"
/***************************************************************************
Macros, Enums, Constants
****************************************************************************/
/***************************************************************************
File globals, typedefs
****************************************************************************/
/* Static function declarations */
static enum bme680_return_type bme680_get_calib_param(struct bme680_t *bme680);
static void bme680_scale_to_multiplication_factor(u16 *duration_u16);
#ifndef __KERNEL__
static void bme680_buffer_restruct_burst_write(u8 arr[], u8 reg_addr,
u8 data_size, u8 arr_size);
#endif
static u8 bme680_find_largest_index(u8 *meas_index);
static enum bme680_return_type bme680_set_memory_page(u8 memory_page_u8,
struct bme680_t *bme680);
static void bme680_align_sensor_type_uncomp_data(u8 *a_data_u8, u8 index,
u8 offset, u8 sensor_type,
struct bme680_uncomp_field_data *uncomp_data);
static void bme680_packing_calib_param(u8 *a_data_u8, struct bme680_t *bme680);
static void bme680_copy_ordered_sensor_field_data(
struct bme680_uncomp_field_data *sensor_data,
u8 latest, u8 recent, u8 old, u8 sensor_type,
struct bme680_uncomp_field_data *temp_sensor_data);
static void bme680_get_latest_recent_old_field_index(
struct bme680_uncomp_field_data *sensor_data,
struct bme680_t *bme680);
#ifdef BME680_SPECIFIC_FIELD_DATA_READ_ENABLED
static enum bme680_return_type bme680_get_field_specific_uncomp_data(
u8 field_index, u8 sensor_type, u8 *a_data_u8, struct bme680_t *bme680);
static enum bme680_return_type bme680_Temp_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680);
static enum bme680_return_type bme680_Pressure_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680);
static enum bme680_return_type bme680_Humidity_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680);
static enum bme680_return_type bme680_Gas_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680);
#endif
/***************************************************************************
Function definitions
****************************************************************************/
/*!
* @brief This function is used to read the
* the chip id and calibration data of the BME680 sensor
* chip id is read in the register 0xD0/0x50(I2C/SPI) from bit 0 to 7
*
* @param bme680 structure pointer.
*
* @note Structure with the below data to be filled
* before passing to this function.
* @note Device address( applicable only for I2C, bypassed for SPI)
* @note Bus interface functions.
* @note Delay function
* @note With the above data properly set, Chip id will be read from the sensor
*
* @note While changing the parameter of the bme680_t
* @note consider the following point:
* Changing the reference value of the parameter
* will change the local copy or local reference
* make sure your changes will not
* affect the reference value of the parameter
* (Better case don't change the reference value of the parameter)
*
*
*
*
* @return Either the results of bus communication status or
* chip_id fail status
* @retval 0 -> Success both bus communication & chip_id
* @retval any negative value -> Bus communication failed
* -4 -> chip_id corrupted and bus communication ok
*
*
*/
enum bme680_return_type bme680_init(struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
u8 data_u8 = BME680_INIT_VALUE;
/* assign the pointer*/
if (BME680_SPI_INTERFACE == bme680->interface) {
/*SPI address 0x45*/
/* read the chip id*/
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_PAGE0_SPI_ID_REG,
&data_u8,
BME680_GEN_READ_DATA_LENGTH);
} else if (BME680_I2C_INTERFACE == bme680->interface) {
/* read the chip id*/
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_PAGE0_I2C_ID_REG,
&data_u8,
BME680_GEN_READ_DATA_LENGTH);
}
bme680->chip_id = data_u8;
if (BME680_COMM_RES_OK == com_status) {
if (BME680_CHIP_ID == bme680->chip_id) {
/* read the calibration values*/
com_status = bme680_get_calib_param(bme680);
} else {
com_status = BME680_CHIP_ID_ERROR;
}
}
return com_status;
}
/*!
* @brief This function is used to retrieve the calibration
* data from the image registers of the sensor.
*
* @note Registers 8Ah to A1h for calibration data 1 to 24
* from bit 0 to 7
* @note Registers E1h to F0h for calibration data 25 to 40
* from bit 0 to 7
* @param bme680 structure pointer.
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
static enum bme680_return_type bme680_get_calib_param(struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* array of data holding the calibration values*/
u8 v_data_u8 = BME680_INIT_VALUE;
u8 a_data_u8[BME680_CALIB_PARAM_SIZE];
u8 index = BME680_INIT_VALUE;
for (; index < BME680_CALIB_PARAM_SIZE; index++)
a_data_u8[index] = BME680_INIT_VALUE;
/* check the bme680 structure pointer as NULL*/
if (BME680_NULL_PTR == bme680) {
com_status = BME680_ERROR_NULL_PTR;
} else {
if (BME680_SPI_INTERFACE == bme680->interface) {
/* memory page switch the SPI address*/
com_status = bme680_set_memory_page(
BME680_PAGE0_INTERFACE_SPI, bme680);
if (BME680_COMM_RES_OK == com_status) {
/* read the pressure and temperature
calibration data*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_CALIB_SPI_ADDR_1,
a_data_u8,
BME680_CALIB_DATA_LENGTH_GAS);
/* read the humidity and gas
calibration data*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_CALIB_SPI_ADDR_2,
(a_data_u8 +
BME680_CALIB_DATA_LENGTH_GAS),
BME680_CALIB_DATA_LENGTH);
}
} else if (BME680_I2C_INTERFACE == bme680->interface) {
/* read the pressure and temperature
calibration data*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_CALIB_I2C_ADDR_1,
a_data_u8,
BME680_CALIB_DATA_LENGTH_GAS);
/* read the humidity and gas
calibration data*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_CALIB_I2C_ADDR_2,
(a_data_u8 +
BME680_CALIB_DATA_LENGTH_GAS),
BME680_CALIB_DATA_LENGTH);
} else {
com_status = BME680_COMM_RES_ERROR;
}
if (BME680_COMM_RES_OK == com_status) {
/*read TPGH calibration*/
bme680_packing_calib_param(a_data_u8, bme680);
if (BME680_SPI_INTERFACE == bme680->interface) {
/* memory page switch the SPI address*/
com_status = bme680_set_memory_page(BME680_PAGE1_INTERFACE_SPI,
bme680);
}
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_RES_HEAT_RANGE,
&v_data_u8,
BME680_GEN_READ_DATA_LENGTH);
bme680->cal_param.res_heat_range = BME680_GET_REG(v_data_u8,
BME680_MASK_RES_HEAT_RANGE,
BME680_SHIFT_RES_HEAT_RANGE);
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_RES_HEAT_VAL,
&v_data_u8,
BME680_GEN_READ_DATA_LENGTH);
bme680->cal_param.res_heat_val = v_data_u8;
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_RANGE_SWITCHING_ERR,
&v_data_u8,
BME680_GEN_READ_DATA_LENGTH);
bme680->cal_param.range_switching_error = BME680_GET_REG(
(s8)v_data_u8,
(s8)BME680_MASK_RANGE_ERR,
BME680_SHIFT_RANGE_ERR);
}
}
return com_status;
}
/*!
* @brief This function is used to write the data to
* the given register
*
*
* @param addr_u8 -> Address of the register
* @param data_u8 -> The data to write to the register
* @param len_u8 -> No of bytes to write
* @param bme680 structure pointer.
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_write_reg(u8 addr_u8, u8 *data_u8, u8 len_u8,
struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* check the bme680 is NULL pointer */
if (BME680_NULL_PTR == bme680) {
com_status = BME680_ERROR_NULL_PTR;
} else {
com_status = (enum bme680_return_type)bme680->bme680_bus_write(
bme680->dev_addr,
addr_u8,
data_u8,
len_u8);
}
return com_status;
}
/*!
* @brief This function is used to reads the data from
* the given register
*
*
* @param addr_u8 -> Address of the register
* @param data_u8 -> Pointer to store the
* received data from the register
* @param len_u8 -> No of bytes to read
* @param bme680 structure pointer.
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_read_reg(u8 addr_u8, u8 *data_u8, u8 len_u8,
struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* check the bme680 is NULL pointer */
if (BME680_NULL_PTR == bme680) {
com_status = BME680_ERROR_NULL_PTR;
} else {
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(bme680->dev_addr,
addr_u8,
data_u8,
len_u8);
}
return com_status;
}
/*!
* @brief This function is used to read the new data0
* @note Field-0(new_data_0),
* Field-1(new_data_1) and Field-2(new_data_2)
* @note Page-1
*
*
* @param new_data_u8: The value of new data
* @param field_u8: The value of field selection for new data
* field | value
* -----------|-------------
* 0 | BME680_FIELD_ZERO
* 1 | BME680_FIELD_ONE
* 2 | BME680_FIELD_TWO
*
* field | Register
* -------------------|------------
* BME680_FIELD_ZERO | 0x1D bit 7
* BME680_FIELD_ONE | 0x2E bit 7
* BME680_FIELD_TWO | 0x3F bit 7
*
*
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_get_new_data(u8 *new_data_u8, u8 field_u8,
struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* check the bme680 is NULL pointer */
if (BME680_NULL_PTR == bme680) {
com_status = BME680_ERROR_NULL_PTR;
} else {
if (BME680_SPI_INTERFACE == bme680->interface) {
/* memory page switch the SPI address*/
com_status = bme680_set_memory_page(
BME680_PAGE1_INTERFACE_SPI,
bme680);
}
if (BME680_I2C_INTERFACE == bme680->interface)
com_status = BME680_COMM_RES_OK;
if (BME680_COMM_RES_OK == com_status) {
switch (field_u8) {
case BME680_FIELD_ZERO:
/* read field0 new data zero*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0,
new_data_u8,
BME680_GEN_READ_DATA_LENGTH);
break;
case BME680_FIELD_ONE:
/* read field1 new data one*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
(BME680_ADDR_FIELD_0 +
BME680_FIELD_ONE_OFFSET),
new_data_u8,
BME680_GEN_READ_DATA_LENGTH);
break;
case BME680_FIELD_TWO:
/* read field2 new data two*/
com_status =
(enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
(BME680_ADDR_FIELD_0 +
BME680_FIELD_TWO_OFFSET),
new_data_u8,
BME680_GEN_READ_DATA_LENGTH);
break;
default:
com_status = BME680_COMM_RES_ERROR;
break;
}
if (BME680_COMM_RES_OK == com_status)
*new_data_u8 = BME680_GET_REG(*new_data_u8,
BME680_MASK_NEW_DATA,
BME680_SHIFT_NEW_DATA);
}
}
return com_status;
}
/*!
* @brief This function is used to read the uncompensated
* sensor data from Field-0, Field-1, Field-2 and page-1
*
* @param uncomp_data:
* Pointer to store the value of uncompensated sensor
* data of pressure, temperature, humidity and gas
*
* @param field_count : total no of field data which needs
* to be read from the sensor
*
* @note:
* field_count = 1 : only latest field data out of 3 fields
* field_count = 2 : latest and recent field data out of 3 field
* field_count = 3 : All 3 latest, recent and old field data
*
* @param sensor_type : Type of sensor
* e.g; BME680_PRESSURE,BME680_TEMPERATURE,BME680_HUMIDITY
* BME680_GAS,BME680_ALL
*
* @note: if "BME680_SPECIFIC_FIELD_DATA_READ_ENABLED" is not defined in
* bme680.h then for any sensor_type function will perform
* read operation for BME680_ALL.
*
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
* @note error code is returned when data readout is attempted
* in sleep mode or when field_count is not in the below range
* it must be 1<= field_count <= 3
*
*/
enum bme680_return_type bme680_get_uncomp_data(
struct bme680_uncomp_field_data *uncomp_data, u8 field_count,
u8 sensor_type, struct bme680_t *bme680)
{
/* used to return the communication result*/
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
u8 index = BME680_INIT_VALUE;
u8 a_data_u8[BME680_LEN_ALL_FIELD_SIZE];
struct bme680_uncomp_field_data temp_sensor_data[BME680_THREE];
#ifdef BME680_SPECIFIC_FIELD_DATA_READ_ENABLED
/*Array to store the new_data status of all 3 fields*/
u8 new_data[BME680_THREE] = {BME680_INIT_VALUE, BME680_INIT_VALUE,
BME680_INIT_VALUE};
#endif
/*clear the the latest, recent and old field index*/
bme680->latest_field_index = BME680_INIT_VALUE;
bme680->recent_field_index = BME680_INIT_VALUE;
bme680->old_field_index = BME680_INIT_VALUE;
if ((field_count < BME680_PRESENT_DATA_FIELD
|| field_count > BME680_ALL_DATA_FIELD)
|| (BME680_SLEEP_MODE == bme680->last_set_mode)) {
com_status = BME680_COMM_RES_ERROR;
} else {
com_status = BME680_COMM_RES_OK;
}
if (BME680_COMM_RES_OK == com_status) {
#ifndef BME680_SPECIFIC_FIELD_DATA_READ_ENABLED
sensor_type = BME680_ALL;
field_count = BME680_ALL_DATA_FIELD;
#endif
if (BME680_FORCED_MODE == bme680->last_set_mode) {
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0, a_data_u8,
BME680_SINGLE_FIELD_LENGTH);
field_count = BME680_PRESENT_DATA_FIELD;
} else {
#ifdef BME680_SPECIFIC_FIELD_DATA_READ_ENABLED
/*read status field of all 3 filed and extract the new_data
flag status.*/
com_status = bme680_read_status_fields(uncomp_data, a_data_u8,
new_data, bme680);
/*get the latest, recent and old field index*/
bme680_get_latest_recent_old_field_index(uncomp_data, bme680);
/*By default read latest field data */
if (BME680_TRUE == new_data[bme680->latest_field_index]) {
com_status =
bme680_get_field_specific_uncomp_data(
bme680->latest_field_index,
sensor_type,
a_data_u8,
bme680);
}
if (BME680_PRESENT_AND_PREVIOUS_DATA_FIELD == field_count) {
/* read recent field data */
if (BME680_TRUE ==
new_data[bme680->recent_field_index]) {
com_status =
bme680_get_field_specific_uncomp_data(
bme680->recent_field_index,
sensor_type,
a_data_u8,
bme680);
}
} else if (BME680_ALL_DATA_FIELD == field_count) {
/* read recent field data */
if (BME680_TRUE ==
new_data[bme680->recent_field_index]) {
com_status =
bme680_get_field_specific_uncomp_data(
bme680->recent_field_index,
sensor_type,
a_data_u8,
bme680);
}
/* read old field data */
if (BME680_TRUE ==
new_data[bme680->old_field_index]) {
com_status =
bme680_get_field_specific_uncomp_data(
bme680->old_field_index,
sensor_type,
a_data_u8,
bme680);
}
}
#else
if (BME680_ALL == sensor_type) {
/*read uncompensated sensor data of field 0,1,2*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0, a_data_u8,
BME680_LEN_ALL_FIELD_SIZE);
}
(uncomp_data + 0)->status.meas_index = a_data_u8[1];
(uncomp_data + 1)->status.meas_index = a_data_u8[18];
(uncomp_data + 2)->status.meas_index = a_data_u8[35];
/*get the latest, recent and old field index*/
bme680_get_latest_recent_old_field_index(uncomp_data, bme680);
#endif
}
if (BME680_COMM_RES_OK == com_status) {
bme680_align_uncomp_data(a_data_u8,
field_count,
sensor_type,
uncomp_data,
bme680);
if (BME680_FORCED_MODE != bme680->last_set_mode) {
for (index = BME680_INIT_VALUE; index <
BME680_ALL_DATA_FIELD; index++)
temp_sensor_data[index] =
*(uncomp_data + index);
bme680_copy_ordered_sensor_field_data(
uncomp_data, bme680->latest_field_index,
bme680->recent_field_index,
bme680->old_field_index, sensor_type,
temp_sensor_data);
}
}
}
return com_status;
}
#ifdef BME680_SPECIFIC_FIELD_DATA_READ_ENABLED
/*!
* @brief This function is used to read the uncompensated
* data according to sensor type
* @note Field-0, Field-1 and Field-2
* @note Page-1
*
*
* @param field_index : index of the field which needs
* to be read from the sensor
*
* @param sensor_type : Type of sensor
* e.g; BME680_PRESSURE,BME680_TEMPERATURE,BME680_HUMIDITY
* BME680_GAS,BME680_ALL
*
* @param a_data_u8 : pointer to store read data.
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_get_field_specific_uncomp_data(
u8 field_index, u8 sensor_type, u8 *a_data_u8, struct bme680_t *bme680)
{
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
if (BME680_PRESSURE == sensor_type ||
BME680_TEMPERATURE == sensor_type ||
BME680_HUMIDITY == sensor_type) {
com_status = bme680_Temp_field_specific_uncomp_read(
field_index,
a_data_u8,
bme680);
switch (sensor_type) {
case BME680_PRESSURE:
com_status = bme680_Pressure_field_specific_uncomp_read(
field_index,
a_data_u8,
bme680);
break;
case BME680_HUMIDITY:
com_status = bme680_Humidity_field_specific_uncomp_read(
field_index,
a_data_u8,
bme680);
break;
}
} else if (BME680_GAS == sensor_type) {
com_status = bme680_Gas_field_specific_uncomp_read(field_index,
a_data_u8,
bme680);
} else if (BME680_ALL == sensor_type) {
/*read uncompensated sensor data of field 0,1,2*/
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0,
a_data_u8,
BME680_LEN_ALL_FIELD_SIZE);
}
return com_status;
}
/*!
* @brief This function is used to read the uncompensated
* Temperature for specific Field type.
* Field-0, Field-1 and Field-2
*
* @param field_index : index of the field which needs
* to be read from the sensor
*
* @param a_data_u8 : pointer to store read data.
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_Temp_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680)
{
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* local buffer length is 5 and it's the maximum */
u8 temp_data_u8[BME680_THREE];
u8 count = BME680_INIT_VALUE;
for (count = BME680_INIT_VALUE; count < BME680_THREE; count++)
temp_data_u8[count] = BME680_INIT_VALUE;
/*read uncompensated Temperature of field 0*/
if (BME680_FIELD_INDEX0 == field_index) {
/*read the 3 byte of T1 data form 0x22*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0_TEMP1,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
5,6 & 7 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[5 + count] = temp_data_u8[count];
/*read the 3 byte of T2 data form 0x27*/
com_status = (enum bme680_return_type)bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0_TEMP2,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
10,11 & 12 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[10 + count] = temp_data_u8[count];
/*read uncompensated Temperature of field 1*/
} else if (BME680_FIELD_INDEX1 == field_index) {
/*read the 3 byte of T1 data form 0x33*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_1_TEMP1,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
22,23 & 24 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[22 + count] = temp_data_u8[count];
/*read the 3 byte of T2 data form 0x38*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_1_TEMP2,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
27,28 & 29 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[27 + count] = temp_data_u8[count];
/*read uncompensated Temperature of field 2*/
} else if (BME680_FIELD_INDEX2 == field_index) {
/*read the 3 byte of T1 data form 0x44*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_2_TEMP1,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
39,40 & 41 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[39 + count] = temp_data_u8[count];
/*read the 3 byte of T2 data form 0x49*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_2_TEMP2,
temp_data_u8,
BME680_TEMPERATURE_DATA_LEN);
/*Assign data to the reserved index
44,45 & 46 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_TEMPERATURE_DATA_LEN; count++)
a_data_u8[44 + count] = temp_data_u8[count];
}
return com_status;
}
/*!
* @brief This function is used to read the uncompensated
* Pressure for specific Field type.
* Field-0, Field-1 and Field-2
*
* @param field_index : index of the field which needs
* to be read from the sensor
*
*
* @param a_data_u8 : pointer to store read data.
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_Pressure_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680)
{
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* local buffer length is 5 and it's the maximum */
u8 temp_data_u8[BME680_THREE];
u8 count = BME680_INIT_VALUE;
for (count = BME680_INIT_VALUE; count < BME680_THREE; count++)
temp_data_u8[count] = BME680_INIT_VALUE;
/*read uncompensated Pressure of field 0*/
if (BME680_FIELD_INDEX0 == field_index) {
/*read the 3 byte of P data form 0x1F*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0_PRESS,
temp_data_u8,
BME680_PRESSURE_DATA_LEN);
/*Assign data to the reserved index
2,3 & 4 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_PRESSURE_DATA_LEN; count++)
a_data_u8[2 + count] = temp_data_u8[count];
/*read uncompensated Pressure of field 1*/
} else if (BME680_FIELD_INDEX1 == field_index) {
/*read the 3 byte of P data
form 0x30*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_1_PRESS,
temp_data_u8,
BME680_PRESSURE_DATA_LEN);
/*Assign data to the
reserved index
19,20 & 21 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_PRESSURE_DATA_LEN; count++)
a_data_u8[19 + count] = temp_data_u8[count];
/*read uncompensated Pressure of field 2*/
} else if (BME680_FIELD_INDEX2 == field_index) {
/*read the 3 byte of P data
form 0x41*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_2_PRESS,
temp_data_u8,
BME680_PRESSURE_DATA_LEN);
/*Assign data to the reserved
index 36,37 & 38 of the input
buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_PRESSURE_DATA_LEN; count++)
a_data_u8[36 + count] = temp_data_u8[count];
}
return com_status;
}
/*!
* @brief This function is used to read the uncompensated
* Humidity for specific Field type.
* Field-0, Field-1 and Field-2
*
* @param field_index : index of the field which needs
* to be read from the sensor
*
*
* @param a_data_u8 : pointer to store read data.
* @param bme680 structure pointer.
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval any negative value -> Error
*
*
*/
enum bme680_return_type bme680_Humidity_field_specific_uncomp_read(
u8 field_index, u8 *a_data_u8, struct bme680_t *bme680)
{
enum bme680_return_type com_status = BME680_COMM_RES_ERROR;
/* local buffer length is 5 and it's the maximum */
u8 temp_data_u8[BME680_TWO];
u8 count = BME680_INIT_VALUE;
for (count = BME680_INIT_VALUE; count < BME680_TWO; count++)
temp_data_u8[count] = BME680_INIT_VALUE;
/*read uncompensated Humidity of field 0*/
if (BME680_FIELD_INDEX0 == field_index) {
/*read the 2 byte of H data form 0x25*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_0_HUM,
temp_data_u8,
BME680_HUMIDITY_DATA_LEN);
/*Assign data to the reserved index
8 & 9 of the input buffer*/
for (count = BME680_INIT_VALUE;
count < BME680_HUMIDITY_DATA_LEN; count++)
a_data_u8[8 + count] = temp_data_u8[count];
/*read uncompensated Humidity of field 1*/
} else if (BME680_FIELD_INDEX1 == field_index) {
/*read the 2 byte of H data form 0x36*/
com_status = (enum bme680_return_type)
bme680->bme680_bus_read(
bme680->dev_addr,
BME680_ADDR_FIELD_1_HUM,
temp_data_u8,
BME680_HUMIDITY_DATA_LEN);
/*Assign data to the reserved index
25 & 26 of the input buffer*/