Change freertos APIs to cmsisos APIs in BaseApplication (#32652)
diff --git a/examples/platform/silabs/BaseApplication.cpp b/examples/platform/silabs/BaseApplication.cpp
index abd2c20..3911047 100644
--- a/examples/platform/silabs/BaseApplication.cpp
+++ b/examples/platform/silabs/BaseApplication.cpp
@@ -98,11 +98,10 @@
* Variable declarations
*********************************************************/
-TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer.
-TimerHandle_t sLightTimer;
-
-TaskHandle_t sAppTaskHandle;
-QueueHandle_t sAppEventQueue;
+osTimerId_t sFunctionTimer;
+osTimerId_t sLightTimer;
+osThreadId_t sAppTaskHandle;
+osMessageQueueId_t sAppEventQueue;
#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT)))
LEDWidget sStatusLED;
@@ -119,11 +118,24 @@
bool sHaveBLEConnections = false;
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
-uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
-StaticQueue_t sAppEventQueueStruct;
+constexpr uint32_t kLightTimerPeriod = static_cast<uint32_t>(pdMS_TO_TICKS(10));
-StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)];
-StaticTask_t appTaskStruct;
+uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
+StaticQueue_t sAppEventQueueStruct; // TODO abstract type for static controlblock
+constexpr osMessageQueueAttr_t appEventQueueAttr = { .cb_mem = &sAppEventQueueStruct,
+ .cb_size = sizeof(sAppEventQueueBuffer),
+ .mq_mem = sAppEventQueueBuffer,
+ .mq_size = sizeof(sAppEventQueueBuffer) };
+
+uint8_t appStack[APP_TASK_STACK_SIZE];
+StaticTask_t appTaskStruct; // TODO abstract type for static controlblock
+constexpr osThreadAttr_t appTaskAttr = { .name = APP_TASK_NAME,
+ .attr_bits = osThreadDetached,
+ .cb_mem = &appTaskStruct,
+ .cb_size = sizeof(appTaskStruct),
+ .stack_mem = appStack,
+ .stack_size = APP_TASK_STACK_SIZE,
+ .priority = osPriorityNormal };
#ifdef DISPLAY_ENABLED
SilabsLCD slLCD;
@@ -192,9 +204,9 @@
* AppTask Definitions
*********************************************************/
-CHIP_ERROR BaseApplication::StartAppTask(TaskFunction_t taskFunction)
+CHIP_ERROR BaseApplication::StartAppTask(osThreadFunc_t taskFunction)
{
- sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
+ sAppEventQueue = osMessageQueueNew(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), &appEventQueueAttr);
if (sAppEventQueue == NULL)
{
SILABS_LOG("Failed to allocate app event queue");
@@ -202,8 +214,7 @@
}
// Start App task.
- sAppTaskHandle =
- xTaskCreateStatic(taskFunction, APP_TASK_NAME, ArraySize(appStack), &sAppEventQueue, 1, appStack, &appTaskStruct);
+ sAppTaskHandle = osThreadNew(taskFunction, &sAppEventQueue, &appTaskAttr);
if (sAppTaskHandle == nullptr)
{
SILABS_LOG("Failed to create app task");
@@ -234,12 +245,11 @@
#endif
- // Create FreeRTOS sw timer for Function Selection.
- sFunctionTimer = xTimerCreate("FnTmr", // Just a text name, not used by the RTOS kernel
- pdMS_TO_TICKS(1), // == default timer period
- false, // no timer reload (==one-shot)
- (void *) this, // init timer id = app task obj context
- FunctionTimerEventHandler // timer callback handler
+ // Create cmsis os sw timer for Function Selection.
+ sFunctionTimer = osTimerNew(FunctionTimerEventHandler, // timer callback handler
+ osTimerOnce, // no timer reload (one-shot timer)
+ (void *) this, // pass the app task obj context
+ NULL // No osTimerAttr_t to provide.
);
if (sFunctionTimer == NULL)
{
@@ -247,12 +257,11 @@
appError(APP_ERROR_CREATE_TIMER_FAILED);
}
- // Create FreeRTOS sw timer for LED Management.
- sLightTimer = xTimerCreate("LightTmr", // Text Name
- pdMS_TO_TICKS(10), // Default timer period
- true, // reload timer
- (void *) this, // Timer Id
- LightTimerEventHandler // Timer callback handler
+ // Create cmsis os sw timer for LED Management.
+ sLightTimer = osTimerNew(LightTimerEventHandler, // Timer callback handler"LightTmr",
+ osTimerPeriodic, // timer repeats automatically
+ (void *) this, // pass the app task obj context
+ NULL // No osTimerAttr_t to provide.
);
if (sLightTimer == NULL)
{
@@ -289,7 +298,7 @@
return err;
}
-void BaseApplication::FunctionTimerEventHandler(TimerHandle_t xTimer)
+void BaseApplication::FunctionTimerEventHandler(osTimerId_t xTimer)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
@@ -524,7 +533,7 @@
void BaseApplication::CancelFunctionTimer()
{
- if (xTimerStop(sFunctionTimer, pdMS_TO_TICKS(0)) == pdFAIL)
+ if (osTimerStop(sFunctionTimer) == osError)
{
SILABS_LOG("app timer stop() failed");
appError(APP_ERROR_STOP_TIMER_FAILED);
@@ -533,16 +542,8 @@
void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs)
{
- if (xTimerIsTimerActive(sFunctionTimer))
- {
- SILABS_LOG("app timer already started!");
- CancelFunctionTimer();
- }
-
- // timer is not active, change its period to required value (== restart).
- // FreeRTOS- Block for a maximum of 100 ms if the change period command
- // cannot immediately be sent to the timer command queue.
- if (xTimerChangePeriod(sFunctionTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
+ // Starts or restarts the function timer
+ if (osTimerStart(sFunctionTimer, pdMS_TO_TICKS(aTimeoutInMs)) != osOK)
{
SILABS_LOG("app timer start() failed");
appError(APP_ERROR_START_TIMER_FAILED);
@@ -587,7 +588,7 @@
void BaseApplication::StartStatusLEDTimer()
{
- if (pdPASS != xTimerStart(sLightTimer, pdMS_TO_TICKS(0)))
+ if (osTimerStart(sLightTimer, kLightTimerPeriod) != osOK)
{
SILABS_LOG("Light Time start failed");
appError(APP_ERROR_START_TIMER_FAILED);
@@ -600,10 +601,10 @@
sStatusLED.Set(false);
#endif // ENABLE_WSTK_LEDS
- if (xTimerStop(sLightTimer, pdMS_TO_TICKS(100)) != pdPASS)
+ if (osTimerStop(sLightTimer) == osError)
{
SILABS_LOG("Light Time start failed");
- appError(APP_ERROR_START_TIMER_FAILED);
+ appError(APP_ERROR_STOP_TIMER_FAILED);
}
}
@@ -676,7 +677,7 @@
}
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER
-void BaseApplication::LightTimerEventHandler(TimerHandle_t xTimer)
+void BaseApplication::LightTimerEventHandler(osTimerId_t xTimer)
{
LightEventHandler();
}
@@ -715,35 +716,16 @@
void BaseApplication::PostEvent(const AppEvent * aEvent)
{
- if (sAppEventQueue != NULL)
+ if (sAppEventQueue != nullptr)
{
- BaseType_t status;
- if (xPortIsInsideInterrupt())
- {
- BaseType_t higherPrioTaskWoken = pdFALSE;
- status = xQueueSendFromISR(sAppEventQueue, aEvent, &higherPrioTaskWoken);
-
-#ifdef portYIELD_FROM_ISR
- portYIELD_FROM_ISR(higherPrioTaskWoken);
-#elif portEND_SWITCHING_ISR // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
- portEND_SWITCHING_ISR(higherPrioTaskWoken);
-#else // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
-#error "Must have portYIELD_FROM_ISR or portEND_SWITCHING_ISR"
-#endif // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
- }
- else
- {
- status = xQueueSend(sAppEventQueue, aEvent, 1);
- }
-
- if (!status)
+ if (osMessageQueuePut(sAppEventQueue, aEvent, osPriorityNormal, 0) != osOK)
{
SILABS_LOG("Failed to post event to app task event queue");
}
}
else
{
- SILABS_LOG("Event Queue is NULL should never happen");
+ SILABS_LOG("App Event Queue is uninitialized");
}
}
diff --git a/examples/platform/silabs/BaseApplication.h b/examples/platform/silabs/BaseApplication.h
index 1a5c555..ffb1f24 100644
--- a/examples/platform/silabs/BaseApplication.h
+++ b/examples/platform/silabs/BaseApplication.h
@@ -27,12 +27,11 @@
#include <stdint.h>
#include "AppEvent.h"
-#include "FreeRTOS.h"
-#include "timers.h" // provides FreeRTOS timer support
#include <app/clusters/identify-server/identify-server.h>
#include <app/server/AppDelegate.h>
#include <app/util/config.h>
#include <ble/BLEEndPoint.h>
+#include <cmsis_os2.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceEvent.h>
#include <platform/CHIPDeviceLayer.h>
@@ -97,7 +96,7 @@
*
* @return CHIP_ERROR CHIP_NO_ERROR if no errors
*/
- CHIP_ERROR StartAppTask(TaskFunction_t taskFunction);
+ CHIP_ERROR StartAppTask(osThreadFunc_t taskFunction);
/**
* @brief Links the application specific led to the baseApplication context
@@ -185,7 +184,7 @@
*
* @param xTimer timer that finished
*/
- static void FunctionTimerEventHandler(TimerHandle_t xTimer);
+ static void FunctionTimerEventHandler(osTimerId_t xTimer);
/**
* @brief Timer Event processing function
@@ -210,7 +209,7 @@
*
* @param xTimer timer that finished
*/
- static void LightTimerEventHandler(TimerHandle_t xTimer);
+ static void LightTimerEventHandler(osTimerId_t xTimer);
/**
* @brief Updates device LEDs