diff --git a/include/sta/devices/stm32/clocks.hpp b/include/sta/devices/stm32/clocks.hpp index 0d8d832..cd2ac38 100644 --- a/include/sta/devices/stm32/clocks.hpp +++ b/include/sta/devices/stm32/clocks.hpp @@ -58,6 +58,7 @@ namespace sta */ #define _STA_STM32_PCLK_IDX_MAP(type, idx) STA_STM32_ ## type ## _ ## idx ## _PCLK_IDX // Get HAL handle to PCLK index map macro + /** * @brief Map instance handle to PCLK index. * diff --git a/include/sta/time.hpp b/include/sta/time.hpp index 6495952..1cb5562 100644 --- a/include/sta/time.hpp +++ b/include/sta/time.hpp @@ -16,12 +16,27 @@ namespace sta * @return Time in milliseconds */ using TimeMsFn = uint32_t (*)(); + /** * @brief Signature for microseconds precision time. * * @return Time in microseconds */ using TimeUsFn = uint32_t (*)(); + + /** + * @brief Gets the current time in milliseconds. + * + * @return Time in milliseconds + */ + uint32_t timeMs(); + + /** + * @brief Gets the current time in microseconds. + * + * @return Time in microseconds + */ + uint32_t timeUs(); } // namespace sta diff --git a/src/devices/stm32/delay.cpp b/src/devices/stm32/delay.cpp index 3592bf0..6475b51 100644 --- a/src/devices/stm32/delay.cpp +++ b/src/devices/stm32/delay.cpp @@ -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 diff --git a/src/devices/stm32/init.cpp b/src/devices/stm32/init.cpp index 0f06016..dc07c79 100644 --- a/src/devices/stm32/init.cpp +++ b/src/devices/stm32/init.cpp @@ -23,6 +23,8 @@ namespace sta STA_ASSERT(isValidDelayUsTIM()); // Start timer base HAL_TIM_Base_Start(&STA_STM32_DELAY_US_TIM); +#else + #endif // STA_STM32_DELAY_US_TIM } } // namespace sta diff --git a/src/devices/stm32/time.cpp b/src/devices/stm32/time.cpp new file mode 100644 index 0000000..3cc7df5 --- /dev/null +++ b/src/devices/stm32/time.cpp @@ -0,0 +1,32 @@ +/* + * time.cpp + * + * Created on: May 22, 2024 + * Author: Dario + */ + +#include + +#include +#ifdef STA_PLATFORM_STM32 + +#include + +namespace sta +{ + uint32_t timeMs() + { + return HAL_GetTick(); + } + +#ifdef STA_STM32_DELAY_US_TIM + extern uint32_t gDelayUsMul; + + uint32_t timeUs() + { + return __HAL_TIM_GET_COUNTER(&STA_STM32_DELAY_US_TIM) / gDelayUsMul; + } +} +#endif // STA_STM32_DELAY_US_TIM + +#endif // STA_PLATFORM_STM32 diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..eeeba78 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,26 @@ +/* + * time.cpp + * + * Created on: May 22, 2024 + * Author: Dario + */ + +#include +#include + +namespace sta +{ + STA_WEAK + uint32_t timeMs() + { + STA_NOT_IMPLEMENTED(); + } + + STA_WEAK + uint32_t timeUs() + { + STA_NOT_IMPLEMENTED(); + } +} // namespace sta + +