-/*
-****************************************************************************
-* Copyright (C) 2013 - 2014 Bosch Sensortec GmbH
-*
-* bme280.c
-* Date: 2014/12/12
-* Revision: 2.0.3(Pressure and Temperature compensation code revision is 1.1
-* and Humidity compensation code revision is 1.0)
-*
-* Usage: Sensor Driver file for BME280 sensor
-*
-****************************************************************************
-* License:
-*
-* Redistribution and use in source and binary forms, with or without
-* modification, are permitted provided that the following conditions are met:
-*
-* Redistributions of source code must retain the above copyright
-* notice, this list of conditions and the following disclaimer.
-*
-* Redistributions in binary form must reproduce the above copyright
-* notice, this list of conditions and the following disclaimer in the
-* documentation and/or other materials provided with the distribution.
-*
-* Neither the name of the copyright holder nor the names of the
-* contributors may be used to endorse or promote products derived from
-* this software without specific prior written permission.
-*
-* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
-* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
-* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
-* OR CONTRIBUTORS BE LIABLE FOR ANY
-* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
-* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
-* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-* ANY WAY OUT OF THE USE OF THIS
-* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
-*
-* The information provided is believed to be accurate and reliable.
-* The copyright holder assumes no responsibility
-* for the consequences of use
-* of such information nor for any infringement of patents or
-* other rights of third parties which may result from its use.
-* No license is granted by implication or otherwise under any patent or
-* patent rights of the copyright holder.
-**************************************************************************/
-
-#include "bme280.h"
-static struct bme280_t *p_bme280; /**< pointer to BME280 */
-
-/*!
- * @brief This function is used for initialize
- * the bus read and bus write functions
- * and assign the chip id and I2C address of the BME280 sensor
- * chip id is read in the register 0xD0 bit from 0 to 7
- *
- * @param bme280 structure pointer.
- *
- * @note While changing the parameter of the bme280_t
- * @note consider the following point:
- * Changing the reference value of the parameter
- * will changes the local copy or local reference
- * make sure your changes will not
- * affect the reference value of the parameter
- * (Better case don't change the reference value of the parameter)
- *
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- p_bme280 = bme280;
- /* assign BME280 ptr */
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr,
- BME280_CHIP_ID_REG, &v_data_u8, BME280_ONE_U8X);
- /* read Chip Id */
- p_bme280->chip_id = v_data_u8;
-
- bme280_get_calib_param();
- /* readout bme280 calibparam structure */
- return com_rslt;
-}
-/*!
- * @brief This API is used to read uncompensated temperature
- * in the registers 0xFA, 0xFB and 0xFC
- * @note 0xFA -> MSB -> bit from 0 to 7
- * @note 0xFB -> LSB -> bit from 0 to 7
- * @note 0xFC -> LSB -> bit from 4 to 7
- *
- * @param v_uncomp_temperature_s32 : The value of uncompensated temperature
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_temperature(
-s32 *v_uncomp_temperature_s32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* Array holding the MSB and LSb value
- a_data_u8r[0] - Temperature MSB
- a_data_u8r[1] - Temperature LSB
- a_data_u8r[2] - Temperature LSB
- */
- u8 a_data_u8r[ARRAY_SIZE_THREE] = {
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_TEMPERATURE_MSB_REG,
- a_data_u8r, BME280_THREE_U8X);
- *v_uncomp_temperature_s32 = (s32)(((
- (u32) (a_data_u8r[INDEX_ZERO]))
- << SHIFT_LEFT_12_POSITION) |
- (((u32)(a_data_u8r[INDEX_ONE]))
- << SHIFT_LEFT_4_POSITION)
- | ((u32)a_data_u8r[INDEX_TWO] >>
- SHIFT_RIGHT_4_POSITION));
- }
- return com_rslt;
-}
-/*!
- * @brief Reads actual temperature from uncompensated temperature
- * @note Returns the value in 0.01 degree Centigrade
- * Output value of "5123" equals 51.23 DegC.
- *
- *
- *
- * @param v_uncomp_temperature_s32 : value of uncompensated temperature
- *
- *
- * @return Returns the actual temperature
- *
-*/
-s32 bme280_compensate_T_int32(s32 v_uncomp_temperature_s32)
-{
- s32 v_x1_u32r = BME280_ZERO_U8X;
- s32 v_x2_u32r = BME280_ZERO_U8X;
- s32 temperature = BME280_ZERO_U8X;
-
- v_x1_u32r = ((((v_uncomp_temperature_s32
- >> SHIFT_RIGHT_3_POSITION) - ((s32)
- p_bme280->cal_param.dig_T1 << SHIFT_LEFT_1_POSITION))) *
- ((s32)p_bme280->cal_param.dig_T2))
- >> SHIFT_RIGHT_11_POSITION;
- v_x2_u32r = (((((v_uncomp_temperature_s32
- >> SHIFT_RIGHT_4_POSITION) -
- ((s32)p_bme280->cal_param.dig_T1))
- * ((v_uncomp_temperature_s32 >> SHIFT_RIGHT_4_POSITION) -
- ((s32)p_bme280->cal_param.dig_T1)))
- >> SHIFT_RIGHT_12_POSITION) *
- ((s32)p_bme280->cal_param.dig_T3))
- >> SHIFT_RIGHT_14_POSITION;
- p_bme280->cal_param.t_fine = v_x1_u32r + v_x2_u32r;
- temperature = (p_bme280->cal_param.t_fine
- * BME280_FIVE_U8X + BME280_ONE_TWENTY_EIGHT_U8X)
- >> SHIFT_RIGHT_8_POSITION;
- return temperature;
-}
-/*!
- * @brief Reads actual temperature from uncompensated temperature
- * @note Returns the value with 500LSB/DegC centred around 24 DegC
- * output value of "5123" equals(5123/500)+24 = 34.246DegC
- *
- *
- * @param v_uncomp_temperature_s32: value of uncompensated temperature
- *
- *
- *
- * @return Return the actual temperature as s16 output
- *
-*/
-s16 bme280_compensate_T_int32_sixteen_bit_output(
-s32 v_uncomp_temperature_s32)
-{
- s16 temperature = BME280_ZERO_U8X;
- bme280_compensate_T_int32(v_uncomp_temperature_s32);
- temperature = (s16)((((
- p_bme280->cal_param.t_fine
- - BME280_TEMP_1_2_2_8_8_0_DATA)
- * BME280_TWENTY_FIVE_U8X)
- + BME280_ONE_TWENTY_EIGHT_U8X)
- >> SHIFT_RIGHT_8_POSITION);
-
- return temperature;
-}
-/*!
- * @brief This API is used to read uncompensated pressure.
- * in the registers 0xF7, 0xF8 and 0xF9
- * @note 0xF7 -> MSB -> bit from 0 to 7
- * @note 0xF8 -> LSB -> bit from 0 to 7
- * @note 0xF9 -> LSB -> bit from 4 to 7
- *
- *
- *
- * @param v_uncomp_pressure_s32 : The value of uncompensated pressure
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure(
-s32 *v_uncomp_pressure_s32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* Array holding the MSB and LSb value
- a_data_u8[0] - Pressure MSB
- a_data_u8[1] - Pressure LSB
- a_data_u8[2] - Pressure LSB
- */
- u8 a_data_u8[ARRAY_SIZE_THREE] = {
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_PRESSURE_MSB_REG,
- a_data_u8, BME280_THREE_U8X);
- *v_uncomp_pressure_s32 = (s32)((
- ((u32)(a_data_u8[INDEX_ZERO]))
- << SHIFT_LEFT_12_POSITION) |
- (((u32)(a_data_u8[INDEX_ONE]))
- << SHIFT_LEFT_4_POSITION) |
- ((u32)a_data_u8[INDEX_TWO] >>
- SHIFT_RIGHT_4_POSITION));
- }
- return com_rslt;
-}
-/*!
- * @brief Reads actual pressure from uncompensated pressure
- * @note Returns the value in Pascal(Pa)
- * Output value of "96386" equals 96386 Pa =
- * 963.86 hPa = 963.86 millibar
- *
- *
- *
- * @param v_uncomp_pressure_s32 : value of uncompensated pressure
- *
- *
- *
- * @return Return the actual pressure output as u32
- *
-*/
-u32 bme280_compensate_P_int32(s32 v_uncomp_pressure_s32)
-{
- s32 v_x1_u32 = BME280_ZERO_U8X;
- s32 v_x2_u32 = BME280_ZERO_U8X;
- u32 v_pressure_u32 = BME280_ZERO_U8X;
-
- v_x1_u32 = (((s32)p_bme280->cal_param.t_fine)
- >> SHIFT_RIGHT_1_POSITION) -
- (s32)BME280_PRESSURE_6_4_0_0_0_DATA;
- v_x2_u32 = (((v_x1_u32 >> SHIFT_RIGHT_2_POSITION)
- * (v_x1_u32 >> SHIFT_RIGHT_2_POSITION))
- >> SHIFT_RIGHT_11_POSITION) *
- ((s32)p_bme280->cal_param.dig_P6);
- v_x2_u32 = v_x2_u32 + ((v_x1_u32 *
- ((s32)p_bme280->cal_param.dig_P5))
- << SHIFT_LEFT_1_POSITION);
- v_x2_u32 = (v_x2_u32 >> SHIFT_RIGHT_2_POSITION) +
- (((s32)p_bme280->cal_param.dig_P4)
- << SHIFT_LEFT_16_POSITION);
- v_x1_u32 = (((p_bme280->cal_param.dig_P3
- * (((v_x1_u32 >> SHIFT_RIGHT_2_POSITION) *
- (v_x1_u32 >> SHIFT_RIGHT_2_POSITION))
- >> SHIFT_RIGHT_13_POSITION)) >> SHIFT_RIGHT_3_POSITION) +
- ((((s32)p_bme280->cal_param.dig_P2) *
- v_x1_u32) >> SHIFT_RIGHT_1_POSITION))
- >> SHIFT_RIGHT_18_POSITION;
- v_x1_u32 = ((((BME280_PRESSURE_3_2_7_6_8_DATA + v_x1_u32)) *
- ((s32)p_bme280->cal_param.dig_P1))
- >> SHIFT_RIGHT_15_POSITION);
- v_pressure_u32 =
- (((u32)(((s32)BME280_PRESSURE_1_0_4_8_5_7_6_DATA)
- - v_uncomp_pressure_s32) -
- (v_x2_u32 >> SHIFT_RIGHT_12_POSITION)))
- * BME280_PRESSURE_3_1_2_5_DATA;
- if (v_pressure_u32
- < BME280_HEX_PRESSURE_8_0_0_0_0_0_0_0_DATA)
- /* Avoid exception caused by division by zero */
- if (v_x1_u32 != BME280_ZERO_U8X)
- v_pressure_u32 =
- (v_pressure_u32 << SHIFT_LEFT_1_POSITION) /
- ((u32)v_x1_u32);
- else
- return BME280_ZERO_U8X;
- else
- /* Avoid exception caused by division by zero */
- if (v_x1_u32 != BME280_ZERO_U8X)
- v_pressure_u32 = (v_pressure_u32
- / (u32)v_x1_u32) * BME280_TWO_U8X;
- else
- return BME280_ZERO_U8X;
-
- v_x1_u32 = (((s32)p_bme280->cal_param.dig_P9) *
- ((s32)(((v_pressure_u32 >> SHIFT_RIGHT_3_POSITION)
- * (v_pressure_u32 >> SHIFT_RIGHT_3_POSITION))
- >> SHIFT_RIGHT_13_POSITION)))
- >> SHIFT_RIGHT_12_POSITION;
- v_x2_u32 = (((s32)(v_pressure_u32
- >> SHIFT_RIGHT_2_POSITION)) *
- ((s32)p_bme280->cal_param.dig_P8))
- >> SHIFT_RIGHT_13_POSITION;
- v_pressure_u32 = (u32)((s32)v_pressure_u32 +
- ((v_x1_u32 + v_x2_u32 + p_bme280->cal_param.dig_P7)
- >> SHIFT_RIGHT_4_POSITION));
-
- return v_pressure_u32;
-}
-/*!
- * @brief This API is used to read uncompensated humidity.
- * in the registers 0xF7, 0xF8 and 0xF9
- * @note 0xFD -> MSB -> bit from 0 to 7
- * @note 0xFE -> LSB -> bit from 0 to 7
- *
- *
- *
- * @param v_uncomp_humidity_s32 : The value of uncompensated humidity
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_humidity(
-s32 *v_uncomp_humidity_s32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* Array holding the MSB and LSb value
- a_data_u8[0] - Humidity MSB
- a_data_u8[1] - Humidity LSB
- */
- u8 a_data_u8[ARRAY_SIZE_TWO] = {
- BME280_ZERO_U8X, BME280_ZERO_U8X};
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_HUMIDITY_MSB_REG, a_data_u8, BME280_TWO_U8X);
- *v_uncomp_humidity_s32 = (s32)(
- (((u32)(a_data_u8[INDEX_ZERO]))
- << SHIFT_LEFT_8_POSITION)|
- ((u32)(a_data_u8[INDEX_ONE])));
- }
- return com_rslt;
-}
-/*!
- * @brief Reads actual humidity from uncompensated humidity
- * @note Returns the value in %rH as unsigned 32bit integer
- * in Q22.10 format(22 integer 10 fractional bits).
- * @note An output value of 42313
- * represents 42313 / 1024 = 41.321 %rH
- *
- *
- *
- * @param v_uncomp_humidity_s32: value of uncompensated humidity
- *
- * @return Return the actual relative humidity output as u32
- *
-*/
-u32 bme280_compensate_H_int32(s32 v_uncomp_humidity_s32)
-{
- s32 v_x1_u32;
- v_x1_u32 = (p_bme280->cal_param.t_fine
- - ((s32)BME280_HUMIDITY_7_6_8_0_0_DATA));
- v_x1_u32 = (((((v_uncomp_humidity_s32
- << SHIFT_LEFT_14_POSITION) -
- (((s32)p_bme280->cal_param.dig_H4)
- << SHIFT_LEFT_20_POSITION) -
- (((s32)p_bme280->cal_param.dig_H5) * v_x1_u32)) +
- ((s32)BME280_HUMIDITY_1_6_3_8_4_DATA))
- >> SHIFT_RIGHT_15_POSITION) *
- (((((((v_x1_u32 *
- ((s32)p_bme280->cal_param.dig_H6))
- >> SHIFT_RIGHT_10_POSITION) *
- (((v_x1_u32 * ((s32)p_bme280->cal_param.dig_H3))
- >> SHIFT_RIGHT_11_POSITION) +
- ((s32)BME280_HUMIDITY_3_2_7_6_8_DATA)))
- >> SHIFT_RIGHT_10_POSITION) +
- ((s32)BME280_HUMIDITY_2_0_9_7_1_5_2_DATA)) *
- ((s32)p_bme280->cal_param.dig_H2)
- + BME280_HUMIDITY_8_1_9_2_DATA)
- >> SHIFT_RIGHT_14_POSITION));
- v_x1_u32 = (v_x1_u32 - (((((v_x1_u32
- >> SHIFT_RIGHT_15_POSITION) *
- (v_x1_u32 >> SHIFT_RIGHT_15_POSITION))
- >> SHIFT_RIGHT_7_POSITION) *
- ((s32)p_bme280->cal_param.dig_H1))
- >> SHIFT_RIGHT_4_POSITION));
- v_x1_u32 = (v_x1_u32 < BME280_ZERO_U8X
- ? BME280_ZERO_U8X : v_x1_u32);
- v_x1_u32 =
- (v_x1_u32 > BME280_HUMIDITY_4_1_9_4_3_0_4_0_0_DATA ?
- BME280_HUMIDITY_4_1_9_4_3_0_4_0_0_DATA : v_x1_u32);
- return (u32)(v_x1_u32 >> SHIFT_RIGHT_12_POSITION);
-}
-/*!
- * @brief Reads actual humidity from uncompensated humidity
- * @note Returns the value in %rH as unsigned 16bit integer
- * @note An output value of 42313
- * represents 42313/512 = 82.643 %rH
- *
- *
- *
- * @param v_uncomp_humidity_s32: value of uncompensated humidity
- *
- *
- * @return Return the actual relative humidity output as u16
- *
-*/
-u16 bme280_compensate_H_int32_sixteen_bit_output(s32 v_uncomp_humidity_s32)
-{
- u32 v_x1_u32;
- u16 v_x2_u32;
- v_x1_u32 = bme280_compensate_H_int32(v_uncomp_humidity_s32);
- v_x2_u32 = (u16)(v_x1_u32 >> SHIFT_RIGHT_1_POSITION);
- return v_x2_u32;
-}
-/*!
- * @brief This API used to read uncompensated
- * pressure,temperature and humidity
- *
- *
- *
- *
- * @param v_uncomp_pressure_s32: The value of uncompensated pressure.
- * @param v_uncomp_temperature_s32: The value of uncompensated temperature
- * @param v_uncomp_humidity_s32: The value of uncompensated humidity.
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_read_uncomp_pressure_temperature_humidity(
-s32 *v_uncomp_pressure_s32,
-s32 *v_uncomp_temperature_s32, s32 *v_uncomp_humidity_s32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* Array holding the MSB and LSb value of
- a_data_u8[0] - Pressure MSB
- a_data_u8[1] - Pressure LSB
- a_data_u8[1] - Pressure LSB
- a_data_u8[1] - Temperature MSB
- a_data_u8[1] - Temperature LSB
- a_data_u8[1] - Temperature LSB
- a_data_u8[1] - Humidity MSB
- a_data_u8[1] - Humidity LSB
- */
- u8 a_data_u8[ARRAY_SIZE_EIGHT] = {
- BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X};
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_PRESSURE_MSB_REG,
- a_data_u8, BME280_EIGHT_U8X);
- /*Pressure*/
- *v_uncomp_pressure_s32 = (s32)((
- ((u32)(a_data_u8[INDEX_ZERO]))
- << SHIFT_LEFT_12_POSITION) |
- (((u32)(a_data_u8[INDEX_ONE]))
- << SHIFT_LEFT_4_POSITION) |
- ((u32)a_data_u8[INDEX_TWO] >>
- SHIFT_RIGHT_4_POSITION));
-
- /* Temperature */
- *v_uncomp_temperature_s32 = (s32)(((
- (u32) (a_data_u8[INDEX_THREE]))
- << SHIFT_LEFT_12_POSITION) |
- (((u32)(a_data_u8[INDEX_FOUR]))
- << SHIFT_LEFT_4_POSITION)
- | ((u32)a_data_u8[INDEX_FIVE]
- >> SHIFT_RIGHT_4_POSITION));
-
- /*Humidity*/
- *v_uncomp_humidity_s32 = (s32)((
- ((u32)(a_data_u8[INDEX_SIX]))
- << SHIFT_LEFT_8_POSITION)|
- ((u32)(a_data_u8[INDEX_SEVEN])));
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to read true pressure, temperature and humidity
- *
- *
- *
- *
- * @param v_pressure_u32 : The value of compensated pressure.
- * @param v_temperature_s32 : The value of compensated temperature.
- * @param v_humidity_u32 : The value of compensated humidity.
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_read_pressure_temperature_humidity(
-u32 *v_pressure_u32, s32 *v_temperature_s32, u32 *v_humidity_u32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- s32 v_uncomp_pressure_s32 = BME280_ZERO_U8X;
- s32 v_uncom_temperature_s32 = BME280_ZERO_U8X;
- s32 v_uncom_humidity_s32 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- /* read the uncompensated pressure,
- temperature and humidity*/
- com_rslt =
- bme280_read_uncomp_pressure_temperature_humidity(
- &v_uncomp_pressure_s32, &v_uncom_temperature_s32,
- &v_uncom_humidity_s32);
- /* read the true pressure, temperature and humidity*/
- *v_temperature_s32 = bme280_compensate_T_int32(
- v_uncom_temperature_s32);
- *v_pressure_u32 = bme280_compensate_P_int32(
- v_uncomp_pressure_s32);
- *v_humidity_u32 = bme280_compensate_H_int32(
- v_uncom_humidity_s32);
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to
- * calibration parameters used for calculation in the registers
- *
- * parameter | Register address | bit
- *------------|------------------|----------------
- * dig_T1 | 0x88 and 0x89 | from 0 : 7 to 8: 15
- * dig_T2 | 0x8A and 0x8B | from 0 : 7 to 8: 15
- * dig_T3 | 0x8C and 0x8D | from 0 : 7 to 8: 15
- * dig_P1 | 0x8E and 0x8F | from 0 : 7 to 8: 15
- * dig_P2 | 0x90 and 0x91 | from 0 : 7 to 8: 15
- * dig_P3 | 0x92 and 0x93 | from 0 : 7 to 8: 15
- * dig_P4 | 0x94 and 0x95 | from 0 : 7 to 8: 15
- * dig_P5 | 0x96 and 0x97 | from 0 : 7 to 8: 15
- * dig_P6 | 0x98 and 0x99 | from 0 : 7 to 8: 15
- * dig_P7 | 0x9A and 0x9B | from 0 : 7 to 8: 15
- * dig_P8 | 0x9C and 0x9D | from 0 : 7 to 8: 15
- * dig_P9 | 0x9E and 0x9F | from 0 : 7 to 8: 15
- * dig_H1 | 0xA1 | from 0 to 7
- * dig_H2 | 0xE1 and 0xE2 | from 0 : 7 to 8: 15
- * dig_H3 | 0xE3 | from 0 to 7
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_calib_param()
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 a_data_u8[ARRAY_SIZE_TWENTY_SIX] = {
- BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X,
- BME280_ZERO_U8X, BME280_ZERO_U8X, BME280_ZERO_U8X};
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_DIG_T1_LSB_REG,
- a_data_u8, BME280_TWENTY_SIX_U8X);
-
- p_bme280->cal_param.dig_T1 = (u16)(((
- (u16)((u8)a_data_u8[INDEX_ONE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_ZERO]);
- p_bme280->cal_param.dig_T2 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_THREE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWO]);
- p_bme280->cal_param.dig_T3 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_FIVE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_FOUR]);
- p_bme280->cal_param.dig_P1 = (u16)(((
- (u16)((u8)a_data_u8[INDEX_SEVEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_SIX]);
- p_bme280->cal_param.dig_P2 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_NINE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_EIGHT]);
- p_bme280->cal_param.dig_P3 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_ELEVEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TEN]);
- p_bme280->cal_param.dig_P4 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_THIRTEEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWELVE]);
- p_bme280->cal_param.dig_P5 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_FIVETEEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_FOURTEEN]);
- p_bme280->cal_param.dig_P6 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_SEVENTEEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_SIXTEEN]);
- p_bme280->cal_param.dig_P7 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_NINETEEN])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_EIGHTEEN]);
- p_bme280->cal_param.dig_P8 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_TWENTY_ONE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWENTY]);
- p_bme280->cal_param.dig_P9 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_TWENTY_THREE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_TWENTY_TWO]);
- p_bme280->cal_param.dig_H1 =
- a_data_u8[INDEX_TWENTY_FIVE];
- com_rslt += p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_DIG_H2_LSB_REG, a_data_u8, BME280_SEVEN_U8X);
- p_bme280->cal_param.dig_H2 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_ONE])) <<
- SHIFT_LEFT_8_POSITION) | a_data_u8[INDEX_ZERO]);
- p_bme280->cal_param.dig_H3 = a_data_u8[INDEX_TWO];
- p_bme280->cal_param.dig_H4 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_THREE])) <<
- SHIFT_LEFT_4_POSITION) |
- (((u8)BME280_HEX_CALIB_0_F_DATA)
- & a_data_u8[INDEX_FOUR]));
- p_bme280->cal_param.dig_H5 = (s16)(((
- (s16)((s8)a_data_u8[INDEX_FIVE])) <<
- SHIFT_LEFT_4_POSITION) | (a_data_u8[INDEX_FOUR] >>
- SHIFT_RIGHT_4_POSITION));
- p_bme280->cal_param.dig_H6 = (s8)a_data_u8[INDEX_SIX];
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to get
- * the temperature oversampling setting in the register 0xF4
- * bits from 5 to 7
- *
- * value | Temperature oversampling
- * ---------------------|---------------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of temperature over sampling
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_temperature(
-u8 *v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE);
-
- p_bme280->oversamp_temperature = *v_value_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to set
- * the temperature oversampling setting in the register 0xF4
- * bits from 5 to 7
- *
- * value | Temperature oversampling
- * ---------------------|---------------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of temperature over sampling
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_temperature(
-u8 v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_data_u8 = p_bme280->ctrl_meas_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE, v_value_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous value
- of configuration register*/
- v_pre_config_value_u8 = p_bme280->config_reg;
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &v_pre_config_value_u8, BME280_ONE_U8X);
- /* write previous value
- of humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write previous and updated value
- of configuration register*/
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- } else {
- com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE__REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- p_bme280->oversamp_temperature = v_value_u8;
- /* read the control measurement register value*/
- com_rslt = bme280_read_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the control
- configuration register value*/
- com_rslt += bme280_read_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to get
- * the pressure oversampling setting in the register 0xF4
- * bits from 2 to 4
- *
- * value | Pressure oversampling
- * --------------------|--------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of pressure oversampling
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_pressure(
-u8 *v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_value_u8 = BME280_GET_BITSLICE(
- v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE);
-
- p_bme280->oversamp_pressure = *v_value_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to set
- * the pressure oversampling setting in the register 0xF4
- * bits from 2 to 4
- *
- * value | Pressure oversampling
- * --------------------|--------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of pressure oversampling
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_pressure(
-u8 v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_data_u8 = p_bme280->ctrl_meas_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE, v_value_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous value of
- configuration register*/
- v_pre_config_value_u8 = p_bme280->config_reg;
- com_rslt = bme280_write_register(
- BME280_CONFIG_REG,
- &v_pre_config_value_u8, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write previous and updated value of
- control measurement register*/
- bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- } else {
- com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE__REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- p_bme280->oversamp_pressure = v_value_u8;
- /* read the control measurement register value*/
- com_rslt = bme280_read_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the control
- configuration register value*/
- com_rslt += bme280_read_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to get
- * the humidity oversampling setting in the register 0xF2
- * bits from 0 to 2
- *
- * value | Humidity oversampling
- * ---------------------|-------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of humidity over sampling
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_oversamp_humidity(
-u8 *v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_value_u8 = BME280_GET_BITSLICE(
- v_data_u8,
- BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY);
-
- p_bme280->oversamp_humidity = *v_value_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to set
- * the humidity oversampling setting in the register 0xF2
- * bits from 0 to 2
- *
- * value | Humidity oversampling
- * ---------------------|-------------------------
- * 0x00 | Skipped
- * 0x01 | BME280_OVERSAMP_1X
- * 0x02 | BME280_OVERSAMP_2X
- * 0x03 | BME280_OVERSAMP_4X
- * 0x04 | BME280_OVERSAMP_8X
- * 0x05,0x06 and 0x07 | BME280_OVERSAMP_16X
- *
- *
- * @param v_value_u8 : The value of humidity over sampling
- *
- *
- *
- * @note The "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
- * register sets the humidity
- * data acquisition options of the device.
- * @note changes to this registers only become
- * effective after a write operation to
- * "BME280_CTRL_MEAS_REG" register.
- * @note In the code automated reading and writing of
- * "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
- * @note register first set the
- * "BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY"
- * and then read and write
- * the "BME280_CTRL_MEAS_REG" register in the function.
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_oversamp_humidity(
-u8 v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
- u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- /* write humidity oversampling*/
- v_data_u8 = p_bme280->ctrl_hum_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY, v_value_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous value of
- configuration register*/
- v_pre_config_value_u8 = p_bme280->config_reg;
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &v_pre_config_value_u8, BME280_ONE_U8X);
- /* write the value of control humidity*/
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- /* write previous value of
- control measurement register*/
- pre_ctrl_meas_value =
- p_bme280->ctrl_meas_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &pre_ctrl_meas_value, BME280_ONE_U8X);
- } else {
- com_rslt +=
- p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_HUMIDITY_REG_OVERSAM_HUMIDITY__REG,
- &v_data_u8, BME280_ONE_U8X);
- /* Control humidity write will effective only
- after the control measurement register*/
- pre_ctrl_meas_value =
- p_bme280->ctrl_meas_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &pre_ctrl_meas_value, BME280_ONE_U8X);
- }
- p_bme280->oversamp_humidity = v_value_u8;
- /* read the control measurement register value*/
- com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the control configuration register value*/
- com_rslt += bme280_read_register(BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to get the
- * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
- *
- *
- *
- * @param v_power_mode_u8 : The value of power mode
- * value | mode
- * -----------------|------------------
- * 0x00 | BME280_SLEEP_MODE
- * 0x01 and 0x02 | BME280_FORCED_MODE
- * 0x03 | BME280_NORMAL_MODE
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_power_mode(u8 *v_power_mode_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_mode_u8r = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_POWER_MODE__REG,
- &v_mode_u8r, BME280_ONE_U8X);
- *v_power_mode_u8 = BME280_GET_BITSLICE(v_mode_u8r,
- BME280_CTRL_MEAS_REG_POWER_MODE);
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to set the
- * Operational Mode from the sensor in the register 0xF4 bit 0 and 1
- *
- *
- *
- * @param v_power_mode_u8 : The value of power mode
- * value | mode
- * -----------------|------------------
- * 0x00 | BME280_SLEEP_MODE
- * 0x01 and 0x02 | BME280_FORCED_MODE
- * 0x03 | BME280_NORMAL_MODE
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_power_mode(u8 v_power_mode_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_mode_u8r = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- u8 v_pre_config_value_u8 = BME280_ZERO_U8X;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- if (v_power_mode_u8 < BME280_FOUR_U8X) {
- v_mode_u8r = p_bme280->ctrl_meas_reg;
- v_mode_u8r =
- BME280_SET_BITSLICE(v_mode_u8r,
- BME280_CTRL_MEAS_REG_POWER_MODE,
- v_power_mode_u8);
- com_rslt = bme280_get_power_mode(
- &v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous value of
- configuration register*/
- v_pre_config_value_u8 =
- p_bme280->config_reg;
- com_rslt = bme280_write_register(
- BME280_CONFIG_REG,
- &v_pre_config_value_u8, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8,
- BME280_ONE_U8X);
- /* write previous and updated value of
- control measurement register*/
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &v_mode_u8r, BME280_ONE_U8X);
- } else {
- com_rslt =
- p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CTRL_MEAS_REG_POWER_MODE__REG,
- &v_mode_u8r, BME280_ONE_U8X);
- }
- /* read the control measurement register value*/
- com_rslt = bme280_read_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the config register value*/
- com_rslt += bme280_read_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- } else {
- com_rslt = E_BME280_OUT_OF_RANGE;
- }
- }
- return com_rslt;
-}
-/*!
- * @brief Used to reset the sensor
- * The value 0xB6 is written to the 0xE0
- * register the device is reset using the
- * complete power-on-reset procedure.
- * @note Soft reset can be easily set using bme280_set_softreset().
- * @note Usage Hint : bme280_set_softreset()
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_soft_rst()
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_SOFT_RESET_CODE;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_RST_REG, &v_data_u8, BME280_ONE_U8X);
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to get the sensor
- * SPI mode(communication type) in the register 0xF5 bit 0
- *
- *
- *
- * @param v_enable_disable_u8 : The value of SPI enable
- * value | Description
- * --------|--------------
- * 0 | Disable
- * 1 | Enable
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_spi3(u8 *v_enable_disable_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_SPI3_ENABLE__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_enable_disable_u8 = BME280_GET_BITSLICE(
- v_data_u8,
- BME280_CONFIG_REG_SPI3_ENABLE);
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to set the sensor
- * SPI mode(communication type) in the register 0xF5 bit 0
- *
- *
- *
- * @param v_enable_disable_u8 : The value of SPI enable
- * value | Description
- * --------|--------------
- * 0 | Disable
- * 1 | Enable
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_spi3(u8 v_enable_disable_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_data_u8 = p_bme280->config_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CONFIG_REG_SPI3_ENABLE, v_enable_disable_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous and updated value of
- configuration register*/
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write previous value of
- control measurement register*/
- pre_ctrl_meas_value =
- p_bme280->ctrl_meas_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &pre_ctrl_meas_value, BME280_ONE_U8X);
- } else {
- com_rslt =
- p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_SPI3_ENABLE__REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- /* read the control measurement register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the control configuration register value*/
- com_rslt += bme280_read_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to reads filter setting
- * in the register 0xF5 bit 3 and 4
- *
- *
- *
- * @param v_value_u8 : The value of IIR filter coefficient
- *
- * value | Filter coefficient
- * -------------|-------------------------
- * 0x00 | BME280_FILTER_COEFF_OFF
- * 0x01 | BME280_FILTER_COEFF_2
- * 0x02 | BME280_FILTER_COEFF_4
- * 0x03 | BME280_FILTER_COEFF_8
- * 0x04 | BME280_FILTER_COEFF_16
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_filter(u8 *v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_FILTER__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_value_u8 = BME280_GET_BITSLICE(v_data_u8,
- BME280_CONFIG_REG_FILTER);
- }
- return com_rslt;
-}
-/*!
- * @brief This API is used to write filter setting
- * in the register 0xF5 bit 3 and 4
- *
- *
- *
- * @param v_value_u8 : The value of IIR filter coefficient
- *
- * value | Filter coefficient
- * -------------|-------------------------
- * 0x00 | BME280_FILTER_COEFF_OFF
- * 0x01 | BME280_FILTER_COEFF_2
- * 0x02 | BME280_FILTER_COEFF_4
- * 0x03 | BME280_FILTER_COEFF_8
- * 0x04 | BME280_FILTER_COEFF_16
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_filter(u8 v_value_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_data_u8 = p_bme280->config_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CONFIG_REG_FILTER, v_value_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous and updated value of
- configuration register*/
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write previous value of
- control measurement register*/
- pre_ctrl_meas_value =
- p_bme280->ctrl_meas_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &pre_ctrl_meas_value, BME280_ONE_U8X);
- } else {
- com_rslt =
- p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_FILTER__REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- /* read the control measurement register value*/
- com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the configuration register value*/
- com_rslt += bme280_read_register(BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to Read the
- * standby duration time from the sensor in the register 0xF5 bit 5 to 7
- *
- * @param v_standby_durn_u8 : The value of standby duration time value.
- * value | standby duration
- * -------------|-----------------------
- * 0x00 | BME280_STANDBY_TIME_1_MS
- * 0x01 | BME280_STANDBY_TIME_63_MS
- * 0x02 | BME280_STANDBY_TIME_125_MS
- * 0x03 | BME280_STANDBY_TIME_250_MS
- * 0x04 | BME280_STANDBY_TIME_500_MS
- * 0x05 | BME280_STANDBY_TIME_1000_MS
- * 0x06 | BME280_STANDBY_TIME_2000_MS
- * 0x07 | BME280_STANDBY_TIME_4000_MS
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_get_standby_durn(u8 *v_standby_durn_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_TSB__REG,
- &v_data_u8, BME280_ONE_U8X);
- *v_standby_durn_u8 = BME280_GET_BITSLICE(
- v_data_u8, BME280_CONFIG_REG_TSB);
- }
- return com_rslt;
-}
-/*!
- * @brief This API used to write the
- * standby duration time from the sensor in the register 0xF5 bit 5 to 7
- *
- * @param v_standby_durn_u8 : The value of standby duration time value.
- * value | standby duration
- * -------------|-----------------------
- * 0x00 | BME280_STANDBY_TIME_1_MS
- * 0x01 | BME280_STANDBY_TIME_63_MS
- * 0x02 | BME280_STANDBY_TIME_125_MS
- * 0x03 | BME280_STANDBY_TIME_250_MS
- * 0x04 | BME280_STANDBY_TIME_500_MS
- * 0x05 | BME280_STANDBY_TIME_1000_MS
- * 0x06 | BME280_STANDBY_TIME_2000_MS
- * 0x07 | BME280_STANDBY_TIME_4000_MS
- *
- * @note Normal mode comprises an automated perpetual
- * cycling between an (active)
- * Measurement period and an (inactive) standby period.
- * @note The standby time is determined by
- * the contents of the register t_sb.
- * Standby time can be set using BME280_STANDBY_TIME_125_MS.
- *
- * @note Usage Hint : bme280_set_standby_durn(BME280_STANDBY_TIME_125_MS)
- *
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE bme280_set_standby_durn(u8 v_standby_durn_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 pre_ctrl_meas_value = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_data_u8 = p_bme280->config_reg;
- v_data_u8 =
- BME280_SET_BITSLICE(v_data_u8,
- BME280_CONFIG_REG_TSB, v_standby_durn_u8);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous and updated value of
- configuration register*/
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write previous value of control
- measurement register*/
- pre_ctrl_meas_value =
- p_bme280->ctrl_meas_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &pre_ctrl_meas_value, BME280_ONE_U8X);
- } else {
- com_rslt =
- p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- BME280_CONFIG_REG_TSB__REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- /* read the control measurement register value*/
- com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the configuration register value*/
- com_rslt += bme280_read_register(BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*
- * @brief Writes the working mode to the sensor
- *
- *
- *
- *
- * @param v_work_mode_u8 : Mode to be set
- * value | Working mode
- * ----------|--------------------
- * 0 | BME280_ULTRALOWPOWER_MODE
- * 1 | BME280_LOWPOWER_MODE
- * 2 | BME280_STANDARDRESOLUTION_MODE
- * 3 | BME280_HIGHRESOLUTION_MODE
- * 4 | BME280_ULTRAHIGHRESOLUTION_MODE
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-/*BME280_RETURN_FUNCTION_TYPE bme280_set_work_mode(u8 v_work_mode_u8)
-{
-BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
-u8 v_data_u8 = BME280_ZERO_U8X;
-if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
-} else {
- if (v_work_mode_u8 <= BME280_FOUR_U8X) {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- if (com_rslt == SUCCESS) {
- switch (v_work_mode_u8) {
- case BME280_ULTRALOWPOWER_MODE:
- p_bme280->oversamp_temperature =
- BME280_ULTRALOWPOWER_OSRS_T;
- p_bme280->osrs_p =
- BME280_ULTRALOWPOWER_OSRS_P;
- break;
- case BME280_LOWPOWER_MODE:
- p_bme280->oversamp_temperature =
- BME280_LOWPOWER_OSRS_T;
- p_bme280->osrs_p = BME280_LOWPOWER_OSRS_P;
- break;
- case BME280_STANDARDRESOLUTION_MODE:
- p_bme280->oversamp_temperature =
- BME280_STANDARDRESOLUTION_OSRS_T;
- p_bme280->osrs_p =
- BME280_STANDARDRESOLUTION_OSRS_P;
- break;
- case BME280_HIGHRESOLUTION_MODE:
- p_bme280->oversamp_temperature =
- BME280_HIGHRESOLUTION_OSRS_T;
- p_bme280->osrs_p = BME280_HIGHRESOLUTION_OSRS_P;
- break;
- case BME280_ULTRAHIGHRESOLUTION_MODE:
- p_bme280->oversamp_temperature =
- BME280_ULTRAHIGHRESOLUTION_OSRS_T;
- p_bme280->osrs_p =
- BME280_ULTRAHIGHRESOLUTION_OSRS_P;
- break;
- }
- v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_TEMPERATURE,
- p_bme280->oversamp_temperature);
- v_data_u8 = BME280_SET_BITSLICE(v_data_u8,
- BME280_CTRL_MEAS_REG_OVERSAMP_PRESSURE,
- p_bme280->osrs_p);
- com_rslt += p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr, BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- }
- } else {
- com_rslt = E_BME280_OUT_OF_RANGE;
- }
-}
-return com_rslt;
-}*/
-/*!
- * @brief This API used to read uncompensated
- * temperature,pressure and humidity in forced mode
- *
- *
- * @param v_uncom_pressure_s32: The value of uncompensated pressure
- * @param v_uncom_temperature_s32: The value of uncompensated temperature
- * @param v_uncom_humidity_s32: The value of uncompensated humidity
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
-*/
-BME280_RETURN_FUNCTION_TYPE
-bme280_get_forced_uncomp_pressure_temperature_humidity(
-s32 *v_uncom_pressure_s32,
-s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- u8 v_data_u8 = BME280_ZERO_U8X;
- u8 v_waittime_u8r = BME280_ZERO_U8X;
- u8 v_prev_pow_mode_u8 = BME280_ZERO_U8X;
- u8 v_mode_u8r = BME280_ZERO_U8X;
- u8 pre_ctrl_config_value = BME280_ZERO_U8X;
- u8 v_pre_ctrl_hum_value_u8 = BME280_ZERO_U8X;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- v_mode_u8r = p_bme280->ctrl_meas_reg;
- v_mode_u8r =
- BME280_SET_BITSLICE(v_mode_u8r,
- BME280_CTRL_MEAS_REG_POWER_MODE, BME280_FORCED_MODE);
- com_rslt = bme280_get_power_mode(&v_prev_pow_mode_u8);
- if (v_prev_pow_mode_u8 != BME280_SLEEP_MODE) {
- com_rslt += bme280_set_soft_rst();
- p_bme280->delay_msec(BME280_3MS_DELAY);
- /* write previous and updated value of
- configuration register*/
- pre_ctrl_config_value = p_bme280->config_reg;
- com_rslt += bme280_write_register(
- BME280_CONFIG_REG,
- &pre_ctrl_config_value, BME280_ONE_U8X);
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write the force mode */
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &v_mode_u8r, BME280_ONE_U8X);
- } else {
- /* write previous value of
- humidity oversampling*/
- v_pre_ctrl_hum_value_u8 =
- p_bme280->ctrl_hum_reg;
- com_rslt += bme280_write_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_pre_ctrl_hum_value_u8, BME280_ONE_U8X);
- /* write the force mode */
- com_rslt += bme280_write_register(
- BME280_CTRL_MEAS_REG,
- &v_mode_u8r, BME280_ONE_U8X);
- }
- bme280_compute_wait_time(&v_waittime_u8r);
- p_bme280->delay_msec(v_waittime_u8r);
- /* read the force-mode value of pressure
- temperature and humidity*/
- com_rslt +=
- bme280_read_uncomp_pressure_temperature_humidity(
- v_uncom_pressure_s32, v_uncom_temperature_s32,
- v_uncom_humidity_s32);
-
- /* read the control humidity register value*/
- com_rslt += bme280_read_register(
- BME280_CTRL_HUMIDITY_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_hum_reg = v_data_u8;
- /* read the configuration register value*/
- com_rslt += bme280_read_register(BME280_CONFIG_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->config_reg = v_data_u8;
-
- /* read the control measurement register value*/
- com_rslt += bme280_read_register(BME280_CTRL_MEAS_REG,
- &v_data_u8, BME280_ONE_U8X);
- p_bme280->ctrl_meas_reg = v_data_u8;
- }
- return com_rslt;
-}
-/*!
- * @brief
- * This API write the data to
- * the given register
- *
- *
- * @param v_addr_u8 -> Address of the register
- * @param v_data_u8 -> The data from the register
- * @param v_len_u8 -> no of bytes to read
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
- */
-BME280_RETURN_FUNCTION_TYPE bme280_write_register(u8 v_addr_u8,
-u8 *v_data_u8, u8 v_len_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_WRITE_FUNC(
- p_bme280->dev_addr,
- v_addr_u8, v_data_u8, v_len_u8);
- }
- return com_rslt;
-}
-/*!
- * @brief
- * This API reads the data from
- * the given register
- *
- *
- * @param v_addr_u8 -> Address of the register
- * @param v_data_u8 -> The data from the register
- * @param v_len_u8 -> no of bytes to read
- *
- *
- * @return results of bus communication function
- * @retval 0 -> Success
- * @retval -1 -> Error
- *
- *
- */
-BME280_RETURN_FUNCTION_TYPE bme280_read_register(u8 v_addr_u8,
-u8 *v_data_u8, u8 v_len_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR;
- /* check the p_bme280 structure pointer as NULL*/
- if (p_bme280 == BME280_NULL) {
- return E_BME280_NULL_PTR;
- } else {
- com_rslt = p_bme280->BME280_BUS_READ_FUNC(
- p_bme280->dev_addr,
- v_addr_u8, v_data_u8, v_len_u8);
- }
- return com_rslt;
-}
-#ifdef BME280_ENABLE_FLOAT
-/*!
- * @brief Reads actual temperature from uncompensated temperature
- * @note returns the value in Degree centigrade
- * @note Output value of "51.23" equals 51.23 DegC.
- *
- *
- *
- * @param v_uncom_temperature_s32 : value of uncompensated temperature
- *
- *
- *
- * @return Return the actual temperature in floating point
- *
-*/
-double bme280_compensate_T_double(s32 v_uncom_temperature_s32)
-{
- double v_x1_u32 = BME280_ZERO_U8X;
- double v_x2_u32 = BME280_ZERO_U8X;
- double temperature = BME280_ZERO_U8X;
-
- v_x1_u32 = (((double)v_uncom_temperature_s32)
- / BME280_FLOAT_TRUE_TEMP_1_6_3_8_4_DATA -
- ((double)p_bme280->cal_param.dig_T1)
- / BME280_FLOAT_TRUE_TEMP_1_0_2_4_DATA) *
- ((double)p_bme280->cal_param.dig_T2);
- v_x2_u32 = ((((double)v_uncom_temperature_s32)
- / BME280_FLOAT_TRUE_TEMP_1_3_1_0_7_2_DATA -
- ((double)p_bme280->cal_param.dig_T1)
- / BME280_FLOAT_TRUE_TEMP_8_1_9_2_DATA) *
- (((double)v_uncom_temperature_s32) /
- BME280_FLOAT_TRUE_TEMP_1_3_1_0_7_2_DATA -
- ((double)p_bme280->cal_param.dig_T1) /
- BME280_FLOAT_TRUE_TEMP_8_1_9_2_DATA)) *
- ((double)p_bme280->cal_param.dig_T3);
- p_bme280->cal_param.t_fine = (s32)(v_x1_u32 + v_x2_u32);
- temperature = (v_x1_u32 + v_x2_u32) /
- BME280_FLOAT_TRUE_TEMP_5_1_2_0_DATA;
-
-
- return temperature;
-}
-/*!
- * @brief Reads actual pressure from uncompensated pressure
- * @note Returns pressure in Pa as double.
- * @note Output value of "96386.2"
- * equals 96386.2 Pa = 963.862 hPa.
- *
- *
- * @param v_uncom_pressure_s32 : value of uncompensated pressure
- *
- *
- * @return Return the actual pressure in floating point
- *
-*/
-double bme280_compensate_P_double(s32 v_uncom_pressure_s32)
-{
- double v_x1_u32 = BME280_ZERO_U8X;
- double v_x2_u32 = BME280_ZERO_U8X;
- double pressure = BME280_ZERO_U8X;
-
- v_x1_u32 = ((double)p_bme280->cal_param.t_fine /
- BME280_FLAOT_TRUE_PRESSURE_2_DATA) -
- BME280_FLAOT_TRUE_PRESSURE_6_4_0_0_0_DATA;
- v_x2_u32 = v_x1_u32 * v_x1_u32 *
- ((double)p_bme280->cal_param.dig_P6) /
- BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA;
- v_x2_u32 = v_x2_u32 + v_x1_u32 *
- ((double)p_bme280->cal_param.dig_P5) *
- BME280_FLAOT_TRUE_PRESSURE_2_DATA;
- v_x2_u32 = (v_x2_u32 / BME280_FLAOT_TRUE_PRESSURE_4_DATA) +
- (((double)p_bme280->cal_param.dig_P4) *
- BME280_FLAOT_TRUE_PRESSURE_6_5_5_3_6_DATA);
- v_x1_u32 = (((double)p_bme280->cal_param.dig_P3) *
- v_x1_u32 * v_x1_u32
- / BME280_FLAOT_TRUE_PRESSURE_5_2_4_2_8_8_DATA +
- ((double)p_bme280->cal_param.dig_P2) * v_x1_u32) /
- BME280_FLAOT_TRUE_PRESSURE_5_2_4_2_8_8_DATA;
- v_x1_u32 = (BME280_FLAOT_TRUE_PRESSURE_1_DATA + v_x1_u32
- / BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA) *
- ((double)p_bme280->cal_param.dig_P1);
- pressure = BME280_FLAOT_TRUE_PRESSURE_1_0_4_8_5_7_6_DATA
- - (double)v_uncom_pressure_s32;
- /* Avoid exception caused by division by zero */
- if (v_x1_u32 != BME280_ZERO_U8X)
- pressure = (pressure - (v_x2_u32
- / BME280_FLAOT_TRUE_PRESSURE_4_0_9_6_DATA))
- * BME280_FLAOT_TRUE_PRESSURE_6_2_5_0_DATA / v_x1_u32;
- else
- return BME280_ZERO_U8X;
- v_x1_u32 = ((double)p_bme280->cal_param.dig_P9) *
- pressure * pressure /
- BME280_FLAOT_TRUE_PRESSURE_2_1_4_7_4_8_3_6_4_8_DATA;
- v_x2_u32 = pressure * ((double)p_bme280->cal_param.dig_P8)
- / BME280_FLAOT_TRUE_PRESSURE_3_2_7_6_8_DATA;
- pressure = pressure + (v_x1_u32 + v_x2_u32 +
- ((double)p_bme280->cal_param.dig_P7))
- / BME280_FLAOT_TRUE_PRESSURE_1_6_DATA;
-
- return pressure;
-}
-/*!
- * @brief Reads actual humidity from uncompensated humidity
- * @note returns the value in relative humidity (%rH)
- * @note Output value of "42.12" equals 42.12 %rH
- *
- * @param v_uncom_humidity_s32 : value of uncompensated humidity
- *
- *
- *
- * @return Return the actual humidity in floating point
- *
-*/
-double bme280_compensate_H_double(s32 v_uncom_humidity_s32)
-{
- double var_h = BME280_ZERO_U8X;
- var_h = (((double)p_bme280->cal_param.t_fine)
- - BME280_TRUE_HUMIDITY_7_6_8_0_0_DATA);
- if (var_h != BME280_ZERO_U8X)
- var_h = (v_uncom_humidity_s32 -
- (((double)p_bme280->cal_param.dig_H4)
- * BME280_TRUE_HUMIDITY_6_4_DATA +
- ((double)p_bme280->cal_param.dig_H5)
- / BME280_TRUE_HUMIDITY_1_6_3_8_4_DATA * var_h))*
- (((double)p_bme280->cal_param.dig_H2)
- /BME280_TRUE_HUMIDITY_6_5_5_3_6_DATA *
- (BME280_TRUE_HUMIDITY_1_DATA + ((double)
- p_bme280->cal_param.dig_H6)
- / BME280_TRUE_HUMIDITY_6_7_1_0_8_8_6_4_DATA
- * var_h * (BME280_TRUE_HUMIDITY_1_DATA + ((double)
- p_bme280->cal_param.dig_H3)
- / BME280_TRUE_HUMIDITY_6_7_1_0_8_8_6_4_DATA * var_h)));
- else
- return BME280_ZERO_U8X;
- var_h = var_h * (BME280_TRUE_HUMIDITY_1_DATA - ((double)
- p_bme280->cal_param.dig_H1)*var_h
- / BME280_TRUE_HUMIDITY_5_2_4_2_8_8_DATA);
- if (var_h > BME280_TRUE_HUMIDITY_1_0_0_DATA)
- var_h = BME280_TRUE_HUMIDITY_1_0_0_DATA;
- else if (var_h < BME280_TRUE_HUMIDITY_0_DATA)
- var_h = BME280_TRUE_HUMIDITY_0_DATA;
- return var_h;
-
-}
-#endif
-#if defined(BME280_ENABLE_INT64) && defined(BME280_64BITSUPPORT_PRESENT)
-/*!
- * @brief Reads actual pressure from uncompensated pressure
- * @note Returns the value in Pa as unsigned 32 bit
- * integer in Q24.8 format (24 integer bits and
- * 8 fractional bits).
- * @note Output value of "24674867"
- * represents 24674867 / 256 = 96386.2 Pa = 963.862 hPa
- *
- *
- *
- * @param v_uncom_pressure_s32 : value of uncompensated temperature
- *
- *
- * @return Return the actual pressure in u32
- *
-*/
-u32 bme280_compensate_P_int64(s32 v_uncom_pressure_s32)
-{
- s64 v_x1_s64r = BME280_ZERO_U8X;
- s64 v_x2_s64r = BME280_ZERO_U8X;
- s64 pressure = BME280_ZERO_U8X;
- v_x1_s64r = ((s64)p_bme280->cal_param.t_fine)
- - BME280_TRUE_PRESSURE_1_2_8_0_0_0_DATA;
- v_x2_s64r = v_x1_s64r * v_x1_s64r *
- (s64)p_bme280->cal_param.dig_P6;
- v_x2_s64r = v_x2_s64r + ((v_x1_s64r *
- (s64)p_bme280->cal_param.dig_P5)
- << SHIFT_LEFT_17_POSITION);
- v_x2_s64r = v_x2_s64r +
- (((s64)p_bme280->cal_param.dig_P4)
- << SHIFT_LEFT_35_POSITION);
- v_x1_s64r = ((v_x1_s64r * v_x1_s64r *
- (s64)p_bme280->cal_param.dig_P3)
- >> SHIFT_RIGHT_8_POSITION) +
- ((v_x1_s64r * (s64)p_bme280->cal_param.dig_P2)
- << SHIFT_LEFT_12_POSITION);
- v_x1_s64r = (((((s64)BME280_ONE_U8X)
- << SHIFT_LEFT_47_POSITION) + v_x1_s64r)) *
- ((s64)p_bme280->cal_param.dig_P1)
- >> SHIFT_RIGHT_33_POSITION;
- pressure = BME280_TRUE_PRESSURE_1_0_4_8_5_7_6_DATA
- - v_uncom_pressure_s32;
- /* Avoid exception caused by division by zero */
- if (v_x1_s64r != BME280_ZERO_U8X)
- #if defined __KERNEL__
- pressure = div64_s64((((pressure
- << SHIFT_LEFT_31_POSITION) - v_x2_s64r)
- * BME280_TRUE_PRESSURE_3_1_2_5_DATA),
- v_x1_s64r);
- #else
- pressure = (((pressure
- << SHIFT_LEFT_31_POSITION) - v_x2_s64r)
- * BME280_TRUE_PRESSURE_3_1_2_5_DATA) / v_x1_s64r;
- #endif
- else
- return BME280_ZERO_U8X;
- v_x1_s64r = (((s64)p_bme280->cal_param.dig_P9) *
- (pressure >> SHIFT_RIGHT_13_POSITION) *
- (pressure >> SHIFT_RIGHT_13_POSITION))
- >> SHIFT_RIGHT_25_POSITION;
- v_x2_s64r = (((s64)p_bme280->cal_param.dig_P8) *
- pressure) >> SHIFT_RIGHT_19_POSITION;
- pressure = (((pressure + v_x1_s64r +
- v_x2_s64r) >> SHIFT_RIGHT_8_POSITION) +
- (((s64)p_bme280->cal_param.dig_P7)
- << SHIFT_LEFT_4_POSITION));
-
- return (u32)pressure;
-}
-/*!
- * @brief Reads actual pressure from uncompensated pressure
- * @note Returns the value in Pa.
- * @note Output value of "12337434"
- * @note represents 12337434 / 128 = 96386.2 Pa = 963.862 hPa
- *
- *
- *
- * @param v_uncom_pressure_s32 : value of uncompensated pressure
- *
- *
- * @return the actual pressure in u32
- *
-*/
-u32 bme280_compensate_P_int64_twentyfour_bit_output(s32 v_uncom_pressure_s32)
-{
- u32 pressure = BME280_ZERO_U8X;
- pressure = bme280_compensate_P_int64(v_uncom_pressure_s32);
- pressure = (u32)(pressure >> SHIFT_RIGHT_1_POSITION);
- return pressure;
-}
-#endif
-/*!
- * @brief Computing waiting time for sensor data read
- *
- *
- *
- *
- * @param v_delaytime_u8 : The value of delay time for force mode
- *
- *
- * @retval 0 -> Success
- *
- *
- */
-BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8
-*v_delaytime_u8)
-{
- /* used to return the communication result*/
- BME280_RETURN_FUNCTION_TYPE com_rslt = SUCCESS;
-
- *v_delaytime_u8 = (T_INIT_MAX +
- T_MEASURE_PER_OSRS_MAX *
- (((BME280_ONE_U8X
- << p_bme280->oversamp_temperature)
- >> SHIFT_RIGHT_1_POSITION) +
- ((BME280_ONE_U8X << p_bme280->oversamp_pressure)
- >> SHIFT_RIGHT_1_POSITION) +
- ((BME280_ONE_U8X << p_bme280->oversamp_humidity)
- >> SHIFT_RIGHT_1_POSITION))+
- (p_bme280->oversamp_pressure ?
- T_SETUP_PRESSURE_MAX : BME280_ZERO_U8X) +
- (p_bme280->oversamp_humidity ?
- T_SETUP_HUMIDITY_MAX : BME280_ZERO_U8X)
- + BME280_FIVETEEN_U8X) / BME280_SIXTEEN_U8X;
- return com_rslt;
-}
+/**\r
+* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
+*\r
+* BSD-3-Clause\r
+*\r
+* Redistribution and use in source and binary forms, with or without\r
+* modification, are permitted provided that the following conditions are met:\r
+*\r
+* 1. Redistributions of source code must retain the above copyright\r
+* notice, this list of conditions and the following disclaimer.\r
+*\r
+* 2. Redistributions in binary form must reproduce the above copyright\r
+* notice, this list of conditions and the following disclaimer in the\r
+* documentation and/or other materials provided with the distribution.\r
+*\r
+* 3. Neither the name of the copyright holder nor the names of its\r
+* contributors may be used to endorse or promote products derived from\r
+* this software without specific prior written permission.\r
+*\r
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
+* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
+* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+* POSSIBILITY OF SUCH DAMAGE.\r
+*\r
+* @file bme280.c\r
+* @date 2020-03-28\r
+* @version v3.5.0\r
+*\r
+*/\r
+\r
+/*! @file bme280.c\r
+ * @brief Sensor driver for BME280 sensor\r
+ */\r
+#include "bme280.h"\r
+\r
+/**\name Internal macros */\r
+/* To identify osr settings selected by user */\r
+#define OVERSAMPLING_SETTINGS UINT8_C(0x07)\r
+\r
+/* To identify filter and standby settings selected by user */\r
+#define FILTER_STANDBY_SETTINGS UINT8_C(0x18)\r
+\r
+/*!\r
+ * @brief This internal API puts the device to sleep mode.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ *\r
+ * @return Result of API execution status.\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t put_device_to_sleep(struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API writes the power mode in the sensor.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ * @param[in] sensor_mode : Variable which contains the power mode to be set.\r
+ *\r
+ * @return Result of API execution status.\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t write_power_mode(uint8_t sensor_mode, struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API is used to validate the device pointer for\r
+ * null conditions.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t null_ptr_check(const struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API interleaves the register address between the\r
+ * register data buffer for burst write operation.\r
+ *\r
+ * @param[in] reg_addr : Contains the register address array.\r
+ * @param[out] temp_buff : Contains the temporary buffer to store the\r
+ * register data and register address.\r
+ * @param[in] reg_data : Contains the register data to be written in the\r
+ * temporary buffer.\r
+ * @param[in] len : No of bytes of data to be written for burst write.\r
+ *\r
+ */\r
+static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len);\r
+\r
+/*!\r
+ * @brief This internal API reads the calibration data from the sensor, parse\r
+ * it and store in the device structure.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t get_calib_data(struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API is used to parse the temperature and\r
+ * pressure calibration data and store it in the device structure.\r
+ *\r
+ * @param[out] dev : Structure instance of bme280_dev to store the calib data.\r
+ * @param[in] reg_data : Contains the calibration data to be parsed.\r
+ *\r
+ */\r
+static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API is used to parse the humidity calibration data\r
+ * and store it in device structure.\r
+ *\r
+ * @param[out] dev : Structure instance of bme280_dev to store the calib data.\r
+ * @param[in] reg_data : Contains calibration data to be parsed.\r
+ *\r
+ */\r
+static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);\r
+\r
+#ifdef BME280_FLOAT_ENABLE\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw pressure data and\r
+ * return the compensated pressure data in double data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated pressure data.\r
+ * @param[in] calib_data : Pointer to the calibration data structure.\r
+ *\r
+ * @return Compensated pressure data in double.\r
+ *\r
+ */\r
+static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data);\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw humidity data and\r
+ * return the compensated humidity data in double data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated humidity data.\r
+ * @param[in] calib_data : Pointer to the calibration data structure.\r
+ *\r
+ * @return Compensated humidity data in double.\r
+ *\r
+ */\r
+static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data);\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw temperature data and\r
+ * return the compensated temperature data in double data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated temperature data.\r
+ * @param[in] calib_data : Pointer to calibration data structure.\r
+ *\r
+ * @return Compensated temperature data in double.\r
+ *\r
+ */\r
+static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data,\r
+ struct bme280_calib_data *calib_data);\r
+\r
+#else\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw temperature data and\r
+ * return the compensated temperature data in integer data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated temperature data.\r
+ * @param[in] calib_data : Pointer to calibration data structure.\r
+ *\r
+ * @return Compensated temperature data in integer.\r
+ *\r
+ */\r
+static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data,\r
+ struct bme280_calib_data *calib_data);\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw pressure data and\r
+ * return the compensated pressure data in integer data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated pressure data.\r
+ * @param[in] calib_data : Pointer to the calibration data structure.\r
+ *\r
+ * @return Compensated pressure data in integer.\r
+ *\r
+ */\r
+static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data);\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw humidity data and\r
+ * return the compensated humidity data in integer data type.\r
+ *\r
+ * @param[in] uncomp_data : Contains the uncompensated humidity data.\r
+ * @param[in] calib_data : Pointer to the calibration data structure.\r
+ *\r
+ * @return Compensated humidity data in integer.\r
+ *\r
+ */\r
+static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data);\r
+\r
+#endif\r
+\r
+/*!\r
+ * @brief This internal API is used to identify the settings which the user\r
+ * wants to modify in the sensor.\r
+ *\r
+ * @param[in] sub_settings : Contains the settings subset to identify particular\r
+ * group of settings which the user is interested to change.\r
+ * @param[in] desired_settings : Contains the user specified settings.\r
+ *\r
+ * @return Indicates whether user is interested to modify the settings which\r
+ * are related to sub_settings.\r
+ * @return True -> User wants to modify this group of settings\r
+ * @return False -> User does not want to modify this group of settings\r
+ *\r
+ */\r
+static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settings);\r
+\r
+/*!\r
+ * @brief This API sets the humidity over sampling settings of the sensor.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API sets the oversampling settings for pressure,\r
+ * temperature and humidity in the sensor.\r
+ *\r
+ * @param[in] desired_settings : Variable used to select the settings which\r
+ * are to be set.\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t set_osr_settings(uint8_t desired_settings, const struct bme280_settings *settings,\r
+ struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This API sets the pressure and/or temperature oversampling settings\r
+ * in the sensor according to the settings selected by the user.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ * @param[in] desired_settings: variable to select the pressure and/or\r
+ * temperature oversampling settings.\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t set_osr_press_temp_settings(uint8_t desired_settings,\r
+ const struct bme280_settings *settings,\r
+ struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API fills the pressure oversampling settings provided by\r
+ * the user in the data buffer so as to write in the sensor.\r
+ *\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[out] reg_data : Variable which is filled according to the pressure\r
+ * oversampling data provided by the user.\r
+ *\r
+ */\r
+static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings);\r
+\r
+/*!\r
+ * @brief This internal API fills the temperature oversampling settings provided\r
+ * by the user in the data buffer so as to write in the sensor.\r
+ *\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[out] reg_data : Variable which is filled according to the temperature\r
+ * oversampling data provided by the user.\r
+ *\r
+ */\r
+static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings);\r
+\r
+/*!\r
+ * @brief This internal API sets the filter and/or standby duration settings\r
+ * in the sensor according to the settings selected by the user.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[in] settings : Structure instance of bme280_settings.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t set_filter_standby_settings(uint8_t desired_settings,\r
+ const struct bme280_settings *settings,\r
+ struct bme280_dev *dev);\r
+\r
+/*!\r
+ * @brief This internal API fills the filter settings provided by the user\r
+ * in the data buffer so as to write in the sensor.\r
+ *\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[out] reg_data : Variable which is filled according to the filter\r
+ * settings data provided by the user.\r
+ *\r
+ */\r
+static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings);\r
+\r
+/*!\r
+ * @brief This internal API fills the standby duration settings provided by the\r
+ * user in the data buffer so as to write in the sensor.\r
+ *\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ * @param[out] reg_data : Variable which is filled according to the standby\r
+ * settings data provided by the user.\r
+ *\r
+ */\r
+static void fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings);\r
+\r
+/*!\r
+ * @brief This internal API parse the oversampling(pressure, temperature\r
+ * and humidity), filter and standby duration settings and store in the\r
+ * device structure.\r
+ *\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be get in the sensor.\r
+ * @param[in] reg_data : Register data to be parsed.\r
+ *\r
+ */\r
+static void parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings);\r
+\r
+/*!\r
+ * @brief This internal API reloads the already existing device settings in the\r
+ * sensor after soft reset.\r
+ *\r
+ * @param[in] dev : Structure instance of bme280_dev.\r
+ * @param[in] settings : Pointer variable which contains the settings to\r
+ * be set in the sensor.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval 0 -> Success.\r
+ * @retval > 0 -> Warning.\r
+ * @retval < 0 -> Fail.\r
+ *\r
+ */\r
+static int8_t reload_device_settings(const struct bme280_settings *settings, struct bme280_dev *dev);\r
+\r
+/****************** Global Function Definitions *******************************/\r
+\r
+/*!\r
+ * @brief This API is the entry point.\r
+ * It reads the chip-id and calibration data from the sensor.\r
+ */\r
+int8_t bme280_init(struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ /* chip id read try count */\r
+ uint8_t try_count = 5;\r
+ uint8_t chip_id = 0;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Proceed if null check is fine */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ while (try_count)\r
+ {\r
+ /* Read the chip-id of bme280 sensor */\r
+ rslt = bme280_get_regs(BME280_CHIP_ID_ADDR, &chip_id, 1, dev);\r
+\r
+ /* Check for chip id validity */\r
+ if ((rslt == BME280_OK) && (chip_id == BME280_CHIP_ID))\r
+ {\r
+ dev->chip_id = chip_id;\r
+\r
+ /* Reset the sensor */\r
+ rslt = bme280_soft_reset(dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Read the calibration data */\r
+ rslt = get_calib_data(dev);\r
+ }\r
+\r
+ break;\r
+ }\r
+\r
+ /* Wait for 1 ms */\r
+ dev->delay_us(1000, dev->intf_ptr);\r
+ --try_count;\r
+ }\r
+\r
+ /* Chip id check failed */\r
+ if (!try_count)\r
+ {\r
+ rslt = BME280_E_DEV_NOT_FOUND;\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API reads the data from the given register address of the sensor.\r
+ */\r
+int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Proceed if null check is fine */\r
+ if ((rslt == BME280_OK) && (reg_data != NULL))\r
+ {\r
+ /* If interface selected is SPI */\r
+ if (dev->intf != BME280_I2C_INTF)\r
+ {\r
+ reg_addr = reg_addr | 0x80;\r
+ }\r
+\r
+ /* Read the data */\r
+ dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);\r
+\r
+ /* Check for communication error */\r
+ if (dev->intf_rslt != BME280_INTF_RET_SUCCESS)\r
+ {\r
+ rslt = BME280_E_COMM_FAIL;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API writes the given data to the register address\r
+ * of the sensor.\r
+ */\r
+int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t temp_buff[20]; /* Typically not to write more than 10 registers */\r
+\r
+ if (len > 10)\r
+ {\r
+ len = 10;\r
+ }\r
+\r
+ uint16_t temp_len;\r
+ uint8_t reg_addr_cnt;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Check for arguments validity */\r
+ if ((rslt == BME280_OK) && (reg_addr != NULL) && (reg_data != NULL))\r
+ {\r
+ if (len != 0)\r
+ {\r
+ temp_buff[0] = reg_data[0];\r
+\r
+ /* If interface selected is SPI */\r
+ if (dev->intf != BME280_I2C_INTF)\r
+ {\r
+ for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)\r
+ {\r
+ reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;\r
+ }\r
+ }\r
+\r
+ /* Burst write mode */\r
+ if (len > 1)\r
+ {\r
+ /* Interleave register address w.r.t data for\r
+ * burst write\r
+ */\r
+ interleave_reg_addr(reg_addr, temp_buff, reg_data, len);\r
+ temp_len = ((len * 2) - 1);\r
+ }\r
+ else\r
+ {\r
+ temp_len = len;\r
+ }\r
+\r
+ dev->intf_rslt = dev->write(reg_addr[0], temp_buff, temp_len, dev->intf_ptr);\r
+\r
+ /* Check for communication error */\r
+ if (dev->intf_rslt != BME280_INTF_RET_SUCCESS)\r
+ {\r
+ rslt = BME280_E_COMM_FAIL;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_INVALID_LEN;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API sets the oversampling, filter and standby duration\r
+ * (normal mode) settings in the sensor.\r
+ */\r
+int8_t bme280_set_sensor_settings(uint8_t desired_settings, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t sensor_mode;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Proceed if null check is fine */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = bme280_get_sensor_mode(&sensor_mode, dev);\r
+\r
+ if ((rslt == BME280_OK) && (sensor_mode != BME280_SLEEP_MODE))\r
+ {\r
+ rslt = put_device_to_sleep(dev);\r
+ }\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Check if user wants to change oversampling\r
+ * settings\r
+ */\r
+ if (are_settings_changed(OVERSAMPLING_SETTINGS, desired_settings))\r
+ {\r
+ rslt = set_osr_settings(desired_settings, &dev->settings, dev);\r
+ }\r
+\r
+ /* Check if user wants to change filter and/or\r
+ * standby settings\r
+ */\r
+ if ((rslt == BME280_OK) && are_settings_changed(FILTER_STANDBY_SETTINGS, desired_settings))\r
+ {\r
+ rslt = set_filter_standby_settings(desired_settings, &dev->settings, dev);\r
+ }\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API gets the oversampling, filter and standby duration\r
+ * (normal mode) settings from the sensor.\r
+ */\r
+int8_t bme280_get_sensor_settings(struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_data[4];\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Proceed if null check is fine */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ parse_device_settings(reg_data, &dev->settings);\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API sets the power mode of the sensor.\r
+ */\r
+int8_t bme280_set_sensor_mode(uint8_t sensor_mode, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t last_set_mode;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = bme280_get_sensor_mode(&last_set_mode, dev);\r
+\r
+ /* If the sensor is not in sleep mode put the device to sleep\r
+ * mode\r
+ */\r
+ if ((rslt == BME280_OK) && (last_set_mode != BME280_SLEEP_MODE))\r
+ {\r
+ rslt = put_device_to_sleep(dev);\r
+ }\r
+\r
+ /* Set the power mode */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = write_power_mode(sensor_mode, dev);\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API gets the power mode of the sensor.\r
+ */\r
+int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ if ((rslt == BME280_OK) && (sensor_mode != NULL))\r
+ {\r
+ /* Read the power mode register */\r
+ rslt = bme280_get_regs(BME280_PWR_CTRL_ADDR, sensor_mode, 1, dev);\r
+\r
+ /* Assign the power mode in the device structure */\r
+ *sensor_mode = BME280_GET_BITS_POS_0(*sensor_mode, BME280_SENSOR_MODE);\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API performs the soft reset of the sensor.\r
+ */\r
+int8_t bme280_soft_reset(struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_addr = BME280_RESET_ADDR;\r
+ uint8_t status_reg = 0;\r
+ uint8_t try_run = 5;\r
+\r
+ /* 0xB6 is the soft reset command */\r
+ uint8_t soft_rst_cmd = BME280_SOFT_RESET_COMMAND;\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ /* Proceed if null check is fine */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Write the soft reset command in the sensor */\r
+ rslt = bme280_set_regs(®_addr, &soft_rst_cmd, 1, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* If NVM not copied yet, Wait for NVM to copy */\r
+ do\r
+ {\r
+ /* As per data sheet - Table 1, startup time is 2 ms. */\r
+ dev->delay_us(2000, dev->intf_ptr);\r
+ rslt = bme280_get_regs(BME280_STATUS_REG_ADDR, &status_reg, 1, dev);\r
+\r
+ } while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));\r
+\r
+ if (status_reg & BME280_STATUS_IM_UPDATE)\r
+ {\r
+ rslt = BME280_E_NVM_COPY_FAILED;\r
+ }\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API reads the pressure, temperature and humidity data from the\r
+ * sensor, compensates the data and store it in the bme280_data structure\r
+ * instance passed by the user.\r
+ */\r
+int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ /* Array to store the pressure, temperature and humidity data read from\r
+ * the sensor\r
+ */\r
+ uint8_t reg_data[BME280_P_T_H_DATA_LEN] = { 0 };\r
+ struct bme280_uncomp_data uncomp_data = { 0 };\r
+\r
+ /* Check for null pointer in the device structure*/\r
+ rslt = null_ptr_check(dev);\r
+\r
+ if ((rslt == BME280_OK) && (comp_data != NULL))\r
+ {\r
+ /* Read the pressure and temperature data from the sensor */\r
+ rslt = bme280_get_regs(BME280_DATA_ADDR, reg_data, BME280_P_T_H_DATA_LEN, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Parse the read data from the sensor */\r
+ bme280_parse_sensor_data(reg_data, &uncomp_data);\r
+\r
+ /* Compensate the pressure and/or temperature and/or\r
+ * humidity data from the sensor\r
+ */\r
+ rslt = bme280_compensate_data(sensor_comp, &uncomp_data, comp_data, &dev->calib_data);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API is used to parse the pressure, temperature and\r
+ * humidity data and store it in the bme280_uncomp_data structure instance.\r
+ */\r
+void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data)\r
+{\r
+ /* Variables to store the sensor data */\r
+ uint32_t data_xlsb;\r
+ uint32_t data_lsb;\r
+ uint32_t data_msb;\r
+\r
+ /* Store the parsed register values for pressure data */\r
+ data_msb = (uint32_t)reg_data[0] << 12;\r
+ data_lsb = (uint32_t)reg_data[1] << 4;\r
+ data_xlsb = (uint32_t)reg_data[2] >> 4;\r
+ uncomp_data->pressure = data_msb | data_lsb | data_xlsb;\r
+\r
+ /* Store the parsed register values for temperature data */\r
+ data_msb = (uint32_t)reg_data[3] << 12;\r
+ data_lsb = (uint32_t)reg_data[4] << 4;\r
+ data_xlsb = (uint32_t)reg_data[5] >> 4;\r
+ uncomp_data->temperature = data_msb | data_lsb | data_xlsb;\r
+\r
+ /* Store the parsed register values for humidity data */\r
+ data_msb = (uint32_t)reg_data[6] << 8;\r
+ data_lsb = (uint32_t)reg_data[7];\r
+ uncomp_data->humidity = data_msb | data_lsb;\r
+}\r
+\r
+/*!\r
+ * @brief This API is used to compensate the pressure and/or\r
+ * temperature and/or humidity data according to the component selected\r
+ * by the user.\r
+ */\r
+int8_t bme280_compensate_data(uint8_t sensor_comp,\r
+ const struct bme280_uncomp_data *uncomp_data,\r
+ struct bme280_data *comp_data,\r
+ struct bme280_calib_data *calib_data)\r
+{\r
+ int8_t rslt = BME280_OK;\r
+\r
+ if ((uncomp_data != NULL) && (comp_data != NULL) && (calib_data != NULL))\r
+ {\r
+ /* Initialize to zero */\r
+ comp_data->temperature = 0;\r
+ comp_data->pressure = 0;\r
+ comp_data->humidity = 0;\r
+\r
+ /* If pressure or temperature component is selected */\r
+ if (sensor_comp & (BME280_PRESS | BME280_TEMP | BME280_HUM))\r
+ {\r
+ /* Compensate the temperature data */\r
+ comp_data->temperature = compensate_temperature(uncomp_data, calib_data);\r
+ }\r
+\r
+ if (sensor_comp & BME280_PRESS)\r
+ {\r
+ /* Compensate the pressure data */\r
+ comp_data->pressure = compensate_pressure(uncomp_data, calib_data);\r
+ }\r
+\r
+ if (sensor_comp & BME280_HUM)\r
+ {\r
+ /* Compensate the humidity data */\r
+ comp_data->humidity = compensate_humidity(uncomp_data, calib_data);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API is used to calculate the maximum delay in milliseconds required for the\r
+ * temperature/pressure/humidity(which ever at enabled) measurement to complete.\r
+ */\r
+uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings)\r
+{\r
+ uint32_t max_delay;\r
+ uint8_t temp_osr;\r
+ uint8_t pres_osr;\r
+ uint8_t hum_osr;\r
+\r
+ /*Array to map OSR config register value to actual OSR */\r
+ uint8_t osr_sett_to_act_osr[] = { 0, 1, 2, 4, 8, 16 };\r
+\r
+ /* Mapping osr settings to the actual osr values e.g. 0b101 -> osr X16 */\r
+ if (settings->osr_t <= 5)\r
+ {\r
+ temp_osr = osr_sett_to_act_osr[settings->osr_t];\r
+ }\r
+ else\r
+ {\r
+ temp_osr = 16;\r
+ }\r
+\r
+ if (settings->osr_p <= 5)\r
+ {\r
+ pres_osr = osr_sett_to_act_osr[settings->osr_p];\r
+ }\r
+ else\r
+ {\r
+ pres_osr = 16;\r
+ }\r
+\r
+ if (settings->osr_h <= 5)\r
+ {\r
+ hum_osr = osr_sett_to_act_osr[settings->osr_h];\r
+ }\r
+ else\r
+ {\r
+ hum_osr = 16;\r
+ }\r
+\r
+ max_delay =\r
+ (uint32_t)((BME280_MEAS_OFFSET + (BME280_MEAS_DUR * temp_osr) +\r
+ ((BME280_MEAS_DUR * pres_osr) + BME280_PRES_HUM_MEAS_OFFSET) +\r
+ ((BME280_MEAS_DUR * hum_osr) + BME280_PRES_HUM_MEAS_OFFSET)) / BME280_MEAS_SCALING_FACTOR);\r
+\r
+ return max_delay;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API sets the oversampling settings for pressure,\r
+ * temperature and humidity in the sensor.\r
+ */\r
+static int8_t set_osr_settings(uint8_t desired_settings, const struct bme280_settings *settings, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt = BME280_W_INVALID_OSR_MACRO;\r
+\r
+ if (desired_settings & BME280_OSR_HUM_SEL)\r
+ {\r
+ rslt = set_osr_humidity_settings(settings, dev);\r
+ }\r
+\r
+ if (desired_settings & (BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL))\r
+ {\r
+ rslt = set_osr_press_temp_settings(desired_settings, settings, dev);\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API sets the humidity oversampling settings of the sensor.\r
+ */\r
+static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t ctrl_hum;\r
+ uint8_t ctrl_meas;\r
+ uint8_t reg_addr = BME280_CTRL_HUM_ADDR;\r
+\r
+ ctrl_hum = settings->osr_h & BME280_CTRL_HUM_MSK;\r
+\r
+ /* Write the humidity control value in the register */\r
+ rslt = bme280_set_regs(®_addr, &ctrl_hum, 1, dev);\r
+\r
+ /* Humidity related changes will be only effective after a\r
+ * write operation to ctrl_meas register\r
+ */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ reg_addr = BME280_CTRL_MEAS_ADDR;\r
+ rslt = bme280_get_regs(reg_addr, &ctrl_meas, 1, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = bme280_set_regs(®_addr, &ctrl_meas, 1, dev);\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This API sets the pressure and/or temperature oversampling settings\r
+ * in the sensor according to the settings selected by the user.\r
+ */\r
+static int8_t set_osr_press_temp_settings(uint8_t desired_settings,\r
+ const struct bme280_settings *settings,\r
+ struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_addr = BME280_CTRL_MEAS_ADDR;\r
+ uint8_t reg_data;\r
+\r
+ rslt = bme280_get_regs(reg_addr, ®_data, 1, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ if (desired_settings & BME280_OSR_PRESS_SEL)\r
+ {\r
+ fill_osr_press_settings(®_data, settings);\r
+ }\r
+\r
+ if (desired_settings & BME280_OSR_TEMP_SEL)\r
+ {\r
+ fill_osr_temp_settings(®_data, settings);\r
+ }\r
+\r
+ /* Write the oversampling settings in the register */\r
+ rslt = bme280_set_regs(®_addr, ®_data, 1, dev);\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API sets the filter and/or standby duration settings\r
+ * in the sensor according to the settings selected by the user.\r
+ */\r
+static int8_t set_filter_standby_settings(uint8_t desired_settings,\r
+ const struct bme280_settings *settings,\r
+ struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_addr = BME280_CONFIG_ADDR;\r
+ uint8_t reg_data;\r
+\r
+ rslt = bme280_get_regs(reg_addr, ®_data, 1, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ if (desired_settings & BME280_FILTER_SEL)\r
+ {\r
+ fill_filter_settings(®_data, settings);\r
+ }\r
+\r
+ if (desired_settings & BME280_STANDBY_SEL)\r
+ {\r
+ fill_standby_settings(®_data, settings);\r
+ }\r
+\r
+ /* Write the oversampling settings in the register */\r
+ rslt = bme280_set_regs(®_addr, ®_data, 1, dev);\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API fills the filter settings provided by the user\r
+ * in the data buffer so as to write in the sensor.\r
+ */\r
+static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings)\r
+{\r
+ *reg_data = BME280_SET_BITS(*reg_data, BME280_FILTER, settings->filter);\r
+}\r
+\r
+/*!\r
+ * @brief This internal API fills the standby duration settings provided by\r
+ * the user in the data buffer so as to write in the sensor.\r
+ */\r
+static void fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings)\r
+{\r
+ *reg_data = BME280_SET_BITS(*reg_data, BME280_STANDBY, settings->standby_time);\r
+}\r
+\r
+/*!\r
+ * @brief This internal API fills the pressure oversampling settings provided by\r
+ * the user in the data buffer so as to write in the sensor.\r
+ */\r
+static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings)\r
+{\r
+ *reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_PRESS, settings->osr_p);\r
+}\r
+\r
+/*!\r
+ * @brief This internal API fills the temperature oversampling settings\r
+ * provided by the user in the data buffer so as to write in the sensor.\r
+ */\r
+static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings)\r
+{\r
+ *reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_TEMP, settings->osr_t);\r
+}\r
+\r
+/*!\r
+ * @brief This internal API parse the oversampling(pressure, temperature\r
+ * and humidity), filter and standby duration settings and store in the\r
+ * device structure.\r
+ */\r
+static void parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings)\r
+{\r
+ settings->osr_h = BME280_GET_BITS_POS_0(reg_data[0], BME280_CTRL_HUM);\r
+ settings->osr_p = BME280_GET_BITS(reg_data[2], BME280_CTRL_PRESS);\r
+ settings->osr_t = BME280_GET_BITS(reg_data[2], BME280_CTRL_TEMP);\r
+ settings->filter = BME280_GET_BITS(reg_data[3], BME280_FILTER);\r
+ settings->standby_time = BME280_GET_BITS(reg_data[3], BME280_STANDBY);\r
+}\r
+\r
+/*!\r
+ * @brief This internal API writes the power mode in the sensor.\r
+ */\r
+static int8_t write_power_mode(uint8_t sensor_mode, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_addr = BME280_PWR_CTRL_ADDR;\r
+\r
+ /* Variable to store the value read from power mode register */\r
+ uint8_t sensor_mode_reg_val;\r
+\r
+ /* Read the power mode register */\r
+ rslt = bme280_get_regs(reg_addr, &sensor_mode_reg_val, 1, dev);\r
+\r
+ /* Set the power mode */\r
+ if (rslt == BME280_OK)\r
+ {\r
+ sensor_mode_reg_val = BME280_SET_BITS_POS_0(sensor_mode_reg_val, BME280_SENSOR_MODE, sensor_mode);\r
+\r
+ /* Write the power mode in the register */\r
+ rslt = bme280_set_regs(®_addr, &sensor_mode_reg_val, 1, dev);\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API puts the device to sleep mode.\r
+ */\r
+static int8_t put_device_to_sleep(struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_data[4];\r
+ struct bme280_settings settings;\r
+\r
+ rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ parse_device_settings(reg_data, &settings);\r
+ rslt = bme280_soft_reset(dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = reload_device_settings(&settings, dev);\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API reloads the already existing device settings in\r
+ * the sensor after soft reset.\r
+ */\r
+static int8_t reload_device_settings(const struct bme280_settings *settings, struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ rslt = set_osr_settings(BME280_ALL_SETTINGS_SEL, settings, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ rslt = set_filter_standby_settings(BME280_ALL_SETTINGS_SEL, settings, dev);\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+#ifdef BME280_FLOAT_ENABLE\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw temperature data and\r
+ * return the compensated temperature data in double data type.\r
+ */\r
+static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data, struct bme280_calib_data *calib_data)\r
+{\r
+ double var1;\r
+ double var2;\r
+ double temperature;\r
+ double temperature_min = -40;\r
+ double temperature_max = 85;\r
+\r
+ var1 = ((double)uncomp_data->temperature) / 16384.0 - ((double)calib_data->dig_t1) / 1024.0;\r
+ var1 = var1 * ((double)calib_data->dig_t2);\r
+ var2 = (((double)uncomp_data->temperature) / 131072.0 - ((double)calib_data->dig_t1) / 8192.0);\r
+ var2 = (var2 * var2) * ((double)calib_data->dig_t3);\r
+ calib_data->t_fine = (int32_t)(var1 + var2);\r
+ temperature = (var1 + var2) / 5120.0;\r
+\r
+ if (temperature < temperature_min)\r
+ {\r
+ temperature = temperature_min;\r
+ }\r
+ else if (temperature > temperature_max)\r
+ {\r
+ temperature = temperature_max;\r
+ }\r
+\r
+ return temperature;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw pressure data and\r
+ * return the compensated pressure data in double data type.\r
+ */\r
+static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data)\r
+{\r
+ double var1;\r
+ double var2;\r
+ double var3;\r
+ double pressure;\r
+ double pressure_min = 30000.0;\r
+ double pressure_max = 110000.0;\r
+\r
+ var1 = ((double)calib_data->t_fine / 2.0) - 64000.0;\r
+ var2 = var1 * var1 * ((double)calib_data->dig_p6) / 32768.0;\r
+ var2 = var2 + var1 * ((double)calib_data->dig_p5) * 2.0;\r
+ var2 = (var2 / 4.0) + (((double)calib_data->dig_p4) * 65536.0);\r
+ var3 = ((double)calib_data->dig_p3) * var1 * var1 / 524288.0;\r
+ var1 = (var3 + ((double)calib_data->dig_p2) * var1) / 524288.0;\r
+ var1 = (1.0 + var1 / 32768.0) * ((double)calib_data->dig_p1);\r
+\r
+ /* avoid exception caused by division by zero */\r
+ if (var1 > (0.0))\r
+ {\r
+ pressure = 1048576.0 - (double) uncomp_data->pressure;\r
+ pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;\r
+ var1 = ((double)calib_data->dig_p9) * pressure * pressure / 2147483648.0;\r
+ var2 = pressure * ((double)calib_data->dig_p8) / 32768.0;\r
+ pressure = pressure + (var1 + var2 + ((double)calib_data->dig_p7)) / 16.0;\r
+\r
+ if (pressure < pressure_min)\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+ else if (pressure > pressure_max)\r
+ {\r
+ pressure = pressure_max;\r
+ }\r
+ }\r
+ else /* Invalid case */\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+\r
+ return pressure;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw humidity data and\r
+ * return the compensated humidity data in double data type.\r
+ */\r
+static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data)\r
+{\r
+ double humidity;\r
+ double humidity_min = 0.0;\r
+ double humidity_max = 100.0;\r
+ double var1;\r
+ double var2;\r
+ double var3;\r
+ double var4;\r
+ double var5;\r
+ double var6;\r
+\r
+ var1 = ((double)calib_data->t_fine) - 76800.0;\r
+ var2 = (((double)calib_data->dig_h4) * 64.0 + (((double)calib_data->dig_h5) / 16384.0) * var1);\r
+ var3 = uncomp_data->humidity - var2;\r
+ var4 = ((double)calib_data->dig_h2) / 65536.0;\r
+ var5 = (1.0 + (((double)calib_data->dig_h3) / 67108864.0) * var1);\r
+ var6 = 1.0 + (((double)calib_data->dig_h6) / 67108864.0) * var1 * var5;\r
+ var6 = var3 * var4 * (var5 * var6);\r
+ humidity = var6 * (1.0 - ((double)calib_data->dig_h1) * var6 / 524288.0);\r
+\r
+ if (humidity > humidity_max)\r
+ {\r
+ humidity = humidity_max;\r
+ }\r
+ else if (humidity < humidity_min)\r
+ {\r
+ humidity = humidity_min;\r
+ }\r
+\r
+ return humidity;\r
+}\r
+\r
+#else\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw temperature data and\r
+ * return the compensated temperature data in integer data type.\r
+ */\r
+static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data,\r
+ struct bme280_calib_data *calib_data)\r
+{\r
+ int32_t var1;\r
+ int32_t var2;\r
+ int32_t temperature;\r
+ int32_t temperature_min = -4000;\r
+ int32_t temperature_max = 8500;\r
+\r
+ var1 = (int32_t)((uncomp_data->temperature / 8) - ((int32_t)calib_data->dig_t1 * 2));\r
+ var1 = (var1 * ((int32_t)calib_data->dig_t2)) / 2048;\r
+ var2 = (int32_t)((uncomp_data->temperature / 16) - ((int32_t)calib_data->dig_t1));\r
+ var2 = (((var2 * var2) / 4096) * ((int32_t)calib_data->dig_t3)) / 16384;\r
+ calib_data->t_fine = var1 + var2;\r
+ temperature = (calib_data->t_fine * 5 + 128) / 256;\r
+\r
+ if (temperature < temperature_min)\r
+ {\r
+ temperature = temperature_min;\r
+ }\r
+ else if (temperature > temperature_max)\r
+ {\r
+ temperature = temperature_max;\r
+ }\r
+\r
+ return temperature;\r
+}\r
+#ifndef BME280_32BIT_ENABLE /* 64 bit compensation for pressure data */\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw pressure data and\r
+ * return the compensated pressure data in integer data type with higher\r
+ * accuracy.\r
+ */\r
+static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data)\r
+{\r
+ int64_t var1;\r
+ int64_t var2;\r
+ int64_t var3;\r
+ int64_t var4;\r
+ uint32_t pressure;\r
+ uint32_t pressure_min = 3000000;\r
+ uint32_t pressure_max = 11000000;\r
+\r
+ var1 = ((int64_t)calib_data->t_fine) - 128000;\r
+ var2 = var1 * var1 * (int64_t)calib_data->dig_p6;\r
+ var2 = var2 + ((var1 * (int64_t)calib_data->dig_p5) * 131072);\r
+ var2 = var2 + (((int64_t)calib_data->dig_p4) * 34359738368);\r
+ var1 = ((var1 * var1 * (int64_t)calib_data->dig_p3) / 256) + ((var1 * ((int64_t)calib_data->dig_p2) * 4096));\r
+ var3 = ((int64_t)1) * 140737488355328;\r
+ var1 = (var3 + var1) * ((int64_t)calib_data->dig_p1) / 8589934592;\r
+\r
+ /* To avoid divide by zero exception */\r
+ if (var1 != 0)\r
+ {\r
+ var4 = 1048576 - uncomp_data->pressure;\r
+ var4 = (((var4 * INT64_C(2147483648)) - var2) * 3125) / var1;\r
+ var1 = (((int64_t)calib_data->dig_p9) * (var4 / 8192) * (var4 / 8192)) / 33554432;\r
+ var2 = (((int64_t)calib_data->dig_p8) * var4) / 524288;\r
+ var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_p7) * 16);\r
+ pressure = (uint32_t)(((var4 / 2) * 100) / 128);\r
+\r
+ if (pressure < pressure_min)\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+ else if (pressure > pressure_max)\r
+ {\r
+ pressure = pressure_max;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+\r
+ return pressure;\r
+}\r
+#else /* 32 bit compensation for pressure data */\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw pressure data and\r
+ * return the compensated pressure data in integer data type.\r
+ */\r
+static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data)\r
+{\r
+ int32_t var1;\r
+ int32_t var2;\r
+ int32_t var3;\r
+ int32_t var4;\r
+ uint32_t var5;\r
+ uint32_t pressure;\r
+ uint32_t pressure_min = 30000;\r
+ uint32_t pressure_max = 110000;\r
+\r
+ var1 = (((int32_t)calib_data->t_fine) / 2) - (int32_t)64000;\r
+ var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_p6);\r
+ var2 = var2 + ((var1 * ((int32_t)calib_data->dig_p5)) * 2);\r
+ var2 = (var2 / 4) + (((int32_t)calib_data->dig_p4) * 65536);\r
+ var3 = (calib_data->dig_p3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;\r
+ var4 = (((int32_t)calib_data->dig_p2) * var1) / 2;\r
+ var1 = (var3 + var4) / 262144;\r
+ var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_p1)) / 32768;\r
+\r
+ /* avoid exception caused by division by zero */\r
+ if (var1)\r
+ {\r
+ var5 = (uint32_t)((uint32_t)1048576) - uncomp_data->pressure;\r
+ pressure = ((uint32_t)(var5 - (uint32_t)(var2 / 4096))) * 3125;\r
+\r
+ if (pressure < 0x80000000)\r
+ {\r
+ pressure = (pressure << 1) / ((uint32_t)var1);\r
+ }\r
+ else\r
+ {\r
+ pressure = (pressure / (uint32_t)var1) * 2;\r
+ }\r
+\r
+ var1 = (((int32_t)calib_data->dig_p9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;\r
+ var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_p8)) / 8192;\r
+ pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_p7) / 16));\r
+\r
+ if (pressure < pressure_min)\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+ else if (pressure > pressure_max)\r
+ {\r
+ pressure = pressure_max;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ pressure = pressure_min;\r
+ }\r
+\r
+ return pressure;\r
+}\r
+#endif\r
+\r
+/*!\r
+ * @brief This internal API is used to compensate the raw humidity data and\r
+ * return the compensated humidity data in integer data type.\r
+ */\r
+static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data,\r
+ const struct bme280_calib_data *calib_data)\r
+{\r
+ int32_t var1;\r
+ int32_t var2;\r
+ int32_t var3;\r
+ int32_t var4;\r
+ int32_t var5;\r
+ uint32_t humidity;\r
+ uint32_t humidity_max = 102400;\r
+\r
+ var1 = calib_data->t_fine - ((int32_t)76800);\r
+ var2 = (int32_t)(uncomp_data->humidity * 16384);\r
+ var3 = (int32_t)(((int32_t)calib_data->dig_h4) * 1048576);\r
+ var4 = ((int32_t)calib_data->dig_h5) * var1;\r
+ var5 = (((var2 - var3) - var4) + (int32_t)16384) / 32768;\r
+ var2 = (var1 * ((int32_t)calib_data->dig_h6)) / 1024;\r
+ var3 = (var1 * ((int32_t)calib_data->dig_h3)) / 2048;\r
+ var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;\r
+ var2 = ((var4 * ((int32_t)calib_data->dig_h2)) + 8192) / 16384;\r
+ var3 = var5 * var2;\r
+ var4 = ((var3 / 32768) * (var3 / 32768)) / 128;\r
+ var5 = var3 - ((var4 * ((int32_t)calib_data->dig_h1)) / 16);\r
+ var5 = (var5 < 0 ? 0 : var5);\r
+ var5 = (var5 > 419430400 ? 419430400 : var5);\r
+ humidity = (uint32_t)(var5 / 4096);\r
+\r
+ if (humidity > humidity_max)\r
+ {\r
+ humidity = humidity_max;\r
+ }\r
+\r
+ return humidity;\r
+}\r
+#endif\r
+\r
+/*!\r
+ * @brief This internal API reads the calibration data from the sensor, parse\r
+ * it and store in the device structure.\r
+ */\r
+static int8_t get_calib_data(struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+ uint8_t reg_addr = BME280_TEMP_PRESS_CALIB_DATA_ADDR;\r
+\r
+ /* Array to store calibration data */\r
+ uint8_t calib_data[BME280_TEMP_PRESS_CALIB_DATA_LEN] = { 0 };\r
+\r
+ /* Read the calibration data from the sensor */\r
+ rslt = bme280_get_regs(reg_addr, calib_data, BME280_TEMP_PRESS_CALIB_DATA_LEN, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Parse temperature and pressure calibration data and store\r
+ * it in device structure\r
+ */\r
+ parse_temp_press_calib_data(calib_data, dev);\r
+ reg_addr = BME280_HUMIDITY_CALIB_DATA_ADDR;\r
+\r
+ /* Read the humidity calibration data from the sensor */\r
+ rslt = bme280_get_regs(reg_addr, calib_data, BME280_HUMIDITY_CALIB_DATA_LEN, dev);\r
+\r
+ if (rslt == BME280_OK)\r
+ {\r
+ /* Parse humidity calibration data and store it in\r
+ * device structure\r
+ */\r
+ parse_humidity_calib_data(calib_data, dev);\r
+ }\r
+ }\r
+\r
+ return rslt;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API interleaves the register address between the\r
+ * register data buffer for burst write operation.\r
+ */\r
+static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len)\r
+{\r
+ uint8_t index;\r
+\r
+ for (index = 1; index < len; index++)\r
+ {\r
+ temp_buff[(index * 2) - 1] = reg_addr[index];\r
+ temp_buff[index * 2] = reg_data[index];\r
+ }\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to parse the temperature and\r
+ * pressure calibration data and store it in device structure.\r
+ */\r
+static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_dev *dev)\r
+{\r
+ struct bme280_calib_data *calib_data = &dev->calib_data;\r
+\r
+ calib_data->dig_t1 = BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);\r
+ calib_data->dig_t2 = (int16_t)BME280_CONCAT_BYTES(reg_data[3], reg_data[2]);\r
+ calib_data->dig_t3 = (int16_t)BME280_CONCAT_BYTES(reg_data[5], reg_data[4]);\r
+ calib_data->dig_p1 = BME280_CONCAT_BYTES(reg_data[7], reg_data[6]);\r
+ calib_data->dig_p2 = (int16_t)BME280_CONCAT_BYTES(reg_data[9], reg_data[8]);\r
+ calib_data->dig_p3 = (int16_t)BME280_CONCAT_BYTES(reg_data[11], reg_data[10]);\r
+ calib_data->dig_p4 = (int16_t)BME280_CONCAT_BYTES(reg_data[13], reg_data[12]);\r
+ calib_data->dig_p5 = (int16_t)BME280_CONCAT_BYTES(reg_data[15], reg_data[14]);\r
+ calib_data->dig_p6 = (int16_t)BME280_CONCAT_BYTES(reg_data[17], reg_data[16]);\r
+ calib_data->dig_p7 = (int16_t)BME280_CONCAT_BYTES(reg_data[19], reg_data[18]);\r
+ calib_data->dig_p8 = (int16_t)BME280_CONCAT_BYTES(reg_data[21], reg_data[20]);\r
+ calib_data->dig_p9 = (int16_t)BME280_CONCAT_BYTES(reg_data[23], reg_data[22]);\r
+ calib_data->dig_h1 = reg_data[25];\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to parse the humidity calibration data\r
+ * and store it in device structure.\r
+ */\r
+static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev)\r
+{\r
+ struct bme280_calib_data *calib_data = &dev->calib_data;\r
+ int16_t dig_h4_lsb;\r
+ int16_t dig_h4_msb;\r
+ int16_t dig_h5_lsb;\r
+ int16_t dig_h5_msb;\r
+\r
+ calib_data->dig_h2 = (int16_t)BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);\r
+ calib_data->dig_h3 = reg_data[2];\r
+ dig_h4_msb = (int16_t)(int8_t)reg_data[3] * 16;\r
+ dig_h4_lsb = (int16_t)(reg_data[4] & 0x0F);\r
+ calib_data->dig_h4 = dig_h4_msb | dig_h4_lsb;\r
+ dig_h5_msb = (int16_t)(int8_t)reg_data[5] * 16;\r
+ dig_h5_lsb = (int16_t)(reg_data[4] >> 4);\r
+ calib_data->dig_h5 = dig_h5_msb | dig_h5_lsb;\r
+ calib_data->dig_h6 = (int8_t)reg_data[6];\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to identify the settings which the user\r
+ * wants to modify in the sensor.\r
+ */\r
+static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settings)\r
+{\r
+ uint8_t settings_changed = FALSE;\r
+\r
+ if (sub_settings & desired_settings)\r
+ {\r
+ /* User wants to modify this particular settings */\r
+ settings_changed = TRUE;\r
+ }\r
+ else\r
+ {\r
+ /* User don't want to modify this particular settings */\r
+ settings_changed = FALSE;\r
+ }\r
+\r
+ return settings_changed;\r
+}\r
+\r
+/*!\r
+ * @brief This internal API is used to validate the device structure pointer for\r
+ * null conditions.\r
+ */\r
+static int8_t null_ptr_check(const struct bme280_dev *dev)\r
+{\r
+ int8_t rslt;\r
+\r
+ if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_us == NULL))\r
+ {\r
+ /* Device structure pointer is not valid */\r
+ rslt = BME280_E_NULL_PTR;\r
+ }\r
+ else\r
+ {\r
+ /* Device structure is fine */\r
+ rslt = BME280_OK;\r
+ }\r
+\r
+ return rslt;\r
+}\r