diff --git a/freertos/FreeRTOSConfig_examples_common.h b/freertos/FreeRTOSConfig_examples_common.h index 88d4ac94f..af0ec3686 100755 --- a/freertos/FreeRTOSConfig_examples_common.h +++ b/freertos/FreeRTOSConfig_examples_common.h @@ -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 diff --git a/freertos/hello_freertos/CMakeLists.txt b/freertos/hello_freertos/CMakeLists.txt index 1da0ce773..703a83649 100644 --- a/freertos/hello_freertos/CMakeLists.txt +++ b/freertos/hello_freertos/CMakeLists.txt @@ -1,3 +1,5 @@ +set(USE_STATIC_ALLOC ON) + set(TARGET_NAME hello_freertos1) add_executable(${TARGET_NAME} hello_freertos.c @@ -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 @@ -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}) diff --git a/freertos/hello_freertos/hello_freertos.c b/freertos/hello_freertos/hello_freertos.c index 77a3c8490..327663156 100755 --- a/freertos/hello_freertos/hello_freertos.c +++ b/freertos/hello_freertos/hello_freertos.c @@ -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; @@ -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 @@ -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. */