Skip to content

Commit

Permalink
Updated to v4.4.4. Reduced transactions required by the parallel mode…
Browse files Browse the repository at this point in the history
… and sequential mode reads. Updated the measurement duration calculation API to return time in microseconds.
  • Loading branch information
BST-Github-Admin authored and kegov committed Mar 18, 2021
1 parent e104fe5 commit 90aebea
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 78 deletions.
149 changes: 125 additions & 24 deletions bme68x.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme68x.c
* @date 2020-11-02
* @version v4.4.2
* @date 2021-03-18
* @version v4.4.4
*
*/

Expand Down Expand Up @@ -84,9 +84,12 @@ static uint8_t calc_res_heat(uint16_t temp, const struct bme68x_dev *dev);

#endif

/* This internal API is used to calculate the field data of sensor */
/* This internal API is used to read a single data of the sensor */
static int8_t read_field_data(uint8_t index, struct bme68x_data *data, struct bme68x_dev *dev);

/* This internal API is used to read all data fields of the sensor */
static int8_t read_all_field_data(struct bme68x_data * const data[], struct bme68x_dev *dev);

/* This internal API is used to switch between SPI memory pages */
static int8_t set_mem_page(uint8_t reg_addr, struct bme68x_dev *dev);

Expand Down Expand Up @@ -478,31 +481,49 @@ int8_t bme68x_get_op_mode(uint8_t *op_mode, struct bme68x_dev *dev)
/*
* @brief This API is used to get the remaining duration that can be used for heating.
*/
uint16_t bme68x_get_meas_dur(const uint8_t op_mode, const struct bme68x_conf *conf)
uint32_t bme68x_get_meas_dur(const uint8_t op_mode, struct bme68x_conf *conf, struct bme68x_dev *dev)
{
uint32_t tph_dur = 0; /* Calculate in us */
int8_t rslt;
uint32_t meas_dur = 0; /* Calculate in us */
uint32_t meas_cycles;
uint8_t os_to_meas_cycles[6] = { 0, 1, 2, 4, 8, 16 };

if (conf != NULL)
{
meas_cycles = os_to_meas_cycles[conf->os_temp];
meas_cycles += os_to_meas_cycles[conf->os_pres];
meas_cycles += os_to_meas_cycles[conf->os_hum];

/* TPH measurement duration */
tph_dur = meas_cycles * UINT32_C(1963);
tph_dur += UINT32_C(477 * 4); /* TPH switching duration */
tph_dur += UINT32_C(477 * 5); /* Gas measurement duration */
tph_dur += UINT32_C(500); /* Get it to the closest integer when converted to ms.*/
tph_dur /= UINT32_C(1000); /* Convert to ms */
if (op_mode != BME68X_PARALLEL_MODE)
/* Boundary check for temperature oversampling */
rslt = boundary_check(&conf->os_temp, BME68X_OS_16X, dev);

if (rslt == BME68X_OK)
{
/* Boundary check for pressure oversampling */
rslt = boundary_check(&conf->os_pres, BME68X_OS_16X, dev);
}

if (rslt == BME68X_OK)
{
tph_dur += UINT32_C(1); /* Wake up duration of 1ms */
/* Boundary check for humidity oversampling */
rslt = boundary_check(&conf->os_hum, BME68X_OS_16X, dev);
}

if (rslt == BME68X_OK)
{
meas_cycles = os_to_meas_cycles[conf->os_temp];
meas_cycles += os_to_meas_cycles[conf->os_pres];
meas_cycles += os_to_meas_cycles[conf->os_hum];

/* TPH measurement duration */
meas_dur = meas_cycles * UINT32_C(1963);
meas_dur += UINT32_C(477 * 4); /* TPH switching duration */
meas_dur += UINT32_C(477 * 5); /* Gas measurement duration */

if (op_mode != BME68X_PARALLEL_MODE)
{
meas_dur += UINT32_C(1000); /* Wake up duration of 1ms */
}
}
}

return (uint16_t)tph_dur;
return meas_dur;
}

