Skip to content

Commit

Permalink
moving to cmsis os and correcting the priority of all tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-silabs committed Aug 29, 2024
1 parent 5bf8850 commit 702136e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 64 deletions.
4 changes: 0 additions & 4 deletions examples/platform/silabs/SiWx917/SiWx917/sl_wifi_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ bool ps_requirement_added = false;

WfxRsi_t wfx_rsi;

/* Declare a variable to hold the data associated with the created event group. */
StaticEventGroup_t rsiDriverEventGroup;

bool hasNotifiedIPV6 = false;
#if (CHIP_DEVICE_CONFIG_ENABLE_IPV4)
bool hasNotifiedIPV4 = false;
Expand Down Expand Up @@ -451,7 +448,6 @@ static sl_status_t wfx_rsi_init(void)
}
#endif // SL_MBEDTLS_USE_TINYCRYPT

wfx_rsi.events = xEventGroupCreateStatic(&rsiDriverEventGroup);
wfx_rsi.dev_state |= WFX_RSI_ST_DEV_READY;
osSemaphoreRelease(sl_rs_ble_init_sem);
return status;
Expand Down
34 changes: 20 additions & 14 deletions examples/platform/silabs/SiWx917/SiWx917/wfx_rsi_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@
#include "wfx_host_events.h"
#include "wfx_rsi.h"

/* wfxRsi Task will use as its stack */
StackType_t wfxRsiTaskStack[WFX_RSI_TASK_SZ] = { 0 };
#include <platform/CHIPDeviceLayer.h>

/* Structure that will hold the TCB of the wfxRsi Task being created. */
StaticTask_t wfxRsiTaskBuffer;
// Thread for the WLAN RSI
constexpr uint32_t kWlanTaskSize = 2048;
static uint8_t wlanStack[kWlanTaskSize];
static osThread_t sWlanTaskControlBlock;
constexpr osThreadAttr_t kWlanTaskAttr = { .name = "wlan_rsi",
.attr_bits = osThreadDetached,
.cb_mem = &sWlanTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = wlanStack,
.stack_size = kWlanTaskSize,
.priority = osPriorityAboveNormal7 };

/*********************************************************************
* @fn sl_status_t wfx_wifi_start(void)
Expand All @@ -45,24 +53,22 @@ StaticTask_t wfxRsiTaskBuffer;
***********************************************************************/
sl_status_t wfx_wifi_start(void)
{
if (wfx_rsi.dev_state & WFX_RSI_ST_STARTED)
{
SILABS_LOG("%s: already started.", __func__);
if (wfx_rsi.dev_state & WFX_RSI_ST_STARTED) {
return SL_STATUS_OK;
}
// VerifyOrReturnValue(wfx_rsi.dev_state & WFX_RSI_ST_STARTED, SL_STATUS_OK);

wfx_rsi.dev_state |= WFX_RSI_ST_STARTED;
SILABS_LOG("%s: starting..", __func__);

/*
* Create the Wifi driver task
*/
wfx_rsi.wlan_task = xTaskCreateStatic(wfx_rsi_task, "wfx_rsi", WFX_RSI_TASK_SZ, NULL, WLAN_DRIVER_TASK_PRIORITY,
wfxRsiTaskStack, &wfxRsiTaskBuffer);
// Creating a Wi-Fi driver thread
wfx_rsi.wlan_thread = osThreadNew(wfx_rsi_task, NULL, &kWlanTaskAttr);

VerifyOrReturnError(wfx_rsi.wlan_thread != NULL, SL_STATUS_FAIL);

if (NULL == wfx_rsi.wlan_task)
{
SILABS_LOG("%s: error: failed to create task.", __func__);
return SL_STATUS_FAIL;
}
return SL_STATUS_OK;
}

Expand Down
7 changes: 6 additions & 1 deletion examples/platform/silabs/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ constexpr osThreadAttr_t kUartTaskAttr = { .name = "UART",
.cb_size = osThreadCbSize,
.stack_mem = uartStack,
.stack_size = kUartTaskSize,
.priority = osPriorityRealtime };
#if SLI_SI91X_MCU_INTERFACE
.priority = osPriorityNormal
#else
.priority = osPriorityRealtime
#endif // SLI_SI91X_MCU_INTERFACE
};

