[Silabs] Refactor Sensor code (#35979)
* Refactor Sensor and LCD code
* Address review comments
diff --git a/examples/platform/silabs/Si70xxSensor.cpp b/examples/platform/silabs/Si70xxSensor.cpp
new file mode 100644
index 0000000..0b03ca0
--- /dev/null
+++ b/examples/platform/silabs/Si70xxSensor.cpp
@@ -0,0 +1,67 @@
+/*
+ *
+ * Copyright (c) 2020 Project CHIP Authors
+ * Copyright (c) 2019 Google LLC.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "sl_board_control.h"
+#include "sl_i2cspm_instances.h"
+#include "sl_si70xx.h"
+#include <Si70xxSensor.h>
+#include <lib/support/CodeUtils.h>
+
+namespace {
+
+constexpr uint16_t kSensorTemperatureOffset = 475;
+bool initialized = false;
+
+} // namespace
+
+namespace Si70xxSensor {
+
+sl_status_t Init()
+{
+ sl_status_t status = SL_STATUS_OK;
+
+ status = sl_board_enable_sensor(SL_BOARD_SENSOR_RHT);
+ VerifyOrReturnError(status == SL_STATUS_OK, status);
+
+ status = sl_si70xx_init(sl_i2cspm_sensor, SI7021_ADDR);
+ VerifyOrReturnError(status == SL_STATUS_OK, status);
+
+ initialized = true;
+ return status;
+}
+
+sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature)
+{
+ VerifyOrReturnError(initialized, SL_STATUS_NOT_INITIALIZED);
+
+ sl_status_t status = SL_STATUS_OK;
+ int32_t tempTemperature = 0;
+ uint32_t tempHumidity = 0;
+
+ status = sl_si70xx_measure_rh_and_temp(sl_i2cspm_sensor, SI7021_ADDR, &tempHumidity, &tempTemperature);
+ VerifyOrReturnError(status == SL_STATUS_OK, status);
+
+ // Sensor precision is milliX. We need to reduce to change the precision to centiX to fit with the cluster attributes presicion.
+ temperature = static_cast<int16_t>(tempTemperature / 10) - kSensorTemperatureOffset;
+ relativeHumidity = static_cast<uint16_t>(tempHumidity / 10);
+
+ return status;
+}
+
+}; // namespace Si70xxSensor
diff --git a/examples/platform/silabs/Si70xxSensor.h b/examples/platform/silabs/Si70xxSensor.h
new file mode 100644
index 0000000..8dc31a8
--- /dev/null
+++ b/examples/platform/silabs/Si70xxSensor.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * Copyright (c) 2020 Project CHIP Authors
+ * Copyright (c) 2019 Google LLC.
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "sl_status.h"
+#include <stdint.h>
+
+namespace Si70xxSensor {
+
+/**
+ * @brief Initialises the Si70xx Sensor.
+ *
+ * @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
+ * Error if an underlying platform error occured
+ */
+sl_status_t Init();
+
+/**
+ * @brief Reads Humidity and temperature values from the Si70xx sensor.
+ * The init function must be called before calling the GetSensorData.
+ *
+ * @param[out] relativeHumidity Relative humidity percentage in centi-pourcentage (1000 == 10.00%)
+ * @param[out] temperature Ambiant temperature in centi-celsium (1000 == 10.00C)
+ *
+ * @return sl_status_t SL_STATUS_OK if there were no errors occured during initialisation.
+ * SL_STATUS_NOT_INITIALIZED if the sensor was not initialised
+ * Error if an underlying platform error occured
+ */
+sl_status_t GetSensorData(uint16_t & relativeHumidity, int16_t & temperature);
+
+}; // namespace Si70xxSensor
diff --git a/examples/platform/silabs/SiWx917/BUILD.gn b/examples/platform/silabs/SiWx917/BUILD.gn
index 156fb38..cf7b7ae 100644
--- a/examples/platform/silabs/SiWx917/BUILD.gn
+++ b/examples/platform/silabs/SiWx917/BUILD.gn
@@ -252,6 +252,13 @@
public_deps += [ ":test-event-trigger" ]
}
+ if (sl_enable_si70xx_sensor) {
+ sources += [
+ "${silabs_common_plat_dir}/Si70xxSensor.cpp",
+ "${silabs_common_plat_dir}/Si70xxSensor.h",
+ ]
+ }
+
if (app_data_model != "") {
public_deps += [ app_data_model ]
}
diff --git a/examples/platform/silabs/TemperatureSensor.cpp b/examples/platform/silabs/TemperatureSensor.cpp
deleted file mode 100644
index d7e13e1..0000000
--- a/examples/platform/silabs/TemperatureSensor.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- *
- * Copyright (c) 2020 Project CHIP Authors
- * Copyright (c) 2019 Google LLC.
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "TemperatureSensor.h"
-
-#include "sl_board_control.h"
-#include "sl_i2cspm_instances.h"
-#include "sl_si70xx.h"
-
-namespace TemperatureSensor {
-constexpr uint16_t kSensorTemperatureOffset = 800;
-static bool initialized = false;
-
-sl_status_t Init()
-{
- sl_status_t status;
- sl_i2cspm_t * rht_sensor = sl_i2cspm_sensor;
- (void) sl_board_enable_sensor(SL_BOARD_SENSOR_RHT);
-
- status = sl_si70xx_init(rht_sensor, SI7021_ADDR);
- initialized = (SL_STATUS_OK == status);
- return status;
-}
-
-sl_status_t GetTemp(uint32_t * relativeHumidity, int16_t * temperature)
-{
- if (!initialized)
- {
- return SL_STATUS_NOT_INITIALIZED;
- }
-
- // Sensor resolution 0.001 C
- // DataModel resolution 0.01 C
- sl_status_t status;
- sl_i2cspm_t * rht_sensor = sl_i2cspm_sensor;
- int32_t temp = 0;
- status = sl_si70xx_measure_rh_and_temp(rht_sensor, SI7021_ADDR, relativeHumidity, &temp);
-
- if (temperature != nullptr)
- {
- *temperature = static_cast<int16_t>(temp / 10) - kSensorTemperatureOffset;
- }
-
- return status;
-}
-}; // namespace TemperatureSensor
diff --git a/examples/platform/silabs/TemperatureSensor.h b/examples/platform/silabs/TemperatureSensor.h
deleted file mode 100644
index 116287e..0000000
--- a/examples/platform/silabs/TemperatureSensor.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- *
- * Copyright (c) 2020 Project CHIP Authors
- * Copyright (c) 2019 Google LLC.
- * All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include "sl_status.h"
-#include <stdint.h>
-
-namespace TemperatureSensor {
-sl_status_t Init();
-sl_status_t GetTemp(uint32_t * relativeHumidity, int16_t * temperature);
-}; // namespace TemperatureSensor
diff --git a/examples/platform/silabs/display/demo-ui.c b/examples/platform/silabs/display/demo-ui.c
index e0fe37e..d909cb1 100644
--- a/examples/platform/silabs/display/demo-ui.c
+++ b/examples/platform/silabs/display/demo-ui.c
@@ -167,6 +167,5 @@
{
GLIB_clear(&glibContext);
demoUIDisplayHeader((char *) name);
- demoUIDisplayApp(false);
demoUIDisplayProtocols();
}
diff --git a/examples/platform/silabs/display/lcd.h b/examples/platform/silabs/display/lcd.h
index b62664c..61fa816 100644
--- a/examples/platform/silabs/display/lcd.h
+++ b/examples/platform/silabs/display/lcd.h
@@ -65,6 +65,7 @@
int DrawPixel(void * pContext, int32_t x, int32_t y);
int Update(void);
void WriteDemoUI(bool state);
+ void WriteDemoUI();
void SetCustomUI(customUICB cb);
void GetScreen(Screen_e & screen);
@@ -85,8 +86,6 @@
bool protocol1 = false; /* data */
} DemoState_t;
- void WriteDemoUI();
-
#ifdef QR_CODE_ENABLED
void WriteQRCode();
void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
diff --git a/examples/platform/silabs/efr32/BUILD.gn b/examples/platform/silabs/efr32/BUILD.gn
index 91cd2c1..f5c79a2 100644
--- a/examples/platform/silabs/efr32/BUILD.gn
+++ b/examples/platform/silabs/efr32/BUILD.gn
@@ -308,6 +308,13 @@
public_deps += [ ":test-event-trigger" ]
}
+ if (sl_enable_si70xx_sensor) {
+ sources += [
+ "${silabs_common_plat_dir}/Si70xxSensor.cpp",
+ "${silabs_common_plat_dir}/Si70xxSensor.h",
+ ]
+ }
+
if (app_data_model != "") {
public_deps += [ app_data_model ]
}
diff --git a/examples/thermostat/silabs/BUILD.gn b/examples/thermostat/silabs/BUILD.gn
index 75b2f75..f51ee14 100644
--- a/examples/thermostat/silabs/BUILD.gn
+++ b/examples/thermostat/silabs/BUILD.gn
@@ -47,10 +47,6 @@
declare_args() {
# Dump memory usage at link time.
chip_print_memory_usage = false
-
- # Enable the temperature sensor
- # Some boards do not have a temperature sensor
- use_temp_sensor = false
}
if (wifi_soc) {
@@ -112,19 +108,6 @@
"PW_RPC_ENABLED",
]
}
-
- if (use_temp_sensor) {
- include_dirs += [
- "${efr32_sdk_root}/platform/driver/i2cspm/inc",
- "${efr32_sdk_root}/app/bluetooth/common/sensor_rht",
- "${efr32_sdk_root}/app/bluetooth/common/sensor_rht/config",
- "${efr32_sdk_root}/hardware/driver/si70xx/inc",
- "${efr32_sdk_root}/app/bluetooth/common/sensor_select",
- "${efr32_sdk_root}/platform/common/config",
- ]
-
- defines += [ "USE_TEMP_SENSOR" ]
- }
}
}
@@ -141,17 +124,6 @@
"src/ZclCallbacks.cpp",
]
- if (use_temp_sensor) {
- sources += [
- "${efr32_sdk_root}/hardware/driver/si70xx/src/sl_si70xx.c",
- "${efr32_sdk_root}/platform/common/src/sl_status.c",
- "${efr32_sdk_root}/platform/driver/i2cspm/src/sl_i2cspm.c",
- "${efr32_sdk_root}/platform/emlib/src/em_i2c.c",
- "${examples_common_plat_dir}/TemperatureSensor.cpp",
- "${sdk_support_root}/matter/efr32/${silabs_family}/${silabs_board}/autogen/sl_i2cspm_init.c",
- ]
- }
-
if (!disable_lcd) {
sources += [ "src/ThermostatUI.cpp" ]
}
diff --git a/examples/thermostat/silabs/src/SensorManager.cpp b/examples/thermostat/silabs/src/SensorManager.cpp
index abecd89..3522e9f 100644
--- a/examples/thermostat/silabs/src/SensorManager.cpp
+++ b/examples/thermostat/silabs/src/SensorManager.cpp
@@ -26,14 +26,15 @@
#include "AppEvent.h"
#include "AppTask.h"
-#ifdef USE_TEMP_SENSOR
-#include "TemperatureSensor.h"
-#endif
+#if defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
+#include "Si70xxSensor.h"
+#endif // defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
/**********************************************************
* Defines and Constants
*********************************************************/
using namespace chip;
+using namespace chip::app;
using namespace ::chip::DeviceLayer;
constexpr EndpointId kThermostatEndpoint = 1;
@@ -45,10 +46,10 @@
*********************************************************/
SensorManager SensorManager::sSensorManager;
-#ifndef USE_TEMP_SENSOR
+#if !(defined(SL_MATTER_USE_SI70XX_SENSOR) && (SL_MATTER_USE_SI70XX_SENSOR))
constexpr uint16_t kSimulatedReadingFrequency = (60000 / kSensorTImerPeriodMs); // Change Simulated number at each minutes
static int16_t mSimulatedTemp[] = { 2300, 2400, 2800, 2550, 2200, 2125, 2100, 2600, 1800, 2700 };
-#endif
+#endif // !(defined(SL_MATTER_USE_SI70XX_SENSOR) && (SL_MATTER_USE_SI70XX_SENSOR))
CHIP_ERROR SensorManager::Init()
{
@@ -61,13 +62,13 @@
return APP_ERROR_CREATE_TIMER_FAILED;
}
-#ifdef USE_TEMP_SENSOR
- if (SL_STATUS_OK != TemperatureSensor::Init())
+#if defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
+ if (SL_STATUS_OK != Si70xxSensor::Init())
{
SILABS_LOG("Failed to Init Sensor");
return CHIP_ERROR_INTERNAL;
}
-#endif
+#endif // defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
// Update Temp immediatly at bootup
SensorTimerEventHandler(nullptr);
@@ -81,19 +82,20 @@
int16_t temperature = 0;
static int16_t lastTemperature = 0;
-#ifdef USE_TEMP_SENSOR
+#if defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
int32_t tempSum = 0;
- uint32_t humidity = 0;
+ uint16_t humidity = 0;
for (uint8_t i = 0; i < 100; i++)
{
- if (SL_STATUS_OK != TemperatureSensor::GetTemp(&humidity, &temperature))
+ if (SL_STATUS_OK != Si70xxSensor::GetSensorData(humidity, temperature))
{
SILABS_LOG("Failed to read Temperature !!!");
}
tempSum += temperature;
}
temperature = static_cast<int16_t>(tempSum / 100);
+
#else
static uint8_t nbOfRepetition = 0;
static uint8_t simulatedIndex = 0;
@@ -109,18 +111,20 @@
simulatedIndex++;
nbOfRepetition = 0;
}
-#endif // USE_TEMP_SENSOR
+#endif // defined(SL_MATTER_USE_SI70XX_SENSOR) && SL_MATTER_USE_SI70XX_SENSOR
SILABS_LOG("Sensor Temp is : %d", temperature);
+ MarkAttributeDirty reportState = MarkAttributeDirty::kNo;
if ((temperature >= (lastTemperature + kMinTemperatureDelta)) || temperature <= (lastTemperature - kMinTemperatureDelta))
{
- lastTemperature = temperature;
- PlatformMgr().LockChipStack();
- // The SensorMagager shouldn't be aware of the Endpoint ID TODO Fix this.
- // TODO Per Spec we should also apply the Offset stored in the same cluster before saving the temp
-
- app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kThermostatEndpoint, temperature);
- PlatformMgr().UnlockChipStack();
+ reportState = MarkAttributeDirty::kIfChanged;
}
+
+ lastTemperature = temperature;
+ PlatformMgr().LockChipStack();
+ // The SensorMagager shouldn't be aware of the Endpoint ID TODO Fix this.
+ // TODO Per Spec we should also apply the Offset stored in the same cluster before saving the temp
+ app::Clusters::Thermostat::Attributes::LocalTemperature::Set(kThermostatEndpoint, temperature, reportState);
+ PlatformMgr().UnlockChipStack();
}
diff --git a/third_party/silabs/efr32_sdk.gni b/third_party/silabs/efr32_sdk.gni
index 470c812..1460773 100644
--- a/third_party/silabs/efr32_sdk.gni
+++ b/third_party/silabs/efr32_sdk.gni
@@ -415,6 +415,17 @@
_include_dirs += [ "${chip_root}/third_party/silabs/mqtt/stack" ]
}
+ if (sl_enable_si70xx_sensor) {
+ _include_dirs += [
+ "${efr32_sdk_root}/platform/driver/i2cspm/inc",
+ "${efr32_sdk_root}/app/bluetooth/common/sensor_rht",
+ "${efr32_sdk_root}/app/bluetooth/common/sensor_rht/config",
+ "${efr32_sdk_root}/hardware/driver/si70xx/inc",
+ "${efr32_sdk_root}/app/bluetooth/common/sensor_select",
+ "${efr32_sdk_root}/platform/common/config",
+ ]
+ }
+
# Note that we're setting the mbedTLS and PSA configuration files through a
# define. This means the build system by default does not pick up changes in
# the content of these, only when changing the filename itself.
@@ -672,6 +683,12 @@
defines += [ "CHIP_CONFIG_SYNCHRONOUS_REPORTS_ENABLED=1" ]
}
+ if (sl_enable_si70xx_sensor) {
+ defines += [ "SL_MATTER_USE_SI70XX_SENSOR=1" ]
+ } else {
+ defines += [ "SL_MATTER_USE_SI70XX_SENSOR=0" ]
+ }
+
cflags = []
foreach(include_dir, _include_dirs) {
cflags += [ "-isystem" + rebase_path(include_dir, root_build_dir) ]
@@ -1068,6 +1085,16 @@
]
}
+ if (sl_enable_si70xx_sensor) {
+ sources += [
+ "${efr32_sdk_root}/hardware/driver/si70xx/src/sl_si70xx.c",
+ "${efr32_sdk_root}/platform/common/src/sl_status.c",
+ "${efr32_sdk_root}/platform/driver/i2cspm/src/sl_i2cspm.c",
+ "${efr32_sdk_root}/platform/emlib/src/em_i2c.c",
+ "${sdk_support_root}/matter/efr32/${silabs_family}/${silabs_board}/autogen/sl_i2cspm_init.c",
+ ]
+ }
+
public_deps = [
":efr32_mbedtls_config",
"${segger_rtt_root}:segger_rtt",
diff --git a/third_party/silabs/matter_support b/third_party/silabs/matter_support
index c3e993c..841d43d 160000
--- a/third_party/silabs/matter_support
+++ b/third_party/silabs/matter_support
@@ -1 +1 @@
-Subproject commit c3e993cea4aad32adc178fe487afb66822f0b42d
+Subproject commit 841d43db7e86877636cd73b5244da4c34d38d544
diff --git a/third_party/silabs/silabs_board.gni b/third_party/silabs/silabs_board.gni
index 2134179..03b2ea7 100644
--- a/third_party/silabs/silabs_board.gni
+++ b/third_party/silabs/silabs_board.gni
@@ -53,6 +53,9 @@
# Self-provision enabled
use_provision_channel = false
+
+ # Temperature Sensor support
+ sl_enable_si70xx_sensor = false
}
declare_args() {
@@ -75,6 +78,9 @@
silabs_mcu = "SiWG917M111MGTBA"
wifi_soc = true
+ assert(!sl_enable_si70xx_sensor,
+ "${silabs_board} does not support the si90xx sensor!")
+
# EFR32 MG24 series ----------
} else if (silabs_board == "BRD4186A" || silabs_board == "BRD4187A") {
variant = string_replace(silabs_board, "A", "C")
@@ -104,6 +110,9 @@
show_qr_code = false
disable_lcd = true
+ assert(!sl_enable_si70xx_sensor,
+ "${silabs_board} does not support the si90xx sensor!")
+
# EFR32 MG24 Modules series ----------
} else if (silabs_board == "BRD4316A") {
silabs_family = "mgm24"
@@ -128,6 +137,9 @@
use_external_flash = false
show_qr_code = false
disable_lcd = true
+
+ assert(!sl_enable_si70xx_sensor,
+ "${silabs_board} does not support the si90xx sensor!")
} else if (silabs_board == "BRD2704A") {
silabs_family = "mgm24"
silabs_mcu = "MGM240PB32VNA"
@@ -137,6 +149,9 @@
use_external_flash = false
show_qr_code = false
disable_lcd = true
+
+ assert(!sl_enable_si70xx_sensor,
+ "${silabs_board} does not support the si90xx sensor!")
} else if (silabs_board == "BRD4318A") {
silabs_family = "mgm24"
silabs_mcu = "MGM240SD22VNA"