From cb31fc4009755ba095e85804cecafc7a70e0c667 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Mon, 29 May 2023 20:01:35 +0300 Subject: [PATCH 1/3] clock: [#55] Overflow handling for FreeRTOS stats. --- source/shoh/freertos/FreeRTOSConfig.h | 3 ++- source/shoh/src/peripherals/Clock.cpp | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/source/shoh/freertos/FreeRTOSConfig.h b/source/shoh/freertos/FreeRTOSConfig.h index da1e190..34d21e9 100644 --- a/source/shoh/freertos/FreeRTOSConfig.h +++ b/source/shoh/freertos/FreeRTOSConfig.h @@ -126,9 +126,10 @@ to exclude the API function. */ * must set up LPC_SCT1. */ void vConfigureTimerForRunTimeStats(void); +unsigned long ulGetTimeForRunTimeStats(void); #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats() /* The value is read directly from the counter register for efficiency and low overhead. */ -#define portGET_RUN_TIME_COUNTER_VALUE() LPC_SCT1->COUNT_U +#define portGET_RUN_TIME_COUNTER_VALUE() ulGetTimeForRunTimeStats() /* Cortex-M specific definitions. */ #ifdef __NVIC_PRIO_BITS diff --git a/source/shoh/src/peripherals/Clock.cpp b/source/shoh/src/peripherals/Clock.cpp index 298d495..feb5b88 100644 --- a/source/shoh/src/peripherals/Clock.cpp +++ b/source/shoh/src/peripherals/Clock.cpp @@ -29,7 +29,7 @@ extern "C" { /** * @brief This function is used by FreeRTOS to configure the collection of - * time used by tasks. + * time used by tasks. (This timer is also used for logging timestamps.) */ void vConfigureTimerForRunTimeStats (void) @@ -55,6 +55,20 @@ extern "C" { LPC_SCT1->CONFIG = SCT_CONFIG_32BIT_COUNTER; LPC_SCT1->CTRL_U = SCT_CTRL_CLRCTR_L; } + + /** + * @brief Used by FreeRTOS to collect runtime statistics. + * @paragraph FreeRTOS is unable to handle the counter overflow, so it must be done from our side. + * This solution will break eventually later on, presumably after 1193 hours of runtime, + * since unsigned long won't be able to hold more than that amount of milliseconds. + * FreeRTOS expects to get unsigned long, so there's nothing more that can be done. + * @return unsigned long + */ + unsigned long ulGetTimeForRunTimeStats(void) + { + return (unsigned long)(((double)(counter_overflows - 1) * max_counter_value) + LPC_SCT1->COUNT_U) + / ((double)Chip_Clock_GetMainClockRate() / 1000); + } } /** From d76151ef15e0ca7a1e367541a8ec71899b341ff9 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 30 May 2023 10:24:57 +0300 Subject: [PATCH 2/3] FreeRTOSConfig: [#55] Set runtime counter type to 64 bit. --- source/shoh/freertos/FreeRTOSConfig.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/shoh/freertos/FreeRTOSConfig.h b/source/shoh/freertos/FreeRTOSConfig.h index 34d21e9..8742ebf 100644 --- a/source/shoh/freertos/FreeRTOSConfig.h +++ b/source/shoh/freertos/FreeRTOSConfig.h @@ -93,7 +93,8 @@ #define configUSE_COUNTING_SEMAPHORES 1 #define configGENERATE_RUN_TIME_STATS 1 #define configRECORD_STACK_HIGH_ADDRESS 1 -#define configUSE_TICKLESS_IDLE 1 +#define configUSE_TICKLESS_IDLE 1 +#define configRUN_TIME_COUNTER_TYPE uint64_t /* Co-routine definitions. */ From af73839406ffc359636704d5a39213f380165c24 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 30 May 2023 10:39:01 +0300 Subject: [PATCH 3/3] clock: [#55] Proper variable type. --- source/shoh/freertos/FreeRTOSConfig.h | 2 +- source/shoh/src/peripherals/Clock.cpp | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/source/shoh/freertos/FreeRTOSConfig.h b/source/shoh/freertos/FreeRTOSConfig.h index 8742ebf..fc7a4d3 100644 --- a/source/shoh/freertos/FreeRTOSConfig.h +++ b/source/shoh/freertos/FreeRTOSConfig.h @@ -127,7 +127,7 @@ to exclude the API function. */ * must set up LPC_SCT1. */ void vConfigureTimerForRunTimeStats(void); -unsigned long ulGetTimeForRunTimeStats(void); +configRUN_TIME_COUNTER_TYPE ulGetTimeForRunTimeStats(void); #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats() /* The value is read directly from the counter register for efficiency and low overhead. */ #define portGET_RUN_TIME_COUNTER_VALUE() ulGetTimeForRunTimeStats() diff --git a/source/shoh/src/peripherals/Clock.cpp b/source/shoh/src/peripherals/Clock.cpp index feb5b88..dcaa0cb 100644 --- a/source/shoh/src/peripherals/Clock.cpp +++ b/source/shoh/src/peripherals/Clock.cpp @@ -59,14 +59,11 @@ extern "C" { /** * @brief Used by FreeRTOS to collect runtime statistics. * @paragraph FreeRTOS is unable to handle the counter overflow, so it must be done from our side. - * This solution will break eventually later on, presumably after 1193 hours of runtime, - * since unsigned long won't be able to hold more than that amount of milliseconds. - * FreeRTOS expects to get unsigned long, so there's nothing more that can be done. - * @return unsigned long + * @return configRUN_TIME_COUNTER_TYPE */ - unsigned long ulGetTimeForRunTimeStats(void) + configRUN_TIME_COUNTER_TYPE ulGetTimeForRunTimeStats(void) { - return (unsigned long)(((double)(counter_overflows - 1) * max_counter_value) + LPC_SCT1->COUNT_U) + return (configRUN_TIME_COUNTER_TYPE)(((double)(counter_overflows - 1) * max_counter_value) + LPC_SCT1->COUNT_U) / ((double)Chip_Clock_GetMainClockRate() / 1000); } }