typedef struct
{
Expand Down
16 changes: 7 additions & 9 deletions examples/platform/silabs/wfx_rsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <app/icd/server/ICDServerConfig.h>
#include <event_groups.h>
#include <wfx_host_events.h>
#include <cmsis_os2.h>
#include <sl_cmsis_os2_common.h>

#ifndef RSI_BLE_ENABLE
#define RSI_BLE_ENABLE (1)
Expand All @@ -28,9 +30,8 @@
* Interface to RSI Sapis
*/

#define WFX_RSI_WLAN_TASK_SZ (1024 + 512 + 256) /* Stack for the WLAN task */
#define WFX_RSI_TASK_SZ (1024 + 1024) /* Stack for the WFX/RSI task */
#define WFX_RSI_BUF_SZ (1024 * 10) /* May need tweak */
#define WFX_RSI_WLAN_TASK_SZ (1024 + 512 + 256) /* Stack for the WLAN task */ // TODO: For rs9116
#define WFX_RSI_BUF_SZ (1024 * 10) /* May need tweak */ // TODO: For rs9116
// TODO: Default values are usually in minutes, but this is in ms. Confirm if this is correct
#define WFX_RSI_DHCP_POLL_INTERVAL (250) /* Poll interval in ms for DHCP */
#define WFX_RSI_NUM_TIMERS (2) /* Number of RSI timers to alloc */
Expand Down Expand Up @@ -71,13 +72,10 @@ typedef struct WfxEvent_s

typedef struct wfx_rsi_s
{
// TODO: Change tp WfxEventType_e once the event queue is implemented
EventGroupHandle_t events;
TaskHandle_t drv_task;
TaskHandle_t wlan_task;
TaskHandle_t init_task;
osThreadId_t drv_thread;
osThreadId_t wlan_thread;
#ifdef RSI_BLE_ENABLE
TaskHandle_t ble_task;
osThreadId_t ble_thread;
#endif
uint16_t dev_state;
uint16_t ap_chan; /* The chan our STA is using */
Expand Down
2 changes: 1 addition & 1 deletion src/platform/silabs/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
void CancelBleAdvTimeoutTimer(void);
CHIPoBLEConState * GetConnectionState(uint8_t conId, bool allocate = false);
static void DriveBLEState(intptr_t arg);
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
static void BleAdvTimeoutHandler(void * arg);
uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate);

#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
Expand Down
17 changes: 6 additions & 11 deletions src/platform/silabs/efr32/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace {
#define BLE_CONFIG_MIN_CE_LENGTH (0) // Leave to min value
#define BLE_CONFIG_MAX_CE_LENGTH (0xFFFF) // Leave to max value

TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
osTimerId_t sbleAdvTimeoutTimer; // SW timer

const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
Expand All @@ -122,13 +122,8 @@ CHIP_ERROR BLEManagerImpl::_Init()
memset(mIndConfId, kUnusedIndex, sizeof(mIndConfId));
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;

// Create FreeRTOS sw timer for BLE timeouts and interval change.
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // 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 = ble obj context
BleAdvTimeoutHandler // timer callback handler
);
// SW timer for BLE timeouts and interval change.
sbleAdvTimeoutTimer = osTimerNew(BleAdvTimeoutHandler, osTimerOnce, NULL, NULL);

mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
Expand Down Expand Up @@ -1016,23 +1011,23 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)

void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
{
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
if (osTimerStop(sbleAdvTimeoutTimer) != osOK)
{
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
}
}

