Skip to content

Commit e6863fc

Browse files
Refactor SiWxPlatformInterface to enhance button event handling and power management. Removed unused UART power requirement handler and added keep awake functionality on button press using a sleeptimer. (#71458)
1 parent 2a94560 commit e6863fc

8 files changed

Lines changed: 99 additions & 182 deletions

File tree

examples/platform/silabs/MatterConfig.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@
4242

4343
#if CHIP_CONFIG_ENABLE_ICD_SERVER
4444
#include <platform/silabs/wifi/icd/WifiSleepManager.h> // nogncheck
45-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
4645

47-
// TODO: We shouldn't need any platform specific includes in this file
48-
#if (defined(SLI_SI91X_MCU_INTERFACE) && SLI_SI91X_MCU_INTERFACE == 1)
49-
#include <platform/silabs/SiWx/SiWxPlatformInterface.h>
50-
#endif // (defined(SLI_SI91X_MCU_INTERFACE) && SLI_SI91X_MCU_INTERFACE == 1)
46+
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
5147
#endif // SL_WIFI
5248

5349
#if defined(PW_RPC_ENABLED) && PW_RPC_ENABLED
@@ -423,10 +419,9 @@ extern "C" void vApplicationIdleHook(void)
423419
{
424420
#if ((defined(SLI_SI91X_MCU_INTERFACE) && SLI_SI91X_MCU_INTERFACE) && CHIP_CONFIG_ENABLE_ICD_SERVER)
425421
#ifdef SL_CATALOG_SIMPLE_BUTTON_PRESENT
426-
SiWxPlatformInterface::sl_si91x_btn_event_handler();
422+
GetPlatform().SleepButtonActionHandler();
427423
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT
428-
SiWxPlatformInterface::sl_si91x_uart_power_requirement_handler();
429-
#endif
424+
#endif // ((defined(SLI_SI91X_MCU_INTERFACE) && SLI_SI91X_MCU_INTERFACE) && CHIP_CONFIG_ENABLE_ICD_SERVER)
430425
#if SL_MATTER_DEBUG_WATCHDOG_ENABLE
431426
GetPlatform().WatchdogFeed();
432427
#endif // SL_MATTER_DEBUG_WATCHDOG_ENABLE

src/platform/silabs/SiWx/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ static_library("SiWx") {
7272
"${silabs_platform_dir}/platformAbstraction/WiseMcuSpam.cpp",
7373
"../../FreeRTOS/SystemTimeSupport.cpp",
7474
"../../SingletonConfigurationManager.cpp",
75-
"SiWxPlatformInterface.h",
7675
"ble/BLEManagerImpl.cpp",
7776
"ble/sl_si91x_ble_init.cpp",
7877
"ble/sl_si91x_ble_init.h",

src/platform/silabs/SiWx/SiWxPlatformInterface.h

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/platform/silabs/platformAbstraction/GsdkSpam.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ uint8_t SilabsPlatform::GetButtonState(uint8_t button)
303303
const sl_button_t * handle = SL_SIMPLE_BUTTON_INSTANCE(button);
304304
return nullptr == handle ? 0 : sl_button_get_state(handle);
305305
}
306-
306+
#ifdef SL_ICD_ENABLED
307+
void SilabsPlatform::SleepButtonActionHandler() {}
308+
#endif // SL_ICD_ENABLED
307309
#else
308310
uint8_t SilabsPlatform::GetButtonState(uint8_t button)
309311
{

src/platform/silabs/platformAbstraction/SilabsPlatform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class SilabsPlatform : virtual public SilabsPlatformAbstractionBase
5252
inline void SetButtonsCb(SilabsButtonCb callback) override { mButtonCallback = callback; }
5353
static SilabsButtonCb mButtonCallback;
5454
uint8_t GetButtonState(uint8_t button) override;
55+
#ifdef SL_ICD_ENABLED
56+
void SleepButtonActionHandler(void) override;
57+
#endif // SL_ICD_ENABLED
5558

5659
#if defined(SL_CATALOG_CUSTOM_MAIN_PRESENT)
5760
void StartScheduler(void) override;

src/platform/silabs/platformAbstraction/SilabsPlatformBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class SilabsPlatformAbstractionBase
4747
typedef void (*SilabsButtonCb)(uint8_t, uint8_t);
4848
virtual void SetButtonsCb(SilabsButtonCb callback) {}
4949
virtual uint8_t GetButtonState(uint8_t button) { return 0; }
50+
virtual void SleepButtonActionHandler(void){};
5051

5152
// LEDS
5253
virtual void InitLed(void) {}

src/platform/silabs/platformAbstraction/WiseMcuSpam.cpp

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
#include <platform/silabs/platformAbstraction/SilabsPlatform.h>
1919
#include <sl_si91x_common_flash_intf.h>
2020

21-
#include "sl_si91x_driver_gpio.h"
22-
#include "sl_si91x_gpio.h"
23-
24-
#include <FreeRTOS.h>
25-
#include <task.h>
21+
#include <cmsis_os2.h>
22+
#include <sl_cmsis_os2_common.h>
2623

2724
#include <app/icd/server/ICDServerConfig.h>
2825

@@ -72,6 +69,11 @@ const sl_rgb_led_t * ledPinArray[SL_LED_COUNT] = { &led_led0 };
7269
uint8_t ledPinArray[SL_LED_COUNT] = { SL_LED_LED0_PIN, SL_LED_LED1_PIN };
7370
#endif // (defined(SL_MATTER_RGB_LED_ENABLED) && SL_MATTER_RGB_LED_ENABLED)
7471
#endif // ENABLE_WSTK_LEDS
72+
73+
#if SL_ICD_ENABLED
74+
#include "sl_si91x_driver_gpio.h"
75+
#include "sl_si91x_power_manager.h"
76+
#endif // SL_ICD_ENABLED
7577
}
7678

7779
#ifdef SL_CATALOG_SYSTEMVIEW_TRACE_PRESENT
@@ -91,9 +93,16 @@ namespace {
9193
uint8_t sButtonStates[SL_SI91x_BUTTON_COUNT] = { 0 };
9294
#endif // SL_CATALOG_SIMPLE_BUTTON_PRESENT
9395

94-
#if CHIP_CONFIG_ENABLE_ICD_SERVER && defined(SL_CATALOG_SIMPLE_BUTTON_PRESENT)
95-
bool btn0_pressed = false;
96-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && defined(SL_CATALOG_SIMPLE_BUTTON_PRESENT)
96+
#if CHIP_CONFIG_ENABLE_ICD_SERVER && (SL_MATTER_GN_BUILD == 0)
97+
98+
// Keep awake duration in milliseconds
99+
static constexpr uint32_t kSiWxKeepAwakeDurationMs = 10000;
100+
101+
// CMSIS-RTOS2 one-shot timer: each BTN0 press (re)starts the full kSiWxKeepAwakeDurationMs window.
102+
static osTimerId_t sSiWxKeepAwakeOsTimer = nullptr;
103+
static bool sSiWxKeepAwakePs4Active = false;
104+
105+
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER && (SL_MATTER_GN_BUILD == 0)
97106

98107
#if CHIP_CONFIG_ENABLE_ICD_SERVER == 0
99108
int soc_pll_config(void)
@@ -234,22 +243,10 @@ extern "C" void sl_button_on_change(uint8_t btn, uint8_t btnAction)
234243
// if the btn was not pressed and only a release event came, ignore it
235244
// if the btn was already pressed and another press event came, ignore it
236245
// essentially, if both of them are in the same state then ignore it.
237-
VerifyOrReturn(btnAction != btn0_pressed);
238-
239-
if ((btnAction == BUTTON_PRESSED) && (btn0_pressed == false))
240-
{
241-
btn0_pressed = true;
242-
}
243-
else if ((btnAction == BUTTON_RELEASED) && (btn0_pressed == true))
244-
{
245-
btn0_pressed = false;
246-
}
246+
VerifyOrReturn(btnAction != GetPlatform().GetButtonState(SL_BUTTON_BTN0_NUMBER));
247247
}
248248
#endif // SL_ICD_ENABLED
249-
if (Silabs::GetPlatform().mButtonCallback == nullptr)
250-
{
251-
return;
252-
}
249+
VerifyOrReturn(GetPlatform().mButtonCallback != nullptr);
253250

254251
if (btn < SL_SI91x_BUTTON_COUNT)
255252
{
@@ -268,6 +265,76 @@ uint8_t SilabsPlatform::GetButtonState(uint8_t button)
268265
{
269266
return (button < SL_SI91x_BUTTON_COUNT) ? sButtonStates[button] : 0;
270267
}
268+
269+
#if SL_ICD_ENABLED
270+
/**
271+
* @brief invoked when button press event is received when in sleep
272+
* @param[in] pin_intr GPIO pin interrupt number.
273+
* @return none.
274+
* @note this is a callback from the Wiseconnect SDK
275+
*/
276+
extern "C" void gpio_uulp_pin_interrupt_callback(uint32_t pin_intr)
277+
{
278+
// UULP_GPIO_2 is used to detect the button 0 press
279+
VerifyOrReturn(pin_intr == RTE_UULP_GPIO_2_PIN, ChipLogError(DeviceLayer, "invalid pin interrupt: %ld", pin_intr));
280+
sl_status_t status = SL_STATUS_OK;
281+
uint8_t pin_intr_status = sl_si91x_gpio_get_uulp_npss_pin(pin_intr);
282+
if (pin_intr_status == LOW)
283+
{
284+
// BTN_0 is pressed
285+
// NOTE: the GPIO is masked since the interrupt is invoked before scheduler is started, thus this is required to hand over
286+
// control to scheduler, the PIN is unmasked in the power manager flow before going to sleep
287+
status = sl_si91x_gpio_driver_mask_set_uulp_npss_interrupt(pin_intr);
288+
VerifyOrReturn(status == SL_STATUS_OK, ChipLogError(DeviceLayer, "failed to mask interrupt: %ld", status));
289+
}
290+
}
291+
#if SL_MATTER_GN_BUILD == 0
292+
static void SiWxKeepAwakeOsTimerCallback(void * arg)
293+
{
294+
(void) arg;
295+
VerifyOrReturn(sSiWxKeepAwakePs4Active == true);
296+
sl_status_t status = sl_si91x_power_manager_remove_ps_requirement(SL_SI91X_POWER_MANAGER_PS4);
297+
VerifyOrReturn(status == SL_STATUS_OK, ChipLogError(DeviceLayer, "remove_ps failed: %lx", status));
298+
sSiWxKeepAwakePs4Active = false;
299+
}
300+
301+
static void SiWxArmKeepAwakeOnBtn0Press()
302+
{
303+
if (!sSiWxKeepAwakePs4Active)
304+
{
305+
sl_status_t status = sl_si91x_power_manager_add_ps_requirement(SL_SI91X_POWER_MANAGER_PS4);
306+
VerifyOrReturn(status == SL_STATUS_OK, ChipLogError(DeviceLayer, "add_ps failed: %lx", status));
307+
button_init_instances();
308+
sSiWxKeepAwakePs4Active = true;
309+
}
310+
311+
if (sSiWxKeepAwakeOsTimer == nullptr)
312+
{
313+
sSiWxKeepAwakeOsTimer = osTimerNew(SiWxKeepAwakeOsTimerCallback, osTimerOnce, nullptr, nullptr);
314+
VerifyOrReturn(sSiWxKeepAwakeOsTimer != nullptr, ChipLogError(DeviceLayer, "keep awake osTimerNew failed"));
315+
}
316+
317+
if (osTimerStart(sSiWxKeepAwakeOsTimer, pdMS_TO_TICKS(kSiWxKeepAwakeDurationMs)) != osOK)
318+
{
319+
ChipLogError(DeviceLayer, "keep awake osTimerStart failed");
320+
}
321+
}
322+
#endif // SL_MATTER_GN_BUILD == 0
323+
324+
void SilabsPlatform::SleepButtonActionHandler()
325+
{
326+
const uint8_t btnAction = (sl_si91x_gpio_get_uulp_npss_pin(SL_BUTTON_BTN0_PIN) == LOW) ? BUTTON_PRESSED : BUTTON_RELEASED;
327+
// If the button state is the same as the last state, return
328+
VerifyOrReturn(btnAction != GetButtonState(SL_BUTTON_BTN0_NUMBER));
329+
if (btnAction == BUTTON_PRESSED)
330+
{
331+
#if SL_MATTER_GN_BUILD == 0
332+
SiWxArmKeepAwakeOnBtn0Press();
333+
#endif // SL_MATTER_GN_BUILD == 0
334+
}
335+
sl_button_on_change(SL_BUTTON_BTN0_NUMBER, btnAction);
336+
}
337+
#endif // SL_ICD_ENABLED
271338
#else
272339
uint8_t SilabsPlatform::GetButtonState(uint8_t button)
273340
{

src/platform/silabs/wifi/SiWx/WifiInterfaceImpl.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -342,24 +342,6 @@ sl_status_t SiWxPlatformInit(void)
342342
status = sl_si91x_m4_ta_secure_handshake(SL_SI91X_ENABLE_XTAL, 1, &xtal_enable, 0, nullptr);
343343
VerifyOrReturnError(status == SL_STATUS_OK, status,
344344
ChipLogError(DeviceLayer, "sl_si91x_m4_ta_secure_handshake failed: 0x%lx", static_cast<uint32_t>(status)));
345-
346-
#if CHIP_CONFIG_ENABLE_ICD_SERVER
347-
#ifdef ENABLE_CHIP_SHELL
348-
#ifdef RTE_UULP_GPIO_1_PIN
349-
// While using the matter shell with a Low Power Build, GPIO 1 is used to check the UULP PIN 1 status
350-
// since UART doesn't act as a wakeup source in the UULP mode.
351-
352-
// Configuring the NPS GPIO 1
353-
RSI_NPSSGPIO_SetPinMux(RTE_UULP_GPIO_1_PIN, 0);
354-
355-
// Configure the NPSS GPIO direction to input
356-
RSI_NPSSGPIO_SetDir(RTE_UULP_GPIO_1_PIN, 1);
357-
358-
// Enable the REN
359-
RSI_NPSSGPIO_InputBufferEn(RTE_UULP_GPIO_1_PIN, 1);
360-
#endif // RTE_UULP_GPIO_1_PIN
361-
#endif // ENABLE_CHIP_SHELL
362-
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
363345
#endif // SLI_SI91X_MCU_INTERFACE
364346

365347
sl_wifi_firmware_version_t version = { 0 };

0 commit comments

Comments
 (0)