/**
* Copyright (c) 2022 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder nor the names of its
* 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 THE
* 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.
*
* @file bmp3.c
* @date 2022-04-01
* @version v2.0.6
*
*/
/*! @file bmp3.c
* @brief Sensor driver for BMP3 sensor */
#include "bmp3.h"
/***************** Static function declarations ******************************/
/*!
* @brief This internal API reads the calibration data from the sensor, parse
* it then compensates it and store in the device structure.
*
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t get_calib_data(struct bmp3_dev *dev);
/*!
* @brief This internal API is used to parse the calibration data, compensates
* it and store it in device structure.
*
* @param[in] dev : Structure instance of bmp3_dev.
* @param[out] reg_data : Contains calibration data to be parsed.
*
*/
static void parse_calib_data(const uint8_t *reg_data, struct bmp3_dev *dev);
/*!
* @brief This internal API gets the over sampling, ODR and filter settings
* from the sensor.
*
* @param[in] settings : Structure instance of bmp3_settings.
* @param[in] dev : Structure instance of bmp3_dev.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t get_odr_filter_settings(struct bmp3_settings *settings, struct bmp3_dev *dev);
/*!
* @brief This internal API is used to parse the pressure and temperature data
* and store it in the bmp3_uncomp_data structure instance.
*
* @param[in] reg_data : Contains the register data which needs to be parsed.
* @param[out] uncomp_data : Contains the uncompensated press and temp data.
*
*/
static void parse_sensor_data(const uint8_t *reg_data, struct bmp3_uncomp_data *uncomp_data);
/*!
* @brief This internal API is used to compensate the pressure or temperature
* or both the data according to the component selected by the user.
*
* @param[in] sensor_comp : Used to select pressure or temperature.
* @param[in] uncomp_data : Contains the uncompensated pressure and
* temperature data.
* @param[out] comp_data : Contains the compensated pressure and
* temperature data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_data(uint8_t sensor_comp,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_data *comp_data,
struct bmp3_calib_data *calib_data);
#ifdef BMP3_FLOAT_COMPENSATION
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data.
*
* @param[out] temperature : Compensated temperature data in double.
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*
*/
static int8_t compensate_temperature(double *temperature,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to compensate the pressure data and return
* the compensated pressure data.
*
* @param[out] comp_pressure : Compensated pressure data in double.
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_pressure(double *pressure,
const struct bmp3_uncomp_data *uncomp_data,
const struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to calculate the power functionality for
* double precision floating point values.
*
* @param[in] base : Contains the base value.
* @param[in] power : Contains the power value.
*
* @return Output of power function.
* @retval Calculated power function output in float.
*/
static float pow_bmp3(double base, uint8_t power);
#else
/*!
* @brief This internal API is used to compensate the raw temperature data and
* return the compensated temperature data in integer data type.
*
* @param[out] temperature : Compensated temperature data in integer.
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_temperature(int64_t *temperature,
const struct bmp3_uncomp_data *uncomp_data,
struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to compensate the pressure data and return
* the compensated pressure data in integer data type.
*
* @param[out] comp_pressure : Compensated pressure data in integer.
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval >0 -> Warning
* @retval <0 -> Fail
*/
static int8_t compensate_pressure(uint64_t *pressure,
const struct bmp3_uncomp_data *uncomp_data,
const struct bmp3_calib_data *calib_data);
/*!
* @brief This internal API is used to calculate the power functionality.
*
* @param[in] base : Contains the base value.
* @param[in] power : Contains the power value.
*
* @return Output of power function.
* @retval Calculated power function output in integer.
*/
static uint32_t pow_bmp3(uint8_t base, uint8_t power);
#endif /* BMP3_FLOAT_COMPENSATION */
/*!
* @brief This internal API is used to identify the settings which the user
* wants to modify in the sensor.
*
* @param[in] sub_settings : Contains the settings subset to identify particular
* group of settings which the user is interested to change.
* @param[in] settings : Co