From e778a445d946aec64893a9278ee9dd2ab37fcaf9 Mon Sep 17 00:00:00 2001 From: melyux <10296053+melyux@users.noreply.github.com> Date: Wed, 25 Jan 2023 15:11:10 -0800 Subject: [PATCH] Add a soft reset in setup() for bmp280 (#4329) fixes https://github.com/esphome/issues/issues/3383 --- esphome/components/bmp280/bmp280.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/esphome/components/bmp280/bmp280.cpp b/esphome/components/bmp280/bmp280.cpp index bda34e6c2b..a5b2517893 100644 --- a/esphome/components/bmp280/bmp280.cpp +++ b/esphome/components/bmp280/bmp280.cpp @@ -1,4 +1,5 @@ #include "bmp280.h" +#include "esphome/core/hal.h" #include "esphome/core/log.h" namespace esphome { @@ -11,8 +12,11 @@ static const uint8_t BMP280_REGISTER_CONTROL = 0xF4; static const uint8_t BMP280_REGISTER_CONFIG = 0xF5; static const uint8_t BMP280_REGISTER_PRESSUREDATA = 0xF7; static const uint8_t BMP280_REGISTER_TEMPDATA = 0xFA; +static const uint8_t BMP280_REGISTER_RESET = 0xE0; static const uint8_t BMP280_MODE_FORCED = 0b01; +static const uint8_t BMP280_SOFT_RESET = 0xB6; +static const uint8_t BMP280_STATUS_IM_UPDATE = 0b01; inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); } @@ -66,6 +70,28 @@ void BMP280Component::setup() { return; } + // Send a soft reset. + if (!this->write_byte(BMP280_REGISTER_RESET, BMP280_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(BMP280_REGISTER_STATUS, &status)) { + ESP_LOGW(TAG, "Error reading status register."); + this->mark_failed(); + return; + } + } while ((status & BMP280_STATUS_IM_UPDATE) && (--retry)); + if (status & BMP280_STATUS_IM_UPDATE) { + ESP_LOGW(TAG, "Timeout loading NVM."); + this->mark_failed(); + return; + } + // Read calibration this->calibration_.t1 = this->read_u16_le_(0x88); this->calibration_.t2 = this->read_s16_le_(0x8A);