From c47f06eb44fc96970f0abfcc941ec16425b2a9e6 Mon Sep 17 00:00:00 2001 From: Bosch Sensortec Date: Mon, 6 Jul 2020 08:05:30 +0200 Subject: [PATCH] Updated interface design and examples. --- LICENSE | 10 +- README.md | 29 +-- bme280.c | 263 +++++++++++++++++--------- bme280.h | 243 +++++++++++++++++++----- bme280_defs.h | 372 ++++++++++++++++++++++--------------- examples/bsd_userspace.c | 229 ++++++++++++++++++----- examples/linux_userspace.c | 113 ++++++----- 7 files changed, 871 insertions(+), 388 deletions(-) diff --git a/LICENSE b/LICENSE index df241f2..315dfa2 100644 --- a/LICENSE +++ b/LICENSE @@ -6,15 +6,15 @@ 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. + 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. + 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. + 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 diff --git a/README.md b/README.md index 312aeff..0a0e398 100644 --- a/README.md +++ b/README.md @@ -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 */ /* diff --git a/bme280.c b/bme280.c index c3eabb5..cabefc2 100644 --- a/bme280.c +++ b/bme280.c @@ -30,9 +30,9 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * -* @file bme280.c -* @date 2020-01-24 -* @version v3.4.3 +* @file bme280.c +* @date 2020-03-28 +* @version v3.5.0 * */ @@ -43,10 +43,10 @@ /**\name Internal macros */ /* To identify osr settings selected by user */ -#define OVERSAMPLING_SETTINGS UINT8_C(0x07) +#define OVERSAMPLING_SETTINGS UINT8_C(0x07) /* To identify filter and standby settings selected by user */ -#define FILTER_STANDBY_SETTINGS UINT8_C(0x18) +#define FILTER_STANDBY_SETTINGS UINT8_C(0x18) /*! * @brief This internal API puts the device to sleep mode. @@ -54,20 +54,28 @@ * @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. * - * @param[in] dev : Structure instance of bme280_dev. + * @param[in] dev : Structure instance of bme280_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); @@ -84,12 +96,13 @@ static int8_t null_ptr_check(const struct bme280_dev *dev); * @brief This internal API interleaves the register address between the * register data buffer for burst write operation. * - * @param[in] reg_addr : Contains the register address array. + * @param[in] reg_addr : Contains the register address array. * @param[out] temp_buff : Contains the temporary buffer to store the * register data and register address. - * @param[in] reg_data : Contains the register data to be written in the + * @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. + * @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); @@ -108,8 +125,9 @@ static int8_t get_calib_data(struct bme280_dev *dev); * @brief This internal API is used to parse the temperature and * pressure calibration data and store it in the device structure. * - * @param[out] dev : Structure instance of bme280_dev to store the calib data. + * @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); @@ -117,8 +135,9 @@ static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_d * @brief This internal API is used to parse the humidity calibration data * and store it in device structure. * - * @param[out] dev : Structure instance of bme280_dev to store the calib data. + * @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); @@ -129,10 +148,10 @@ static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev * return the compensated pressure data in double data type. * * @param[in] uncomp_data : Contains the uncompensated pressure data. - * @param[in] calib_data : Pointer to the calibration data structure. + * @param[in] calib_data : Pointer to the calibration data structure. + * + * @return Compensated pressure data in double. * - * @return Compensated pressure data. - * @retval Compensated pressure data in double. */ static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data, const struct bme280_calib_data *calib_data); @@ -142,10 +161,10 @@ static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data, * return the compensated humidity data in double data type. * * @param[in] uncomp_data : Contains the uncompensated humidity data. - * @param[in] calib_data : Pointer to the calibration data structure. + * @param[in] calib_data : Pointer to the calibration data structure. + * + * @return Compensated humidity data in double. * - * @return Compensated humidity data. - * @retval Compensated humidity data in double. */ static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data, const struct bme280_calib_data *calib_data); @@ -155,10 +174,10 @@ static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data, * return the compensated temperature data in double data type. * * @param[in] uncomp_data : Contains the uncompensated temperature data. - * @param[in] calib_data : Pointer to calibration data structure. + * @param[in] calib_data : Pointer to calibration data structure. + * + * @return Compensated temperature data in double. * - * @return Compensated temperature data. - * @retval Compensated temperature data in double. */ static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data, struct bme280_calib_data *calib_data); @@ -170,10 +189,10 @@ static double compensate_temperature(const struct bme280_uncomp_data *uncomp_dat * return the compensated temperature data in integer data type. * * @param[in] uncomp_data : Contains the uncompensated temperature data. - * @param[in] calib_data : Pointer to calibration data structure. + * @param[in] calib_data : Pointer to calibration data structure. + * + * @return Compensated temperature data in integer. * - * @return Compensated temperature data. - * @retval Compensated temperature data in integer. */ static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data, struct bme280_calib_data *calib_data); @@ -183,10 +202,10 @@ static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_da * return the compensated pressure data in integer data type. * * @param[in] uncomp_data : Contains the uncompensated pressure data. - * @param[in] calib_data : Pointer to the calibration data structure. + * @param[in] calib_data : Pointer to the calibration data structure. + * + * @return Compensated pressure data in integer. * - * @return Compensated pressure data. - * @retval Compensated pressure data in integer. */ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data, const struct bme280_calib_data *calib_data); @@ -196,10 +215,10 @@ static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data * return the compensated humidity data in integer data type. * * @param[in] uncomp_data : Contains the uncompensated humidity data. - * @param[in] calib_data : Pointer to the calibration data structure. + * @param[in] calib_data : Pointer to the calibration data structure. + * + * @return Compensated humidity data in integer. * - * @return Compensated humidity data. - * @retval Compensated humidity data in integer. */ static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data, const struct bme280_calib_data *calib_data); @@ -210,26 +229,33 @@ static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data * @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 + * @param[in] sub_settings : Contains the settings subset to identify particular * group of settings which the user is interested to change. * @param[in] desired_settings : Contains the user specified settings. * * @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); /*! - * @brief This API sets the humidity oversampling settings of the sensor. + * @brief This API sets the humidity over sampling settings of the sensor. * - * @param[in] dev : Structure instance of bme280_dev. + * @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] dev : Structure instance of bme280_dev. + * @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(®_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, ®_data, 1, dev); + if (rslt == BME280_OK) { if (desired_settings & BME280_OSR_PRESS_SEL) { fill_osr_press_settings(®_data, settings); } + if (desired_settings & BME280_OSR_TEMP_SEL) { fill_osr_temp_settings(®_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, ®_data, 1, dev); + if (rslt == BME280_OK) { if (desired_settings & BME280_FILTER_SEL) { fill_filter_settings(®_data, settings); } + if (desired_settings & BME280_STANDBY_SEL) { fill_standby_settings(®_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; diff --git a/bme280.h b/bme280.h index 9f9f5af..fd95990 100644 --- a/bme280.h +++ b/bme280.h @@ -30,9 +30,9 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * -* @file bme280.h -* @date 2020-01-24 -* @version v3.4.3 +* @file bme280.h +* @date 2020-03-28 +* @version v3.5.0 * */ @@ -41,8 +41,11 @@ */ /*! - * @defgroup BME280 SENSOR API + * @defgroup bme280 BME280 + * @brief Product Overview + * and Sensor API Source Code */ + #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. * - * @param[in,out] dev : Structure instance of bme280_dev + * @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_init(struct bme280_dev *dev); +/** + * \ingroup bme280 + * \defgroup bme280ApiRegister Registers + * @brief Generic API for accessing sensor registers + */ + /*! - * @brief This API writes the given data to the 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 address from where the data to be written. + * @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 sensor. - * @param[in] len : No of bytes of data to write.. - * @param[in] dev : Structure instance of bme280_dev. + * 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. * - * @return Result of API execution status - * @retval zero -> Success / +ve value -> Warning / -ve value -> Error */ -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); /*! - * @brief This API reads the data from the given register address of the sensor. + * \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[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] len : No of bytes of data to be read. + * @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_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 */ -int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev); /*! - * @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 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_soft_reset(struct bme280_dev *dev); + +/** + * \ingroup bme280 + * \defgroup bme280ApiSensorData Sensor Data + * @brief Data processing of sensor */ -int8_t bme280_soft_reset(const struct bme280_dev *dev); /*! - * @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 diff --git a/bme280_defs.h b/bme280_defs.h index cf141cd..686ba6a 100644 --- a/bme280_defs.h +++ b/bme280_defs.h @@ -30,20 +30,12 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * -* @file bme280_defs.h -* @date 2020-01-24 -* @version v3.4.3 +* @file bme280_defs.h +* @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_ @@ -62,96 +54,110 @@ /********************************************************/ #if !defined(UINT8_C) && !defined(INT8_C) -#define INT8_C(x) S8_C(x) -#define UINT8_C(x) U8_C(x) +#define INT8_C(x) S8_C(x) +#define UINT8_C(x) U8_C(x) #endif #if !defined(UINT16_C) && !defined(INT16_C) -#define INT16_C(x) S16_C(x) -#define UINT16_C(x) U16_C(x) +#define INT16_C(x) S16_C(x) +#define UINT16_C(x) U16_C(x) #endif #if !defined(INT32_C) && !defined(UINT32_C) -#define INT32_C(x) S32_C(x) -#define UINT32_C(x) U32_C(x) +#define INT32_C(x) S32_C(x) +#define UINT32_C(x) U32_C(x) #endif #if !defined(INT64_C) && !defined(UINT64_C) -#define INT64_C(x) S64_C(x) -#define UINT64_C(x) U64_C(x) +#define INT64_C(x) S64_C(x) +#define UINT64_C(x) U64_C(x) #endif /**@}*/ /**\name C standard macros */ #ifndef NULL #ifdef __cplusplus -#define NULL 0 +#define NULL 0 #else -#define NULL ((void *) 0) +#define NULL ((void *) 0) #endif #endif /********************************************************/ -#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 #endif #ifndef TRUE -#define TRUE UINT8_C(1) +#define TRUE UINT8_C(1) #endif #ifndef FALSE -#define FALSE UINT8_C(0) +#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) +#define BME280_I2C_ADDR_PRIM UINT8_C(0x76) +#define BME280_I2C_ADDR_SEC UINT8_C(0x77) /**\name BME280 chip identifier */ -#define BME280_CHIP_ID UINT8_C(0x60) +#define BME280_CHIP_ID UINT8_C(0x60) /**\name Register Address */ -#define BME280_CHIP_ID_ADDR UINT8_C(0xD0) -#define BME280_RESET_ADDR UINT8_C(0xE0) -#define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88) -#define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1) -#define BME280_PWR_CTRL_ADDR UINT8_C(0xF4) -#define BME280_CTRL_HUM_ADDR UINT8_C(0xF2) -#define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4) -#define BME280_CONFIG_ADDR UINT8_C(0xF5) -#define BME280_DATA_ADDR UINT8_C(0xF7) +#define BME280_CHIP_ID_ADDR UINT8_C(0xD0) +#define BME280_RESET_ADDR UINT8_C(0xE0) +#define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88) +#define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1) +#define BME280_PWR_CTRL_ADDR UINT8_C(0xF4) +#define BME280_CTRL_HUM_ADDR UINT8_C(0xF2) +#define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4) +#define BME280_CONFIG_ADDR UINT8_C(0xF5) +#define BME280_DATA_ADDR UINT8_C(0xF7) /**\name API success code */ -#define BME280_OK INT8_C(0) +#define BME280_OK INT8_C(0) /**\name API error codes */ -#define BME280_E_NULL_PTR INT8_C(-1) -#define BME280_E_DEV_NOT_FOUND INT8_C(-2) -#define BME280_E_INVALID_LEN INT8_C(-3) -#define BME280_E_COMM_FAIL INT8_C(-4) -#define BME280_E_SLEEP_MODE_FAIL INT8_C(-5) -#define BME280_E_NVM_COPY_FAILED INT8_C(-6) +#define BME280_E_NULL_PTR INT8_C(-1) +#define BME280_E_DEV_NOT_FOUND INT8_C(-2) +#define BME280_E_INVALID_LEN INT8_C(-3) +#define BME280_E_COMM_FAIL INT8_C(-4) +#define BME280_E_SLEEP_MODE_FAIL INT8_C(-5) +#define BME280_E_NVM_COPY_FAILED INT8_C(-6) /**\name API warning codes */ -#define BME280_W_INVALID_OSR_MACRO INT8_C(1) +#define BME280_W_INVALID_OSR_MACRO INT8_C(1) /**\name Macros related to size */ -#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26) -#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7) -#define BME280_P_T_H_DATA_LEN UINT8_C(8) +#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26) +#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7) +#define BME280_P_T_H_DATA_LEN UINT8_C(8) /**\name Sensor power modes */ -#define BME280_SLEEP_MODE UINT8_C(0x00) -#define BME280_FORCED_MODE UINT8_C(0x01) -#define BME280_NORMAL_MODE UINT8_C(0x03) +#define BME280_SLEEP_MODE UINT8_C(0x00) +#define BME280_FORCED_MODE UINT8_C(0x01) +#define BME280_NORMAL_MODE UINT8_C(0x03) /**\name Macro to combine two 8 bit data's to form a 16 bit data */ -#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb) +#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb) #define BME280_SET_BITS(reg_data, bitname, data) \ ((reg_data & ~(bitname##_MSK)) | \ @@ -160,129 +166,201 @@ ((reg_data & ~(bitname##_MSK)) | \ (data & bitname##_MSK)) -#define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \ - (bitname##_POS)) -#define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK)) +#define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \ + (bitname##_POS)) +#define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK)) /**\name Macros for bit masking */ -#define BME280_SENSOR_MODE_MSK UINT8_C(0x03) -#define BME280_SENSOR_MODE_POS UINT8_C(0x00) +#define BME280_SENSOR_MODE_MSK UINT8_C(0x03) +#define BME280_SENSOR_MODE_POS UINT8_C(0x00) -#define BME280_CTRL_HUM_MSK UINT8_C(0x07) -#define BME280_CTRL_HUM_POS UINT8_C(0x00) +#define BME280_CTRL_HUM_MSK UINT8_C(0x07) +#define BME280_CTRL_HUM_POS UINT8_C(0x00) -#define BME280_CTRL_PRESS_MSK UINT8_C(0x1C) -#define BME280_CTRL_PRESS_POS UINT8_C(0x02) +#define BME280_CTRL_PRESS_MSK UINT8_C(0x1C) +#define BME280_CTRL_PRESS_POS UINT8_C(0x02) -#define BME280_CTRL_TEMP_MSK UINT8_C(0xE0) -#define BME280_CTRL_TEMP_POS UINT8_C(0x05) +#define BME280_CTRL_TEMP_MSK UINT8_C(0xE0) +#define BME280_CTRL_TEMP_POS UINT8_C(0x05) -#define BME280_FILTER_MSK UINT8_C(0x1C) -#define BME280_FILTER_POS UINT8_C(0x02) +#define BME280_FILTER_MSK UINT8_C(0x1C) +#define BME280_FILTER_POS UINT8_C(0x02) -#define BME280_STANDBY_MSK UINT8_C(0xE0) -#define BME280_STANDBY_POS UINT8_C(0x05) +#define BME280_STANDBY_MSK UINT8_C(0xE0) +#define BME280_STANDBY_POS UINT8_C(0x05) /**\name Sensor component selection macros * These values are internal for API implementation. Don't relate this to * data sheet. */ -#define BME280_PRESS UINT8_C(1) -#define BME280_TEMP UINT8_C(1 << 1) -#define BME280_HUM UINT8_C(1 << 2) -#define BME280_ALL UINT8_C(0x07) +#define BME280_PRESS UINT8_C(1) +#define BME280_TEMP UINT8_C(1 << 1) +#define BME280_HUM UINT8_C(1 << 2) +#define BME280_ALL UINT8_C(0x07) /**\name Settings selection macros */ -#define BME280_OSR_PRESS_SEL UINT8_C(1) -#define BME280_OSR_TEMP_SEL UINT8_C(1 << 1) -#define BME280_OSR_HUM_SEL UINT8_C(1 << 2) -#define BME280_FILTER_SEL UINT8_C(1 << 3) -#define BME280_STANDBY_SEL UINT8_C(1 << 4) -#define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F) +#define BME280_OSR_PRESS_SEL UINT8_C(1) +#define BME280_OSR_TEMP_SEL UINT8_C(1 << 1) +#define BME280_OSR_HUM_SEL UINT8_C(1 << 2) +#define BME280_FILTER_SEL UINT8_C(1 << 3) +#define BME280_STANDBY_SEL UINT8_C(1 << 4) +#define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F) /**\name Oversampling macros */ -#define BME280_NO_OVERSAMPLING UINT8_C(0x00) -#define BME280_OVERSAMPLING_1X UINT8_C(0x01) -#define BME280_OVERSAMPLING_2X UINT8_C(0x02) -#define BME280_OVERSAMPLING_4X UINT8_C(0x03) -#define BME280_OVERSAMPLING_8X UINT8_C(0x04) -#define BME280_OVERSAMPLING_16X UINT8_C(0x05) +#define BME280_NO_OVERSAMPLING UINT8_C(0x00) +#define BME280_OVERSAMPLING_1X UINT8_C(0x01) +#define BME280_OVERSAMPLING_2X UINT8_C(0x02) +#define BME280_OVERSAMPLING_4X UINT8_C(0x03) +#define BME280_OVERSAMPLING_8X UINT8_C(0x04) +#define BME280_OVERSAMPLING_16X UINT8_C(0x05) /**\name Measurement delay calculation macros */ -#define BME280_MEAS_OFFSET UINT16_C(1250) -#define BME280_MEAS_DUR UINT16_C(2300) -#define BME280_PRES_HUM_MEAS_OFFSET UINT16_C(575) -#define BME280_MEAS_SCALING_FACTOR UINT16_C(1000) +#define BME280_MEAS_OFFSET UINT16_C(1250) +#define BME280_MEAS_DUR UINT16_C(2300) +#define BME280_PRES_HUM_MEAS_OFFSET UINT16_C(575) +#define BME280_MEAS_SCALING_FACTOR UINT16_C(1000) /**\name Standby duration selection macros */ -#define BME280_STANDBY_TIME_0_5_MS (0x00) -#define BME280_STANDBY_TIME_62_5_MS (0x01) -#define BME280_STANDBY_TIME_125_MS (0x02) -#define BME280_STANDBY_TIME_250_MS (0x03) -#define BME280_STANDBY_TIME_500_MS (0x04) -#define BME280_STANDBY_TIME_1000_MS (0x05) -#define BME280_STANDBY_TIME_10_MS (0x06) -#define BME280_STANDBY_TIME_20_MS (0x07) +#define BME280_STANDBY_TIME_0_5_MS (0x00) +#define BME280_STANDBY_TIME_62_5_MS (0x01) +#define BME280_STANDBY_TIME_125_MS (0x02) +#define BME280_STANDBY_TIME_250_MS (0x03) +#define BME280_STANDBY_TIME_500_MS (0x04) +#define BME280_STANDBY_TIME_1000_MS (0x05) +#define BME280_STANDBY_TIME_10_MS (0x06) +#define BME280_STANDBY_TIME_20_MS (0x07) /**\name Filter coefficient selection macros */ -#define BME280_FILTER_COEFF_OFF (0x00) -#define BME280_FILTER_COEFF_2 (0x01) -#define BME280_FILTER_COEFF_4 (0x02) -#define BME280_FILTER_COEFF_8 (0x03) -#define BME280_FILTER_COEFF_16 (0x04) +#define BME280_FILTER_COEFF_OFF (0x00) +#define BME280_FILTER_COEFF_2 (0x01) +#define BME280_FILTER_COEFF_4 (0x02) +#define BME280_FILTER_COEFF_8 (0x03) +#define BME280_FILTER_COEFF_16 (0x04) -#define BME280_STATUS_REG_ADDR (0xF3) -#define BME280_SOFT_RESET_COMMAND (0xB6) -#define BME280_STATUS_IM_UPDATE (0x01) +#define BME280_STATUS_REG_ADDR (0xF3) +#define BME280_SOFT_RESET_COMMAND (0xB6) +#define BME280_STATUS_IM_UPDATE (0x01) /*! * @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; + + /*< 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; - int32_t t_fine; - /**@}*/ + /*< 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_ */ -/** @}*/ -/** @}*/ diff --git a/examples/bsd_userspace.c b/examples/bsd_userspace.c index 5e32da3..411a459 100644 --- a/examples/bsd_userspace.c +++ b/examples/bsd_userspace.c @@ -4,7 +4,15 @@ * tested: NanoPi NEO. * Use like: ./bme280 /dev/iic0 */ -#include "bme280.h" + +#ifdef __KERNEL__ +#include +#include +#endif + +/******************************************************************************/ +/*! System header files */ + #include #include #include @@ -12,67 +20,175 @@ #include #include -#include -#include - -int fd; +/******************************************************************************/ +/*! Own header files */ +#include +#include "bme280.h" -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); +/******************************************************************************/ +/*! 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 */ + +/*! + * @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); -int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len) +/*! + * @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, ®_addr}, - {id << 1 | IIC_M_RD, IIC_M_RD, len, data}, + { id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, ®_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data }, }; - struct iic_rdwr_data rdwr_data = {msgs, 2}; + struct iic_rdwr_data rdwr_data = { msgs, 2 }; + + int error = ioctl(id.fd, I2CRDWR, &rdwr_data); - int error = ioctl(fd, I2CRDWR, &rdwr_data); - if (error) { + 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) { - uint8_t *buf = malloc( (1 + len) * sizeof(uint8_t) ); - if (buf == NULL) { + struct identifier id; + + id = *((struct identifier *)intf_ptr); + + uint8_t *buf = malloc((1 + len) * sizeof(uint8_t)); + + if (buf == NULL) + { return BME280_E_COMM_FAIL; } buf[0] = reg_addr; - for (int i = 0; i < len; i++) { - buf[i+1] = data[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}; + struct iic_rdwr_data rdwr_data = { &msg, 1 }; + + int error = ioctl(id.fd, I2CRDWR, &rdwr_data); - int error = ioctl(fd, I2CRDWR, &rdwr_data); - if (error) { + 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,55 +258,58 @@ 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 -; + 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_ms = user_delay_ms; + dev.delay_us = user_delay_us; - - if ((fd = open(argv[1], O_RDWR)) < 0) - { - fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]); - exit(1); - } + /* 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; } diff --git a/examples/linux_userspace.c b/examples/linux_userspace.c index 62c8eb5..0b8fc9a 100644 --- a/examples/linux_userspace.c +++ b/examples/linux_userspace.c @@ -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] 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] 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, ®_addr, 1); - read(fd, data, len); + struct identifier id; + + id = *((struct identifier *)intf_ptr); + + write(id.fd, ®_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) { -- 2.44.0