Skip to content

Commit a8eace1

Browse files
ljd42kartben
authored andcommitted
drivers: sensor: bme280: fix "config" register intialization issue
A write to the "ctrl_meas" register can cause the sensor to transition from sleep to normal mode (which is default Kconfig settings). As per BME280 datasheet, writes to the "config" register in normal mode may be ignored. This can lead to use BME280_STANDBY_05MS and BME280_FILTER_OFF instead of the values set by the application. To fix this, write to the "config" register before "ctrl_meas" in the sensor init function. Signed-off-by: Loic Domaigne <tech@domaigne.com>
1 parent 38e00d1 commit a8eace1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/sensor/bosch/bme280/bme280.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ static int bme280_chip_init(const struct device *dev)
348348
return -ENOTSUP;
349349
}
350350

351+
/* reset the sensor. This will put the sensor is sleep mode */
351352
err = bme280_reg_write(dev, BME280_REG_RESET, BME280_CMD_SOFT_RESET);
352353
if (err < 0) {
353354
LOG_DBG("Soft-reset failed: %d", err);
@@ -373,17 +374,21 @@ static int bme280_chip_init(const struct device *dev)
373374
}
374375
}
375376

376-
err = bme280_reg_write(dev, BME280_REG_CTRL_MEAS,
377-
BME280_CTRL_MEAS_VAL);
377+
/* Writes to "config" register may be ignored in normal
378+
* mode, but never in sleep mode [datasheet 5.4.6].
379+
*
380+
* So perform "config" write before "ctrl_meas", as "ctrl_meas"
381+
* could cause the sensor to transition from sleep to normal mode.
382+
*/
383+
err = bme280_reg_write(dev, BME280_REG_CONFIG, BME280_CONFIG_VAL);
378384
if (err < 0) {
379-
LOG_DBG("CTRL_MEAS write failed: %d", err);
385+
LOG_DBG("CONFIG write failed: %d", err);
380386
return err;
381387
}
382388

383-
err = bme280_reg_write(dev, BME280_REG_CONFIG,
384-
BME280_CONFIG_VAL);
389+
err = bme280_reg_write(dev, BME280_REG_CTRL_MEAS, BME280_CTRL_MEAS_VAL);
385390
if (err < 0) {
386-
LOG_DBG("CONFIG write failed: %d", err);
391+
LOG_DBG("CTRL_MEAS write failed: %d", err);
387392
return err;
388393
}
389394
/* Wait for the sensor to be ready */

0 commit comments

Comments
 (0)