From 3df9055b4c1a343b386ef225dc4e04a54efa79fe Mon Sep 17 00:00:00 2001 From: lumapu Date: Wed, 20 Jan 2021 19:57:39 +0100 Subject: [PATCH 1/2] Added check connection state for i2c devices If the BME280 isn't connected correctly you can handle this issue in your application. --- src/BME280.cpp | 24 +++++++++++++----------- src/BME280.h | 6 +++++- src/BME280I2C.cpp | 13 +++++++++++++ src/BME280I2C.h | 4 ++++ src/BME280I2C_BRZO.cpp | 12 ++++++++++++ src/BME280I2C_BRZO.h | 4 ++++ src/BME280Spi.cpp | 8 ++++++++ src/BME280Spi.h | 4 ++++ src/BME280SpiSw.cpp | 9 ++++++++- src/BME280SpiSw.h | 4 ++++ 10 files changed, 75 insertions(+), 13 deletions(-) diff --git a/src/BME280.cpp b/src/BME280.cpp index bc57c38..be26b6f 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -48,21 +48,23 @@ bool BME280::Initialize() { bool success(true); - success &= ReadChipID(); + if(IsConnected()) { + success &= ReadChipID(); - if(success) - { - success &= ReadTrim(); - - if(m_settings.filter != Filter_Off) + if(success) { - InitializeFilter(); + success &= ReadTrim(); + + if(m_settings.filter != Filter_Off) + { + InitializeFilter(); + } + + WriteSettings(); } - - WriteSettings(); - } - m_initialized = success; + m_initialized = success; + } return m_initialized; } diff --git a/src/BME280.h b/src/BME280.h index a0c3df0..6e474cd 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -211,7 +211,7 @@ class BME280 virtual bool Initialize(); /////////////////////////////////////////////////////////////// - /// Force a unfiltered measurement to populate the filter + /// Force a unfiltered measurement to populate the filter /// buffer. bool InitializeFilter(); @@ -282,6 +282,10 @@ class BME280 uint8_t data[], uint8_t length)=0; + ///////////////////////////////////////////////////////////////// + /// check if BME280 is connected and available. + virtual bool IsConnected(void)=0; + /*****************************************************************/ /* WORKER FUNCTIONS */ diff --git a/src/BME280I2C.cpp b/src/BME280I2C.cpp index 242127b..1e973bb 100644 --- a/src/BME280I2C.cpp +++ b/src/BME280I2C.cpp @@ -40,6 +40,7 @@ BME280I2C::BME280I2C ):BME280(settings), m_settings(settings) { + Wire.begin(); } @@ -100,3 +101,15 @@ bool BME280I2C::ReadRegister return ord == length; } + + +/****************************************************************/ +bool BME280I2C::IsConnected(void) +{ + bool connected = false; + Wire.beginTransmission(m_settings.bme280Addr); + if(0 == Wire.endTransmission()) + connected = true; + + return connected; +} diff --git a/src/BME280I2C.h b/src/BME280I2C.h index 32cd3ac..35af8f3 100644 --- a/src/BME280I2C.h +++ b/src/BME280I2C.h @@ -101,5 +101,9 @@ class BME280I2C: public BME280 uint8_t data[], uint8_t length); + ///////////////////////////////////////////////////////////////// + /// check if BME280 is connected and available. + virtual bool IsConnected(void); + }; #endif // TG_BME_280_I2C_H diff --git a/src/BME280I2C_BRZO.cpp b/src/BME280I2C_BRZO.cpp index 451cc59..43fdbe5 100644 --- a/src/BME280I2C_BRZO.cpp +++ b/src/BME280I2C_BRZO.cpp @@ -94,4 +94,16 @@ bool BME280I2C_BRZO::ReadRegister return (brzo_i2c_end_transaction()==0); } + +/****************************************************************/ +bool BME280I2C_BRZO::IsConnected(void) +{ + bool connected = false; + brzo_i2c_start_transaction(m_settings.bme280Addr, m_settings.i2cClockRate); + if(0 == brzo_i2c_end_transaction()) + connected = true; + + return connected; +} + #endif diff --git a/src/BME280I2C_BRZO.h b/src/BME280I2C_BRZO.h index ca16532..eb8bc69 100644 --- a/src/BME280I2C_BRZO.h +++ b/src/BME280I2C_BRZO.h @@ -97,5 +97,9 @@ class BME280I2C_BRZO : public BME280I2C uint8_t data[], uint8_t length); + ///////////////////////////////////////////////////////////////// + /// check if BME280 is connected and available. + virtual bool IsConnected(void); + }; #endif // BME280I2C_BRZO_H diff --git a/src/BME280Spi.cpp b/src/BME280Spi.cpp index fe18236..bd162c1 100644 --- a/src/BME280Spi.cpp +++ b/src/BME280Spi.cpp @@ -132,3 +132,11 @@ bool BME280Spi::WriteRegister return true; } + + +/****************************************************************/ +bool BME280Spi::IsConnected(void) { + // not possible here. Connection state can be discovered by reading the + // chip ID which is done during initalization + return true; +} diff --git a/src/BME280Spi.h b/src/BME280Spi.h index a593c94..9aaf257 100644 --- a/src/BME280Spi.h +++ b/src/BME280Spi.h @@ -100,5 +100,9 @@ class BME280Spi: public BME280 uint8_t addr, uint8_t data); + ///////////////////////////////////////////////////////////////// + /// check if BME280 is connected and available. + virtual bool IsConnected(void); + }; #endif // TG_BME_280_SPI_H diff --git a/src/BME280SpiSw.cpp b/src/BME280SpiSw.cpp index 824cfed..2c37615 100644 --- a/src/BME280SpiSw.cpp +++ b/src/BME280SpiSw.cpp @@ -147,6 +147,13 @@ bool BME280SpiSw::WriteRegister // de-select the device digitalWrite(m_settings.spiCsPin, HIGH); -return true; + return true; } + +/****************************************************************/ +bool BME280SpiSw::IsConnected(void) { + // not possible here. Connection state can be discovered by reading the + // chip ID which is done during initalization + return true; +} diff --git a/src/BME280SpiSw.h b/src/BME280SpiSw.h index f2206c3..17453e9 100644 --- a/src/BME280SpiSw.h +++ b/src/BME280SpiSw.h @@ -111,5 +111,9 @@ class BME280SpiSw: public BME280{ uint8_t addr, uint8_t data); + ///////////////////////////////////////////////////////////////// + /// check if BME280 is connected and available. + virtual bool IsConnected(void); + }; #endif // TG_BME_280_SPI_H From 05a485ca2b90a1ab844bb1ad2a18387bf86ddb39 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 27 Feb 2021 23:17:21 +0100 Subject: [PATCH 2/2] IsConnected was removed and connection check is done only in derived class BME280I2C. No connection check in base class any more. --- src/BME280.cpp | 24 +++++++++++------------- src/BME280.h | 6 +----- src/BME280I2C.cpp | 29 +++++++++++++++++------------ src/BME280I2C.h | 13 +++++++++---- src/BME280I2C_BRZO.cpp | 30 ++++++++++++++++++------------ src/BME280I2C_BRZO.h | 13 +++++++++---- src/BME280Spi.cpp | 8 -------- src/BME280Spi.h | 4 ---- src/BME280SpiSw.cpp | 9 +-------- src/BME280SpiSw.h | 4 ---- 10 files changed, 66 insertions(+), 74 deletions(-) diff --git a/src/BME280.cpp b/src/BME280.cpp index be26b6f..bc57c38 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -48,24 +48,22 @@ bool BME280::Initialize() { bool success(true); - if(IsConnected()) { - success &= ReadChipID(); + success &= ReadChipID(); - if(success) - { - success &= ReadTrim(); - - if(m_settings.filter != Filter_Off) - { - InitializeFilter(); - } + if(success) + { + success &= ReadTrim(); - WriteSettings(); + if(m_settings.filter != Filter_Off) + { + InitializeFilter(); } - - m_initialized = success; + + WriteSettings(); } + m_initialized = success; + return m_initialized; } diff --git a/src/BME280.h b/src/BME280.h index 6e474cd..a0c3df0 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -211,7 +211,7 @@ class BME280 virtual bool Initialize(); /////////////////////////////////////////////////////////////// - /// Force a unfiltered measurement to populate the filter + /// Force a unfiltered measurement to populate the filter /// buffer. bool InitializeFilter(); @@ -282,10 +282,6 @@ class BME280 uint8_t data[], uint8_t length)=0; - ///////////////////////////////////////////////////////////////// - /// check if BME280 is connected and available. - virtual bool IsConnected(void)=0; - /*****************************************************************/ /* WORKER FUNCTIONS */ diff --git a/src/BME280I2C.cpp b/src/BME280I2C.cpp index 1e973bb..1720145 100644 --- a/src/BME280I2C.cpp +++ b/src/BME280I2C.cpp @@ -44,6 +44,23 @@ BME280I2C::BME280I2C } +/****************************************************************/ +bool BME280I2C::Initialize() +{ + bool success(false); + Wire.beginTransmission(m_settings.bme280Addr); + if(0 == Wire.endTransmission()) + success = true; + + if(success) + { + success = BME280::Initialize(); + } + + return success; +} + + /****************************************************************/ void BME280I2C::setSettings ( @@ -101,15 +118,3 @@ bool BME280I2C::ReadRegister return ord == length; } - - -/****************************************************************/ -bool BME280I2C::IsConnected(void) -{ - bool connected = false; - Wire.beginTransmission(m_settings.bme280Addr); - if(0 == Wire.endTransmission()) - connected = true; - - return connected; -} diff --git a/src/BME280I2C.h b/src/BME280I2C.h index 35af8f3..196daf1 100644 --- a/src/BME280I2C.h +++ b/src/BME280I2C.h @@ -69,6 +69,15 @@ class BME280I2C: public BME280 BME280I2C( const Settings& settings = Settings()); +/*****************************************************************/ +/* CONSTRUCTOR INIT FUNCTIONS */ +/*****************************************************************/ + + /////////////////////////////////////////////////////////////// + /// Write configuration to BME280, return true if successful. + /// Must be called from any child classes. + virtual bool Initialize(); + /*****************************************************************/ /* ACCESSOR FUNCTIONS */ @@ -101,9 +110,5 @@ class BME280I2C: public BME280 uint8_t data[], uint8_t length); - ///////////////////////////////////////////////////////////////// - /// check if BME280 is connected and available. - virtual bool IsConnected(void); - }; #endif // TG_BME_280_I2C_H diff --git a/src/BME280I2C_BRZO.cpp b/src/BME280I2C_BRZO.cpp index 43fdbe5..3612f97 100644 --- a/src/BME280I2C_BRZO.cpp +++ b/src/BME280I2C_BRZO.cpp @@ -46,6 +46,24 @@ BME280I2C_BRZO::BME280I2C_BRZO { } + +/****************************************************************/ +bool BME280I2C_BRZO::Initialize() +{ + bool success(false); + brzo_i2c_start_transaction(m_settings.bme280Addr, m_settings.i2cClockRate); + if(0 == brzo_i2c_end_transaction()) + success = true; + + if(success) + { + success = BME280::Initialize(); + } + + return success; +} + + /****************************************************************/ void BME280I2C_BRZO::setSettings ( @@ -94,16 +112,4 @@ bool BME280I2C_BRZO::ReadRegister return (brzo_i2c_end_transaction()==0); } - -/****************************************************************/ -bool BME280I2C_BRZO::IsConnected(void) -{ - bool connected = false; - brzo_i2c_start_transaction(m_settings.bme280Addr, m_settings.i2cClockRate); - if(0 == brzo_i2c_end_transaction()) - connected = true; - - return connected; -} - #endif diff --git a/src/BME280I2C_BRZO.h b/src/BME280I2C_BRZO.h index eb8bc69..fce535a 100644 --- a/src/BME280I2C_BRZO.h +++ b/src/BME280I2C_BRZO.h @@ -65,6 +65,15 @@ class BME280I2C_BRZO : public BME280I2C BME280I2C_BRZO( const Settings& settings = Settings()); +/*****************************************************************/ +/* CONSTRUCTOR INIT FUNCTIONS */ +/*****************************************************************/ + + /////////////////////////////////////////////////////////////// + /// Write configuration to BME280, return true if successful. + /// Must be called from any child classes. + virtual bool Initialize(); + /*****************************************************************/ /* ACCESSOR FUNCTIONS */ @@ -97,9 +106,5 @@ class BME280I2C_BRZO : public BME280I2C uint8_t data[], uint8_t length); - ///////////////////////////////////////////////////////////////// - /// check if BME280 is connected and available. - virtual bool IsConnected(void); - }; #endif // BME280I2C_BRZO_H diff --git a/src/BME280Spi.cpp b/src/BME280Spi.cpp index bd162c1..fe18236 100644 --- a/src/BME280Spi.cpp +++ b/src/BME280Spi.cpp @@ -132,11 +132,3 @@ bool BME280Spi::WriteRegister return true; } - - -/****************************************************************/ -bool BME280Spi::IsConnected(void) { - // not possible here. Connection state can be discovered by reading the - // chip ID which is done during initalization - return true; -} diff --git a/src/BME280Spi.h b/src/BME280Spi.h index 9aaf257..a593c94 100644 --- a/src/BME280Spi.h +++ b/src/BME280Spi.h @@ -100,9 +100,5 @@ class BME280Spi: public BME280 uint8_t addr, uint8_t data); - ///////////////////////////////////////////////////////////////// - /// check if BME280 is connected and available. - virtual bool IsConnected(void); - }; #endif // TG_BME_280_SPI_H diff --git a/src/BME280SpiSw.cpp b/src/BME280SpiSw.cpp index 2c37615..824cfed 100644 --- a/src/BME280SpiSw.cpp +++ b/src/BME280SpiSw.cpp @@ -147,13 +147,6 @@ bool BME280SpiSw::WriteRegister // de-select the device digitalWrite(m_settings.spiCsPin, HIGH); - return true; +return true; } - -/****************************************************************/ -bool BME280SpiSw::IsConnected(void) { - // not possible here. Connection state can be discovered by reading the - // chip ID which is done during initalization - return true; -} diff --git a/src/BME280SpiSw.h b/src/BME280SpiSw.h index 17453e9..f2206c3 100644 --- a/src/BME280SpiSw.h +++ b/src/BME280SpiSw.h @@ -111,9 +111,5 @@ class BME280SpiSw: public BME280{ uint8_t addr, uint8_t data); - ///////////////////////////////////////////////////////////////// - /// check if BME280 is connected and available. - virtual bool IsConnected(void); - }; #endif // TG_BME_280_SPI_H