Add HAL delay wrapper

This commit is contained in:
Henrik Stickann 2022-04-14 15:19:05 +02:00
parent 9056cfc264
commit 073fe83e7e
2 changed files with 72 additions and 0 deletions

40
include/sta/hal/delay.hpp Normal file
View File

@ -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 <sta/config.hpp>
#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

32
src/hal/delay.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <sta/hal/delay.hpp>
#ifdef STA_HAL_DELAY_ENABLE
#include <main.h>
namespace sta
{
void delayMs(uint32_t ms)
{
HAL_Delay(ms);
}
} // namespace sta
#ifdef STA_HAL_DELAY_US_TIM
#include <tim.h>
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