Skip to content

async_context_freertos: Add support for configSUPPORT_STATIC_ALLOCATION #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/rp2_common/pico_async_context/async_context_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,42 @@ bool async_context_freertos_init(async_context_freertos_t *self, async_context_f
self->core.type = &template;
self->core.flags = ASYNC_CONTEXT_FLAG_CALLBACK_FROM_NON_IRQ;
self->core.core_num = get_core_num();
#if configSUPPORT_STATIC_ALLOCATION
self->lock_mutex = xSemaphoreCreateRecursiveMutexStatic(&self->lock_mutex_buf);
self->work_needed_sem = xSemaphoreCreateBinaryStatic(&self->work_needed_sem_buf);
self->timer_handle = xTimerCreateStatic( "async_context_timer", // Just a text name, not used by the kernel.
portMAX_DELAY,
pdFALSE, // The timers will auto-reload themselves when they expire.
self,
timer_handler,
&self->timer_buf);
self->task_handle = xTaskCreateStatic( async_context_task,
"async_context_task",
config->task_stack_size,
self,
config->task_priority,
config->task_stack,
&self->task_buf);
#else
self->lock_mutex = xSemaphoreCreateRecursiveMutex();
self->work_needed_sem = xSemaphoreCreateBinary();
self->timer_handle = xTimerCreate( "async_context_timer", // Just a text name, not used by the kernel.
portMAX_DELAY,
pdFALSE, // The timers will auto-reload themselves when they expire.
self,
timer_handler);
#endif

if (!self->lock_mutex ||
!self->work_needed_sem ||
!self->timer_handle ||
pdPASS != xTaskCreate(async_context_task, "async_context_task", config->task_stack_size, self,
config->task_priority, &self->task_handle)) {
#if configSUPPORT_STATIC_ALLOCATION
!self->task_handle
#else
pdPASS == xTaskCreate(async_context_task, "async_context_task", config->task_stack_size, self,
config->task_priority, &self->task_handle)
#endif
) {
async_context_deinit(&self->core);
return false;
}
Expand Down Expand Up @@ -179,6 +202,9 @@ void async_context_freertos_lock_check(__unused async_context_t *self_base) {
typedef struct sync_func_call{
async_when_pending_worker_t worker;
SemaphoreHandle_t sem;
#if configSUPPORT_STATIC_ALLOCATION
StaticSemaphore_t sem_buf;
#endif
uint32_t (*func)(void *param);
void *param;
uint32_t rc;
Expand All @@ -197,7 +223,11 @@ uint32_t async_context_freertos_execute_sync(async_context_t *self_base, uint32_
call.worker.do_work = handle_sync_func_call;
call.func = func;
call.param = param;
#if configSUPPORT_STATIC_ALLOCATION
call.sem = xSemaphoreCreateBinaryStatic(&call.sem_buf);
#else
call.sem = xSemaphoreCreateBinary();
#endif
async_context_add_when_pending_worker(self_base, &call.worker);
async_context_set_work_pending(self_base, &call.worker);
xSemaphoreTake(call.sem, portMAX_DELAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ typedef struct async_context_freertos_config {
* \brief Stack size for the async_context task
*/
configSTACK_DEPTH_TYPE task_stack_size;
/**
* \brief Pointer to stack memory for the async_context task.
* If this is not provided, then a stack will be allocated from the
* freertos heap.
*/
#if configSUPPORT_STATIC_ALLOCATION
StackType_t *task_stack;
#endif
/**
* \brief the core ID (see \ref portGET_CORE_ID()) to pin the task to.
* This is only relevant in SMP mode.
Expand All @@ -71,6 +79,12 @@ struct async_context_freertos {
SemaphoreHandle_t work_needed_sem;
TimerHandle_t timer_handle;
TaskHandle_t task_handle;
#if configSUPPORT_STATIC_ALLOCATION
StaticSemaphore_t lock_mutex_buf;
StaticSemaphore_t work_needed_sem_buf;
StaticTimer_t timer_buf;
StaticTask_t task_buf;
#endif
uint8_t nesting;
volatile bool task_should_exit;
};
Expand Down
7 changes: 7 additions & 0 deletions src/rp2_common/pico_cyw43_arch/cyw43_arch_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@

static async_context_freertos_t cyw43_async_context_freertos;

#if configSUPPORT_STATIC_ALLOCATION
static StackType_t cyw43_async_context_freertos_task_stack[CYW43_TASK_STACK_SIZE];
#endif

async_context_t *cyw43_arch_init_default_async_context(void) {
async_context_freertos_config_t config = async_context_freertos_default_config();
#ifdef CYW43_TASK_PRIORITY
config.task_priority = CYW43_TASK_PRIORITY;
#endif
#ifdef CYW43_TASK_STACK_SIZE
config.task_stack_size = CYW43_TASK_STACK_SIZE;
#endif
#if configSUPPORT_STATIC_ALLOCATION
config.task_stack = cyw43_async_context_freertos_task_stack;
#endif
if (async_context_freertos_init(&cyw43_async_context_freertos, &config))
return &cyw43_async_context_freertos.core;
Expand Down