mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/sta-core.git
synced 2025-06-10 16:55:58 +00:00
67 lines
1.3 KiB
C++
67 lines
1.3 KiB
C++
#include <sta/stm32/delay.hpp>
|
|
#ifdef STA_STM32_DELAY_ENABLE
|
|
|
|
#include <sta/stm32/hal.hpp>
|
|
#include <sta/stm32/clocks.hpp>
|
|
|
|
#include <sta/assert.hpp>
|
|
#include <sta/lang.hpp>
|
|
|
|
|
|
namespace sta
|
|
{
|
|
void delayMs(uint32_t ms)
|
|
{
|
|
HAL_Delay(ms);
|
|
}
|
|
} // namespace sta
|
|
|
|
|
|
#ifdef STA_STM32_DELAY_US_TIM
|
|
|
|
#include <tim.h>
|
|
|
|
namespace sta
|
|
{
|
|
void delayUs(uint32_t us)
|
|
{
|
|
__HAL_TIM_SET_COUNTER(&STA_STM32_DELAY_US_TIM, 0);
|
|
while (__HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) < us);
|
|
}
|
|
|
|
|
|
bool isValidDelayUsTIM()
|
|
{
|
|
// Get PCLK multiplier for TIM clock
|
|
uint32_t pclkMul = 1;
|
|
switch (STA_STM32_DELAY_US_TIM.Init.ClockDivision)
|
|
{
|
|
case TIM_CLOCKDIVISION_DIV1:
|
|
pclkMul = 1;
|
|
break;
|
|
case TIM_CLOCKDIVISION_DIV2:
|
|
pclkMul = 2;
|
|
break;
|
|
case TIM_CLOCKDIVISION_DIV4:
|
|
pclkMul = 4;
|
|
break;
|
|
default:
|
|
STA_ASSERT(false);
|
|
STA_UNREACHABLE();
|
|
}
|
|
|
|
// 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 updateFreq = clkFreq / STA_STM32_DELAY_US_TIM.Init.Prescaler;
|
|
|
|
// TIM must have at least microsecond precision (>= 1 MHz frequency)
|
|
return (updateFreq == 1000000);
|
|
}
|
|
} // namespace sta
|
|
|
|
#endif // STA_STM32_DELAY_US_TIM
|
|
|
|
|
|
#endif // STA_STM32_DELAY_ENABLE
|