2 ****************************************************************************
3 * Copyright (C) 2013 - 2015 Bosch Sensortec GmbH
7 * Revision: 2.0.4(Pressure and Temperature compensation code revision is 1.1
8 * and Humidity compensation code revision is 1.0)
10 * Usage: Sensor Driver file for BME280 sensor
12 ****************************************************************************
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * Neither the name of the copyright holder nor the names of the
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY
35 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
36 * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
37 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
42 * ANY WAY OUT OF THE USE OF THIS
43 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
45 * The information provided is believed to be accurate and reliable.
46 * The copyright holder assumes no responsibility
47 * for the consequences of use
48 * of such information nor for any infringement of patents or
49 * other rights of third parties which may result from its use.
50 * No license is granted by implication or otherwise under any patent or
51 * patent rights of the copyright holder.
52 **************************************************************************/
55 static struct bme280_t *p_bme280; /**< pointer to BME280 */
58 * @brief This function is used for initialize
59 * the bus read and bus write functions
60 * and assign the chip id and I2C address of the BME280 sensor
61 * chip id is read in the register 0xD0 bit from 0 to 7
63 * @param bme280 structure pointer.
65 * @note While changing the parameter of the bme280_t
66 * @note consider the following point:
67 * Changing the reference value of the parameter
68 * will changes the local copy or local reference
69 * make sure your changes will not
70 * affect the reference value of the parameter
71 * (Better case don't change the reference value of the parameter)
76 * @return results of bus communication function
77 * @retval 0 -> Success
82 BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280)
84 /* used to return the communication result*/
85 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
86 u8 v_data_u8 = BME280_INIT_VALUE;
89 /* assign BME280 ptr */
90 com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
91 BME280_CHIP_ID_REG, &v_data_u8,
92 BME280_GEN_READ_WRITE_DATA_LENGTH);
94 p_bme280->chip_id = v_data_u8;
96 com_rslt += bme280_get_calib_param();
97 /* readout bme280 calibparam structure */
101 * @brief This API is used to read uncompensated temperature
102 * in the registers 0xFA, 0xFB and 0xFC
103 * @note 0xFA -> MSB -> bit from 0 to 7
104 * @note 0xFB -> LSB -> bit from 0 to 7
105 * @note 0xFC -> LSB -> bit from 4 to 7
107 * @param v_uncomp_temperature_s32 : The value of uncompensated temperature
111 * @return results of bus communication function
112 * @retval 0 -> Success
113 * @retval -1 -> Error
117 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
118 s32 *v_uncomp_temperature_s32)
120 /* used to return the communication result*/
121 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
122 /* Array holding the MSB and LSb value
123 a_data_u8r[0] - Temperature MSB
124 a_data_u8r[1] - Temperature LSB
125 a_data_u8r[2] - Temperature XLSB
127 u8 a_data_u8r[BME280_TEMPERATURE_DATA_SIZE] = {
128 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
129 /* check the p_bme280 structure pointer as NULL*/
130 if (p_bme280 == BME280_NULL) {
131 return E_BME280_NULL_PTR;
133 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
135 BME280_TEMPERATURE_MSB_REG,
137 BME280_TEMPERATURE_DATA_LENGTH);
138 *v_uncomp_temperature_s32 = (s32)(((
139 (u32) (a_data_u8r[BME280_TEMPERATURE_MSB_DATA]))
140 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
141 (((u32)(a_data_u8r[BME280_TEMPERATURE_LSB_DATA]))
142 << BME280_SHIFT_BIT_POSITION_BY_04_BITS)
143 | ((u32)a_data_u8r[BME280_TEMPERATURE_XLSB_DATA] >>
144 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
149 * @brief Reads actual temperature from uncompensated temperature
150 * @note Returns the value in 0.01 degree Centigrade
151 * Output value of "5123" equals 51.23 DegC.
155 * @param v_uncomp_temperature_s32 : value of uncompensated temperature
158 * @return Returns the actual temperature
161 s32 bme280_compensate_temperature_int32(s32 v_uncomp_temperature_s32)
163 s32 v_x1_u32r = BME280_INIT_VALUE;
164 s32 v_x2_u32r = BME280_INIT_VALUE;
165 s32 temperature = BME280_INIT_VALUE;
169 ((((v_uncomp_temperature_s32
170 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS) -
171 ((s32)p_bme280->cal_param.dig_T1
172 << BME280_SHIFT_BIT_POSITION_BY_01_BIT))) *
173 ((s32)p_bme280->cal_param.dig_T2)) >>
174 BME280_SHIFT_BIT_POSITION_BY_11_BITS;
176 v_x2_u32r = (((((v_uncomp_temperature_s32
177 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
178 ((s32)p_bme280->cal_param.dig_T1))
179 * ((v_uncomp_temperature_s32 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
180 ((s32)p_bme280->cal_param.dig_T1)))
181 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS) *
182 ((s32)p_bme280->cal_param.dig_T3))
183 >> BME280_SHIFT_BIT_POSITION_BY_14_BITS;
184 /* calculate t_fine*/
185 p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
186 /* calculate temperature*/
187 temperature = (p_bme280->cal_param.t_fine * 5 + 128)
188 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS;
192 * @brief Reads actual temperature from uncompensated temperature
193 * @note Returns the value with 500LSB/DegC centred around 24 DegC
194 * output value of "5123" equals(5123/500)+24 = 34.246DegC
197 * @param v_uncomp_temperature_s32: value of uncompensated temperature
201 * @return Return the actual temperature as s16 output
204 s16 bme280_compensate_temperature_int32_sixteen_bit_output(
205 s32 v_uncomp_temperature_s32)
207 s16 temperature = BME280_INIT_VALUE;
209 bme280_compensate_temperature_int32(
210 v_uncomp_temperature_s32);
211 temperature = (s16)((((
212 p_bme280->cal_param.t_fine - 122880) * 25) + 128)
213 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS);
218 * @brief This API is used to read uncompensated pressure.
219 * in the registers 0xF7, 0xF8 and 0xF9
220 * @note 0xF7 -> MSB -> bit from 0 to 7
221 * @note 0xF8 -> LSB -> bit from 0 to 7
222 * @note 0xF9 -> LSB -> bit from 4 to 7
226 * @param v_uncomp_pressure_s32 : The value of uncompensated pressure
230 * @return results of bus communication function
231 * @retval 0 -> Success
232 * @retval -1 -> Error
236 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
237 s32 *v_uncomp_pressure_s32)
239 /* used to return the communication result*/
240 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
241 /* Array holding the MSB and LSb value
242 a_data_u8[0] - Pressure MSB
243 a_data_u8[1] - Pressure LSB
244 a_data_u8[2] - Pressure XLSB
246 u8 a_data_u8[BME280_PRESSURE_DATA_SIZE] = {
247 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
248 /* check the p_bme280 structure pointer as NULL*/
249 if (p_bme280 == BME280_NULL) {
250 return E_BME280_NULL_PTR;
252 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
254 BME280_PRESSURE_MSB_REG,
255 a_data_u8, BME280_PRESSURE_DATA_LENGTH);
256 *v_uncomp_pressure_s32 = (s32)((
257 ((u32)(a_data_u8[BME280_PRESSURE_MSB_DATA]))
258 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
259 (((u32)(a_data_u8[BME280_PRESSURE_LSB_DATA]))
260 << BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
261 ((u32)a_data_u8[BME280_PRESSURE_XLSB_DATA] >>
262 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
267 * @brief Reads actual pressure from uncompensated pressure
268 * @note Returns the value in Pascal(Pa)
269 * Output value of "96386" equals 96386 Pa =
270 * 963.86 hPa = 963.86 millibar
274 * @param v_uncomp_pressure_s32 : value of uncompensated pressure
278 * @return Return the actual pressure output as u32
281 u32 bme280_compensate_pressure_int32(s32 v_uncomp_pressure_s32)
283 s32 v_x1_u32 = BME280_INIT_VALUE;
284 s32 v_x2_u32 = BME280_INIT_VALUE;
285 u32 v_pressure_u32 = BME280_INIT_VALUE;
288 v_x1_u32 = (((s32)p_bme280->cal_param.t_fine)
289 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) - (s32)64000;
291 v_x2_u32 = (((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
292 * (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
293 ) >> BME280_SHIFT_BIT_POSITION_BY_11_BITS)
294 * ((s32)p_bme280->cal_param.dig_P6);
296 v_x2_u32 = v_x2_u32 + ((v_x1_u32 *
297 ((s32)p_bme280->cal_param.dig_P5))
298 << BME280_SHIFT_BIT_POSITION_BY_01_BIT);
300 v_x2_u32 = (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) +
301 (((s32)p_bme280->cal_param.dig_P4)
302 << BME280_SHIFT_BIT_POSITION_BY_16_BITS);
304 v_x1_u32 = (((p_bme280->cal_param.dig_P3 *
305 (((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) *
306 (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS))
307 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
308 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS) +
309 ((((s32)p_bme280->cal_param.dig_P2) *
310 v_x1_u32) >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
311 >> BME280_SHIFT_BIT_POSITION_BY_18_BITS;
313 v_x1_u32 = ((((32768 + v_x1_u32)) *
314 ((s32)p_bme280->cal_param.dig_P1))
315 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS);
316 /* calculate pressure*/
318 (((u32)(((s32)1048576) - v_uncomp_pressure_s32)
319 - (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS))) * 3125;
322 /* Avoid exception caused by division by zero */
323 if (v_x1_u32 != BME280_INIT_VALUE)
326 << BME280_SHIFT_BIT_POSITION_BY_01_BIT) /
329 return BME280_INVALID_DATA;
331 /* Avoid exception caused by division by zero */
332 if (v_x1_u32 != BME280_INIT_VALUE)
333 v_pressure_u32 = (v_pressure_u32
334 / (u32)v_x1_u32) * 2;
336 return BME280_INVALID_DATA;
338 v_x1_u32 = (((s32)p_bme280->cal_param.dig_P9) *
339 ((s32)(((v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS)
340 * (v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS))
341 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS)))
342 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS;
343 v_x2_u32 = (((s32)(v_pressure_u32
344 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)) *
345 ((s32)p_bme280->cal_param.dig_P8))
346 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS;
347 v_pressure_u32 = (u32)((s32)v_pressure_u32 +
348 ((v_x1_u32 + v_x2_u32 + p_bme280->cal_param.dig_P7)
349 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
351 return v_pressure_u32;
354 * @brief This API is used to read uncompensated humidity.
355 * in the registers 0xF7, 0xF8 and 0xF9
356 * @note 0xFD -> MSB -> bit from 0 to 7
357 * @note 0xFE -> LSB -> bit from 0 to 7
361 * @param v_uncomp_humidity_s32 : The value of uncompensated humidity
365 * @return results of bus communication function
366 * @retval 0 -> Success
367 * @retval -1 -> Error
371 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_humidity(
372 s32 *v_uncomp_humidity_s32)
374 /* used to return the communication result*/
375 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
376 /* Array holding the MSB and LSb value
377 a_data_u8[0] - Humidity MSB
378 a_data_u8[1] - Humidity LSB
380 u8 a_data_u8[BME280_HUMIDITY_DATA_SIZE] = {
381 BME280_INIT_VALUE, BME280_INIT_VALUE};
382 /* check the p_bme280 structure pointer as NULL*/
383 if (p_bme280 == BME280_NULL) {
384 return E_BME280_NULL_PTR;
386 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
388 BME280_HUMIDITY_MSB_REG, a_data_u8,
389 BME280_HUMIDITY_DATA_LENGTH);
390 *v_uncomp_humidity_s32 = (s32)(
391 (((u32)(a_data_u8[BME280_HUMIDITY_MSB_DATA]))
392 << BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
393 ((u32)(a_data_u8[BME280_HUMIDITY_LSB_DATA])));
398 * @brief Reads actual humidity from uncompensated humidity
399 * @note Returns the value in %rH as unsigned 32bit integer
400 * in Q22.10 format(22 integer 10 fractional bits).
401 * @note An output value of 42313
402 * represents 42313 / 1024 = 41.321 %rH
406 * @param v_uncomp_humidity_s32: value of uncompensated humidity
408 * @return Return the actual relative humidity output as u32
411 u32 bme280_compensate_humidity_int32(s32 v_uncomp_humidity_s32)
413 s32 v_x1_u32 = BME280_INIT_VALUE;
416 v_x1_u32 = (p_bme280->cal_param.t_fine - ((s32)76800));
418 v_x1_u32 = (((((v_uncomp_humidity_s32
419 << BME280_SHIFT_BIT_POSITION_BY_14_BITS) -
420 (((s32)p_bme280->cal_param.dig_H4)
421 << BME280_SHIFT_BIT_POSITION_BY_20_BITS) -
422 (((s32)p_bme280->cal_param.dig_H5) * v_x1_u32)) +
423 ((s32)16384)) >> BME280_SHIFT_BIT_POSITION_BY_15_BITS)
425 ((s32)p_bme280->cal_param.dig_H6))
426 >> BME280_SHIFT_BIT_POSITION_BY_10_BITS) *
427 (((v_x1_u32 * ((s32)p_bme280->cal_param.dig_H3))
428 >> BME280_SHIFT_BIT_POSITION_BY_11_BITS) + ((s32)32768)))
429 >> BME280_SHIFT_BIT_POSITION_BY_10_BITS) + ((s32)2097152)) *
430 ((s32)p_bme280->cal_param.dig_H2) + 8192) >> 14));
431 v_x1_u32 = (v_x1_u32 - (((((v_x1_u32
432 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS) *
433 (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS))
434 >> BME280_SHIFT_BIT_POSITION_BY_07_BITS) *
435 ((s32)p_bme280->cal_param.dig_H1))
436 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
437 v_x1_u32 = (v_x1_u32 < 0 ? 0 : v_x1_u32);
438 v_x1_u32 = (v_x1_u32 > 419430400 ?
439 419430400 : v_x1_u32);
440 return (u32)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS);
443 * @brief Reads actual humidity from uncompensated humidity
444 * @note Returns the value in %rH as unsigned 16bit integer
445 * @note An output value of 42313
446 * represents 42313/512 = 82.643 %rH
450 * @param v_uncomp_humidity_s32: value of uncompensated humidity
453 * @return Return the actual relative humidity output as u16
456 u16 bme280_compensate_humidity_int32_sixteen_bit_output(
457 s32 v_uncomp_humidity_s32)
459 u32 v_x1_u32 = BME280_INIT_VALUE;
460 u16 v_x2_u32 = BME280_INIT_VALUE;
462 v_x1_u32 = bme280_compensate_humidity_int32(v_uncomp_humidity_s32);
463 v_x2_u32 = (u16)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
467 * @brief This API used to read uncompensated
468 * pressure,temperature and humidity
473 * @param v_uncomp_pressure_s32: The value of uncompensated pressure.
474 * @param v_uncomp_temperature_s32: The value of uncompensated temperature
475 * @param v_uncomp_humidity_s32: The value of uncompensated humidity.
479 * @return results of bus communication function
480 * @retval 0 -> Success
481 * @retval -1 -> Error
485 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure_temperature_humidity(
486 s32 *v_uncomp_pressure_s32,
487 s32 *v_uncomp_temperature_s32, s32 *v_uncomp_humidity_s32)
489 /* used to return the communication result*/
490 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
491 /* Array holding the MSB and LSb value of
492 a_data_u8[0] - Pressure MSB
493 a_data_u8[1] - Pressure LSB
494 a_data_u8[1] - Pressure LSB
495 a_data_u8[1] - Temperature MSB
496 a_data_u8[1] - Temperature LSB
497 a_data_u8[1] - Temperature LSB
498 a_data_u8[1] - Humidity MSB
499 a_data_u8[1] - Humidity LSB
501 u8 a_data_u8[BME280_DATA_FRAME_SIZE] = {
502 BME280_INIT_VALUE, BME280_INIT_VALUE,
503 BME280_INIT_VALUE, BME280_INIT_VALUE,
504 BME280_INIT_VALUE, BME280_INIT_VALUE,
505 BME280_INIT_VALUE, BME280_INIT_VALUE};
506 /* check the p_bme280 structure pointer as NULL*/
507 if (p_bme280 == BME280_NULL) {
508 return E_BME280_NULL_PTR;
510 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
512 BME280_PRESSURE_MSB_REG,
513 a_data_u8, BME280_ALL_DATA_FRAME_LENGTH);
515 *v_uncomp_pressure_s32 = (s32)((
517 BME280_DATA_FRAME_PRESSURE_MSB_BYTE]))
518 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
520 BME280_DATA_FRAME_PRESSURE_LSB_BYTE]))
521 << BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
523 BME280_DATA_FRAME_PRESSURE_XLSB_BYTE] >>
524 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
527 *v_uncomp_temperature_s32 = (s32)(((
529 BME280_DATA_FRAME_TEMPERATURE_MSB_BYTE]))
530 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
532 BME280_DATA_FRAME_TEMPERATURE_LSB_BYTE]))
533 << BME280_SHIFT_BIT_POSITION_BY_04_BITS)
535 BME280_DATA_FRAME_TEMPERATURE_XLSB_BYTE]
536 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
539 *v_uncomp_humidity_s32 = (s32)((
541 BME280_DATA_FRAME_HUMIDITY_MSB_BYTE]))
542 << BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
544 BME280_DATA_FRAME_HUMIDITY_LSB_BYTE])));
549 * @brief This API used to read true pressure, temperature and humidity
554 * @param v_pressure_u32 : The value of compensated pressure.
555 * @param v_temperature_s32 : The value of compensated temperature.
556 * @param v_humidity_u32 : The value of compensated humidity.
559 * @return results of bus communication function
560 * @retval 0 -> Success
561 * @retval -1 -> Error
565 BME280_RETURN_FUNCTION_TYPE bme280_read_pressure_temperature_humidity(
566 u32 *v_pressure_u32, s32 *v_temperature_s32, u32 *v_humidity_u32)
568 /* used to return the communication result*/
569 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
570 s32 v_uncomp_pressure_s32 = BME280_INIT_VALUE;
571 s32 v_uncom_temperature_s32 = BME280_INIT_VALUE;
572 s32 v_uncom_humidity_s32 = BME280_INIT_VALUE;
573 /* check the p_bme280 structure pointer as NULL*/
574 if (p_bme280 == BME280_NULL) {
575 return E_BME280_NULL_PTR;
577 /* read the uncompensated pressure,
578 temperature and humidity*/
580 bme280_read_uncomp_pressure_temperature_humidity(
581 &v_uncomp_pressure_s32, &v_uncom_temperature_s32,
582 &v_uncom_humidity_s32);
583 /* read the true pressure, temperature and humidity*/
585 bme280_compensate_temperature_int32(
586 v_uncom_temperature_s32);
587 *v_pressure_u32 = bme280_compensate_pressure_int32(
588 v_uncomp_pressure_s32);
589 *v_humidity_u32 = bme280_compensate_humidity_int32(
590 v_uncom_humidity_s32);
595 * @brief This API is used to
596 * calibration parameters used for calculation in the registers
598 * parameter | Register address | bit
599 *------------|------------------|----------------
600 * dig_T1 | 0x88 and 0x89 | from 0 : 7 to 8: 15
601 * dig_T2 | 0x8A and 0x8B | from 0 : 7 to 8: 15
602 * dig_T3 | 0x8C and 0x8D | from 0 : 7 to 8: 15
603 * dig_P1 | 0x8E and 0x8F | from 0 : 7 to 8: 15
604 * dig_P2 | 0x90 and 0x91 | from 0 : 7 to 8: 15
605 * dig_P3 | 0x92 and 0x93 | from 0 : 7 to 8: 15
606 * dig_P4 | 0x94 and 0x95 | from 0 : 7 to 8: 15
607 * dig_P5 | 0x96 and 0x97 | from 0 : 7 to 8: 15
608 * dig_P6 | 0x98 and 0x99 | from 0 : 7 to 8: 15
609 * dig_P7 | 0x9A and 0x9B | from 0 : 7 to 8: 15
610 * dig_P8 | 0x9C and 0x9D | from 0 : 7 to 8: 15
611 * dig_P9 | 0x9E and 0x9F | from 0 : 7 to 8: 15
612 * dig_H1 | 0xA1 | from 0 to 7
613 * dig_H2 | 0xE1 and 0xE2 | from 0 : 7 to 8: 15
614 * dig_H3 | 0xE3 | from 0 to 7
616 * @return results of bus communication function
617 * @retval 0 -> Success
618 * @retval -1 -> Error
622 BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param(void)
624 /* used to return the communication result*/
625 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
626 u8 a_data_u8[BME280_CALIB_DATA_SIZE] = {
627 BME280_INIT_VALUE, BME280_INIT_VALUE,
628 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
629 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
630 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
631 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
632 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
633 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
634 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
635 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
636 /* check the p_bme280 structure pointer as NULL*/
637 if (p_bme280 == BME280_NULL) {
638 return E_BME280_NULL_PTR;
640 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
642 BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
644 BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH);
646 p_bme280->cal_param.dig_T1 = (u16)(((
648 BME280_TEMPERATURE_CALIB_DIG_T1_MSB])) <<
649 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
650 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T1_LSB]);
651 p_bme280->cal_param.dig_T2 = (s16)(((
653 BME280_TEMPERATURE_CALIB_DIG_T2_MSB])) <<
654 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
655 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T2_LSB]);
656 p_bme280->cal_param.dig_T3 = (s16)(((
658 BME280_TEMPERATURE_CALIB_DIG_T3_MSB])) <<
659 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
660 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T3_LSB]);
661 p_bme280->cal_param.dig_P1 = (u16)(((
663 BME280_PRESSURE_CALIB_DIG_P1_MSB])) <<
664 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
665 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P1_LSB]);
666 p_bme280->cal_param.dig_P2 = (s16)(((
668 BME280_PRESSURE_CALIB_DIG_P2_MSB])) <<
669 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
670 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P2_LSB]);
671 p_bme280->cal_param.dig_P3 = (s16)(((
673 BME280_PRESSURE_CALIB_DIG_P3_MSB])) <<
674 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
676 BME280_PRESSURE_CALIB_DIG_P3_LSB]);
677 p_bme280->cal_param.dig_P4 = (s16)(((
679 BME280_PRESSURE_CALIB_DIG_P4_MSB])) <<
680 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
681 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P4_LSB]);
682 p_bme280->cal_param.dig_P5 = (s16)(((
684 BME280_PRESSURE_CALIB_DIG_P5_MSB])) <<
685 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
686 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P5_LSB]);
687 p_bme280->cal_param.dig_P6 = (s16)(((
689 BME280_PRESSURE_CALIB_DIG_P6_MSB])) <<
690 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
691 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P6_LSB]);
692 p_bme280->cal_param.dig_P7 = (s16)(((
694 BME280_PRESSURE_CALIB_DIG_P7_MSB])) <<
695 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
696 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P7_LSB]);
697 p_bme280->cal_param.dig_P8 = (s16)(((
699 BME280_PRESSURE_CALIB_DIG_P8_MSB])) <<
700 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
701 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P8_LSB]);
702 p_bme280->cal_param.dig_P9 = (s16)(((
704 BME280_PRESSURE_CALIB_DIG_P9_MSB])) <<
705 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
706 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P9_LSB]);
707 p_bme280->cal_param.dig_H1 =
708 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H1];
709 com_rslt += p_bme280->BME280_BUS_READ_FUNC(
711 BME280_HUMIDITY_CALIB_DIG_H2_LSB_REG, a_data_u8,
712 BME280_HUMIDITY_CALIB_DATA_LENGTH);
713 p_bme280->cal_param.dig_H2 = (s16)(((
715 BME280_HUMIDITY_CALIB_DIG_H2_MSB])) <<
716 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
717 | a_data_u8[BME280_HUMIDITY_CALIB_DIG_H2_LSB]);
718 p_bme280->cal_param.dig_H3 =
719 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H3];
720 p_bme280->cal_param.dig_H4 = (s16)(((
722 BME280_HUMIDITY_CALIB_DIG_H4_MSB])) <<
723 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
724 (((u8)BME280_MASK_DIG_H4) &
725 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB]));
726 p_bme280->cal_param.dig_H5 = (s16)(((
728 BME280_HUMIDITY_CALIB_DIG_H5_MSB])) <<
729 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
730 (a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB] >>
731 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
732 p_bme280->cal_param.dig_H6 =
733 (s8)a_data_u8[BME280_HUMIDITY_CALIB_DIG_H6];
738 * @brief This API is used to get
739 * the temperature oversampling setting in the register 0xF4
742 * value | Temperature oversampling
743 * ---------------------|---------------------------------
745 * 0x01 | BME280_OVERSAMP_1X
746 * 0x02 | BME280_OVERSAMP_2X
747 * 0x03 | BME280_OVERSAMP_4X
748 * 0x04 | BME280_OVERSAMP_8X
749 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
752 * @param v_value_u8 : The value of temperature over sampling
756 * @return results of bus communication function
757 * @retval 0 -> Success
758 * @retval -1 -> Error
762 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
765 /* used to return the communication result*/
766 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
767 u8 v_data_u8 = BME280_INIT_VALUE;
768 /* check the p_bme280 structure pointer as NULL*/
769 if (p_bme280 == BME280_NULL) {
770 return E_BME280_NULL_PTR;
772 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
774 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
775 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
776 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
777 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
779 p_bme280->oversamp_temperature = *v_value_u8;
784 * @brief This API is used to set
785 * the temperature oversampling setting in the register 0xF4
788 * value | Temperature oversampling
789 * ---------------------|---------------------------------
791 * 0x01 | BME280_OVERSAMP_1X
792 * 0x02 | BME280_OVERSAMP_2X
793 * 0x03 | BME280_OVERSAMP_4X
794 * 0x04 | BME280_OVERSAMP_8X
795 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
798 * @param v_value_u8 : The value of temperature over sampling
802 * @return results of bus communication function
803 * @retval 0 -> Success
804 * @retval -1 -> Error
808 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
811 /* used to return the communication result*/
812 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
813 u8 v_data_u8 = BME280_INIT_VALUE;
814 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
815 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
816 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
817 /* check the p_bme280 structure pointer as NULL*/
818 if (p_bme280 == BME280_NULL) {
819 return E_BME280_NULL_PTR;
821 v_data_u8 = p_bme280->ctrl_meas_reg;
823 BME280_SET_BITSLICE(v_data_u8,
824 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
825 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
826 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
827 com_rslt += bme280_set_soft_rst();
828 p_bme280->delay_msec(BME280_3MS_DELAY);
829 /* write previous value
830 of configuration register*/
831 v_pre_config_value_u8 = p_bme280->config_reg;
832 com_rslt += bme280_write_register(
834 &v_pre_config_value_u8,
835 BME280_GEN_READ_WRITE_DATA_LENGTH);
836 /* write previous value
837 of humidity oversampling*/
838 v_pre_ctrl_hum_value_u8 =
839 p_bme280->ctrl_hum_reg;
840 com_rslt += bme280_write_register(
841 BME280_CTRL_HUMIDITY_REG,
842 &v_pre_ctrl_hum_value_u8,
843 BME280_GEN_READ_WRITE_DATA_LENGTH);
844 /* write previous and updated value
845 of configuration register*/
846 com_rslt += bme280_write_register(
847 BME280_CTRL_MEAS_REG,
848 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
850 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
852 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
853 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
855 p_bme280->oversamp_temperature = v_value_u8;
856 /* read the control measurement register value*/
857 com_rslt = bme280_read_register(
858 BME280_CTRL_MEAS_REG,
859 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
860 p_bme280->ctrl_meas_reg = v_data_u8;
861 /* read the control humidity register value*/
862 com_rslt += bme280_read_register(
863 BME280_CTRL_HUMIDITY_REG,
864 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
865 p_bme280->ctrl_hum_reg = v_data_u8;
867 configuration register value*/
868 com_rslt += bme280_read_register(
870 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
871 p_bme280->config_reg = v_data_u8;
876 * @brief This API is used to get
877 * the pressure oversampling setting in the register 0xF4
880 * value | Pressure oversampling
881 * --------------------|--------------------------
883 * 0x01 | BME280_OVERSAMP_1X
884 * 0x02 | BME280_OVERSAMP_2X
885 * 0x03 | BME280_OVERSAMP_4X
886 * 0x04 | BME280_OVERSAMP_8X
887 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
890 * @param v_value_u8 : The value of pressure oversampling
894 * @return results of bus communication function
895 * @retval 0 -> Success
896 * @retval -1 -> Error
900 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
903 /* used to return the communication result*/
904 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
905 u8 v_data_u8 = BME280_INIT_VALUE;
906 /* check the p_bme280 structure pointer as NULL*/
907 if (p_bme280 == BME280_NULL) {
908 return E_BME280_NULL_PTR;
910 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
912 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
913 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
914 *v_value_u8 = BME280_GET_BITSLICE(
916 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
918 p_bme280->oversamp_pressure = *v_value_u8;
923 * @brief This API is used to set
924 * the pressure oversampling setting in the register 0xF4
927 * value | Pressure oversampling
928 * --------------------|--------------------------
930 * 0x01 | BME280_OVERSAMP_1X
931 * 0x02 | BME280_OVERSAMP_2X
932 * 0x03 | BME280_OVERSAMP_4X
933 * 0x04 | BME280_OVERSAMP_8X
934 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
937 * @param v_value_u8 : The value of pressure oversampling
941 * @return results of bus communication function
942 * @retval 0 -> Success
943 * @retval -1 -> Error
947 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
950 /* used to return the communication result*/
951 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
952 u8 v_data_u8 = BME280_INIT_VALUE;
953 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
954 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
955 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
956 /* check the p_bme280 structure pointer as NULL*/
957 if (p_bme280 == BME280_NULL) {
958 return E_BME280_NULL_PTR;
960 v_data_u8 = p_bme280->ctrl_meas_reg;
962 BME280_SET_BITSLICE(v_data_u8,
963 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
964 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
965 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
966 com_rslt += bme280_set_soft_rst();
967 p_bme280->delay_msec(BME280_3MS_DELAY);
968 /* write previous value of
969 configuration register*/
970 v_pre_config_value_u8 = p_bme280->config_reg;
971 com_rslt = bme280_write_register(
973 &v_pre_config_value_u8,
974 BME280_GEN_READ_WRITE_DATA_LENGTH);
975 /* write previous value of
976 humidity oversampling*/
977 v_pre_ctrl_hum_value_u8 =
978 p_bme280->ctrl_hum_reg;
979 com_rslt += bme280_write_register(
980 BME280_CTRL_HUMIDITY_REG,
981 &v_pre_ctrl_hum_value_u8,
982 BME280_GEN_READ_WRITE_DATA_LENGTH);
983 /* write previous and updated value of
984 control measurement register*/
985 bme280_write_register(
986 BME280_CTRL_MEAS_REG,
987 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
989 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
991 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
992 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
994 p_bme280->oversamp_pressure = v_value_u8;
995 /* read the control measurement register value*/
996 com_rslt = bme280_read_register(
997 BME280_CTRL_MEAS_REG,
998 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
999 p_bme280->ctrl_meas_reg = v_data_u8;
1000 /* read the control humidity register value*/
1001 com_rslt += bme280_read_register(
1002 BME280_CTRL_HUMIDITY_REG,
1003 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1004 p_bme280->ctrl_hum_reg = v_data_u8;
1006 configuration register value*/
1007 com_rslt += bme280_read_register(
1009 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1010 p_bme280->config_reg = v_data_u8;
1015 * @brief This API is used to get
1016 * the humidity oversampling setting in the register 0xF2
1019 * value | Humidity oversampling
1020 * ---------------------|-------------------------
1022 * 0x01 | BME280_OVERSAMP_1X
1023 * 0x02 | BME280_OVERSAMP_2X
1024 * 0x03 | BME280_OVERSAMP_4X
1025 * 0x04 | BME280_OVERSAMP_8X
1026 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1029 * @param v_value_u8 : The value of humidity over sampling
1033 * @return results of bus communication function
1034 * @retval 0 -> Success
1035 * @retval -1 -> Error
1039 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
1042 /* used to return the communication result*/
1043 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1044 u8 v_data_u8 = BME280_INIT_VALUE;
1045 /* check the p_bme280 structure pointer as NULL*/
1046 if (p_bme280 == BME280_NULL) {
1047 return E_BME280_NULL_PTR;
1049 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1051 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1052 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1053 *v_value_u8 = BME280_GET_BITSLICE(
1055 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY);
1057 p_bme280->oversamp_humidity = *v_value_u8;
1062 * @brief This API is used to set
1063 * the humidity oversampling setting in the register 0xF2
1066 * value | Humidity oversampling
1067 * ---------------------|-------------------------
1069 * 0x01 | BME280_OVERSAMP_1X
1070 * 0x02 | BME280_OVERSAMP_2X
1071 * 0x03 | BME280_OVERSAMP_4X
1072 * 0x04 | BME280_OVERSAMP_8X
1073 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1076 * @param v_value_u8 : The value of humidity over sampling
1080 * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1081 * register sets the humidity
1082 * data acquisition options of the device.
1083 * @note changes to this registers only become
1084 * effective after a write operation to
1085 * "BME280_CTRL_MEAS_REG" register.
1086 * @note In the code automated reading and writing of
1087 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1088 * @note register first set the
1089 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1090 * and then read and write
1091 * the "BME280_CTRL_MEAS_REG" register in the function.
1094 * @return results of bus communication function
1095 * @retval 0 -> Success
1096 * @retval -1 -> Error
1100 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
1103 /* used to return the communication result*/
1104 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1105 u8 v_data_u8 = BME280_INIT_VALUE;
1106 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1107 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1108 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1109 /* check the p_bme280 structure pointer as NULL*/
1110 if (p_bme280 == BME280_NULL) {
1111 return E_BME280_NULL_PTR;
1113 /* write humidity oversampling*/
1114 v_data_u8 = p_bme280->ctrl_hum_reg;
1116 BME280_SET_BITSLICE(v_data_u8,
1117 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY, v_value_u8);
1118 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1119 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1120 com_rslt += bme280_set_soft_rst();
1121 p_bme280->delay_msec(BME280_3MS_DELAY);
1122 /* write previous value of
1123 configuration register*/
1124 v_pre_config_value_u8 = p_bme280->config_reg;
1125 com_rslt += bme280_write_register(
1127 &v_pre_config_value_u8,
1128 BME280_GEN_READ_WRITE_DATA_LENGTH);
1129 /* write the value of control humidity*/
1130 com_rslt += bme280_write_register(
1131 BME280_CTRL_HUMIDITY_REG,
1132 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1133 /* write previous value of
1134 control measurement register*/
1135 pre_ctrl_meas_value =
1136 p_bme280->ctrl_meas_reg;
1137 com_rslt += bme280_write_register(
1138 BME280_CTRL_MEAS_REG,
1139 &pre_ctrl_meas_value,
1140 BME280_GEN_READ_WRITE_DATA_LENGTH);
1143 p_bme280->BME280_BUS_WRITE_FUNC(
1145 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1146 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1147 /* Control humidity write will effective only
1148 after the control measurement register*/
1149 pre_ctrl_meas_value =
1150 p_bme280->ctrl_meas_reg;
1151 com_rslt += bme280_write_register(
1152 BME280_CTRL_MEAS_REG,
1153 &pre_ctrl_meas_value,
1154 BME280_GEN_READ_WRITE_DATA_LENGTH);
1156 p_bme280->oversamp_humidity = v_value_u8;
1157 /* read the control measurement register value*/
1158 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1159 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1160 p_bme280->ctrl_meas_reg = v_data_u8;
1161 /* read the control humidity register value*/
1162 com_rslt += bme280_read_register(
1163 BME280_CTRL_HUMIDITY_REG,
1164 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1165 p_bme280->ctrl_hum_reg = v_data_u8;
1166 /* read the control configuration register value*/
1167 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1168 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1169 p_bme280->config_reg = v_data_u8;
1174 * @brief This API used to get the
1175 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1179 * @param v_power_mode_u8 : The value of power mode
1181 * -----------------|------------------
1182 * 0x00 | BME280_SLEEP_MODE
1183 * 0x01 and 0x02 | BME280_FORCED_MODE
1184 * 0x03 | BME280_NORMAL_MODE
1186 * @return results of bus communication function
1187 * @retval 0 -> Success
1188 * @retval -1 -> Error
1192 BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
1194 /* used to return the communication result*/
1195 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1196 u8 v_mode_u8r = BME280_INIT_VALUE;
1197 /* check the p_bme280 structure pointer as NULL*/
1198 if (p_bme280 == BME280_NULL) {
1199 return E_BME280_NULL_PTR;
1201 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1203 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1204 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1205 *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
1206 BME280_CTRL_MEAS_REG_POWER_MODE);
1211 * @brief This API used to set the
1212 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1216 * @param v_power_mode_u8 : The value of power mode
1218 * -----------------|------------------
1219 * 0x00 | BME280_SLEEP_MODE
1220 * 0x01 and 0x02 | BME280_FORCED_MODE
1221 * 0x03 | BME280_NORMAL_MODE
1223 * @return results of bus communication function
1224 * @retval 0 -> Success
1225 * @retval -1 -> Error
1229 BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
1231 /* used to return the communication result*/
1232 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1233 u8 v_mode_u8r = BME280_INIT_VALUE;
1234 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1235 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1236 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1237 u8 v_data_u8 = BME280_INIT_VALUE;
1238 /* check the p_bme280 structure pointer as NULL*/
1239 if (p_bme280 == BME280_NULL) {
1240 return E_BME280_NULL_PTR;
1242 if (v_power_mode_u8 <= BME280_NORMAL_MODE) {
1243 v_mode_u8r = p_bme280->ctrl_meas_reg;
1245 BME280_SET_BITSLICE(v_mode_u8r,
1246 BME280_CTRL_MEAS_REG_POWER_MODE,
1248 com_rslt = bme280_get_power_mode(
1249 &v_prev_pow_mode_u8);
1250 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1251 com_rslt += bme280_set_soft_rst();
1252 p_bme280->delay_msec(BME280_3MS_DELAY);
1253 /* write previous value of
1254 configuration register*/
1255 v_pre_config_value_u8 =
1256 p_bme280->config_reg;
1257 com_rslt = bme280_write_register(
1259 &v_pre_config_value_u8,
1260 BME280_GEN_READ_WRITE_DATA_LENGTH);
1261 /* write previous value of
1262 humidity oversampling*/
1263 v_pre_ctrl_hum_value_u8 =
1264 p_bme280->ctrl_hum_reg;
1265 com_rslt += bme280_write_register(
1266 BME280_CTRL_HUMIDITY_REG,
1267 &v_pre_ctrl_hum_value_u8,
1268 BME280_GEN_READ_WRITE_DATA_LENGTH);
1269 /* write previous and updated value of
1270 control measurement register*/
1271 com_rslt += bme280_write_register(
1272 BME280_CTRL_MEAS_REG,
1274 BME280_GEN_READ_WRITE_DATA_LENGTH);
1277 p_bme280->BME280_BUS_WRITE_FUNC(
1279 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1281 BME280_GEN_READ_WRITE_DATA_LENGTH);
1283 /* read the control measurement register value*/
1284 com_rslt = bme280_read_register(
1285 BME280_CTRL_MEAS_REG,
1286 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1287 p_bme280->ctrl_meas_reg = v_data_u8;
1288 /* read the control humidity register value*/
1289 com_rslt += bme280_read_register(
1290 BME280_CTRL_HUMIDITY_REG,
1291 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1292 p_bme280->ctrl_hum_reg = v_data_u8;
1293 /* read the config register value*/
1294 com_rslt += bme280_read_register(
1296 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1297 p_bme280->config_reg = v_data_u8;
1299 com_rslt = E_BME280_OUT_OF_RANGE;
1305 * @brief Used to reset the sensor
1306 * The value 0xB6 is written to the 0xE0
1307 * register the device is reset using the
1308 * complete power-on-reset procedure.
1309 * @note Soft reset can be easily set using bme280_set_softreset().
1310 * @note Usage Hint : bme280_set_softreset()
1313 * @return results of bus communication function
1314 * @retval 0 -> Success
1315 * @retval -1 -> Error
1319 BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst(void)
1321 /* used to return the communication result*/
1322 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1323 u8 v_data_u8 = BME280_SOFT_RESET_CODE;
1324 /* check the p_bme280 structure pointer as NULL*/
1325 if (p_bme280 == BME280_NULL) {
1326 return E_BME280_NULL_PTR;
1328 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1330 BME280_RST_REG, &v_data_u8,
1331 BME280_GEN_READ_WRITE_DATA_LENGTH);
1336 * @brief This API used to get the sensor
1337 * SPI mode(communication type) in the register 0xF5 bit 0
1341 * @param v_enable_disable_u8 : The value of SPI enable
1342 * value | Description
1343 * --------|--------------
1349 * @return results of bus communication function
1350 * @retval 0 -> Success
1351 * @retval -1 -> Error
1355 BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
1357 /* used to return the communication result*/
1358 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1359 u8 v_data_u8 = BME280_INIT_VALUE;
1360 /* check the p_bme280 structure pointer as NULL*/
1361 if (p_bme280 == BME280_NULL) {
1362 return E_BME280_NULL_PTR;
1364 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1366 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1367 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1368 *v_enable_disable_u8 = BME280_GET_BITSLICE(
1370 BME280_CONFIG_REG_SPI3_ENABLE);
1375 * @brief This API used to set the sensor
1376 * SPI mode(communication type) in the register 0xF5 bit 0
1380 * @param v_enable_disable_u8 : The value of SPI enable
1381 * value | Description
1382 * --------|--------------
1388 * @return results of bus communication function
1389 * @retval 0 -> Success
1390 * @retval -1 -> Error
1394 BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
1396 /* used to return the communication result*/
1397 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1398 u8 v_data_u8 = BME280_INIT_VALUE;
1399 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1400 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1401 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1402 /* check the p_bme280 structure pointer as NULL*/
1403 if (p_bme280 == BME280_NULL) {
1404 return E_BME280_NULL_PTR;
1406 v_data_u8 = p_bme280->config_reg;
1408 BME280_SET_BITSLICE(v_data_u8,
1409 BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
1410 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1411 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1412 com_rslt += bme280_set_soft_rst();
1413 p_bme280->delay_msec(BME280_3MS_DELAY);
1414 /* write previous and updated value of
1415 configuration register*/
1416 com_rslt += bme280_write_register(
1418 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1419 /* write previous value of
1420 humidity oversampling*/
1421 v_pre_ctrl_hum_value_u8 =
1422 p_bme280->ctrl_hum_reg;
1423 com_rslt += bme280_write_register(
1424 BME280_CTRL_HUMIDITY_REG,
1425 &v_pre_ctrl_hum_value_u8,
1426 BME280_GEN_READ_WRITE_DATA_LENGTH);
1427 /* write previous value of
1428 control measurement register*/
1429 pre_ctrl_meas_value =
1430 p_bme280->ctrl_meas_reg;
1431 com_rslt += bme280_write_register(
1432 BME280_CTRL_MEAS_REG,
1433 &pre_ctrl_meas_value,
1434 BME280_GEN_READ_WRITE_DATA_LENGTH);
1437 p_bme280->BME280_BUS_WRITE_FUNC(
1439 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1440 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1442 /* read the control measurement register value*/
1443 com_rslt += bme280_read_register(
1444 BME280_CTRL_MEAS_REG,
1445 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1446 p_bme280->ctrl_meas_reg = v_data_u8;
1447 /* read the control humidity register value*/
1448 com_rslt += bme280_read_register(
1449 BME280_CTRL_HUMIDITY_REG,
1450 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1451 p_bme280->ctrl_hum_reg = v_data_u8;
1452 /* read the control configuration register value*/
1453 com_rslt += bme280_read_register(
1455 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1456 p_bme280->config_reg = v_data_u8;
1461 * @brief This API is used to reads filter setting
1462 * in the register 0xF5 bit 3 and 4
1466 * @param v_value_u8 : The value of IIR filter coefficient
1468 * value | Filter coefficient
1469 * -------------|-------------------------
1470 * 0x00 | BME280_FILTER_COEFF_OFF
1471 * 0x01 | BME280_FILTER_COEFF_2
1472 * 0x02 | BME280_FILTER_COEFF_4
1473 * 0x03 | BME280_FILTER_COEFF_8
1474 * 0x04 | BME280_FILTER_COEFF_16
1476 * @return results of bus communication function
1477 * @retval 0 -> Success
1478 * @retval -1 -> Error
1482 BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
1484 /* used to return the communication result*/
1485 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1486 u8 v_data_u8 = BME280_INIT_VALUE;
1487 /* check the p_bme280 structure pointer as NULL*/
1488 if (p_bme280 == BME280_NULL) {
1489 return E_BME280_NULL_PTR;
1491 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1493 BME280_CONFIG_REG_FILTER__REG,
1494 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1495 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
1496 BME280_CONFIG_REG_FILTER);
1501 * @brief This API is used to write filter setting
1502 * in the register 0xF5 bit 3 and 4
1506 * @param v_value_u8 : The value of IIR filter coefficient
1508 * value | Filter coefficient
1509 * -------------|-------------------------
1510 * 0x00 | BME280_FILTER_COEFF_OFF
1511 * 0x01 | BME280_FILTER_COEFF_2
1512 * 0x02 | BME280_FILTER_COEFF_4
1513 * 0x03 | BME280_FILTER_COEFF_8
1514 * 0x04 | BME280_FILTER_COEFF_16
1516 * @return results of bus communication function
1517 * @retval 0 -> Success
1518 * @retval -1 -> Error
1522 BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
1524 /* used to return the communication result*/
1525 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1526 u8 v_data_u8 = BME280_INIT_VALUE;
1527 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1528 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1529 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1530 /* check the p_bme280 structure pointer as NULL*/
1531 if (p_bme280 == BME280_NULL) {
1532 return E_BME280_NULL_PTR;
1534 v_data_u8 = p_bme280->config_reg;
1536 BME280_SET_BITSLICE(v_data_u8,
1537 BME280_CONFIG_REG_FILTER, v_value_u8);
1538 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1539 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1540 com_rslt += bme280_set_soft_rst();
1541 p_bme280->delay_msec(BME280_3MS_DELAY);
1542 /* write previous and updated value of
1543 configuration register*/
1544 com_rslt += bme280_write_register(
1546 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1547 /* write previous value of
1548 humidity oversampling*/
1549 v_pre_ctrl_hum_value_u8 =
1550 p_bme280->ctrl_hum_reg;
1551 com_rslt += bme280_write_register(
1552 BME280_CTRL_HUMIDITY_REG,
1553 &v_pre_ctrl_hum_value_u8,
1554 BME280_GEN_READ_WRITE_DATA_LENGTH);
1555 /* write previous value of
1556 control measurement register*/
1557 pre_ctrl_meas_value =
1558 p_bme280->ctrl_meas_reg;
1559 com_rslt += bme280_write_register(
1560 BME280_CTRL_MEAS_REG,
1561 &pre_ctrl_meas_value,
1562 BME280_GEN_READ_WRITE_DATA_LENGTH);
1565 p_bme280->BME280_BUS_WRITE_FUNC(
1567 BME280_CONFIG_REG_FILTER__REG,
1568 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1570 /* read the control measurement register value*/
1571 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1572 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1573 p_bme280->ctrl_meas_reg = v_data_u8;
1574 /* read the control humidity register value*/
1575 com_rslt += bme280_read_register(
1576 BME280_CTRL_HUMIDITY_REG,
1577 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1578 p_bme280->ctrl_hum_reg = v_data_u8;
1579 /* read the configuration register value*/
1580 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1581 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1582 p_bme280->config_reg = v_data_u8;
1587 * @brief This API used to Read the
1588 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1590 * @param v_standby_durn_u8 : The value of standby duration time value.
1591 * value | standby duration
1592 * -------------|-----------------------
1593 * 0x00 | BME280_STANDBY_TIME_1_MS
1594 * 0x01 | BME280_STANDBY_TIME_63_MS
1595 * 0x02 | BME280_STANDBY_TIME_125_MS
1596 * 0x03 | BME280_STANDBY_TIME_250_MS
1597 * 0x04 | BME280_STANDBY_TIME_500_MS
1598 * 0x05 | BME280_STANDBY_TIME_1000_MS
1599 * 0x06 | BME280_STANDBY_TIME_2000_MS
1600 * 0x07 | BME280_STANDBY_TIME_4000_MS
1603 * @return results of bus communication function
1604 * @retval 0 -> Success
1605 * @retval -1 -> Error
1609 BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
1611 /* used to return the communication result*/
1612 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1613 u8 v_data_u8 = BME280_INIT_VALUE;
1614 /* check the p_bme280 structure pointer as NULL*/
1615 if (p_bme280 == BME280_NULL) {
1616 return E_BME280_NULL_PTR;
1618 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1620 BME280_CONFIG_REG_TSB__REG,
1621 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1622 *v_standby_durn_u8 = BME280_GET_BITSLICE(
1623 v_data_u8, BME280_CONFIG_REG_TSB);
1628 * @brief This API used to write the
1629 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1631 * @param v_standby_durn_u8 : The value of standby duration time value.
1632 * value | standby duration
1633 * -------------|-----------------------
1634 * 0x00 | BME280_STANDBY_TIME_1_MS
1635 * 0x01 | BME280_STANDBY_TIME_63_MS
1636 * 0x02 | BME280_STANDBY_TIME_125_MS
1637 * 0x03 | BME280_STANDBY_TIME_250_MS
1638 * 0x04 | BME280_STANDBY_TIME_500_MS
1639 * 0x05 | BME280_STANDBY_TIME_1000_MS
1640 * 0x06 | BME280_STANDBY_TIME_2000_MS
1641 * 0x07 | BME280_STANDBY_TIME_4000_MS
1643 * @note Normal mode comprises an automated perpetual
1644 * cycling between an (active)
1645 * Measurement period and an (inactive) standby period.
1646 * @note The standby time is determined by
1647 * the contents of the register t_sb.
1648 * Standby time can be set using BME280_STANDBY_TIME_125_MS.
1650 * @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
1654 * @return results of bus communication function
1655 * @retval 0 -> Success
1656 * @retval -1 -> Error
1660 BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
1662 /* used to return the communication result*/
1663 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1664 u8 v_data_u8 = BME280_INIT_VALUE;
1665 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1666 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1667 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1668 /* check the p_bme280 structure pointer as NULL*/
1669 if (p_bme280 == BME280_NULL) {
1670 return E_BME280_NULL_PTR;
1672 v_data_u8 = p_bme280->config_reg;
1674 BME280_SET_BITSLICE(v_data_u8,
1675 BME280_CONFIG_REG_TSB, v_standby_durn_u8);
1676 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1677 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1678 com_rslt += bme280_set_soft_rst();
1679 p_bme280->delay_msec(BME280_3MS_DELAY);
1680 /* write previous and updated value of
1681 configuration register*/
1682 com_rslt += bme280_write_register(
1684 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1685 /* write previous value of
1686 humidity oversampling*/
1687 v_pre_ctrl_hum_value_u8 =
1688 p_bme280->ctrl_hum_reg;
1689 com_rslt += bme280_write_register(
1690 BME280_CTRL_HUMIDITY_REG,
1691 &v_pre_ctrl_hum_value_u8,
1692 BME280_GEN_READ_WRITE_DATA_LENGTH);
1693 /* write previous value of control
1694 measurement register*/
1695 pre_ctrl_meas_value =
1696 p_bme280->ctrl_meas_reg;
1697 com_rslt += bme280_write_register(
1698 BME280_CTRL_MEAS_REG,
1699 &pre_ctrl_meas_value,
1700 BME280_GEN_READ_WRITE_DATA_LENGTH);
1703 p_bme280->BME280_BUS_WRITE_FUNC(
1705 BME280_CONFIG_REG_TSB__REG,
1706 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1708 /* read the control measurement register value*/
1709 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1710 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1711 p_bme280->ctrl_meas_reg = v_data_u8;
1712 /* read the control humidity register value*/
1713 com_rslt += bme280_read_register(
1714 BME280_CTRL_HUMIDITY_REG,
1715 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1716 p_bme280->ctrl_hum_reg = v_data_u8;
1717 /* read the configuration register value*/
1718 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1719 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1720 p_bme280->config_reg = v_data_u8;
1725 * @brief Writes the working mode to the sensor
1730 * @param v_work_mode_u8 : Mode to be set
1731 * value | Working mode
1732 * ----------|--------------------
1733 * 0 | BME280_ULTRALOWPOWER_MODE
1734 * 1 | BME280_LOWPOWER_MODE
1735 * 2 | BME280_STANDARDRESOLUTION_MODE
1736 * 3 | BME280_HIGHRESOLUTION_MODE
1737 * 4 | BME280_ULTRAHIGHRESOLUTION_MODE
1739 * @return results of bus communication function
1740 * @retval 0 -> Success
1741 * @retval -1 -> Error
1745 /*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
1747 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1748 u8 v_data_u8 = BME280_INIT_VALUE;
1749 if (p_bme280 == BME280_NULL) {
1750 return E_BME280_NULL_PTR;
1752 if (v_work_mode_u8 <= BME280_ULTRAHIGHRESOLUTION_MODE) {
1753 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1754 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1755 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1756 if (com_rslt == SUCCESS) {
1757 switch (v_work_mode_u8) {
1758 case BME280_ULTRALOWPOWER_MODE:
1759 p_bme280->oversamp_temperature =
1760 BME280_ULTRALOWPOWER_OSRS_T;
1762 BME280_ULTRALOWPOWER_OSRS_P;
1764 case BME280_LOWPOWER_MODE:
1765 p_bme280->oversamp_temperature =
1766 BME280_LOWPOWER_OSRS_T;
1767 p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
1769 case BME280_STANDARDRESOLUTION_MODE:
1770 p_bme280->oversamp_temperature =
1771 BME280_STANDARDRESOLUTION_OSRS_T;
1773 BME280_STANDARDRESOLUTION_OSRS_P;
1775 case BME280_HIGHRESOLUTION_MODE:
1776 p_bme280->oversamp_temperature =
1777 BME280_HIGHRESOLUTION_OSRS_T;
1778 p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
1780 case BME280_ULTRAHIGHRESOLUTION_MODE:
1781 p_bme280->oversamp_temperature =
1782 BME280_ULTRAHIGHRESOLUTION_OSRS_T;
1784 BME280_ULTRAHIGHRESOLUTION_OSRS_P;
1787 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1788 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
1789 p_bme280->oversamp_temperature);
1790 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1791 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
1793 com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
1794 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1795 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1798 com_rslt = E_BME280_OUT_OF_RANGE;
1804 * @brief This API used to read uncompensated
1805 * temperature,pressure and humidity in forced mode
1808 * @param v_uncom_pressure_s32: The value of uncompensated pressure
1809 * @param v_uncom_temperature_s32: The value of uncompensated temperature
1810 * @param v_uncom_humidity_s32: The value of uncompensated humidity
1813 * @return results of bus communication function
1814 * @retval 0 -> Success
1815 * @retval -1 -> Error
1819 BME280_RETURN_FUNCTION_TYPE
1820 bme280_get_forced_uncomp_pressure_temperature_humidity(
1821 s32 *v_uncom_pressure_s32,
1822 s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
1824 /* used to return the communication result*/
1825 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1826 u8 v_data_u8 = BME280_INIT_VALUE;
1827 u8 v_waittime_u8r = BME280_INIT_VALUE;
1828 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1829 u8 v_mode_u8r = BME280_INIT_VALUE;
1830 u8 pre_ctrl_config_value = BME280_INIT_VALUE;
1831 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1832 /* check the p_bme280 structure pointer as NULL*/
1833 if (p_bme280 == BME280_NULL) {
1834 return E_BME280_NULL_PTR;
1836 v_mode_u8r = p_bme280->ctrl_meas_reg;
1838 BME280_SET_BITSLICE(v_mode_u8r,
1839 BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
1840 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1841 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1842 com_rslt += bme280_set_soft_rst();
1843 p_bme280->delay_msec(BME280_3MS_DELAY);
1844 /* write previous and updated value of
1845 configuration register*/
1846 pre_ctrl_config_value = p_bme280->config_reg;
1847 com_rslt += bme280_write_register(
1849 &pre_ctrl_config_value,
1850 BME280_GEN_READ_WRITE_DATA_LENGTH);
1851 /* write previous value of
1852 humidity oversampling*/
1853 v_pre_ctrl_hum_value_u8 =
1854 p_bme280->ctrl_hum_reg;
1855 com_rslt += bme280_write_register(
1856 BME280_CTRL_HUMIDITY_REG,
1857 &v_pre_ctrl_hum_value_u8,
1858 BME280_GEN_READ_WRITE_DATA_LENGTH);
1859 /* write the force mode */
1860 com_rslt += bme280_write_register(
1861 BME280_CTRL_MEAS_REG,
1862 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1864 /* write previous value of
1865 humidity oversampling*/
1866 v_pre_ctrl_hum_value_u8 =
1867 p_bme280->ctrl_hum_reg;
1868 com_rslt += bme280_write_register(
1869 BME280_CTRL_HUMIDITY_REG,
1870 &v_pre_ctrl_hum_value_u8,
1871 BME280_GEN_READ_WRITE_DATA_LENGTH);
1872 /* write the force mode */
1873 com_rslt += bme280_write_register(
1874 BME280_CTRL_MEAS_REG,
1875 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1877 bme280_compute_wait_time(&v_waittime_u8r);
1878 p_bme280->delay_msec(v_waittime_u8r);
1879 /* read the force-mode value of pressure
1880 temperature and humidity*/
1882 bme280_read_uncomp_pressure_temperature_humidity(
1883 v_uncom_pressure_s32, v_uncom_temperature_s32,
1884 v_uncom_humidity_s32);
1886 /* read the control humidity register value*/
1887 com_rslt += bme280_read_register(
1888 BME280_CTRL_HUMIDITY_REG,
1889 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1890 p_bme280->ctrl_hum_reg = v_data_u8;
1891 /* read the configuration register value*/
1892 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1893 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1894 p_bme280->config_reg = v_data_u8;
1896 /* read the control measurement register value*/
1897 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1898 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1899 p_bme280->ctrl_meas_reg = v_data_u8;
1905 * This API write the data to
1906 * the given register
1909 * @param v_addr_u8 -> Address of the register
1910 * @param v_data_u8 -> The data from the register
1911 * @param v_len_u8 -> no of bytes to read
1914 * @return results of bus communication function
1915 * @retval 0 -> Success
1916 * @retval -1 -> Error
1920 BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
1921 u8 *v_data_u8, u8 v_len_u8)
1923 /* used to return the communication result*/
1924 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1925 /* check the p_bme280 structure pointer as NULL*/
1926 if (p_bme280 == BME280_NULL) {
1927 return E_BME280_NULL_PTR;
1929 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1931 v_addr_u8, v_data_u8, v_len_u8);
1937 * This API reads the data from
1938 * the given register
1941 * @param v_addr_u8 -> Address of the register
1942 * @param v_data_u8 -> The data from the register
1943 * @param v_len_u8 -> no of bytes to read
1946 * @return results of bus communication function
1947 * @retval 0 -> Success
1948 * @retval -1 -> Error
1952 BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
1953 u8 *v_data_u8, u8 v_len_u8)
1955 /* used to return the communication result*/
1956 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1957 /* check the p_bme280 structure pointer as NULL*/
1958 if (p_bme280 == BME280_NULL) {
1959 return E_BME280_NULL_PTR;
1961 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1963 v_addr_u8, v_data_u8, v_len_u8);
1967 #ifdef BME280_ENABLE_FLOAT
1969 * @brief Reads actual temperature from uncompensated temperature
1970 * @note returns the value in Degree centigrade
1971 * @note Output value of "51.23" equals 51.23 DegC.
1975 * @param v_uncom_temperature_s32 : value of uncompensated temperature
1979 * @return Return the actual temperature in floating point
1982 double bme280_compensate_temperature_double(s32 v_uncom_temperature_s32)
1984 double v_x1_u32 = BME280_INIT_VALUE;
1985 double v_x2_u32 = BME280_INIT_VALUE;
1986 double temperature = BME280_INIT_VALUE;
1988 v_x1_u32 = (((double)v_uncom_temperature_s32) / 16384.0 -
1989 ((double)p_bme280->cal_param.dig_T1) / 1024.0) *
1990 ((double)p_bme280->cal_param.dig_T2);
1991 v_x2_u32 = ((((double)v_uncom_temperature_s32) / 131072.0 -
1992 ((double)p_bme280->cal_param.dig_T1) / 8192.0) *
1993 (((double)v_uncom_temperature_s32) / 131072.0 -
1994 ((double)p_bme280->cal_param.dig_T1) / 8192.0)) *
1995 ((double)p_bme280->cal_param.dig_T3);
1996 p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
1997 temperature = (v_x1_u32 + v_x2_u32) / 5120.0;
2003 * @brief Reads actual pressure from uncompensated pressure
2004 * @note Returns pressure in Pa as double.
2005 * @note Output value of "96386.2"
2006 * equals 96386.2 Pa = 963.862 hPa.
2009 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2012 * @return Return the actual pressure in floating point
2015 double bme280_compensate_pressure_double(s32 v_uncom_pressure_s32)
2017 double v_x1_u32 = BME280_INIT_VALUE;
2018 double v_x2_u32 = BME280_INIT_VALUE;
2019 double pressure = BME280_INIT_VALUE;
2021 v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
2023 v_x2_u32 = v_x1_u32 * v_x1_u32 *
2024 ((double)p_bme280->cal_param.dig_P6) / 32768.0;
2025 v_x2_u32 = v_x2_u32 + v_x1_u32 *
2026 ((double)p_bme280->cal_param.dig_P5) * 2.0;
2027 v_x2_u32 = (v_x2_u32 / 4.0) +
2028 (((double)p_bme280->cal_param.dig_P4) * 65536.0);
2029 v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
2030 v_x1_u32 * v_x1_u32 / 524288.0 +
2031 ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) / 524288.0;
2032 v_x1_u32 = (1.0 + v_x1_u32 / 32768.0) *
2033 ((double)p_bme280->cal_param.dig_P1);
2034 pressure = 1048576.0 - (double)v_uncom_pressure_s32;
2035 /* Avoid exception caused by division by zero */
2036 if (v_x1_u32 != BME280_INIT_VALUE)
2037 pressure = (pressure - (v_x2_u32 / 4096.0)) * 6250.0 / v_x1_u32;
2039 return BME280_INVALID_DATA;
2040 v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
2041 pressure * pressure / 2147483648.0;
2042 v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8) / 32768.0;
2043 pressure = pressure + (v_x1_u32 + v_x2_u32 +
2044 ((double)p_bme280->cal_param.dig_P7)) / 16.0;
2049 * @brief Reads actual humidity from uncompensated humidity
2050 * @note returns the value in relative humidity (%rH)
2051 * @note Output value of "42.12" equals 42.12 %rH
2053 * @param v_uncom_humidity_s32 : value of uncompensated humidity
2057 * @return Return the actual humidity in floating point
2060 double bme280_compensate_humidity_double(s32 v_uncom_humidity_s32)
2062 double var_h = BME280_INIT_VALUE;
2064 var_h = (((double)p_bme280->cal_param.t_fine) - 76800.0);
2065 if (var_h != BME280_INIT_VALUE)
2066 var_h = (v_uncom_humidity_s32 -
2067 (((double)p_bme280->cal_param.dig_H4) * 64.0 +
2068 ((double)p_bme280->cal_param.dig_H5) / 16384.0 * var_h))*
2069 (((double)p_bme280->cal_param.dig_H2) / 65536.0 *
2070 (1.0 + ((double) p_bme280->cal_param.dig_H6)
2071 / 67108864.0 * var_h * (1.0 + ((double)
2072 p_bme280->cal_param.dig_H3) / 67108864.0 * var_h)));
2074 return BME280_INVALID_DATA;
2075 var_h = var_h * (1.0 - ((double)
2076 p_bme280->cal_param.dig_H1)*var_h / 524288.0);
2079 else if (var_h < 0.0)
2085 #if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
2087 * @brief Reads actual pressure from uncompensated pressure
2088 * @note Returns the value in Pa as unsigned 32 bit
2089 * integer in Q24.8 format (24 integer bits and
2090 * 8 fractional bits).
2091 * @note Output value of "24674867"
2092 * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
2096 * @param v_uncom_pressure_s32 : value of uncompensated temperature
2099 * @return Return the actual pressure in u32
2102 u32 bme280_compensate_pressure_int64(s32 v_uncom_pressure_s32)
2104 s64 v_x1_s64r = BME280_INIT_VALUE;
2105 s64 v_x2_s64r = BME280_INIT_VALUE;
2106 s64 pressure = BME280_INIT_VALUE;
2108 v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
2110 v_x2_s64r = v_x1_s64r * v_x1_s64r *
2111 (s64)p_bme280->cal_param.dig_P6;
2112 v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
2113 (s64)p_bme280->cal_param.dig_P5)
2114 << BME280_SHIFT_BIT_POSITION_BY_17_BITS);
2115 v_x2_s64r = v_x2_s64r +
2116 (((s64)p_bme280->cal_param.dig_P4)
2117 << BME280_SHIFT_BIT_POSITION_BY_35_BITS);
2118 v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
2119 (s64)p_bme280->cal_param.dig_P3)
2120 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2121 ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
2122 << BME280_SHIFT_BIT_POSITION_BY_12_BITS);
2123 v_x1_s64r = (((((s64)1)
2124 << BME280_SHIFT_BIT_POSITION_BY_47_BITS) + v_x1_s64r)) *
2125 ((s64)p_bme280->cal_param.dig_P1)
2126 >> BME280_SHIFT_BIT_POSITION_BY_33_BITS;
2127 pressure = 1048576 - v_uncom_pressure_s32;
2128 /* Avoid exception caused by division by zero */
2129 if (v_x1_s64r != BME280_INIT_VALUE)
2130 #if defined __KERNEL__
2131 pressure = div64_s64((((pressure
2132 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2133 * 3125), v_x1_s64r);
2135 pressure = (((pressure
2136 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2137 * 3125) / v_x1_s64r;
2140 return BME280_INVALID_DATA;
2141 v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
2142 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS) *
2143 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
2144 >> BME280_SHIFT_BIT_POSITION_BY_25_BITS;
2145 v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
2146 pressure) >> BME280_SHIFT_BIT_POSITION_BY_19_BITS;
2147 pressure = (((pressure + v_x1_s64r +
2148 v_x2_s64r) >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2149 (((s64)p_bme280->cal_param.dig_P7)
2150 << BME280_SHIFT_BIT_POSITION_BY_04_BITS));
2152 return (u32)pressure;
2155 * @brief Reads actual pressure from uncompensated pressure
2156 * @note Returns the value in Pa.
2157 * @note Output value of "12337434"
2158 * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
2162 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2165 * @return the actual pressure in u32
2168 u32 bme280_compensate_pressure_int64_twentyfour_bit_output(
2169 s32 v_uncom_pressure_s32)
2171 u32 pressure = BME280_INIT_VALUE;
2173 pressure = bme280_compensate_pressure_int64(
2174 v_uncom_pressure_s32);
2175 pressure = (u32)(pressure >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
2180 * @brief Computing waiting time for sensor data read
2185 * @param v_delaytime_u8 : The value of delay time for force mode
2188 * @retval 0 -> Success
2192 BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
2195 /* used to return the communication result*/
2196 BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
2198 *v_delaytime_u8 = (T_INIT_MAX +
2199 T_MEASURE_PER_OSRS_MAX *
2201 p_bme280->oversamp_temperature)
2202 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT)
2203 + ((1 << p_bme280->oversamp_pressure)
2204 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) +
2205 ((1 << p_bme280->oversamp_humidity)
2206 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
2207 + (p_bme280->oversamp_pressure ?
2208 T_SETUP_PRESSURE_MAX : 0) +
2209 (p_bme280->oversamp_humidity ?
2210 T_SETUP_HUMIDITY_MAX : 0) + 15) / 16;