diff --git a/include/sta/hal/delay.hpp b/include/sta/hal/delay.hpp new file mode 100644 index 0000000..afb2b5f --- /dev/null +++ b/include/sta/hal/delay.hpp @@ -0,0 +1,40 @@ +/** + * @brief Delay functions. + * + * Configuration: + * STA_HAL_DELAY_ENABLE: Enable module + * STA_HAL_DELAY_US_TIM: HAL TIM instance for `delayUs()` + * + * NOTE: Don't forget to start TIM. When using startup system task this + * is automatically handled. + */ +#ifndef STA_HAL_DELAY_HPP +#define STA_HAL_DELAY_HPP + +#include +#ifdef STA_HAL_DELAY_ENABLE + + +namespace sta +{ + /** + * @brief Millisecond delay. + * + * @param ms Milliseconds + */ + void delayMs(uint32_t ms); + +#ifdef STA_HAL_DELAY_US_TIM + /** + * @brief Microsecond delay. + * + * @param us Microseconds + */ + void delayUs(uint32_t us); +#endif // STA_HAL_DELAY_US_TIM +} // namespace sta + + +#endif // STA_HAL_DELAY_TIM + +#endif // STA_HAL_DELAY_HPP diff --git a/src/hal/delay.cpp b/src/hal/delay.cpp new file mode 100644 index 0000000..9e4a0d0 --- /dev/null +++ b/src/hal/delay.cpp @@ -0,0 +1,32 @@ +#include +#ifdef STA_HAL_DELAY_ENABLE + +#include + + +namespace sta +{ + void delayMs(uint32_t ms) + { + HAL_Delay(ms); + } +} // namespace sta + + +#ifdef STA_HAL_DELAY_US_TIM + +#include + +namespace sta +{ + void delayUs(uint32_t us) + { + __HAL_TIM_SET_COUNTER(&STA_HAL_DELAY_US_TIM, 0); + while (__HAL_TIM_GET_COUNTER(&STA_HAL_DELAY_US_TIM) < us); + } +} // namespace sta + +#endif // STA_HAL_DELAY_US_TIM + + +#endif // STA_HAL_DELAY_ENABLE