Skip to content

Commit 03531f7

Browse files
authoredMar 20, 2024
Change freertos APIs to cmsisos APIs in BaseApplication (project-chip#32652)
1 parent b6f8e4f commit 03531f7

File tree

2 files changed

+49
-68
lines changed

2 files changed

+49
-68
lines changed
 

‎examples/platform/silabs/BaseApplication.cpp

+45-63
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,10 @@ namespace {
9898
* Variable declarations
9999
*********************************************************/
100100

101-
TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer.
102-
TimerHandle_t sLightTimer;
103-
104-
TaskHandle_t sAppTaskHandle;
105-
QueueHandle_t sAppEventQueue;
101+
osTimerId_t sFunctionTimer;
102+
osTimerId_t sLightTimer;
103+
osThreadId_t sAppTaskHandle;
104+
osMessageQueueId_t sAppEventQueue;
106105

107106
#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT)))
108107
LEDWidget sStatusLED;
@@ -119,11 +118,24 @@ bool sIsAttached = false;
119118
bool sHaveBLEConnections = false;
120119
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
121120

122-
uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
123-
StaticQueue_t sAppEventQueueStruct;
121+
constexpr uint32_t kLightTimerPeriod = static_cast<uint32_t>(pdMS_TO_TICKS(10));
124122

125-
StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)];
126-
StaticTask_t appTaskStruct;
123+
uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
124+
StaticQueue_t sAppEventQueueStruct; // TODO abstract type for static controlblock
125+
constexpr osMessageQueueAttr_t appEventQueueAttr = { .cb_mem = &sAppEventQueueStruct,
126+
.cb_size = sizeof(sAppEventQueueBuffer),
127+
.mq_mem = sAppEventQueueBuffer,
128+
.mq_size = sizeof(sAppEventQueueBuffer) };
129+
130+
uint8_t appStack[APP_TASK_STACK_SIZE];
131+
StaticTask_t appTaskStruct; // TODO abstract type for static controlblock
132+
constexpr osThreadAttr_t appTaskAttr = { .name = APP_TASK_NAME,
133+
.attr_bits = osThreadDetached,
134+
.cb_mem = &appTaskStruct,
135+
.cb_size = sizeof(appTaskStruct),
136+
.stack_mem = appStack,
137+
.stack_size = APP_TASK_STACK_SIZE,
138+
.priority = osPriorityNormal };
127139

128140
#ifdef DISPLAY_ENABLED
129141
SilabsLCD slLCD;
@@ -192,18 +204,17 @@ void BaseApplicationDelegate::OnCommissioningWindowClosed()
192204
* AppTask Definitions
193205
*********************************************************/
194206

195-
CHIP_ERROR BaseApplication::StartAppTask(TaskFunction_t taskFunction)
207+
CHIP_ERROR BaseApplication::StartAppTask(osThreadFunc_t taskFunction)
196208
{
197-
sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
209+
sAppEventQueue = osMessageQueueNew(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), &appEventQueueAttr);
198210
if (sAppEventQueue == NULL)
199211
{
200212
SILABS_LOG("Failed to allocate app event queue");
201213
appError(APP_ERROR_EVENT_QUEUE_FAILED);
202214
}
203215

