Skip to content

Commit aa6726b

Browse files
authored
fix(BLE): Consolidate FreeRTOS CordioM and CordioH in one task that runs at lower priority than timer service (#1548)
1 parent 7c098d8 commit aa6726b

10 files changed

Lines changed: 35 additions & 64 deletions

File tree

Examples/MAX32655/Bluetooth/BLE_FreeRTOS/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void vAssertCalled(const char *const pcFileName, uint32_t ulLine);
7373
vAssertCalled(__FILE__, __LINE__)
7474

7575
#define configUSE_TIMERS 1
76-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
76+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7777
#define configTIMER_QUEUE_LENGTH 8
7878
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7979

Examples/MAX32655/Bluetooth/BLE_fit_FreeRTOS/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#define configUSE_MUTEXES 1
6161

6262
#define configUSE_TIMERS 1
63-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
63+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
6464
#define configTIMER_QUEUE_LENGTH 8
6565
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
6666

Examples/MAX32655/Bluetooth/RF_Test/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ increase the amount of time spent in standby mode, thus reducing average power c
6767
#define configUSE_MUTEXES 1
6868

6969
#define configUSE_TIMERS 1
70-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
70+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7171
#define configTIMER_QUEUE_LENGTH 8
7272
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7373

Examples/MAX32665/Bluetooth/BLE_FreeRTOS/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void vAssertCalled(const char *const pcFileName, uint32_t ulLine);
7373
vAssertCalled(__FILE__, __LINE__)
7474

7575
#define configUSE_TIMERS 1
76-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
76+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7777
#define configTIMER_QUEUE_LENGTH 8
7878
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7979

Examples/MAX32665/Bluetooth/RF_Test/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ increase the amount of time spent in standby mode, thus reducing average power c
6767
#define configUSE_MUTEXES 1
6868

6969
#define configUSE_TIMERS 1
70-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
70+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7171
#define configTIMER_QUEUE_LENGTH 8
7272
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7373

Examples/MAX32665/FreeRTOSDemo/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void vAssertCalled(const char *const pcFileName, uint32_t ulLine);
7272
vAssertCalled(__FILE__, __LINE__)
7373

7474
#define configUSE_TIMERS 1
75-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
75+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7676
#define configTIMER_QUEUE_LENGTH 8
7777
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7878

Examples/MAX32690/Bluetooth/BLE_FreeRTOS/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void vAssertCalled(const char *const pcFileName, uint32_t ulLine);
7373
vAssertCalled(__FILE__, __LINE__)
7474

7575
#define configUSE_TIMERS 1
76-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
76+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7777
#define configTIMER_QUEUE_LENGTH 8
7878
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7979

Examples/MAX32690/Bluetooth/RF_Test/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ increase the amount of time spent in standby mode, thus reducing average power c
6767
#define configUSE_MUTEXES 1
6868

6969
#define configUSE_TIMERS 1
70-
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 3)
70+
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 2)
7171
#define configTIMER_QUEUE_LENGTH 8
7272
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
7373

Libraries/Cordio/wsf/sources/targets/freertos/wsf_os.c

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,14 @@ WSF_CT_ASSERT(sizeof(uint32_t) == 4);
5757
/* maximum number of event handlers per task */
5858
#define WSF_MAX_HANDLERS 16
5959

60-
#define WSF_DISPATCHER_MSG_STACK_SIZE 4096
61-
#define WSF_DISPATCHER_HND_STACK_SIZE 4096
62-
#define WSF_DISPATCHER_MSG_TASK_PRIORITY configMAX_PRIORITIES - 2
63-
#define WSF_DISPATCHER_HND_TASK_PRIORITY configMAX_PRIORITIES - 3
60+
#define WSF_DISPATCHER_STACK_SIZE 4096
61+
#define WSF_DISPATCHER_TASK_PRIORITY configMAX_PRIORITIES - 3
6462

6563
/*! \brief OS serivice function number */
6664
#define WSF_OS_MAX_SERVICE_FUNCTIONS 3
6765

