Updated interface design and examples.

This commit is contained in:
Bosch Sensortec 2020-07-06 08:05:30 +02:00 committed by Kevin Goveas
parent 94f553126c
commit c47f06eb44
7 changed files with 890 additions and 407 deletions

View File

@ -34,7 +34,9 @@ struct bme280_dev dev;
int8_t rslt = BME280_OK;
/* Sensor_0 interface over SPI with native chip select line */
dev.dev_id = 0;
uint8_t dev_addr = 0;
dev.intf_ptr = &dev_addr;
dev.intf = BME280_SPI_INTF;
dev.read = user_spi_read;
dev.write = user_spi_write;
@ -46,8 +48,9 @@ rslt = bme280_init(&dev);
``` c
struct bme280_dev dev;
int8_t rslt = BME280_OK;
uint8_t dev_addr = BME280_I2C_ADDR_PRIM;
dev.dev_id = BME280_I2C_ADDR_PRIM;
dev.intf_ptr = &dev_addr;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
@ -119,7 +122,7 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
while (1) {
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
/* Wait for the measurement to complete and print data @25Hz */
dev->delay_ms(req_delay);
dev->delay_ms(req_delay, dev->intf_ptr);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
print_sensor_data(&comp_data);
}
@ -161,7 +164,7 @@ int8_t stream_sensor_data_normal_mode(struct bme280_dev *dev)
printf("Temperature, Pressure, Humidity\r\n");
while (1) {
/* Delay while the sensor completes a measurement */
dev->delay_ms(70);
dev->delay_ms(70, dev->intf_ptr);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
print_sensor_data(&comp_data);
}
@ -182,7 +185,7 @@ void print_sensor_data(struct bme280_data *comp_data)
### Templates for function pointers
``` c
void user_delay_ms(uint32_t period)
void user_delay_ms(uint32_t period, void *intf_ptr)
{
/*
* Return control or wait,
@ -190,12 +193,12 @@ void user_delay_ms(uint32_t period)
*/
}
int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
int8_t user_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
/*
* The parameter dev_id can be used as a variable to select which Chip Select pin has
* The parameter intf_ptr can be used as a variable to select which Chip Select pin has
* to be set low to activate the relevant device on the SPI bus
*/
@ -216,12 +219,12 @@ int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16
return rslt;
}
int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
int8_t user_spi_write(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
/*
* The parameter dev_id can be used as a variable to select which Chip Select pin has
* The parameter intf_ptr can be used as a variable to select which Chip Select pin has
* to be set low to activate the relevant device on the SPI bus
*/
@ -242,12 +245,12 @@ int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint1
return rslt;
}
int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
/*
* The parameter dev_id can be used as a variable to store the I2C address of the device
* The parameter intf_ptr can be used as a variable to store the I2C address of the device
*/
/*
@ -269,12 +272,12 @@ int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16
return rslt;
}
int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
int8_t user_i2c_write(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */
/*
* The parameter dev_id can be used as a variable to store the I2C address of the device
* The parameter intf_ptr can be used as a variable to store the I2C address of the device
*/
/*

225
bme280.c
View File

@ -31,8 +31,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme280.c
* @date 2020-01-24
* @version v3.4.3
* @date 2020-03-28
* @version v3.5.0
*
*/
@ -54,9 +54,13 @@
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status.
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t put_device_to_sleep(const struct bme280_dev *dev);
static int8_t put_device_to_sleep(struct bme280_dev *dev);
/*!
* @brief This internal API writes the power mode in the sensor.
@ -65,9 +69,13 @@ static int8_t put_device_to_sleep(const struct bme280_dev *dev);
* @param[in] sensor_mode : Variable which contains the power mode to be set.
*
* @return Result of API execution status.
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev);
static int8_t write_power_mode(uint8_t sensor_mode, struct bme280_dev *dev);
/*!
* @brief This internal API is used to validate the device pointer for
@ -76,7 +84,11 @@ static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t null_ptr_check(const struct bme280_dev *dev);
@ -90,6 +102,7 @@ static int8_t null_ptr_check(const struct bme280_dev *dev);
* @param[in] reg_data : Contains the register data to be written in the
* temporary buffer.
* @param[in] len : No of bytes of data to be written for burst write.
*
*/
static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len);
@ -100,7 +113,11 @@ static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, con
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t get_calib_data(struct bme280_dev *dev);
@ -110,6 +127,7 @@ static int8_t get_calib_data(struct bme280_dev *dev);
*
* @param[out] dev : Structure instance of bme280_dev to store the calib data.
* @param[in] reg_data : Contains the calibration data to be parsed.
*
*/
static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);
@ -119,6 +137,7 @@ static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_d
*
* @param[out] dev : Structure instance of bme280_dev to store the calib data.
* @param[in] reg_data : Contains calibration data to be parsed.
*
*/
static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);
@ -131,8 +150,8 @@ static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Compensated pressure data.
* @retval Compensated pressure data in double.
* @return Compensated pressure data in double.
*
*/
static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data);
@ -144,8 +163,8 @@ static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
* @param[in] uncomp_data : Contains the uncompensated humidity data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Compensated humidity data.
* @retval Compensated humidity data in double.
* @return Compensated humidity data in double.
*
*/
static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data);
@ -157,8 +176,8 @@ static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Compensated temperature data.
* @retval Compensated temperature data in double.
* @return Compensated temperature data in double.
*
*/
static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
struct bme280_calib_data *calib_data);
@ -172,8 +191,8 @@ static double compensate_temperature(const struct bme280_uncomp_data *uncomp_dat
* @param[in] uncomp_data : Contains the uncompensated temperature data.
* @param[in] calib_data : Pointer to calibration data structure.
*
* @return Compensated temperature data.
* @retval Compensated temperature data in integer.
* @return Compensated temperature data in integer.
*
*/
static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
struct bme280_calib_data *calib_data);
@ -185,8 +204,8 @@ static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_da
* @param[in] uncomp_data : Contains the uncompensated pressure data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Compensated pressure data.
* @retval Compensated pressure data in integer.
* @return Compensated pressure data in integer.
*
*/
static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data);
@ -198,8 +217,8 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
* @param[in] uncomp_data : Contains the uncompensated humidity data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Compensated humidity data.
* @retval Compensated humidity data in integer.
* @return Compensated humidity data in integer.
*
*/
static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
const struct bme280_calib_data *calib_data);
@ -216,8 +235,9 @@ static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data
*
* @return Indicates whether user is interested to modify the settings which
* are related to sub_settings.
* @retval True -> User wants to modify this group of settings
* @retval False -> User does not want to modify this group of settings
* @return True -> User wants to modify this group of settings
* @return False -> User does not want to modify this group of settings
*
*/
static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settings);
@ -225,11 +245,17 @@ static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settin
* @brief This API sets the humidity over sampling settings of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, const struct bme280_dev *dev);
static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, struct bme280_dev *dev);
/*!
* @brief This internal API sets the oversampling settings for pressure,
@ -237,14 +263,19 @@ static int8_t set_osr_humidity_settings(const struct bme280_settings *settings,
*
* @param[in] desired_settings : Variable used to select the settings which
* are to be set.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t set_osr_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev);
static int8_t set_osr_settings(uint8_t desired_settings, const struct bme280_settings *settings,
struct bme280_dev *dev);
/*!
* @brief This API sets the pressure and/or temperature oversampling settings
@ -253,21 +284,29 @@ static int8_t set_osr_settings(uint8_t desired_settings,
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] desired_settings: variable to select the pressure and/or
* temperature oversampling settings.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t set_osr_press_temp_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev);
struct bme280_dev *dev);
/*!
* @brief This internal API fills the pressure oversampling settings provided by
* the user in the data buffer so as to write in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[out] reg_data : Variable which is filled according to the pressure
* oversampling data provided by the user.
*
*/
static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings);
@ -275,9 +314,11 @@ static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_setti
* @brief This internal API fills the temperature oversampling settings provided
* by the user in the data buffer so as to write in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[out] reg_data : Variable which is filled according to the temperature
* oversampling data provided by the user.
*
*/
static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings);
@ -286,23 +327,30 @@ static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settin
* in the sensor according to the settings selected by the user.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] desired_settings : variable to select the filter and/or
* standby duration settings.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[in] settings : Structure instance of bme280_settings.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t set_filter_standby_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev);
struct bme280_dev *dev);
/*!
* @brief This internal API fills the filter settings provided by the user
* in the data buffer so as to write in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[out] reg_data : Variable which is filled according to the filter
* settings data provided by the user.
*
*/
static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings);
@ -310,9 +358,11 @@ static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings
* @brief This internal API fills the standby duration settings provided by the
* user in the data buffer so as to write in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be set in the sensor.
* @param[out] reg_data : Variable which is filled according to the standby
* settings data provided by the user.
*
*/
static void fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings);
@ -321,8 +371,10 @@ static void fill_standby_settings(uint8_t *reg_data, const struct bme280_setting
* and humidity), filter and standby duration settings and store in the
* device structure.
*
* @param[out] dev : Structure instance of bme280_dev.
* @param[in] settings : Pointer variable which contains the settings to
* be get in the sensor.
* @param[in] reg_data : Register data to be parsed.
*
*/
static void parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings);
@ -335,9 +387,13 @@ static void parse_device_settings(const uint8_t *reg_data, struct bme280_setting
* be set in the sensor.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
static int8_t reload_device_settings(const struct bme280_settings *settings, const struct bme280_dev *dev);
static int8_t reload_device_settings(const struct bme280_settings *settings, struct bme280_dev *dev);
/****************** Global Function Definitions *******************************/
@ -371,16 +427,18 @@ int8_t bme280_init(struct bme280_dev *dev)
/* Reset the sensor */
rslt = bme280_soft_reset(dev);
if (rslt == BME280_OK)
{
/* Read the calibration data */
rslt = get_calib_data(dev);
}
break;
}
/* Wait for 1 ms */
dev->delay_ms(1);
dev->delay_us(1000, dev->intf_ptr);
--try_count;
}
@ -397,7 +455,7 @@ int8_t bme280_init(struct bme280_dev *dev)
/*!
* @brief This API reads the data from the given register address of the sensor.
*/
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev)
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bme280_dev *dev)
{
int8_t rslt;
@ -405,7 +463,7 @@ int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const
rslt = null_ptr_check(dev);
/* Proceed if null check is fine */
if (rslt == BME280_OK)
if ((rslt == BME280_OK) && (reg_data != NULL))
{
/* If interface selected is SPI */
if (dev->intf != BME280_I2C_INTF)
@ -414,14 +472,18 @@ int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const
}
/* Read the data */
rslt = dev->read(dev->dev_id, reg_addr, reg_data, len);
dev->intf_rslt = dev->read(reg_addr, reg_data, len, dev->intf_ptr);
/* Check for communication error */
if (rslt != BME280_OK)
if (dev->intf_rslt != BME280_INTF_RET_SUCCESS)
{
rslt = BME280_E_COMM_FAIL;
}
}
else
{
rslt = BME280_E_NULL_PTR;
}
return rslt;
}
@ -430,7 +492,7 @@ int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const
* @brief This API writes the given data to the register address
* of the sensor.
*/
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bme280_dev *dev)
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, struct bme280_dev *dev)
{
int8_t rslt;
uint8_t temp_buff[20]; /* Typically not to write more than 10 registers */
@ -439,6 +501,7 @@ int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len,
{
len = 10;
}
uint16_t temp_len;
uint8_t reg_addr_cnt;
@ -474,10 +537,11 @@ int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len,
{
temp_len = len;
}
rslt = dev->write(dev->dev_id, reg_addr[0], temp_buff, temp_len);
dev->intf_rslt = dev->write(reg_addr[0], temp_buff, temp_len, dev->intf_ptr);
/* Check for communication error */
if (rslt != BME280_OK)
if (dev->intf_rslt != BME280_INTF_RET_SUCCESS)
{
rslt = BME280_E_COMM_FAIL;
}
@ -499,7 +563,7 @@ int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len,
* @brief This API sets the oversampling, filter and standby duration
* (normal mode) settings in the sensor.
*/
int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev)
int8_t bme280_set_sensor_settings(uint8_t desired_settings, struct bme280_dev *dev)
{
int8_t rslt;
uint8_t sensor_mode;
@ -511,10 +575,12 @@ int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_
if (rslt == BME280_OK)
{
rslt = bme280_get_sensor_mode(&sensor_mode, dev);
if ((rslt == BME280_OK) && (sensor_mode != BME280_SLEEP_MODE))
{
rslt = put_device_to_sleep(dev);
}
if (rslt == BME280_OK)
{
/* Check if user wants to change oversampling
@ -554,6 +620,7 @@ int8_t bme280_get_sensor_settings(struct bme280_dev *dev)
if (rslt == BME280_OK)
{
rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);
if (rslt == BME280_OK)
{
parse_device_settings(reg_data, &dev->settings);
@ -566,13 +633,14 @@ int8_t bme280_get_sensor_settings(struct bme280_dev *dev)
/*!
* @brief This API sets the power mode of the sensor.
*/
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, struct bme280_dev *dev)
{
int8_t rslt;
uint8_t last_set_mode;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if (rslt == BME280_OK)
{
rslt = bme280_get_sensor_mode(&last_set_mode, dev);
@ -598,13 +666,14 @@ int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
/*!
* @brief This API gets the power mode of the sensor.
*/
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev)
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, struct bme280_dev *dev)
{
int8_t rslt;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if (rslt == BME280_OK)
if ((rslt == BME280_OK) && (sensor_mode != NULL))
{
/* Read the power mode register */
rslt = bme280_get_regs(BME280_PWR_CTRL_ADDR, sensor_mode, 1, dev);
@ -612,6 +681,10 @@ int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev
/* Assign the power mode in the device structure */
*sensor_mode = BME280_GET_BITS_POS_0(*sensor_mode, BME280_SENSOR_MODE);
}
else
{
rslt = BME280_E_NULL_PTR;
}
return rslt;
}
@ -619,7 +692,7 @@ int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev
/*!
* @brief This API performs the soft reset of the sensor.
*/
int8_t bme280_soft_reset(const struct bme280_dev *dev)
int8_t bme280_soft_reset(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME280_RESET_ADDR;
@ -644,15 +717,15 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev)
do
{
/* As per data sheet - Table 1, startup time is 2 ms. */
dev->delay_ms(2);
dev->delay_us(2000, dev->intf_ptr);
rslt = bme280_get_regs(BME280_STATUS_REG_ADDR, &status_reg, 1, dev);
} while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));
if (status_reg & BME280_STATUS_IM_UPDATE)
{
rslt = BME280_E_NVM_COPY_FAILED;
}
}
}
@ -676,10 +749,12 @@ int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
if ((rslt == BME280_OK) && (comp_data != NULL))
{
/* Read the pressure and temperature data from the sensor */
rslt = bme280_get_regs(BME280_DATA_ADDR, reg_data, BME280_P_T_H_DATA_LEN, dev);
if (rslt == BME280_OK)
{
/* Parse the read data from the sensor */
@ -753,11 +828,13 @@ int8_t bme280_compensate_data(uint8_t sensor_comp,
/* Compensate the temperature data */
comp_data->temperature = compensate_temperature(uncomp_data, calib_data);
}
if (sensor_comp & BME280_PRESS)
{
/* Compensate the pressure data */
comp_data->pressure = compensate_pressure(uncomp_data, calib_data);
}
if (sensor_comp & BME280_HUM)
{
/* Compensate the humidity data */
@ -826,9 +903,7 @@ uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings)
* @brief This internal API sets the oversampling settings for pressure,
* temperature and humidity in the sensor.
*/
static int8_t set_osr_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev)
static int8_t set_osr_settings(uint8_t desired_settings, const struct bme280_settings *settings, struct bme280_dev *dev)
{
int8_t rslt = BME280_W_INVALID_OSR_MACRO;
@ -836,6 +911,7 @@ static int8_t set_osr_settings(uint8_t desired_settings,
{
rslt = set_osr_humidity_settings(settings, dev);
}
if (desired_settings & (BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL))
{
rslt = set_osr_press_temp_settings(desired_settings, settings, dev);
@ -847,7 +923,7 @@ static int8_t set_osr_settings(uint8_t desired_settings,
/*!
* @brief This API sets the humidity oversampling settings of the sensor.
*/
static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, const struct bme280_dev *dev)
static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, struct bme280_dev *dev)
{
int8_t rslt;
uint8_t ctrl_hum;
@ -866,6 +942,7 @@ static int8_t set_osr_humidity_settings(const struct bme280_settings *settings,
{
reg_addr = BME280_CTRL_MEAS_ADDR;
rslt = bme280_get_regs(reg_addr, &ctrl_meas, 1, dev);
if (rslt == BME280_OK)
{
rslt = bme280_set_regs(&reg_addr, &ctrl_meas, 1, dev);
@ -881,19 +958,21 @@ static int8_t set_osr_humidity_settings(const struct bme280_settings *settings,
*/
static int8_t set_osr_press_temp_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev)
struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME280_CTRL_MEAS_ADDR;
uint8_t reg_data;
rslt = bme280_get_regs(reg_addr, &reg_data, 1, dev);
if (rslt == BME280_OK)
{
if (desired_settings & BME280_OSR_PRESS_SEL)
{
fill_osr_press_settings(&reg_data, settings);
}
if (desired_settings & BME280_OSR_TEMP_SEL)
{
fill_osr_temp_settings(&reg_data, settings);
@ -912,19 +991,21 @@ static int8_t set_osr_press_temp_settings(uint8_t desired_settings,
*/
static int8_t set_filter_standby_settings(uint8_t desired_settings,
const struct bme280_settings *settings,
const struct bme280_dev *dev)
struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME280_CONFIG_ADDR;
uint8_t reg_data;
rslt = bme280_get_regs(reg_addr, &reg_data, 1, dev);
if (rslt == BME280_OK)
{
if (desired_settings & BME280_FILTER_SEL)
{
fill_filter_settings(&reg_data, settings);
}
if (desired_settings & BME280_STANDBY_SEL)
{
fill_standby_settings(&reg_data, settings);
@ -990,7 +1071,7 @@ static void parse_device_settings(const uint8_t *reg_data, struct bme280_setting
/*!
* @brief This internal API writes the power mode in the sensor.
*/
static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
static int8_t write_power_mode(uint8_t sensor_mode, struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME280_PWR_CTRL_ADDR;
@ -1016,17 +1097,19 @@ static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev
/*!
* @brief This internal API puts the device to sleep mode.
*/
static int8_t put_device_to_sleep(const struct bme280_dev *dev)
static int8_t put_device_to_sleep(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_data[4];
struct bme280_settings settings;
rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);
if (rslt == BME280_OK)
{
parse_device_settings(reg_data, &settings);
rslt = bme280_soft_reset(dev);
if (rslt == BME280_OK)
{
rslt = reload_device_settings(&settings, dev);
@ -1040,11 +1123,12 @@ static int8_t put_device_to_sleep(const struct bme280_dev *dev)
* @brief This internal API reloads the already existing device settings in
* the sensor after soft reset.
*/
static int8_t reload_device_settings(const struct bme280_settings *settings, const struct bme280_dev *dev)
static int8_t reload_device_settings(const struct bme280_settings *settings, struct bme280_dev *dev)
{
int8_t rslt;
rslt = set_osr_settings(BME280_ALL_SETTINGS_SEL, settings, dev);
if (rslt == BME280_OK)
{
rslt = set_filter_standby_settings(BME280_ALL_SETTINGS_SEL, settings, dev);
@ -1073,6 +1157,7 @@ static double compensate_temperature(const struct bme280_uncomp_data *uncomp_dat
var2 = (var2 * var2) * ((double)calib_data->dig_t3);
calib_data->t_fine = (int32_t)(var1 + var2);
temperature = (var1 + var2) / 5120.0;
if (temperature < temperature_min)
{
temperature = temperature_min;
@ -1115,6 +1200,7 @@ static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
var1 = ((double)calib_data->dig_p9) * pressure * pressure / 2147483648.0;
var2 = pressure * ((double)calib_data->dig_p8) / 32768.0;
pressure = pressure + (var1 + var2 + ((double)calib_data->dig_p7)) / 16.0;
if (pressure < pressure_min)
{
pressure = pressure_min;
@ -1238,6 +1324,7 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
var2 = (((int64_t)calib_data->dig_p8) * var4) / 524288;
var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_p7) * 16);
pressure = (uint32_t)(((var4 / 2) * 100) / 128);
if (pressure < pressure_min)
{
pressure = pressure_min;
@ -1286,6 +1373,7 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
{
var5 = (uint32_t)((uint32_t)1048576) - uncomp_data->pressure;
pressure = ((uint32_t)(var5 - (uint32_t)(var2 / 4096))) * 3125;
if (pressure < 0x80000000)
{
pressure = (pressure << 1) / ((uint32_t)var1);
@ -1294,9 +1382,11 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data
{
pressure = (pressure / (uint32_t)var1) * 2;
}
var1 = (((int32_t)calib_data->dig_p9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_p8)) / 8192;
pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_p7) / 16));
if (pressure < pressure_min)
{
pressure = pressure_min;
@ -1345,6 +1435,7 @@ static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data
var5 = (var5 < 0 ? 0 : var5);
var5 = (var5 > 419430400 ? 419430400 : var5);
humidity = (uint32_t)(var5 / 4096);
if (humidity > humidity_max)
{
humidity = humidity_max;
@ -1368,6 +1459,7 @@ static int8_t get_calib_data(struct bme280_dev *dev)
/* Read the calibration data from the sensor */
rslt = bme280_get_regs(reg_addr, calib_data, BME280_TEMP_PRESS_CALIB_DATA_LEN, dev);
if (rslt == BME280_OK)
{
/* Parse temperature and pressure calibration data and store
@ -1378,6 +1470,7 @@ static int8_t get_calib_data(struct bme280_dev *dev)
/* Read the humidity calibration data from the sensor */
rslt = bme280_get_regs(reg_addr, calib_data, BME280_HUMIDITY_CALIB_DATA_LEN, dev);
if (rslt == BME280_OK)
{
/* Parse humidity calibration data and store it in
@ -1481,7 +1574,7 @@ static int8_t null_ptr_check(const struct bme280_dev *dev)
{
int8_t rslt;
if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL))
if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_us == NULL))
{
/* Device structure pointer is not valid */
rslt = BME280_E_NULL_PTR;

243
bme280.h
View File

@ -31,8 +31,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme280.h
* @date 2020-01-24
* @version v3.4.3
* @date 2020-03-28
* @version v3.5.0
*
*/
@ -41,8 +41,11 @@
*/
/*!
* @defgroup BME280 SENSOR API
* @defgroup bme280 BME280
* @brief <a href="https://www.bosch-sensortec.com/bst/products/all_products/bme280">Product Overview</a>
* and <a href="https://github.com/BoschSensortec/BME280_driver">Sensor API Source Code</a>
*/
#ifndef BME280_H_
#define BME280_H_
@ -54,47 +57,97 @@ extern "C" {
/* Header includes */
#include "bme280_defs.h"
/**
* \ingroup bme280
* \defgroup bme280ApiInit Initialization
* @brief Initialize the sensor and device structure
*/
/*!
* @brief This API is the entry point.
* It reads the chip-id and calibration data from the sensor.
* \ingroup bme280ApiInit
* \page bme280_api_bme280_init bme280_init
* \code
* int8_t bme280_init(struct bme280_dev *dev);
* \endcode
* @details This API reads the chip-id of the sensor which is the first step to
* verify the sensor and also calibrates the sensor
* As this API is the entry point, call this API before using other APIs.
*
* @param[in,out] dev : Structure instance of bme280_dev
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
* @return Result of API execution status.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_init(struct bme280_dev *dev);
/*!
* @brief This API writes the given data to the register address
* of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be written.
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the sensor.
* @param[in] len : No of bytes of data to write..
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
/**
* \ingroup bme280
* \defgroup bme280ApiRegister Registers
* @brief Generic API for accessing sensor registers
*/
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bme280_dev *dev);
/*!
* @brief This API reads the data from the given register address of the sensor.
* \ingroup bme280ApiRegister
* \page bme280_api_bme280_set_regs bme280_set_regs
* \code
* int8_t bme280_set_regs(const uint8_t reg_addr, const uint8_t *reg_data, uint8_t len, struct bme280_dev *dev);
* \endcode
* @details This API writes the given data to the register address of the sensor
*
* @param[in] reg_addr : Register addresses to where the data is to be written
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the reg_addr of sensor.
* @param[in] len : No of bytes of data to write
* @param[in,out] dev : Structure instance of bme280_dev
*
* @return Result of API execution status.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, struct bme280_dev *dev);
/*!
* \ingroup bme280ApiRegister
* \page bme280_api_bme280_get_regs bme280_get_regs
* \code
* int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint8_t len, struct bme280_dev *dev);
* \endcode
* @details This API reads the data from the given register address of sensor.
*
* @param[in] reg_addr : Register address from where the data to be read
* @param[out] reg_data : Pointer to data buffer to store the read data.
* @param[in] len : No of bytes of data to be read.
* @param[in] dev : Structure instance of bme280_dev.
* @param[in,out] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev);
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, struct bme280_dev *dev);
/**
* \ingroup bme280
* \defgroup bme280ApiSensorSettings Sensor Settings
* @brief Generic API for accessing sensor settings
*/
/*!
* @brief This API sets the oversampling, filter and standby duration
* \ingroup bme280ApiSensorSettings
* \page bme280_api_bme280_set_sensor_settings bme280_set_sensor_settings
* \code
* int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev);
* \endcode
* @details This API sets the oversampling, filter and standby duration
* (normal mode) settings in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
@ -114,23 +167,47 @@ int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const
* BME280_STANDBY_SEL | To set standby duration setting.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev);
int8_t bme280_set_sensor_settings(uint8_t desired_settings, struct bme280_dev *dev);
/*!
* @brief This API gets the oversampling, filter and standby duration
* \ingroup bme280ApiSensorSettings
* \page bme280_api_bme280_get_sensor_settings bme280_get_sensor_settings
* \code
* int8_t bme280_get_sensor_settings(struct bme280_dev *dev);
* \endcode
* @details This API gets the oversampling, filter and standby duration
* (normal mode) settings from the sensor.
*
* @param[in,out] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_get_sensor_settings(struct bme280_dev *dev);
/**
* \ingroup bme280
* \defgroup bme280ApiSensorMode Sensor Mode
* @brief Generic API for configuring sensor power mode
*/
/*!
* @brief This API sets the power mode of the sensor.
* \ingroup bme280ApiSensorMode
* \page bme280_api_bme280_set_sensor_mode bme280_set_sensor_mode
* \code
* int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev);
* \endcode
* @details This API sets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] sensor_mode : Variable which contains the power mode to be set.
@ -142,12 +219,21 @@ int8_t bme280_get_sensor_settings(struct bme280_dev *dev);
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev);
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, struct bme280_dev *dev);
/*!
* @brief This API gets the power mode of the sensor.
* \ingroup bme280ApiSensorMode
* \page bme280_api_bme280_get_sensor_mode bme280_get_sensor_mode
* \code
* int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev);
* \endcode
* @details This API gets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[out] sensor_mode : Pointer variable to store the power mode.
@ -159,22 +245,52 @@ int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, struct bme280_dev *dev);
/**
* \ingroup bme280
* \defgroup bme280ApiSystem System
* @brief API that performs system-level operations
*/
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev);
/*!
* @brief This API performs the soft reset of the sensor.
* \ingroup bme280ApiSystem
* \page bme280_api_bme280_soft_reset bme280_soft_reset
* \code
* int8_t bme280_soft_reset(struct bme280_dev *dev);
* \endcode
* @details This API soft-resets the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in,out] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status.
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_soft_reset(const struct bme280_dev *dev);
int8_t bme280_soft_reset(struct bme280_dev *dev);
/**
* \ingroup bme280
* \defgroup bme280ApiSensorData Sensor Data
* @brief Data processing of sensor
*/
/*!
* @brief This API reads the pressure, temperature and humidity data from the
* \ingroup bme280ApiSensorData
* \page bme280_api_bme280_get_sensor_data bme280_get_sensor_data
* \code
* int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev);
* \endcode
* @details This API reads the pressure, temperature and humidity data from the
* sensor, compensates the data and store it in the bme280_data structure
* instance passed by the user.
*
@ -192,22 +308,40 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev);
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev);
/*!
* @brief This API is used to parse the pressure, temperature and
* \ingroup bme280ApiSensorData
* \page bme280_api_bme280_parse_sensor_data bme280_parse_sensor_data
* \code
* void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data);
* \endcode
* @details This API is used to parse the pressure, temperature and
* humidity data and store it in the bme280_uncomp_data structure instance.
*
* @param[in] reg_data : Contains register data which needs to be parsed
* @param[out] uncomp_data : Contains the uncompensated pressure, temperature
* and humidity data.
*
*/
void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data);
/*!
* @brief This API is used to compensate the pressure and/or
* \ingroup bme280ApiSensorData
* \page bme280_api_bme280_compensate_data bme280_compensate_data
* \code
* int8_t bme280_compensate_data(uint8_t sensor_comp,
* const struct bme280_uncomp_data *uncomp_data,
* struct bme280_data *comp_data,
* struct bme280_calib_data *calib_data);
* \endcode
* @details This API is used to compensate the pressure and/or
* temperature and/or humidity data according to the component selected by the
* user.
*
@ -220,14 +354,29 @@ void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status.
* @retval zero -> Success / -ve value -> Error
*
* @retval 0 -> Success.
* @retval > 0 -> Warning.
* @retval < 0 -> Fail.
*
*/
int8_t bme280_compensate_data(uint8_t sensor_comp,
const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data,
struct bme280_calib_data *calib_data);
/**
* \ingroup bme280
* \defgroup bme280ApiSensorDelay Sensor Delay
* @brief Generic API for measuring sensor delay
*/
/*!
* \ingroup bme280ApiSensorDelay
* \page bme280_api_bme280_cal_meas_delay bme280_cal_meas_delay
* \code
* uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings);
* \endcode
* @brief This API is used to calculate the maximum delay in milliseconds required for the
* temperature/pressure/humidity(which ever are enabled) measurement to complete.
* The delay depends upon the number of sensors enabled and their oversampling configuration.
@ -235,8 +384,8 @@ int8_t bme280_compensate_data(uint8_t sensor_comp,
* @param[in] settings : contains the oversampling configurations.
*
* @return delay required in milliseconds.
*
*/
uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings);
#ifdef __cplusplus

View File

@ -31,19 +31,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme280_defs.h
* @date 2020-01-24
* @version v3.4.3
* @date 2020-03-28
* @version v3.5.0
*
*/
/*! @file bme280_defs.h
* @brief Sensor driver for BME280 sensor
*/
/*!
* @defgroup BME280 SENSOR API
* @brief
*/
#ifndef BME280_DEFS_H_
#define BME280_DEFS_H_
@ -93,9 +85,9 @@
/********************************************************/
#ifndef BME280_64BIT_ENABLE /* Check if 64-bit integer (using BME280_64BIT_ENABLE) is enabled */
#ifndef BME280_32BIT_ENABLE /* Check if 32-bit integer (using BME280_32BIT_ENABLE) is enabled */
#ifndef BME280_FLOAT_ENABLE /* If any of the integer data types not enabled then enable BME280_FLOAT_ENABLE */
#ifndef BME280_64BIT_ENABLE /*< Check if 64-bit integer (using BME280_64BIT_ENABLE) is enabled */
#ifndef BME280_32BIT_ENABLE /*< Check if 32-bit integer (using BME280_32BIT_ENABLE) is enabled */
#ifndef BME280_FLOAT_ENABLE /*< If any of the integer data types not enabled then enable BME280_FLOAT_ENABLE */
#define BME280_FLOAT_ENABLE
#endif
#endif
@ -108,6 +100,20 @@
#define FALSE UINT8_C(0)
#endif
/**
* BME280_INTF_RET_TYPE is the read/write interface return type which can be overwritten by the build system.
*/
#ifndef BME280_INTF_RET_TYPE
#define BME280_INTF_RET_TYPE int8_t
#endif
/**
* The last error code from read/write interface is stored in the device structure as intf_rslt.
*/
#ifndef BME280_INTF_RET_SUCCESS
#define BME280_INTF_RET_SUCCESS INT8_C(0)
#endif
/**\name I2C addresses */
#define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
#define BME280_I2C_ADDR_SEC UINT8_C(0x77)
@ -239,50 +245,122 @@
* @brief Interface selection Enums
*/
enum bme280_intf {
/*! SPI interface */
/*< SPI interface */
BME280_SPI_INTF,
/*! I2C interface */
/*< I2C interface */
BME280_I2C_INTF
};
/*!
* @brief Type definitions
*/
typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);
typedef void (*bme280_delay_fptr_t)(uint32_t period);
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific read functions of the user
*
* @param[in] reg_addr : Register address from which data is read.
* @param[out] reg_data : Pointer to data buffer where read data is stored.
* @param[in] len : Number of bytes of data to be read.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs.
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*
*/
typedef BME280_INTF_RET_TYPE (*bme280_read_fptr_t)(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr);
/*!
* @brief Bus communication function pointer which should be mapped to
* the platform specific write functions of the user
*
* @param[in] reg_addr : Register address to which the data is written.
* @param[in] reg_data : Pointer to data buffer in which data to be written
* is stored.
* @param[in] len : Number of bytes of data to be written.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
* @retval 0 -> Success.
* @retval Non zero value -> Fail.
*
*/
typedef BME280_INTF_RET_TYPE (*bme280_write_fptr_t)(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len,
void *intf_ptr);
/*!
* @brief Delay function pointer which should be mapped to
* delay function of the user
*
* @param[in] period : Delay in microseconds.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
*/
typedef void (*bme280_delay_us_fptr_t)(uint32_t period, void *intf_ptr);
/*!
* @brief Calibration data
*/
struct bme280_calib_data
{
/**
* @ Trim Variables
*/
/**@{*/
/*< Calibration coefficient for the temperature sensor */
uint16_t dig_t1;
int16_t dig_t2;
int16_t dig_t3;
uint16_t dig_p1;
int16_t dig_p2;
int16_t dig_p3;
int16_t dig_p4;
int16_t dig_p5;
int16_t dig_p6;
int16_t dig_p7;
int16_t dig_p8;
int16_t dig_p9;
uint8_t dig_h1;
int16_t dig_h2;
uint8_t dig_h3;
int16_t dig_h4;
int16_t dig_h5;
int8_t dig_h6;
int32_t t_fine;
/**@}*/
/*< Calibration coefficient for the temperature sensor */
int16_t dig_t2;
/*< Calibration coefficient for the temperature sensor */
int16_t dig_t3;
/*< Calibration coefficient for the pressure sensor */
uint16_t dig_p1;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p2;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p3;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p4;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p5;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p6;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p7;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p8;
/*< Calibration coefficient for the pressure sensor */
int16_t dig_p9;
/*< Calibration coefficient for the humidity sensor */
uint8_t dig_h1;
/*< Calibration coefficient for the humidity sensor */
int16_t dig_h2;
/*< Calibration coefficient for the humidity sensor */
uint8_t dig_h3;
/*< Calibration coefficient for the humidity sensor */
int16_t dig_h4;
/*< Calibration coefficient for the humidity sensor */
int16_t dig_h5;
/*< Calibration coefficient for the humidity sensor */
int8_t dig_h6;
/*< Variable to store the intermediate temperature coefficient */
int32_t t_fine;
};
/*!
@ -292,28 +370,28 @@ struct bme280_calib_data
#ifdef BME280_FLOAT_ENABLE
struct bme280_data
{
/*! Compensated pressure */
/*< Compensated pressure */
double pressure;
/*! Compensated temperature */
/*< Compensated temperature */
double temperature;
/*! Compensated humidity */
/*< Compensated humidity */
double humidity;
};
#else
struct bme280_data
{
/*! Compensated pressure */
/*< Compensated pressure */
uint32_t pressure;
/*! Compensated temperature */
/*< Compensated temperature */
int32_t temperature;
/*! Compensated humidity */
/*< Compensated humidity */
uint32_t humidity;
};
#endif /* BME280_USE_FLOATING_POINT */
#endif /*! BME280_USE_FLOATING_POINT */
/*!
* @brief bme280 sensor structure which comprises of uncompensated temperature,
@ -321,13 +399,13 @@ struct bme280_data
*/
struct bme280_uncomp_data
{
/*! un-compensated pressure */
/*< un-compensated pressure */
uint32_t pressure;
/*! un-compensated temperature */
/*< un-compensated temperature */
uint32_t temperature;
/*! un-compensated humidity */
/*< un-compensated humidity */
uint32_t humidity;
};
@ -337,19 +415,19 @@ struct bme280_uncomp_data
*/
struct bme280_settings
{
/*! pressure oversampling */
/*< pressure oversampling */
uint8_t osr_p;
/*! temperature oversampling */
/*< temperature oversampling */
uint8_t osr_t;
/*! humidity oversampling */
/*< humidity oversampling */
uint8_t osr_h;
/*! filter coefficient */
/*< filter coefficient */
uint8_t filter;
/*! standby time */
/*< standby time */
uint8_t standby_time;
};
@ -358,31 +436,35 @@ struct bme280_settings
*/
struct bme280_dev
{
/*! Chip Id */
/*< Chip Id */
uint8_t chip_id;
/*! Device Id */
uint8_t dev_id;
/*< Interface function pointer used to enable the device address for I2C and chip selection for SPI */
void *intf_ptr;
/*! SPI/I2C interface */
/*< Interface Selection
* For SPI, intf = BME280_SPI_INTF
* For I2C, intf = BME280_I2C_INTF
* */
enum bme280_intf intf;
/*! Read function pointer */
bme280_com_fptr_t read;
/*< Read function pointer */
bme280_read_fptr_t read;
/*! Write function pointer */
bme280_com_fptr_t write;
/*< Write function pointer */
bme280_write_fptr_t write;
/*! Delay function pointer */
bme280_delay_fptr_t delay_ms;
/*< Delay function pointer */
bme280_delay_us_fptr_t delay_us;
/*! Trim data */
/*< Trim data */
struct bme280_calib_data calib_data;
/*! Sensor settings */
/*< Sensor settings */
struct bme280_settings settings;
/*< Variable to store result of read/write function */
BME280_INTF_RET_TYPE intf_rslt;
};
#endif /* BME280_DEFS_H_ */
/** @}*/
/** @}*/

View File

@ -4,7 +4,15 @@
* tested: NanoPi NEO.
* Use like: ./bme280 /dev/iic0
*/
#include "bme280.h"
#ifdef __KERNEL__
#include <sys/ioctl.h>
#include <dev/iicbus/iic.h>
#endif
/******************************************************************************/
/*! System header files */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@ -12,67 +20,175 @@
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <dev/iicbus/iic.h>
/******************************************************************************/
/*! Own header files */
#include <iic.h>
#include "bme280.h"
int fd;
/******************************************************************************/
/*! Structures */
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
void user_delay_ms(uint32_t period);
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
void print_sensor_data(struct bme280_data *comp_data);
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
/* Structure that contains identifier details used in example */
struct identifier
{
/* Variable to hold device address */
uint8_t dev_addr;
/* Variable that contains file descriptor */
int8_t fd;
};
/******************************************************************************/
/*! Functions */
/*!
* @brief Function for reading the sensor's registers through I2C bus.
*
* @param[in] reg_addr : Register address.
* @param[out] data : Pointer to the data buffer to store the read data.
* @param[in] len : No of bytes to read.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs.
*
* @return Status of execution
*
* @retval 0 -> Success
* @retval > 0 -> Failure Info
*
*/
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function that creates a mandatory delay required in some of the APIs.
*
* @param[in] period : Delay in microseconds.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
* @return void.
*
*/
void user_delay_us(uint32_t period, void *intf_ptr);
/*!
* @brief Function for writing the sensor's registers through I2C bus.
*
* @param[in] reg_addr : Register address.
* @param[in] data : Pointer to the data buffer whose value is to be written.
* @param[in] len : No of bytes to write.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
* @return Status of execution
*
* @retval BME280_OK -> Success
* @retval BME280_E_COMM_FAIL -> Communication failure.
*
*/
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function for print the temperature, humidity and pressure data.
*
* @param[out] comp_data : Structure instance of bme280_data
*
* @note Sensor data whose can be read
*
* sens_list
* --------------
* Pressure
* Temperature
* Humidity
*
*/
static void print_sensor_data(struct bme280_data *comp_data);
/*!
* @brief Function reads temperature, humidity and pressure data in forced mode.
*
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
*
* @retval BME280_OK - Success.
* @retval BME280_E_NULL_PTR - Error: Null pointer error
* @retval BME280_E_COMM_FAIL - Error: Communication fail error
* @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed
*
*/
static int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
/*!
* @brief This function reading the sensor's registers through I2C bus.
*/
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)
{
struct identifier id;
id = *((struct identifier *)intf_ptr);
struct iic_msg msgs[2] = {
{id << 1 | IIC_M_WR, IIC_M_WR, 1, &reg_addr},
{id << 1 | IIC_M_RD, IIC_M_RD, len, data},
{ id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, &reg_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data },
};
struct iic_rdwr_data rdwr_data = { msgs, 2 };
int error = ioctl(fd, I2CRDWR, &rdwr_data);
if (error) {
int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
if (error)
{
return BME280_E_COMM_FAIL;
}
return BME280_OK;
}
void user_delay_ms(uint32_t period)
/*!
* @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
* APIs
*/
void user_delay_us(uint32_t period, void *intf_ptr)
{
usleep(period * 1000);
usleep(period);
}
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
/*!
* @brief This function for writing the sensor's registers through I2C bus.
*/
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)
{
struct identifier id;
id = *((struct identifier *)intf_ptr);
uint8_t *buf = malloc((1 + len) * sizeof(uint8_t));
if (buf == NULL) {
if (buf == NULL)
{
return BME280_E_COMM_FAIL;
}
buf[0] = reg_addr;
for (int i = 0; i < len; i++) {
for (uint8_t i = 0; i < len; i++)
{
buf[i + 1] = data[i];
}
struct iic_msg msg;
msg.slave = id << 1 | !IIC_M_RD;
msg.slave = id.dev_addr << 1 | !IIC_M_RD;
msg.flags = !IIC_M_RD;
msg.len = (1 + len) * sizeof(uint8_t);
msg.buf = buf;
struct iic_rdwr_data rdwr_data = { &msg, 1 };
int error = ioctl(fd, I2CRDWR, &rdwr_data);
if (error) {
int error = ioctl(id.fd, I2CRDWR, &rdwr_data);
if (error)
{
free(buf);
return BME280_E_COMM_FAIL;
}
@ -81,10 +197,13 @@ int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
return BME280_OK;
}
/*!
* @brief This API used to print the sensor temperature, pressure and humidity data.
*/
void print_sensor_data(struct bme280_data *comp_data)
{
float temp, press, hum;
#ifdef BME280_FLOAT_ENABLE
temp = comp_data->temperature;
press = 0.01 * comp_data->pressure;
@ -103,7 +222,9 @@ void print_sensor_data(struct bme280_data *comp_data)
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
}
/*!
* @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
*/
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
{
int8_t rslt;
@ -122,10 +243,12 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
return rslt;
}
printf("Temperature, Pressure, Humidity\n");
/* Continuously stream sensor data */
while (1)
{
@ -135,56 +258,59 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
break;
}
/* Wait for the measurement to complete and print data @25Hz */
dev->delay_ms(40);
dev->delay_us(40000, dev->intf_ptr);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
break;
}
print_sensor_data(&comp_data);
dev->delay_ms(1000);
dev->delay_us(1000000, dev->intf_ptr);
}
return rslt;
}
/*!
* @brief This function starts execution of the program.
*/
int main(int argc, char* argv[])
{
struct bme280_dev dev;
int8_t rslt = BME280_OK;
struct identifier id;
if (argc < 2)
{
fprintf(stderr, "Missing argument for i2c bus.\n");
exit(1);
}
// make sure to select BME280_I2C_ADDR_PRIM
// or BME280_I2C_ADDR_SEC as needed
dev.dev_id =
#if 1
BME280_I2C_ADDR_PRIM
#else
BME280_I2C_ADDR_SEC
#endif
;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
if ((fd = open(argv[1], O_RDWR)) < 0)
if ((id.fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}
/*
* make sure to select BME280_I2C_ADDR_PRIM
* or BME280_I2C_ADDR_SEC as needed
*/
id.dev_addr = BME280_I2C_ADDR_PRIM;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_us = user_delay_us;
/* Update interface pointer with the structure that contains both device address and file descriptor */
dev.intf_ptr = &id;
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
{
@ -198,5 +324,6 @@ int main(int argc, char* argv[])
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
exit(1);
}
return 0;
}

View File

@ -1,13 +1,18 @@
/*
* Copyright (C) 2020 Bosch Sensortec GmbH
*
* The license is available at root folder
/**\
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
**/
/**
* \ingroup bme280
* \defgroup bme280Examples Examples
* @brief Reference Examples
*/
/*!
* @ingroup bme280GroupExample
* @defgroup bme280GroupExample linux_userspace
* @ingroup bme280Examples
* @defgroup bme280GroupExampleLU linux_userspace
* @brief Linux userspace test code, simple and mose code directly from the doco.
* compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280
* tested: Raspberry Pi.
@ -33,9 +38,18 @@
/*! Own header files */
#include "bme280.h"
/*****************************************************************************/
/*! Global variables */
int fd;
/******************************************************************************/
/*! Structures */
/* Structure that contains identifier details used in example */
struct identifier
{
/* Variable to hold device address */
uint8_t dev_addr;
/* Variable that contains file descriptor */
int8_t fd;
};
/****************************************************************************/
/*! Functions */
@ -43,11 +57,13 @@ int fd;
/*!
* @brief Function that creates a mandatory delay required in some of the APIs.
*
* @param[in] period : The required wait time in microseconds.
* @param[in] period : Delay in microseconds.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
* @return void.
*
*/
void user_delay_ms(uint32_t period);
void user_delay_us(uint32_t period, void *intf_ptr);
/*!
* @brief Function for print the temperature, humidity and pressure data.
@ -68,10 +84,11 @@ void print_sensor_data(struct bme280_data *comp_data);
/*!
* @brief Function for reading the sensor's registers through I2C bus.
*
* @param[in] id : Sensor I2C address.
* @param[in] reg_addr : Register address.
* @param[out] data : Pointer to the data buffer to store the read data.
* @param[in] len : No of bytes to read.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs.
*
* @return Status of execution
*
@ -79,15 +96,16 @@ void print_sensor_data(struct bme280_data *comp_data);
* @retval > 0 -> Failure Info
*
*/
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function for writing the sensor's registers through I2C bus.
*
* @param[in] id : Sensor I2C address.
* @param[in] reg_addr : Register address.
* @param[in] data : Pointer to the data buffer whose value is to be written.
* @param[in] len : No of bytes to write.
* @param[in, out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related call backs
*
* @return Status of execution
*
@ -95,7 +113,7 @@ int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
* @retval BME280_E_COMM_FAIL -> Communication failure.
*
*/
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);
/*!
* @brief Function reads temperature, humidity and pressure data in forced mode.
@ -119,6 +137,8 @@ int main(int argc, char* argv[])
{
struct bme280_dev dev;
struct identifier id;
/* Variable to define the result */
int8_t rslt = BME280_OK;
@ -128,29 +148,32 @@ int main(int argc, char* argv[])
exit(1);
}
/* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */
dev.dev_id = BME280_I2C_ADDR_PRIM;
/* dev.dev_id = BME280_I2C_ADDR_SEC; */
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
if ((fd = open(argv[1], O_RDWR)) < 0)
if ((id.fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}
#ifdef __KERNEL__
if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
if (ioctl(id.fd, I2C_SLAVE, id.dev_addr) < 0)
{
fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
#endif
/* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */
id.dev_addr = BME280_I2C_ADDR_PRIM;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_us = user_delay_us;
/* Update interface pointer with the structure that contains both device address and file descriptor */
dev.intf_ptr = &id;
/* Initialize the bme280 */
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
@ -172,10 +195,14 @@ int main(int argc, char* argv[])
/*!
* @brief This function reading the sensor's registers through I2C bus.
*/
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)
{
write(fd, &reg_addr, 1);
read(fd, data, len);
struct identifier id;
id = *((struct identifier *)intf_ptr);
write(id.fd, &reg_addr, 1);
read(id.fd, data, len);
return 0;
}
@ -184,23 +211,25 @@ int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
* @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
* APIs
*/
void user_delay_ms(uint32_t period)
void user_delay_us(uint32_t period, void *intf_ptr)
{
/* Milliseconds convert to microseconds */
usleep(period * 1000);
usleep(period);
}
/*!
* @brief This function for writing the sensor's registers through I2C bus.
*/
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)
{
int8_t *buf;
uint8_t *buf;
struct identifier id;
id = *((struct identifier *)intf_ptr);
buf = malloc(len + 1);
buf[0] = reg_addr;
memcpy(buf + 1, data, len);
if (write(fd, buf, len + 1) < len)
if (write(id.fd, buf, len + 1) < (uint16_t)len)
{
return BME280_E_COMM_FAIL;
}
@ -287,7 +316,7 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
}
/* Wait for the measurement to complete and print data */
dev->delay_ms(req_delay);
dev->delay_us(req_delay, dev->intf_ptr);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{