-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbme280.c
1677 lines (1266 loc) · 48 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
/**
*******************************************
* @file bme280.c
* @author Łukasz Juraszek / JuraszekL
* @date 20.04.2023
* @brief Source code for BME280 Driver
* @note https://github.com/JuraszekL/BME280_Driver
*******************************************
*/
/**
* @addtogroup BME280_Driver
* @{
*/
//***************************************
#include <stdint.h>
#include <stddef.h>
#include "bme280.h"
/**
* @defgroup BME280_priv Private Resources
* @brief only for internal library purposes
* @{
*/
/**
* @defgroup BME280_privmacros Macros
* @{
*/
/// concatenate two bytes into signed half-word
#define CAT_I16T(msb, lsb) ((int16_t)(((int16_t)msb << 8) | (int16_t)lsb))
/// concatenate two bytes into unsigned half-word
#define CAT_UI16T(msb, lsb) ((uint16_t)(((uint16_t)msb << 8) | (uint16_t)lsb))
/// check if x is null
#define IS_NULL(x) ((NULL == x))
///@}
/**
* @defgroup BME280_privenums Enums
* @{
*/
/// type of read, used as parameter to call #bme280_read_compensate function
enum { read_all = 0, read_temp, read_press, read_hum};
/// possible value of "mode" variable inside #BME280_t structure
enum { sleep_mode = 0x00, forced_mode = 0x01, normal_mode = 0x03 };
/// possible value of "initialized" variabie inside #BME280_t structure
enum { not_initialized = 0x00, initialized };
///@}
/**
* @struct adc_regs
* @brief keeps raw adc values from sensor, used in #bme280_read_compensate function
* @{
*/
struct adc_regs {
uint8_t press_raw[BME280_PRESS_ADC_LEN]; ///< keeps raw adc value of pressure
uint8_t temp_raw[BME280_TEMP_ADC_LEN]; ///< keeps raw adc value of temperature
uint8_t hum_raw[BME280_HUM_ADC_LEN]; ///< keeps raw adc value of humidity
} __attribute__((aligned(1))) ;
///@}
/**
*@defgroup BME280_privfunct Functions
*@{
*/
/**
* @brief read compensation data
*
* Function reads individual compensation data from sensor and stores them into #BME280_calibration_data
* inside *Dev structure
*/
static int8_t bme280_read_compensation_parameters(BME280_t *Dev);
/**
* @brief read and compensate measured values
*
* Function reads selected adc values from sensor, converts them into single variables and
* compensate with calibration data stored in #BME280_calibration_data inside *Dev structure. These
* variales are then returned as single integer values.
*/
static int8_t bme280_read_compensate(uint8_t read_type, BME280_t *Dev, BME280_S32_t *temp,
BME280_U32_t *press, BME280_U32_t *hum);
/**
* @brief convert buffer to single variable
*
* Function converts raw adc values of temperature or pressure to single #BME280_S32_t variable
*/
static BME280_S32_t bme280_parse_press_temp_s32t(uint8_t *raw);
/**
* @brief convert buffer to single variable
*
* Function converts raw adc values of humidity to single #BME280_S32_t variable
*/
static BME280_S32_t bme280_parse_hum_s32t(uint8_t *raw);
/**
* @brief compensate temperature value
*
* Function returns compensated temperature in DegC, resolution is 0.01 DegC. Output value of “5123”
* equals 51.23 DegC. It calculates t_fine variable stored inside *Dev structure as well.
*/
static BME280_S32_t bme280_compensate_t_s32t(BME280_t *Dev, BME280_S32_t adc_T);
/**
* @brief compensate pressure value
*
* Function returns compensated pressure in Pa as unsigned 32 bit integer. Output value depends of #USE_64BIT
* configuration.
*/
static BME280_U32_t bme280_compensate_p_u32t(BME280_t *Dev, BME280_S32_t adc_P);
/**
* @brief compensate humidity value
*
* Function returns compensated humidity in %RH as unsigned 32bit integer. Output value of "47445"
* represents 47445/1000 = 47.445 %RH
*/
static BME280_U32_t bme280_compensate_h_u32t(BME280_t *Dev, BME280_S32_t adc_H);
#ifdef USE_INTEGER_RESULTS
/**
* @brief convert temperature to structure
*
* Function converts temperature stored in #BME280_S32_t to #BME280_Data_t structure
*/
static void bme280_convert_t_S32_struct(BME280_S32_t temp, BME280_Data_t *data);
#endif
#ifdef USE_FLOATS_RESULTS
/**
* @brief convert temperature to float
*
* Function converts temperature stored in #BME280_S32_t to float variable
*/
static void bme280_convert_t_S32_float(BME280_S32_t temp_in, float *temp_out);
#endif
#ifdef USE_INTEGER_RESULTS
/**
* @brief convert pressure to structure
*
* Function converts pressure stored in #BME280_U32_t to #BME280_Data_t structure
*/
static void bme280_convert_p_U32_struct(BME280_U32_t press, BME280_Data_t *data);
#endif
#ifdef USE_FLOATS_RESULTS
/**
* @brief convert pressure to structure
*
* Function converts pressure stored in #BME280_U32_t to float variable
*/
static void bme280_convert_p_U32_float(BME280_U32_t press_in, float *press_out);
#endif
#ifdef USE_INTEGER_RESULTS
/**
* @brief convert humidity to structure
*
* Function converts humidity stored in #BME280_U32_t to #BME280_Data_t structure
*/
static void bme280_convert_h_U32_struct(BME280_U32_t hum, BME280_Data_t *data);
#endif
#ifdef USE_FLOATS_RESULTS
/**
* @brief convert humidity to structure
*
* Function converts humidity stored in #BME280_U32_t to float variable
*/
static void bme280_convert_h_U32_float(BME280_U32_t hum_in, float *hum_out);
#endif
#ifdef USE_NORMAL_MODE
/**
* @brief check for normal mode
*
* Function checks if device is initializes and if it's status in *Dev structure
* is set as "normal_mode"
*/
static int8_t bme280_is_normal_mode(BME280_t *Dev);
#endif
/**
* @brief check for sleep mode
*
* Function checks if device is initializes and if it's status in *Dev structure
* is set as "sleep_mode"
*/
static int8_t bme280_is_sleep_mode(BME280_t *Dev);
#ifdef USE_FORCED_MODE
/**
* @brief check and set forced mode
*
* Function checks if conditions to set forced mode are met, calculates and returns required
* delay time (via *delay pointer) then sets forced mode
*/
static int8_t bme280_set_forced_mode(BME280_t *Dev, uint8_t *delay);
/**
* @brief change osrs_x reg value to oversampling
*
* Function converts register value to oversampling value
* f.e. osrs_p = 0x04 then pressure oversampling = x8
*/
static void bme280_osrs_to_oversampling(uint8_t *osrs);
/**
* @brief check if sensor is busy
*
* Function reads "status" register and checks value of two its bits
* returns #BME280_BUSY_ERR if any of them is set
*/
static int8_t bme280_busy_check(BME280_t *Dev);
#endif
///@}
///@}
//***************************************
/* public functions */
//***************************************
/* function that initiates minimum required parameters to operate with a sensor */
int8_t BME280_Init(BME280_t *Dev, BME280_Driver_t *Driver){
int8_t res = BME280_OK;
uint8_t id = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Driver) || IS_NULL(Driver->write) || IS_NULL(Driver->read) ||
IS_NULL(Driver->delay) ) return BME280_PARAM_ERR;
/* attach the driver to main structure */
Dev->driver = Driver;
/* perform sensor reset */
res = BME280_Reset(Dev);
if(BME280_OK != res) return res;
/* Start-up time = 2ms */
Dev->driver->delay(2);
/* read and check chip ID */
res = Dev->driver->read(BME280_ID_ADDR, &id, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
if(BME280_ID != id) return BME280_ID_ERR;
/* read, parse and store compensation data */
res = bme280_read_compensation_parameters(Dev);
if(BME280_OK == res) Dev->initialized = initialized;
return res;
}
/* Function configures all sensor parameters at once */
int8_t BME280_ConfigureAll(BME280_t *Dev, BME280_Config_t *Config){
int8_t res = BME280_OK;
uint8_t ctrl_hum = 0, ctrl_meas = 0, config = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Config) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* set the data from Config structure to the right positions in
* sensor registers */
ctrl_hum = Config->oversampling_h & 0x07; //0x07 - 0b00000111
ctrl_meas |= (Config->oversampling_t << 5) & 0xE0; //0xE0 - 0b11100000
ctrl_meas |= (Config->oversampling_p << 2) & 0x1C; //0x1C - 0b00011100
ctrl_meas |= Config->mode & 0x03; //0x03 - 0b00000011
config |= (Config->t_stby << 5) & 0xE0; //0xE0 - 0b11100000
config |= (Config->filter << 2) & 0x1C; //0x1C - 0b00011100
config |= Config->spi3w_enable & 0x01; //0x01 - 0b00000001
/* send three config bytes to the device */
res = Dev->driver->write(BME280_CTRL_HUM_ADDR, ctrl_hum, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
res = Dev->driver->write(BME280_CTRL_MEAS_ADDR, ctrl_meas, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
res = Dev->driver->write(BME280_CONFIG_ADDR, config, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* set oparing mode inside Dev structure */
if(BME280_SLEEPMODE == Config->mode){
Dev->mode = sleep_mode;
}
else if(BME280_FORCEDMODE == Config->mode){
Dev->mode = forced_mode;
}
else if(BME280_NORMALMODE == Config->mode){
Dev->mode = normal_mode;
}
return res;
}
/* function performs power-on reset procedure for sensor */
int8_t BME280_Reset(BME280_t *Dev){
int8_t res = BME280_OK;
/* check parameter */
if( IS_NULL(Dev) || IS_NULL(Dev->driver->write) ) return BME280_PARAM_ERR;
/* write reset commad to reset register */
res = Dev->driver->write(BME280_RESET_ADDR, BME280_RESET_VALUE, Dev->driver);
/* set mode to default */
Dev->mode = sleep_mode;
return res;
}
#ifdef USE_GETTERS
/* Function reads current operation mode from sensor */
int8_t BME280_GetMode(BME280_t *Dev, uint8_t *Mode){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Mode) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse mode values from ctrl_meas */
ctrl_meas &= 0x03;
if(0x02 == ctrl_meas) ctrl_meas = BME280_FORCEDMODE;
/* update value inside Dev structure */
Dev->mode = ctrl_meas;
/* set output pointer */
*Mode = ctrl_meas;
return res;
}
/* function gets current pressure oversampling value from sensor */
int8_t BME280_GetPOvs(BME280_t *Dev, uint8_t *POvs){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(POvs) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse pressure oversampling value from ctrl_meas */
ctrl_meas = (ctrl_meas >> 2) & 0x07;
/* set output pointer */
*POvs = ctrl_meas;
return res;
}
/* function gets current temperature oversampling value from sensor */
int8_t BME280_GetTOvs(BME280_t *Dev, uint8_t *TOvs){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(TOvs) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse temperature oversampling value from ctrl_meas */
ctrl_meas = (ctrl_meas >> 5) & 0x07;
/* set output pointer */
*TOvs = ctrl_meas;
return res;
}
/* function gets current humidity oversampling value from sensor */
int8_t BME280_GetHOvs(BME280_t *Dev, uint8_t *HOvs){
int8_t res = BME280_OK;
uint8_t ctrl_hum = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(HOvs) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_hum register from sensor */
res = Dev->driver->read(BME280_CTRL_HUM_ADDR, &ctrl_hum, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse humidity oversampling value from ctrl_hum */
ctrl_hum = ctrl_hum & 0x07;
/* set output pointer */
*HOvs = ctrl_hum;
return res;
}
/* function gets current standby time for normal mode */
int8_t BME280_GetTStby(BME280_t *Dev, uint8_t *TStby){
int8_t res = BME280_OK;
uint8_t config = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(TStby) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse standby time value from config */
config = (config >> 5) & 0x07;
/* set output pointer */
*TStby = config;
return res;
}
/* function gets current value of IIR filter */
int8_t BME280_GetTFilter(BME280_t *Dev, uint8_t *Filter){
int8_t res = BME280_OK;
uint8_t config = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Filter) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse filter value from config */
config = (config >> 2) & 0x07;
/* set output pointer */
*Filter = config;
return res;
}
/* function reads current 3-wire SPI setup (0 = 3w SPI disabled, 1 - 3w SPI enabled) */
int8_t BME280_Is3WireSPIEnabled(BME280_t *Dev, uint8_t *Result){
int8_t res = BME280_OK;
uint8_t config = 0;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Result) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* parse mode values from ctrl_meas */
config &= 0x01;
/* set output pointer */
*Result = config;
return res;
}
#endif
#ifdef USE_SETTERS
/* Function sets sensor operation mode */
int8_t BME280_SetMode(BME280_t *Dev, uint8_t Mode){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0, tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (Mode > BME280_NORMALMODE) ) return BME280_PARAM_ERR;
/* check if sensor is initialized */
res = bme280_is_sleep_mode(Dev);
if(BME280_NO_INIT_ERR == res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current mode differs from requested */
tmp = ctrl_meas & 0x03;
if(0x02 == tmp) tmp = BME280_FORCEDMODE;
if(Mode == tmp) return BME280_OK;
/* send new ctrl_meas value to sensor if required */
ctrl_meas &= 0xFC; //0xFC - 0b11111100
ctrl_meas |= Mode;
res = Dev->driver->write(BME280_CTRL_MEAS_ADDR, ctrl_meas, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* update value inside Dev structure */
Dev->mode = Mode;
return res;
}
/* function sets pressure oversampling value */
int8_t BME280_SetPOvs(BME280_t *Dev, uint8_t POvs){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0, tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (POvs > BME280_OVERSAMPLING_X16) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = (ctrl_meas >> 2) & 0x07;
if(POvs == tmp) return BME280_OK;
/* send new ctrl_meas value to sensor if required */
ctrl_meas &= 0xE3; //0xE3 - 0b11100011
ctrl_meas |= (POvs << 2);
res = Dev->driver->write(BME280_CTRL_MEAS_ADDR, ctrl_meas, Dev->driver);
return res;
}
/* function sets temperature oversampling value */
int8_t BME280_SetTOvs(BME280_t *Dev, uint8_t TOvs){
int8_t res = BME280_OK;
uint8_t ctrl_meas = 0, tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (TOvs > BME280_OVERSAMPLING_X16) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of ctrl_meas register from sensor */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &ctrl_meas, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = (ctrl_meas >> 5) & 0x07;
if(TOvs == tmp) return BME280_OK;
/* send new ctrl_meas value to sensor if required */
ctrl_meas &= 0x1F; //0x1F - 0b00011111
ctrl_meas |= (TOvs << 5);
res = Dev->driver->write(BME280_CTRL_MEAS_ADDR, ctrl_meas, Dev->driver);
return res;
}
/* function sets humidity oversampling value */
int8_t BME280_SetHOvs(BME280_t *Dev, uint8_t HOvs){
int8_t res = BME280_OK;
uint8_t tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (HOvs > BME280_OVERSAMPLING_X16) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* send requested value to sensor */
res = Dev->driver->write(BME280_CTRL_HUM_ADDR, HOvs, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* to make the change effective we need to write ctrl_meas register,
* check documentation */
res = Dev->driver->read(BME280_CTRL_MEAS_ADDR, &tmp, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
res = Dev->driver->write(BME280_CTRL_MEAS_ADDR, tmp, Dev->driver);
return res;
}
/* function sets standby time */
int8_t BME280_SetTStby(BME280_t *Dev, uint8_t TStby){
int8_t res = BME280_OK;
uint8_t config = 0, tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (TStby > BME280_STBY_20MS) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = (config >> 5) & 0x07;
if(TStby == tmp) return BME280_OK;
/* send new config value to sensor if required */
config &= 0x1F; //0x1F - 0b00011111
config |= (TStby << 5);
res = Dev->driver->write(BME280_CONFIG_ADDR, config, Dev->driver);
return res;
}
/* function sets IIR filter value */
int8_t BME280_SetFilter(BME280_t *Dev, uint8_t Filter){
int8_t res = BME280_OK;
uint8_t config = 0, tmp = 0;
/* check parameters */
if( IS_NULL(Dev) || (Filter > BME280_FILTER_16) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = (config >> 2) & 0x07;
if(Filter == tmp) return BME280_OK;
/* send new config value to sensor if required */
config &= 0xE3; //0xE3 - 0b11100011
config |= (Filter << 2);
res = Dev->driver->write(BME280_CONFIG_ADDR, config, Dev->driver);
return res;
}
/* function enables 3-wire SPI interface */
int8_t BME280_Enable3WireSPI(BME280_t *Dev){
int8_t res = BME280_OK;
uint8_t config = 0, tmp = 0;
/* check parameter */
if( IS_NULL(Dev) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = config & 0x01;
if(0x01 == tmp) return BME280_OK;
/* send new config value to sensor if required */
config &= 0xFE; //0xFE - 0b11111110
config |= 0x01;
res = Dev->driver->write(BME280_CONFIG_ADDR, config, Dev->driver);
return res;
}
/* function disables 3-wire SPI interface */
int8_t BME280_Disable3WireSPI(BME280_t *Dev){
int8_t res = BME280_OK;
uint8_t config = 0, tmp = 0;
/* check parameter */
if( IS_NULL(Dev) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* read value of config register from sensor */
res = Dev->driver->read(BME280_CONFIG_ADDR, &config, 1, Dev->driver);
if(BME280_OK != res) return BME280_INTERFACE_ERR;
/* check if current value differs from requested */
tmp = config & 0x01;
if(0x00 == tmp) return BME280_OK;
/* send new config value to sensor if required */
config &= 0xFE; //0xFE - 0b11111110
res = Dev->driver->write(BME280_CONFIG_ADDR, config, Dev->driver);
return res;
}
#endif
#ifdef USE_INTEGER_RESULTS
#ifdef USE_NORMAL_MODE
/* function reads last measured values from sensor in normal mode (no floats) */
int8_t BME280_ReadAllLast(BME280_t *Dev, BME280_Data_t *Data){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t press, hum;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Data) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in normal mode */
res = bme280_is_normal_mode(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_all, Dev, &temp, &press, &hum);
if(BME280_OK != res) return res;
/* convert 32bit values to Data structure */
bme280_convert_t_S32_struct(temp, Data);
bme280_convert_p_U32_struct(press, Data);
bme280_convert_h_U32_struct(hum, Data);
return res;
}
/* function reads last measured temperature from sensor in normal mode (no floats) */
int8_t BME280_ReadTempLast(BME280_t *Dev, int8_t *TempInt, uint8_t *TempFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_Data_t data;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(TempInt) || IS_NULL(TempFract) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in normal mode */
res = bme280_is_normal_mode(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_temp, Dev, &temp, 0, 0);
if(BME280_OK != res) return res;
/* convert 32bit values to local data structure */
bme280_convert_t_S32_struct(temp, &data);
/* set values of external variables */
*TempInt = data.temp_int;
*TempFract = data.temp_fract;
return res;
}
/* function reads last measured pressure from sensor in normal mode (no floats) */
int8_t BME280_ReadPressLast(BME280_t *Dev, uint16_t *PressInt, uint16_t *PressFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t press;
BME280_Data_t data;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(PressInt) || IS_NULL(PressFract) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in normal mode */
res = bme280_is_normal_mode(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_press, Dev, &temp, &press, 0);
if(BME280_OK != res) return res;
/* convert 32bit value to local data structure */
bme280_convert_p_U32_struct(press, &data);
/* set values of external variables */
*PressInt = data.pressure_int;
*PressFract = data.pressure_fract;
return res;
}
/* function reads last measured humidity from sensor in normal mode (no floats) */
int8_t BME280_ReadHumLast(BME280_t *Dev, uint8_t *HumInt, uint16_t *HumFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t hum;
BME280_Data_t data;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(HumInt) || IS_NULL(HumFract) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in normal mode */
res = bme280_is_normal_mode(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_hum, Dev, &temp, 0, &hum);
if(BME280_OK != res) return res;
/* convert 32bit value to local data structure */
bme280_convert_h_U32_struct(hum, &data);
/* set values of external variables */
*HumInt = data.humidity_int;
*HumFract = data.humidity_fract;
return res;
}
#endif
#ifdef USE_FORCED_MODE
/* function forces single measurement and reads all data (no floats) */
int8_t BME280_ReadAllForce(BME280_t *Dev, BME280_Data_t *Data){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t press, hum;
uint8_t delay;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(Data) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* force single measure */
res = bme280_set_forced_mode(Dev, &delay);
if(BME280_OK != res) return res;
/* wait until it ends */
Dev->driver->delay(delay);
/* check if measure is completed */
res = bme280_busy_check(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_all, Dev, &temp, &press, &hum);
if(BME280_OK != res) return res;
/* convert 32bit values to Data structure */
bme280_convert_t_S32_struct(temp, Data);
bme280_convert_p_U32_struct(press, Data);
bme280_convert_h_U32_struct(hum, Data);
return res;
}
/* function forces single measurement and reads the temperature (no floats) */
int8_t BME280_ReadTempForce(BME280_t *Dev, int8_t *TempInt, uint8_t *TempFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_Data_t data;
uint8_t delay;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(TempInt) || IS_NULL(TempFract) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* force single measure */
res = bme280_set_forced_mode(Dev, &delay);
if(BME280_OK != res) return res;
/* wait until it ends */
Dev->driver->delay(delay);
/* check if measure is completed */
res = bme280_busy_check(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_temp, Dev, &temp, 0, 0);
if(BME280_OK != res) return res;
/* convert 32bit values to local data structure */
bme280_convert_t_S32_struct(temp, &data);
/* set values of external variables */
*TempInt = data.temp_int;
*TempFract = data.temp_fract;
return res;
}
/* function forces single measurement and reads the pressure (no floats) */
int8_t BME280_ReadPressForce(BME280_t *Dev, uint16_t *PressInt, uint16_t *PressFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t press;
BME280_Data_t data;
uint8_t delay;
/* check parameters */
if( IS_NULL(Dev) || IS_NULL(PressInt) || IS_NULL(PressFract) ) return BME280_PARAM_ERR;
/* check if sensor is initialized and in sleep mode */
res = bme280_is_sleep_mode(Dev);
if(BME280_OK != res) return res;
/* force single measure */
res = bme280_set_forced_mode(Dev, &delay);
if(BME280_OK != res) return res;
/* wait until it ends */
Dev->driver->delay(delay);
/* check if measure is completed */
res = bme280_busy_check(Dev);
if(BME280_OK != res) return res;
/* read the data from sensor */
res = bme280_read_compensate(read_press, Dev, &temp, &press, 0);
if(BME280_OK != res) return res;
/* convert 32bit value to local data structure */
bme280_convert_p_U32_struct(press, &data);
/* set values of external variables */
*PressInt = data.pressure_int;
*PressFract = data.pressure_fract;
return res;
}
/* function forces single measurement and reads the humidity (no floats) */
int8_t BME280_ReadHumForce(BME280_t *Dev, uint8_t *HumInt, uint16_t *HumFract){
int8_t res = BME280_OK;
BME280_S32_t temp;
BME280_U32_t hum;