Skip to content

Commit 8e12436

Browse files
[Silabs] Migration to CMSIS OS2 api continuation. (project-chip#32874)
* Migrate MatterConfig/MainTask creation to cmsisos api * Migrate silabs matter shell to cmsisos * use sl_cmsis_os2_common in BaseApplication * move local variable to a namespace
1 parent c9dac81 commit 8e12436

File tree

10 files changed

+51
-63
lines changed

10 files changed

+51
-63
lines changed

examples/platform/silabs/BaseApplication.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <platform/CHIPDeviceLayer.h>
4646
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
4747
#include <setup_payload/SetupPayload.h>
48+
#include <sl_cmsis_os2_common.h>
4849

4950
#if CHIP_ENABLE_OPENTHREAD
5051
#include <platform/OpenThread/OpenThreadUtils.h>
@@ -78,7 +79,6 @@
7879
#ifndef APP_TASK_STACK_SIZE
7980
#define APP_TASK_STACK_SIZE (4096)
8081
#endif
81-
#define APP_TASK_PRIORITY 2
8282
#ifndef APP_EVENT_QUEUE_SIZE // Allow apps to define a different app queue size
8383
#define APP_EVENT_QUEUE_SIZE 10
8484
#endif
@@ -123,18 +123,18 @@ bool sHaveBLEConnections = false;
123123
constexpr uint32_t kLightTimerPeriod = static_cast<uint32_t>(pdMS_TO_TICKS(10));
124124

125125
uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
126-
StaticQueue_t sAppEventQueueStruct; // TODO abstract type for static controlblock
126+
osMessageQueue_t sAppEventQueueStruct;
127127
constexpr osMessageQueueAttr_t appEventQueueAttr = { .cb_mem = &sAppEventQueueStruct,
128-
.cb_size = sizeof(sAppEventQueueBuffer),
128+
.cb_size = osMessageQueueCbSize,
129129
.mq_mem = sAppEventQueueBuffer,
130130
.mq_size = sizeof(sAppEventQueueBuffer) };
131131

132132
uint8_t appStack[APP_TASK_STACK_SIZE];
133-
StaticTask_t appTaskStruct; // TODO abstract type for static controlblock
133+
osThread_t appTaskControlBlock;
134134
constexpr osThreadAttr_t appTaskAttr = { .name = APP_TASK_NAME,
135135
.attr_bits = osThreadDetached,
136-
.cb_mem = &appTaskStruct,
137-
.cb_size = sizeof(appTaskStruct),
136+
.cb_mem = &appTaskControlBlock,
137+
.cb_size = osThreadCbSize,
138138
.stack_mem = appStack,
139139
.stack_size = APP_TASK_STACK_SIZE,
140140
.priority = osPriorityNormal };

examples/platform/silabs/MatterConfig.cpp

+19-16
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#include "BaseApplication.h"
2222
#include "OTAConfig.h"
2323
#include <MatterConfig.h>
24-
25-
#include <FreeRTOS.h>
24+
#include <cmsis_os2.h>
2625

2726
#include <mbedtls/platform.h>
2827

@@ -80,27 +79,16 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys
8079

8180
#include <platform/silabs/platformAbstraction/SilabsPlatform.h>
8281

83-
#include "FreeRTOSConfig.h"
84-
#include "event_groups.h"
85-
#include "task.h"
86-
8782
/**********************************************************
8883
* Defines
8984
*********************************************************/
9085

91-
#define MAIN_TASK_STACK_SIZE (1024 * 5)
92-
#define MAIN_TASK_PRIORITY (configMAX_PRIORITIES - 1)
93-
9486
using namespace ::chip;
9587
using namespace ::chip::Inet;
9688
using namespace ::chip::DeviceLayer;
9789
using namespace ::chip::Credentials::Silabs;
9890
using namespace chip::DeviceLayer::Silabs;
9991

100-
TaskHandle_t main_Task;
101-
volatile int apperror_cnt;
102-
static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
103-
10492
#if CHIP_ENABLE_OPENTHREAD
10593
#include <inet/EndPointStateOpenThread.h>
10694
#include <openthread/cli.h>
@@ -156,7 +144,20 @@ CHIP_ERROR SilabsMatterConfig::InitOpenThread(void)
156144
#endif // CHIP_ENABLE_OPENTHREAD
157145

158146
namespace {
159-
void application_start(void * unused)
147+
148+
constexpr uint32_t kMainTaskStackSize = (1024 * 5);
149+
// Task is dynamically allocated with max priority. This task gets deleted once the inits are completed.
150+
constexpr osThreadAttr_t kMainTaskAttr = { .name = "main",
151+
.attr_bits = osThreadDetached,
152+
.cb_mem = NULL,
153+
.cb_size = 0U,
154+
.stack_mem = NULL,
155+
.stack_size = kMainTaskStackSize,
156+
.priority = osPriorityRealtime7 };
157+
osThreadId_t sMainTaskHandle;
158+
static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
159+
160+
void ApplicationStart(void * unused)
160161
{
161162
CHIP_ERROR err = SilabsMatterConfig::InitMatter(BLE_DEV_NAME);
162163
if (err != CHIP_NO_ERROR)
@@ -175,16 +176,18 @@ void application_start(void * unused)
175176
if (err != CHIP_NO_ERROR)
176177
appError(err);
177178

178-
vTaskDelete(main_Task);
179+
VerifyOrDie(osThreadTerminate(sMainTaskHandle) == osOK); // Deleting the main task should never fail.
180+
sMainTaskHandle = nullptr;
179181
}
180182
} // namespace
181183

182184
void SilabsMatterConfig::AppInit()
183185
{
184186
GetPlatform().Init();
185187

186-
xTaskCreate(application_start, "main_task", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &main_Task);
188+
sMainTaskHandle = osThreadNew(ApplicationStart, nullptr, &kMainTaskAttr);
187189
SILABS_LOG("Starting scheduler");
190+
VerifyOrDie(sMainTaskHandle); // We can't proceed if the Main Task creation failed.
188191
GetPlatform().StartScheduler();
189192

190193
// Should never get here.

examples/platform/silabs/SiWx917/uart.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void callback_event(uint32_t event)
4949
break;
5050
case SL_USART_EVENT_RECEIVE_COMPLETE:
5151
#ifdef ENABLE_CHIP_SHELL
52-
chip::NotifyShellProcessFromISR();
52+
chip::NotifyShellProcess();
5353
#endif
5454
case SL_USART_EVENT_TRANSFER_COMPLETE:
5555
break;

examples/platform/silabs/efr32/uart.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void uartConsoleInit(void)
291291
void USART_IRQHandler(void)
292292
{
293293
#ifdef ENABLE_CHIP_SHELL
294-
chip::NotifyShellProcessFromISR();
294+
chip::NotifyShellProcess();
295295
#elif !defined(PW_RPC_ENABLED)
296296
otSysEventSignalPending();
297297
#endif
@@ -333,7 +333,7 @@ static void UART_rx_callback(UARTDRV_Handle_t handle, Ecode_t transferStatus, ui
333333
UARTDRV_Receive(vcom_handle, data, transferCount, UART_rx_callback);
334334

335335
#ifdef ENABLE_CHIP_SHELL
336-
chip::NotifyShellProcessFromISR();
336+
chip::NotifyShellProcess();
337337
#elif !defined(PW_RPC_ENABLED)
338338
otSysEventSignalPending();
339339
#endif

examples/platform/silabs/matter_shell.cpp

+19-28
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,28 @@
1717

1818
#include "matter_shell.h"
1919
#include <ChipShellCollection.h>
20-
#include <FreeRTOS.h>
20+
#include <cmsis_os2.h>
2121
#include <lib/core/CHIPCore.h>
2222
#include <lib/shell/Engine.h>
23-
#include <task.h>
23+
#include <sl_cmsis_os2_common.h>
2424

2525
using namespace ::chip;
2626
using chip::Shell::Engine;
2727

2828
namespace {
2929

30-
#define SHELL_TASK_STACK_SIZE 2048
31-
#define SHELL_TASK_PRIORITY 5
32-
TaskHandle_t shellTaskHandle;
33-
StackType_t shellStack[SHELL_TASK_STACK_SIZE / sizeof(StackType_t)];
34-
StaticTask_t shellTaskStruct;
30+
constexpr uint32_t kShellProcessFlag = 1;
31+
constexpr uint32_t kShellTaskStackSize = 2048;
32+
uint8_t shellTaskStack[kShellTaskStackSize];
33+
osThread_t shellTaskControlBlock;
34+
constexpr osThreadAttr_t kShellTaskAttr = { .name = "shell",
35+
.attr_bits = osThreadDetached,
36+
.cb_mem = &shellTaskControlBlock,
37+
.cb_size = osThreadCbSize,
38+
.stack_mem = shellTaskStack,
39+
.stack_size = kShellTaskStackSize,
40+
.priority = osPriorityBelowNormal };
41+
osThreadId_t shellTaskHandle;
3542

3643
void MatterShellTask(void * args)
3744
{
@@ -40,33 +47,17 @@ void MatterShellTask(void * args)
4047

4148
} // namespace
4249

43-
extern "C" unsigned int sleep(unsigned int seconds)
44-
{
45-
const TickType_t xDelay = pdMS_TO_TICKS(1000 * seconds);
46-
vTaskDelay(xDelay);
47-
return 0;
48-
}
49-
5050
namespace chip {
5151

5252
void NotifyShellProcess()
5353
{
54-
xTaskNotifyGive(shellTaskHandle);
55-
}
56-
57-
void NotifyShellProcessFromISR()
58-
{
59-
BaseType_t yieldRequired = pdFALSE;
60-
if (shellTaskHandle != NULL)
61-
{
62-
vTaskNotifyGiveFromISR(shellTaskHandle, &yieldRequired);
63-
}
64-
portYIELD_FROM_ISR(yieldRequired);
54+
// This function may be called from Interrupt Service Routines.
55+
osThreadFlagsSet(shellTaskHandle, kShellProcessFlag);
6556
}
6657

6758
void WaitForShellActivity()
6859
{
69-
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
60+
osThreadFlagsWait(kShellProcessFlag, osFlagsWaitAny, osWaitForever);
7061
}
7162

7263
void startShellTask()
@@ -79,8 +70,8 @@ void startShellTask()
7970
cmd_misc_init();
8071
cmd_otcli_init();
8172

82-
shellTaskHandle = xTaskCreateStatic(MatterShellTask, "matter_cli", ArraySize(shellStack), NULL, SHELL_TASK_PRIORITY, shellStack,
83-
&shellTaskStruct);
73+
shellTaskHandle = osThreadNew(MatterShellTask, nullptr, &kShellTaskAttr);
74+
VerifyOrDie(shellTaskHandle);
8475
}
8576

8677
} // namespace chip

examples/platform/silabs/matter_shell.h

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
namespace chip {
2121

2222
void NotifyShellProcess();
23-
void NotifyShellProcessFromISR();
2423
void WaitForShellActivity();
2524
void startShellTask();
2625

examples/thermostat/silabs/include/AppEvent.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#pragma once
21+
#include <stdint.h>
2122

2223
struct AppEvent;
2324
typedef void (*EventHandler)(AppEvent *);

examples/thermostat/silabs/include/TemperatureManager.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818

1919
#pragma once
2020

21-
#include <stdbool.h>
22-
#include <stdint.h>
23-
2421
#include "AppEvent.h"
25-
26-
#include "FreeRTOS.h"
27-
#include "timers.h" // provides FreeRTOS timer support
2822
#include <app-common/zap-generated/attributes/Accessors.h>
29-
3023
#include <lib/core/CHIPError.h>
24+
#include <stdbool.h>
25+
#include <stdint.h>
3126

3227
using namespace chip;
3328

examples/thermostat/silabs/src/TemperatureManager.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "AppConfig.h"
2626
#include "AppEvent.h"
2727
#include "AppTask.h"
28-
#include "semphr.h"
2928

3029
/**********************************************************
3130
* Defines and Constants

src/lib/shell/MainLoopSilabs.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void ReadLine(char * buffer, size_t max)
5555

5656
#ifdef BRD4325A
5757
// for 917 SoC board, we need to create a rx event before we wait for the shell activity
58-
// NotifyShellProcessFromISR() is called once the buffer is filled
58+
// NotifyShellProcess() is called once the buffer is filled
5959
while (streamer_read(streamer_get(), buffer + read, 1) == 1)
6060
{
6161
// Count how many characters were read; usually one but could be copy/paste

0 commit comments

Comments
 (0)