From 9a4341bb8714ac7dc4ae5f7517e3fb579448f909 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 17 Sep 2025 12:27:41 +0200 Subject: [PATCH] refactor(freertos): Moved FreeRTOS Run Time Stats gathering to port.c This commit creates a new port layer API xPortGetRunTimeCounterValue() in port.c files. This helps to remove inclusion of header files such as esp_timer.h and xtensa/hal.h from portmacro.h --- .../riscv/include/freertos/portmacro.h | 17 +++++------------ .../FreeRTOS-Kernel-SMP/portable/riscv/port.c | 18 ++++++++++++++++++ .../xtensa/include/freertos/portmacro.h | 16 +++++----------- .../portable/xtensa/port.c | 19 +++++++++++++++++++ .../riscv/include/freertos/portmacro.h | 18 +++++------------- .../FreeRTOS-Kernel/portable/riscv/port.c | 18 ++++++++++++++++++ .../xtensa/include/freertos/portmacro.h | 19 ++++--------------- .../FreeRTOS-Kernel/portable/xtensa/port.c | 19 +++++++++++++++++++ 8 files changed, 93 insertions(+), 51 deletions(-) diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h index e238498ec8..d66b6df762 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h @@ -254,11 +254,10 @@ extern void vTaskExitCritical( void ); // ------------------- Run Time Stats ---------------------- #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else -#define portGET_RUN_TIME_COUNTER_VALUE() 0 -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- @@ -420,19 +419,13 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th #include #include #include +#include #include "esp_attr.h" #include "esp_newlib.h" #include "esp_heap_caps.h" #include "esp_rom_sys.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ -#include - -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif #ifdef __cplusplus } diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c index 914964fa00..6d61e46c61 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c @@ -31,6 +31,9 @@ #include "port_systick.h" #include "portmacro.h" #include "esp_memory_utils.h" +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER #include "soc/periph_defs.h" #include "soc/system_reg.h" @@ -516,3 +519,18 @@ void vApplicationPassiveIdleHook( void ) esp_vApplicationIdleHook(); //Run IDF style hooks } #endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else + return 0; +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h index d29389c457..59ae3548eb 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -239,11 +239,10 @@ extern void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus ); //Timers are already configured, so nothing to do for configuration of run time stats timer #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else // Uses CCOUNT -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount()) -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- @@ -451,11 +450,6 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th #include #include -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif - #ifdef __cplusplus } #endif diff --git a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c index a9dbaaebe8..6ccd739fae 100644 --- a/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c +++ b/components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c @@ -34,6 +34,10 @@ #include "esp_freertos_hooks.h" #include "esp_intr_alloc.h" #include "esp_memory_utils.h" +#include /* required for xthal_get_ccount() */ +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER #include "soc/periph_defs.h" #include "soc/system_reg.h" @@ -695,3 +699,18 @@ void vApplicationPassiveIdleHook( void ) esp_vApplicationIdleHook(); //Run IDF style hooks } #endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else // Uses CCOUNT + return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount(); +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h index 56939101fe..83017af485 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h @@ -6,7 +6,7 @@ * * SPDX-License-Identifier: MIT * - * SPDX-FileContributor: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2023-2025 Espressif Systems (Shanghai) CO LTD * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -71,15 +71,8 @@ #include "esp_heap_caps.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ #include "esp_newlib.h" - -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ #include -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif - #ifdef __cplusplus extern "C" { #endif @@ -603,11 +596,10 @@ void vPortTCBPreDeleteHook( void *pxTCB ); // ------------------- Run Time Stats ---------------------- #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else -#define portGET_RUN_TIME_COUNTER_VALUE() 0 -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- diff --git a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c index 902e761d7a..e2ca2a5ef8 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c +++ b/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c @@ -56,6 +56,9 @@ #include "portmacro.h" #include "port_systick.h" #include "esp_memory_utils.h" +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #if SOC_CPU_HAS_HWLOOP #include "riscv/csr.h" @@ -874,6 +877,21 @@ void vPortCoprocUsedInISR(void* frame) #endif /* SOC_CPU_COPROC_NUM > 0 */ +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else + return 0; +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */ + /* ---------------------------------------------- Misc Implementations ------------------------------------------------- * * ------------------------------------------------------------------------------------------------------------------ */ diff --git a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h index 53a35e0dfb..2da5d7b714 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h +++ b/components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h @@ -43,7 +43,6 @@ #include #include #include -#include /* required for xthal_get_ccount. [refactor-todo] use cpu_hal instead */ #include /* required for XTOS_SET_INTLEVEL. [refactor-todo] add common intr functions to esp_hw_support */ #include "xt_instr_macros.h" #include "spinlock.h" @@ -57,16 +56,7 @@ #include "esp_rom_sys.h" #include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */ #include "portbenchmark.h" - -/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */ #include -#include -#include - -/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */ -#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#include "esp_timer.h" -#endif #ifdef __cplusplus extern "C" { @@ -510,11 +500,10 @@ extern void _frxt_setup_switch( void ); //Defined in portasm.S #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() -#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time()) -#else // Uses CCOUNT -#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount()) -#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ); +#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue() +#endif // --------------------- TCB Cleanup ----------------------- diff --git a/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c b/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c index 7f75dc241a..36337aa11a 100644 --- a/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c +++ b/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c @@ -48,6 +48,10 @@ #include "task.h" /* Required for TaskHandle_t, tskNO_AFFINITY, and vTaskStartScheduler */ #include "port_systick.h" #include "esp_cpu.h" +#include /* required for xthal_get_ccount() */ +#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +#include "esp_timer.h" +#endif #include "esp_memory_utils.h" _Static_assert(portBYTE_ALIGNMENT == 16, "portBYTE_ALIGNMENT must be set to 16"); @@ -659,3 +663,18 @@ void vPortTCBPreDeleteHook( void *pxTCB ) vPortCleanUpCoprocArea( pxTCB ); #endif /* XCHAL_CP_NUM > 0 */ } + +/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */ + +#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS ) + +configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void ) +{ +#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER + return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time(); +#else // Uses CCOUNT + return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount(); +#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER +} + +#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */