check validity of gas readings and if heater managed to reach target temp within given timeframe

This commit is contained in:
William Clark 2023-11-06 17:02:41 +00:00
parent 12918a6a7d
commit 9e67234a31
3 changed files with 21 additions and 6 deletions

View File

@ -83,6 +83,8 @@ int bme680_init(bme680_t *bme680, uint8_t mode) {
bme680->mode = mode; bme680->mode = mode;
bme680->spi_page = 0; bme680->spi_page = 0;
bme680->gas_valid = 0;
bme680->heat_stab = 0;
if (bme680->dev.init() != 0) { if (bme680->dev.init() != 0) {
return 1; return 1;
@ -177,10 +179,6 @@ int bme680_configure(bme680_t *bme680) {
err |= write_dev(bme680, REG_CTRL_GAS_1, ctrl_gas1); err |= write_dev(bme680, REG_CTRL_GAS_1, ctrl_gas1);
err |= write_dev(bme680, REG_CTRL_GAS_0, ctrl_gas0); err |= write_dev(bme680, REG_CTRL_GAS_0, ctrl_gas0);
// TODO: check gas_valid_r and heat_stab_r in 0x2B
SKIP_GAS: SKIP_GAS:
return err; return err;
} }
@ -249,12 +247,21 @@ int bme680_read(bme680_t *bme680) {
} }
/* read gas adc ?*/ /* read gas adc ?*/
// TODO: read 2 bytes once
if (BME680_GAS_ENABLED(bme680->mode)) { if (BME680_GAS_ENABLED(bme680->mode)) {
err |= read_dev(bme680, 0x2A, buffer, 2); err |= read_dev(bme680, 0x2A, buffer, 2);
bme680->adc.gas = (buffer[0] << 2) | (buffer[1] >> 6); bme680->adc.gas = (buffer[0] << 2) | (buffer[1] >> 6);
err |= read_dev(bme680, 0x2B, buffer, 1); err |= read_dev(bme680, 0x2B, buffer, 1);
bme680->adc.gas_range = buffer[0] & 0xF; bme680->adc.gas_range = buffer[0] & 0xF;
/* check gas validity status (if one actually took place ??? ) */
err |= read_dev(bme680, 0x2B, buffer, 1);
bme680->gas_valid = (buffer[0] >> 5) & 1 ;
/* check heater stability. if it managed to get to temp within given time + preload current */
err |= read_dev(bme680, 0x2B, buffer, 1);
bme680->heat_stab = (buffer[0] >> 4) & 1;
} }
/* read/convert in order ..*/ /* read/convert in order ..*/

View File

@ -135,6 +135,8 @@ struct bme680 {
struct bme680_adc adc; struct bme680_adc adc;
uint8_t mode; uint8_t mode;
uint8_t spi_page; uint8_t spi_page;
uint8_t gas_valid;
uint8_t heat_stab;
}; };
typedef struct bme680 bme680_t; typedef struct bme680 bme680_t;

10
main.c
View File

@ -76,9 +76,9 @@ int main(void) {
/* define the time between the start of heating and start of resistance sensing in this s.p.*/ /* define the time between the start of heating and start of resistance sensing in this s.p.*/
/* Bosch datasheet suggests ~30 - 40ms is usually all that is required to get up to temp. */ /* Bosch datasheet suggests ~30 - 40ms is usually all that is required to get up to temp. */
/* 50 * X4 = 200 ms wait before sampling resistance starts. */ /* 25 * X4 = 100 ms wait before sampling resistance starts. */
/* the first value is 6-bit (0...64) with 1 ms step size. */ /* the first value is 6-bit (0...64) with 1 ms step size. */
bme680.cfg.gas_wait[i] = BME680_GAS_WAIT(50, BME680_GAS_WAIT_X4); bme680.cfg.gas_wait[i] = BME680_GAS_WAIT(25, BME680_GAS_WAIT_X4);
} }
/* The BME680 does not cycle between setpoints. They have to be manually set. */ /* The BME680 does not cycle between setpoints. They have to be manually set. */
@ -140,6 +140,12 @@ int main(void) {
} }
} }
/* print out heat stability and gas valid flags. should both be 1 !! */
if (BME680_GAS_ENABLED(bme680.mode)) {
printf("=== gas_valid_r: %d\n", bme680.gas_valid);
printf("=== heat_stab_r: %d\n", bme680.heat_stab);
}
bme680_deinit(&bme680); bme680_deinit(&bme680);
return 0; return 0;