Difference between revisions of "STM32 FreeRTOS Statistics"
(→Code) |
(→Code) |
||
Line 46: | Line 46: | ||
/* USER CODE END TIM1_TRG_COM_TIM11_IRQn 1 */ | /* USER CODE END TIM1_TRG_COM_TIM11_IRQn 1 */ | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | Enabling the statistics in [[Stm32CubeMX]] result in a couple of weak functions added to <pre>freertos.c</pre>. We can override those functions in our <pre>main.c</pre>: | ||
+ | |||
+ | <pre> | ||
+ | void configureTimerForRunTimeStats(void) | ||
+ | { | ||
+ | ulHighFrequencyTimerTicks = 0; | ||
+ | HAL_TIM_Base_Start_IT(&htim10); | ||
+ | } | ||
+ | |||
+ | unsigned long getRunTimeCounterValue(void) | ||
+ | { | ||
+ | return ulHighFrequencyTimerTicks; | ||
} | } | ||
</pre> | </pre> |
Revision as of 01:32, 18 October 2021
When developing applications which use FreeRTOS it can be quite unclear how much processing time is actually being used by each task. Fortunately, FreeRTOS have the option of enabling task statistics.
I was at some point messing around with DMA driven PWM (see here) on a Black Pill board. This particular example enables 3 tasks. One task is toggling the built-in LED on PC13. Two other tasks are updating the DMA buffers triggered by an interrupt call back.
CubeMX Configuration
First step is to enable a timer. The clock configuration is like this:
In other words, the APB1 Timer Clock is running at 96 MHz. Configuring the timer with a counter period of 959 and no prescaler will result in an interrupt frequency of 100 kHz - or 100 interrupts every millisecond.
The final step is to enable the statistics in the FreeRTOS section of CubeMX:
Code
Once we have the timer in place and the statistics options enabled in FreeRTOS we need to add a little bit of code.
First step is to add a variable to count the high frequency timer ticks:
volatile unsigned long ulHighFrequencyTimerTicks;
Since the variable is going to get updated in the interrupt handler, we declare it as volatile.
Second step is to update this variable in the interrupt handler itself. In the case of the Black Pill board, which in turn is based on a STM32F411 MCU, the interrupt handler is generated in
stm32f4xx_it.c
. We simply increment the counter there:
/** * @brief This function handles TIM1 trigger and commutation interrupts and TIM11 global interrupt. */ void TIM1_TRG_COM_TIM11_IRQHandler(void) { /* USER CODE BEGIN TIM1_TRG_COM_TIM11_IRQn 0 */ // Needed for freertos stats ulHighFrequencyTimerTicks++; /* USER CODE END TIM1_TRG_COM_TIM11_IRQn 0 */ HAL_TIM_IRQHandler(&htim11); /* USER CODE BEGIN TIM1_TRG_COM_TIM11_IRQn 1 */ /* USER CODE END TIM1_TRG_COM_TIM11_IRQn 1 */ }
Enabling the statistics in Stm32CubeMX result in a couple of weak functions added to
freertos.c
. We can override those functions in our
main.c
:
void configureTimerForRunTimeStats(void) { ulHighFrequencyTimerTicks = 0; HAL_TIM_Base_Start_IT(&htim10); } unsigned long getRunTimeCounterValue(void) { return ulHighFrequencyTimerTicks; }