Skip to content

Commit aa9f8e5

Browse files
Migrate our <apps>Manager from freeRTOS api to CMSIS OS2 Api (project-chip#32705)
1 parent 4b26ef4 commit aa9f8e5

File tree

17 files changed

+119
-187
lines changed

17 files changed

+119
-187
lines changed

examples/chef/silabs/include/LightingManager.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
#include "AppEvent.h"
2525

26-
#include "FreeRTOS.h"
27-
#include "timers.h" // provides FreeRTOS timer support
28-
26+
#include <cmsis_os2.h>
2927
#include <lib/core/CHIPError.h>
3028

3129
class LightingManager
@@ -68,11 +66,12 @@ class LightingManager
6866
bool mAutoTurnOff;
6967
uint32_t mAutoTurnOffDuration;
7068
bool mAutoTurnOffTimerArmed;
69+
osTimerId_t mLightTimer;
7170

7271
void CancelTimer(void);
7372
void StartTimer(uint32_t aTimeoutMs);
7473

75-
static void TimerEventHandler(TimerHandle_t xTimer);
74+
static void TimerEventHandler(void * timerCbArg);
7675
static void AutoTurnOffTimerEventHandler(AppEvent * aEvent);
7776
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
7877

examples/chef/silabs/src/LightingManager.cpp

+17-30
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,20 @@
2121

2222
#include "AppConfig.h"
2323
#include "AppTask.h"
24-
#include <FreeRTOS.h>
2524

2625
LightingManager LightingManager::sLight;
2726

28-
TimerHandle_t sLightTimer;
29-
3027
CHIP_ERROR LightingManager::Init()
3128
{
32-
// Create FreeRTOS sw timer for light timer.
33-
sLightTimer = xTimerCreate("lightTmr", // Just a text name, not used by the RTOS kernel
34-
1, // == default timer period (mS)
35-
false, // no timer reload (==one-shot)
36-
(void *) this, // init timer id = light obj context
37-
TimerEventHandler // timer callback handler
38-
);
39-
40-
if (sLightTimer == NULL)
29+
// Create cmsis os sw timer for light timer.
30+
mLightTimer = osTimerNew(TimerEventHandler, // timer callback handler
31+
osTimerOnce, // no timer reload (one-shot timer)
32+
(void *) this, // pass the app task obj context
33+
NULL // No osTimerAttr_t to provide.
34+
35+
if (mLightTimer == NULL)
4136
{
42-
SILABS_LOG("sLightTimer timer create failed");
37+
SILABS_LOG("mLightTimer timer create failed");
4338
return APP_ERROR_CREATE_TIMER_FAILED;
4439
}
4540

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

124119
void LightingManager::StartTimer(uint32_t aTimeoutMs)
125120
{
126-
if (xTimerIsTimerActive(sLightTimer))
127-
{
128-
SILABS_LOG("app timer already started!");
129-
CancelTimer();
130-
}
131-
132-
// timer is not active, change its period to required value (== restart).
133-
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
134-
// cannot immediately be sent to the timer command queue.
135-
if (xTimerChangePeriod(sLightTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
121+
// Starts or restarts the function timer
122+
if (osTimerStart(mLightTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
136123
{
137-
SILABS_LOG("sLightTimer timer start() failed");
124+
SILABS_LOG("mLightTimer timer start() failed");
138125
appError(APP_ERROR_START_TIMER_FAILED);
139126
}
140127
}
141128

142129
void LightingManager::CancelTimer(void)
143130
{
144-
if (xTimerStop(sLightTimer, 0) == pdFAIL)
131+
if (osTimerStop(mLightTimer) == osError)
145132
{
146-
SILABS_LOG("sLightTimer stop() failed");
133+
SILABS_LOG("mLightTimer stop() failed");
147134
appError(APP_ERROR_STOP_TIMER_FAILED);
148135
}
149136
}
150137

151-
void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
138+
void LightingManager::TimerEventHandler(void * timerCbArg)
152139
{
153-
// Get light obj context from timer id.
154-
LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
140+
// The callback argument is the light obj context assigned at timer creation.
141+
LightingManager * light = static_cast<LightingManager *>(timerCbArg);
155142

156143
// The timer event handler will be called in the context of the timer task
157-
// once sLightTimer expires. Post an event to apptask queue with the actual handler
144+
// once mLightTimer expires. Post an event to apptask queue with the actual handler
158145
// so that the event can be handled in the context of the apptask.
159146
AppEvent event;
160147
event.Type = AppEvent::kEventType_Timer;

examples/lighting-app/silabs/include/LightingManager.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323

2424
#include "AppEvent.h"
2525

26-
#include "FreeRTOS.h"
27-
#include "timers.h" // provides FreeRTOS timer support
2826
#include <app/clusters/on-off-server/on-off-server.h>
29-
27+
#include <cmsis_os2.h>
3028
#include <lib/core/CHIPError.h>
3129

3230
class LightingManager
@@ -72,11 +70,12 @@ class LightingManager
7270
uint32_t mAutoTurnOffDuration;
7371
bool mAutoTurnOffTimerArmed;
7472
bool mOffEffectArmed;
73+
osTimerId_t mLightTimer;
7574

7675
void CancelTimer(void);
7776
void StartTimer(uint32_t aTimeoutMs);
7877

79-
static void TimerEventHandler(TimerHandle_t xTimer);
78+
static void TimerEventHandler(void * timerCbArg);
8079
static void AutoTurnOffTimerEventHandler(AppEvent * aEvent);
8180
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
8281
static void OffEffectTimerEventHandler(AppEvent * aEvent);

examples/lighting-app/silabs/src/LightingManager.cpp

+16-29
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "AppConfig.h"
2323
#include "AppTask.h"
24-
#include <FreeRTOS.h>
2524

2625
#include <lib/support/TypeTraits.h>
2726

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

3231
LightingManager LightingManager::sLight;
33-
34-
TimerHandle_t sLightTimer;
35-
3632
namespace {
3733

3834
/**********************************************************
@@ -50,17 +46,16 @@ OnOffEffect gEffect = {
5046

5147
CHIP_ERROR LightingManager::Init()
5248
{
53-
// Create FreeRTOS sw timer for light timer.
54-
sLightTimer = xTimerCreate("lightTmr", // Just a text name, not used by the RTOS kernel
55-
pdMS_TO_TICKS(1), // == default timer period
56-
false, // no timer reload (==one-shot)
57-
(void *) this, // init timer id = light obj context
58-
TimerEventHandler // timer callback handler
49+
// Create cmsis os sw timer for light timer.
50+
mLightTimer = osTimerNew(TimerEventHandler, // timer callback handler
51+
osTimerOnce, // no timer reload (one-shot timer)
52+
(void *) this, // pass the app task obj context
53+
NULL // No osTimerAttr_t to provide.
5954
);
6055

61-
if (sLightTimer == NULL)
56+
if (mLightTimer == NULL)
6257
{
63-
SILABS_LOG("sLightTimer timer create failed");
58+
SILABS_LOG("mLightTimer timer create failed");
6459
return APP_ERROR_CREATE_TIMER_FAILED;
6560
}
6661

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

158153
void LightingManager::StartTimer(uint32_t aTimeoutMs)
159154
{
160-
if (xTimerIsTimerActive(sLightTimer))
161-
{
162-
SILABS_LOG("app timer already started!");
163-
CancelTimer();
164-
}
165-
166-
// timer is not active, change its period to required value (== restart).
167-
// FreeRTOS- Block for a maximum of 100 ms if the change period command
168-
// cannot immediately be sent to the timer command queue.
169-
if (xTimerChangePeriod(sLightTimer, pdMS_TO_TICKS(aTimeoutMs), pdMS_TO_TICKS(100)) != pdPASS)
155+
// Starts or restarts the function timer
156+
if (osTimerStart(mLightTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
170157
{
171-
SILABS_LOG("sLightTimer timer start() failed");
158+
SILABS_LOG("mLightTimer timer start() failed");
172159
appError(APP_ERROR_START_TIMER_FAILED);
173160
}
174161
}
175162

176163
void LightingManager::CancelTimer(void)
177164
{
178-
if (xTimerStop(sLightTimer, pdMS_TO_TICKS(0)) == pdFAIL)
165+
if (osTimerStop(mLightTimer) == osError)
179166
{
180-
SILABS_LOG("sLightTimer stop() failed");
167+
SILABS_LOG("mLightTimer stop() failed");
181168
appError(APP_ERROR_STOP_TIMER_FAILED);
182169
}
183170
}
184171

185-
void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
172+
void LightingManager::TimerEventHandler(void * timerCbArg)
186173
{
187-
// Get light obj context from timer id.
188-
LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
174+
// The callback argument is the light obj context assigned at timer creation.
175+
LightingManager * light = static_cast<LightingManager *>(timerCbArg);
189176

190177
// The timer event handler will be called in the context of the timer task
191-
// once sLightTimer expires. Post an event to apptask queue with the actual handler
178+
// once mLightTimer expires. Post an event to apptask queue with the actual handler
192179
// so that the event can be handled in the context of the apptask.
193180
AppEvent event;
194181
event.Type = AppEvent::kEventType_Timer;

examples/lock-app/silabs/include/LockManager.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424

2525
#include "AppEvent.h"
2626

27-
#include "FreeRTOS.h"
28-
#include "timers.h" // provides FreeRTOS timer support
29-
27+
#include <cmsis_os2.h>
3028
#include <lib/core/CHIPError.h>
3129

3230
struct WeekDaysScheduleInfo
@@ -203,10 +201,11 @@ class LockManager
203201
void CancelTimer(void);
204202
void StartTimer(uint32_t aTimeoutMs);
205203

206-
static void TimerEventHandler(TimerHandle_t xTimer);
204+
static void TimerEventHandler(void * timerCbArg);
207205
static void AutoLockTimerEventHandler(AppEvent * aEvent);
208206
static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
209207

208+
osTimerId_t mLockTimer;
210209
EmberAfPluginDoorLockUserInfo mLockUsers[kMaxUsers];
211210
EmberAfPluginDoorLockCredentialInfo mLockCredentials[kNumCredentialTypes][kMaxCredentials];
212211
WeekDaysScheduleInfo mWeekdaySchedule[kMaxUsers][kMaxWeekdaySchedulesPerUser];

examples/lock-app/silabs/src/LockManager.cpp

+16-28
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121

2222
#include "AppConfig.h"
2323
#include "AppTask.h"
24-
#include <FreeRTOS.h>
2524
#include <app-common/zap-generated/attributes/Accessors.h>
2625
#include <cstring>
2726
#include <lib/support/logging/CHIPLogging.h>
2827

2928
LockManager LockManager::sLock;
3029

31-
TimerHandle_t sLockTimer;
32-
3330
using namespace ::chip::DeviceLayer::Internal;
3431
using namespace EFR32DoorLock::LockInitParams;
3532

@@ -77,17 +74,16 @@ CHIP_ERROR LockManager::Init(chip::app::DataModel::Nullable<chip::app::Clusters:
7774
return APP_ERROR_ALLOCATION_FAILED;
7875
}
7976

80-
// Create FreeRTOS sw timer for lock timer.
81-
sLockTimer = xTimerCreate("lockTmr", // Just a text name, not used by the RTOS kernel
82-
1, // == default timer period
83-
false, // no timer reload (==one-shot)
84-
(void *) this, // init timer id = lock obj context
85-
TimerEventHandler // timer callback handler
77+
// Create cmsis os sw timer for lock timer.
78+
mLockTimer = osTimerNew(TimerEventHandler, // timer callback handler
79+
osTimerOnce, // no timer reload (one-shot timer)
80+
(void *) this, // pass the app task obj context
81+
NULL // No osTimerAttr_t to provide.
8682
);
8783

88-
if (sLockTimer == NULL)
84+
if (mLockTimer == NULL)
8985
{
90-
SILABS_LOG("sLockTimer timer create failed");
86+
SILABS_LOG("mLockTimer timer create failed");
9187
return APP_ERROR_CREATE_TIMER_FAILED;
9288
}
9389

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

223219
void LockManager::StartTimer(uint32_t aTimeoutMs)
224220
{
225-
if (xTimerIsTimerActive(sLockTimer))
226-
{
227-
SILABS_LOG("app timer already started!");
228-
CancelTimer();
229-
}
230-
231-
// timer is not active, change its period to required value (== restart).
232-
// FreeRTOS- Block for a maximum of 100 ms if the change period command
233-
// cannot immediately be sent to the timer command queue.
234-
if (xTimerChangePeriod(sLockTimer, pdMS_TO_TICKS(aTimeoutMs), pdMS_TO_TICKS(100)) != pdPASS)
221+
// Starts or restarts the function timer
222+
if (osTimerStart(mLockTimer, pdMS_TO_TICKS(aTimeoutMs)) != osOK)
235223
{
236-
SILABS_LOG("sLockTimer timer start() failed");
224+
SILABS_LOG("mLockTimer timer start() failed");
237225
appError(APP_ERROR_START_TIMER_FAILED);
238226
}
239227
}
240228

241229
void LockManager::CancelTimer(void)
242230
{
243-
if (xTimerStop(sLockTimer, pdMS_TO_TICKS(0)) == pdFAIL)
231+
if (osTimerStop(mLockTimer) == osError)
244232
{
245-
SILABS_LOG("sLockTimer stop() failed");
233+
SILABS_LOG("mLockTimer stop() failed");
246234
appError(APP_ERROR_STOP_TIMER_FAILED);
247235
}
248236
}
249237

250-
void LockManager::TimerEventHandler(TimerHandle_t xTimer)
238+
void LockManager::TimerEventHandler(void * timerCbArg)
251239
{
252-
// Get lock obj context from timer id.
253-
LockManager * lock = static_cast<LockManager *>(pvTimerGetTimerID(xTimer));
240+
// The callback argument is the light obj context assigned at timer creation.
241+
LockManager * lock = static_cast<LockManager *>(timerCbArg);
254242

255243
// The timer event handler will be called in the context of the timer task
256-
// once sLockTimer expires. Post an event to apptask queue with the actual handler
244+
// once mLockTimer expires. Post an event to apptask queue with the actual handler
257245
// so that the event can be handled in the context of the apptask.
258246
AppEvent event;
259247
event.Type = AppEvent::kEventType_Timer;

examples/platform/silabs/BaseApplication.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@
7979
#define APP_TASK_STACK_SIZE (4096)
8080
#endif
8181
#define APP_TASK_PRIORITY 2
82+
#ifndef APP_EVENT_QUEUE_SIZE // Allow apps to define a different app queue size
8283
#define APP_EVENT_QUEUE_SIZE 10
84+
#endif
8385
#define EXAMPLE_VENDOR_ID 0xcafe
8486

8587
#if (defined(ENABLE_WSTK_LEDS) && (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT)))
@@ -298,11 +300,11 @@ CHIP_ERROR BaseApplication::Init()
298300
return err;
299301
}
300302

301-
void BaseApplication::FunctionTimerEventHandler(osTimerId_t xTimer)
303+
void BaseApplication::FunctionTimerEventHandler(void * timerCbArg)
302304
{
303305
AppEvent event;
304306
event.Type = AppEvent::kEventType_Timer;
305-
event.TimerEvent.Context = (void *) xTimer;
307+
event.TimerEvent.Context = timerCbArg;
306308
event.Handler = FunctionEventHandler;
307309
PostEvent(&event);
308310
}
@@ -677,7 +679,7 @@ void BaseApplication::OnTriggerIdentifyEffect(Identify * identify)
677679
}
678680
#endif // MATTER_DM_PLUGIN_IDENTIFY_SERVER
679681

680-
void BaseApplication::LightTimerEventHandler(osTimerId_t xTimer)
682+
void BaseApplication::LightTimerEventHandler(void * timerCbArg)
681683
{
682684
LightEventHandler();
683685
}

examples/platform/silabs/BaseApplication.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ class BaseApplication
182182
* @brief Function Timer finished callback function
183183
* Post an FunctionEventHandler event
184184
*
185-
* @param xTimer timer that finished
185+
* @param timerCbArg argument to the timer callback function assigned at timer creation
186186
*/
187-
static void FunctionTimerEventHandler(osTimerId_t xTimer);
187+
static void FunctionTimerEventHandler(void * timerCbArg);
188188

189189
/**
190190
* @brief Timer Event processing function
@@ -207,9 +207,9 @@ class BaseApplication
207207
* @brief Light Timer finished callback function
208208
* Calls LED processing function
209209
*
210-
* @param xTimer timer that finished
210+
* @param timerCbArg argument to the timer callback function assigned at timer creation
211211
*/
212-
static void LightTimerEventHandler(osTimerId_t xTimer);
212+
static void LightTimerEventHandler(void * timerCbArg);
213213

214214
/**
215215
* @brief Updates device LEDs

0 commit comments

Comments
 (0)