6866
/* Forward declaration */
69-
static void prvWSFMsgTask(void *pvParameters);
70-
static void prvWSFHndTask(void *pvParameters);
67+
static void prvWSFDispatchTask(void *pvParameters);
7168

7269
/**************************************************************************************************
7370
Data Types
@@ -78,8 +75,7 @@ typedef struct {
7875
wsfEventHandler_t handler[WSF_MAX_HANDLERS];
7976
wsfEventMask_t handlerEventMask[WSF_MAX_HANDLERS];
8077
wsfQueue_t msgQueue;
81-
xTaskHandle msgTaskHandle;
82-
xTaskHandle hndTaskHandle;
78+
xTaskHandle taskHandle;
8379
uint8_t numHandler;
8480
} wsfOsTask_t;
8581

@@ -137,9 +133,9 @@ void WsfSetEvent(wsfHandlerId_t handlerId, wsfEventMask_t event)
137133

138134
/* Notify the dispatcher task */
139135
if (xPortIsInsideInterrupt()) {
140-
xTaskNotifyFromISR(wsfOs.task.hndTaskHandle, WSF_HANDLER_EVENT, eSetBits, NULL);
136+
xTaskNotifyFromISR(wsfOs.task.taskHandle, WSF_HANDLER_EVENT, eSetBits, NULL);
141137
} else {
142-
xTaskNotify(wsfOs.task.hndTaskHandle, WSF_HANDLER_EVENT, eSetBits);
138+
xTaskNotify(wsfOs.task.taskHandle, WSF_HANDLER_EVENT, eSetBits);
143139
}
144140
}
145141

@@ -158,9 +154,9 @@ void WsfTaskSetReady(wsfHandlerId_t handlerId, wsfTaskEvent_t event)
158154

159155
/* Notify the dispatcher task */
160156
if (xPortIsInsideInterrupt()) {
161-
xTaskNotifyFromISR(wsfOs.task.msgTaskHandle, event, eSetBits, NULL);
157+
xTaskNotifyFromISR(wsfOs.task.taskHandle, event, eSetBits, NULL);
162158
} else {
163-
xTaskNotify(wsfOs.task.msgTaskHandle, event, eSetBits);
159+
xTaskNotify(wsfOs.task.taskHandle, event, eSetBits);
164160
}
165161
}
166162

@@ -212,39 +208,34 @@ wsfHandlerId_t WsfOsSetNextHandler(wsfEventHandler_t handler)
212208
void WsfOsInit(void)
213209
{
214210
memset(&wsfOs, 0, sizeof(wsfOs));
215-
xTaskCreate(prvWSFMsgTask, /* The function that implements the task. */
216-
"CordioM", /* Text name for the task, just to help debugging. */
217-
WSF_DISPATCHER_MSG_STACK_SIZE, /* The size (in words) of the stack that should be created for the task. */
211+
xTaskCreate(prvWSFDispatchTask, /* The function that implements the task. */
212+
"Cordio", /* Text name for the task, just to help debugging. */
213+
WSF_DISPATCHER_STACK_SIZE, /* The size (in words) of the stack that should be created for the task. */
218214
NULL, /* A parameter that can be passed into the task. Not used. */
219-
WSF_DISPATCHER_MSG_TASK_PRIORITY, /* The priority to assign to the task. tskIDLE_PRIORITY (which is 0) is the lowest priority. configMAX_PRIORITIES - 1 is the highest priority. */
220-
&wsfOs.task.msgTaskHandle); /* Used to obtain a handle to the created task. Not used, so set to NULL. */
221-
WSF_ASSERT(wsfOs.task.msgTaskHandle);
222-
xTaskCreate(prvWSFHndTask, /* The function that implements the task. */
223-
"CordioH", /* Text name for the task, just to help debugging. */
224-
WSF_DISPATCHER_HND_STACK_SIZE, /* The size (in words) of the stack that should be created for the task. */
225-
NULL, /* A parameter that can be passed into the task. Not used. */
226-
WSF_DISPATCHER_HND_TASK_PRIORITY, /* The priority to assign to the task. tskIDLE_PRIORITY (which is 0) is the lowest priority. configMAX_PRIORITIES - 1 is the highest priority. */
227-
&wsfOs.task.hndTaskHandle); /* Used to obtain a handle to the created task. Not used, so set to NULL. */
228-
WSF_ASSERT(wsfOs.task.hndTaskHandle);
215+
WSF_DISPATCHER_TASK_PRIORITY, /* The priority to assign to the task. tskIDLE_PRIORITY (which is 0) is the lowest priority. configMAX_PRIORITIES - 1 is the highest priority. */
216+
&wsfOs.task.taskHandle); /* Used to obtain a handle to the created task. */
217+
WSF_ASSERT(wsfOs.task.taskHandle);
229218
}
230219

