Fix usDelay handling freq != 1MHz incorrectly

This commit is contained in:
Henrik Stickann 2023-01-13 14:04:23 +01:00
parent 441f42fb62
commit d793e1dc1c

View File

@ -23,10 +23,12 @@ namespace sta
namespace sta namespace sta
{ {
uint32_t gDelayUsMul = 1;
void delayUs(uint32_t us) void delayUs(uint32_t us)
{ {
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0); __HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us); while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us * gDelayUsMul);
} }
@ -53,10 +55,13 @@ namespace sta
// Calculate TIM clock frequency // 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)();
// Calculate update frequency based on prescaler value // Calculate update frequency based on prescaler value
uint32_t updateFreq = clkFreq / STA_STM32_DELAY_US_TIM.Init.Prescaler; uint32_t prescaler = (STA_STM32_DELAY_US_TIM.Init.Prescaler) ? STA_STM32_DELAY_US_TIM.Init.Prescaler : 1;
uint32_t updateFreq = clkFreq / prescaler;
gDelayUsMul = updateFreq / 1000000;
// TIM must have at least microsecond precision (>= 1 MHz frequency) // TIM must have at least microsecond precision (>= 1 MHz frequency)
return (updateFreq == 1000000); return (updateFreq >= 1000000);
} }
} // namespace sta } // namespace sta