Updated clock prescaler, tested profiler and delayUs

This commit is contained in:
dario
2024-05-28 12:06:09 +02:00
parent e5c4b9a6cc
commit 4b0e8bd4ab
7 changed files with 91 additions and 52 deletions

View File

@@ -51,7 +51,7 @@ namespace sta
}
// Calculate TIM clock frequency
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)();
uint32_t clkFreq = pclkMul * STA_STM32_GET_HANDLE_PCLK_FREQ_FN(STA_STM32_DELAY_US_TIM)() * STA_STM32_GET_HANDLE_PCLK_TIM_PRESCALER(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;
@@ -68,33 +68,22 @@ namespace sta
// Check if the specified timer is usable for microsecond delays.
STA_ASSERT(isValidDelayUsTIM());
// Use millisecond delays for us > 1000 and use microseconds delay for the remainder.
delayMs(us / 1000);
us = us % 1000;
// Save the current value of the counter.
uint32_t current = __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM);
// __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
// Set the timer counter to zero to avoid overflows during delay-
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
uint32_t startTime = __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM);
// Wait for the desired microseconds.
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
// 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);
}
// Set the timer counter to the previous value and add the waited duration to it.
// This avoids collisions with code relying on timeUs measurements.
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, current + us * gDelayUsMul);
}
} // namespace sta
#endif // STA_STM32_DELAY_US_TIM
#endif // STA_PLATFORM_STM32

View File

@@ -11,6 +11,7 @@
#ifdef STA_PLATFORM_STM32
#include <sta/devices/stm32/hal.hpp>
#include <sta/devices/stm32/clocks.hpp>
namespace sta
{

View File

@@ -14,12 +14,16 @@ namespace sta
uint32_t timeMs()
{
STA_NOT_IMPLEMENTED();
return 0;
}
STA_WEAK
uint32_t timeUs()
{
STA_NOT_IMPLEMENTED();
return 0;
}
} // namespace sta