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
634 * @return results of bus communication function
635 * @retval 0 -> Success
636 * @retval -1 -> Error
640 BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param(void)
642 /* used to return the communication result*/
643 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
644 u8 a_data_u8[BME280_CALIB_DATA_SIZE] = {
645 BME280_INIT_VALUE, BME280_INIT_VALUE,
646 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
647 BME280_INIT_VALUE, BME280_INIT_VALUE, BME280_INIT_VALUE,
648 BME280_INIT_VALUE, 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 /* check the p_bme280 structure pointer as NULL*/
655 if (p_bme280 == BME280_NULL) {
656 return E_BME280_NULL_PTR;
658 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
660 BME280_TEMPERATURE_CALIB_DIG_T1_LSB_REG,
662 BME280_PRESSURE_TEMPERATURE_CALIB_DATA_LENGTH);
664 p_bme280->cal_param.dig_T1 = (u16)(((
666 BME280_TEMPERATURE_CALIB_DIG_T1_MSB])) <<
667 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
668 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T1_LSB]);
669 p_bme280->cal_param.dig_T2 = (s16)(((
671 BME280_TEMPERATURE_CALIB_DIG_T2_MSB])) <<
672 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
673 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T2_LSB]);
674 p_bme280->cal_param.dig_T3 = (s16)(((
676 BME280_TEMPERATURE_CALIB_DIG_T3_MSB])) <<
677 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
678 | a_data_u8[BME280_TEMPERATURE_CALIB_DIG_T3_LSB]);
679 p_bme280->cal_param.dig_P1 = (u16)(((
681 BME280_PRESSURE_CALIB_DIG_P1_MSB])) <<
682 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
683 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P1_LSB]);
684 p_bme280->cal_param.dig_P2 = (s16)(((
686 BME280_PRESSURE_CALIB_DIG_P2_MSB])) <<
687 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
688 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P2_LSB]);
689 p_bme280->cal_param.dig_P3 = (s16)(((
691 BME280_PRESSURE_CALIB_DIG_P3_MSB])) <<
692 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
694 BME280_PRESSURE_CALIB_DIG_P3_LSB]);
695 p_bme280->cal_param.dig_P4 = (s16)(((
697 BME280_PRESSURE_CALIB_DIG_P4_MSB])) <<
698 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
699 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P4_LSB]);
700 p_bme280->cal_param.dig_P5 = (s16)(((
702 BME280_PRESSURE_CALIB_DIG_P5_MSB])) <<
703 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
704 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P5_LSB]);
705 p_bme280->cal_param.dig_P6 = (s16)(((
707 BME280_PRESSURE_CALIB_DIG_P6_MSB])) <<
708 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
709 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P6_LSB]);
710 p_bme280->cal_param.dig_P7 = (s16)(((
712 BME280_PRESSURE_CALIB_DIG_P7_MSB])) <<
713 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
714 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P7_LSB]);
715 p_bme280->cal_param.dig_P8 = (s16)(((
717 BME280_PRESSURE_CALIB_DIG_P8_MSB])) <<
718 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
719 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P8_LSB]);
720 p_bme280->cal_param.dig_P9 = (s16)(((
722 BME280_PRESSURE_CALIB_DIG_P9_MSB])) <<
723 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
724 | a_data_u8[BME280_PRESSURE_CALIB_DIG_P9_LSB]);
725 p_bme280->cal_param.dig_H1 =
726 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H1];
727 com_rslt += p_bme280->BME280_BUS_READ_FUNC(
729 BME280_HUMIDITY_CALIB_DIG_H2_LSB_REG, a_data_u8,
730 BME280_HUMIDITY_CALIB_DATA_LENGTH);
731 p_bme280->cal_param.dig_H2 = (s16)(((
733 BME280_HUMIDITY_CALIB_DIG_H2_MSB])) <<
734 BME280_SHIFT_BIT_POSITION_BY_08_BITS)
735 | a_data_u8[BME280_HUMIDITY_CALIB_DIG_H2_LSB]);
736 p_bme280->cal_param.dig_H3 =
737 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H3];
738 p_bme280->cal_param.dig_H4 = (s16)(((
740 BME280_HUMIDITY_CALIB_DIG_H4_MSB])) <<
741 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
742 (((u8)BME280_MASK_DIG_H4) &
743 a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB]));
744 p_bme280->cal_param.dig_H5 = (s16)(((
746 BME280_HUMIDITY_CALIB_DIG_H5_MSB])) <<
747 BME280_SHIFT_BIT_POSITION_BY_04_BITS) |
748 (a_data_u8[BME280_HUMIDITY_CALIB_DIG_H4_LSB] >>
749 BME280_SHIFT_BIT_POSITION_BY_04_BITS));
750 p_bme280->cal_param.dig_H6 =
751 (s8)a_data_u8[BME280_HUMIDITY_CALIB_DIG_H6];
756 * @brief This API is used to get
757 * the temperature oversampling setting in the register 0xF4
760 * value | Temperature oversampling
761 * ---------------------|---------------------------------
763 * 0x01 | BME280_OVERSAMP_1X
764 * 0x02 | BME280_OVERSAMP_2X
765 * 0x03 | BME280_OVERSAMP_4X
766 * 0x04 | BME280_OVERSAMP_8X
767 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
770 * @param v_value_u8 : The value of temperature over sampling
774 * @return results of bus communication function
775 * @retval 0 -> Success
776 * @retval -1 -> Error
780 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
783 /* used to return the communication result*/
784 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
785 u8 v_data_u8 = BME280_INIT_VALUE;
786 /* check the p_bme280 structure pointer as NULL*/
787 if (p_bme280 == BME280_NULL) {
788 return E_BME280_NULL_PTR;
790 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
792 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
793 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
794 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
795 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
797 p_bme280->oversamp_temperature = *v_value_u8;
802 * @brief This API is used to set
803 * the temperature oversampling setting in the register 0xF4
806 * value | Temperature oversampling
807 * ---------------------|---------------------------------
809 * 0x01 | BME280_OVERSAMP_1X
810 * 0x02 | BME280_OVERSAMP_2X
811 * 0x03 | BME280_OVERSAMP_4X
812 * 0x04 | BME280_OVERSAMP_8X
813 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
816 * @param v_value_u8 : The value of temperature over sampling
820 * @return results of bus communication function
821 * @retval 0 -> Success
822 * @retval -1 -> Error
826 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
829 /* used to return the communication result*/
830 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
831 u8 v_data_u8 = BME280_INIT_VALUE;
832 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
833 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
834 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
835 /* check the p_bme280 structure pointer as NULL*/
836 if (p_bme280 == BME280_NULL) {
837 return E_BME280_NULL_PTR;
839 v_data_u8 = p_bme280->ctrl_meas_reg;
841 BME280_SET_BITSLICE(v_data_u8,
842 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
843 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
844 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
845 com_rslt += bme280_set_soft_rst();
846 p_bme280->delay_msec(BME280_3MS_DELAY);
847 /* write previous value
848 of configuration register*/
849 v_pre_config_value_u8 = p_bme280->config_reg;
850 com_rslt += bme280_write_register(
852 &v_pre_config_value_u8,
853 BME280_GEN_READ_WRITE_DATA_LENGTH);
854 /* write previous value
855 of humidity oversampling*/
856 v_pre_ctrl_hum_value_u8 =
857 p_bme280->ctrl_hum_reg;
858 com_rslt += bme280_write_register(
859 BME280_CTRL_HUMIDITY_REG,
860 &v_pre_ctrl_hum_value_u8,
861 BME280_GEN_READ_WRITE_DATA_LENGTH);
862 /* write previous and updated value
863 of configuration register*/
864 com_rslt += bme280_write_register(
865 BME280_CTRL_MEAS_REG,
866 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
868 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
870 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
871 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
873 p_bme280->oversamp_temperature = v_value_u8;
874 /* read the control measurement register value*/
875 com_rslt = bme280_read_register(
876 BME280_CTRL_MEAS_REG,
877 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
878 p_bme280->ctrl_meas_reg = v_data_u8;
879 /* read the control humidity register value*/
880 com_rslt += bme280_read_register(
881 BME280_CTRL_HUMIDITY_REG,
882 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
883 p_bme280->ctrl_hum_reg = v_data_u8;
885 configuration register value*/
886 com_rslt += bme280_read_register(
888 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
889 p_bme280->config_reg = v_data_u8;
894 * @brief This API is used to get
895 * the pressure oversampling setting in the register 0xF4
898 * value | Pressure oversampling
899 * --------------------|--------------------------
901 * 0x01 | BME280_OVERSAMP_1X
902 * 0x02 | BME280_OVERSAMP_2X
903 * 0x03 | BME280_OVERSAMP_4X
904 * 0x04 | BME280_OVERSAMP_8X
905 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
908 * @param v_value_u8 : The value of pressure oversampling
912 * @return results of bus communication function
913 * @retval 0 -> Success
914 * @retval -1 -> Error
918 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
921 /* used to return the communication result*/
922 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
923 u8 v_data_u8 = BME280_INIT_VALUE;
924 /* check the p_bme280 structure pointer as NULL*/
925 if (p_bme280 == BME280_NULL) {
926 return E_BME280_NULL_PTR;
928 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
930 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
931 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
932 *v_value_u8 = BME280_GET_BITSLICE(
934 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
936 p_bme280->oversamp_pressure = *v_value_u8;
941 * @brief This API is used to set
942 * the pressure oversampling setting in the register 0xF4
945 * value | Pressure oversampling
946 * --------------------|--------------------------
948 * 0x01 | BME280_OVERSAMP_1X
949 * 0x02 | BME280_OVERSAMP_2X
950 * 0x03 | BME280_OVERSAMP_4X
951 * 0x04 | BME280_OVERSAMP_8X
952 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
955 * @param v_value_u8 : The value of pressure oversampling
959 * @return results of bus communication function
960 * @retval 0 -> Success
961 * @retval -1 -> Error
965 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
968 /* used to return the communication result*/
969 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
970 u8 v_data_u8 = BME280_INIT_VALUE;
971 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
972 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
973 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
974 /* check the p_bme280 structure pointer as NULL*/
975 if (p_bme280 == BME280_NULL) {
976 return E_BME280_NULL_PTR;
978 v_data_u8 = p_bme280->ctrl_meas_reg;
980 BME280_SET_BITSLICE(v_data_u8,
981 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
982 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
983 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
984 com_rslt += bme280_set_soft_rst();
985 p_bme280->delay_msec(BME280_3MS_DELAY);
986 /* write previous value of
987 configuration register*/
988 v_pre_config_value_u8 = p_bme280->config_reg;
989 com_rslt = bme280_write_register(
991 &v_pre_config_value_u8,
992 BME280_GEN_READ_WRITE_DATA_LENGTH);
993 /* write previous value of
994 humidity oversampling*/
995 v_pre_ctrl_hum_value_u8 =
996 p_bme280->ctrl_hum_reg;
997 com_rslt += bme280_write_register(
998 BME280_CTRL_HUMIDITY_REG,
999 &v_pre_ctrl_hum_value_u8,
1000 BME280_GEN_READ_WRITE_DATA_LENGTH);
1001 /* write previous and updated value of
1002 control measurement register*/
1003 bme280_write_register(
1004 BME280_CTRL_MEAS_REG,
1005 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1007 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1009 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
1010 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1012 p_bme280->oversamp_pressure = v_value_u8;
1013 /* read the control measurement register value*/
1014 com_rslt = bme280_read_register(
1015 BME280_CTRL_MEAS_REG,
1016 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1017 p_bme280->ctrl_meas_reg = v_data_u8;
1018 /* read the control humidity register value*/
1019 com_rslt += bme280_read_register(
1020 BME280_CTRL_HUMIDITY_REG,
1021 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1022 p_bme280->ctrl_hum_reg = v_data_u8;
1024 configuration register value*/
1025 com_rslt += bme280_read_register(
1027 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1028 p_bme280->config_reg = v_data_u8;
1033 * @brief This API is used to get
1034 * the humidity oversampling setting in the register 0xF2
1037 * value | Humidity oversampling
1038 * ---------------------|-------------------------
1040 * 0x01 | BME280_OVERSAMP_1X
1041 * 0x02 | BME280_OVERSAMP_2X
1042 * 0x03 | BME280_OVERSAMP_4X
1043 * 0x04 | BME280_OVERSAMP_8X
1044 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1047 * @param v_value_u8 : The value of humidity over sampling
1051 * @return results of bus communication function
1052 * @retval 0 -> Success
1053 * @retval -1 -> Error
1057 BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
1060 /* used to return the communication result*/
1061 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1062 u8 v_data_u8 = BME280_INIT_VALUE;
1063 /* check the p_bme280 structure pointer as NULL*/
1064 if (p_bme280 == BME280_NULL) {
1065 return E_BME280_NULL_PTR;
1067 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1069 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1070 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1071 *v_value_u8 = BME280_GET_BITSLICE(
1073 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY);
1075 p_bme280->oversamp_humidity = *v_value_u8;
1080 * @brief This API is used to set
1081 * the humidity oversampling setting in the register 0xF2
1084 * value | Humidity oversampling
1085 * ---------------------|-------------------------
1087 * 0x01 | BME280_OVERSAMP_1X
1088 * 0x02 | BME280_OVERSAMP_2X
1089 * 0x03 | BME280_OVERSAMP_4X
1090 * 0x04 | BME280_OVERSAMP_8X
1091 * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
1094 * @param v_value_u8 : The value of humidity over sampling
1098 * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1099 * register sets the humidity
1100 * data acquisition options of the device.
1101 * @note changes to this registers only become
1102 * effective after a write operation to
1103 * "BME280_CTRL_MEAS_REG" register.
1104 * @note In the code automated reading and writing of
1105 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1106 * @note register first set the
1107 * "BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY"
1108 * and then read and write
1109 * the "BME280_CTRL_MEAS_REG" register in the function.
1112 * @return results of bus communication function
1113 * @retval 0 -> Success
1114 * @retval -1 -> Error
1118 BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
1121 /* used to return the communication result*/
1122 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1123 u8 v_data_u8 = BME280_INIT_VALUE;
1124 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1125 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1126 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1127 /* check the p_bme280 structure pointer as NULL*/
1128 if (p_bme280 == BME280_NULL) {
1129 return E_BME280_NULL_PTR;
1131 /* write humidity oversampling*/
1132 v_data_u8 = p_bme280->ctrl_hum_reg;
1134 BME280_SET_BITSLICE(v_data_u8,
1135 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY, v_value_u8);
1136 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1137 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1138 com_rslt += bme280_set_soft_rst();
1139 p_bme280->delay_msec(BME280_3MS_DELAY);
1140 /* write previous value of
1141 configuration register*/
1142 v_pre_config_value_u8 = p_bme280->config_reg;
1143 com_rslt += bme280_write_register(
1145 &v_pre_config_value_u8,
1146 BME280_GEN_READ_WRITE_DATA_LENGTH);
1147 /* write the value of control humidity*/
1148 com_rslt += bme280_write_register(
1149 BME280_CTRL_HUMIDITY_REG,
1150 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1151 /* write previous value of
1152 control measurement register*/
1153 pre_ctrl_meas_value =
1154 p_bme280->ctrl_meas_reg;
1155 com_rslt += bme280_write_register(
1156 BME280_CTRL_MEAS_REG,
1157 &pre_ctrl_meas_value,
1158 BME280_GEN_READ_WRITE_DATA_LENGTH);
1161 p_bme280->BME280_BUS_WRITE_FUNC(
1163 BME280_CTRL_HUMIDITY_REG_OVERSAMP_HUMIDITY__REG,
1164 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1165 /* Control humidity write will effective only
1166 after the control measurement register*/
1167 pre_ctrl_meas_value =
1168 p_bme280->ctrl_meas_reg;
1169 com_rslt += bme280_write_register(
1170 BME280_CTRL_MEAS_REG,
1171 &pre_ctrl_meas_value,
1172 BME280_GEN_READ_WRITE_DATA_LENGTH);
1174 p_bme280->oversamp_humidity = v_value_u8;
1175 /* read the control measurement register value*/
1176 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1177 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1178 p_bme280->ctrl_meas_reg = v_data_u8;
1179 /* read the control humidity register value*/
1180 com_rslt += bme280_read_register(
1181 BME280_CTRL_HUMIDITY_REG,
1182 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1183 p_bme280->ctrl_hum_reg = v_data_u8;
1184 /* read the control configuration register value*/
1185 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1186 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1187 p_bme280->config_reg = v_data_u8;
1192 * @brief This API used to get the
1193 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1197 * @param v_power_mode_u8 : The value of power mode
1199 * -----------------|------------------
1200 * 0x00 | BME280_SLEEP_MODE
1201 * 0x01 and 0x02 | BME280_FORCED_MODE
1202 * 0x03 | BME280_NORMAL_MODE
1204 * @return results of bus communication function
1205 * @retval 0 -> Success
1206 * @retval -1 -> Error
1210 BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
1212 /* used to return the communication result*/
1213 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1214 u8 v_mode_u8r = BME280_INIT_VALUE;
1215 /* check the p_bme280 structure pointer as NULL*/
1216 if (p_bme280 == BME280_NULL) {
1217 return E_BME280_NULL_PTR;
1219 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1221 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1222 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1223 *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
1224 BME280_CTRL_MEAS_REG_POWER_MODE);
1229 * @brief This API used to set the
1230 * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
1234 * @param v_power_mode_u8 : The value of power mode
1236 * -----------------|------------------
1237 * 0x00 | BME280_SLEEP_MODE
1238 * 0x01 and 0x02 | BME280_FORCED_MODE
1239 * 0x03 | BME280_NORMAL_MODE
1241 * @return results of bus communication function
1242 * @retval 0 -> Success
1243 * @retval -1 -> Error
1247 BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
1249 /* used to return the communication result*/
1250 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1251 u8 v_mode_u8r = BME280_INIT_VALUE;
1252 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1253 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1254 u8 v_pre_config_value_u8 = BME280_INIT_VALUE;
1255 u8 v_data_u8 = BME280_INIT_VALUE;
1256 /* check the p_bme280 structure pointer as NULL*/
1257 if (p_bme280 == BME280_NULL) {
1258 return E_BME280_NULL_PTR;
1260 if (v_power_mode_u8 <= BME280_NORMAL_MODE) {
1261 v_mode_u8r = p_bme280->ctrl_meas_reg;
1263 BME280_SET_BITSLICE(v_mode_u8r,
1264 BME280_CTRL_MEAS_REG_POWER_MODE,
1266 com_rslt = bme280_get_power_mode(
1267 &v_prev_pow_mode_u8);
1268 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1269 com_rslt += bme280_set_soft_rst();
1270 p_bme280->delay_msec(BME280_3MS_DELAY);
1271 /* write previous value of
1272 configuration register*/
1273 v_pre_config_value_u8 =
1274 p_bme280->config_reg;
1275 com_rslt = bme280_write_register(
1277 &v_pre_config_value_u8,
1278 BME280_GEN_READ_WRITE_DATA_LENGTH);
1279 /* write previous value of
1280 humidity oversampling*/
1281 v_pre_ctrl_hum_value_u8 =
1282 p_bme280->ctrl_hum_reg;
1283 com_rslt += bme280_write_register(
1284 BME280_CTRL_HUMIDITY_REG,
1285 &v_pre_ctrl_hum_value_u8,
1286 BME280_GEN_READ_WRITE_DATA_LENGTH);
1287 /* write previous and updated value of
1288 control measurement register*/
1289 com_rslt += bme280_write_register(
1290 BME280_CTRL_MEAS_REG,
1292 BME280_GEN_READ_WRITE_DATA_LENGTH);
1295 p_bme280->BME280_BUS_WRITE_FUNC(
1297 BME280_CTRL_MEAS_REG_POWER_MODE__REG,
1299 BME280_GEN_READ_WRITE_DATA_LENGTH);
1301 /* read the control measurement register value*/
1302 com_rslt = bme280_read_register(
1303 BME280_CTRL_MEAS_REG,
1304 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1305 p_bme280->ctrl_meas_reg = v_data_u8;
1306 /* read the control humidity register value*/
1307 com_rslt += bme280_read_register(
1308 BME280_CTRL_HUMIDITY_REG,
1309 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1310 p_bme280->ctrl_hum_reg = v_data_u8;
1311 /* read the config register value*/
1312 com_rslt += bme280_read_register(
1314 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1315 p_bme280->config_reg = v_data_u8;
1317 com_rslt = E_BME280_OUT_OF_RANGE;
1323 * @brief Used to reset the sensor
1324 * The value 0xB6 is written to the 0xE0
1325 * register the device is reset using the
1326 * complete power-on-reset procedure.
1327 * @note Soft reset can be easily set using bme280_set_softreset().
1328 * @note Usage Hint : bme280_set_softreset()
1331 * @return results of bus communication function
1332 * @retval 0 -> Success
1333 * @retval -1 -> Error
1337 BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst(void)
1339 /* used to return the communication result*/
1340 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1341 u8 v_data_u8 = BME280_SOFT_RESET_CODE;
1342 /* check the p_bme280 structure pointer as NULL*/
1343 if (p_bme280 == BME280_NULL) {
1344 return E_BME280_NULL_PTR;
1346 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1348 BME280_RST_REG, &v_data_u8,
1349 BME280_GEN_READ_WRITE_DATA_LENGTH);
1354 * @brief This API used to get the sensor
1355 * SPI mode(communication type) in the register 0xF5 bit 0
1359 * @param v_enable_disable_u8 : The value of SPI enable
1360 * value | Description
1361 * --------|--------------
1367 * @return results of bus communication function
1368 * @retval 0 -> Success
1369 * @retval -1 -> Error
1373 BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
1375 /* used to return the communication result*/
1376 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1377 u8 v_data_u8 = BME280_INIT_VALUE;
1378 /* check the p_bme280 structure pointer as NULL*/
1379 if (p_bme280 == BME280_NULL) {
1380 return E_BME280_NULL_PTR;
1382 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1384 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1385 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1386 *v_enable_disable_u8 = BME280_GET_BITSLICE(
1388 BME280_CONFIG_REG_SPI3_ENABLE);
1393 * @brief This API used to set the sensor
1394 * SPI mode(communication type) in the register 0xF5 bit 0
1398 * @param v_enable_disable_u8 : The value of SPI enable
1399 * value | Description
1400 * --------|--------------
1406 * @return results of bus communication function
1407 * @retval 0 -> Success
1408 * @retval -1 -> Error
1412 BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
1414 /* used to return the communication result*/
1415 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1416 u8 v_data_u8 = BME280_INIT_VALUE;
1417 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1418 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1419 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1420 /* check the p_bme280 structure pointer as NULL*/
1421 if (p_bme280 == BME280_NULL) {
1422 return E_BME280_NULL_PTR;
1424 v_data_u8 = p_bme280->config_reg;
1426 BME280_SET_BITSLICE(v_data_u8,
1427 BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
1428 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1429 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1430 com_rslt += bme280_set_soft_rst();
1431 p_bme280->delay_msec(BME280_3MS_DELAY);
1432 /* write previous and updated value of
1433 configuration register*/
1434 com_rslt += bme280_write_register(
1436 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1437 /* write previous value of
1438 humidity oversampling*/
1439 v_pre_ctrl_hum_value_u8 =
1440 p_bme280->ctrl_hum_reg;
1441 com_rslt += bme280_write_register(
1442 BME280_CTRL_HUMIDITY_REG,
1443 &v_pre_ctrl_hum_value_u8,
1444 BME280_GEN_READ_WRITE_DATA_LENGTH);
1445 /* write previous value of
1446 control measurement register*/
1447 pre_ctrl_meas_value =
1448 p_bme280->ctrl_meas_reg;
1449 com_rslt += bme280_write_register(
1450 BME280_CTRL_MEAS_REG,
1451 &pre_ctrl_meas_value,
1452 BME280_GEN_READ_WRITE_DATA_LENGTH);
1455 p_bme280->BME280_BUS_WRITE_FUNC(
1457 BME280_CONFIG_REG_SPI3_ENABLE__REG,
1458 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1460 /* read the control measurement register value*/
1461 com_rslt += bme280_read_register(
1462 BME280_CTRL_MEAS_REG,
1463 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1464 p_bme280->ctrl_meas_reg = v_data_u8;
1465 /* read the control humidity register value*/
1466 com_rslt += bme280_read_register(
1467 BME280_CTRL_HUMIDITY_REG,
1468 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1469 p_bme280->ctrl_hum_reg = v_data_u8;
1470 /* read the control configuration register value*/
1471 com_rslt += bme280_read_register(
1473 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1474 p_bme280->config_reg = v_data_u8;
1479 * @brief This API is used to reads filter setting
1480 * in the register 0xF5 bit 3 and 4
1484 * @param v_value_u8 : The value of IIR filter coefficient
1486 * value | Filter coefficient
1487 * -------------|-------------------------
1488 * 0x00 | BME280_FILTER_COEFF_OFF
1489 * 0x01 | BME280_FILTER_COEFF_2
1490 * 0x02 | BME280_FILTER_COEFF_4
1491 * 0x03 | BME280_FILTER_COEFF_8
1492 * 0x04 | BME280_FILTER_COEFF_16
1494 * @return results of bus communication function
1495 * @retval 0 -> Success
1496 * @retval -1 -> Error
1500 BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
1502 /* used to return the communication result*/
1503 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1504 u8 v_data_u8 = BME280_INIT_VALUE;
1505 /* check the p_bme280 structure pointer as NULL*/
1506 if (p_bme280 == BME280_NULL) {
1507 return E_BME280_NULL_PTR;
1509 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1511 BME280_CONFIG_REG_FILTER__REG,
1512 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1513 *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
1514 BME280_CONFIG_REG_FILTER);
1519 * @brief This API is used to write filter setting
1520 * in the register 0xF5 bit 3 and 4
1524 * @param v_value_u8 : The value of IIR filter coefficient
1526 * value | Filter coefficient
1527 * -------------|-------------------------
1528 * 0x00 | BME280_FILTER_COEFF_OFF
1529 * 0x01 | BME280_FILTER_COEFF_2
1530 * 0x02 | BME280_FILTER_COEFF_4
1531 * 0x03 | BME280_FILTER_COEFF_8
1532 * 0x04 | BME280_FILTER_COEFF_16
1534 * @return results of bus communication function
1535 * @retval 0 -> Success
1536 * @retval -1 -> Error
1540 BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
1542 /* used to return the communication result*/
1543 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1544 u8 v_data_u8 = BME280_INIT_VALUE;
1545 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1546 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1547 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1548 /* check the p_bme280 structure pointer as NULL*/
1549 if (p_bme280 == BME280_NULL) {
1550 return E_BME280_NULL_PTR;
1552 v_data_u8 = p_bme280->config_reg;
1554 BME280_SET_BITSLICE(v_data_u8,
1555 BME280_CONFIG_REG_FILTER, v_value_u8);
1556 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1557 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1558 com_rslt += bme280_set_soft_rst();
1559 p_bme280->delay_msec(BME280_3MS_DELAY);
1560 /* write previous and updated value of
1561 configuration register*/
1562 com_rslt += bme280_write_register(
1564 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1565 /* write previous value of
1566 humidity oversampling*/
1567 v_pre_ctrl_hum_value_u8 =
1568 p_bme280->ctrl_hum_reg;
1569 com_rslt += bme280_write_register(
1570 BME280_CTRL_HUMIDITY_REG,
1571 &v_pre_ctrl_hum_value_u8,
1572 BME280_GEN_READ_WRITE_DATA_LENGTH);
1573 /* write previous value of
1574 control measurement register*/
1575 pre_ctrl_meas_value =
1576 p_bme280->ctrl_meas_reg;
1577 com_rslt += bme280_write_register(
1578 BME280_CTRL_MEAS_REG,
1579 &pre_ctrl_meas_value,
1580 BME280_GEN_READ_WRITE_DATA_LENGTH);
1583 p_bme280->BME280_BUS_WRITE_FUNC(
1585 BME280_CONFIG_REG_FILTER__REG,
1586 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1588 /* read the control measurement register value*/
1589 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1590 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1591 p_bme280->ctrl_meas_reg = v_data_u8;
1592 /* read the control humidity register value*/
1593 com_rslt += bme280_read_register(
1594 BME280_CTRL_HUMIDITY_REG,
1595 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1596 p_bme280->ctrl_hum_reg = v_data_u8;
1597 /* read the configuration register value*/
1598 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1599 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1600 p_bme280->config_reg = v_data_u8;
1605 * @brief This API used to Read the
1606 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1608 * @param v_standby_durn_u8 : The value of standby duration time value.
1609 * value | standby duration
1610 * -------------|-----------------------
1611 * 0x00 | BME280_STANDBY_TIME_1_MS
1612 * 0x01 | BME280_STANDBY_TIME_63_MS
1613 * 0x02 | BME280_STANDBY_TIME_125_MS
1614 * 0x03 | BME280_STANDBY_TIME_250_MS
1615 * 0x04 | BME280_STANDBY_TIME_500_MS
1616 * 0x05 | BME280_STANDBY_TIME_1000_MS
1617 * 0x06 | BME280_STANDBY_TIME_2000_MS
1618 * 0x07 | BME280_STANDBY_TIME_4000_MS
1621 * @return results of bus communication function
1622 * @retval 0 -> Success
1623 * @retval -1 -> Error
1627 BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
1629 /* used to return the communication result*/
1630 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1631 u8 v_data_u8 = BME280_INIT_VALUE;
1632 /* check the p_bme280 structure pointer as NULL*/
1633 if (p_bme280 == BME280_NULL) {
1634 return E_BME280_NULL_PTR;
1636 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1638 BME280_CONFIG_REG_TSB__REG,
1639 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1640 *v_standby_durn_u8 = BME280_GET_BITSLICE(
1641 v_data_u8, BME280_CONFIG_REG_TSB);
1646 * @brief This API used to write the
1647 * standby duration time from the sensor in the register 0xF5 bit 5 to 7
1649 * @param v_standby_durn_u8 : The value of standby duration time value.
1650 * value | standby duration
1651 * -------------|-----------------------
1652 * 0x00 | BME280_STANDBY_TIME_1_MS
1653 * 0x01 | BME280_STANDBY_TIME_63_MS
1654 * 0x02 | BME280_STANDBY_TIME_125_MS
1655 * 0x03 | BME280_STANDBY_TIME_250_MS
1656 * 0x04 | BME280_STANDBY_TIME_500_MS
1657 * 0x05 | BME280_STANDBY_TIME_1000_MS
1658 * 0x06 | BME280_STANDBY_TIME_2000_MS
1659 * 0x07 | BME280_STANDBY_TIME_4000_MS
1661 * @note Normal mode comprises an automated perpetual
1662 * cycling between an (active)
1663 * Measurement period and an (inactive) standby period.
1664 * @note The standby time is determined by
1665 * the contents of the register t_sb.
1666 * Standby time can be set using BME280_STANDBY_TIME_125_MS.
1668 * @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
1672 * @return results of bus communication function
1673 * @retval 0 -> Success
1674 * @retval -1 -> Error
1678 BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
1680 /* used to return the communication result*/
1681 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1682 u8 v_data_u8 = BME280_INIT_VALUE;
1683 u8 pre_ctrl_meas_value = BME280_INIT_VALUE;
1684 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1685 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1686 /* check the p_bme280 structure pointer as NULL*/
1687 if (p_bme280 == BME280_NULL) {
1688 return E_BME280_NULL_PTR;
1690 v_data_u8 = p_bme280->config_reg;
1692 BME280_SET_BITSLICE(v_data_u8,
1693 BME280_CONFIG_REG_TSB, v_standby_durn_u8);
1694 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1695 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1696 com_rslt += bme280_set_soft_rst();
1697 p_bme280->delay_msec(BME280_3MS_DELAY);
1698 /* write previous and updated value of
1699 configuration register*/
1700 com_rslt += bme280_write_register(
1702 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1703 /* write previous value of
1704 humidity oversampling*/
1705 v_pre_ctrl_hum_value_u8 =
1706 p_bme280->ctrl_hum_reg;
1707 com_rslt += bme280_write_register(
1708 BME280_CTRL_HUMIDITY_REG,
1709 &v_pre_ctrl_hum_value_u8,
1710 BME280_GEN_READ_WRITE_DATA_LENGTH);
1711 /* write previous value of control
1712 measurement register*/
1713 pre_ctrl_meas_value =
1714 p_bme280->ctrl_meas_reg;
1715 com_rslt += bme280_write_register(
1716 BME280_CTRL_MEAS_REG,
1717 &pre_ctrl_meas_value,
1718 BME280_GEN_READ_WRITE_DATA_LENGTH);
1721 p_bme280->BME280_BUS_WRITE_FUNC(
1723 BME280_CONFIG_REG_TSB__REG,
1724 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1726 /* read the control measurement register value*/
1727 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1728 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1729 p_bme280->ctrl_meas_reg = v_data_u8;
1730 /* read the control humidity register value*/
1731 com_rslt += bme280_read_register(
1732 BME280_CTRL_HUMIDITY_REG,
1733 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1734 p_bme280->ctrl_hum_reg = v_data_u8;
1735 /* read the configuration register value*/
1736 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1737 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1738 p_bme280->config_reg = v_data_u8;
1743 * @brief Writes the working mode to the sensor
1748 * @param v_work_mode_u8 : Mode to be set
1749 * value | Working mode
1750 * ----------|--------------------
1751 * 0 | BME280_ULTRALOWPOWER_MODE
1752 * 1 | BME280_LOWPOWER_MODE
1753 * 2 | BME280_STANDARDRESOLUTION_MODE
1754 * 3 | BME280_HIGHRESOLUTION_MODE
1755 * 4 | BME280_ULTRAHIGHRESOLUTION_MODE
1757 * @return results of bus communication function
1758 * @retval 0 -> Success
1759 * @retval -1 -> Error
1763 /*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
1765 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1766 u8 v_data_u8 = BME280_INIT_VALUE;
1767 if (p_bme280 == BME280_NULL) {
1768 return E_BME280_NULL_PTR;
1770 if (v_work_mode_u8 <= BME280_ULTRAHIGHRESOLUTION_MODE) {
1771 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1772 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1773 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1774 if (com_rslt == SUCCESS) {
1775 switch (v_work_mode_u8) {
1776 case BME280_ULTRALOWPOWER_MODE:
1777 p_bme280->oversamp_temperature =
1778 BME280_ULTRALOWPOWER_OSRS_T;
1780 BME280_ULTRALOWPOWER_OSRS_P;
1782 case BME280_LOWPOWER_MODE:
1783 p_bme280->oversamp_temperature =
1784 BME280_LOWPOWER_OSRS_T;
1785 p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
1787 case BME280_STANDARDRESOLUTION_MODE:
1788 p_bme280->oversamp_temperature =
1789 BME280_STANDARDRESOLUTION_OSRS_T;
1791 BME280_STANDARDRESOLUTION_OSRS_P;
1793 case BME280_HIGHRESOLUTION_MODE:
1794 p_bme280->oversamp_temperature =
1795 BME280_HIGHRESOLUTION_OSRS_T;
1796 p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
1798 case BME280_ULTRAHIGHRESOLUTION_MODE:
1799 p_bme280->oversamp_temperature =
1800 BME280_ULTRAHIGHRESOLUTION_OSRS_T;
1802 BME280_ULTRAHIGHRESOLUTION_OSRS_P;
1805 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1806 BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
1807 p_bme280->oversamp_temperature);
1808 v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
1809 BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
1811 com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
1812 p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
1813 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1816 com_rslt = E_BME280_OUT_OF_RANGE;
1822 * @brief This API used to read uncompensated
1823 * temperature,pressure and humidity in forced mode
1826 * @param v_uncom_pressure_s32: The value of uncompensated pressure
1827 * @param v_uncom_temperature_s32: The value of uncompensated temperature
1828 * @param v_uncom_humidity_s32: The value of uncompensated humidity
1831 * @return results of bus communication function
1832 * @retval 0 -> Success
1833 * @retval -1 -> Error
1837 BME280_RETURN_FUNCTION_TYPE
1838 bme280_get_forced_uncomp_pressure_temperature_humidity(
1839 s32 *v_uncom_pressure_s32,
1840 s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
1842 /* used to return the communication result*/
1843 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1844 u8 v_data_u8 = BME280_INIT_VALUE;
1845 u8 v_waittime_u8 = BME280_INIT_VALUE;
1846 u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE;
1847 u8 v_mode_u8r = BME280_INIT_VALUE;
1848 u8 pre_ctrl_config_value = BME280_INIT_VALUE;
1849 u8 v_pre_ctrl_hum_value_u8 = BME280_INIT_VALUE;
1850 /* check the p_bme280 structure pointer as NULL*/
1851 if (p_bme280 == BME280_NULL) {
1852 return E_BME280_NULL_PTR;
1854 v_mode_u8r = p_bme280->ctrl_meas_reg;
1856 BME280_SET_BITSLICE(v_mode_u8r,
1857 BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
1858 com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
1859 if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
1860 com_rslt += bme280_set_soft_rst();
1861 p_bme280->delay_msec(BME280_3MS_DELAY);
1862 /* write previous and updated value of
1863 configuration register*/
1864 pre_ctrl_config_value = p_bme280->config_reg;
1865 com_rslt += bme280_write_register(
1867 &pre_ctrl_config_value,
1868 BME280_GEN_READ_WRITE_DATA_LENGTH);
1869 /* write previous value of
1870 humidity oversampling*/
1871 v_pre_ctrl_hum_value_u8 =
1872 p_bme280->ctrl_hum_reg;
1873 com_rslt += bme280_write_register(
1874 BME280_CTRL_HUMIDITY_REG,
1875 &v_pre_ctrl_hum_value_u8,
1876 BME280_GEN_READ_WRITE_DATA_LENGTH);
1877 /* write the force mode */
1878 com_rslt += bme280_write_register(
1879 BME280_CTRL_MEAS_REG,
1880 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1882 /* write previous value of
1883 humidity oversampling*/
1884 v_pre_ctrl_hum_value_u8 =
1885 p_bme280->ctrl_hum_reg;
1886 com_rslt += bme280_write_register(
1887 BME280_CTRL_HUMIDITY_REG,
1888 &v_pre_ctrl_hum_value_u8,
1889 BME280_GEN_READ_WRITE_DATA_LENGTH);
1890 /* write the force mode */
1891 com_rslt += bme280_write_register(
1892 BME280_CTRL_MEAS_REG,
1893 &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH);
1895 bme280_compute_wait_time(&v_waittime_u8);
1896 p_bme280->delay_msec(v_waittime_u8);
1897 /* read the force-mode value of pressure
1898 temperature and humidity*/
1900 bme280_read_uncomp_pressure_temperature_humidity(
1901 v_uncom_pressure_s32, v_uncom_temperature_s32,
1902 v_uncom_humidity_s32);
1904 /* read the control humidity register value*/
1905 com_rslt += bme280_read_register(
1906 BME280_CTRL_HUMIDITY_REG,
1907 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1908 p_bme280->ctrl_hum_reg = v_data_u8;
1909 /* read the configuration register value*/
1910 com_rslt += bme280_read_register(BME280_CONFIG_REG,
1911 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1912 p_bme280->config_reg = v_data_u8;
1914 /* read the control measurement register value*/
1915 com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
1916 &v_data_u8, BME280_GEN_READ_WRITE_DATA_LENGTH);
1917 p_bme280->ctrl_meas_reg = v_data_u8;
1923 * This API write the data to
1924 * the given register
1927 * @param v_addr_u8 -> Address of the register
1928 * @param v_data_u8 -> The data from the register
1929 * @param v_len_u8 -> no of bytes to read
1932 * @return results of bus communication function
1933 * @retval 0 -> Success
1934 * @retval -1 -> Error
1938 BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
1939 u8 *v_data_u8, u8 v_len_u8)
1941 /* used to return the communication result*/
1942 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1943 /* check the p_bme280 structure pointer as NULL*/
1944 if (p_bme280 == BME280_NULL) {
1945 return E_BME280_NULL_PTR;
1947 com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
1949 v_addr_u8, v_data_u8, v_len_u8);
1955 * This API reads the data from
1956 * the given register
1959 * @param v_addr_u8 -> Address of the register
1960 * @param v_data_u8 -> The data from the register
1961 * @param v_len_u8 -> no of bytes to read
1964 * @return results of bus communication function
1965 * @retval 0 -> Success
1966 * @retval -1 -> Error
1970 BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
1971 u8 *v_data_u8, u8 v_len_u8)
1973 /* used to return the communication result*/
1974 BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
1975 /* check the p_bme280 structure pointer as NULL*/
1976 if (p_bme280 == BME280_NULL) {
1977 return E_BME280_NULL_PTR;
1979 com_rslt = p_bme280->BME280_BUS_READ_FUNC(
1981 v_addr_u8, v_data_u8, v_len_u8);
1985 #ifdef BME280_ENABLE_FLOAT
1987 * @brief Reads actual temperature from uncompensated temperature
1988 * @note returns the value in Degree centigrade
1989 * @note Output value of "51.23" equals 51.23 DegC.
1993 * @param v_uncom_temperature_s32 : value of uncompensated temperature
1997 * @return Return the actual temperature in floating point
2000 double bme280_compensate_temperature_double(s32 v_uncom_temperature_s32)
2002 double v_x1_u32 = BME280_INIT_VALUE;
2003 double v_x2_u32 = BME280_INIT_VALUE;
2004 double temperature = BME280_INIT_VALUE;
2006 v_x1_u32 = (((double)v_uncom_temperature_s32) / 16384.0 -
2007 ((double)p_bme280->cal_param.dig_T1) / 1024.0) *
2008 ((double)p_bme280->cal_param.dig_T2);
2009 v_x2_u32 = ((((double)v_uncom_temperature_s32) / 131072.0 -
2010 ((double)p_bme280->cal_param.dig_T1) / 8192.0) *
2011 (((double)v_uncom_temperature_s32) / 131072.0 -
2012 ((double)p_bme280->cal_param.dig_T1) / 8192.0)) *
2013 ((double)p_bme280->cal_param.dig_T3);
2014 p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
2015 temperature = (v_x1_u32 + v_x2_u32) / 5120.0;
2021 * @brief Reads actual pressure from uncompensated pressure
2022 * @note Returns pressure in Pa as double.
2023 * @note Output value of "96386.2"
2024 * equals 96386.2 Pa = 963.862 hPa.
2027 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2030 * @return Return the actual pressure in floating point
2033 double bme280_compensate_pressure_double(s32 v_uncom_pressure_s32)
2035 double v_x1_u32 = BME280_INIT_VALUE;
2036 double v_x2_u32 = BME280_INIT_VALUE;
2037 double pressure = BME280_INIT_VALUE;
2039 v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
2041 v_x2_u32 = v_x1_u32 * v_x1_u32 *
2042 ((double)p_bme280->cal_param.dig_P6) / 32768.0;
2043 v_x2_u32 = v_x2_u32 + v_x1_u32 *
2044 ((double)p_bme280->cal_param.dig_P5) * 2.0;
2045 v_x2_u32 = (v_x2_u32 / 4.0) +
2046 (((double)p_bme280->cal_param.dig_P4) * 65536.0);
2047 v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
2048 v_x1_u32 * v_x1_u32 / 524288.0 +
2049 ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) / 524288.0;
2050 v_x1_u32 = (1.0 + v_x1_u32 / 32768.0) *
2051 ((double)p_bme280->cal_param.dig_P1);
2052 pressure = 1048576.0 - (double)v_uncom_pressure_s32;
2053 /* Avoid exception caused by division by zero */
2054 if ((v_x1_u32 > 0) || (v_x1_u32 < 0))
2055 pressure = (pressure - (v_x2_u32 / 4096.0)) * 6250.0 / v_x1_u32;
2057 return BME280_INVALID_DATA;
2058 v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
2059 pressure * pressure / 2147483648.0;
2060 v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8) / 32768.0;
2061 pressure = pressure + (v_x1_u32 + v_x2_u32 +
2062 ((double)p_bme280->cal_param.dig_P7)) / 16.0;
2067 * @brief Reads actual humidity from uncompensated humidity
2068 * @note returns the value in relative humidity (%rH)
2069 * @note Output value of "42.12" equals 42.12 %rH
2071 * @param v_uncom_humidity_s32 : value of uncompensated humidity
2075 * @return Return the actual humidity in floating point
2078 double bme280_compensate_humidity_double(s32 v_uncom_humidity_s32)
2080 double var_h = BME280_INIT_VALUE;
2082 var_h = (((double)p_bme280->cal_param.t_fine) - 76800.0);
2083 if ((var_h > 0) || (var_h < 0))
2084 var_h = (v_uncom_humidity_s32 -
2085 (((double)p_bme280->cal_param.dig_H4) * 64.0 +
2086 ((double)p_bme280->cal_param.dig_H5) / 16384.0 * var_h))*
2087 (((double)p_bme280->cal_param.dig_H2) / 65536.0 *
2088 (1.0 + ((double) p_bme280->cal_param.dig_H6)
2089 / 67108864.0 * var_h * (1.0 + ((double)
2090 p_bme280->cal_param.dig_H3) / 67108864.0 * var_h)));
2092 return BME280_INVALID_DATA;
2093 var_h = var_h * (1.0 - ((double)
2094 p_bme280->cal_param.dig_H1)*var_h / 524288.0);
2097 else if (var_h < 0.0)
2103 #if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
2105 * @brief Reads actual pressure from uncompensated pressure
2106 * @note Returns the value in Pa as unsigned 32 bit
2107 * integer in Q24.8 format (24 integer bits and
2108 * 8 fractional bits).
2109 * @note Output value of "24674867"
2110 * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
2114 * @param v_uncom_pressure_s32 : value of uncompensated temperature
2117 * @return Return the actual pressure in u32
2120 u32 bme280_compensate_pressure_int64(s32 v_uncom_pressure_s32)
2122 s64 v_x1_s64r = BME280_INIT_VALUE;
2123 s64 v_x2_s64r = BME280_INIT_VALUE;
2124 s64 pressure = BME280_INIT_VALUE;
2126 v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
2128 v_x2_s64r = v_x1_s64r * v_x1_s64r *
2129 (s64)p_bme280->cal_param.dig_P6;
2130 v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
2131 (s64)p_bme280->cal_param.dig_P5)
2132 << BME280_SHIFT_BIT_POSITION_BY_17_BITS);
2133 v_x2_s64r = v_x2_s64r +
2134 (((s64)p_bme280->cal_param.dig_P4)
2135 << BME280_SHIFT_BIT_POSITION_BY_35_BITS);
2136 v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
2137 (s64)p_bme280->cal_param.dig_P3)
2138 >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2139 ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
2140 << BME280_SHIFT_BIT_POSITION_BY_12_BITS);
2141 v_x1_s64r = (((((s64)1)
2142 << BME280_SHIFT_BIT_POSITION_BY_47_BITS) + v_x1_s64r)) *
2143 ((s64)p_bme280->cal_param.dig_P1)
2144 >> BME280_SHIFT_BIT_POSITION_BY_33_BITS;
2145 pressure = 1048576 - v_uncom_pressure_s32;
2146 /* Avoid exception caused by division by zero */
2147 if (v_x1_s64r != BME280_INIT_VALUE)
2148 #if defined __KERNEL__
2149 pressure = div64_s64((((pressure
2150 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2151 * 3125), v_x1_s64r);
2153 pressure = (((pressure
2154 << BME280_SHIFT_BIT_POSITION_BY_31_BITS) - v_x2_s64r)
2155 * 3125) / v_x1_s64r;
2158 return BME280_INVALID_DATA;
2159 v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
2160 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS) *
2161 (pressure >> BME280_SHIFT_BIT_POSITION_BY_13_BITS))
2162 >> BME280_SHIFT_BIT_POSITION_BY_25_BITS;
2163 v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
2164 pressure) >> BME280_SHIFT_BIT_POSITION_BY_19_BITS;
2165 pressure = (((pressure + v_x1_s64r +
2166 v_x2_s64r) >> BME280_SHIFT_BIT_POSITION_BY_08_BITS) +
2167 (((s64)p_bme280->cal_param.dig_P7)
2168 << BME280_SHIFT_BIT_POSITION_BY_04_BITS));
2170 return (u32)pressure;
2173 * @brief Reads actual pressure from uncompensated pressure
2174 * @note Returns the value in Pa.
2175 * @note Output value of "12337434"
2176 * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
2180 * @param v_uncom_pressure_s32 : value of uncompensated pressure
2183 * @return the actual pressure in u32
2186 u32 bme280_compensate_pressure_int64_twentyfour_bit_output(
2187 s32 v_uncom_pressure_s32)
2189 u32 pressure = BME280_INIT_VALUE;
2191 pressure = bme280_compensate_pressure_int64(
2192 v_uncom_pressure_s32);
2193 pressure = (u32)(pressure >> BME280_SHIFT_BIT_POSITION_BY_01_BIT);
2198 * @brief Computing waiting time for sensor data read
2203 * @param v_delaytime_u8 : The value of delay time for force mode
2206 * @retval 0 -> Success
2210 BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
2213 /* used to return the communication result*/
2214 BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
2216 *v_delaytime_u8 = (T_INIT_MAX +
2217 T_MEASURE_PER_OSRS_MAX *
2219 p_bme280->oversamp_temperature)
2220 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT)
2221 + ((1 << p_bme280->oversamp_pressure)
2222 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) +
2223 ((1 << p_bme280->oversamp_humidity)
2224 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT))
2225 + ((p_bme280->oversamp_pressure > 0) ?
2226 T_SETUP_PRESSURE_MAX : 0) +
2227 ((p_bme280->oversamp_humidity > 0) ?
2228 T_SETUP_HUMIDITY_MAX : 0) + 15) / 16;