/*
Expand All @@ -514,8 +535,8 @@ int8_t bme68x_get_data(uint8_t op_mode, struct bme68x_data *data, uint8_t *n_dat
{
int8_t rslt;
uint8_t i = 0, j = 0, new_fields = 0;
struct bme68x_data *field_ptr[3];
struct bme68x_data field_data[3];
struct bme68x_data *field_ptr[3] = { 0 };
struct bme68x_data field_data[3] = { { 0 } };

field_ptr[0] = &field_data[0];
field_ptr[1] = &field_data[1];
Expand Down Expand Up @@ -544,9 +565,11 @@ int8_t bme68x_get_data(uint8_t op_mode, struct bme68x_data *data, uint8_t *n_dat
else if ((op_mode == BME68X_PARALLEL_MODE) || (op_mode == BME68X_SEQUENTIAL_MODE))
{
/* Read the 3 fields and count the number of new data fields */
for (i = 0; ((i < 3) && (rslt == BME68X_OK)); i++)
rslt = read_all_field_data(field_ptr, dev);

new_fields = 0;
for (i = 0; (i < 3) && (rslt == BME68X_OK); i++)
{
rslt = read_field_data(i, field_ptr[i], dev);
if (field_ptr[i]->status & BME68X_NEW_DATA_MSK)
{
new_fields++;
Expand Down Expand Up @@ -1126,7 +1149,7 @@ static uint8_t calc_gas_wait(uint16_t dur)
return durval;
}

/* This internal API is used to calculate the field data of sensor */
/* This internal API is used to read a single data of the sensor */
static int8_t read_field_data(uint8_t index, struct bme68x_data *data, struct bme68x_dev *dev)
{
int8_t rslt = BME68X_OK;
Expand Down Expand Up @@ -1215,6 +1238,84 @@ static int8_t read_field_data(uint8_t index, struct bme68x_data *data, struct bm
return rslt;
}

/* This internal API is used to read all data fields of the sensor */
static int8_t read_all_field_data(struct bme68x_data * const data[], struct bme68x_dev *dev)
{
int8_t rslt = BME68X_OK;
uint8_t buff[BME68X_LEN_FIELD * 3] = { 0 };
uint8_t gas_range_l, gas_range_h;
uint32_t adc_temp;
uint32_t adc_pres;
uint16_t adc_hum;
uint16_t adc_gas_res_low, adc_gas_res_high;
uint8_t off;
uint8_t set_val[30] = { 0 }; /* idac, res_heat, gas_wait */
uint8_t i;

if (!data[0] && !data[1] && !data[2])
{
rslt = BME68X_E_NULL_PTR;
}

if (rslt == BME68X_OK)
{
rslt = bme68x_get_regs(BME68X_REG_FIELD0, buff, (uint32_t) BME68X_LEN_FIELD * 3, dev);
}

if (rslt == BME68X_OK)
{
rslt = bme68x_get_regs(BME68X_REG_IDAC_HEAT0, set_val, 30, dev);
}

for (i = 0; ((i < 3) && (rslt == BME68X_OK)); i++)
{
off = (uint8_t)(i * BME68X_LEN_FIELD);
data[i]->status = buff[off] & BME68X_NEW_DATA_MSK;
data[i]->gas_index = buff[off] & BME68X_GAS_INDEX_MSK;
data[i]->meas_index = buff[off + 1];

/* read the raw data from the sensor */
adc_pres =
(uint32_t) (((uint32_t) buff[off + 2] * 4096) | ((uint32_t) buff[off + 3] * 16) |
((uint32_t) buff[off + 4] / 16));
adc_temp =
(uint32_t) (((uint32_t) buff[off + 5] * 4096) | ((uint32_t) buff[off + 6] * 16) |
((uint32_t) buff[off + 7] / 16));
adc_hum = (uint16_t) (((uint32_t) buff[off + 8] * 256) | (uint32_t) buff[off + 9]);
adc_gas_res_low = (uint16_t) ((uint32_t) buff[off + 13] * 4 | (((uint32_t) buff[off + 14]) / 64));
adc_gas_res_high = (uint16_t) ((uint32_t) buff[off + 15] * 4 | (((uint32_t) buff[off + 16]) / 64));
gas_range_l = buff[off + 14] & BME68X_GAS_RANGE_MSK;
gas_range_h = buff[off + 16] & BME68X_GAS_RANGE_MSK;
if (dev->variant_id == BME68X_VARIANT_GAS_HIGH)
{
data[i]->status |= buff[off + 16] & BME68X_GASM_VALID_MSK;
data[i]->status |= buff[off + 16] & BME68X_HEAT_STAB_MSK;
}
else
{
data[i]->status |= buff[off + 14] & BME68X_GASM_VALID_MSK;
data[i]->status |= buff[off + 14] & BME68X_HEAT_STAB_MSK;
}

data[i]->idac = set_val[data[i]->gas_index];
data[i]->res_heat = set_val[10 + data[i]->gas_index];
data[i]->gas_wait = set_val[20 + data[i]->gas_index];
data[i]->temperature = calc_temperature(adc_temp, dev);
data[i]->pressure = calc_pressure(adc_pres, dev);
data[i]->humidity = calc_humidity(adc_hum, dev);
if (dev->variant_id == BME68X_VARIANT_GAS_HIGH)
{
data[i]->gas_resistance = calc_gas_resistance(adc_gas_res_high, gas_range_h, dev);
}
else
{
data[i]->gas_resistance = calc_gas_resistance(adc_gas_res_low, gas_range_l, dev);
}
}

return rslt;
}

/* This internal API is used to switch between SPI memory pages */
static int8_t set_mem_page(uint8_t reg_addr, struct bme68x_dev *dev)
{
Expand Down Expand Up @@ -1380,7 +1481,7 @@ static int8_t set_conf(const struct bme68x_heatr_conf *conf, uint8_t op_mode, ui
rh_reg_addr[i] = BME68X_REG_RES_HEAT0 + i;
rh_reg_data[i] = calc_res_heat(conf->heatr_temp_prof[i], dev);
gw_reg_addr[i] = BME68X_REG_GAS_WAIT0 + i;
gw_reg_data[i] = (uint8_t)conf->heatr_dur_prof[i];
gw_reg_data[i] = (uint8_t) conf->heatr_dur_prof[i];
}

(*nb_conv) = conf->profile_len;
Expand Down
20 changes: 11 additions & 9 deletions bme68x.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme68x.h
* @date 2020-11-02
* @version v4.4.2
* @date 2021-03-18
* @version v4.4.4
*
*/

/*!
* @defgroup bme68x BME68X
* @brief <a href="https://www.bosch-sensortec.com/bst/products/all_products/bme68x">Product Overview</a>
* and <a href="https://github.com/BoschSensortec/BME68X_driver">Sensor API Source Code</a>
* @brief <a href="https://www.bosch-sensortec.com/bst/products/all_products/bme680">Product Overview</a>
* and <a href="https://github.com/BoschSensortec/BME68x-Sensor-API">Sensor API Source Code</a>
*/

#ifndef BME68X_H_
Expand Down Expand Up @@ -186,15 +186,17 @@ int8_t bme68x_get_op_mode(uint8_t *op_mode, struct bme68x_dev *dev);
* \ingroup bme68xApiConfig
* \page bme68x_api_bme68x_get_meas_dur bme68x_get_meas_dur
* \code
* uint16_t bme68x_get_meas_dur(const uint8_t op_mode, const struct bme68x_conf *conf);
* uint32_t bme68x_get_meas_dur(const uint8_t op_mode, struct bme68x_conf *conf, struct bme68x_dev *dev);
* \endcode
* @details This API is used to get the remaining duration that can be used for heating.
*
* @return Result of API execution status
* @retval 0 -> Success
* @retval < 0 -> Fail
* @param[in] op_mode : Desired operation mode.
* @param[in] conf : Desired sensor configuration.
* @param[in] dev : Structure instance of bme68x_dev
*
* @return Measurement duration calculated in microseconds
*/
uint16_t bme68x_get_meas_dur(const uint8_t op_mode, const struct bme68x_conf *conf);
uint32_t bme68x_get_meas_dur(const uint8_t op_mode, struct bme68x_conf *conf, struct bme68x_dev *dev);

/**
* \ingroup bme68x
Expand Down
8 changes: 4 additions & 4 deletions bme68x_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme68x_defs.h
* @date 2020-11-02
* @version v4.4.2
* @date 2021-03-18
* @version v4.4.4
*
*/

Expand Down Expand Up @@ -687,7 +687,7 @@ typedef BME68X_INTF_RET_TYPE (*bme68x_write_fptr_t)(uint8_t reg_addr, const uint
* @param[in,out] intf_ptr : Void pointer that can enable the linking of descriptors
* for interface related callbacks
*/
typedef void (*bm68x_delay_us_fptr_t)(uint32_t period, void *intf_ptr);
typedef void (*bme68x_delay_us_fptr_t)(uint32_t period, void *intf_ptr);

/*
* @brief Generic communication function pointer
Expand Down Expand Up @@ -959,7 +959,7 @@ struct bme68x_dev
bme68x_write_fptr_t write;

/*! Delay function pointer */
bm68x_delay_us_fptr_t delay_us;
bme68x_delay_us_fptr_t delay_us;

/*! To store interface pointer error */
BME68X_INTF_RET_TYPE intf_rslt;
Expand Down
24 changes: 19 additions & 5 deletions examples/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,25 @@ void bme68x_check_rslt(const char api_name[], int8_t rslt)
/* Do nothing */
break;
case BME68X_E_NULL_PTR:
printf("Error [%d] : Null pointer\r\n", rslt);
printf("API name [%s] Error [%d] : Null pointer\r\n", api_name, rslt);
break;
case BME68X_E_COM_FAIL:
printf("Error [%d] : Communication failure\r\n", rslt);
printf("API name [%s] Error [%d] : Communication failure\r\n", api_name, rslt);
break;
case BME68X_E_INVALID_LENGTH:
printf("Error [%d] : Incorrect length parameter\r\n", rslt);
printf("API name [%s] Error [%d] : Incorrect length parameter\r\n", api_name, rslt);
break;
case BME68X_E_DEV_NOT_FOUND:
printf("Error [%d] : Device not found\r\n", rslt);
printf("API name [%s] Error [%d] : Device not found\r\n", api_name, rslt);
break;
case BME68X_E_SELF_TEST:
printf("API name [%s] Error [%d] : Self test error\r\n", api_name, rslt);
break;
case BME68X_W_NO_NEW_DATA:
printf("API name [%s] Warning [%d] : No new data found\r\n", api_name, rslt);
break;
default:
printf("Error [%d] : Unknown error code\r\n", rslt);
printf("API name [%s] Error [%d] : Unknown error code\r\n", api_name, rslt);
break;
}
}
Expand Down Expand Up @@ -173,5 +179,13 @@ int8_t bme68x_interface_init(struct bme68x_dev *bme, uint8_t intf)

void bme68x_coines_deinit(void)
{
fflush(stdout);

coines_set_shuttleboard_vdd_vddio_config(0, 0);
coines_delay_msec(1000);

/* Coines interface reset */
coines_soft_reset();
coines_delay_msec(1000);
coines_close_comm_intf(COINES_COMM_INTF_USB);
}
Loading

0 comments on commit 90aebea

Please sign in to comment.