204216
// Start App task.
205-
sAppTaskHandle =
206-
xTaskCreateStatic(taskFunction, APP_TASK_NAME, ArraySize(appStack), &sAppEventQueue, 1, appStack, &appTaskStruct);
217+
sAppTaskHandle = osThreadNew(taskFunction, &sAppEventQueue, &appTaskAttr);
207218
if (sAppTaskHandle == nullptr)
208219
{
209220
SILABS_LOG("Failed to create app task");
@@ -234,25 +245,23 @@ CHIP_ERROR BaseApplication::Init()
234245

235246
#endif
236247

237-
// Create FreeRTOS sw timer for Function Selection.
238-
sFunctionTimer = xTimerCreate("FnTmr", // Just a text name, not used by the RTOS kernel
239-
pdMS_TO_TICKS(1), // == default timer period
240-
false, // no timer reload (==one-shot)
241-
(void *) this, // init timer id = app task obj context
242-
FunctionTimerEventHandler // timer callback handler
248+
// Create cmsis os sw timer for Function Selection.
249+
sFunctionTimer = osTimerNew(FunctionTimerEventHandler, // timer callback handler
250+
osTimerOnce, // no timer reload (one-shot timer)
251+
(void *) this, // pass the app task obj context
252+
NULL // No osTimerAttr_t to provide.
243253
);
244254
if (sFunctionTimer == NULL)
245255
{
246256
SILABS_LOG("funct timer create failed");
247257
appError(APP_ERROR_CREATE_TIMER_FAILED);
248258
}
249259

250-
// Create FreeRTOS sw timer for LED Management.
251-
sLightTimer = xTimerCreate("LightTmr", // Text Name
252-
pdMS_TO_TICKS(10), // Default timer period
253-
true, // reload timer
254-
(void *) this, // Timer Id
255-
LightTimerEventHandler // Timer callback handler
260+
// Create cmsis os sw timer for LED Management.
261+
sLightTimer = osTimerNew(LightTimerEventHandler, // Timer callback handler"LightTmr",
262+
osTimerPeriodic, // timer repeats automatically
263+
(void *) this, // pass the app task obj context
264+
NULL // No osTimerAttr_t to provide.
256265
);
257266
if (sLightTimer == NULL)
258267
{
@@ -289,7 +298,7 @@ CHIP_ERROR BaseApplication::Init()
289298
return err;
290299
}
291300

292-
void BaseApplication::FunctionTimerEventHandler(TimerHandle_t xTimer)
301+
void BaseApplication::FunctionTimerEventHandler(osTimerId_t xTimer)
293302
{
294303
AppEvent event;
295304
event.Type = AppEvent::kEventType_Timer;
@@ -524,7 +533,7 @@ void BaseApplication::UpdateDisplay()
524533

525534
void BaseApplication::CancelFunctionTimer()
526535
{
527-
if (xTimerStop(sFunctionTimer, pdMS_TO_TICKS(0)) == pdFAIL)
536+
if (osTimerStop(sFunctionTimer) == osError)
528537
{
529538
SILABS_LOG("app timer stop() failed");
530539
appError(APP_ERROR_STOP_TIMER_FAILED);
@@ -533,16 +542,8 @@ void BaseApplication::CancelFunctionTimer()
533542

534543
void BaseApplication::StartFunctionTimer(uint32_t aTimeoutInMs)
535544
{
536-
if (xTimerIsTimerActive(sFunctionTimer))
537-
{
538-
SILABS_LOG("app timer already started!");
539-
CancelFunctionTimer();
540-
}
541-
542-
// timer is not active, change its period to required value (== restart).
543-
// FreeRTOS- Block for a maximum of 100 ms if the change period command
544-
// cannot immediately be sent to the timer command queue.
545-
if (xTimerChangePeriod(sFunctionTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
545+
// Starts or restarts the function timer
546+
if (osTimerStart(sFunctionTimer, pdMS_TO_TICKS(aTimeoutInMs)) != osOK)
546547
{
547548
SILABS_LOG("app timer start() failed");
548549
appError(APP_ERROR_START_TIMER_FAILED);
@@ -587,7 +588,7 @@ void BaseApplication::CancelFactoryResetSequence()
587588

588589
void BaseApplication::StartStatusLEDTimer()
589590
{
590-
if (pdPASS != xTimerStart(sLightTimer, pdMS_TO_TICKS(0)))
591+
if (osTimerStart(sLightTimer, kLightTimerPeriod) != osOK)
591592
{
592593
SILABS_LOG("Light Time start failed");
593594
appError(APP_ERROR_START_TIMER_FAILED);
@@ -600,10 +601,10 @@ void BaseApplication::StopStatusLEDTimer()
600601
sStatusLED.Set(false);
601602
#endif // ENABLE_WSTK_LEDS
602603

603-
if (xTimerStop(sLightTimer, pdMS_TO_TICKS(100)) != pdPASS)
604+
if (osTimerStop(sLightTimer) == osError)
604605
{
605606
SILABS_LOG("Light Time start failed");
606-
appError(APP_ERROR_START_TIMER_FAILED);
607+
appError(APP_ERROR_STOP_TIMER_FAILED);
607608
}
608609
}
609610

@@ -676,7 +677,7 @@ void BaseApplication::OnTriggerIdentifyEffect(Identify * identify)
676677
}
677678
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER
678679

679-
void BaseApplication::LightTimerEventHandler(TimerHandle_t xTimer)
680+
void BaseApplication::LightTimerEventHandler(osTimerId_t xTimer)
680681
{
681682
LightEventHandler();
682683
}
@@ -715,35 +716,16 @@ void BaseApplication::UpdateLCDStatusScreen(void)
715716

716717
void BaseApplication::PostEvent(const AppEvent * aEvent)
717718
{
718-
if (sAppEventQueue != NULL)
719+
if (sAppEventQueue != nullptr)
719720
{
720-
BaseType_t status;
721-
if (xPortIsInsideInterrupt())
722-
{
723-
BaseType_t higherPrioTaskWoken = pdFALSE;
724-
status = xQueueSendFromISR(sAppEventQueue, aEvent, &higherPrioTaskWoken);
725-
726-
#ifdef portYIELD_FROM_ISR
727-
portYIELD_FROM_ISR(higherPrioTaskWoken);
728-
#elif portEND_SWITCHING_ISR // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
729-
portEND_SWITCHING_ISR(higherPrioTaskWoken);
730-
#else // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
731-
#error "Must have portYIELD_FROM_ISR or portEND_SWITCHING_ISR"
732-
#endif // portYIELD_FROM_ISR or portEND_SWITCHING_ISR
733-
}
734-
else
735-
{
736-
status = xQueueSend(sAppEventQueue, aEvent, 1);
737-
}
738-
739-
if (!status)
721+
if (osMessageQueuePut(sAppEventQueue, aEvent, osPriorityNormal, 0) != osOK)
740722
{
741723
SILABS_LOG("Failed to post event to app task event queue");
742724
}
743725
}
744726
else
745727
{
746-
SILABS_LOG("Event Queue is NULL should never happen");
728+
SILABS_LOG("App Event Queue is uninitialized");
747729
}
748730
}
749731

‎examples/platform/silabs/BaseApplication.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@
2727
#include <stdint.h>
2828

2929
#include "AppEvent.h"
30-
#include "FreeRTOS.h"
31-
#include "timers.h" // provides FreeRTOS timer support
3230
#include <app/clusters/identify-server/identify-server.h>
3331
#include <app/server/AppDelegate.h>
3432
#include <app/util/config.h>
3533
#include <ble/BLEEndPoint.h>
34+
#include <cmsis_os2.h>
3635
#include <lib/core/CHIPError.h>
3736
#include <platform/CHIPDeviceEvent.h>
3837
#include <platform/CHIPDeviceLayer.h>
@@ -97,7 +96,7 @@ class BaseApplication
9796
*
9897
* @return CHIP_ERROR CHIP_NO_ERROR if no errors
9998
*/
100-
CHIP_ERROR StartAppTask(TaskFunction_t taskFunction);
99+
CHIP_ERROR StartAppTask(osThreadFunc_t taskFunction);
101100

102101
/**
103102
* @brief Links the application specific led to the baseApplication context
@@ -185,7 +184,7 @@ class BaseApplication
185184
*
186185
* @param xTimer timer that finished
187186
*/
188-
static void FunctionTimerEventHandler(TimerHandle_t xTimer);
187+
static void FunctionTimerEventHandler(osTimerId_t xTimer);
189188

190189
/**
191190
* @brief Timer Event processing function
@@ -210,7 +209,7 @@ class BaseApplication
210209
*
211210
* @param xTimer timer that finished
212211
*/
213-
static void LightTimerEventHandler(TimerHandle_t xTimer);
212+
static void LightTimerEventHandler(osTimerId_t xTimer);
214213

215214
/**
216215
* @brief Updates device LEDs

0 commit comments

Comments
 (0)
Please sign in to comment.