Skip to content

Commit 9a4341b

Browse files
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
1 parent 649741f commit 9a4341b

File tree

8 files changed

+93
-51
lines changed

8 files changed

+93
-51
lines changed

components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/include/freertos/portmacro.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,10 @@ extern void vTaskExitCritical( void );
254254
// ------------------- Run Time Stats ----------------------
255255

256256
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
257-
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
258-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time())
259-
#else
260-
#define portGET_RUN_TIME_COUNTER_VALUE() 0
261-
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
257+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
258+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void );
259+
#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue()
260+
#endif
262261

263262
// --------------------- TCB Cleanup -----------------------
264263

@@ -420,19 +419,13 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th
420419
#include <stdlib.h>
421420
#include <stdbool.h>
422421
#include <stdarg.h>
422+
#include <limits.h>
423423
#include "esp_attr.h"
424424
#include "esp_newlib.h"
425425
#include "esp_heap_caps.h"
426426
#include "esp_rom_sys.h"
427427
#include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */
428428

429-
/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */
430-
#include <limits.h>
431-
432-
/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */
433-
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
434-
#include "esp_timer.h"
435-
#endif
436429

437430
#ifdef __cplusplus
438431
}

components/freertos/FreeRTOS-Kernel-SMP/portable/riscv/port.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "port_systick.h"
3232
#include "portmacro.h"
3333
#include "esp_memory_utils.h"
34+
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
35+
#include "esp_timer.h"
36+
#endif
3437
#ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER
3538
#include "soc/periph_defs.h"
3639
#include "soc/system_reg.h"
@@ -516,3 +519,18 @@ void vApplicationPassiveIdleHook( void )
516519
esp_vApplicationIdleHook(); //Run IDF style hooks
517520
}
518521
#endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK
522+
523+
/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */
524+
525+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
526+
527+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void )
528+
{
529+
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
530+
return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time();
531+
#else
532+
return 0;
533+
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
534+
}
535+
536+
#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */

components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/include/freertos/portmacro.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -239,11 +239,10 @@ extern void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus );
239239

240240
//Timers are already configured, so nothing to do for configuration of run time stats timer
241241
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
242-
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
243-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time())
244-
#else // Uses CCOUNT
245-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount())
246-
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
242+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
243+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void );
244+
#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue()
245+
#endif
247246

248247
// --------------------- TCB Cleanup -----------------------
249248

@@ -451,11 +450,6 @@ portmacro.h. Therefore, we need to keep these headers around for now to allow th
451450
#include <xtensa/config/system.h>
452451
#include <xtensa_api.h>
453452

454-
/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */
455-
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
456-
#include "esp_timer.h"
457-
#endif
458-
459453
#ifdef __cplusplus
460454
}
461455
#endif

components/freertos/FreeRTOS-Kernel-SMP/portable/xtensa/port.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include "esp_freertos_hooks.h"
3535
#include "esp_intr_alloc.h"
3636
#include "esp_memory_utils.h"
37+
#include <xtensa/hal.h> /* required for xthal_get_ccount() */
38+
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
39+
#include "esp_timer.h"
40+
#endif
3741
#ifdef CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER
3842
#include "soc/periph_defs.h"
3943
#include "soc/system_reg.h"
@@ -695,3 +699,18 @@ void vApplicationPassiveIdleHook( void )
695699
esp_vApplicationIdleHook(); //Run IDF style hooks
696700
}
697701
#endif // CONFIG_FREERTOS_USE_PASSIVE_IDLE_HOOK
702+
703+
/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */
704+
705+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
706+
707+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void )
708+
{
709+
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
710+
return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time();
711+
#else // Uses CCOUNT
712+
return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount();
713+
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
714+
}
715+
716+
#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */

components/freertos/FreeRTOS-Kernel/portable/riscv/include/freertos/portmacro.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* SPDX-License-Identifier: MIT
88
*
9-
* SPDX-FileContributor: 2023-2024 Espressif Systems (Shanghai) CO LTD
9+
* SPDX-FileContributor: 2023-2025 Espressif Systems (Shanghai) CO LTD
1010
*
1111
* Permission is hereby granted, free of charge, to any person obtaining a copy of
1212
* this software and associated documentation files (the "Software"), to deal in
@@ -71,15 +71,8 @@
7171
#include "esp_heap_caps.h"
7272
#include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */
7373
#include "esp_newlib.h"
74-
75-
/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */
7674
#include <limits.h>
7775