void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
{
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
if (osTimerIsRunning(sbleAdvTimeoutTimer))
{
CancelBleAdvTimeoutTimer();
}

// 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(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
if (osTimerStart(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs)) != osOK)
{
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
}
Expand Down
45 changes: 21 additions & 24 deletions src/platform/silabs/rs911x/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ extern "C" {

extern sl_wfx_msg_t event_msg;

StaticTask_t rsiBLETaskStruct;

osSemaphoreId_t sl_ble_event_sem;
osSemaphoreId_t sl_rs_ble_init_sem;

/* wfxRsi Task will use as its stack */
StackType_t wfxBLETaskStack[WFX_RSI_TASK_SZ] = { 0 };
osTimerId_t sbleAdvTimeoutTimer;

constexpr uint32_t kBleTaskSize = 2048;
static uint8_t bleStack[kBleTaskSize];
static osThread_t sBleTaskControlBlock;
constexpr osThreadAttr_t kBleTaskAttr = { .name = "rsi_ble",
.attr_bits = osThreadDetached,
.cb_mem = &sBleTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = bleStack,
.stack_size = kBleTaskSize,
.priority = osPriorityHigh };

using namespace ::chip;
using namespace ::chip::Ble;
Expand Down Expand Up @@ -110,7 +118,7 @@ void sl_ble_init()
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
}

void sl_ble_event_handling_task(void)
void sl_ble_event_handling_task(void * args)
{
int32_t event_id;

Expand Down Expand Up @@ -236,8 +244,6 @@ namespace {
#define BLE_CONFIG_MIN_CE_LENGTH (0) // Leave to min value
#define BLE_CONFIG_MAX_CE_LENGTH (0xFFFF) // Leave to max value

TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.

const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
const uint8_t ShortUUID_CHIPoBLEService[] = { 0xF6, 0xFF };
Expand All @@ -253,13 +259,9 @@ CHIP_ERROR BLEManagerImpl::_Init()
sl_rs_ble_init_sem = osSemaphoreNew(1, 0, NULL);
sl_ble_event_sem = osSemaphoreNew(1, 0, NULL);

wfx_rsi.ble_task = xTaskCreateStatic((TaskFunction_t) sl_ble_event_handling_task, "rsi_ble", WFX_RSI_TASK_SZ, NULL,
BLE_DRIVER_TASK_PRIORITY, wfxBLETaskStack, &rsiBLETaskStruct);
wfx_rsi.ble_thread = osThreadNew(sl_ble_event_handling_task, NULL, &kBleTaskAttr);

if (wfx_rsi.ble_task == NULL)
{
ChipLogError(DeviceLayer, "%s: error: failed to create ble task.", __func__);
}
VerifyOrReturnError(wfx_rsi.ble_thread != nullptr, CHIP_ERROR_INCORRECT_STATE);

// Initialize the CHIP BleLayer.
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
Expand All @@ -269,13 +271,8 @@ CHIP_ERROR BLEManagerImpl::_Init()
memset(mIndConfId, kUnusedIndex, sizeof(mIndConfId));
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;

// Create FreeRTOS sw timer for BLE timeouts and interval change.
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // Just a text name, not used by the RTOS kernel
pdMS_TO_TICKS(BLE_DEFAULT_TIMER_PERIOD_MS), // == default timer period
false, // no timer reload (==one-shot)
(void *) this, // init timer id = ble obj context
BleAdvTimeoutHandler // timer callback handler
);
// SW timer for BLE timeouts and interval change.
sbleAdvTimeoutTimer = osTimerNew(BleAdvTimeoutHandler, osTimerOnce, NULL, NULL);

mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
Expand Down Expand Up @@ -1070,7 +1067,7 @@ uint8_t BLEManagerImpl::GetTimerHandle(uint8_t connectionHandle, bool allocate)
return freeIndex;
}

void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
void BLEManagerImpl::BleAdvTimeoutHandler(void * arg)
{
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
{
Expand All @@ -1081,23 +1078,23 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)

void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
{
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
if (osTimerStop(sbleAdvTimeoutTimer) != osOK)
{
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
}
}

void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
{
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
if (osTimerIsRunning(sbleAdvTimeoutTimer))
{
CancelBleAdvTimeoutTimer();
}

// 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(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
if (osTimerStart(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs)) != osOK)
{
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
}
Expand Down

0 comments on commit 702136e

Please sign in to comment.