Skip to content

Support static allocation in Freertos example #651

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
4 changes: 4 additions & 0 deletions freertos/FreeRTOSConfig_examples_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t

/* Memory allocation related definitions. */
#ifndef configSUPPORT_STATIC_ALLOCATION
#define configSUPPORT_STATIC_ALLOCATION 0
#endif
#ifndef configSUPPORT_DYNAMIC_ALLOCATION
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#endif
#define configTOTAL_HEAP_SIZE (128*1024)
#define configAPPLICATION_ALLOCATED_HEAP 0

Expand Down
34 changes: 30 additions & 4 deletions freertos/hello_freertos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set(USE_STATIC_ALLOC ON)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the CI job be modified to build this example both with and without static allocation?


set(TARGET_NAME hello_freertos1)
add_executable(${TARGET_NAME}
hello_freertos.c
Expand All @@ -7,14 +9,26 @@ target_include_directories(${TARGET_NAME} PRIVATE
)
target_link_libraries(${TARGET_NAME} PRIVATE
pico_async_context_freertos
FreeRTOS-Kernel-Heap4
pico_stdlib
)
if(USE_STATIC_ALLOC)
target_compile_definitions(${TARGET_NAME} PRIVATE
configSUPPORT_STATIC_ALLOCATION=1
configSUPPORT_DYNAMIC_ALLOCATION=0
)
target_link_libraries(${TARGET_NAME} PRIVATE
FreeRTOS-Kernel-Static
)
else()
target_link_libraries(${TARGET_NAME} PRIVATE
FreeRTOS-Kernel-Heap4
)
endif()
if(PICO_CYW43_SUPPORTED)
# For led support on pico_w
target_link_libraries(${TARGET_NAME} PRIVATE
pico_cyw43_arch_none
)
)
endif()
target_compile_definitions(${TARGET_NAME} PRIVATE
configNUMBER_OF_CORES=1
Expand All @@ -30,13 +44,25 @@ target_include_directories(${TARGET_NAME} PRIVATE
)
target_link_libraries(${TARGET_NAME} PRIVATE
pico_async_context_freertos
FreeRTOS-Kernel-Heap4
pico_stdlib
)
if(USE_STATIC_ALLOC)
target_compile_definitions(${TARGET_NAME} PRIVATE
configSUPPORT_STATIC_ALLOCATION=1
configSUPPORT_DYNAMIC_ALLOCATION=0
)
target_link_libraries(${TARGET_NAME} PRIVATE
FreeRTOS-Kernel-Static
)
else()
target_link_libraries(${TARGET_NAME} PRIVATE
FreeRTOS-Kernel-Heap4
)
endif()
if(PICO_CYW43_SUPPORTED)
# For led support on pico_w
target_link_libraries(${TARGET_NAME} PRIVATE
pico_cyw43_arch_none
)
)
endif()
pico_add_extra_outputs(${TARGET_NAME})
23 changes: 21 additions & 2 deletions freertos/hello_freertos/hello_freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ static async_context_t *example_async_context(void) {
async_context_freertos_config_t config = async_context_freertos_default_config();
config.task_priority = WORKER_TASK_PRIORITY; // defaults to ASYNC_CONTEXT_DEFAULT_FREERTOS_TASK_PRIORITY
config.task_stack_size = WORKER_TASK_STACK_SIZE; // defaults to ASYNC_CONTEXT_DEFAULT_FREERTOS_TASK_STACK_SIZE
#if configSUPPORT_STATIC_ALLOCATION
static StackType_t async_context_freertos_task_stack[WORKER_TASK_STACK_SIZE];
config.task_stack = async_context_freertos_task_stack;
#endif
if (!async_context_freertos_init(&async_context_instance, &config))
return NULL;
return &async_context_instance.core;
Expand Down Expand Up @@ -127,8 +131,15 @@ void main_task(__unused void *params) {
async_context_add_at_time_worker_in_ms(context, &worker_timeout, 0);
#if USE_LED
// start the led blinking
#if configSUPPORT_STATIC_ALLOCATION
static StackType_t blink_stack[BLINK_TASK_STACK_SIZE];
static StaticTask_t blink_buf;
xTaskCreateStatic(blink_task, "BlinkThread", BLINK_TASK_STACK_SIZE, NULL, BLINK_TASK_PRIORITY, blink_stack, &blink_buf);
#else
static_assert(configSUPPORT_DYNAMIC_ALLOCATION, "");
xTaskCreate(blink_task, "BlinkThread", BLINK_TASK_STACK_SIZE, NULL, BLINK_TASK_PRIORITY, NULL);
#endif
#endif // configSUPPORT_STATIC_ALLOCATION
#endif // USE_LED
int count = 0;
while(true) {
#if configNUMBER_OF_CORES > 1
Expand All @@ -146,11 +157,19 @@ void main_task(__unused void *params) {

void vLaunch( void) {
TaskHandle_t task;
#if configSUPPORT_STATIC_ALLOCATION
static StackType_t main_stack[MAIN_TASK_STACK_SIZE];
static StaticTask_t main_buf;
task = xTaskCreateStatic(main_task, "MainThread", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, main_stack, &main_buf);
#else
static_assert(configSUPPORT_DYNAMIC_ALLOCATION, "");
xTaskCreate(main_task, "MainThread", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &task);

#endif // configSUPPORT_STATIC_ALLOCATION
#if configUSE_CORE_AFFINITY && configNUMBER_OF_CORES > 1
// we must bind the main task to one core (well at least while the init is called)
vTaskCoreAffinitySet(task, 1);
#else
(void)task;
#endif

/* Start the tasks and timer running. */
Expand Down