Skip to content

Commit 6207c40

Browse files
committed
Add static allocation Freertos example
1 parent f9ef15b commit 6207c40

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

freertos/FreeRTOSConfig_examples_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@
7070
#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
7171

7272
/* Memory allocation related definitions. */
73+
#ifndef configSUPPORT_STATIC_ALLOCATION
7374
#define configSUPPORT_STATIC_ALLOCATION 0
75+
#endif
76+
#ifndef configSUPPORT_DYNAMIC_ALLOCATION
7477
#define configSUPPORT_DYNAMIC_ALLOCATION 1
78+
#endif
7579
#define configTOTAL_HEAP_SIZE (128*1024)
7680
#define configAPPLICATION_ALLOCATED_HEAP 0
7781

freertos/hello_freertos/CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Example running FreeRTOS on 1 core
12
set(TARGET_NAME hello_freertos1)
23
add_executable(${TARGET_NAME}
34
hello_freertos.c
@@ -14,13 +15,14 @@ if(PICO_CYW43_SUPPORTED)
1415
# For led support on pico_w
1516
target_link_libraries(${TARGET_NAME} PRIVATE
1617
pico_cyw43_arch_none
17-
)
18+
)
1819
endif()
1920
target_compile_definitions(${TARGET_NAME} PRIVATE
2021
configNUMBER_OF_CORES=1
2122
)
2223
pico_add_extra_outputs(${TARGET_NAME})
2324

25+
# Example running FreeRTOS on 2 cores
2426
set(TARGET_NAME hello_freertos2)
2527
add_executable(${TARGET_NAME}
2628
hello_freertos.c
@@ -37,6 +39,31 @@ if(PICO_CYW43_SUPPORTED)
3739
# For led support on pico_w
3840
target_link_libraries(${TARGET_NAME} PRIVATE
3941
pico_cyw43_arch_none
42+
)
43+
endif()
44+
pico_add_extra_outputs(${TARGET_NAME})
45+
46+
# Example running FreeRTOS on 2 cores with static RAM allocation
47+
set(TARGET_NAME hello_freertos_static)
48+
add_executable(${TARGET_NAME}
49+
hello_freertos.c
4050
)
51+
target_include_directories(${TARGET_NAME} PRIVATE
52+
${CMAKE_CURRENT_LIST_DIR}/..
53+
)
54+
target_link_libraries(${TARGET_NAME} PRIVATE
55+
pico_async_context_freertos
56+
FreeRTOS-Kernel-Static
57+
pico_stdlib
58+
)
59+
target_compile_definitions(${TARGET_NAME} PRIVATE
60+
configSUPPORT_STATIC_ALLOCATION=1
61+
configSUPPORT_DYNAMIC_ALLOCATION=0
62+
)
63+
if(PICO_CYW43_SUPPORTED)
64+
# For led support on pico_w
65+
target_link_libraries(${TARGET_NAME} PRIVATE
66+
pico_cyw43_arch_none
67+
)
4168
endif()
4269
pico_add_extra_outputs(${TARGET_NAME})

freertos/hello_freertos/hello_freertos.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ static async_context_t *example_async_context(void) {
5252
async_context_freertos_config_t config = async_context_freertos_default_config();
5353
config.task_priority = WORKER_TASK_PRIORITY; // defaults to ASYNC_CONTEXT_DEFAULT_FREERTOS_TASK_PRIORITY
5454
config.task_stack_size = WORKER_TASK_STACK_SIZE; // defaults to ASYNC_CONTEXT_DEFAULT_FREERTOS_TASK_STACK_SIZE
55+
#if configSUPPORT_STATIC_ALLOCATION
56+
static StackType_t async_context_freertos_task_stack[WORKER_TASK_STACK_SIZE];
57+
config.task_stack = async_context_freertos_task_stack;
58+
#endif
5559
if (!async_context_freertos_init(&async_context_instance, &config))
5660
return NULL;
5761
return &async_context_instance.core;
@@ -127,8 +131,15 @@ void main_task(__unused void *params) {
127131
async_context_add_at_time_worker_in_ms(context, &worker_timeout, 0);
128132
#if USE_LED
129133
// start the led blinking
134+
#if configSUPPORT_STATIC_ALLOCATION
135+
static StackType_t blink_stack[BLINK_TASK_STACK_SIZE];
136+
static StaticTask_t blink_buf;
137+
xTaskCreateStatic(blink_task, "BlinkThread", BLINK_TASK_STACK_SIZE, NULL, BLINK_TASK_PRIORITY, blink_stack, &blink_buf);
138+
#else
139+
static_assert(configSUPPORT_DYNAMIC_ALLOCATION, "");
130140
xTaskCreate(blink_task, "BlinkThread", BLINK_TASK_STACK_SIZE, NULL, BLINK_TASK_PRIORITY, NULL);
131-
#endif
141+
#endif // configSUPPORT_STATIC_ALLOCATION
142+
#endif // USE_LED
132143
int count = 0;
133144
while(true) {
134145
#if configNUMBER_OF_CORES > 1
@@ -146,11 +157,19 @@ void main_task(__unused void *params) {
146157

147158
void vLaunch( void) {
148159
TaskHandle_t task;
160+
#if configSUPPORT_STATIC_ALLOCATION
161+
static StackType_t main_stack[MAIN_TASK_STACK_SIZE];
162+
static StaticTask_t main_buf;
163+
task = xTaskCreateStatic(main_task, "MainThread", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, main_stack, &main_buf);
164+
#else
165+
static_assert(configSUPPORT_DYNAMIC_ALLOCATION, "");
149166
xTaskCreate(main_task, "MainThread", MAIN_TASK_STACK_SIZE, NULL, MAIN_TASK_PRIORITY, &task);
150-
167+
#endif // configSUPPORT_STATIC_ALLOCATION
151168
#if configUSE_CORE_AFFINITY && configNUMBER_OF_CORES > 1
152169
// we must bind the main task to one core (well at least while the init is called)
153170
vTaskCoreAffinitySet(task, 1);
171+
#else
172+
(void)task;
154173
#endif
155174

156175
/* Start the tasks and timer running. */

0 commit comments

Comments
 (0)