231220
/*************************************************************************************************/
232221
/*!
233-
* \brief The message and timer handler task loop
222+
* \brief The dispatcher task loop. Processes messages, timers, and events in the same order
223+
* as the baremetal dispatcher (MSG -> TIMER -> HANDLER), ensuring all protocol state
224+
* machine processing is single-threaded and free from inter-task race conditions.
234225
*
235226
* \param pvParameters The task parameters (optional)
236227
* \note Must never return
237228
*
238229
* \return None.
239230
*/
240231
/*************************************************************************************************/
241-
static void prvWSFMsgTask(void *pvParameters)
232+
static void prvWSFDispatchTask(void *pvParameters)
242233
{
243-
244234
wsfOsTask_t *pTask;
245235
void *pMsg;
246236
wsfTimer_t *pTimer;
247237
wsfHandlerId_t handlerId;
238+
uint8_t i;
248239

249240
pTask = &wsfOs.task;
250241

@@ -270,36 +261,9 @@ static void prvWSFMsgTask(void *pvParameters)
270261
(*pTask->handler[pTimer->handlerId])(0, &pTimer->msg);
271262
}
272263
}
273-
}
274-
}
275-
276-
/*************************************************************************************************/
277-
/*!
278-
* \brief The dispatcher task loop
279-
*
280-
* \param pvParameters The task parameters (optional)
281-
* \note Must never return
282-
*
283-
* \return None.
284-
*/
285-
/*************************************************************************************************/
286-
static void prvWSFHndTask(void *pvParameters)
287-
{
288-
289-
wsfOsTask_t *pTask;
290-
uint8_t i;
291-
292-
pTask = &wsfOs.task;
293-
294-
while (1) {
295-
uint32_t taskEventMask = 0;
296-
297-
/* Wait for a FreeRTOS task notification */
298-
if (!xTaskNotifyWait(0, 0xFFFFFFFF, &taskEventMask, portMAX_DELAY))
299-
continue; /* No notifications, restart waiting */
300264

301265
if (taskEventMask & WSF_HANDLER_EVENT) {
302-
/* service handlers */
266+
/* service event handlers */
303267
for (i = 0; i < WSF_MAX_HANDLERS; i++) {
304268
if ((pTask->handlerEventMask[i] != 0)
305269
&& (pTask->handler[i] != NULL)) {

Libraries/Cordio/wsf/sources/targets/freertos/wsf_timer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
#error Enable timers in FreeRTOSConfig.h by definiing configUSE_TIMERS as 1
4242
#endif
4343

44+
/* assuming WSF_DISPATCHER_TASK_PRIORITY is configMAX_PRIORITIES - 3 =>
45+
* require configTIMER_TASK_PRIORITY higher at configMAX_PRIORITIES - 2
46+
*/
47+
#if (configTIMER_TASK_PRIORITY != (configMAX_PRIORITIES - 2))
48+
#error "Timer task priority must be set to configMAX_PRIORITIES - 2 in FreeRTOSConfig.h"
49+
#endif
50+
4451
typedef struct TimerStruct {
4552
struct TimerStruct *next;
4653
struct TimerStruct *prev;

0 commit comments

Comments
 (0)