Skip to content

Commit

Permalink
Change freertos APIs to cmsisos APIs in BaseApplication (#32652)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinez-silabs authored and pull[bot] committed May 24, 2024
1 parent 2042bbf commit 3268724
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 68 deletions.
108 changes: 45 additions & 63 deletions examples/platform/silabs/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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;
Expand Down Expand Up @@ -192,18 +204,17 @@ 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");
appError(APP_ERROR_EVENT_QUEUE_FAILED);
}

// 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");
Expand Down Expand Up @@ -234,25 +245,23 @@ 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)
{
SILABS_LOG("funct timer create failed");
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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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");
}
}

Expand Down
9 changes: 4 additions & 5 deletions examples/platform/silabs/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3268724

Please sign in to comment.