78-
/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */
79-
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
80-
#include "esp_timer.h"
81-
#endif
82-
8376
#ifdef __cplusplus
8477
extern "C" {
8578
#endif
@@ -603,11 +596,10 @@ void vPortTCBPreDeleteHook( void *pxTCB );
603596
// ------------------- Run Time Stats ----------------------
604597

605598
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
606-
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
607-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time())
608-
#else
609-
#define portGET_RUN_TIME_COUNTER_VALUE() 0
610-
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
599+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
600+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void );
601+
#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue()
602+
#endif
611603

612604
// --------------------- TCB Cleanup -----------------------
613605

components/freertos/FreeRTOS-Kernel/portable/riscv/port.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#include "portmacro.h"
5757
#include "port_systick.h"
5858
#include "esp_memory_utils.h"
59+
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
60+
#include "esp_timer.h"
61+
#endif
5962

6063
#if SOC_CPU_HAS_HWLOOP
6164
#include "riscv/csr.h"
@@ -874,6 +877,21 @@ void vPortCoprocUsedInISR(void* frame)
874877

875878
#endif /* SOC_CPU_COPROC_NUM > 0 */
876879

880+
/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */
881+
882+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
883+
884+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void )
885+
{
886+
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
887+
return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time();
888+
#else
889+
return 0;
890+
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
891+
}
892+
893+
#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */
894+
877895
/* ---------------------------------------------- Misc Implementations -------------------------------------------------
878896
*
879897
* ------------------------------------------------------------------------------------------------------------------ */

components/freertos/FreeRTOS-Kernel/portable/xtensa/include/freertos/portmacro.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <stdbool.h>
4444
#include <stdarg.h>
4545
#include <xtensa/config/core.h>
46-
#include <xtensa/hal.h> /* required for xthal_get_ccount. [refactor-todo] use cpu_hal instead */
4746
#include <xtensa/xtruntime.h> /* required for XTOS_SET_INTLEVEL. [refactor-todo] add common intr functions to esp_hw_support */
4847
#include "xt_instr_macros.h"
4948
#include "spinlock.h"
@@ -57,16 +56,7 @@
5756
#include "esp_rom_sys.h"
5857
#include "esp_system.h" /* required by esp_get_...() functions in portable.h. [refactor-todo] Update portable.h */
5958
#include "portbenchmark.h"
60-
61-
/* [refactor-todo] These includes are not directly used in this file. They are kept into to prevent a breaking change. Remove these. */
6259
#include <limits.h>
63-
#include <xtensa/config/system.h>
64-
#include <xtensa_api.h>
65-
66-
/* [refactor-todo] introduce a port wrapper function to avoid including esp_timer.h into the public header */
67-
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
68-
#include "esp_timer.h"
69-
#endif
7060

7161
#ifdef __cplusplus
7262
extern "C" {
@@ -510,11 +500,10 @@ extern void _frxt_setup_switch( void ); //Defined in portasm.S
510500

511501
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
512502

513-
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
514-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) esp_timer_get_time())
515-
#else // Uses CCOUNT
516-
#define portGET_RUN_TIME_COUNTER_VALUE() ((configRUN_TIME_COUNTER_TYPE) xthal_get_ccount())
517-
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
503+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
504+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void );
505+
#define portGET_RUN_TIME_COUNTER_VALUE() xPortGetRunTimeCounterValue()
506+
#endif
518507

519508
// --------------------- TCB Cleanup -----------------------
520509

components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#include "task.h" /* Required for TaskHandle_t, tskNO_AFFINITY, and vTaskStartScheduler */
4949
#include "port_systick.h"
5050
#include "esp_cpu.h"
51+
#include <xtensa/hal.h> /* required for xthal_get_ccount() */
52+
#if CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
53+
#include "esp_timer.h"
54+
#endif
5155
#include "esp_memory_utils.h"
5256

5357
_Static_assert(portBYTE_ALIGNMENT == 16, "portBYTE_ALIGNMENT must be set to 16");
@@ -659,3 +663,18 @@ void vPortTCBPreDeleteHook( void *pxTCB )
659663
vPortCleanUpCoprocArea( pxTCB );
660664
#endif /* XCHAL_CP_NUM > 0 */
661665
}
666+
667+
/* ------------------------------------------------ Run Time Stats ------------------------------------------------- */
668+
669+
#if ( CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS )
670+
671+
configRUN_TIME_COUNTER_TYPE xPortGetRunTimeCounterValue( void )
672+
{
673+
#ifdef CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
674+
return (configRUN_TIME_COUNTER_TYPE) esp_timer_get_time();
675+
#else // Uses CCOUNT
676+
return (configRUN_TIME_COUNTER_TYPE) xthal_get_ccount();
677+
#endif // CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
678+
}
679+
680+
#endif /* CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS */

0 commit comments

Comments
 (0)