Skip to content

Commit

Permalink
Migrate our <apps>Manager from freeRTOS api to CMSIS OS2 Api (#32705)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinez-silabs committed Mar 26, 2024
1 parent 4b26ef4 commit aa9f8e5
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 187 deletions.
7 changes: 3 additions & 4 deletions examples/chef/silabs/include/LightingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

#include "AppEvent.h"

#include "FreeRTOS.h"
#include "timers.h" // provides FreeRTOS timer support

#include <cmsis_os2.h>
#include <lib/core/CHIPError.h>

class LightingManager
Expand Down Expand Up @@ -68,11 +66,12 @@ class LightingManager
bool mAutoTurnOff;
uint32_t mAutoTurnOffDuration;
bool mAutoTurnOffTimerArmed;
osTimerId_t mLightTimer;

void CancelTimer(void);
void StartTimer(uint32_t aTimeoutMs);

static void TimerEventHandler(TimerHandle_t xTimer);
static void TimerEventHandler(void * timerCbArg);
static void AutoTurnOffTimerEventHandler(AppEvent * aEvent);
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);

Expand Down
47 changes: 17 additions & 30 deletions examples/chef/silabs/src/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,20 @@

#include "AppConfig.h"
#include "AppTask.h"
#include <FreeRTOS.h>

LightingManager LightingManager::sLight;

TimerHandle_t sLightTimer;

