diff --git a/README.md b/README.md index 5cf73d2..aea2bc5 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ This package contains the Bosch Sensortec's BME680 gas sensor API The sensor driver package includes bme680.h, bme680.c and bme680_defs.h files ## Version -File | Version | Date ------|---------|----- -bme680.c | 3.5.1 | 5 Jul 2017 -bme680.h | 3.5.1 | 5 Jul 2017 -bme680_defs.h | 3.5.1 | 5 Jul 2017 +File | Version | Date +--------------|---------|------------- +bme680.c | 3.5.3 | 30 Oct 2017 +bme680.h | 3.5.3 | 30 Oct 2017 +bme680_defs.h | 3.5.3 | 30 Oct 2017 ## Integration details * Integrate bme680.h, bme680_defs.h and bme680.c file in to your project. @@ -112,7 +112,7 @@ fill in the various parameters as shown below printf("T: %.2f degC, P: %.2f hPa, H %.2f %%rH ", data.temperature / 100.0f, data.pressure / 100.0f, data.humidity / 1000.0f ); /* Avoid using measurements from an unstable heating setup */ - if(data.status & BME680_HEAT_STAB_MSK) + if(data.status & BME680_GASM_VALID_MSK) printf(", G: %d ohms", data.gas_resistance); printf("\r\n"); diff --git a/Self test/bme680_selftest.c b/Self test/bme680_selftest.c index c804859..fa2d6be 100644 --- a/Self test/bme680_selftest.c +++ b/Self test/bme680_selftest.c @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * File bme680_selftest.c - * @date 5 Jul 2017 - * @version 3.5.1 + * @date 10 Oct 2017 + * @version 3.5.2 * */ @@ -53,7 +53,7 @@ #include "bme680_selftest.h" #define MIN_TEMPERATURE INT16_C(0) /* 0 degree Celsius */ -#define MAX_TEMPERATURE INT16_C(4000) /* 40 degree Celsius */ +#define MAX_TEMPERATURE INT16_C(6000) /* 60 degree Celsius */ #define MIN_PRESSURE UINT32_C(90000) /* 900 hecto Pascals */ #define MAX_PRESSURE UINT32_C(110000) /* 1100 hecto Pascals */ @@ -63,7 +63,7 @@ #define HEATR_DUR 2000 #define N_MEAS 6 -#define LOW_TEMP 200 +#define LOW_TEMP 150 #define HIGH_TEMP 350 /*! @@ -124,9 +124,9 @@ int8_t bme680_self_test(struct bme680_dev *dev) if (rslt == BME680_OK) { if (i % 2 == 0) - t_dev.gas_sett.heatr_temp = LOW_TEMP; /* Lower temperature */ - else t_dev.gas_sett.heatr_temp = HIGH_TEMP; /* Higher temperature */ + else + t_dev.gas_sett.heatr_temp = LOW_TEMP; /* Lower temperature */ rslt = bme680_set_sensor_settings(settings_sel, &t_dev); @@ -169,15 +169,16 @@ static int8_t analyze_sensor_data(struct bme680_field_data *data, uint8_t n_meas self_test_failed++; for (i = 0; i < n_meas; i++) /* Every gas measurement should be valid */ - if (!(data[i].status & (BME680_GASM_VALID_MSK | BME680_HEAT_STAB_MSK))) + if (!(data[i].status & BME680_GASM_VALID_MSK)) self_test_failed++; - for (i = 2; i < n_meas; i += 2) { - /* Invert formula to get integer values for centroid resistance, i.e. > 1 */ - cent_res = (data[i - 2].gas_resistance + data[i].gas_resistance) / (2 * data[i - 1].gas_resistance); - } + /* 3 cycles heating are completed(HT1/LT1, HT2/LT2,HT3/LT3) + centroid gas ratio = 2*HT3 / (LT2+LT3) < 0.5*/ + /* Invert formula to get integer values for centroid resistance */ + if (n_meas >= 6) + cent_res = (data[3].gas_resistance + data[5].gas_resistance) / (2 * data[4].gas_resistance); - if ((cent_res < 3) || (cent_res > 20)) /* 0.05 > cent_res^-1 < 0.03 */ + if ((cent_res < 2)) /*cent_res^-1 < 0.5 */ self_test_failed++; if (self_test_failed) diff --git a/Self test/bme680_selftest.h b/Self test/bme680_selftest.h index 1b1be60..21a4f42 100644 --- a/Self test/bme680_selftest.h +++ b/Self test/bme680_selftest.h @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * @file bme680_selftest.h - * @date 5 Jul 2017 - * @version 3.5.1 + * @date 10 Oct 2017 + * @version 3.5.2 * @brief * */ diff --git a/bme680.c b/bme680.c index 1b53aa5..b457dac 100644 --- a/bme680.c +++ b/bme680.c @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * File bme680.c - * @date 5 Jul 2017 - * @version 3.5.1 + * @date 30 Oct 2017 + * @version 3.5.3 * */ @@ -52,14 +52,14 @@ /**static variables */ /**Look up table for the possible gas range values */ uint32_t lookupTable1[16] = { UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647), UINT32_C(2147483647), - UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2130303777), UINT32_C(2147483647), - UINT32_C(2147483647), UINT32_C(2143188679), UINT32_C(2136746228), UINT32_C(2147483647), UINT32_C(2126008810), - UINT32_C(2147483647), UINT32_C(2147483647) }; + UINT32_C(2147483647), UINT32_C(2126008810), UINT32_C(2147483647), UINT32_C(2130303777), UINT32_C(2147483647), + UINT32_C(2147483647), UINT32_C(2143188679), UINT32_C(2136746228), UINT32_C(2147483647), UINT32_C(2126008810), + UINT32_C(2147483647), UINT32_C(2147483647) }; /**Look up table for the possible gas range values */ uint32_t lookupTable2[16] = { UINT32_C(4096000000), UINT32_C(2048000000), UINT32_C(1024000000), UINT32_C(512000000), - UINT32_C(255744255), UINT32_C(127110228), UINT32_C(64000000), UINT32_C(32258064), UINT32_C(16016016), UINT32_C( - 8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000), UINT32_C(250000), - UINT32_C(125000) }; + UINT32_C(255744255), UINT32_C(127110228), UINT32_C(64000000), UINT32_C(32258064), UINT32_C(16016016), UINT32_C( + 8000000), UINT32_C(4000000), UINT32_C(2000000), UINT32_C(1000000), UINT32_C(500000), UINT32_C(250000), + UINT32_C(125000) }; /*! * @brief This internal API is used to read the calibrated data from the sensor. @@ -239,15 +239,14 @@ int8_t bme680_init(struct bme680_dev *dev) /* Check for null pointer in the device structure*/ rslt = null_ptr_check(dev); - if (rslt == BME680_OK) - { + if (rslt == BME680_OK) { /* Soft reset to restore it to default values*/ rslt = bme680_soft_reset(dev); - if (rslt == BME680_OK) { rslt = bme680_get_regs(BME680_CHIP_ID_ADDR, &dev->chip_id, 1, dev); if (rslt == BME680_OK) { if (dev->chip_id == BME680_CHIP_ID) { + /* Get the Calibration data */ rslt = get_calib_data(dev); } else { rslt = BME680_E_DEV_NOT_FOUND; @@ -398,7 +397,8 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev * /* Selecting heater control for the sensor */ if (desired_settings & BME680_HCNTRL_SEL) { - rslt = boundary_check(&dev->gas_sett.heatr_ctrl, BME680_ENABLE_HEATER, BME680_DISABLE_HEATER, dev); + rslt = boundary_check(&dev->gas_sett.heatr_ctrl, BME680_ENABLE_HEATER, + BME680_DISABLE_HEATER, dev); reg_addr = BME680_CONF_HEAT_CTRL_ADDR; if (rslt == BME680_OK) @@ -445,9 +445,14 @@ int8_t bme680_set_sensor_settings(uint16_t desired_settings, struct bme680_dev * /* Selecting the runGas and NB conversion settings for the sensor */ if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) { - rslt = boundary_check(&dev->gas_sett.run_gas, BME680_RUN_GAS_DISABLE, BME680_RUN_GAS_ENABLE, dev); - if (rslt == BME680_OK) - rslt = boundary_check(&dev->gas_sett.nb_conv, BME680_NBCONV_MIN, BME680_NBCONV_MAX, dev); + rslt = boundary_check(&dev->gas_sett.run_gas, BME680_RUN_GAS_DISABLE, + BME680_RUN_GAS_ENABLE, dev); + if (rslt == BME680_OK) { + /* Validate boundary conditions */ + rslt = boundary_check(&dev->gas_sett.nb_conv, BME680_NBCONV_MIN, + BME680_NBCONV_MAX, dev); + } + reg_addr = BME680_CONF_ODR_RUN_GAS_NBC_ADDR; if (rslt == BME680_OK) @@ -496,7 +501,8 @@ int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev * /* get the T,P,H ,Filter,ODR settings here */ if (desired_settings & BME680_FILTER_SEL) - dev->tph_sett.filter = BME680_GET_BITS(data_array[BME680_REG_FILTER_INDEX], BME680_FILTER); + dev->tph_sett.filter = BME680_GET_BITS(data_array[BME680_REG_FILTER_INDEX], + BME680_FILTER); if (desired_settings & (BME680_OST_SEL | BME680_OSP_SEL)) { dev->tph_sett.os_temp = BME680_GET_BITS(data_array[BME680_REG_TEMP_INDEX], BME680_OST); @@ -504,15 +510,19 @@ int8_t bme680_get_sensor_settings(uint16_t desired_settings, struct bme680_dev * } if (desired_settings & BME680_OSH_SEL) - dev->tph_sett.os_hum = BME680_GET_BITS_POS_0(data_array[BME680_REG_HUM_INDEX], BME680_OSH); + dev->tph_sett.os_hum = BME680_GET_BITS_POS_0(data_array[BME680_REG_HUM_INDEX], + BME680_OSH); /* get the gas related settings */ if (desired_settings & BME680_HCNTRL_SEL) - dev->gas_sett.heatr_ctrl = BME680_GET_BITS_POS_0(data_array[BME680_REG_HCTRL_INDEX], BME680_HCTRL); + dev->gas_sett.heatr_ctrl = BME680_GET_BITS_POS_0(data_array[BME680_REG_HCTRL_INDEX], + BME680_HCTRL); if (desired_settings & (BME680_RUN_GAS_SEL | BME680_NBCONV_SEL)) { - dev->gas_sett.nb_conv = BME680_GET_BITS_POS_0(data_array[BME680_REG_NBCONV_INDEX], BME680_NBCONV); - dev->gas_sett.run_gas = BME680_GET_BITS(data_array[BME680_REG_RUN_GAS_INDEX], BME680_RUN_GAS); + dev->gas_sett.nb_conv = BME680_GET_BITS_POS_0(data_array[BME680_REG_NBCONV_INDEX], + BME680_NBCONV); + dev->gas_sett.run_gas = BME680_GET_BITS(data_array[BME680_REG_RUN_GAS_INDEX], + BME680_RUN_GAS); } } } else { @@ -602,7 +612,7 @@ void bme680_set_profile_dur(uint16_t duration, struct bme680_dev *dev) /*! * @brief This API is used to get the profile duration of the sensor. */ -void bme680_get_profile_dur(uint16_t *duration, struct bme680_dev *dev) +void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev) { uint32_t tph_dur; /* Calculate in us */ @@ -614,8 +624,14 @@ void bme680_get_profile_dur(uint16_t *duration, struct bme680_dev *dev) tph_dur /= UINT32_C(1000); /* Convert to ms */ tph_dur += UINT32_C(1); /* Wake up duration of 1ms */ - /* The remaining time should be used for heating */ - *duration = dev->gas_sett.heatr_dur + (uint16_t) tph_dur; + + *duration = (uint16_t) tph_dur; + + /* Get the gas duration only when the run gas is enabled */ + if (dev->gas_sett.run_gas) { + /* The remaining time should be used for heating */ + *duration += dev->gas_sett.heatr_dur; + } } /*! @@ -650,6 +666,7 @@ static int8_t get_calib_data(struct bme680_dev *dev) { int8_t rslt; uint8_t coeff_array[BME680_COEFF_SIZE] = { 0 }; + uint8_t temp_var = 0; /* Temporary variable */ /* Check for null pointer in the device structure*/ rslt = null_ptr_check(dev); @@ -657,39 +674,39 @@ static int8_t get_calib_data(struct bme680_dev *dev) rslt = bme680_get_regs(BME680_COEFF_ADDR1, coeff_array, BME680_COEFF_ADDR1_LEN, dev); /* Append the second half in the same array */ if (rslt == BME680_OK) - rslt = bme680_get_regs(BME680_COEFF_ADDR2, &coeff_array[BME680_COEFF_ADDR1_LEN], BME680_COEFF_ADDR2_LEN, - dev); + rslt = bme680_get_regs(BME680_COEFF_ADDR2, &coeff_array[BME680_COEFF_ADDR1_LEN] + , BME680_COEFF_ADDR2_LEN, dev); /* Temperature related coefficients */ dev->calib.par_t1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T1_MSB_REG], - coeff_array[BME680_T1_LSB_REG])); + coeff_array[BME680_T1_LSB_REG])); dev->calib.par_t2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_T2_MSB_REG], - coeff_array[BME680_T2_LSB_REG])); + coeff_array[BME680_T2_LSB_REG])); dev->calib.par_t3 = (int8_t) (coeff_array[BME680_T3_REG]); /* Pressure related coefficients */ dev->calib.par_p1 = (uint16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P1_MSB_REG], - coeff_array[BME680_P1_LSB_REG])); + coeff_array[BME680_P1_LSB_REG])); dev->calib.par_p2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P2_MSB_REG], - coeff_array[BME680_P2_LSB_REG])); + coeff_array[BME680_P2_LSB_REG])); dev->calib.par_p3 = (int8_t) coeff_array[BME680_P3_REG]; dev->calib.par_p4 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P4_MSB_REG], - coeff_array[BME680_P4_LSB_REG])); + coeff_array[BME680_P4_LSB_REG])); dev->calib.par_p5 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P5_MSB_REG], - coeff_array[BME680_P5_LSB_REG])); + coeff_array[BME680_P5_LSB_REG])); dev->calib.par_p6 = (int8_t) (coeff_array[BME680_P6_REG]); dev->calib.par_p7 = (int8_t) (coeff_array[BME680_P7_REG]); dev->calib.par_p8 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P8_MSB_REG], - coeff_array[BME680_P8_LSB_REG])); + coeff_array[BME680_P8_LSB_REG])); dev->calib.par_p9 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_P9_MSB_REG], - coeff_array[BME680_P9_LSB_REG])); + coeff_array[BME680_P9_LSB_REG])); dev->calib.par_p10 = (uint8_t) (coeff_array[BME680_P10_REG]); /* Humidity related coefficients */ dev->calib.par_h1 = (uint16_t) (((uint16_t) coeff_array[BME680_H1_MSB_REG] << BME680_HUM_REG_SHIFT_VAL) - | (coeff_array[BME680_H1_LSB_REG] & BME680_BIT_H1_DATA_MSK)); + | (coeff_array[BME680_H1_LSB_REG] & BME680_BIT_H1_DATA_MSK)); dev->calib.par_h2 = (uint16_t) (((uint16_t) coeff_array[BME680_H2_MSB_REG] << BME680_HUM_REG_SHIFT_VAL) - | ((coeff_array[BME680_H2_LSB_REG]) >> BME680_HUM_REG_SHIFT_VAL)); + | ((coeff_array[BME680_H2_LSB_REG]) >> BME680_HUM_REG_SHIFT_VAL)); dev->calib.par_h3 = (int8_t) coeff_array[BME680_H3_REG]; dev->calib.par_h4 = (int8_t) coeff_array[BME680_H4_REG]; dev->calib.par_h5 = (int8_t) coeff_array[BME680_H5_REG]; @@ -699,12 +716,10 @@ static int8_t get_calib_data(struct bme680_dev *dev) /* Gas heater related coefficients */ dev->calib.par_gh1 = (int8_t) coeff_array[BME680_GH1_REG]; dev->calib.par_gh2 = (int16_t) (BME680_CONCAT_BYTES(coeff_array[BME680_GH2_MSB_REG], - coeff_array[BME680_GH2_LSB_REG])); + coeff_array[BME680_GH2_LSB_REG])); dev->calib.par_gh3 = (int8_t) coeff_array[BME680_GH3_REG]; /* Other coefficients */ - uint8_t temp_var = 0; /* Temporary variable */ - if (rslt == BME680_OK) { rslt = bme680_get_regs(BME680_ADDR_RES_HEAT_RANGE_ADDR, &temp_var, 1, dev); @@ -734,7 +749,8 @@ static int8_t set_gas_config(struct bme680_dev *dev) rslt = null_ptr_check(dev); if (rslt == BME680_OK) { - uint8_t reg_addr[2], reg_data[2]; + uint8_t reg_addr[2] = {0}; + uint8_t reg_data[2] = {0}; if (dev->power_mode == BME680_FORCED_MODE) { reg_addr[0] = BME680_RES_HEAT0_ADDR; @@ -800,12 +816,12 @@ static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev) int64_t var3; int16_t calc_temp; - var1 = ((int32_t) temp_adc / 8) - ((int32_t) dev->calib.par_t1 * 2); - var2 = (var1 * (int32_t) dev->calib.par_t2) / 2048; - var3 = ((var1 / 2) * (var1 / 2)) / 4096; - var3 = ((var3) * ((int32_t) dev->calib.par_t3 * 16)) / 16384; + var1 = ((int32_t) temp_adc >> 3) - ((int32_t) dev->calib.par_t1 << 1); + var2 = (var1 * (int32_t) dev->calib.par_t2) >> 11; + var3 = ((var1 >> 1) * (var1 >> 1)) >> 12; + var3 = ((var3) * ((int32_t) dev->calib.par_t3 << 4)) >> 14; dev->calib.t_fine = (int32_t) (var2 + var3); - calc_temp = (int16_t) (((dev->calib.t_fine * 5) + 128) / 256); + calc_temp = (int16_t) (((dev->calib.t_fine * 5) + 128) >> 8); return calc_temp; } @@ -815,30 +831,42 @@ static int16_t calc_temperature(uint32_t temp_adc, struct bme680_dev *dev) */ static uint32_t calc_pressure(uint32_t pres_adc, const struct bme680_dev *dev) { - int32_t var1; - int32_t var2; - int32_t var3; - int32_t calc_pres; + int32_t var1 = 0; + int32_t var2 = 0; + int32_t var3 = 0; + int32_t var4 = 0; + int32_t pressure_comp = 0; - var1 = (((int32_t) dev->calib.t_fine) / 2) - 64000; - var2 = ((var1 / 4) * (var1 / 4)) / 2048; - var2 = ((var2) * (int32_t) dev->calib.par_p6) / 4; - var2 = var2 + ((var1 * (int32_t) dev->calib.par_p5) * 2); - var2 = (var2 / 4) + ((int32_t) dev->calib.par_p4 * 65536); - var1 = ((var1 / 4) * (var1 / 4)) / 8192; - var1 = (((var1) * ((int32_t) dev->calib.par_p3 * 32)) / 8) + (((int32_t) dev->calib.par_p2 * var1) / 2); - var1 = var1 / 262144; - var1 = ((32768 + var1) * (int32_t) dev->calib.par_p1) / 32768; - calc_pres = (int32_t) (1048576 - pres_adc); - calc_pres = (int32_t) ((calc_pres - (var2 / 4096)) * (3125)); - calc_pres = ((calc_pres / var1) * 2); - var1 = ((int32_t) dev->calib.par_p9 * (int32_t) (((calc_pres / 8) * (calc_pres / 8)) / 8192)) / 4096; - var2 = ((int32_t) (calc_pres / 4) * (int32_t) dev->calib.par_p8) / 8192; - var3 = ((int32_t) (calc_pres / 256) * (int32_t) (calc_pres / 256) * (int32_t) (calc_pres / 256) - * (int32_t) dev->calib.par_p10) / 131072; - calc_pres = (int32_t) (calc_pres) + ((var1 + var2 + var3 + ((int32_t) dev->calib.par_p7 * 128)) / 16); + var1 = (((int32_t)dev->calib.t_fine) >> 1) - 64000; + var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * + (int32_t)dev->calib.par_p6) >> 2; + var2 = var2 + ((var1 * (int32_t)dev->calib.par_p5) << 1); + var2 = (var2 >> 2) + ((int32_t)dev->calib.par_p4 << 16); + var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * + ((int32_t)dev->calib.par_p3 << 5)) >> 3) + + (((int32_t)dev->calib.par_p2 * var1) >> 1); + var1 = var1 >> 18; + var1 = ((32768 + var1) * (int32_t)dev->calib.par_p1) >> 15; + pressure_comp = 1048576 - pres_adc; + pressure_comp = (int32_t)((pressure_comp - (var2 >> 12)) * ((uint32_t)3125)); + var4 = (1 << 31); + if (pressure_comp >= var4) + pressure_comp = ((pressure_comp / (uint32_t)var1) << 1); + else + pressure_comp = ((pressure_comp << 1) / (uint32_t)var1); + var1 = ((int32_t)dev->calib.par_p9 * (int32_t)(((pressure_comp >> 3) * + (pressure_comp >> 3)) >> 13)) >> 12; + var2 = ((int32_t)(pressure_comp >> 2) * + (int32_t)dev->calib.par_p8) >> 13; + var3 = ((int32_t)(pressure_comp >> 8) * (int32_t)(pressure_comp >> 8) * + (int32_t)(pressure_comp >> 8) * + (int32_t)dev->calib.par_p10) >> 17; + + pressure_comp = (int32_t)(pressure_comp) + ((var1 + var2 + var3 + + ((int32_t)dev->calib.par_p7 << 7)) >> 4); + + return (uint32_t)pressure_comp; - return (uint32_t) calc_pres; } /*! @@ -855,19 +883,19 @@ static uint32_t calc_humidity(uint16_t hum_adc, const struct bme680_dev *dev) int32_t temp_scaled; int32_t calc_hum; - temp_scaled = (((int32_t) dev->calib.t_fine * 5) + 128) / 256; + temp_scaled = (((int32_t) dev->calib.t_fine * 5) + 128) >> 8; var1 = (int32_t) (hum_adc - ((int32_t) ((int32_t) dev->calib.par_h1 * 16))) - - (((temp_scaled * (int32_t) dev->calib.par_h3) / ((int32_t) 100)) / 2); + - (((temp_scaled * (int32_t) dev->calib.par_h3) / ((int32_t) 100)) >> 1); var2 = ((int32_t) dev->calib.par_h2 - * (((temp_scaled * (int32_t) dev->calib.par_h4) / ((int32_t) 100)) - + (((temp_scaled * ((temp_scaled * (int32_t) dev->calib.par_h5) / ((int32_t) 100))) / 64) - / ((int32_t) 100)) + (int32_t) (1 * 16384))) / 1024; + * (((temp_scaled * (int32_t) dev->calib.par_h4) / ((int32_t) 100)) + + (((temp_scaled * ((temp_scaled * (int32_t) dev->calib.par_h5) / ((int32_t) 100))) >> 6) + / ((int32_t) 100)) + (int32_t) (1 << 14))) >> 10; var3 = var1 * var2; - var4 = (int32_t) dev->calib.par_h6 * 128; - var4 = ((var4) + ((temp_scaled * (int32_t) dev->calib.par_h7) / ((int32_t) 100))) / 16; - var5 = ((var3 / 16384) * (var3 / 16384)) / 1024; - var6 = (var4 * var5) / 2; - calc_hum = (((var3 + var6) / 1024) * ((int32_t) 1000)) / 4096; + var4 = (int32_t) dev->calib.par_h6 << 7; + var4 = ((var4) + ((temp_scaled * (int32_t) dev->calib.par_h7) / ((int32_t) 100))) >> 4; + var5 = ((var3 >> 14) * (var3 >> 14)) >> 10; + var6 = (var4 * var5) >> 1; + calc_hum = (((var3 + var6) >> 10) * ((int32_t) 1000)) >> 12; if (calc_hum > 100000) /* Cap at 100%rH */ calc_hum = 100000; @@ -887,10 +915,11 @@ static uint32_t calc_gas_resistance(uint16_t gas_res_adc, uint8_t gas_range, con int64_t var3; uint32_t calc_gas_res; - var1 = (int64_t) ((1340 + (5 * (int64_t) dev->calib.range_sw_err)) * ((int64_t) lookupTable1[gas_range])) / 65536; - var2 = (((int64_t) ((int64_t) gas_res_adc * 32768) - (int64_t) (16777216)) + var1); - var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) / 512); - calc_gas_res = (uint32_t) ((var3 + ((int64_t) var2 / 2)) / (int64_t) var2); + var1 = (int64_t) ((1340 + (5 * (int64_t) dev->calib.range_sw_err)) * + ((int64_t) lookupTable1[gas_range])) >> 16; + var2 = (((int64_t) ((int64_t) gas_res_adc << 15) - (int64_t) (16777216)) + var1); + var3 = (((int64_t) lookupTable2[gas_range] * (int64_t) var1) >> 9); + calc_gas_res = (uint32_t) ((var3 + ((int64_t) var2 >> 1)) / (int64_t) var2); return calc_gas_res; } @@ -963,15 +992,18 @@ static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev rslt = null_ptr_check(dev); do { if (rslt == BME680_OK) { - rslt = bme680_get_regs(((uint8_t) (BME680_FIELD0_ADDR)), buff, (uint16_t) BME680_FIELD_LENGTH, dev); + rslt = bme680_get_regs(((uint8_t) (BME680_FIELD0_ADDR)), buff, (uint16_t) BME680_FIELD_LENGTH, + dev); data->status = buff[0] & BME680_NEW_DATA_MSK; data->gas_index = buff[0] & BME680_GAS_INDEX_MSK; data->meas_index = buff[1]; /* read the raw data from the sensor */ - adc_pres = (uint32_t) (((uint32_t) buff[2] * 4096) | ((uint32_t) buff[3] * 16) | ((uint32_t) buff[4] / 16)); - adc_temp = (uint32_t) (((uint32_t) buff[5] * 4096) | ((uint32_t) buff[6] * 16) | ((uint32_t) buff[7] / 16)); + adc_pres = (uint32_t) (((uint32_t) buff[2] * 4096) | ((uint32_t) buff[3] * 16) + | ((uint32_t) buff[4] / 16)); + adc_temp = (uint32_t) (((uint32_t) buff[5] * 4096) | ((uint32_t) buff[6] * 16) + | ((uint32_t) buff[7] / 16)); adc_hum = (uint16_t) (((uint32_t) buff[8] * 256) | (uint32_t) buff[9]); adc_gas_res = (uint16_t) ((uint32_t) buff[13] * 4 | (((uint32_t) buff[14]) / 64)); gas_range = buff[14] & BME680_GAS_RANGE_MSK; @@ -985,9 +1017,9 @@ static int8_t read_field_data(struct bme680_field_data *data, struct bme680_dev data->humidity = calc_humidity(adc_hum, dev); data->gas_resistance = calc_gas_resistance(adc_gas_res, gas_range, dev); break; - } else { - dev->delay_ms(BME680_POLL_PERIOD_MS); } + /* Delay to poll the data */ + dev->delay_ms(BME680_POLL_PERIOD_MS); } tries--; } while (tries); @@ -1026,7 +1058,8 @@ static int8_t set_mem_page(uint8_t reg_addr, struct bme680_dev *dev) reg = reg & (~BME680_MEM_PAGE_MSK); reg = reg | (dev->mem_page & BME680_MEM_PAGE_MSK); - dev->com_rslt = dev->write(dev->dev_id, BME680_MEM_PAGE_ADDR & BME680_SPI_WR_MSK, ®, 1); + dev->com_rslt = dev->write(dev->dev_id, BME680_MEM_PAGE_ADDR & BME680_SPI_WR_MSK, + ®, 1); if (dev->com_rslt != 0) rslt = BME680_E_COM_FAIL; } diff --git a/bme680.h b/bme680.h index 38b8628..56eb7a8 100644 --- a/bme680.h +++ b/bme680.h @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * @file bme680.h - * @date 5 Jul 2017 - * @version 3.5.1 + * @date 30 Oct 2017 + * @version 3.5.3 * @brief * */ @@ -162,7 +162,7 @@ void bme680_set_profile_dur(uint16_t duration, struct bme680_dev *dev); * * @return Nothing */ -void bme680_get_profile_dur(uint16_t *duration, struct bme680_dev *dev); +void bme680_get_profile_dur(uint16_t *duration, const struct bme680_dev *dev); /*! * @brief This API reads the pressure, temperature and humidity and gas data @@ -185,17 +185,17 @@ int8_t bme680_get_sensor_data(struct bme680_field_data *data, struct bme680_dev * @param[in] desired_settings : Variable used to select the settings which * are to be set in the sensor. * - * Macros | Functionality - *-------------------------|---------------------------------------------- - * BME680_OST_SEL | To set temperature oversampling. - * BME680_OSP_SEL | To set pressure oversampling. - * BME680_OSH_SEL | To set humidity oversampling. - * BME680_GAS_MEAS_SEL | To set gas measurement setting. - * BME680_FILTER_SEL | To set filter setting. - * BME680_HCNTRL_SEL | To set humidity control setting. - * BME680_RUN_GAS_SEL | To set run gas setting. - * BME680_NBCONV_SEL | To set NB conversion setting. - * BME680_GAS_SENSOR_SEL | To set all gas sensor related settings + * Macros | Functionality + *---------------------------------|---------------------------------------------- + * BME680_OST_SEL | To set temperature oversampling. + * BME680_OSP_SEL | To set pressure oversampling. + * BME680_OSH_SEL | To set humidity oversampling. + * BME680_GAS_MEAS_SEL | To set gas measurement setting. + * BME680_FILTER_SEL | To set filter setting. + * BME680_HCNTRL_SEL | To set humidity control setting. + * BME680_RUN_GAS_SEL | To set run gas setting. + * BME680_NBCONV_SEL | To set NB conversion setting. + * BME680_GAS_SENSOR_SEL | To set all gas sensor related settings * * @note : Below are the macros to be used by the user for selecting the * desired settings. User can do OR operation of these macros for configuring diff --git a/bme680_defs.h b/bme680_defs.h index 4ef701f..3afb94c 100644 --- a/bme680_defs.h +++ b/bme680_defs.h @@ -40,8 +40,8 @@ * patent rights of the copyright holder. * * @file bme680_defs.h - * @date 5 Jul 2017 - * @version 3.5.1 + * @date 30 Oct 2017 + * @version 3.5.3 * @brief * */ @@ -59,55 +59,36 @@ /* header includes */ #ifdef __KERNEL__ #include +#include #else #include +#include #endif -#ifdef __KERNEL__ -#if (LONG_MAX) > 0x7fffffff -#define __have_long64 1 -#elif (LONG_MAX) == 0x7fffffff -#define __have_long32 1 +/******************************************************************************/ +/*! @name Common macros */ +/******************************************************************************/ + +#if !defined(UINT8_C) && !defined(INT8_C) +#define INT8_C(x) S8_C(x) +#define UINT8_C(x) U8_C(x) #endif -#if !defined(UINT8_C) -#define INT8_C(x) x -#if (INT_MAX) > 0x7f -#define UINT8_C(x) x -#else -#define UINT8_C(x) x##U -#endif -#endif - -#if !defined(UINT16_C) -#define INT16_C(x) x -#if (INT_MAX) > 0x7fff -#define UINT16_C(x) x -#else -#define UINT16_C(x) x##U -#endif +#if !defined(UINT16_C) && !defined(INT16_C) +#define INT16_C(x) S16_C(x) +#define UINT16_C(x) U16_C(x) #endif #if !defined(INT32_C) && !defined(UINT32_C) -#if __have_long32 -#define INT32_C(x) x##L -#define UINT32_C(x) x##UL -#else -#define INT32_C(x) x -#define UINT32_C(x) x##U -#endif +#define INT32_C(x) S32_C(x) +#define UINT32_C(x) U32_C(x) #endif #if !defined(INT64_C) && !defined(UINT64_C) -#if __have_long64 -#define INT64_C(x) x##L -#define UINT64_C(x) x##UL -#else -#define INT64_C(x) x##LL -#define UINT64_C(x) x##ULL -#endif -#endif +#define INT64_C(x) S64_C(x) +#define UINT64_C(x) U64_C(x) #endif + /**@}*/ /**\name C standard macros */ @@ -221,7 +202,7 @@ #define BME680_FORCED_MODE UINT8_C(1) /** Delay related macro declaration */ -#define BME680_RESET_PERIOD UINT32_C(10) +#define BME680_RESET_PERIOD UINT32_C(10) /** SPI memory page settings */ #define BME680_MEM_PAGE0 UINT8_C(0x10) @@ -250,7 +231,7 @@ #define BME680_HCNTRL_SEL UINT16_C(32) #define BME680_RUN_GAS_SEL UINT16_C(64) #define BME680_NBCONV_SEL UINT16_C(128) -#define BME680_GAS_SENSOR_SEL UINT16_C(BME680_GAS_MEAS_SEL | BME680_RUN_GAS_SEL | BME680_NBCONV_SEL) +#define BME680_GAS_SENSOR_SEL (BME680_GAS_MEAS_SEL | BME680_RUN_GAS_SEL | BME680_NBCONV_SEL) /** Number of conversion settings*/ #define BME680_NBCONV_MIN UINT8_C(0) @@ -350,10 +331,10 @@ /* * Generic communication function pointer * @param[in] dev_id: Place holder to store the id of the device structure - * Can be used to store the index of the Chip select or - * I2C address of the device. + * Can be used to store the index of the Chip select or + * I2C address of the device. * @param[in] reg_addr: Used to select the register the where data needs to - * be read from or written to. + * be read from or written to. * @param[in/out] reg_data: Data array to read/write * @param[in] len: Length of the data array */ @@ -524,6 +505,8 @@ struct bme680_dev { int8_t com_rslt; }; + + #endif /* BME680_DEFS_H_ */ /** @}*/ /** @}*/ diff --git a/changelog.md b/changelog.md index 36bc4ab..c777229 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,15 @@ # Change Log All notable changes to the BME680 Sensor API will be documented in this file. +## v3.5.3, 30 Oct 2017 +### Changed + - Changed the compensation equation formulae to use shifting operation + - Updated the "bme680_get_profile_dur" API + - Fixed Checkpatch and made linux compatible +## v3.5.2, 18 Oct 2017 +### Changed + - Fixed bug of temperature compensation in pressure + ## v3.5.1, 5 Jul 2017 ### Changed - Fixed bug with overwriting of the result with communication results