added profiler and updated delay implementation

This commit is contained in:
dario
2024-05-22 13:07:36 +02:00
parent 7aac2badc5
commit 767bd19c36
6 changed files with 99 additions and 3 deletions

View File

@@ -52,6 +52,7 @@ namespace sta
// Calculate TIM clock frequency
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)();
// Calculate update frequency based on prescaler value
uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1;
uint32_t updateFreq = clkFreq / prescaler;
@@ -66,10 +67,29 @@ namespace sta
{
// Check if the specified timer is usable for microsecond delays.
STA_ASSERT(isValidDelayUsTIM());
STA_ASSERT(us < 1000);
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
// Use millisecond delays for us > 1000 and use microseconds delay for the remainder.
delayMs(us / 1000);
us = us % 1000;
// __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
uint32_t startTime = __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM);
// Check if an overflow is expected during the delay time.
if (startTime < __HAL_TIM_GET_AUTORELOAD(&STA_STM32_DELAY_US_TIM) - us * gDelayUsMul)
{
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < startTime + us * gDelayUsMul);
}
else
{
uint32_t overflowTime = __HAL_TIM_GET_AUTORELOAD(&STA_STM32_DELAY_US_TIM) - startTime;
// Wait until the overflow happens
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) > startTime);
// Wait the remaining time after the overflow.
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul - overflowTime);
}
}
} // namespace sta