From 599a455150d085209c5f0314cbdbaa9d59e0be36 Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Mon, 8 Aug 2022 05:50:15 +1000 Subject: [PATCH] Add a soft reset in setup() for bme280. (#3615) --- esphome/components/bme280/bme280.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/esphome/components/bme280/bme280.cpp b/esphome/components/bme280/bme280.cpp index a4ea8d608e..345b24a36e 100644 --- a/esphome/components/bme280/bme280.cpp +++ b/esphome/components/bme280/bme280.cpp @@ -1,4 +1,5 @@ #include "bme280.h" +#include "esphome/core/hal.h" #include "esphome/core/log.h" namespace esphome { @@ -28,6 +29,7 @@ static const uint8_t BME280_REGISTER_DIG_H5 = 0xE5; static const uint8_t BME280_REGISTER_DIG_H6 = 0xE7; static const uint8_t BME280_REGISTER_CHIPID = 0xD0; +static const uint8_t BME280_REGISTER_RESET = 0xE0; static const uint8_t BME280_REGISTER_CONTROLHUMID = 0xF2; static const uint8_t BME280_REGISTER_STATUS = 0xF3; @@ -39,6 +41,8 @@ static const uint8_t BME280_REGISTER_TEMPDATA = 0xFA; static const uint8_t BME280_REGISTER_HUMIDDATA = 0xFD; static const uint8_t BME280_MODE_FORCED = 0b01; +static const uint8_t BME280_SOFT_RESET = 0xB6; +static const uint8_t BME280_STATUS_IM_UPDATE = 0b01; inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); } @@ -97,6 +101,28 @@ void BME280Component::setup() { return; } + // Send a soft reset. + if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) { + this->mark_failed(); + return; + } + // Wait until the NVM data has finished loading. + uint8_t status; + uint8_t retry = 5; + do { + delay(2); + if (!this->read_byte(BME280_REGISTER_STATUS, &status)) { + ESP_LOGW(TAG, "Error reading status register."); + this->mark_failed(); + return; + } + } while ((status & BME280_STATUS_IM_UPDATE) && (--retry)); + if (status & BME280_STATUS_IM_UPDATE) { + ESP_LOGW(TAG, "Timeout loading NVM."); + this->mark_failed(); + return; + } + // Read calibration this->calibration_.t1 = read_u16_le_(BME280_REGISTER_DIG_T1); this->calibration_.t2 = read_s16_le_(BME280_REGISTER_DIG_T2);