From 32687241d701805495b4b42d9f52e7e0074d6d68 Mon Sep 17 00:00:00 2001 From: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> Date: Wed, 20 Mar 2024 17:03:14 -0400 Subject: [PATCH] Change freertos APIs to cmsisos APIs in BaseApplication (#32652) --- examples/platform/silabs/BaseApplication.cpp | 108 ++++++++----------- examples/platform/silabs/BaseApplication.h | 9 +- 2 files changed, 49 insertions(+), 68 deletions(-) diff --git a/examples/platform/silabs/BaseApplication.cpp b/examples/platform/silabs/BaseApplication.cpp index abd2c207da0b06..391104785b35c8 100644 --- a/examples/platform/silabs/BaseApplication.cpp +++ b/examples/platform/silabs/BaseApplication.cpp @@ -98,11 +98,10 @@ namespace { * 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 sIsAttached = false; 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(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 @@ void BaseApplicationDelegate::OnCommissioningWindowClosed() * 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 @@ CHIP_ERROR BaseApplication::StartAppTask(TaskFunction_t taskFunction) } // 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 @@ CHIP_ERROR BaseApplication::Init() #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 @@ CHIP_ERROR BaseApplication::Init() 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 @@ CHIP_ERROR BaseApplication::Init() 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::UpdateDisplay() 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::CancelFunctionTimer() 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::CancelFactoryResetSequence() 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 @@ void BaseApplication::StopStatusLEDTimer() 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 @@ void BaseApplication::OnTriggerIdentifyEffect(Identify * identify) } #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::UpdateLCDStatusScreen(void) 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 1a5c55587d76fe..ffb1f24cae2b81 100644 --- a/examples/platform/silabs/BaseApplication.h +++ b/examples/platform/silabs/BaseApplication.h @@ -27,12 +27,11 @@ #include #include "AppEvent.h" -#include "FreeRTOS.h" -#include "timers.h" // provides FreeRTOS timer support #include #include #include #include +#include #include #include #include @@ -97,7 +96,7 @@ class BaseApplication * * @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 @@ class BaseApplication * * @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 @@ class BaseApplication * * @param xTimer timer that finished */ - static void LightTimerEventHandler(TimerHandle_t xTimer); + static void LightTimerEventHandler(osTimerId_t xTimer); /** * @brief Updates device LEDs