CHIP_ERROR LightingManager::Init()
{
// Create FreeRTOS sw timer for light timer.
sLightTimer = xTimerCreate("lightTmr", // Just a text name, not used by the RTOS kernel
1, // == default timer period (mS)
false, // no timer reload (==one-shot)
(void *) this, // init timer id = light obj context
TimerEventHandler // timer callback handler
);

if (sLightTimer == NULL)
// Create cmsis os sw timer for light timer.
mLightTimer = osTimerNew(TimerEventHandler, // 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 (mLightTimer == NULL)
{
SILABS_LOG("sLightTimer timer create failed");
SILABS_LOG("mLightTimer timer create failed");
return APP_ERROR_CREATE_TIMER_FAILED;
}

Expand Down Expand Up @@ -123,38 +118,30 @@ bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)

void LightingManager::StartTimer(uint32_t aTimeoutMs)
{
if (xTimerIsTimerActive(sLightTimer))
{
SILABS_LOG("app timer already started!");
CancelTimer();
}

// timer is not active, change its period to required value (== restart).
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
// cannot immediately be sent to the timer command queue.
if (xTimerChangePeriod(sLightTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
// Starts or restarts the function timer
if (osTimerStart(mLightTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
{
SILABS_LOG("sLightTimer timer start() failed");
SILABS_LOG("mLightTimer timer start() failed");
appError(APP_ERROR_START_TIMER_FAILED);
}
}

void LightingManager::CancelTimer(void)
{
if (xTimerStop(sLightTimer, 0) == pdFAIL)
if (osTimerStop(mLightTimer) == osError)
{
SILABS_LOG("sLightTimer stop() failed");
SILABS_LOG("mLightTimer stop() failed");
appError(APP_ERROR_STOP_TIMER_FAILED);
}
}

void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
void LightingManager::TimerEventHandler(void * timerCbArg)
{
// Get light obj context from timer id.
LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
// The callback argument is the light obj context assigned at timer creation.
LightingManager * light = static_cast<LightingManager *>(timerCbArg);

// The timer event handler will be called in the context of the timer task
// once sLightTimer expires. Post an event to apptask queue with the actual handler
// once mLightTimer expires. Post an event to apptask queue with the actual handler
// so that the event can be handled in the context of the apptask.
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
Expand Down
7 changes: 3 additions & 4 deletions examples/lighting-app/silabs/include/LightingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@

#include "AppEvent.h"

#include "FreeRTOS.h"
#include "timers.h" // provides FreeRTOS timer support
#include <app/clusters/on-off-server/on-off-server.h>

#include <cmsis_os2.h>
#include <lib/core/CHIPError.h>

class LightingManager
Expand Down Expand Up @@ -72,11 +70,12 @@ class LightingManager
uint32_t mAutoTurnOffDuration;
bool mAutoTurnOffTimerArmed;
bool mOffEffectArmed;
osTimerId_t mLightTimer;

void CancelTimer(void);
void StartTimer(uint32_t aTimeoutMs);

static void TimerEventHandler(TimerHandle_t xTimer);
static void TimerEventHandler(void * timerCbArg);
static void AutoTurnOffTimerEventHandler(AppEvent * aEvent);
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
static void OffEffectTimerEventHandler(AppEvent * aEvent);
Expand Down
45 changes: 16 additions & 29 deletions examples/lighting-app/silabs/src/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "AppConfig.h"
#include "AppTask.h"
#include <FreeRTOS.h>

#include <lib/support/TypeTraits.h>

Expand All @@ -30,9 +29,6 @@ using namespace ::chip::app::Clusters::OnOff;
using namespace ::chip::DeviceLayer;

LightingManager LightingManager::sLight;

TimerHandle_t sLightTimer;

namespace {

/**********************************************************
Expand All @@ -50,17 +46,16 @@ OnOffEffect gEffect = {

CHIP_ERROR LightingManager::Init()
{
// Create FreeRTOS sw timer for light timer.
sLightTimer = xTimerCreate("lightTmr", // 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 = light obj context
TimerEventHandler // timer callback handler
// Create cmsis os sw timer for light timer.
mLightTimer = osTimerNew(TimerEventHandler, // 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 (sLightTimer == NULL)
if (mLightTimer == NULL)
{
SILABS_LOG("sLightTimer timer create failed");
SILABS_LOG("mLightTimer timer create failed");
return APP_ERROR_CREATE_TIMER_FAILED;
}

Expand Down Expand Up @@ -157,38 +152,30 @@ bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)

void LightingManager::StartTimer(uint32_t aTimeoutMs)
{
if (xTimerIsTimerActive(sLightTimer))
{
SILABS_LOG("app timer already started!");
CancelTimer();
}

// 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(sLightTimer, pdMS_TO_TICKS(aTimeoutMs), pdMS_TO_TICKS(100)) != pdPASS)
// Starts or restarts the function timer
if (osTimerStart(mLightTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
{
SILABS_LOG("sLightTimer timer start() failed");
SILABS_LOG("mLightTimer timer start() failed");
appError(APP_ERROR_START_TIMER_FAILED);
}
}

void LightingManager::CancelTimer(void)
{
if (xTimerStop(sLightTimer, pdMS_TO_TICKS(0)) == pdFAIL)
if (osTimerStop(mLightTimer) == osError)
{
SILABS_LOG("sLightTimer stop() failed");
SILABS_LOG("mLightTimer stop() failed");
appError(APP_ERROR_STOP_TIMER_FAILED);
}
}

void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
void LightingManager::TimerEventHandler(void * timerCbArg)
{
// Get light obj context from timer id.
LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
// The callback argument is the light obj context assigned at timer creation.
LightingManager * light = static_cast<LightingManager *>(timerCbArg);

// The timer event handler will be called in the context of the timer task
// once sLightTimer expires. Post an event to apptask queue with the actual handler
// once mLightTimer expires. Post an event to apptask queue with the actual handler
// so that the event can be handled in the context of the apptask.
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
Expand Down
7 changes: 3 additions & 4 deletions examples/lock-app/silabs/include/LockManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

#include "AppEvent.h"

#include "FreeRTOS.h"
#include "timers.h" // provides FreeRTOS timer support

#include <cmsis_os2.h>
#include <lib/core/CHIPError.h>

struct WeekDaysScheduleInfo
Expand Down Expand Up @@ -203,10 +201,11 @@ class LockManager
void CancelTimer(void);
void StartTimer(uint32_t aTimeoutMs);

static void TimerEventHandler(TimerHandle_t xTimer);
static void TimerEventHandler(void * timerCbArg);
static void AutoLockTimerEventHandler(AppEvent * aEvent);
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);

osTimerId_t mLockTimer;
EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers];
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentials];
WeekDaysScheduleInfo mWeekdaySchedule[kMaxUsers][kMaxWeekdaySchedulesPerUser];
Expand Down
44 changes: 16 additions & 28 deletions examples/lock-app/silabs/src/LockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@

#include "AppConfig.h"
#include "AppTask.h"
#include <FreeRTOS.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <cstring>
#include <lib/support/logging/CHIPLogging.h>

LockManager LockManager::sLock;

TimerHandle_t sLockTimer;

using namespace ::chip::DeviceLayer::Internal;
using namespace EFR32DoorLock::LockInitParams;

Expand Down Expand Up @@ -77,17 +74,16 @@ CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters:
return APP_ERROR_ALLOCATION_FAILED;
}

// Create FreeRTOS sw timer for lock timer.
sLockTimer = xTimerCreate("lockTmr", // Just a text name, not used by the RTOS kernel
1, // == default timer period
false, // no timer reload (==one-shot)
(void *) this, // init timer id = lock obj context
TimerEventHandler // timer callback handler
// Create cmsis os sw timer for lock timer.
mLockTimer = osTimerNew(TimerEventHandler, // 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 (sLockTimer == NULL)
if (mLockTimer == NULL)
{
SILABS_LOG("sLockTimer timer create failed");
SILABS_LOG("mLockTimer timer create failed");
return APP_ERROR_CREATE_TIMER_FAILED;
}

Expand Down Expand Up @@ -222,38 +218,30 @@ bool LockManager::InitiateAction(int32_t aActor, Action_t aAction)

void LockManager::StartTimer(uint32_t aTimeoutMs)
{
if (xTimerIsTimerActive(sLockTimer))
{
SILABS_LOG("app timer already started!");
CancelTimer();
}

// 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(sLockTimer, pdMS_TO_TICKS(aTimeoutMs), pdMS_TO_TICKS(100)) != pdPASS)
// Starts or restarts the function timer
if (osTimerStart(mLockTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
{
SILABS_LOG("sLockTimer timer start() failed");
SILABS_LOG("mLockTimer timer start() failed");
appError(APP_ERROR_START_TIMER_FAILED);
}
}

void LockManager::CancelTimer(void)
{
if (xTimerStop(sLockTimer, pdMS_TO_TICKS(0)) == pdFAIL)
if (osTimerStop(mLockTimer) == osError)
{
SILABS_LOG("sLockTimer stop() failed");
SILABS_LOG("mLockTimer stop() failed");
appError(APP_ERROR_STOP_TIMER_FAILED);
}
}

void LockManager::TimerEventHandler(TimerHandle_t xTimer)
void LockManager::TimerEventHandler(void * timerCbArg)
{
// Get lock obj context from timer id.
LockManager * lock = static_cast<LockManager *>(pvTimerGetTimerID(xTimer));
// The callback argument is the light obj context assigned at timer creation.
LockManager * lock = static_cast<LockManager *>(timerCbArg);

// The timer event handler will be called in the context of the timer task
// once sLockTimer expires. Post an event to apptask queue with the actual handler
// once mLockTimer expires. Post an event to apptask queue with the actual handler
// so that the event can be handled in the context of the apptask.
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
Expand Down
8 changes: 5 additions & 3 deletions examples/platform/silabs/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
#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
#define EXAMPLE_VENDOR_ID 0xcafe

#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT)))
Expand Down Expand Up @@ -298,11 +300,11 @@ CHIP_ERROR BaseApplication::Init()
return err;
}

void BaseApplication::FunctionTimerEventHandler(osTimerId_t xTimer)
void BaseApplication::FunctionTimerEventHandler(void * timerCbArg)
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
event.TimerEvent.Context = (void *) xTimer;
event.TimerEvent.Context = timerCbArg;
event.Handler = FunctionEventHandler;
PostEvent(&event);
}
Expand Down Expand Up @@ -677,7 +679,7 @@ void BaseApplication::OnTriggerIdentifyEffect(Identify * identify)
}
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER

void BaseApplication::LightTimerEventHandler(osTimerId_t xTimer)
void BaseApplication::LightTimerEventHandler(void * timerCbArg)
{
LightEventHandler();
}
Expand Down
8 changes: 4 additions & 4 deletions examples/platform/silabs/BaseApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ class BaseApplication
* @brief Function Timer finished callback function
* Post an FunctionEventHandler event
*
* @param xTimer timer that finished
* @param timerCbArg argument to the timer callback function assigned at timer creation
*/
static void FunctionTimerEventHandler(osTimerId_t xTimer);
static void FunctionTimerEventHandler(void * timerCbArg);

/**
* @brief Timer Event processing function
Expand All @@ -207,9 +207,9 @@ class BaseApplication
* @brief Light Timer finished callback function
* Calls LED processing function
*
* @param xTimer timer that finished
* @param timerCbArg argument to the timer callback function assigned at timer creation
*/
static void LightTimerEventHandler(osTimerId_t xTimer);
static void LightTimerEventHandler(void * timerCbArg);

/**
* @brief Updates device LEDs
Expand Down
Loading

0 comments on commit aa9f8e5

Please sign in to comment.