Skip to content

Commit

Permalink
[Silabs] Migration to CMSIS OS2 api continuation. (#32874)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jmartinez-silabs authored Apr 8, 2024
1 parent c9dac81 commit 8e12436
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 63 deletions.
12 changes: 6 additions & 6 deletions examples/platform/silabs/BaseApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <platform/CHIPDeviceLayer.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <sl_cmsis_os2_common.h>

#if CHIP_ENABLE_OPENTHREAD
#include <platform/OpenThread/OpenThreadUtils.h>
Expand Down Expand Up @@ -78,7 +79,6 @@
#ifndef APP_TASK_STACK_SIZE
#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
Expand Down Expand Up @@ -123,18 +123,18 @@ bool sHaveBLEConnections = false;
constexpr uint32_t kLightTimerPeriod = static_cast<uint32_t>(pdMS_TO_TICKS(10));

uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
StaticQueue_t sAppEventQueueStruct; // TODO abstract type for static controlblock
osMessageQueue_t sAppEventQueueStruct;
constexpr osMessageQueueAttr_t appEventQueueAttr = { .cb_mem = &sAppEventQueueStruct,
.cb_size = sizeof(sAppEventQueueBuffer),
.cb_size = osMessageQueueCbSize,
.mq_mem = sAppEventQueueBuffer,
.mq_size = sizeof(sAppEventQueueBuffer) };

uint8_t appStack[APP_TASK_STACK_SIZE];
StaticTask_t appTaskStruct; // TODO abstract type for static controlblock
osThread_t appTaskControlBlock;
constexpr osThreadAttr_t appTaskAttr = { .name = APP_TASK_NAME,
.attr_bits = osThreadDetached,
.cb_mem = &appTaskStruct,
.cb_size = sizeof(appTaskStruct),
.cb_mem = &appTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = appStack,
.stack_size = APP_TASK_STACK_SIZE,
.priority = osPriorityNormal };
Expand Down
35 changes: 19 additions & 16 deletions examples/platform/silabs/MatterConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include "BaseApplication.h"
#include "OTAConfig.h"
#include <MatterConfig.h>

#include <FreeRTOS.h>
#include <cmsis_os2.h>

#include <mbedtls/platform.h>

Expand Down Expand Up @@ -80,27 +79,16 @@ static chip::DeviceLayer::Internal::Efr32PsaOperationalKeystore gOperationalKeys

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

#include "FreeRTOSConfig.h"
#include "event_groups.h"
#include "task.h"

/**********************************************************
* Defines
*********************************************************/

#define MAIN_TASK_STACK_SIZE (1024 * 5)
#define MAIN_TASK_PRIORITY (configMAX_PRIORITIES - 1)

using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Credentials::Silabs;
using namespace chip::DeviceLayer::Silabs;

TaskHandle_t main_Task;
volatile int apperror_cnt;
static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;

#if CHIP_ENABLE_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <openthread/cli.h>
Expand Down Expand Up @@ -156,7 +144,20 @@ CHIP_ERROR SilabsMatterConfig::InitOpenThread(void)
#endif // CHIP_ENABLE_OPENTHREAD

namespace {
void application_start(void * unused)

constexpr uint32_t kMainTaskStackSize = (1024 * 5);
// Task is dynamically allocated with max priority. This task gets deleted once the inits are completed.
constexpr osThreadAttr_t kMainTaskAttr = { .name = "main",
.attr_bits = osThreadDetached,
.cb_mem = NULL,
.cb_size = 0U,
.stack_mem = NULL,
.stack_size = kMainTaskStackSize,
.priority = osPriorityRealtime7 };
osThreadId_t sMainTaskHandle;
static chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;

void ApplicationStart(void * unused)
{
CHIP_ERROR err = SilabsMatterConfig::InitMatter(BLE_DEV_NAME);
if (err != CHIP_NO_ERROR)
Expand All @@ -175,16 +176,18 @@ void application_start(void * unused)
if (err != CHIP_NO_ERROR)
appError(err);

vTaskDelete(main_Task);
VerifyOrDie(osThreadTerminate(sMainTaskHandle) == osOK); // Deleting the main task should never fail.
sMainTaskHandle = nullptr;
}
} // namespace

void SilabsMatterConfig::AppInit()
{
GetPlatform().Init();

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

// Should never get here.
Expand Down
2 changes: 1 addition & 1 deletion examples/platform/silabs/SiWx917/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void callback_event(uint32_t event)
break;
case SL_USART_EVENT_RECEIVE_COMPLETE:
#ifdef ENABLE_CHIP_SHELL
chip::NotifyShellProcessFromISR();
chip::NotifyShellProcess();
#endif
case SL_USART_EVENT_TRANSFER_COMPLETE:
break;
Expand Down
4 changes: 2 additions & 2 deletions examples/platform/silabs/efr32/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void uartConsoleInit(void)
void USART_IRQHandler(void)
{
#ifdef ENABLE_CHIP_SHELL
chip::NotifyShellProcessFromISR();
chip::NotifyShellProcess();
#elif !defined(PW_RPC_ENABLED)
otSysEventSignalPending();
#endif
Expand Down Expand Up @@ -333,7 +333,7 @@ static void UART_rx_callback(UARTDRV_Handle_t handle, Ecode_t transferStatus, ui
UARTDRV_Receive(vcom_handle, data, transferCount, UART_rx_callback);

#ifdef ENABLE_CHIP_SHELL
chip::NotifyShellProcessFromISR();
chip::NotifyShellProcess();
#elif !defined(PW_RPC_ENABLED)
otSysEventSignalPending();
#endif
Expand Down
47 changes: 19 additions & 28 deletions examples/platform/silabs/matter_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@

#include "matter_shell.h"
#include <ChipShellCollection.h>
#include <FreeRTOS.h>
#include <cmsis_os2.h>
#include <lib/core/CHIPCore.h>
#include <lib/shell/Engine.h>
#include <task.h>
#include <sl_cmsis_os2_common.h>

using namespace ::chip;
using chip::Shell::Engine;

namespace {

#define SHELL_TASK_STACK_SIZE 2048
#define SHELL_TASK_PRIORITY 5
TaskHandle_t shellTaskHandle;
StackType_t shellStack[SHELL_TASK_STACK_SIZE / sizeof(StackType_t)];
StaticTask_t shellTaskStruct;
constexpr uint32_t kShellProcessFlag = 1;
constexpr uint32_t kShellTaskStackSize = 2048;
uint8_t shellTaskStack[kShellTaskStackSize];
osThread_t shellTaskControlBlock;
constexpr osThreadAttr_t kShellTaskAttr = { .name = "shell",
.attr_bits = osThreadDetached,
.cb_mem = &shellTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = shellTaskStack,
.stack_size = kShellTaskStackSize,
.priority = osPriorityBelowNormal };
osThreadId_t shellTaskHandle;

void MatterShellTask(void * args)
{
Expand All @@ -40,33 +47,17 @@ void MatterShellTask(void * args)

} // namespace

extern "C" unsigned int sleep(unsigned int seconds)
{
const TickType_t xDelay = pdMS_TO_TICKS(1000 * seconds);
vTaskDelay(xDelay);
return 0;
}

namespace chip {

void NotifyShellProcess()
{
xTaskNotifyGive(shellTaskHandle);
}

void NotifyShellProcessFromISR()
{
BaseType_t yieldRequired = pdFALSE;
if (shellTaskHandle != NULL)
{
vTaskNotifyGiveFromISR(shellTaskHandle, &yieldRequired);
}
portYIELD_FROM_ISR(yieldRequired);
// This function may be called from Interrupt Service Routines.
osThreadFlagsSet(shellTaskHandle, kShellProcessFlag);
}

void WaitForShellActivity()
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
osThreadFlagsWait(kShellProcessFlag, osFlagsWaitAny, osWaitForever);
}

void startShellTask()
Expand All @@ -79,8 +70,8 @@ void startShellTask()
cmd_misc_init();
cmd_otcli_init();

shellTaskHandle = xTaskCreateStatic(MatterShellTask, "matter_cli", ArraySize(shellStack), NULL, SHELL_TASK_PRIORITY, shellStack,
&shellTaskStruct);
shellTaskHandle = osThreadNew(MatterShellTask, nullptr, &kShellTaskAttr);
VerifyOrDie(shellTaskHandle);
}

} // namespace chip
1 change: 0 additions & 1 deletion examples/platform/silabs/matter_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace chip {

void NotifyShellProcess();
void NotifyShellProcessFromISR();
void WaitForShellActivity();
void startShellTask();

Expand Down
1 change: 1 addition & 0 deletions examples/thermostat/silabs/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#pragma once
#include <stdint.h>

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);
Expand Down
9 changes: 2 additions & 7 deletions examples/thermostat/silabs/include/TemperatureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@

#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "AppEvent.h"

#include "FreeRTOS.h"
#include "timers.h" // provides FreeRTOS timer support
#include <app-common/zap-generated/attributes/Accessors.h>

#include <lib/core/CHIPError.h>
#include <stdbool.h>
#include <stdint.h>

using namespace chip;

Expand Down
1 change: 0 additions & 1 deletion examples/thermostat/silabs/src/TemperatureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "AppConfig.h"
#include "AppEvent.h"
#include "AppTask.h"
#include "semphr.h"

/**********************************************************
* Defines and Constants
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shell/MainLoopSilabs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void ReadLine(char * buffer, size_t max)

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

0 comments on commit 8e12436

Please sign in to comment.