From fb14f9df4905154788afb0bcac81351f75e687c5 Mon Sep 17 00:00:00 2001
From: Matteo Pignataro <matteo.pignataro@skywarder.eu>
Date: Sat, 19 Mar 2022 11:48:20 +0100
Subject: [PATCH] [BME280] Repair init

---
 src/shared/sensors/BME280/BME280.cpp | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/shared/sensors/BME280/BME280.cpp b/src/shared/sensors/BME280/BME280.cpp
index ccc4e90af..fe621101c 100644
--- a/src/shared/sensors/BME280/BME280.cpp
+++ b/src/shared/sensors/BME280/BME280.cpp
@@ -66,13 +66,17 @@ bool BME280::init()
 
     loadCompensationParameters();
 
-    // Read once the temperature to compute fineTemperature
-    setConfiguration(BME280_CONFIG_TEMP_SINGLE);
-    miosix::Thread::sleep(
-        calculateMaxMeasurementTime(BME280_CONFIG_TEMP_SINGLE));
-    readTemperature();
+    // Set the configuration 10 times to be sure
+    for (int i = 0; i < 10; i++)
+    {
+        // Read once the temperature to compute fineTemperature
+        setConfiguration(BME280_CONFIG_TEMP_SINGLE);
+        miosix::Thread::sleep(
+            calculateMaxMeasurementTime(BME280_CONFIG_TEMP_SINGLE));
+        readTemperature();
 
-    setConfiguration();
+        setConfiguration();
+    }
 
     // Set a sleep time to allow the sensor to change internally the data
     miosix::Thread::sleep(100);
@@ -83,7 +87,7 @@ bool BME280::init()
     bool readConfigResult = false;
     BME280Config readBackConfig;
 
-    for (int i = 0; i < 5; i++)
+    for (int i = 0; i < 10; i++)
     {
         readBackConfig = readConfiguration();
         // Check if the configration on the device matches ours
@@ -97,11 +101,11 @@ bool BME280::init()
         }
 
         // After the check i sleep 100 milliseconds
-        miosix::Thread::sleep(100);
+        miosix::Thread::sleep(20);
     }
 
-    // If after the 5 iterations the sensor didn't report the configuration set
-    // I can report the init error
+    //   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");
-- 
GitLab