2 ****************************************************************************
3 * Copyright (C) 2015 - 2016 Bosch Sensortec GmbH
7 * Revision: 2.0.5(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;
87 u8 v_chip_id_read_count = BME280_CHIP_ID_READ_COUNT;
89 /* assign BME280 ptr */
92 while (v_chip_id_read_count > 0) {
95 com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
96 BME280_CHIP_ID_REG, &v_data_u8,
97 BME280_GEN_READ_WRITE_DATA_LENGTH);
98 /* Check for the correct chip id */
99 if (v_data_u8 == BME280_CHIP_ID)
101 v_chip_id_read_count--;
102 /* Delay added concerning the low speed of power up system to
103 facilitate the proper reading of the chip ID */
104 p_bme280->delay_msec(BME280_REGISTER_READ_DELAY);
106 /*assign chip ID to the global structure*/
107 p_bme280->chip_id = v_data_u8;
108 /*com_rslt status of chip ID read*/
109 com_rslt = (v_chip_id_read_count == BME280_INIT_VALUE) ?
110 BME280_CHIP_ID_READ_FAIL : BME280_CHIP_ID_READ_SUCCESS;
112 if (com_rslt == BME280_CHIP_ID_READ_SUCCESS) {
113 /* readout bme280 calibparam structure */
114 com_rslt += bme280_get_calib_param();
119 * @brief This API is used to read uncompensated temperature
120 * in the registers 0xFA, 0xFB and 0xFC
121 * @note 0xFA -> MSB -> bit from 0 to 7
122 * @note 0xFB -> LSB -> bit from 0 to 7
123 * @note 0xFC -> LSB -> bit from 4 to 7
125 * @param v_uncomp_temperature_s32 : The value of uncompensated temperature
129 * @return results of bus communication function
130 * @retval 0 -> Success
131 * @retval -1 -> Error
135 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
136 s32 *v_uncomp_temperature_s32)
138 /* used to return the communication result*/
139 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
140 /* Array holding the MSB and LSb value
141 a_data_u8r[0] - Temperature MSB
142 a_data_u8r[1] - Temperature LSB
143 a_data_u8r[2] - Temperature XLSB
145 u8 a_data_u8r[BME280_TEMPERATURE_DATA_SIZE] = {
146 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
147 /* check the p_bme280 structure pointer as NULL*/
148 if (p_bme280 == BME280_NULL) {
149 return E_BME280_NULL_PTR;
151 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
153 BME280_TEMPERATURE_MSB_REG,
155 BME280_TEMPERATURE_DATA_LENGTH);
156 *v_uncomp_temperature_s32 = (s32)(((
157 (u32) (a_data_u8r[BME280_TEMPERATURE_MSB_DATA]))
158 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
159 (((u32)(a_data_u8r[BME280_TEMPERATURE_LSB_DATA]))
160 << BME280_SHIFT_BIT_POSITION_BY_04_BITS)
161 | ((u32)a_data_u8r[BME280_TEMPERATURE_XLSB_DATA] >>
162 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
167 * @brief Reads actual temperature from uncompensated temperature
168 * @note Returns the value in 0.01 degree Centigrade
169 * Output value of "5123" equals 51.23 DegC.
173 * @param v_uncomp_temperature_s32 : value of uncompensated temperature
176 * @return Returns the actual temperature
179 s32 bme280_compensate_temperature_int32(s32 v_uncomp_temperature_s32)
181 s32 v_x1_u32r = BME280_INIT_VALUE;
182 s32 v_x2_u32r = BME280_INIT_VALUE;
183 s32 temperature = BME280_INIT_VALUE;
187 ((((v_uncomp_temperature_s32
188 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS) -
189 ((s32)p_bme280->cal_param.dig_T1
190 << BME280_SHIFT_BIT_POSITION_BY_01_BIT))) *
191 ((s32)p_bme280->cal_param.dig_T2)) >>
192 BME280_SHIFT_BIT_POSITION_BY_11_BITS;
194 v_x2_u32r = (((((v_uncomp_temperature_s32
195 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
196 ((s32)p_bme280->cal_param.dig_T1))
197 * ((v_uncomp_temperature_s32 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS) -
198 ((s32)p_bme280->cal_param.dig_T1)))
199 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS) *
200 ((s32)p_bme280->cal_param.dig_T3))
201 >> BME280_SHIFT_BIT_POSITION_BY_14_BITS;
202 /* calculate t_fine*/
203 p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
204 /* calculate temperature*/
205 temperature = (p_bme280->cal_param.t_fine * 5 + 128)
206 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS;
210 * @brief Reads actual temperature from uncompensated temperature
211 * @note Returns the value with 500LSB/DegC centred around 24 DegC
212 * output value of "5123" equals(5123/500)+24 = 34.246DegC
215 * @param v_uncomp_temperature_s32: value of uncompensated temperature
219 * @return Return the actual temperature as s16 output
222 s16 bme280_compensate_temperature_int32_sixteen_bit_output(
223 s32 v_uncomp_temperature_s32)
225 s16 temperature = BME280_INIT_VALUE;
227 bme280_compensate_temperature_int32(
228 v_uncomp_temperature_s32);
229 temperature = (s16)((((
230 p_bme280->cal_param.t_fine - 122880) * 25) + 128)
231 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS);
236 * @brief This API is used to read uncompensated pressure.
237 * in the registers 0xF7, 0xF8 and 0xF9
238 * @note 0xF7 -> MSB -> bit from 0 to 7
239 * @note 0xF8 -> LSB -> bit from 0 to 7
240 * @note 0xF9 -> LSB -> bit from 4 to 7
244 * @param v_uncomp_pressure_s32 : The value of uncompensated pressure
248 * @return results of bus communication function
249 * @retval 0 -> Success
250 * @retval -1 -> Error
254 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
255 s32 *v_uncomp_pressure_s32)
257 /* used to return the communication result*/
258 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
259 /* Array holding the MSB and LSb value
260 a_data_u8[0] - Pressure MSB
261 a_data_u8[1] - Pressure LSB
262 a_data_u8[2] - Pressure XLSB
264 u8 a_data_u8[BME280_PRESSURE_DATA_SIZE] = {
265 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
266 /* check the p_bme280 structure pointer as NULL*/
267 if (p_bme280 == BME280_NULL) {
268 return E_BME280_NULL_PTR;
270 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
272 BME280_PRESSURE_MSB_REG,
273 a_data_u8, BME280_PRESSURE_DATA_LENGTH);
274 *v_uncomp_pressure_s32 = (s32)((
275 ((u32)(a_data_u8[BME280_PRESSURE_MSB_DATA]))
276 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
277 (((u32)(a_data_u8[BME280_PRESSURE_LSB_DATA]))
278 << BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
279 ((u32)a_data_u8[BME280_PRESSURE_XLSB_DATA] >>
280 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
285 * @brief Reads actual pressure from uncompensated pressure
286 * @note Returns the value in Pascal(Pa)
287 * Output value of "96386" equals 96386 Pa =
288 * 963.86 hPa = 963.86 millibar
292 * @param v_uncomp_pressure_s32 : value of uncompensated pressure
296 * @return Return the actual pressure output as u32
299 u32 bme280_compensate_pressure_int32(s32 v_uncomp_pressure_s32)
301 s32 v_x1_u32 = BME280_INIT_VALUE;
302 s32 v_x2_u32 = BME280_INIT_VALUE;
303 u32 v_pressure_u32 = BME280_INIT_VALUE;
306 v_x1_u32 = (((s32)p_bme280->cal_param.t_fine)
307 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) - (s32)64000;
309 v_x2_u32 = (((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
310 * (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)
311 ) >> BME280_SHIFT_BIT_POSITION_BY_11_BITS)
312 * ((s32)p_bme280->cal_param.dig_P6);
314 v_x2_u32 = v_x2_u32 + ((v_x1_u32 *
315 ((s32)p_bme280->cal_param.dig_P5))
316 << BME280_SHIFT_BIT_POSITION_BY_01_BIT);
318 v_x2_u32 = (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) +
319 (((s32)p_bme280->cal_param.dig_P4)
320 << BME280_SHIFT_BIT_POSITION_BY_16_BITS);
322 v_x1_u32 = (((p_bme280->cal_param.dig_P3 *
323 (((v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS) *
324 (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS))
325 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
326 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS) +
327 ((((s32)p_bme280->cal_param.dig_P2) *
328 v_x1_u32) >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
329 >> BME280_SHIFT_BIT_POSITION_BY_18_BITS;
331 v_x1_u32 = ((((32768 + v_x1_u32)) *
332 ((s32)p_bme280->cal_param.dig_P1))
333 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS);
334 /* calculate pressure*/
336 (((u32)(((s32)1048576) - v_uncomp_pressure_s32)
337 - (v_x2_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS))) * 3125;
340 /* Avoid exception caused by division by zero */
341 if (v_x1_u32 != BME280_INIT_VALUE)
344 << BME280_SHIFT_BIT_POSITION_BY_01_BIT) /
347 return BME280_INVALID_DATA;
349 /* Avoid exception caused by division by zero */
350 if (v_x1_u32 != BME280_INIT_VALUE)
351 v_pressure_u32 = (v_pressure_u32
352 / (u32)v_x1_u32) * 2;
354 return BME280_INVALID_DATA;
356 v_x1_u32 = (((s32)p_bme280->cal_param.dig_P9) *
357 ((s32)(((v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS)
358 * (v_pressure_u32 >> BME280_SHIFT_BIT_POSITION_BY_03_BITS))
359 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS)))
360 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS;
361 v_x2_u32 = (((s32)(v_pressure_u32
362 >> BME280_SHIFT_BIT_POSITION_BY_02_BITS)) *
363 ((s32)p_bme280->cal_param.dig_P8))
364 >> BME280_SHIFT_BIT_POSITION_BY_13_BITS;
365 v_pressure_u32 = (u32)((s32)v_pressure_u32 +
366 ((v_x1_u32 + v_x2_u32 + p_bme280->cal_param.dig_P7)
367 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
369 return v_pressure_u32;
372 * @brief This API is used to read uncompensated humidity.
373 * in the registers 0xF7, 0xF8 and 0xF9
374 * @note 0xFD -> MSB -> bit from 0 to 7
375 * @note 0xFE -> LSB -> bit from 0 to 7
379 * @param v_uncomp_humidity_s32 : The value of uncompensated humidity
383 * @return results of bus communication function
384 * @retval 0 -> Success
385 * @retval -1 -> Error
389 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_humidity(
390 s32 *v_uncomp_humidity_s32)
392 /* used to return the communication result*/
393 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
394 /* Array holding the MSB and LSb value
395 a_data_u8[0] - Humidity MSB
396 a_data_u8[1] - Humidity LSB
398 u8 a_data_u8[BME280_HUMIDITY_DATA_SIZE] = {
399 BME280_INIT_VALUE, BME280_INIT_VALUE};
400 /* check the p_bme280 structure pointer as NULL*/
401 if (p_bme280 == BME280_NULL) {
402 return E_BME280_NULL_PTR;
404 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
406 BME280_HUMIDITY_MSB_REG, a_data_u8,
407 BME280_HUMIDITY_DATA_LENGTH);
408 *v_uncomp_humidity_s32 = (s32)(
409 (((u32)(a_data_u8[BME280_HUMIDITY_MSB_DATA]))
410 << BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
411 ((u32)(a_data_u8[BME280_HUMIDITY_LSB_DATA])));
416 * @brief Reads actual humidity from uncompensated humidity
417 * @note Returns the value in %rH as unsigned 32bit integer
418 * in Q22.10 format(22 integer 10 fractional bits).
419 * @note An output value of 42313
420 * represents 42313 / 1024 = 41.321 %rH
424 * @param v_uncomp_humidity_s32: value of uncompensated humidity
426 * @return Return the actual relative humidity output as u32
429 u32 bme280_compensate_humidity_int32(s32 v_uncomp_humidity_s32)
431 s32 v_x1_u32 = BME280_INIT_VALUE;
434 v_x1_u32 = (p_bme280->cal_param.t_fine - ((s32)76800));
436 v_x1_u32 = (((((v_uncomp_humidity_s32
437 << BME280_SHIFT_BIT_POSITION_BY_14_BITS) -
438 (((s32)p_bme280->cal_param.dig_H4)
439 << BME280_SHIFT_BIT_POSITION_BY_20_BITS) -
440 (((s32)p_bme280->cal_param.dig_H5) * v_x1_u32)) +
441 ((s32)16384)) >> BME280_SHIFT_BIT_POSITION_BY_15_BITS)
443 ((s32)p_bme280->cal_param.dig_H6))
444 >> BME280_SHIFT_BIT_POSITION_BY_10_BITS) *
445 (((v_x1_u32 * ((s32)p_bme280->cal_param.dig_H3))
446 >> BME280_SHIFT_BIT_POSITION_BY_11_BITS) + ((s32)32768)))
447 >> BME280_SHIFT_BIT_POSITION_BY_10_BITS) + ((s32)2097152)) *
448 ((s32)p_bme280->cal_param.dig_H2) + 8192) >> 14));
449 v_x1_u32 = (v_x1_u32 - (((((v_x1_u32
450 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS) *
451 (v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_15_BITS))
452 >> BME280_SHIFT_BIT_POSITION_BY_07_BITS) *
453 ((s32)p_bme280->cal_param.dig_H1))
454 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
455 v_x1_u32 = (v_x1_u32 < 0 ? 0 : v_x1_u32);
456 v_x1_u32 = (v_x1_u32 > 419430400 ?
457 419430400 : v_x1_u32);
458 return (u32)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_12_BITS);
461 * @brief Reads actual humidity from uncompensated humidity
462 * @note Returns the value in %rH as unsigned 16bit integer
463 * @note An output value of 42313
464 * represents 42313/512 = 82.643 %rH
468 * @param v_uncomp_humidity_s32: value of uncompensated humidity
471 * @return Return the actual relative humidity output as u16
474 u16 bme280_compensate_humidity_int32_sixteen_bit_output(
475 s32 v_uncomp_humidity_s32)
477 u32 v_x1_u32 = BME280_INIT_VALUE;
478 u16 v_x2_u32 = BME280_INIT_VALUE;
480 v_x1_u32 = bme280_compensate_humidity_int32(v_uncomp_humidity_s32);
481 v_x2_u32 = (u16)(v_x1_u32 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
485 * @brief This API used to read uncompensated
486 * pressure,temperature and humidity
491 * @param v_uncomp_pressure_s32: The value of uncompensated pressure.
492 * @param v_uncomp_temperature_s32: The value of uncompensated temperature
493 * @param v_uncomp_humidity_s32: The value of uncompensated humidity.
497 * @return results of bus communication function
498 * @retval 0 -> Success
499 * @retval -1 -> Error
503 BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure_temperature_humidity(
504 s32 *v_uncomp_pressure_s32,
505 s32 *v_uncomp_temperature_s32, s32 *v_uncomp_humidity_s32)
507 /* used to return the communication result*/
508 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
509 /* Array holding the MSB and LSb value of
510 a_data_u8[0] - Pressure MSB
511 a_data_u8[1] - Pressure LSB
512 a_data_u8[1] - Pressure LSB
513 a_data_u8[1] - Temperature MSB
514 a_data_u8[1] - Temperature LSB
515 a_data_u8[1] - Temperature LSB
516 a_data_u8[1] - Humidity MSB
517 a_data_u8[1] - Humidity LSB
519 u8 a_data_u8[BME280_DATA_FRAME_SIZE] = {
520 BME280_INIT_VALUE, BME280_INIT_VALUE,
521 BME280_INIT_VALUE, BME280_INIT_VALUE,
522 BME280_INIT_VALUE, BME280_INIT_VALUE,
523 BME280_INIT_VALUE, BME280_INIT_VALUE};
524 /* check the p_bme280 structure pointer as NULL*/
525 if (p_bme280 == BME280_NULL) {
526 return E_BME280_NULL_PTR;
528 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
530 BME280_PRESSURE_MSB_REG,
531 a_data_u8, BME280_ALL_DATA_FRAME_LENGTH);
533 *v_uncomp_pressure_s32 = (s32)((
535 BME280_DATA_FRAME_PRESSURE_MSB_BYTE]))
536 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
538 BME280_DATA_FRAME_PRESSURE_LSB_BYTE]))
539 << BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
541 BME280_DATA_FRAME_PRESSURE_XLSB_BYTE] >>
542 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
545 *v_uncomp_temperature_s32 = (s32)(((
547 BME280_DATA_FRAME_TEMPERATURE_MSB_BYTE]))
548 << BME280_SHIFT_BIT_POSITION_BY_12_BITS) |
550 BME280_DATA_FRAME_TEMPERATURE_LSB_BYTE]))
551 << BME280_SHIFT_BIT_POSITION_BY_04_BITS)
553 BME280_DATA_FRAME_TEMPERATURE_XLSB_BYTE]
554 >> BME280_SHIFT_BIT_POSITION_BY_04_BITS));
557 *v_uncomp_humidity_s32 = (s32)((
559 BME280_DATA_FRAME_HUMIDITY_MSB_BYTE]))
560 << BME280_SHIFT_BIT_POSITION_BY_08_BITS)|
562 BME280_DATA_FRAME_HUMIDITY_LSB_BYTE])));
567 * @brief This API used to read true pressure, temperature and humidity
572 * @param v_pressure_u32 : The value of compensated pressure.
573 * @param v_temperature_s32 : The value of compensated temperature.
574 * @param v_humidity_u32 : The value of compensated humidity.
577 * @return results of bus communication function
578 * @retval 0 -> Success
579 * @retval -1 -> Error
583 BME280_RETURN_FUNCTION_TYPE bme280_read_pressure_temperature_humidity(
584 u32 *v_pressure_u32, s32 *v_temperature_s32, u32 *v_humidity_u32)
586 /* used to return the communication result*/
587 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
588 s32 v_uncomp_pressure_s32 = BME280_INIT_VALUE;
589 s32 v_uncom_temperature_s32 = BME280_INIT_VALUE;
590 s32 v_uncom_humidity_s32 = BME280_INIT_VALUE;
591 /* check the p_bme280 structure pointer as NULL*/
592 if (p_bme280 == BME280_NULL) {
593 return E_BME280_NULL_PTR;
595 /* read the uncompensated pressure,
596 temperature and humidity*/
598 bme280_read_uncomp_pressure_temperature_humidity(
599 &v_uncomp_pressure_s32, &v_uncom_temperature_s32,
600 &v_uncom_humidity_s32);
601 /* read the true pressure, temperature and humidity*/
603 bme280_compensate_temperature_int32(
604 v_uncom_temperature_s32);
605 *v_pressure_u32 = bme280_compensate_pressure_int32(
606 v_uncomp_pressure_s32);
607 *v_humidity_u32 = bme280_compensate_humidity_int32(
608 v_uncom_humidity_s32);
613 * @brief This API is used to
614 * calibration parameters used for calculation in the registers
616 * parameter | Register address | bit
617 *------------|------------------|----------------
618 * dig_T1 | 0x88 and 0x89 | from 0 : 7 to 8: 15
619 * dig_T2 | 0x8A and 0x8B | from 0 : 7 to 8: 15
620 * dig_T3 | 0x8C and 0x8D | from 0 : 7 to 8: 15
621 * dig_P1 | 0x8E and 0x8F | from 0 : 7 to 8: 15
622 * dig_P2 | 0x90 and 0x91 | from 0 : 7 to 8: 15
623 * dig_P3 | 0x92 and 0x93 | from 0 : 7 to 8: 15
624 * dig_P4 | 0x94 and 0x95 | from 0 : 7 to 8: 15
625 * dig_P5 | 0x96 and 0x97 | from 0 : 7 to 8: 15
626 * dig_P6 | 0x98 and 0x99 | from 0 : 7 to 8: 15
627 * dig_P7 | 0x9A and 0x9B | from 0 : 7 to 8: 15
628 * dig_P8 | 0x9C and 0x9D | from 0 : 7 to 8: 15
629 * dig_P9 | 0x9E and 0x9F | from 0 : 7 to 8: 15
630 * dig_H1 | 0xA1 | from 0 to 7
631 * dig_H2 | 0xE1 and 0xE2 | from 0 : 7 to 8: 15
632 * dig_H3 | 0xE3 | from 0 to 7
633 * dig_H4 | 0xE4 and 0xE5 | from 4 : 11 to 0: 3
634 * dig_H5 | 0xE5 and 0xE6 | from 0 : 3 to 4: 11
635 * dig_H6 | 0xE7 | from 0 to 7
637 * @return results of bus communication function
638 * @retval 0 -> Success
639 * @retval -1 -> Error
643 BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param(void)
645 /* used to return the communication result*/
646 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
647 u8 a_data_u8[BME280_CALIB_DATA_SIZE] = {
648 BME280_INIT_VALUE, BME280_INIT_VALUE,
649 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
650 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
651 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
652 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
653 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
654 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
655 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
656 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE};
657 /* check the p_bme280 structure pointer as NULL*/
658 if (p_bme280 == BME280_NULL) {
659 return E_BME280_NULL_PTR;
661 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
663 BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
665 BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH);
667 p_bme280->cal_param.dig_T1 = (u16)(((
669 BME280_TEMPERATURE_CALIB_DIG_T1_MSB])) <<
670 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
671 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T1_LSB]);
672 p_bme280->cal_param.dig_T2 = (s16)(((
674 BME280_TEMPERATURE_CALIB_DIG_T2_MSB])) <<
675 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
676 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T2_LSB]);
677 p_bme280->cal_param.dig_T3 = (s16)(((
679 BME280_TEMPERATURE_CALIB_DIG_T3_MSB])) <<
680 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
681 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T3_LSB]);
682 p_bme280->cal_param.dig_P1 = (u16)(((
684 BME280_PRESSURE_CALIB_DIG_P1_MSB])) <<
685 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
686 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P1_LSB]);
687 p_bme280->cal_param.dig_P2 = (s16)(((
689 BME280_PRESSURE_CALIB_DIG_P2_MSB])) <<
690 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
691 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P2_LSB]);
692 p_bme280->cal_param.dig_P3 = (s16)(((
694 BME280_PRESSURE_CALIB_DIG_P3_MSB])) <<
695 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
697 BME280_PRESSURE_CALIB_DIG_P3_LSB]);
698 p_bme280->cal_param.dig_P4 = (s16)(((
700 BME280_PRESSURE_CALIB_DIG_P4_MSB])) <<
701 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
702 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P4_LSB]);
703 p_bme280->cal_param.dig_P5 = (s16)(((
705 BME280_PRESSURE_CALIB_DIG_P5_MSB])) <<
706 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
707 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P5_LSB]);
708 p_bme280->cal_param.dig_P6 = (s16)(((
710 BME280_PRESSURE_CALIB_DIG_P6_MSB])) <<
711 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
712 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P6_LSB]);
713 p_bme280->cal_param.dig_P7 = (s16)(((
715 BME280_PRESSURE_CALIB_DIG_P7_MSB])) <<
716 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
717 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P7_LSB]);
718 p_bme280->cal_param.dig_P8 = (s16)(((
720 BME280_PRESSURE_CALIB_DIG_P8_MSB])) <<
721 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
722 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P8_LSB]);
723 p_bme280->cal_param.dig_P9 = (s16)(((
725 BME280_PRESSURE_CALIB_DIG_P9_MSB])) <<
726 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
727 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P9_LSB]);
728 p_bme280->cal_param.dig_H1 =
729 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H1];
730 com_rslt += p_bme280->BME280_BUS_READ_FUNC(
732 BME280_HUMIDITY_CALIB_DIG_H2_LSB_REG, a_data_u8,
733 BME280_HUMIDITY_CALIB_DATA_LENGTH);
734 p_bme280->cal_param.dig_H2 = (s16)(((
736 BME280_HUMIDITY_CALIB_DIG_H2_MSB])) <<
737 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
738 | a_data_u8[BME280_HUMIDITY_CALIB_DIG_H2_LSB]);
739 p_bme280->cal_param.dig_H3 =
740 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H3];
741 p_bme280->cal_param.dig_H4 = (s16)(((
743 BME280_HUMIDITY_CALIB_DIG_H4_MSB])) <<
744 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
745 (((u8)BME280_MASK_DIG_H4) &
746 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB]));
747 p_bme280->cal_param.dig_H5 = (s16)(((
749 BME280_HUMIDITY_CALIB_DIG_H5_MSB])) <<
750 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
751 (a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB] >>
752 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
753 p_bme280->cal_param.dig_H6 =
754 (s8)a_data_u8[BME280_HUMIDITY_CALIB_DIG_H6];
759 * @brief This API is used to get
760 * the temperature oversampling setting in the register 0xF4
763 * value | Temperature oversampling
764 * ---------------------|---------------------------------
766 * 0x01 | BME280_OVERSAMP_1X
767 * 0x02 | BME280_OVERSAMP_2X
768 * 0x03 | BME280_OVERSAMP_4X
769 * 0x04 | BME280_OVERSAMP_8X
770 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
773 * @param v_value_u8 : The value of temperature over sampling
777 * @return results of bus communication function
778 * @retval 0 -> Success
779 * @retval -1 -> Error
783 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
786 /* used to return the communication result*/
787 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
788 u8 v_data_u8 = BME280_INIT_VALUE;
789 /* check the p_bme280 structure pointer as NULL*/
790 if (p_bme280 == BME280_NULL) {
791 return E_BME280_NULL_PTR;
793 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
795 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
796 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
797 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
798 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
800 p_bme280->oversamp_temperature = *v_value_u8;
805 * @brief This API is used to set
806 * the temperature oversampling setting in the register 0xF4
809 * value | Temperature oversampling
810 * ---------------------|---------------------------------
812 * 0x01 | BME280_OVERSAMP_1X
813 * 0x02 | BME280_OVERSAMP_2X
814 * 0x03 | BME280_OVERSAMP_4X
815 * 0x04 | BME280_OVERSAMP_8X
816 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
819 * @param v_value_u8 : The value of temperature over sampling
823 * @return results of bus communication function
824 * @retval 0 -> Success
825 * @retval -1 -> Error
829 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
832 /* used to return the communication result*/
833 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
834 u8 v_data_u8 = BME280_INIT_VALUE;
835 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
836 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
837 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
838 /* check the p_bme280 structure pointer as NULL*/
839 if (p_bme280 == BME280_NULL) {
840 return E_BME280_NULL_PTR;
842 v_data_u8 = p_bme280->ctrl_meas_reg;
844 BME280_SET_BITSLICE(v_data_u8,
845 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
846 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
847 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
848 com_rslt += bme280_set_soft_rst();
849 p_bme280->delay_msec(BME280_3MS_DELAY);
850 /* write previous value
851 of configuration register*/
852 v_pre_config_value_u8 = p_bme280->config_reg;
853 com_rslt += bme280_write_register(
855 &v_pre_config_value_u8,
856 BME280_GEN_READ_WRITE_DATA_LENGTH);
857 /* write previous value
858 of humidity oversampling*/
859 v_pre_ctrl_hum_value_u8 =
860 p_bme280->ctrl_hum_reg;
861 com_rslt += bme280_write_register(
862 BME280_CTRL_HUMIDITY_REG,
863 &v_pre_ctrl_hum_value_u8,
864 BME280_GEN_READ_WRITE_DATA_LENGTH);
865 /* write previous and updated value
866 of configuration register*/
867 com_rslt += bme280_write_register(
868 BME280_CTRL_MEAS_REG,
869 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
871 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
873 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
874 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
876 p_bme280->oversamp_temperature = v_value_u8;
877 /* read the control measurement register value*/
878 com_rslt = bme280_read_register(
879 BME280_CTRL_MEAS_REG,
880 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
881 p_bme280->ctrl_meas_reg = v_data_u8;
882 /* read the control humidity register value*/
883 com_rslt += bme280_read_register(
884 BME280_CTRL_HUMIDITY_REG,
885 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
886 p_bme280->ctrl_hum_reg = v_data_u8;
888 configuration register value*/
889 com_rslt += bme280_read_register(
891 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
892 p_bme280->config_reg = v_data_u8;
897 * @brief This API is used to get
898 * the pressure oversampling setting in the register 0xF4
901 * value | Pressure oversampling
902 * --------------------|--------------------------
904 * 0x01 | BME280_OVERSAMP_1X
905 * 0x02 | BME280_OVERSAMP_2X
906 * 0x03 | BME280_OVERSAMP_4X
907 * 0x04 | BME280_OVERSAMP_8X
908 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
911 * @param v_value_u8 : The value of pressure oversampling
915 * @return results of bus communication function
916 * @retval 0 -> Success
917 * @retval -1 -> Error
921 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
924 /* used to return the communication result*/
925 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
926 u8 v_data_u8 = BME280_INIT_VALUE;
927 /* check the p_bme280 structure pointer as NULL*/
928 if (p_bme280 == BME280_NULL) {
929 return E_BME280_NULL_PTR;
931 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
933 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
934 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
935 *v_value_u8 = BME280_GET_BITSLICE(
937 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
939 p_bme280->oversamp_pressure = *v_value_u8;
944 * @brief This API is used to set
945 * the pressure oversampling setting in the register 0xF4
948 * value | Pressure oversampling
949 * --------------------|--------------------------
951 * 0x01 | BME280_OVERSAMP_1X
952 * 0x02 | BME280_OVERSAMP_2X
953 * 0x03 | BME280_OVERSAMP_4X
954 * 0x04 | BME280_OVERSAMP_8X
955 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
958 * @param v_value_u8 : The value of pressure oversampling
962 * @return results of bus communication function
963 * @retval 0 -> Success
964 * @retval -1 -> Error
968 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
971 /* used to return the communication result*/
972 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
973 u8 v_data_u8 = BME280_INIT_VALUE;
974 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
975 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
976 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
977 /* check the p_bme280 structure pointer as NULL*/
978 if (p_bme280 == BME280_NULL) {
979 return E_BME280_NULL_PTR;
981 v_data_u8 = p_bme280->ctrl_meas_reg;
983 BME280_SET_BITSLICE(v_data_u8,
984 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
985 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
986 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
987 com_rslt += bme280_set_soft_rst();
988 p_bme280->delay_msec(BME280_3MS_DELAY);
989 /* write previous value of
990 configuration register*/
991 v_pre_config_value_u8 = p_bme280->config_reg;
992 com_rslt = bme280_write_register(
994 &v_pre_config_value_u8,
995 BME280_GEN_READ_WRITE_DATA_LENGTH);
996 /* write previous value of
997 humidity oversampling*/
998 v_pre_ctrl_hum_value_u8 =
999 p_bme280->ctrl_hum_reg;
1000 com_rslt += bme280_write_register(
1001 BME280_CTRL_HUMIDITY_REG,
1002 &v_pre_ctrl_hum_value_u8,
1003 BME280_GEN_READ_WRITE_DATA_LENGTH);
1004 /* write previous and updated value of
1005 control measurement register*/
1006 bme280_write_register(
1007 BME280_CTRL_MEAS_REG,
1008 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1010 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1012 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
1013 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1015 p_bme280->oversamp_pressure = v_value_u8;
1016 /* read the control measurement register value*/
1017 com_rslt = bme280_read_register(
1018 BME280_CTRL_MEAS_REG,
1019 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1020 p_bme280->ctrl_meas_reg = v_data_u8;
1021 /* read the control humidity register value*/
1022 com_rslt += bme280_read_register(
1023 BME280_CTRL_HUMIDITY_REG,
1024 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1025 p_bme280->ctrl_hum_reg = v_data_u8;
1027 configuration register value*/
1028 com_rslt += bme280_read_register(
1030 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1031 p_bme280->config_reg = v_data_u8;
1036 * @brief This API is used to get
1037 * the humidity oversampling setting in the register 0xF2
1040 * value | Humidity oversampling
1041 * ---------------------|-------------------------
1043 * 0x01 | BME280_OVERSAMP_1X
1044 * 0x02 | BME280_OVERSAMP_2X
1045 * 0x03 | BME280_OVERSAMP_4X
1046 * 0x04 | BME280_OVERSAMP_8X
1047 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1050 * @param v_value_u8 : The value of humidity over sampling
1054 * @return results of bus communication function
1055 * @retval 0 -> Success
1056 * @retval -1 -> Error
1060 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
1063 /* used to return the communication result*/
1064 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1065 u8 v_data_u8 = BME280_INIT_VALUE;
1066 /* check the p_bme280 structure pointer as NULL*/
1067 if (p_bme280 == BME280_NULL) {
1068 return E_BME280_NULL_PTR;
1070 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1072 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1073 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1074 *v_value_u8 = BME280_GET_BITSLICE(
1076 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY);
1078 p_bme280->oversamp_humidity = *v_value_u8;
1083 * @brief This API is used to set
1084 * the humidity oversampling setting in the register 0xF2
1087 * value | Humidity oversampling
1088 * ---------------------|-------------------------
1090 * 0x01 | BME280_OVERSAMP_1X
1091 * 0x02 | BME280_OVERSAMP_2X
1092 * 0x03 | BME280_OVERSAMP_4X
1093 * 0x04 | BME280_OVERSAMP_8X
1094 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1097 * @param v_value_u8 : The value of humidity over sampling
1101 * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1102 * register sets the humidity
1103 * data acquisition options of the device.
1104 * @note changes to this registers only become
1105 * effective after a write operation to
1106 * "BME280_CTRL_MEAS_REG" register.
1107 * @note In the code automated reading and writing of
1108 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1109 * @note register first set the
1110 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1111 * and then read and write
1112 * the "BME280_CTRL_MEAS_REG" register in the function.
1115 * @return results of bus communication function
1116 * @retval 0 -> Success
1117 * @retval -1 -> Error
1121 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
1124 /* used to return the communication result*/
1125 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1126 u8 v_data_u8 = BME280_INIT_VALUE;
1127 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1128 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1129 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1130 /* check the p_bme280 structure pointer as NULL*/
1131 if (p_bme280 == BME280_NULL) {
1132 return E_BME280_NULL_PTR;
1134 /* write humidity oversampling*/
1135 v_data_u8 = p_bme280->ctrl_hum_reg;
1137 BME280_SET_BITSLICE(v_data_u8,
1138 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY, v_value_u8);
1139 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1140 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1141 com_rslt += bme280_set_soft_rst();
1142 p_bme280->delay_msec(BME280_3MS_DELAY);
1143 /* write previous value of
1144 configuration register*/
1145 v_pre_config_value_u8 = p_bme280->config_reg;
1146 com_rslt += bme280_write_register(
1148 &v_pre_config_value_u8,
1149 BME280_GEN_READ_WRITE_DATA_LENGTH);
1150 /* write the value of control humidity*/
1151 com_rslt += bme280_write_register(
1152 BME280_CTRL_HUMIDITY_REG,
1153 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1154 /* write previous value of
1155 control measurement register*/
1156 pre_ctrl_meas_value =
1157 p_bme280->ctrl_meas_reg;
1158 com_rslt += bme280_write_register(
1159 BME280_CTRL_MEAS_REG,
1160 &pre_ctrl_meas_value,
1161 BME280_GEN_READ_WRITE_DATA_LENGTH);
1164 p_bme280->BME280_BUS_WRITE_FUNC(
1166 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1167 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1168 /* Control humidity write will effective only
1169 after the control measurement register*/
1170 pre_ctrl_meas_value =
1171 p_bme280->ctrl_meas_reg;
1172 com_rslt += bme280_write_register(
1173 BME280_CTRL_MEAS_REG,
1174 &pre_ctrl_meas_value,
1175 BME280_GEN_READ_WRITE_DATA_LENGTH);
1177 p_bme280->oversamp_humidity = v_value_u8;
1178 /* read the control measurement register value*/
1179 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1180 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1181 p_bme280->ctrl_meas_reg = v_data_u8;
1182 /* read the control humidity register value*/
1183 com_rslt += bme280_read_register(
1184 BME280_CTRL_HUMIDITY_REG,
1185 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1186 p_bme280->ctrl_hum_reg = v_data_u8;
1187 /* read the control configuration register value*/
1188 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1189 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1190 p_bme280->config_reg = v_data_u8;
1195 * @brief This API used to get the
1196 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1200 * @param v_power_mode_u8 : The value of power mode
1202 * -----------------|------------------
1203 * 0x00 | BME280_SLEEP_MODE
1204 * 0x01 and 0x02 | BME280_FORCED_MODE
1205 * 0x03 | BME280_NORMAL_MODE
1207 * @return results of bus communication function
1208 * @retval 0 -> Success
1209 * @retval -1 -> Error
1213 BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
1215 /* used to return the communication result*/
1216 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1217 u8 v_mode_u8r = BME280_INIT_VALUE;
1218 /* check the p_bme280 structure pointer as NULL*/
1219 if (p_bme280 == BME280_NULL) {
1220 return E_BME280_NULL_PTR;
1222 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1224 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1225 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1226 *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
1227 BME280_CTRL_MEAS_REG_POWER_MODE);
1232 * @brief This API used to set the
1233 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1237 * @param v_power_mode_u8 : The value of power mode
1239 * -----------------|------------------
1240 * 0x00 | BME280_SLEEP_MODE
1241 * 0x01 and 0x02 | BME280_FORCED_MODE
1242 * 0x03 | BME280_NORMAL_MODE
1244 * @return results of bus communication function
1245 * @retval 0 -> Success
1246 * @retval -1 -> Error
1250 BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
1252 /* used to return the communication result*/
1253 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1254 u8 v_mode_u8r = BME280_INIT_VALUE;
1255 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1256 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1257 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1258 u8 v_data_u8 = BME280_INIT_VALUE;
1259 /* check the p_bme280 structure pointer as NULL*/
1260 if (p_bme280 == BME280_NULL) {
1261 return E_BME280_NULL_PTR;
1263 if (v_power_mode_u8 <= BME280_NORMAL_MODE) {
1264 v_mode_u8r = p_bme280->ctrl_meas_reg;
1266 BME280_SET_BITSLICE(v_mode_u8r,
1267 BME280_CTRL_MEAS_REG_POWER_MODE,
1269 com_rslt = bme280_get_power_mode(
1270 &v_prev_pow_mode_u8);
1271 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1272 com_rslt += bme280_set_soft_rst();
1273 p_bme280->delay_msec(BME280_3MS_DELAY);
1274 /* write previous value of
1275 configuration register*/
1276 v_pre_config_value_u8 =
1277 p_bme280->config_reg;
1278 com_rslt = bme280_write_register(
1280 &v_pre_config_value_u8,
1281 BME280_GEN_READ_WRITE_DATA_LENGTH);
1282 /* write previous value of
1283 humidity oversampling*/
1284 v_pre_ctrl_hum_value_u8 =
1285 p_bme280->ctrl_hum_reg;
1286 com_rslt += bme280_write_register(
1287 BME280_CTRL_HUMIDITY_REG,
1288 &v_pre_ctrl_hum_value_u8,
1289 BME280_GEN_READ_WRITE_DATA_LENGTH);
1290 /* write previous and updated value of
1291 control measurement register*/
1292 com_rslt += bme280_write_register(
1293 BME280_CTRL_MEAS_REG,
1295 BME280_GEN_READ_WRITE_DATA_LENGTH);
1298 p_bme280->BME280_BUS_WRITE_FUNC(
1300 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1302 BME280_GEN_READ_WRITE_DATA_LENGTH);
1304 /* read the control measurement register value*/
1305 com_rslt = bme280_read_register(
1306 BME280_CTRL_MEAS_REG,
1307 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1308 p_bme280->ctrl_meas_reg = v_data_u8;
1309 /* read the control humidity register value*/
1310 com_rslt += bme280_read_register(
1311 BME280_CTRL_HUMIDITY_REG,
1312 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1313 p_bme280->ctrl_hum_reg = v_data_u8;
1314 /* read the config register value*/
1315 com_rslt += bme280_read_register(
1317 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1318 p_bme280->config_reg = v_data_u8;
1320 com_rslt = E_BME280_OUT_OF_RANGE;
1326 * @brief Used to reset the sensor
1327 * The value 0xB6 is written to the 0xE0
1328 * register the device is reset using the
1329 * complete power-on-reset procedure.
1330 * @note Soft reset can be easily set using bme280_set_softreset().
1331 * @note Usage Hint : bme280_set_softreset()
1334 * @return results of bus communication function
1335 * @retval 0 -> Success
1336 * @retval -1 -> Error
1340 BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst(void)
1342 /* used to return the communication result*/
1343 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1344 u8 v_data_u8 = BME280_SOFT_RESET_CODE;
1345 /* check the p_bme280 structure pointer as NULL*/
1346 if (p_bme280 == BME280_NULL) {
1347 return E_BME280_NULL_PTR;
1349 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1351 BME280_RST_REG, &v_data_u8,
1352 BME280_GEN_READ_WRITE_DATA_LENGTH);
1357 * @brief This API used to get the sensor
1358 * SPI mode(communication type) in the register 0xF5 bit 0
1362 * @param v_enable_disable_u8 : The value of SPI enable
1363 * value | Description
1364 * --------|--------------
1370 * @return results of bus communication function
1371 * @retval 0 -> Success
1372 * @retval -1 -> Error
1376 BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
1378 /* used to return the communication result*/
1379 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1380 u8 v_data_u8 = BME280_INIT_VALUE;
1381 /* check the p_bme280 structure pointer as NULL*/
1382 if (p_bme280 == BME280_NULL) {
1383 return E_BME280_NULL_PTR;
1385 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1387 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1388 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1389 *v_enable_disable_u8 = BME280_GET_BITSLICE(
1391 BME280_CONFIG_REG_SPI3_ENABLE);
1396 * @brief This API used to set the sensor
1397 * SPI mode(communication type) in the register 0xF5 bit 0
1401 * @param v_enable_disable_u8 : The value of SPI enable
1402 * value | Description
1403 * --------|--------------
1409 * @return results of bus communication function
1410 * @retval 0 -> Success
1411 * @retval -1 -> Error
1415 BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
1417 /* used to return the communication result*/
1418 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1419 u8 v_data_u8 = BME280_INIT_VALUE;
1420 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1421 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1422 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1423 /* check the p_bme280 structure pointer as NULL*/
1424 if (p_bme280 == BME280_NULL) {
1425 return E_BME280_NULL_PTR;
1427 v_data_u8 = p_bme280->config_reg;
1429 BME280_SET_BITSLICE(v_data_u8,
1430 BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
1431 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1432 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1433 com_rslt += bme280_set_soft_rst();
1434 p_bme280->delay_msec(BME280_3MS_DELAY);
1435 /* write previous and updated value of
1436 configuration register*/
1437 com_rslt += bme280_write_register(
1439 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1440 /* write previous value of
1441 humidity oversampling*/
1442 v_pre_ctrl_hum_value_u8 =
1443 p_bme280->ctrl_hum_reg;
1444 com_rslt += bme280_write_register(
1445 BME280_CTRL_HUMIDITY_REG,
1446 &v_pre_ctrl_hum_value_u8,
1447 BME280_GEN_READ_WRITE_DATA_LENGTH);
1448 /* write previous value of
1449 control measurement register*/
1450 pre_ctrl_meas_value =
1451 p_bme280->ctrl_meas_reg;
1452 com_rslt += bme280_write_register(
1453 BME280_CTRL_MEAS_REG,
1454 &pre_ctrl_meas_value,
1455 BME280_GEN_READ_WRITE_DATA_LENGTH);
1458 p_bme280->BME280_BUS_WRITE_FUNC(
1460 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1461 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1463 /* read the control measurement register value*/
1464 com_rslt += bme280_read_register(
1465 BME280_CTRL_MEAS_REG,
1466 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1467 p_bme280->ctrl_meas_reg = v_data_u8;
1468 /* read the control humidity register value*/
1469 com_rslt += bme280_read_register(
1470 BME280_CTRL_HUMIDITY_REG,
1471 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1472 p_bme280->ctrl_hum_reg = v_data_u8;
1473 /* read the control configuration register value*/
1474 com_rslt += bme280_read_register(
1476 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1477 p_bme280->config_reg = v_data_u8;
1482 * @brief This API is used to reads filter setting
1483 * in the register 0xF5 bit 3 and 4
1487 * @param v_value_u8 : The value of IIR filter coefficient
1489 * value | Filter coefficient
1490 * -------------|-------------------------
1491 * 0x00 | BME280_FILTER_COEFF_OFF
1492 * 0x01 | BME280_FILTER_COEFF_2
1493 * 0x02 | BME280_FILTER_COEFF_4
1494 * 0x03 | BME280_FILTER_COEFF_8
1495 * 0x04 | BME280_FILTER_COEFF_16
1497 * @return results of bus communication function
1498 * @retval 0 -> Success
1499 * @retval -1 -> Error
1503 BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
1505 /* used to return the communication result*/
1506 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1507 u8 v_data_u8 = BME280_INIT_VALUE;
1508 /* check the p_bme280 structure pointer as NULL*/
1509 if (p_bme280 == BME280_NULL) {
1510 return E_BME280_NULL_PTR;
1512 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1514 BME280_CONFIG_REG_FILTER__REG,
1515 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1516 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
1517 BME280_CONFIG_REG_FILTER);
1522 * @brief This API is used to write filter setting
1523 * in the register 0xF5 bit 3 and 4
1527 * @param v_value_u8 : The value of IIR filter coefficient
1529 * value | Filter coefficient
1530 * -------------|-------------------------
1531 * 0x00 | BME280_FILTER_COEFF_OFF
1532 * 0x01 | BME280_FILTER_COEFF_2
1533 * 0x02 | BME280_FILTER_COEFF_4
1534 * 0x03 | BME280_FILTER_COEFF_8
1535 * 0x04 | BME280_FILTER_COEFF_16
1537 * @return results of bus communication function
1538 * @retval 0 -> Success
1539 * @retval -1 -> Error
1543 BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
1545 /* used to return the communication result*/
1546 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1547 u8 v_data_u8 = BME280_INIT_VALUE;
1548 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1549 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1550 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1551 /* check the p_bme280 structure pointer as NULL*/
1552 if (p_bme280 == BME280_NULL) {
1553 return E_BME280_NULL_PTR;
1555 v_data_u8 = p_bme280->config_reg;
1557 BME280_SET_BITSLICE(v_data_u8,
1558 BME280_CONFIG_REG_FILTER, v_value_u8);
1559 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1560 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1561 com_rslt += bme280_set_soft_rst();
1562 p_bme280->delay_msec(BME280_3MS_DELAY);
1563 /* write previous and updated value of
1564 configuration register*/
1565 com_rslt += bme280_write_register(
1567 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1568 /* write previous value of
1569 humidity oversampling*/
1570 v_pre_ctrl_hum_value_u8 =
1571 p_bme280->ctrl_hum_reg;
1572 com_rslt += bme280_write_register(
1573 BME280_CTRL_HUMIDITY_REG,
1574 &v_pre_ctrl_hum_value_u8,
1575 BME280_GEN_READ_WRITE_DATA_LENGTH);
1576 /* write previous value of
1577 control measurement register*/
1578 pre_ctrl_meas_value =
1579 p_bme280->ctrl_meas_reg;
1580 com_rslt += bme280_write_register(
1581 BME280_CTRL_MEAS_REG,
1582 &pre_ctrl_meas_value,
1583 BME280_GEN_READ_WRITE_DATA_LENGTH);
1586 p_bme280->BME280_BUS_WRITE_FUNC(
1588 BME280_CONFIG_REG_FILTER__REG,
1589 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1591 /* read the control measurement register value*/
1592 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1593 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1594 p_bme280->ctrl_meas_reg = v_data_u8;
1595 /* read the control humidity register value*/
1596 com_rslt += bme280_read_register(
1597 BME280_CTRL_HUMIDITY_REG,
1598 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1599 p_bme280->ctrl_hum_reg = v_data_u8;
1600 /* read the configuration register value*/
1601 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1602 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1603 p_bme280->config_reg = v_data_u8;
1608 * @brief This API used to Read the
1609 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1611 * @param v_standby_durn_u8 : The value of standby duration time value.
1612 * value | standby duration
1613 * -------------|-----------------------
1614 * 0x00 | BME280_STANDBY_TIME_1_MS
1615 * 0x01 | BME280_STANDBY_TIME_63_MS
1616 * 0x02 | BME280_STANDBY_TIME_125_MS
1617 * 0x03 | BME280_STANDBY_TIME_250_MS
1618 * 0x04 | BME280_STANDBY_TIME_500_MS
1619 * 0x05 | BME280_STANDBY_TIME_1000_MS
1620 * 0x06 | BME280_STANDBY_TIME_2000_MS
1621 * 0x07 | BME280_STANDBY_TIME_4000_MS
1624 * @return results of bus communication function
1625 * @retval 0 -> Success
1626 * @retval -1 -> Error
1630 BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
1632 /* used to return the communication result*/
1633 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1634 u8 v_data_u8 = BME280_INIT_VALUE;
1635 /* check the p_bme280 structure pointer as NULL*/
1636 if (p_bme280 == BME280_NULL) {
1637 return E_BME280_NULL_PTR;
1639 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1641 BME280_CONFIG_REG_TSB__REG,
1642 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1643 *v_standby_durn_u8 = BME280_GET_BITSLICE(
1644 v_data_u8, BME280_CONFIG_REG_TSB);
1649 * @brief This API used to write the
1650 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1652 * @param v_standby_durn_u8 : The value of standby duration time value.
1653 * value | standby duration
1654 * -------------|-----------------------
1655 * 0x00 | BME280_STANDBY_TIME_1_MS
1656 * 0x01 | BME280_STANDBY_TIME_63_MS
1657 * 0x02 | BME280_STANDBY_TIME_125_MS
1658 * 0x03 | BME280_STANDBY_TIME_250_MS
1659 * 0x04 | BME280_STANDBY_TIME_500_MS
1660 * 0x05 | BME280_STANDBY_TIME_1000_MS
1661 * 0x06 | BME280_STANDBY_TIME_2000_MS
1662 * 0x07 | BME280_STANDBY_TIME_4000_MS
1664 * @note Normal mode comprises an automated perpetual
1665 * cycling between an (active)
1666 * Measurement period and an (inactive) standby period.
1667 * @note The standby time is determined by
1668 * the contents of the register t_sb.
1669 * Standby time can be set using BME280_STANDBY_TIME_125_MS.
1671 * @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
1675 * @return results of bus communication function
1676 * @retval 0 -> Success
1677 * @retval -1 -> Error
1681 BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
1683 /* used to return the communication result*/
1684 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1685 u8 v_data_u8 = BME280_INIT_VALUE;
1686 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1687 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1688 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1689 /* check the p_bme280 structure pointer as NULL*/
1690 if (p_bme280 == BME280_NULL) {
1691 return E_BME280_NULL_PTR;
1693 v_data_u8 = p_bme280->config_reg;
1695 BME280_SET_BITSLICE(v_data_u8,
1696 BME280_CONFIG_REG_TSB, v_standby_durn_u8);
1697 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1698 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1699 com_rslt += bme280_set_soft_rst();
1700 p_bme280->delay_msec(BME280_3MS_DELAY);
1701 /* write previous and updated value of
1702 configuration register*/
1703 com_rslt += bme280_write_register(
1705 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1706 /* write previous value of
1707 humidity oversampling*/
1708 v_pre_ctrl_hum_value_u8 =
1709 p_bme280->ctrl_hum_reg;
1710 com_rslt += bme280_write_register(
1711 BME280_CTRL_HUMIDITY_REG,
1712 &v_pre_ctrl_hum_value_u8,
1713 BME280_GEN_READ_WRITE_DATA_LENGTH);
1714 /* write previous value of control
1715 measurement register*/
1716 pre_ctrl_meas_value =
1717 p_bme280->ctrl_meas_reg;
1718 com_rslt += bme280_write_register(
1719 BME280_CTRL_MEAS_REG,
1720 &pre_ctrl_meas_value,
1721 BME280_GEN_READ_WRITE_DATA_LENGTH);
1724 p_bme280->BME280_BUS_WRITE_FUNC(
1726 BME280_CONFIG_REG_TSB__REG,
1727 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1729 /* read the control measurement register value*/
1730 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1731 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1732 p_bme280->ctrl_meas_reg = v_data_u8;
1733 /* read the control humidity register value*/
1734 com_rslt += bme280_read_register(
1735 BME280_CTRL_HUMIDITY_REG,
1736 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1737 p_bme280->ctrl_hum_reg = v_data_u8;
1738 /* read the configuration register value*/
1739 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1740 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1741 p_bme280->config_reg = v_data_u8;
1746 * @brief Writes the working mode to the sensor
1751 * @param v_work_mode_u8 : Mode to be set
1752 * value | Working mode
1753 * ----------|--------------------
1754 * 0 | BME280_ULTRALOWPOWER_MODE
1755 * 1 | BME280_LOWPOWER_MODE
1756 * 2 | BME280_STANDARDRESOLUTION_MODE
1757 * 3 | BME280_HIGHRESOLUTION_MODE
1758 * 4 | BME280_ULTRAHIGHRESOLUTION_MODE
1760 * @return results of bus communication function
1761 * @retval 0 -> Success
1762 * @retval -1 -> Error
1766 /*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
1768 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1769 u8 v_data_u8 = BME280_INIT_VALUE;
1770 if (p_bme280 == BME280_NULL) {
1771 return E_BME280_NULL_PTR;
1773 if (v_work_mode_u8 <= BME280_ULTRAHIGHRESOLUTION_MODE) {
1774 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1775 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1776 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1777 if (com_rslt == SUCCESS) {
1778 switch (v_work_mode_u8) {
1779 case BME280_ULTRALOWPOWER_MODE:
1780 p_bme280->oversamp_temperature =
1781 BME280_ULTRALOWPOWER_OSRS_T;
1783 BME280_ULTRALOWPOWER_OSRS_P;
1785 case BME280_LOWPOWER_MODE:
1786 p_bme280->oversamp_temperature =
1787 BME280_LOWPOWER_OSRS_T;
1788 p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
1790 case BME280_STANDARDRESOLUTION_MODE:
1791 p_bme280->oversamp_temperature =
1792 BME280_STANDARDRESOLUTION_OSRS_T;
1794 BME280_STANDARDRESOLUTION_OSRS_P;
1796 case BME280_HIGHRESOLUTION_MODE:
1797 p_bme280->oversamp_temperature =
1798 BME280_HIGHRESOLUTION_OSRS_T;
1799 p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
1801 case BME280_ULTRAHIGHRESOLUTION_MODE:
1802 p_bme280->oversamp_temperature =
1803 BME280_ULTRAHIGHRESOLUTION_OSRS_T;
1805 BME280_ULTRAHIGHRESOLUTION_OSRS_P;
1808 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1809 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
1810 p_bme280->oversamp_temperature);
1811 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1812 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
1814 com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
1815 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1816 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1819 com_rslt = E_BME280_OUT_OF_RANGE;
1825 * @brief This API used to read uncompensated
1826 * temperature,pressure and humidity in forced mode
1829 * @param v_uncom_pressure_s32: The value of uncompensated pressure
1830 * @param v_uncom_temperature_s32: The value of uncompensated temperature
1831 * @param v_uncom_humidity_s32: The value of uncompensated humidity
1834 * @return results of bus communication function
1835 * @retval 0 -> Success
1836 * @retval -1 -> Error
1840 BME280_RETURN_FUNCTION_TYPE
1841 bme280_get_forced_uncomp_pressure_temperature_humidity(
1842 s32 *v_uncom_pressure_s32,
1843 s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
1845 /* used to return the communication result*/
1846 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1847 u8 v_data_u8 = BME280_INIT_VALUE;
1848 u8 v_waittime_u8 = BME280_INIT_VALUE;
1849 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1850 u8 v_mode_u8r = BME280_INIT_VALUE;
1851 u8 pre_ctrl_config_value = BME280_INIT_VALUE;
1852 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1853 /* check the p_bme280 structure pointer as NULL*/
1854 if (p_bme280 == BME280_NULL) {
1855 return E_BME280_NULL_PTR;
1857 v_mode_u8r = p_bme280->ctrl_meas_reg;
1859 BME280_SET_BITSLICE(v_mode_u8r,
1860 BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
1861 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1862 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1863 com_rslt += bme280_set_soft_rst();
1864 p_bme280->delay_msec(BME280_3MS_DELAY);
1865 /* write previous and updated value of
1866 configuration register*/
1867 pre_ctrl_config_value = p_bme280->config_reg;
1868 com_rslt += bme280_write_register(
1870 &pre_ctrl_config_value,
1871 BME280_GEN_READ_WRITE_DATA_LENGTH);
1872 /* write previous value of
1873 humidity oversampling*/
1874 v_pre_ctrl_hum_value_u8 =
1875 p_bme280->ctrl_hum_reg;
1876 com_rslt += bme280_write_register(
1877 BME280_CTRL_HUMIDITY_REG,
1878 &v_pre_ctrl_hum_value_u8,
1879 BME280_GEN_READ_WRITE_DATA_LENGTH);
1880 /* write the force mode */
1881 com_rslt += bme280_write_register(
1882 BME280_CTRL_MEAS_REG,
1883 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1885 /* write previous value of
1886 humidity oversampling*/
1887 v_pre_ctrl_hum_value_u8 =
1888 p_bme280->ctrl_hum_reg;
1889 com_rslt += bme280_write_register(
1890 BME280_CTRL_HUMIDITY_REG,
1891 &v_pre_ctrl_hum_value_u8,
1892 BME280_GEN_READ_WRITE_DATA_LENGTH);
1893 /* write the force mode */
1894 com_rslt += bme280_write_register(
1895 BME280_CTRL_MEAS_REG,
1896 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1898 bme280_compute_wait_time(&v_waittime_u8);
1899 p_bme280->delay_msec(v_waittime_u8);
1900 /* read the force-mode value of pressure
1901 temperature and humidity*/
1903 bme280_read_uncomp_pressure_temperature_humidity(
1904 v_uncom_pressure_s32, v_uncom_temperature_s32,
1905 v_uncom_humidity_s32);
1907 /* read the control humidity register value*/
1908 com_rslt += bme280_read_register(
1909 BME280_CTRL_HUMIDITY_REG,
1910 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1911 p_bme280->ctrl_hum_reg = v_data_u8;
1912 /* read the configuration register value*/
1913 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1914 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1915 p_bme280->config_reg = v_data_u8;
1917 /* read the control measurement register value*/
1918 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1919 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1920 p_bme280->ctrl_meas_reg = v_data_u8;
1926 * This API write the data to
1927 * the given register
1930 * @param v_addr_u8 -> Address of the register
1931 * @param v_data_u8 -> The data from the register
1932 * @param v_len_u8 -> no of bytes to read
1935 * @return results of bus communication function
1936 * @retval 0 -> Success
1937 * @retval -1 -> Error
1941 BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
1942 u8 *v_data_u8, u8 v_len_u8)
1944 /* used to return the communication result*/
1945 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1946 /* check the p_bme280 structure pointer as NULL*/
1947 if (p_bme280 == BME280_NULL) {
1948 return E_BME280_NULL_PTR;
1950 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1952 v_addr_u8, v_data_u8, v_len_u8);
1958 * This API reads the data from
1959 * the given register
1962 * @param v_addr_u8 -> Address of the register
1963 * @param v_data_u8 -> The data from the register
1964 * @param v_len_u8 -> no of bytes to read
1967 * @return results of bus communication function
1968 * @retval 0 -> Success
1969 * @retval -1 -> Error
1973 BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
1974 u8 *v_data_u8, u8 v_len_u8)
1976 /* used to return the communication result*/
1977 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1978 /* check the p_bme280 structure pointer as NULL*/
1979 if (p_bme280 == BME280_NULL) {
1980 return E_BME280_NULL_PTR;
1982 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1984 v_addr_u8, v_data_u8, v_len_u8);
1988 #ifdef BME280_ENABLE_FLOAT
1990 * @brief Reads actual temperature from uncompensated temperature
1991 * @note returns the value in Degree centigrade
1992 * @note Output value of "51.23" equals 51.23 DegC.
1996 * @param v_uncom_temperature_s32 : value of uncompensated temperature
2000 * @return Return the actual temperature in floating point
2003 double bme280_compensate_temperature_double(s32 v_uncom_temperature_s32)
2005 double v_x1_u32 = BME280_INIT_VALUE;
2006 double v_x2_u32 = BME280_INIT_VALUE;
2007 double temperature = BME280_INIT_VALUE;
2009 v_x1_u32 = (((double)v_uncom_temperature_s32) / 16384.0 -
2010 ((double)p_bme280->cal_param.dig_T1) / 1024.0) *
2011 ((double)p_bme280->cal_param.dig_T2);
2012 v_x2_u32 = ((((double)v_uncom_temperature_s32) / 131072.0 -
2013 ((double)p_bme280->cal_param.dig_T1) / 8192.0) *
2014 (((double)v_uncom_temperature_s32) / 131072.0 -
2015 ((double)p_bme280->cal_param.dig_T1) / 8192.0)) *
2016 ((double)p_bme280->cal_param.dig_T3);
2017 p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
2018 temperature = (v_x1_u32 + v_x2_u32) / 5120.0;
2024 * @brief Reads actual pressure from uncompensated pressure
2025 * @note Returns pressure in Pa as double.
2026 * @note Output value of "96386.2"
2027 * equals 96386.2 Pa = 963.862 hPa.
2030 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2033 * @return Return the actual pressure in floating point
2036 double bme280_compensate_pressure_double(s32 v_uncom_pressure_s32)
2038 double v_x1_u32 = BME280_INIT_VALUE;
2039 double v_x2_u32 = BME280_INIT_VALUE;
2040 double pressure = BME280_INIT_VALUE;
2042 v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
2044 v_x2_u32 = v_x1_u32 * v_x1_u32 *
2045 ((double)p_bme280->cal_param.dig_P6) / 32768.0;
2046 v_x2_u32 = v_x2_u32 + v_x1_u32 *
2047 ((double)p_bme280->cal_param.dig_P5) * 2.0;
2048 v_x2_u32 = (v_x2_u32 / 4.0) +
2049 (((double)p_bme280->cal_param.dig_P4) * 65536.0);
2050 v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
2051 v_x1_u32 * v_x1_u32 / 524288.0 +
2052 ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) / 524288.0;
2053 v_x1_u32 = (1.0 + v_x1_u32 / 32768.0) *
2054 ((double)p_bme280->cal_param.dig_P1);
2055 pressure = 1048576.0 - (double)v_uncom_pressure_s32;
2056 /* Avoid exception caused by division by zero */
2057 if ((v_x1_u32 > 0) || (v_x1_u32 < 0))
2058 pressure = (pressure - (v_x2_u32 / 4096.0)) * 6250.0 / v_x1_u32;
2060 return BME280_INVALID_DATA;
2061 v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
2062 pressure * pressure / 2147483648.0;
2063 v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8) / 32768.0;
2064 pressure = pressure + (v_x1_u32 + v_x2_u32 +
2065 ((double)p_bme280->cal_param.dig_P7)) / 16.0;
2070 * @brief Reads actual humidity from uncompensated humidity
2071 * @note returns the value in relative humidity (%rH)
2072 * @note Output value of "42.12" equals 42.12 %rH
2074 * @param v_uncom_humidity_s32 : value of uncompensated humidity
2078 * @return Return the actual humidity in floating point
2081 double bme280_compensate_humidity_double(s32 v_uncom_humidity_s32)
2083 double var_h = BME280_INIT_VALUE;
2085 var_h = (((double)p_bme280->cal_param.t_fine) - 76800.0);
2086 if ((var_h > 0) || (var_h < 0))
2087 var_h = (v_uncom_humidity_s32 -
2088 (((double)p_bme280->cal_param.dig_H4) * 64.0 +
2089 ((double)p_bme280->cal_param.dig_H5) / 16384.0 * var_h))*
2090 (((double)p_bme280->cal_param.dig_H2) / 65536.0 *
2091 (1.0 + ((double) p_bme280->cal_param.dig_H6)
2092 / 67108864.0 * var_h * (1.0 + ((double)
2093 p_bme280->cal_param.dig_H3) / 67108864.0 * var_h)));
2095 return BME280_INVALID_DATA;
2096 var_h = var_h * (1.0 - ((double)
2097 p_bme280->cal_param.dig_H1)*var_h / 524288.0);
2100 else if (var_h < 0.0)
2106 #if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
2108 * @brief Reads actual pressure from uncompensated pressure
2109 * @note Returns the value in Pa as unsigned 32 bit
2110 * integer in Q24.8 format (24 integer bits and
2111 * 8 fractional bits).
2112 * @note Output value of "24674867"
2113 * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
2117 * @param v_uncom_pressure_s32 : value of uncompensated temperature
2120 * @return Return the actual pressure in u32
2123 u32 bme280_compensate_pressure_int64(s32 v_uncom_pressure_s32)
2125 s64 v_x1_s64r = BME280_INIT_VALUE;
2126 s64 v_x2_s64r = BME280_INIT_VALUE;
2127 s64 pressure = BME280_INIT_VALUE;
2129 v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
2131 v_x2_s64r = v_x1_s64r * v_x1_s64r *
2132 (s64)p_bme280->cal_param.dig_P6;
2133 v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
2134 (s64)p_bme280->cal_param.dig_P5)
2135 << BME280_SHIFT_BIT_POSITION_BY_17_BITS);
2136 v_x2_s64r = v_x2_s64r +
2137 (((s64)p_bme280->cal_param.dig_P4)
2138 << BME280_SHIFT_BIT_POSITION_BY_35_BITS);
2139 v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
2140 (s64)p_bme280->cal_param.dig_P3)
2141 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2142 ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
2143 << BME280_SHIFT_BIT_POSITION_BY_12_BITS);
2144 v_x1_s64r = (((((s64)1)
2145 << BME280_SHIFT_BIT_POSITION_BY_47_BITS) + v_x1_s64r)) *
2146 ((s64)p_bme280->cal_param.dig_P1)
2147 >> BME280_SHIFT_BIT_POSITION_BY_33_BITS;
2148 pressure = 1048576 - v_uncom_pressure_s32;
2149 /* Avoid exception caused by division by zero */
2150 if (v_x1_s64r != BME280_INIT_VALUE)
2151 #if defined __KERNEL__
2152 pressure = div64_s64((((pressure
2153 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2154 * 3125), v_x1_s64r);
2156 pressure = (((pressure
2157 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2158 * 3125) / v_x1_s64r;
2161 return BME280_INVALID_DATA;
2162 v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
2163 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS) *
2164 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
2165 >> BME280_SHIFT_BIT_POSITION_BY_25_BITS;
2166 v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
2167 pressure) >> BME280_SHIFT_BIT_POSITION_BY_19_BITS;
2168 pressure = (((pressure + v_x1_s64r +
2169 v_x2_s64r) >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2170 (((s64)p_bme280->cal_param.dig_P7)
2171 << BME280_SHIFT_BIT_POSITION_BY_04_BITS));
2173 return (u32)pressure;
2176 * @brief Reads actual pressure from uncompensated pressure
2177 * @note Returns the value in Pa.
2178 * @note Output value of "12337434"
2179 * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
2183 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2186 * @return the actual pressure in u32
2189 u32 bme280_compensate_pressure_int64_twentyfour_bit_output(
2190 s32 v_uncom_pressure_s32)
2192 u32 pressure = BME280_INIT_VALUE;
2194 pressure = bme280_compensate_pressure_int64(
2195 v_uncom_pressure_s32);
2196 pressure = (u32)(pressure >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
2201 * @brief Computing waiting time for sensor data read
2206 * @param v_delaytime_u8 : The value of delay time for force mode
2209 * @retval 0 -> Success
2213 BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
2216 /* used to return the communication result*/
2217 BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
2219 *v_delaytime_u8 = (T_INIT_MAX +
2220 T_MEASURE_PER_OSRS_MAX *
2222 p_bme280->oversamp_temperature)
2223 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT)
2224 + ((1 << p_bme280->oversamp_pressure)
2225 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) +
2226 ((1 << p_bme280->oversamp_humidity)
2227 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
2228 + ((p_bme280->oversamp_pressure > 0) ?
2229 T_SETUP_PRESSURE_MAX : 0) +
2230 ((p_bme280->oversamp_humidity > 0) ?
2231 T_SETUP_HUMIDITY_MAX : 0) + 15) / 16;