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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
|
/*
* /\
* / \
* / !! \
* /______\
*
* In order to use the I2C interface, CSB needs to be pulled up before turning
* on VDDIO (and after VDD). On most BME280 breakout boards sold on
* AliExpress and similar sites, VDD and VDDIO are connected and there is only
* one external VCC input, so following the power sequence outlined in the
* datasheet is not possible. Additionally, the pull-up resistor connecting
* CSB to VCC may delay logic high level on CSB long enough for the BM280
* to start in SPI mode.
*
* In this case, you should connect (or power up, when using GPIO power)
* breakout board pins in the following order:
* * GND, SDA, SCLD
* * CSB to 3V3
* * VDD to 3V3
*/
/**\mainpage
* Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
*
* 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 bme280.c
* Date 26 Aug 2019
* Version 3.3.7
*
*/
/*! @file bme280.c
* @brief Sensor driver for BME280 sensor
*/
#include "driver/bme280.h"
/**\name Internal macros */
/* To identify osr settings selected by user */
#define OVERSAMPLING_SETTINGS UINT8_C(0x07)
/* To identify filter and standby settings selected by user */
#define FILTER_STANDBY_SETTINGS UINT8_C(0x18)
/****************** Global Function Definitions *******************************/
/*!
* @brief This API is the entry point.
* It reads the chip-id and calibration data from the sensor.
*/
int8_t BME280::init()
{
int8_t rslt;
/* chip id read try count */
uint8_t try_count = 5;
uint8_t chip_id = 0;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Proceed if null check is fine */
if (rslt == BME280_OK)
{
while (try_count)
{
/* Read the chip-id of bme280 sensor */
rslt = getRegs(BME280_CHIP_ID_ADDR, &chip_id, 1);
/* Check for chip id validity */
if ((rslt == BME280_OK) && (chip_id == BME280_CHIP_ID))
{
/* Reset the sensor */
rslt = softReset();
if (rslt == BME280_OK)
{
/* Read the calibration data */
rslt = get_calib_data();
}
break;
}
/* Wait for 1 ms */
delay_ms(1);
--try_count;
}
/* Chip id check failed */
if (!try_count)
{
rslt = BME280_E_DEV_NOT_FOUND;
}
}
return rslt;
}
/*!
* @brief This API reads the data from the given register address of the sensor.
*/
int8_t BME280::getRegs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Proceed if null check is fine */
if (rslt == BME280_OK)
{
/* If interface selected is SPI */
if (intf != BME280_I2C_INTF)
{
reg_addr = reg_addr | 0x80;
}
/* Read the data */
rslt = read(dev_id, reg_addr, reg_data, len);
/* Check for communication error */
if (rslt != BME280_OK)
{
rslt = BME280_E_COMM_FAIL;
}
}
return rslt;
}
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*/
int8_t BME280::setRegs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len)
{
int8_t rslt;
uint8_t temp_buff[20]; /* Typically not to write more than 10 registers */
if (len > 10)
{
len = 10;
}
uint16_t temp_len;
uint8_t reg_addr_cnt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Check for arguments validity */
if ((rslt == BME280_OK) && (reg_addr != NULL) && (reg_data != NULL))
{
if (len != 0)
{
temp_buff[0] = reg_data[0];
/* If interface selected is SPI */
if (intf != BME280_I2C_INTF)
{
for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)
{
reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;
}
}
/* Burst write mode */
if (len > 1)
{
/* Interleave register address w.r.t data for
* burst write
*/
interleave_reg_addr(reg_addr, temp_buff, reg_data, len);
temp_len = ((len * 2) - 1);
}
else
{
temp_len = len;
}
rslt = write(dev_id, reg_addr[0], temp_buff, temp_len);
/* Check for communication error */
if (rslt != BME280_OK)
{
rslt = BME280_E_COMM_FAIL;
}
}
else
{
rslt = BME280_E_INVALID_LEN;
}
}
else
{
rslt = BME280_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API sets the oversampling, filter and standby duration
* (normal mode) settings in the sensor.
*/
int8_t BME280::setSensorSettings(uint8_t desired_settings)
{
int8_t rslt;
uint8_t sensor_mode;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Proceed if null check is fine */
if (rslt == BME280_OK)
{
rslt = getSensorMode(&sensor_mode);
if ((rslt == BME280_OK) && (sensor_mode != BME280_SLEEP_MODE))
{
rslt = put_device_to_sleep();
}
if (rslt == BME280_OK)
{
/* Check if user wants to change oversampling
* settings
*/
if (are_settings_changed(OVERSAMPLING_SETTINGS, desired_settings))
{
rslt = set_osr_settings(desired_settings, &settings);
}
/* Check if user wants to change filter and/or
* standby settings
*/
if ((rslt == BME280_OK) && are_settings_changed(FILTER_STANDBY_SETTINGS, desired_settings))
{
rslt = set_filter_standby_settings(desired_settings, &settings);
}
}
}
return rslt;
}
/*!
* @brief This API gets the oversampling, filter and standby duration
* (normal mode) settings from the sensor.
*/
int8_t BME280::getSensorSettings()
{
int8_t rslt;
uint8_t reg_data[4];
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Proceed if null check is fine */
if (rslt == BME280_OK)
{
rslt = getRegs(BME280_CTRL_HUM_ADDR, reg_data, 4);
if (rslt == BME280_OK)
{
parse_device_settings(reg_data, &settings);
}
}
return rslt;
}
/*!
* @brief This API sets the power mode of the sensor.
*/
int8_t BME280::setSensorMode(uint8_t sensor_mode)
{
int8_t rslt;
uint8_t last_set_mode;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
if (rslt == BME280_OK)
{
rslt = getSensorMode(&last_set_mode);
/* If the sensor is not in sleep mode put the device to sleep
* mode
*/
if ((rslt == BME280_OK) && (last_set_mode != BME280_SLEEP_MODE))
{
rslt = put_device_to_sleep();
}
/* Set the power mode */
if (rslt == BME280_OK)
{
rslt = write_power_mode(sensor_mode);
}
}
return rslt;
}
/*!
* @brief This API gets the power mode of the sensor.
*/
int8_t BME280::getSensorMode(uint8_t *sensor_mode)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
if (rslt == BME280_OK)
{
/* Read the power mode register */
rslt = getRegs(BME280_PWR_CTRL_ADDR, sensor_mode, 1);
/* Assign the power mode in the device structure */
*sensor_mode = BME280_GET_BITS_POS_0(*sensor_mode, BME280_SENSOR_MODE);
}
return rslt;
}
/*!
* @brief This API performs the soft reset of the sensor.
*/
int8_t BME280::softReset()
{
int8_t rslt;
uint8_t reg_addr = BME280_RESET_ADDR;
uint8_t status_reg = 0;
uint8_t try_run = 5;
/* 0xB6 is the soft reset command */
uint8_t soft_rst_cmd = BME280_SOFT_RESET_COMMAND;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
/* Proceed if null check is fine */
if (rslt == BME280_OK)
{
/* Write the soft reset command in the sensor */
rslt = setRegs(®_addr, &soft_rst_cmd, 1);
if (rslt == BME280_OK)
{
/* If NVM not copied yet, Wait for NVM to copy */
do
{
/* As per data sheet - Table 1, startup time is 2 ms. */
delay_ms(2);
rslt = getRegs(BME280_STATUS_REG_ADDR, &status_reg, 1);
} while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));
if (status_reg & BME280_STATUS_IM_UPDATE)
{
rslt = BME280_E_NVM_COPY_FAILED;
}
}
}
return rslt;
}
/*!
* @brief This API reads the pressure, temperature and humidity data from the
* sensor, compensates the data and store it in the bme280_data structure
* instance passed by the user.
*/
int8_t BME280::getSensorData(uint8_t sensor_comp, struct bme280_data *comp_data)
{
int8_t rslt;
/* Array to store the pressure, temperature and humidity data read from
* the sensor
*/
uint8_t reg_data[BME280_P_T_H_DATA_LEN] = { 0 };
struct bme280_uncomp_data uncomp_data = { 0, 0, 0 };
/* Check for null pointer in the device structure*/
rslt = null_ptr_check();
if ((rslt == BME280_OK) && (comp_data != NULL))
{
/* Read the pressure and temperature data from the sensor */
rslt = getRegs(BME280_DATA_ADDR, reg_data, BME280_P_T_H_DATA_LEN);
if (rslt == BME280_OK)
{
/* Parse the read data from the sensor */
parseSensorData(reg_data, &uncomp_data);
/* Compensate the pressure and/or temperature and/or
* humidity data from the sensor
*/
rslt = compensateSensorData(sensor_comp, &uncomp_data, comp_data, &calib_data);
}
}
else
{
rslt = BME280_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This API is used to parse the pressure, temperature and
* humidity data and store it in the bme280_uncomp_data structure instance.
*/
void BME280::parseSensorData(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data)
{
/* Variables to store the sensor data */
uint32_t data_xlsb;
uint32_t data_lsb;
uint32_t data_msb;
/* Store the parsed register values for pressure data */
data_msb = (uint32_t)reg_data[0] << 12;
data_lsb = (uint32_t)reg_data[1] << 4;
data_xlsb = (uint32_t)reg_data[2] >> 4;
uncomp_data->pressure = data_msb | data_lsb | data_xlsb;
/* Store the parsed register values for temperature data */
data_msb = (uint32_t)reg_data[3] << 12;
data_lsb = (uint32_t)reg_data[4] << 4;
data_xlsb = (uint32_t)reg_data[5] >> 4;
uncomp_data->temperature = data_msb | data_lsb | data_xlsb;
/* Store the parsed register values for temperature data */
data_lsb = (uint32_t)reg_data[6] << 8;
data_msb = (uint32_t)reg_data[7];
uncomp_data->humidity = data_msb | data_lsb;
}
/*!
* @brief This API is used to compensate the pressure and/or
* temperature and/or humidity data according to the component selected
* by the user.
*/
int8_t BME280::compensateSensorData(uint8_t sensor_comp,
const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data,
struct bme280_calib_data *calib_data)
{
int8_t rslt = BME280_OK;
if ((uncomp_data != NULL) && (comp_data != NULL) && (calib_data != NULL))
{
/* Initialize to zero */
comp_data->temperature = 0;
comp_data->pressure = 0;
comp_data->humidity = 0;
/* If pressure or temperature component is selected */
if (sensor_comp & (BME280_PRESS | BME280_TEMP | BME280_HUM))
{
/* Compensate the temperature data */
comp_data->temperature = compensate_temperature(uncomp_data, calib_data);
}
if (sensor_comp & BME280_PRESS)
{
/* Compensate the pressure data */
comp_data->pressure = compensate_pressure(uncomp_data, calib_data);
}
if (sensor_comp & BME280_HUM)
{
/* Compensate the humidity data */
comp_data->humidity = compensate_humidity(uncomp_data, calib_data);
}
}
else
{
rslt = BME280_E_NULL_PTR;
}
return rslt;
}
/*!
* @brief This internal API sets the oversampling settings for pressure,
* temperature and humidity in the sensor.
*/
int8_t BME280::set_osr_settings(uint8_t desired_settings,
const struct bme280_settings *settings)
{
int8_t rslt = BME280_W_INVALID_OSR_MACRO;
if (desired_settings & BME280_OSR_HUM_SEL)
{
rslt = set_osr_humidity_settings(settings);
}
if (desired_settings & (BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL))
{
rslt = set_osr_press_temp_settings(desired_settings, settings);
}
return rslt;
}
/*!
* @brief This API sets the humidity oversampling settings of the sensor.
*/
int8_t BME280::set_osr_humidity_settings(const struct bme280_settings *settings)
{
int8_t rslt;
uint8_t ctrl_hum;
uint8_t ctrl_meas;
uint8_t reg_addr = BME280_CTRL_HUM_ADDR;
ctrl_hum = settings->osr_h & BME280_CTRL_HUM_MSK;
/* Write the humidity control value in the register */
rslt = setRegs(®_addr, &ctrl_hum, 1);
/* Humidity related changes will be only effective after a
* write operation to ctrl_meas register
*/
if (rslt == BME280_OK)
{
reg_addr = BME280_CTRL_MEAS_ADDR;
rslt = getRegs(reg_addr, &ctrl_meas, 1);
if (rslt == BME280_OK)
{
rslt = setRegs(®_addr, &ctrl_meas, 1);
}
}
return rslt;
}
/*!
* @brief This API sets the pressure and/or temperature oversampling settings
* in the sensor according to the settings selected by the user.
*/
int8_t BME280::set_osr_press_temp_settings(uint8_t desired_settings,
const struct bme280_settings *settings)
{
int8_t rslt;
uint8_t reg_addr = BME280_CTRL_MEAS_ADDR;
uint8_t reg_data;
rslt = BME280::getRegs(reg_addr, ®_data, 1);
if (rslt == BME280_OK)
{
if (desired_settings & BME280_OSR_PRESS_SEL)
{
fill_osr_press_settings(®_data, settings);
}
if (desired_settings & BME280_OSR_TEMP_SEL)
{
fill_osr_temp_settings(®_data, settings);
}
/* Write the oversampling settings in the register */
rslt = BME280::setRegs(®_addr, ®_data, 1);
}
return rslt;
}
/*!
* @brief This internal API sets the filter and/or standby duration settings
* in the sensor according to the settings selected by the user.
*/
int8_t BME280::set_filter_standby_settings(uint8_t desired_settings,
const struct bme280_settings *settings)
{
int8_t rslt;
uint8_t reg_addr = BME280_CONFIG_ADDR;
uint8_t reg_data;
rslt = getRegs(reg_addr, ®_data, 1);
if (rslt == BME280_OK)
{
if (desired_settings & BME280_FILTER_SEL)
{
fill_filter_settings(®_data, settings);
}
if (desired_settings & BME280_STANDBY_SEL)
{
fill_standby_settings(®_data, settings);
}
/* Write the oversampling settings in the register */
rslt = setRegs(®_addr, ®_data, 1);
}
return rslt;
}
/*!
* @brief This internal API fills the filter settings provided by the user
* in the data buffer so as to write in the sensor.
*/
void BME280::fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings)
{
*reg_data = BME280_SET_BITS(*reg_data, BME280_FILTER, settings->filter);
}
/*!
* @brief This internal API fills the standby duration settings provided by
* the user in the data buffer so as to write in the sensor.
*/
void BME280::fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings)
{
*reg_data = BME280_SET_BITS(*reg_data, BME280_STANDBY, settings->standby_time);
}
/*!
* @brief This internal API fills the pressure oversampling settings provided by
* the user in the data buffer so as to write in the sensor.
*/
void BME280::fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings)
{
*reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_PRESS, settings->osr_p);
}
/*!
* @brief This internal API fills the temperature oversampling settings
* provided by the user in the data buffer so as to write in the sensor.
*/
void BME280::fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings)
{
*reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_TEMP, settings->osr_t);
}
/*!
* @brief This internal API parse the oversampling(pressure, temperature
* and humidity), filter and standby duration settings and store in the
* device structure.
*/
void BME280::parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings)
{
settings->osr_h = BME280_GET_BITS_POS_0(reg_data[0], BME280_CTRL_HUM);
settings->osr_p = BME280_GET_BITS(reg_data[2], BME280_CTRL_PRESS);
settings->osr_t = BME280_GET_BITS(reg_data[2], BME280_CTRL_TEMP);
settings->filter = BME280_GET_BITS(reg_data[3], BME280_FILTER);
settings->standby_time = BME280_GET_BITS(reg_data[3], BME280_STANDBY);
}
/*!
* @brief This internal API writes the power mode in the sensor.
*/
int8_t BME280::write_power_mode(uint8_t sensor_mode)
{
int8_t rslt;
uint8_t reg_addr = BME280_PWR_CTRL_ADDR;
/* Variable to store the value read from power mode register */
uint8_t sensor_mode_reg_val;
/* Read the power mode register */
rslt = getRegs(reg_addr, &sensor_mode_reg_val, 1);
/* Set the power mode */
if (rslt == BME280_OK)
{
sensor_mode_reg_val = BME280_SET_BITS_POS_0(sensor_mode_reg_val, BME280_SENSOR_MODE, sensor_mode);
/* Write the power mode in the register */
rslt = setRegs(®_addr, &sensor_mode_reg_val, 1);
}
return rslt;
}
/*!
* @brief This internal API puts the device to sleep mode.
*/
int8_t BME280::put_device_to_sleep()
{
int8_t rslt;
uint8_t reg_data[4];
struct bme280_settings settings;
rslt = getRegs(BME280_CTRL_HUM_ADDR, reg_data, 4);
if (rslt == BME280_OK)
{
parse_device_settings(reg_data, &settings);
rslt = softReset();
if (rslt == BME280_OK)
{
rslt = reload_device_settings(&settings);
}
}
return rslt;
}
/*!
* @brief This internal API reloads the already existing device settings in
* the sensor after soft reset.
*/
int8_t BME280::reload_device_settings(const struct bme280_settings *settings)
{
int8_t rslt;
rslt = set_osr_settings(BME280_ALL_SETTINGS_SEL, settings);
if (rslt == BME280_OK)
{
rslt = set_filter_standby_settings(BME280_ALL_SETTINGS_SEL, settings);
}
return rslt;
}
#ifdef BME280_FLOAT_ENABLE
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data in double data type.
*/
double BME280::compensate_temperature(const struct bme280_uncomp_data *uncomp_data, struct bme280_calib_data *calib_data)
{
double var1;
double var2;
double temperature;
double temperature_min = -40;
double temperature_max = 85;
var1 = ((double)uncomp_data->temperature) / 16384.0 - ((double)calib_data->dig_T1) / 1024.0;
var1 = var1 * ((double)calib_data->dig_T2);
var2 = (((double)uncomp_data->temperature) / 131072.0 - ((double)calib_data->dig_T1) / 8192.0);
var2 = (var2 * var2) * ((double)calib_data->dig_T3);
calib_data->t_fine = (int32_t)(var1 + var2);
temperature = (var1 + var2) / 5120.0;
if (temperature < temperature_min)
{
temperature = temperature_min;
}
else if (temperature > temperature_max)
{
temperature = temperature_max;
}
return temperature;
}
/*!
* @brief This internal API is used to compensate the raw pressure data and
* return the compensated pressure data in double data type.
*/
double BME280::compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data)
{
double var1;
double var2;
double var3;
double pressure;
double pressure_min = 30000.0;
double pressure_max = 110000.0;
var1 = ((double)calib_data->t_fine / 2.0) - 64000.0;
var2 = var1 * var1 * ((double)calib_data->dig_P6) / 32768.0;
var2 = var2 + var1 * ((double)calib_data->dig_P5) * 2.0;
var2 = (var2 / 4.0) + (((double)calib_data->dig_P4) * 65536.0);
var3 = ((double)calib_data->dig_P3) * var1 * var1 / 524288.0;
var1 = (var3 + ((double)calib_data->dig_P2) * var1) / 524288.0;
var1 = (1.0 + var1 / 32768.0) * ((double)calib_data->dig_P1);
/* avoid exception caused by division by zero */
if (var1)
{
pressure = 1048576.0 - (double) uncomp_data->pressure;
pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((double)calib_data->dig_P9) * pressure * pressure / 2147483648.0;
var2 = pressure * ((double)calib_data->dig_P8) / 32768.0;
pressure = pressure + (var1 + var2 + ((double)calib_data->dig_P7)) / 16.0;
if (pressure < pressure_min)
{
pressure = pressure_min;
}
else if (pressure > pressure_max)
{
pressure = pressure_max;
}
}
else /* Invalid case */
{
pressure = pressure_min;
}
return pressure;
}
/*!
* @brief This internal API is used to compensate the raw humidity data and
* return the compensated humidity data in double data type.
*/
double BME280::compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data)
{
double humidity;
double humidity_min = 0.0;
double humidity_max = 100.0;
double var1;
double var2;
double var3;
double var4;
double var5;
double var6;
var1 = ((double)calib_data->t_fine) - 76800.0;
var2 = (((double)calib_data->dig_H4) * 64.0 + (((double)calib_data->dig_H5) / 16384.0) * var1);
var3 = uncomp_data->humidity - var2;
var4 = ((double)calib_data->dig_H2) / 65536.0;
var5 = (1.0 + (((double)calib_data->dig_H3) / 67108864.0) * var1);
var6 = 1.0 + (((double)calib_data->dig_H6) / 67108864.0) * var1 * var5;
var6 = var3 * var4 * (var5 * var6);
humidity = var6 * (1.0 - ((double)calib_data->dig_H1) * var6 / 524288.0);
if (humidity > humidity_max)
{
humidity = humidity_max;
}
else if (humidity < humidity_min)
{
humidity = humidity_min;
}
return humidity;
}
#else
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data in integer data type.
*/
int32_t BME280::compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
struct bme280_calib_data *calib_data)
{
int32_t var1;
int32_t var2;
int32_t temperature;
int32_t temperature_min = -4000;
int32_t temperature_max = 8500;
var1 = (int32_t)((uncomp_data->temperature / 8) - ((int32_t)calib_data->dig_T1 * 2));
var1 = (var1 * ((int32_t)calib_data->dig_T2)) / 2048;
var2 = (int32_t)((uncomp_data->temperature / 16) - ((int32_t)calib_data->dig_T1));
var2 = (((var2 * var2) / 4096) * ((int32_t)calib_data->dig_T3)) / 16384;
calib_data->t_fine = var1 + var2;
temperature = (calib_data->t_fine * 5 + 128) / 256;
if (temperature < temperature_min)
{
temperature = temperature_min;
}
else if (temperature > temperature_max)
{
temperature = temperature_max;
}
return temperature;
}
#ifdef BME280_64BIT_ENABLE
/*!
* @brief This internal API is used to compensate the raw pressure data and
* return the compensated pressure data in integer data type with higher
* accuracy.
*/
uint32_t BME280::compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data)
{
int64_t var1;
int64_t var2;
int64_t var3;
int64_t var4;
uint32_t pressure;
uint32_t pressure_min = 3000000;
uint32_t pressure_max = 11000000;
var1 = ((int64_t)calib_data->t_fine) - 128000;
var2 = var1 * var1 * (int64_t)calib_data->dig_P6;
var2 = var2 + ((var1 * (int64_t)calib_data->dig_P5) * 131072);
var2 = var2 + (((int64_t)calib_data->dig_P4) * 34359738368);
var1 = ((var1 * var1 * (int64_t)calib_data->dig_P3) / 256) + ((var1 * ((int64_t)calib_data->dig_P2) * 4096));
var3 = ((int64_t)1) * 140737488355328;
var1 = (var3 + var1) * ((int64_t)calib_data->dig_P1) / 8589934592;
/* To avoid divide by zero exception */
if (var1 != 0)
{
var4 = 1048576 - uncomp_data->pressure;
var4 = (((var4 * INT64_C(2147483648)) - var2) * 3125) / var1;
var1 = (((int64_t)calib_data->dig_P9) * (var4 / 8192) * (var4 / 8192)) / 33554432;
var2 = (((int64_t)calib_data->dig_P8) * var4) / 524288;
var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_P7) * 16);
pressure = (uint32_t)(((var4 / 2) * 100) / 128);
if (pressure < pressure_min)
{
pressure = pressure_min;
}
else if (pressure > pressure_max)
{
pressure = pressure_max;
}
}
else
{
pressure = pressure_min;
}
return pressure;
}
#else
/*!
* @brief This internal API is used to compensate the raw pressure data and
* return the compensated pressure data in integer data type.
*/
uint32_t BME280::compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data)
{
int32_t var1;
int32_t var2;
int32_t var3;
int32_t var4;
uint32_t var5;
uint32_t pressure;
uint32_t pressure_min = 30000;
uint32_t pressure_max = 110000;
var1 = (((int32_t)calib_data->t_fine) / 2) - (int32_t)64000;
var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_P6);
var2 = var2 + ((var1 * ((int32_t)calib_data->dig_P5)) * 2);
var2 = (var2 / 4) + (((int32_t)calib_data->dig_P4) * 65536);
var3 = (calib_data->dig_P3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
var4 = (((int32_t)calib_data->dig_P2) * var1) / 2;
var1 = (var3 + var4) / 262144;
var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_P1)) / 32768;
/* avoid exception caused by division by zero */
if (var1)
{
var5 = (uint32_t)((uint32_t)1048576) - uncomp_data->pressure;
pressure = ((uint32_t)(var5 - (uint32_t)(var2 / 4096))) * 3125;
if (pressure < 0x80000000)
{
pressure = (pressure << 1) / ((uint32_t)var1);
}
else
{
pressure = (pressure / (uint32_t)var1) * 2;
}
var1 = (((int32_t)calib_data->dig_P9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_P8)) / 8192;
pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_P7) / 16));
if (pressure < pressure_min)
{
pressure = pressure_min;
}
else if (pressure > pressure_max)
{
pressure = pressure_max;
}
}
else
{
pressure = pressure_min;
}
return pressure;
}
#endif
/*!
* @brief This internal API is used to compensate the raw humidity data and
* return the compensated humidity data in integer data type.
*/
uint32_t BME280::compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data)
{
int32_t var1;
int32_t var2;
int32_t var3;
int32_t var4;
int32_t var5;
uint32_t humidity;
uint32_t humidity_max = 102400;
var1 = calib_data->t_fine - ((int32_t)76800);
var2 = (int32_t)(uncomp_data->humidity * 16384);
var3 = (int32_t)(((int32_t)calib_data->dig_H4) * 1048576);
var4 = ((int32_t)calib_data->dig_H5) * var1;
var5 = (((var2 - var3) - var4) + (int32_t)16384) / 32768;
var2 = (var1 * ((int32_t)calib_data->dig_H6)) / 1024;
var3 = (var1 * ((int32_t)calib_data->dig_H3)) / 2048;
var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;
var2 = ((var4 * ((int32_t)calib_data->dig_H2)) + 8192) / 16384;
var3 = var5 * var2;
var4 = ((var3 / 32768) * (var3 / 32768)) / 128;
var5 = var3 - ((var4 * ((int32_t)calib_data->dig_H1)) / 16);
var5 = (var5 < 0 ? 0 : var5);
var5 = (var5 > 419430400 ? 419430400 : var5);
humidity = (uint32_t)(var5 / 4096);
if (humidity > humidity_max)
{
humidity = humidity_max;
}
return humidity;
}
#endif
/*!
* @brief This internal API reads the calibration data from the sensor, parse
* it and store in the device structure.
*/
int8_t BME280::get_calib_data()
{
int8_t rslt;
uint8_t reg_addr = BME280_TEMP_PRESS_CALIB_DATA_ADDR;
/* Array to store calibration data */
uint8_t calib_data[BME280_TEMP_PRESS_CALIB_DATA_LEN] = { 0 };
/* Read the calibration data from the sensor */
rslt = getRegs(reg_addr, calib_data, BME280_TEMP_PRESS_CALIB_DATA_LEN);
if (rslt == BME280_OK)
{
/* Parse temperature and pressure calibration data and store
* it in device structure
*/
parse_temp_press_calib_data(calib_data);
reg_addr = BME280_HUMIDITY_CALIB_DATA_ADDR;
/* Read the humidity calibration data from the sensor */
rslt = getRegs(reg_addr, calib_data, BME280_HUMIDITY_CALIB_DATA_LEN);
if (rslt == BME280_OK)
{
/* Parse humidity calibration data and store it in
* device structure
*/
parse_humidity_calib_data(calib_data);
}
}
return rslt;
}
/*!
* @brief This internal API interleaves the register address between the
* register data buffer for burst write operation.
*/
void BME280::interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len)
{
uint8_t index;
for (index = 1; index < len; index++)
{
temp_buff[(index * 2) - 1] = reg_addr[index];
temp_buff[index * 2] = reg_data[index];
}
}
/*!
* @brief This internal API is used to parse the temperature and
* pressure calibration data and store it in device structure.
*/
void BME280::parse_temp_press_calib_data(const uint8_t *reg_data)
{
calib_data.dig_T1 = BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
calib_data.dig_T2 = (int16_t)BME280_CONCAT_BYTES(reg_data[3], reg_data[2]);
calib_data.dig_T3 = (int16_t)BME280_CONCAT_BYTES(reg_data[5], reg_data[4]);
calib_data.dig_P1 = BME280_CONCAT_BYTES(reg_data[7], reg_data[6]);
calib_data.dig_P2 = (int16_t)BME280_CONCAT_BYTES(reg_data[9], reg_data[8]);
calib_data.dig_P3 = (int16_t)BME280_CONCAT_BYTES(reg_data[11], reg_data[10]);
calib_data.dig_P4 = (int16_t)BME280_CONCAT_BYTES(reg_data[13], reg_data[12]);
calib_data.dig_P5 = (int16_t)BME280_CONCAT_BYTES(reg_data[15], reg_data[14]);
calib_data.dig_P6 = (int16_t)BME280_CONCAT_BYTES(reg_data[17], reg_data[16]);
calib_data.dig_P7 = (int16_t)BME280_CONCAT_BYTES(reg_data[19], reg_data[18]);
calib_data.dig_P8 = (int16_t)BME280_CONCAT_BYTES(reg_data[21], reg_data[20]);
calib_data.dig_P9 = (int16_t)BME280_CONCAT_BYTES(reg_data[23], reg_data[22]);
calib_data.dig_H1 = reg_data[25];
}
/*!
* @brief This internal API is used to parse the humidity calibration data
* and store it in device structure.
*/
void BME280::parse_humidity_calib_data(const uint8_t *reg_data)
{
int16_t dig_H4_lsb;
int16_t dig_H4_msb;
int16_t dig_H5_lsb;
int16_t dig_H5_msb;
calib_data.dig_H2 = (int16_t)BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
calib_data.dig_H3 = reg_data[2];
dig_H4_msb = (int16_t)(int8_t)reg_data[3] * 16;
dig_H4_lsb = (int16_t)(reg_data[4] & 0x0F);
calib_data.dig_H4 = dig_H4_msb | dig_H4_lsb;
dig_H5_msb = (int16_t)(int8_t)reg_data[5] * 16;
dig_H5_lsb = (int16_t)(reg_data[4] >> 4);
calib_data.dig_H5 = dig_H5_msb | dig_H5_lsb;
calib_data.dig_H6 = (int8_t)reg_data[6];
}
/*!
* @brief This internal API is used to identify the settings which the user
* wants to modify in the sensor.
*/
uint8_t BME280::are_settings_changed(uint8_t sub_settings, uint8_t desired_settings)
{
uint8_t settings_changed = FALSE;
if (sub_settings & desired_settings)
{
/* User wants to modify this particular settings */
settings_changed = TRUE;
}
else
{
/* User don't want to modify this particular settings */
settings_changed = FALSE;
}
return settings_changed;
}
/*!
* @brief This internal API is used to validate the device structure pointer for
* null conditions.
*/
int8_t BME280::null_ptr_check()
{
int8_t rslt;
if ((read == NULL) || (write == NULL) || (delay_ms == NULL))
{
/* Device structure pointer is not valid */
rslt = BME280_E_NULL_PTR;
}
else
{
/* Device structure is fine */
rslt = BME280_OK;
}
return rslt;
}
BME280 bme280(BME280_I2C_ADDR_PRIM);
|