From 68646e23ae5f80fcaa6523a03d074c01898029f6 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Thu, 24 Nov 2022 22:28:13 +0800 Subject: [PATCH] freertos: Refactor port common functions This commit refactors port_common.c so that it only contains implementation of FreeRTOS port functions that are common to all FreeRTOS ports (i.e., on all architectures and on all FreeRTOS implementations). --- components/freertos/CMakeLists.txt | 3 +- .../FreeRTOS-Kernel-SMP/portable/riscv/port.c | 46 -------------- .../portable/xtensa/port.c | 46 -------------- .../portable => }/port_common.c | 61 +++++++++++++------ 4 files changed, 45 insertions(+), 111 deletions(-) rename components/freertos/{FreeRTOS-Kernel/portable => }/port_common.c (53%) diff --git a/components/freertos/CMakeLists.txt b/components/freertos/CMakeLists.txt index 534b9c2594..911e232755 100644 --- a/components/freertos/CMakeLists.txt +++ b/components/freertos/CMakeLists.txt @@ -50,6 +50,7 @@ if(CONFIG_FREERTOS_SMP) endif() list(APPEND srcs + "port_common.c" "FreeRTOS-Kernel-SMP/croutine.c" "FreeRTOS-Kernel-SMP/event_groups.c" "FreeRTOS-Kernel-SMP/list.c" @@ -113,7 +114,7 @@ else() endif() list(APPEND srcs - "FreeRTOS-Kernel/portable/port_common.c" + "port_common.c" "FreeRTOS-Kernel/portable/port_systick.c" "FreeRTOS-Kernel/croutine.c" "FreeRTOS-Kernel/event_groups.c" diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c index 32b45128ff..683f614b5b 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c @@ -385,52 +385,6 @@ void vPortFreeStack( void *pv ) } #endif -#if ( configSUPPORT_STATIC_ALLOCATION == 1 ) -void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, - StackType_t **ppxIdleTaskStackBuffer, - uint32_t *pulIdleTaskStackSize ) -{ - StackType_t *pxStackBufferTemp; - StaticTask_t *pxTCBBufferTemp; - /* Stack always grows downwards (from high address to low address) on all - * ESP RISC-V targets. Given that the heap allocator likely allocates memory - * from low to high address, we allocate the stack first and then the TCB so - * that the stack does not grow downwards into the TCB. - * - * Allocate TCB and stack buffer in internal memory. */ - pxStackBufferTemp = pvPortMalloc(CONFIG_FREERTOS_IDLE_TASK_STACKSIZE); - pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); - assert(pxStackBufferTemp != NULL); - assert(pxTCBBufferTemp != NULL); - // Write back pointers - *ppxIdleTaskStackBuffer = pxStackBufferTemp; - *ppxIdleTaskTCBBuffer = pxTCBBufferTemp; - *pulIdleTaskStackSize = CONFIG_FREERTOS_IDLE_TASK_STACKSIZE; -} - -void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, - StackType_t **ppxTimerTaskStackBuffer, - uint32_t *pulTimerTaskStackSize ) -{ - StackType_t *pxStackBufferTemp; - StaticTask_t *pxTCBBufferTemp; - /* Stack always grows downwards (from high address to low address) on all - * ESP RISC-V targets. Given that the heap allocator likely allocates memory - * from low to high address, we allocate the stack first and then the TCB so - * that the stack does not grow downwards into the TCB. - * - * Allocate TCB and stack buffer in internal memory. */ - pxStackBufferTemp = pvPortMalloc(configTIMER_TASK_STACK_DEPTH); - pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); - assert(pxStackBufferTemp != NULL); - assert(pxTCBBufferTemp != NULL); - // Write back pointers - *ppxTimerTaskStackBuffer = pxStackBufferTemp; - *ppxTimerTaskTCBBuffer = pxTCBBufferTemp; - *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; -} -#endif //( configSUPPORT_STATIC_ALLOCATION == 1 ) - // ------------------------ Stack -------------------------- #if CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER /** diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c index 67719a620b..2dd1dffc74 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c @@ -405,52 +405,6 @@ void vPortFreeStack( void *pv ) } #endif -#if ( configSUPPORT_STATIC_ALLOCATION == 1 ) -void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, - StackType_t **ppxIdleTaskStackBuffer, - uint32_t *pulIdleTaskStackSize ) -{ - StackType_t *pxStackBufferTemp; - StaticTask_t *pxTCBBufferTemp; - /* Stack always grows downwards (from high address to low address) on all - * ESP Xtensa targets. Given that the heap allocator likely allocates memory - * from low to high address, we allocate the stack first and then the TCB so - * that the stack does not grow downwards into the TCB. - * - * Allocate TCB and stack buffer in internal memory. */ - pxStackBufferTemp = pvPortMalloc(CONFIG_FREERTOS_IDLE_TASK_STACKSIZE); - pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); - assert(pxStackBufferTemp != NULL); - assert(pxTCBBufferTemp != NULL); - // Write back pointers - *ppxIdleTaskStackBuffer = pxStackBufferTemp; - *ppxIdleTaskTCBBuffer = pxTCBBufferTemp; - *pulIdleTaskStackSize = CONFIG_FREERTOS_IDLE_TASK_STACKSIZE; -} - -void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, - StackType_t **ppxTimerTaskStackBuffer, - uint32_t *pulTimerTaskStackSize ) -{ - StaticTask_t *pxTCBBufferTemp; - StackType_t *pxStackBufferTemp; - /* Stack always grows downwards (from high address to low address) on all - * ESP Xtensa targets. Given that the heap allocator likely allocates memory - * from low to high address, we allocate the stack first and then the TCB so - * that the stack does not grow downwards into the TCB. - * - * Allocate TCB and stack buffer in internal memory. */ - pxStackBufferTemp = pvPortMalloc(configTIMER_TASK_STACK_DEPTH); - pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); - assert(pxStackBufferTemp != NULL); - assert(pxTCBBufferTemp != NULL); - // Write back pointers - *ppxTimerTaskStackBuffer = pxStackBufferTemp; - *ppxTimerTaskTCBBuffer = pxTCBBufferTemp; - *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; -} -#endif //( configSUPPORT_STATIC_ALLOCATION == 1 ) - // ------------------------ Stack -------------------------- // User exception dispatcher when exiting diff --git a/components/freertos/FreeRTOS-Kernel/portable/port_common.c b/components/freertos/port_common.c similarity index 53% rename from components/freertos/FreeRTOS-Kernel/portable/port_common.c rename to components/freertos/port_common.c index 093bd73263..0580203e7e 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/port_common.c +++ b/components/freertos/port_common.c @@ -11,8 +11,14 @@ #include "esp_memory_utils.h" #include "sdkconfig.h" +/* ----------------------------------------- Port Implementations (Common) -------------------------------------------- + * - Common FreeRTOS port function implementations + * - These functions are common to all FreeRTOS ports (i.e., on all architectures and all FreeRTOS implementations). + * ------------------------------------------------------------------------------------------------------------------ */ + // -------------------- Heap Related ----------------------- +#if !CONFIG_FREERTOS_SMP // IDF-3997 bool xPortCheckValidTCBMem(const void *ptr) { return esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr); @@ -26,16 +32,18 @@ bool xPortcheckValidStackMem(const void *ptr) return esp_ptr_internal(ptr) && esp_ptr_byte_accessible(ptr); #endif } +#endif // ------------- FreeRTOS Static Allocation ---------------- /* -This function is required by FreeRTOS when configSUPPORT_STATIC_ALLOCATION is -enabled and is used by FreeRTOS to obtain memory for its IDLE tasks. +These function are required by FreeRTOS when configSUPPORT_STATIC_ALLOCATION is +enabled and is used by FreeRTOS to obtain memory for its IDLE/Timer tasks. Like the pvPortMallocTcbMem() and pvPortMallocStackMem() macros, TCB and stack memory MUST be placed in internal RAM. */ +#if ( configSUPPORT_STATIC_ALLOCATION == 1 ) void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) @@ -49,14 +57,24 @@ void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, #if (portSTACK_GROWTH > 0) { //Allocate TCB and stack buffer in internal memory - pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); - pxStackBufferTemp = pvPortMallocStackMem(configIDLE_TASK_STACK_SIZE); + #if CONFIG_FREERTOS_SMP // IDF-3997 + pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); + pxStackBufferTemp = pvPortMalloc(configMINIMAL_STACK_SIZE); + #else + pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + pxStackBufferTemp = pvPortMallocStackMem(configIDLE_TASK_STACK_SIZE); + #endif /* CONFIG_FREERTOS_SMP */ } #else /* portSTACK_GROWTH */ { //Allocate TCB and stack buffer in internal memory - pxStackBufferTemp = pvPortMallocStackMem(configIDLE_TASK_STACK_SIZE); - pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + #if CONFIG_FREERTOS_SMP // IDF-3997 + pxStackBufferTemp = pvPortMalloc(configMINIMAL_STACK_SIZE); + pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); + #else + pxStackBufferTemp = pvPortMallocStackMem(configIDLE_TASK_STACK_SIZE); + pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + #endif /* CONFIG_FREERTOS_SMP */ } #endif /* portSTACK_GROWTH */ @@ -65,17 +83,13 @@ void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, //Write back pointers *ppxIdleTaskTCBBuffer = pxTCBBufferTemp; *ppxIdleTaskStackBuffer = pxStackBufferTemp; +#if CONFIG_FREERTOS_SMP + *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; +#else *pulIdleTaskStackSize = configIDLE_TASK_STACK_SIZE; +#endif /* CONFIG_FREERTOS_SMP */ } -/* -This function is required by FreeRTOS when configSUPPORT_STATIC_ALLOCATION is -enabled and is used by the FreeRTOS Timer to obtain memory for its daemone task. - - -Like the pvPortMallocTcbMem() and pvPortMallocStackMem() macros, TCB and stack -memory MUST be placed in internal RAM. -*/ void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize ) @@ -89,14 +103,24 @@ void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, #if (portSTACK_GROWTH > 0) { //Allocate TCB and stack buffer in internal memory - pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); - pxStackBufferTemp = pvPortMallocStackMem(configTIMER_TASK_STACK_DEPTH); + #if CONFIG_FREERTOS_SMP // IDF-3997 + pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); + pxStackBufferTemp = pvPortMalloc(configTIMER_TASK_STACK_DEPTH); + #else + pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + pxStackBufferTemp = pvPortMallocStackMem(configTIMER_TASK_STACK_DEPTH); + #endif /* CONFIG_FREERTOS_SMP */ } #else /* portSTACK_GROWTH */ { //Allocate TCB and stack buffer in internal memory - pxStackBufferTemp = pvPortMallocStackMem(configTIMER_TASK_STACK_DEPTH); - pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + #if CONFIG_FREERTOS_SMP // IDF-3997 + pxStackBufferTemp = pvPortMalloc(configTIMER_TASK_STACK_DEPTH); + pxTCBBufferTemp = pvPortMalloc(sizeof(StaticTask_t)); + #else + pxStackBufferTemp = pvPortMallocStackMem(configTIMER_TASK_STACK_DEPTH); + pxTCBBufferTemp = pvPortMallocTcbMem(sizeof(StaticTask_t)); + #endif /* CONFIG_FREERTOS_SMP */ } #endif /* portSTACK_GROWTH */ @@ -107,3 +131,4 @@ void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, *ppxTimerTaskStackBuffer = pxStackBufferTemp; *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH; } +#endif // configSUPPORT_STATIC_ALLOCATION == 1