From b5046959c5b80dfc645060d36c246344f33dfab5 Mon Sep 17 00:00:00 2001
From: Matteo Pignataro <matteo.pignataro@skywarder.eu>
Date: Thu, 17 Mar 2022 11:22:45 +0100
Subject: [PATCH] [BME280] Driver adjustments related to timing issues

---
 src/shared/sensors/BME280/BME280.cpp | 36 +++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/shared/sensors/BME280/BME280.cpp b/src/shared/sensors/BME280/BME280.cpp
index 907b92432..233e6b14d 100644
--- a/src/shared/sensors/BME280/BME280.cpp
+++ b/src/shared/sensors/BME280/BME280.cpp
@@ -84,19 +84,39 @@ bool BME280::init()
 
     setConfiguration();
 
-    BME280Config readBackConfig = readConfiguration();
+    // Set a sleep time to allow the sensor to change internally the data
+    miosix::Thread::sleep(100);
 
-    // Check if the configration on the device matches ours
-    if (config.bytes.ctrlHumidity != readBackConfig.bytes.ctrlHumidity ||
-        config.bytes.ctrlPressureAndTemperature !=
-            readBackConfig.bytes.ctrlPressureAndTemperature ||
-        config.bytes.config != readBackConfig.bytes.config)
+    // I create the config state which represents the logic or of all the
+    // 5 readConfiguration controls (We perform 5 checks to avoid that the
+    // sensor is busy implicating in wrong responses)
+    bool readConfigResult = false;
+    BME280Config readBackConfig;
+
+    for (int i = 0; i < 5; i++)
+    {
+        readBackConfig = readConfiguration();
+        // Check if the configration on the device matches ours
+        if (config.bytes.ctrlHumidity == readBackConfig.bytes.ctrlHumidity &&
+            config.bytes.ctrlPressureAndTemperature ==
+                readBackConfig.bytes.ctrlPressureAndTemperature &&
+            config.bytes.config == readBackConfig.bytes.config)
+        {
+            readConfigResult = true;
+            break;
+        }
+
+        // After the check i sleep 100 milliseconds
+        miosix::Thread::sleep(100);
+    }
+
+    // If after the 5 iterations the sensor didn't report the configuration set
+    // I can report the init error
+    if (readConfigResult)
     {
         LOG_ERR(logger, "Device configuration incorrect, setup failed");
 
         lastError = SensorErrors::NOT_INIT;
-
-        return false;
     }
 
     initialized = true;
-- 
GitLab