drivers: bme280: fetch data after measurement is done
After powering-on the sensor, and before every measurement, it loads the NMV. We must wait until this process is completed otherwise we will read weird values. Since it was observed that the time may be a bit long after a cold start, it is more convinient to just wait until the sensor iready, without a timout. Signed-off-by: Efrain Calderon <efrain.calderon@aquarobur.com>
This commit is contained in:
parent
c009450d44
commit
5274aa5dc2
2 changed files with 29 additions and 9 deletions
|
@ -170,6 +170,23 @@ static void bme280_compensate_humidity(struct bme280_data *data,
|
|||
data->comp_humidity = (uint32_t)(h >> 12);
|
||||
}
|
||||
|
||||
static int bme280_wait_until_ready(const struct device *dev)
|
||||
{
|
||||
uint8_t status = 0;
|
||||
int ret;
|
||||
|
||||
/* Wait for NVM to copy and and measurement to be completed */
|
||||
do {
|
||||
k_sleep(K_MSEC(3));
|
||||
ret = bme280_reg_read(dev, BME280_REG_STATUS, &status, 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
} while (status & (BME280_STATUS_MEASURING | BME280_STATUS_IM_UPDATE));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bme280_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
{
|
||||
|
@ -192,16 +209,13 @@ static int bme280_sample_fetch(const struct device *dev,
|
|||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
do {
|
||||
k_sleep(K_MSEC(3));
|
||||
ret = bme280_reg_read(dev, BME280_REG_STATUS, buf, 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
} while (buf[0] & 0x08);
|
||||
#endif
|
||||
|
||||
ret = bme280_wait_until_ready(dev);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (data->chip_id == BME280_CHIP_ID) {
|
||||
size = 8;
|
||||
}
|
||||
|
@ -356,6 +370,10 @@ static int bme280_chip_init(const struct device *dev)
|
|||
err = bme280_reg_write(dev, BME280_REG_RESET, BME280_CMD_SOFT_RESET);
|
||||
if (err < 0) {
|
||||
LOG_DBG("Soft-reset failed: %d", err);
|
||||
}
|
||||
|
||||
err = bme280_wait_until_ready(dev);
|
||||
if (err < 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
|
@ -74,6 +74,8 @@ extern const struct bme280_bus_io bme280_bus_io_i2c;
|
|||
#define BME280_MODE_NORMAL 0x03
|
||||
#define BME280_SPI_3W_DISABLE 0x00
|
||||
#define BME280_CMD_SOFT_RESET 0xB6
|
||||
#define BME280_STATUS_MEASURING 0x08
|
||||
#define BME280_STATUS_IM_UPDATE 0x01
|
||||
|
||||
#if defined CONFIG_BME280_MODE_NORMAL
|
||||
#define BME280_MODE BME280_MODE_NORMAL
|
||||
|
@ -155,7 +157,7 @@ extern const struct bme280_bus_io bme280_bus_io_i2c;
|
|||
BME280_SPI_3W_DISABLE)
|
||||
|
||||
|
||||
#define BME280_CTRL_MEAS_OFF_VAL (BME280_PRESS_OVER | \
|
||||
#define BME280_CTRL_MEAS_OFF_VAL (BME280_PRESS_OVER | \
|
||||
BME280_TEMP_OVER | \
|
||||
BME280_MODE_SLEEP)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue