[Silabs] Migration to CMSIS OS2 api continuation. (#32874)

* Migrate MatterConfig/MainTask creation to cmsisos api

* Migrate silabs matter shell to cmsisos

* use sl_cmsis_os2_common in BaseApplication

* move local variable to a namespace
diff --git a/examples/platform/silabs/BaseApplication.cpp b/examples/platform/silabs/BaseApplication.cpp
index d8df720..a6986db 100644
--- a/examples/platform/silabs/BaseApplication.cpp
+++ b/examples/platform/silabs/BaseApplication.cpp
@@ -45,6 +45,7 @@
 #include <platform/CHIPDeviceLayer.h>
 #include <setup_payload/QRCodeSetupPayloadGenerator.h>
 #include <setup_payload/SetupPayload.h>
+#include <sl_cmsis_os2_common.h>
 
 #if CHIP_ENABLE_OPENTHREAD
 #include <platform/OpenThread/OpenThreadUtils.h>
@@ -78,7 +79,6 @@
 #ifndef APP_TASK_STACK_SIZE
 #define APP_TASK_STACK_SIZE (4096)
 #endif
-#define APP_TASK_PRIORITY 2
 #ifndef APP_EVENT_QUEUE_SIZE // Allow apps to define a different app queue size
 #define APP_EVENT_QUEUE_SIZE 10
 #endif
@@ -123,18 +123,18 @@
 constexpr uint32_t kLightTimerPeriod = static_cast<uint32_t>(pdMS_TO_TICKS(10));
 
 uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
-StaticQueue_t sAppEventQueueStruct; // TODO abstract type for static controlblock
+osMessageQueue_t sAppEventQueueStruct;
 constexpr osMessageQueueAttr_t appEventQueueAttr = { .cb_mem  = &sAppEventQueueStruct,
-                                                     .cb_size = sizeof(sAppEventQueueBuffer),
+                                                     .cb_size = osMessageQueueCbSize,
                                                      .mq_mem  = sAppEventQueueBuffer,
                                                      .mq_size = sizeof(sAppEventQueueBuffer) };
 
 uint8_t appStack[APP_TASK_STACK_SIZE];
-StaticTask_t appTaskStruct; // TODO abstract type for static controlblock
+osThread_t appTaskControlBlock;
 constexpr osThreadAttr_t appTaskAttr = { .name       = APP_TASK_NAME,
                                          .attr_bits  = osThreadDetached,
-                                         .cb_mem     = &appTaskStruct,
-                                         .cb_size    = sizeof(appTaskStruct),
+                                         .cb_mem     = &appTaskControlBlock,
+                                         .cb_size    = osThreadCbSize,
                                          .stack_mem  = appStack,
                                          .stack_size = APP_TASK_STACK_SIZE,
                                          .priority   = osPriorityNormal };
diff --git a/examples/platform/silabs/MatterConfig.cpp b/examples/platform/silabs/MatterConfig.cpp
index d41a415..9be9f07 100644
--- a/examples/platform/silabs/MatterConfig.cpp
+++ b/examples/platform/silabs/MatterConfig.cpp
@@ -21,8 +21,7 @@
 #include "BaseApplication.h"
 #include "OTAConfig.h"
 #include <MatterConfig.h>
-
-#include <FreeRTOS.h>
+#include <cmsis_os2.h>
 
 #include <mbedtls/platform.h>
 
@@ -80,27 +79,16 @@
 
 #include <platform/silabs/platformAbstraction/SilabsPlatform.h>
 
-#include "FreeRTOSConfig.h"
-#include "event_groups.h"
-#include "task.h"
-
 /**********************************************************
  * Defines
  *********************************************************/
 
-#define MAIN_TASK_STACK_SIZE (1024 * 5)
-#define MAIN_TASK_PRIORITY (configMAX_PRIORITIES - 1)
-
 using namespace ::chip;
 using namespace ::chip::Inet;
 using namespace ::chip::DeviceLayer;
 using namespace ::chip::Credentials::Silabs;
 using namespace chip::DeviceLayer::Silabs;
 
-TaskHandle_t main_Task;
-volatile int apperror_cnt;
-static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
-
 #if CHIP_ENABLE_OPENTHREAD
 #include <inet/EndPointStateOpenThread.h>
 #include <openthread/cli.h>
@@ -156,7 +144,20 @@
 #endif // CHIP_ENABLE_OPENTHREAD
 
 namespace {
-void application_start(void * unused)
+
+constexpr uint32_t kMainTaskStackSize = (1024 * 5);
+// Task is dynamically allocated with max priority. This task gets deleted once the inits are completed.
+constexpr osThreadAttr_t kMainTaskAttr = { .name       = "main",
+                                           .attr_bits  = osThreadDetached,
+                                           .cb_mem     = NULL,
+                                           .cb_size    = 0U,
+                                           .stack_mem  = NULL,
+                                           .stack_size = kMainTaskStackSize,
+                                           .priority   = osPriorityRealtime7 };
+osThreadId_t sMainTaskHandle;
+static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
+
+void ApplicationStart(void * unused)
 {
     CHIP_ERROR err = SilabsMatterConfig::InitMatter(BLE_DEV_NAME);
     if (err != CHIP_NO_ERROR)
@@ -175,7 +176,8 @@
     if (err != CHIP_NO_ERROR)
         appError(err);
 
-    vTaskDelete(main_Task);
+    VerifyOrDie(osThreadTerminate(sMainTaskHandle) == osOK); // Deleting the main task should never fail.
+    sMainTaskHandle = nullptr;
 }
 } // namespace
 
@@ -183,8 +185,9 @@
 {
     GetPlatform().Init();
 
-    xTaskCreate(application_start, "main_task", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &main_Task);
+    sMainTaskHandle = osThreadNew(ApplicationStart, nullptr, &kMainTaskAttr);
     SILABS_LOG("Starting scheduler");
+    VerifyOrDie(sMainTaskHandle); // We can't proceed if the Main Task creation failed.
     GetPlatform().StartScheduler();
 
     // Should never get here.
diff --git a/examples/platform/silabs/SiWx917/uart.cpp b/examples/platform/silabs/SiWx917/uart.cpp
index 91df5d5..863beef 100644
--- a/examples/platform/silabs/SiWx917/uart.cpp
+++ b/examples/platform/silabs/SiWx917/uart.cpp
@@ -49,7 +49,7 @@
         break;
     case SL_USART_EVENT_RECEIVE_COMPLETE:
 #ifdef ENABLE_CHIP_SHELL
-        chip::NotifyShellProcessFromISR();
+        chip::NotifyShellProcess();
 #endif
     case SL_USART_EVENT_TRANSFER_COMPLETE:
         break;
diff --git a/examples/platform/silabs/efr32/uart.cpp b/examples/platform/silabs/efr32/uart.cpp
index 3941114..544016e 100644
--- a/examples/platform/silabs/efr32/uart.cpp
+++ b/examples/platform/silabs/efr32/uart.cpp
@@ -291,7 +291,7 @@
 void USART_IRQHandler(void)
 {
 #ifdef ENABLE_CHIP_SHELL
-    chip::NotifyShellProcessFromISR();
+    chip::NotifyShellProcess();
 #elif !defined(PW_RPC_ENABLED)
     otSysEventSignalPending();
 #endif
@@ -333,7 +333,7 @@
     UARTDRV_Receive(vcom_handle, data, transferCount, UART_rx_callback);
 
 #ifdef ENABLE_CHIP_SHELL
-    chip::NotifyShellProcessFromISR();
+    chip::NotifyShellProcess();
 #elif !defined(PW_RPC_ENABLED)
     otSysEventSignalPending();
 #endif
diff --git a/examples/platform/silabs/matter_shell.cpp b/examples/platform/silabs/matter_shell.cpp
index 9a59adb..03a50ea 100644
--- a/examples/platform/silabs/matter_shell.cpp
+++ b/examples/platform/silabs/matter_shell.cpp
@@ -17,21 +17,28 @@
 
 #include "matter_shell.h"
 #include <ChipShellCollection.h>
-#include <FreeRTOS.h>
+#include <cmsis_os2.h>
 #include <lib/core/CHIPCore.h>
 #include <lib/shell/Engine.h>
-#include <task.h>
+#include <sl_cmsis_os2_common.h>
 
 using namespace ::chip;
 using chip::Shell::Engine;
 
 namespace {
 
-#define SHELL_TASK_STACK_SIZE 2048
-#define SHELL_TASK_PRIORITY 5
-TaskHandle_t shellTaskHandle;
-StackType_t shellStack[SHELL_TASK_STACK_SIZE / sizeof(StackType_t)];
-StaticTask_t shellTaskStruct;
+constexpr uint32_t kShellProcessFlag   = 1;
+constexpr uint32_t kShellTaskStackSize = 2048;
+uint8_t shellTaskStack[kShellTaskStackSize];
+osThread_t shellTaskControlBlock;
+constexpr osThreadAttr_t kShellTaskAttr = { .name       = "shell",
+                                            .attr_bits  = osThreadDetached,
+                                            .cb_mem     = &shellTaskControlBlock,
+                                            .cb_size    = osThreadCbSize,
+                                            .stack_mem  = shellTaskStack,
+                                            .stack_size = kShellTaskStackSize,
+                                            .priority   = osPriorityBelowNormal };
+osThreadId_t shellTaskHandle;
 
 void MatterShellTask(void * args)
 {
@@ -40,33 +47,17 @@
 
 } // namespace
 
-extern "C" unsigned int sleep(unsigned int seconds)
-{
-    const TickType_t xDelay = pdMS_TO_TICKS(1000 * seconds);
-    vTaskDelay(xDelay);
-    return 0;
-}
-
 namespace chip {
 
 void NotifyShellProcess()
 {
-    xTaskNotifyGive(shellTaskHandle);
-}
-
-void NotifyShellProcessFromISR()
-{
-    BaseType_t yieldRequired = pdFALSE;
-    if (shellTaskHandle != NULL)
-    {
-        vTaskNotifyGiveFromISR(shellTaskHandle, &yieldRequired);
-    }
-    portYIELD_FROM_ISR(yieldRequired);
+    // This function may be called from Interrupt Service Routines.
+    osThreadFlagsSet(shellTaskHandle, kShellProcessFlag);
 }
 
 void WaitForShellActivity()
 {
-    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
+    osThreadFlagsWait(kShellProcessFlag, osFlagsWaitAny, osWaitForever);
 }
 
 void startShellTask()
@@ -79,8 +70,8 @@
     cmd_misc_init();
     cmd_otcli_init();
 
-    shellTaskHandle = xTaskCreateStatic(MatterShellTask, "matter_cli", ArraySize(shellStack), NULL, SHELL_TASK_PRIORITY, shellStack,
-                                        &shellTaskStruct);
+    shellTaskHandle = osThreadNew(MatterShellTask, nullptr, &kShellTaskAttr);
+    VerifyOrDie(shellTaskHandle);
 }
 
 } // namespace chip
diff --git a/examples/platform/silabs/matter_shell.h b/examples/platform/silabs/matter_shell.h
index 9b818bf..d32ee62 100644
--- a/examples/platform/silabs/matter_shell.h
+++ b/examples/platform/silabs/matter_shell.h
@@ -20,7 +20,6 @@
 namespace chip {
 
 void NotifyShellProcess();
-void NotifyShellProcessFromISR();
 void WaitForShellActivity();
 void startShellTask();
 
diff --git a/examples/thermostat/silabs/include/AppEvent.h b/examples/thermostat/silabs/include/AppEvent.h
index d8d7de4..03b5e70 100644
--- a/examples/thermostat/silabs/include/AppEvent.h
+++ b/examples/thermostat/silabs/include/AppEvent.h
@@ -18,6 +18,7 @@
  */
 
 #pragma once
+#include <stdint.h>
 
 struct AppEvent;
 typedef void (*EventHandler)(AppEvent *);
diff --git a/examples/thermostat/silabs/include/TemperatureManager.h b/examples/thermostat/silabs/include/TemperatureManager.h
index 1005a67..ccdae44 100644
--- a/examples/thermostat/silabs/include/TemperatureManager.h
+++ b/examples/thermostat/silabs/include/TemperatureManager.h
@@ -18,17 +18,12 @@
 
 #pragma once
 
+#include "AppEvent.h"
+#include <app-common/zap-generated/attributes/Accessors.h>
+#include <lib/core/CHIPError.h>
 #include <stdbool.h>
 #include <stdint.h>
 
-#include "AppEvent.h"
-
-#include "FreeRTOS.h"
-#include "timers.h" // provides FreeRTOS timer support
-#include <app-common/zap-generated/attributes/Accessors.h>
-
-#include <lib/core/CHIPError.h>
-
 using namespace chip;
 
 // AppCluster Spec Table 85.
diff --git a/examples/thermostat/silabs/src/TemperatureManager.cpp b/examples/thermostat/silabs/src/TemperatureManager.cpp
index f858e43..d919d71 100644
--- a/examples/thermostat/silabs/src/TemperatureManager.cpp
+++ b/examples/thermostat/silabs/src/TemperatureManager.cpp
@@ -25,7 +25,6 @@
 #include "AppConfig.h"
 #include "AppEvent.h"
 #include "AppTask.h"
-#include "semphr.h"
 
 /**********************************************************
  * Defines and Constants
diff --git a/src/lib/shell/MainLoopSilabs.cpp b/src/lib/shell/MainLoopSilabs.cpp
index 932d31a..83ba4ea 100644
--- a/src/lib/shell/MainLoopSilabs.cpp
+++ b/src/lib/shell/MainLoopSilabs.cpp
@@ -55,7 +55,7 @@
 
 #ifdef BRD4325A
         // for 917 SoC board, we need to create a rx event before we wait for the shell activity
-        // NotifyShellProcessFromISR() is called once the buffer is filled
+        // NotifyShellProcess() is called once the buffer is filled
         while (streamer_read(streamer_get(), buffer + read, 1) == 1)
         {
             // Count how many characters were read; usually one but could be copy/paste