-
Notifications
You must be signed in to change notification settings - Fork 0
/
bme280.c
2233 lines (2200 loc) · 69.4 KB
/
bme280.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) 2015 - 2016 Bosch Sensortec GmbH
*
* bme280.c
* Date: 2016/07/04
* Revision: 2.0.5(Pressure and Temperature compensation code revision is 1.1
* and Humidity compensation code revision is 1.0)
*
* Usage: Sensor Driver file for BME280 sensor
*
****************************************************************************
* 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.
**************************************************************************/
#include "bme280.h"
static struct bme280_t *p_bme280; /**< pointer to BME280 */
/*!
* @brief This function is used for initialize
* the bus read and bus write functions
* and assign the chip id and I2C address of the BME280 sensor
* chip id is read in the register 0xD0 bit from 0 to 7
*
* @param bme280 structure pointer.
*
* @note While changing the parameter of the bme280_t
* @note consider the following point:
* Changing the reference value of the parameter
* will changes 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 results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
u8 v_chip_id_read_count = BME280_CHIP_ID_READ_COUNT;
/* assign BME280 ptr */
p_bme280 = bme280;
while (v_chip_id_read_count > 0) {
/* read Chip Id */
com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
BME280_CHIP_ID_REG, &v_data_u8,
BME280_GEN_READ_WRITE_DATA_LENGTH);
/* Check for the correct chip id */
if (v_data_u8 == BME280_CHIP_ID)
break;
v_chip_id_read_count--;
/* Delay added concerning the low speed of power up system to
facilitate the proper reading of the chip ID */
p_bme280->delay_msec(BME280_REGISTER_READ_DELAY);
}
/*assign chip ID to the global structure*/
p_bme280->chip_id = v_data_u8;
/*com_rslt status of chip ID read*/
com_rslt = (v_chip_id_read_count == BME280_INIT_VALUE) ?
BME280_CHIP_ID_READ_FAIL : BME280_CHIP_ID_READ_SUCCESS;
if (com_rslt == BME280_CHIP_ID_READ_SUCCESS) {
/* readout bme280 calibparam structure */
com_rslt += bme280_get_calib_param();
}
return com_rslt;
}
/*!
* @brief This API is used to read uncompensated temperature
* in the registers 0xFA, 0xFB and 0xFC
* @note 0xFA -> MSB -> bit from 0 to 7
* @note 0xFB -> LSB -> bit from 0 to 7
* @note 0xFC -> LSB -> bit from 4 to 7
*
* @param v_uncomp_temperature_s32 : The value of uncompensated temperature
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
s32 *v_uncomp_temperature_s32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the MSB and LSb value
a_data_u8r[0] - Temperature MSB
a_data_u8r[1] - Temperature LSB
a_data_u8r[2] - Temperature XLSB
*/
u8 a_data_u8r[BME280_TEMPERATURE_DATA_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_TEMPERATURE_MSB_REG,
a_data_u8r,
BME280_TEMPERATURE_DATA_LENGTH);
*v_uncomp_temperature_s32 = (s32)(((
(u32) (a_data_u8r[BME280_TEMPERATURE_MSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
(((u32)(a_data_u8r[BME280_TEMPERATURE_LSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_04_BITS)
| ((u32)a_data_u8r[BME280_TEMPERATURE_XLSB_DATA] >>
BME280_SHIFT_BIT_POSITION_BY_04_BITS));
}
return com_rslt;
}
/*!
* @brief Reads actual temperature from uncompensated temperature
* @note Returns the value in 0.01 degree Centigrade
* Output value of "5123" equals 51.23 DegC.
*
*
*
* @param v_uncomp_temperature_s32 : value of uncompensated temperature
*
*
* @return Returns the actual temperature
*
*/
s32 bme280_compensate_temperature_int32(s32 v_uncomp_temperature_s32)
{
s32 v_x1_u32r = BME280_INIT_VALUE;
s32 v_x2_u32r = BME280_INIT_VALUE;
s32 temperature = BME280_INIT_VALUE;
/* calculate x1*/
v_x1_u32r =
((((v_uncomp_temperature_s32
>> BME280_SHIFT_BIT_POSITION_BY_03_BITS) -
((s32)p_bme280->cal_param.dig_T1
<< BME280_SHIFT_BIT_POSITION_BY_01_BIT))) *
((s32)p_bme280->cal_param.dig_T2)) >>
BME280_SHIFT_BIT_POSITION_BY_11_BITS;
/* calculate x2*/
v_x2_u32r = (((((v_uncomp_temperature_s32
>> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
((s32)p_bme280->cal_param.dig_T1))
* ((v_uncomp_temperature_s32 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
((s32)p_bme280->cal_param.dig_T1)))
>> BME280_SHIFT_BIT_POSITION_BY_12_BITS) *
((s32)p_bme280->cal_param.dig_T3))
>> BME280_SHIFT_BIT_POSITION_BY_14_BITS;
/* calculate t_fine*/
p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
/* calculate temperature*/
temperature = (p_bme280->cal_param.t_fine * 5 + 128)
>> BME280_SHIFT_BIT_POSITION_BY_08_BITS;
return temperature;
}
/*!
* @brief Reads actual temperature from uncompensated temperature
* @note Returns the value with 500LSB/DegC centred around 24 DegC
* output value of "5123" equals(5123/500)+24 = 34.246DegC
*
*
* @param v_uncomp_temperature_s32: value of uncompensated temperature
*
*
*
* @return Return the actual temperature as s16 output
*
*/
s16 bme280_compensate_temperature_int32_sixteen_bit_output(
s32 v_uncomp_temperature_s32)
{
s16 temperature = BME280_INIT_VALUE;
bme280_compensate_temperature_int32(
v_uncomp_temperature_s32);
temperature = (s16)((((
p_bme280->cal_param.t_fine - 122880) * 25) + 128)
>> BME280_SHIFT_BIT_POSITION_BY_08_BITS);
return temperature;
}
/*!
* @brief This API is used to read uncompensated pressure.
* in the registers 0xF7, 0xF8 and 0xF9
* @note 0xF7 -> MSB -> bit from 0 to 7
* @note 0xF8 -> LSB -> bit from 0 to 7
* @note 0xF9 -> LSB -> bit from 4 to 7
*
*
*
* @param v_uncomp_pressure_s32 : The value of uncompensated pressure
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
s32 *v_uncomp_pressure_s32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the MSB and LSb value
a_data_u8[0] - Pressure MSB
a_data_u8[1] - Pressure LSB
a_data_u8[2] - Pressure XLSB
*/
u8 a_data_u8[BME280_PRESSURE_DATA_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_PRESSURE_MSB_REG,
a_data_u8, BME280_PRESSURE_DATA_LENGTH);
*v_uncomp_pressure_s32 = (s32)((
((u32)(a_data_u8[BME280_PRESSURE_MSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
(((u32)(a_data_u8[BME280_PRESSURE_LSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
((u32)a_data_u8[BME280_PRESSURE_XLSB_DATA] >>
BME280_SHIFT_BIT_POSITION_BY_04_BITS));
}
return com_rslt;
}
/*!
* @brief Reads actual pressure from uncompensated pressure
* @note Returns the value in Pascal(Pa)
* Output value of "96386" equals 96386 Pa =
* 963.86 hPa = 963.86 millibar
*
*
*
* @param v_uncomp_pressure_s32 : value of uncompensated pressure
*
*
*
* @return Return the actual pressure output as u32
*
*/
u32 bme280_compensate_pressure_int32(s32 v_uncomp_pressure_s32)
{
s32 v_x1_u32 = BME280_INIT_VALUE;
s32 v_x2_u32 = BME280_INIT_VALUE;
u32 v_pressure_u32 = BME280_INIT_VALUE;
/* calculate x1*/
v_x1_u32 = (((s32)p_bme280->cal_param.t_fine)
>> BME280_SHIFT_BIT_POSITION_BY_01_BIT) - (s32)64000;
/* calculate x2*/
v_x2_u32 = (((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
* (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
) >> BME280_SHIFT_BIT_POSITION_BY_11_BITS)
* ((s32)p_bme280->cal_param.dig_P6);
/* calculate x2*/
v_x2_u32 = v_x2_u32 + ((v_x1_u32 *
((s32)p_bme280->cal_param.dig_P5))
<< BME280_SHIFT_BIT_POSITION_BY_01_BIT);
/* calculate x2*/
v_x2_u32 = (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) +
(((s32)p_bme280->cal_param.dig_P4)
<< BME280_SHIFT_BIT_POSITION_BY_16_BITS);
/* calculate x1*/
v_x1_u32 = (((p_bme280->cal_param.dig_P3 *
(((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) *
(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS))
>> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
>> BME280_SHIFT_BIT_POSITION_BY_03_BITS) +
((((s32)p_bme280->cal_param.dig_P2) *
v_x1_u32) >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
>> BME280_SHIFT_BIT_POSITION_BY_18_BITS;
/* calculate x1*/
v_x1_u32 = ((((32768 + v_x1_u32)) *
((s32)p_bme280->cal_param.dig_P1))
>> BME280_SHIFT_BIT_POSITION_BY_15_BITS);
/* calculate pressure*/
v_pressure_u32 =
(((u32)(((s32)1048576) - v_uncomp_pressure_s32)
- (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS))) * 3125;
if (v_pressure_u32
< 0x80000000)
/* Avoid exception caused by division by zero */
if (v_x1_u32 != BME280_INIT_VALUE)
v_pressure_u32 =
(v_pressure_u32
<< BME280_SHIFT_BIT_POSITION_BY_01_BIT) /
((u32)v_x1_u32);
else
return BME280_INVALID_DATA;
else
/* Avoid exception caused by division by zero */
if (v_x1_u32 != BME280_INIT_VALUE)
v_pressure_u32 = (v_pressure_u32
/ (u32)v_x1_u32) * 2;
else
return BME280_INVALID_DATA;
v_x1_u32 = (((s32)p_bme280->cal_param.dig_P9) *
((s32)(((v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS)
* (v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS))
>> BME280_SHIFT_BIT_POSITION_BY_13_BITS)))
>> BME280_SHIFT_BIT_POSITION_BY_12_BITS;
v_x2_u32 = (((s32)(v_pressure_u32
>> BME280_SHIFT_BIT_POSITION_BY_02_BITS)) *
((s32)p_bme280->cal_param.dig_P8))
>> BME280_SHIFT_BIT_POSITION_BY_13_BITS;
v_pressure_u32 = (u32)((s32)v_pressure_u32 +
((v_x1_u32 + v_x2_u32 + p_bme280->cal_param.dig_P7)
>> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
return v_pressure_u32;
}
/*!
* @brief This API is used to read uncompensated humidity.
* in the registers 0xF7, 0xF8 and 0xF9
* @note 0xFD -> MSB -> bit from 0 to 7
* @note 0xFE -> LSB -> bit from 0 to 7
*
*
*
* @param v_uncomp_humidity_s32 : The value of uncompensated humidity
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_humidity(
s32 *v_uncomp_humidity_s32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the MSB and LSb value
a_data_u8[0] - Humidity MSB
a_data_u8[1] - Humidity LSB
*/
u8 a_data_u8[BME280_HUMIDITY_DATA_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_HUMIDITY_MSB_REG, a_data_u8,
BME280_HUMIDITY_DATA_LENGTH);
*v_uncomp_humidity_s32 = (s32)(
(((u32)(a_data_u8[BME280_HUMIDITY_MSB_DATA]))
<< BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
((u32)(a_data_u8[BME280_HUMIDITY_LSB_DATA])));
}
return com_rslt;
}
/*!
* @brief Reads actual humidity from uncompensated humidity
* @note Returns the value in %rH as unsigned 32bit integer
* in Q22.10 format(22 integer 10 fractional bits).
* @note An output value of 42313
* represents 42313 / 1024 = 41.321 %rH
*
*
*
* @param v_uncomp_humidity_s32: value of uncompensated humidity
*
* @return Return the actual relative humidity output as u32
*
*/
u32 bme280_compensate_humidity_int32(s32 v_uncomp_humidity_s32)
{
s32 v_x1_u32 = BME280_INIT_VALUE;
/* calculate x1*/
v_x1_u32 = (p_bme280->cal_param.t_fine - ((s32)76800));
/* calculate x1*/
v_x1_u32 = (((((v_uncomp_humidity_s32
<< BME280_SHIFT_BIT_POSITION_BY_14_BITS) -
(((s32)p_bme280->cal_param.dig_H4)
<< BME280_SHIFT_BIT_POSITION_BY_20_BITS) -
(((s32)p_bme280->cal_param.dig_H5) * v_x1_u32)) +
((s32)16384)) >> BME280_SHIFT_BIT_POSITION_BY_15_BITS)
* (((((((v_x1_u32 *
((s32)p_bme280->cal_param.dig_H6))
>> BME280_SHIFT_BIT_POSITION_BY_10_BITS) *
(((v_x1_u32 * ((s32)p_bme280->cal_param.dig_H3))
>> BME280_SHIFT_BIT_POSITION_BY_11_BITS) + ((s32)32768)))
>> BME280_SHIFT_BIT_POSITION_BY_10_BITS) + ((s32)2097152)) *
((s32)p_bme280->cal_param.dig_H2) + 8192) >> 14));
v_x1_u32 = (v_x1_u32 - (((((v_x1_u32
>> BME280_SHIFT_BIT_POSITION_BY_15_BITS) *
(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS))
>> BME280_SHIFT_BIT_POSITION_BY_07_BITS) *
((s32)p_bme280->cal_param.dig_H1))
>> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
v_x1_u32 = (v_x1_u32 < 0 ? 0 : v_x1_u32);
v_x1_u32 = (v_x1_u32 > 419430400 ?
419430400 : v_x1_u32);
return (u32)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS);
}
/*!
* @brief Reads actual humidity from uncompensated humidity
* @note Returns the value in %rH as unsigned 16bit integer
* @note An output value of 42313
* represents 42313/512 = 82.643 %rH
*
*
*
* @param v_uncomp_humidity_s32: value of uncompensated humidity
*
*
* @return Return the actual relative humidity output as u16
*
*/
u16 bme280_compensate_humidity_int32_sixteen_bit_output(
s32 v_uncomp_humidity_s32)
{
u32 v_x1_u32 = BME280_INIT_VALUE;
u16 v_x2_u32 = BME280_INIT_VALUE;
v_x1_u32 = bme280_compensate_humidity_int32(v_uncomp_humidity_s32);
v_x2_u32 = (u16)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
return v_x2_u32;
}
/*!
* @brief This API used to read uncompensated
* pressure,temperature and humidity
*
*
*
*
* @param v_uncomp_pressure_s32: The value of uncompensated pressure.
* @param v_uncomp_temperature_s32: The value of uncompensated temperature
* @param v_uncomp_humidity_s32: The value of uncompensated humidity.
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure_temperature_humidity(
s32 *v_uncomp_pressure_s32,
s32 *v_uncomp_temperature_s32, s32 *v_uncomp_humidity_s32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
/* Array holding the MSB and LSb value of
a_data_u8[0] - Pressure MSB
a_data_u8[1] - Pressure LSB
a_data_u8[1] - Pressure LSB
a_data_u8[1] - Temperature MSB
a_data_u8[1] - Temperature LSB
a_data_u8[1] - Temperature LSB
a_data_u8[1] - Humidity MSB
a_data_u8[1] - Humidity LSB
*/
u8 a_data_u8[BME280_DATA_FRAME_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_PRESSURE_MSB_REG,
a_data_u8, BME280_ALL_DATA_FRAME_LENGTH);
/*Pressure*/
*v_uncomp_pressure_s32 = (s32)((
((u32)(a_data_u8[
BME280_DATA_FRAME_PRESSURE_MSB_BYTE]))
<< BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
(((u32)(a_data_u8[
BME280_DATA_FRAME_PRESSURE_LSB_BYTE]))
<< BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
((u32)a_data_u8[
BME280_DATA_FRAME_PRESSURE_XLSB_BYTE] >>
BME280_SHIFT_BIT_POSITION_BY_04_BITS));
/* Temperature */
*v_uncomp_temperature_s32 = (s32)(((
(u32) (a_data_u8[
BME280_DATA_FRAME_TEMPERATURE_MSB_BYTE]))
<< BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
(((u32)(a_data_u8[
BME280_DATA_FRAME_TEMPERATURE_LSB_BYTE]))
<< BME280_SHIFT_BIT_POSITION_BY_04_BITS)
| ((u32)a_data_u8[
BME280_DATA_FRAME_TEMPERATURE_XLSB_BYTE]
>> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
/*Humidity*/
*v_uncomp_humidity_s32 = (s32)((
((u32)(a_data_u8[
BME280_DATA_FRAME_HUMIDITY_MSB_BYTE]))
<< BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
((u32)(a_data_u8[
BME280_DATA_FRAME_HUMIDITY_LSB_BYTE])));
}
return com_rslt;
}
/*!
* @brief This API used to read true pressure, temperature and humidity
*
*
*
*
* @param v_pressure_u32 : The value of compensated pressure.
* @param v_temperature_s32 : The value of compensated temperature.
* @param v_humidity_u32 : The value of compensated humidity.
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_read_pressure_temperature_humidity(
u32 *v_pressure_u32, s32 *v_temperature_s32, u32 *v_humidity_u32)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
s32 v_uncomp_pressure_s32 = BME280_INIT_VALUE;
s32 v_uncom_temperature_s32 = BME280_INIT_VALUE;
s32 v_uncom_humidity_s32 = BME280_INIT_VALUE;
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
/* read the uncompensated pressure,
temperature and humidity*/
com_rslt =
bme280_read_uncomp_pressure_temperature_humidity(
&v_uncomp_pressure_s32, &v_uncom_temperature_s32,
&v_uncom_humidity_s32);
/* read the true pressure, temperature and humidity*/
*v_temperature_s32 =
bme280_compensate_temperature_int32(
v_uncom_temperature_s32);
*v_pressure_u32 = bme280_compensate_pressure_int32(
v_uncomp_pressure_s32);
*v_humidity_u32 = bme280_compensate_humidity_int32(
v_uncom_humidity_s32);
}
return com_rslt;
}
/*!
* @brief This API is used to
* calibration parameters used for calculation in the registers
*
* parameter | Register address | bit
*------------|------------------|----------------
* dig_T1 | 0x88 and 0x89 | from 0 : 7 to 8: 15
* dig_T2 | 0x8A and 0x8B | from 0 : 7 to 8: 15
* dig_T3 | 0x8C and 0x8D | from 0 : 7 to 8: 15
* dig_P1 | 0x8E and 0x8F | from 0 : 7 to 8: 15
* dig_P2 | 0x90 and 0x91 | from 0 : 7 to 8: 15
* dig_P3 | 0x92 and 0x93 | from 0 : 7 to 8: 15
* dig_P4 | 0x94 and 0x95 | from 0 : 7 to 8: 15
* dig_P5 | 0x96 and 0x97 | from 0 : 7 to 8: 15
* dig_P6 | 0x98 and 0x99 | from 0 : 7 to 8: 15
* dig_P7 | 0x9A and 0x9B | from 0 : 7 to 8: 15
* dig_P8 | 0x9C and 0x9D | from 0 : 7 to 8: 15
* dig_P9 | 0x9E and 0x9F | from 0 : 7 to 8: 15
* dig_H1 | 0xA1 | from 0 to 7
* dig_H2 | 0xE1 and 0xE2 | from 0 : 7 to 8: 15
* dig_H3 | 0xE3 | from 0 to 7
* dig_H4 | 0xE4 and 0xE5 | from 4 : 11 to 0: 3
* dig_H5 | 0xE5 and 0xE6 | from 0 : 3 to 4: 11
* dig_H6 | 0xE7 | from 0 to 7
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param(void)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 a_data_u8[BME280_CALIB_DATA_SIZE] = {
BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
a_data_u8,
BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH);
p_bme280->cal_param.dig_T1 = (u16)(((
(u16)((u8)a_data_u8[
BME280_TEMPERATURE_CALIB_DIG_T1_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T1_LSB]);
p_bme280->cal_param.dig_T2 = (s16)(((
(s16)((s8)a_data_u8[
BME280_TEMPERATURE_CALIB_DIG_T2_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T2_LSB]);
p_bme280->cal_param.dig_T3 = (s16)(((
(s16)((s8)a_data_u8[
BME280_TEMPERATURE_CALIB_DIG_T3_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T3_LSB]);
p_bme280->cal_param.dig_P1 = (u16)(((
(u16)((u8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P1_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P1_LSB]);
p_bme280->cal_param.dig_P2 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P2_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P2_LSB]);
p_bme280->cal_param.dig_P3 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P3_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[
BME280_PRESSURE_CALIB_DIG_P3_LSB]);
p_bme280->cal_param.dig_P4 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P4_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P4_LSB]);
p_bme280->cal_param.dig_P5 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P5_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P5_LSB]);
p_bme280->cal_param.dig_P6 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P6_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P6_LSB]);
p_bme280->cal_param.dig_P7 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P7_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P7_LSB]);
p_bme280->cal_param.dig_P8 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P8_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P8_LSB]);
p_bme280->cal_param.dig_P9 = (s16)(((
(s16)((s8)a_data_u8[
BME280_PRESSURE_CALIB_DIG_P9_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_PRESSURE_CALIB_DIG_P9_LSB]);
p_bme280->cal_param.dig_H1 =
a_data_u8[BME280_HUMIDITY_CALIB_DIG_H1];
com_rslt += p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_HUMIDITY_CALIB_DIG_H2_LSB_REG, a_data_u8,
BME280_HUMIDITY_CALIB_DATA_LENGTH);
p_bme280->cal_param.dig_H2 = (s16)(((
(s16)((s8)a_data_u8[
BME280_HUMIDITY_CALIB_DIG_H2_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_08_BITS)
| a_data_u8[BME280_HUMIDITY_CALIB_DIG_H2_LSB]);
p_bme280->cal_param.dig_H3 =
a_data_u8[BME280_HUMIDITY_CALIB_DIG_H3];
p_bme280->cal_param.dig_H4 = (s16)(((
(s16)((s8)a_data_u8[
BME280_HUMIDITY_CALIB_DIG_H4_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
(((u8)BME280_MASK_DIG_H4) &
a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB]));
p_bme280->cal_param.dig_H5 = (s16)(((
(s16)((s8)a_data_u8[
BME280_HUMIDITY_CALIB_DIG_H5_MSB])) <<
BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
(a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB] >>
BME280_SHIFT_BIT_POSITION_BY_04_BITS));
p_bme280->cal_param.dig_H6 =
(s8)a_data_u8[BME280_HUMIDITY_CALIB_DIG_H6];
}
return com_rslt;
}
/*!
* @brief This API is used to get
* the temperature oversampling setting in the register 0xF4
* bits from 5 to 7
*
* value | Temperature oversampling
* ---------------------|---------------------------------
* 0x00 | Skipped
* 0x01 | BME280_OVERSAMP_1X
* 0x02 | BME280_OVERSAMP_2X
* 0x03 | BME280_OVERSAMP_4X
* 0x04 | BME280_OVERSAMP_8X
* 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
*
*
* @param v_value_u8 : The value of temperature over sampling
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
u8 *v_value_u8)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
*v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
p_bme280->oversamp_temperature = *v_value_u8;
}
return com_rslt;
}
/*!
* @brief This API is used to set
* the temperature oversampling setting in the register 0xF4
* bits from 5 to 7
*
* value | Temperature oversampling
* ---------------------|---------------------------------
* 0x00 | Skipped
* 0x01 | BME280_OVERSAMP_1X
* 0x02 | BME280_OVERSAMP_2X
* 0x03 | BME280_OVERSAMP_4X
* 0x04 | BME280_OVERSAMP_8X
* 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
*
*
* @param v_value_u8 : The value of temperature over sampling
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
u8 v_value_u8)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
v_data_u8 = p_bme280->ctrl_meas_reg;
v_data_u8 =
BME280_SET_BITSLICE(v_data_u8,
BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
com_rslt += bme280_set_soft_rst();
p_bme280->delay_msec(BME280_3MS_DELAY);
/* write previous value
of configuration register*/
v_pre_config_value_u8 = p_bme280->config_reg;
com_rslt += bme280_write_register(
BME280_CONFIG_REG,
&v_pre_config_value_u8,
BME280_GEN_READ_WRITE_DATA_LENGTH);
/* write previous value
of humidity oversampling*/
v_pre_ctrl_hum_value_u8 =
p_bme280->ctrl_hum_reg;
com_rslt += bme280_write_register(
BME280_CTRL_HUMIDITY_REG,
&v_pre_ctrl_hum_value_u8,
BME280_GEN_READ_WRITE_DATA_LENGTH);
/* write previous and updated value
of configuration register*/
com_rslt += bme280_write_register(
BME280_CTRL_MEAS_REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
} else {
com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
p_bme280->dev_addr,
BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
}
p_bme280->oversamp_temperature = v_value_u8;
/* read the control measurement register value*/
com_rslt = bme280_read_register(
BME280_CTRL_MEAS_REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
p_bme280->ctrl_meas_reg = v_data_u8;
/* read the control humidity register value*/
com_rslt += bme280_read_register(
BME280_CTRL_HUMIDITY_REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
p_bme280->ctrl_hum_reg = v_data_u8;
/* read the control
configuration register value*/
com_rslt += bme280_read_register(
BME280_CONFIG_REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
p_bme280->config_reg = v_data_u8;
}
return com_rslt;
}
/*!
* @brief This API is used to get
* the pressure oversampling setting in the register 0xF4
* bits from 2 to 4
*
* value | Pressure oversampling
* --------------------|--------------------------
* 0x00 | Skipped
* 0x01 | BME280_OVERSAMP_1X
* 0x02 | BME280_OVERSAMP_2X
* 0x03 | BME280_OVERSAMP_4X
* 0x04 | BME280_OVERSAMP_8X
* 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
*
*
* @param v_value_u8 : The value of pressure oversampling
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
u8 *v_value_u8)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
com_rslt = p_bme280->BME280_BUS_READ_FUNC(
p_bme280->dev_addr,
BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
&v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
*v_value_u8 = BME280_GET_BITSLICE(
v_data_u8,
BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
p_bme280->oversamp_pressure = *v_value_u8;
}
return com_rslt;
}
/*!
* @brief This API is used to set
* the pressure oversampling setting in the register 0xF4
* bits from 2 to 4
*
* value | Pressure oversampling
* --------------------|--------------------------
* 0x00 | Skipped
* 0x01 | BME280_OVERSAMP_1X
* 0x02 | BME280_OVERSAMP_2X
* 0x03 | BME280_OVERSAMP_4X
* 0x04 | BME280_OVERSAMP_8X
* 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
*
*
* @param v_value_u8 : The value of pressure oversampling
*
*
*
* @return results of bus communication function
* @retval 0 -> Success
* @retval -1 -> Error
*
*
*/
BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
u8 v_value_u8)
{
/* used to return the communication result*/
BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
u8 v_data_u8 = BME280_INIT_VALUE;
u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
/* check the p_bme280 structure pointer as NULL*/
if (p_bme280 == BME280_NULL) {
return E_BME280_NULL_PTR;
} else {
v_data_u8 = p_bme280->ctrl_meas_reg;
v_data_u8 =
BME280_SET_BITSLICE(v_data_u8,
BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
com_rslt += bme280_set_soft_rst();
p_bme280->delay_msec(BME280_3MS_DELAY);
/* write previous value of
configuration register*/
v_pre_config_value_u8 = p_bme280->config_reg;
com_rslt = bme280_write_register(
BME280_CONFIG_REG,
&v_pre_config_value_u8,
BME280_GEN_READ_WRITE_DATA_LENGTH);
/* write previous value of
humidity oversampling*/
v_pre_ctrl_hum_value_u8 =
p_bme280->ctrl_hum_reg;
com_rslt += bme280_write_register(