200 lines
5.5 KiB
C
200 lines
5.5 KiB
C
|
/**
|
|||
|
****************************************************************************************************
|
|||
|
* @file bsp_delay.c
|
|||
|
* @author
|
|||
|
* @version V1.0
|
|||
|
* @date 2024-03-10
|
|||
|
* @brief 延时模块
|
|||
|
****************************************************************************************************
|
|||
|
*/
|
|||
|
|
|||
|
#include "drv_sys.h"
|
|||
|
#include "bsp_delay.h"
|
|||
|
|
|||
|
|
|||
|
static uint32_t g_fac_us = 0; /* us延时倍乘数 */
|
|||
|
|
|||
|
/* 如果SYS_SUPPORT_OS定义了,说明要支持OS了(不限于UCOS) */
|
|||
|
#if SYS_SUPPORT_OS
|
|||
|
|
|||
|
/* 添加公共头文件 ( ucos需要用到) */
|
|||
|
#include "os.h"
|
|||
|
|
|||
|
/**
|
|||
|
* @brief systick中断服务函数,使用OS时用到
|
|||
|
* @param ticks : 延时的节拍数
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void SysTick_Handler(void)
|
|||
|
{
|
|||
|
/* OS 开始跑了,才执行正常的调度处理 */
|
|||
|
if (OSRunning == OS_STATE_OS_RUNNING)
|
|||
|
{
|
|||
|
/* 调用 uC/OS-III 的 SysTick 中断服务函数 */
|
|||
|
OS_CPU_SysTickHandler();
|
|||
|
}
|
|||
|
HAL_IncTick();
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 初始化延迟函数
|
|||
|
* @param sysclk: 系统时钟频率, 即CPU频率(rcc_c_ck), 168MHz
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void delay_init(uint16_t sysclk)
|
|||
|
{
|
|||
|
#if SYS_SUPPORT_OS /* 如果需要支持OS */
|
|||
|
uint32_t reload;
|
|||
|
#endif
|
|||
|
SysTick->CTRL = 0;
|
|||
|
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);/* SYSTICK使用外部时钟源,频率为HCLK */
|
|||
|
g_fac_us = sysclk; /* 不论是否使用OS,g_fac_us都需要使用 */
|
|||
|
#if SYS_SUPPORT_OS /* 如果需要支持OS. */
|
|||
|
reload = sysclk; /* 每秒钟的计数次数 单位为M */
|
|||
|
reload *= 1000000 / OSCfg_TickRate_Hz; /* 根据delay_ostickspersec设定溢出时间,reload为24位
|
|||
|
* 寄存器,最大值:16777216,在168M下,约合0.09986s左右
|
|||
|
*/
|
|||
|
SysTick->CTRL |= 1 << 1;
|
|||
|
SysTick->LOAD = reload;
|
|||
|
SysTick->CTRL |= 1 << 0;
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
#if SYS_SUPPORT_OS /* 如果需要支持OS, 用以下代码 */
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 延时nus
|
|||
|
* @param nus: 要延时的us数
|
|||
|
* @note nus取值范围 : 0 ~ 190887435us(最大值即 2^32 / fac_us @fac_us = 21)
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void delay_us(uint32_t nus)
|
|||
|
{
|
|||
|
uint32_t ticks;
|
|||
|
uint32_t told, tnow, tcnt = 0;
|
|||
|
uint32_t reload = SysTick->LOAD; /* LOAD的值 */
|
|||
|
/* 定义用于接收错误代码的变量 */
|
|||
|
OS_ERR err;
|
|||
|
ticks = nus * g_fac_us; /* 需要的节拍数 */
|
|||
|
/* 锁定 uC/OS-III 的任务调度器 */
|
|||
|
OSSchedLock(&err);
|
|||
|
told = SysTick->VAL; /* 刚进入时的计数器值 */
|
|||
|
while (1)
|
|||
|
{
|
|||
|
tnow = SysTick->VAL;
|
|||
|
if (tnow != told)
|
|||
|
{
|
|||
|
if (tnow < told)
|
|||
|
{
|
|||
|
tcnt += told - tnow; /* 这里注意一下SYSTICK是一个递减的计数器就可以了 */
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
tcnt += reload - tnow + told;
|
|||
|
}
|
|||
|
told = tnow;
|
|||
|
if (tcnt >= ticks)
|
|||
|
{
|
|||
|
break; /* 时间超过/等于要延迟的时间,则退出 */
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/* 恢复 uC/OS-III 的任务调度器 */
|
|||
|
OSSchedUnlock(&err);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 延时nms
|
|||
|
* @param nms: 要延时的ms数 (0< nms <= 65535)
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void delay_ms(uint16_t nms)
|
|||
|
{
|
|||
|
uint32_t i;
|
|||
|
for (i=0; i<nms; i++)
|
|||
|
{
|
|||
|
delay_us(1000);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#else /* 不使用OS时, 用以下代码 */
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 延时nus
|
|||
|
* @param nus: 要延时的us数.
|
|||
|
* @note nus取值范围 : 0~190887435(最大值即 2^32 / fac_us @fac_us = 21)
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void delay_us(uint32_t nus)
|
|||
|
{
|
|||
|
uint32_t ticks;
|
|||
|
uint32_t told, tnow, tcnt = 0;
|
|||
|
uint32_t reload = SysTick->LOAD; /* LOAD的值 */
|
|||
|
ticks = nus * g_fac_us; /* 需要的节拍数 */
|
|||
|
told = SysTick->VAL; /* 刚进入时的计数器值 */
|
|||
|
while (1)
|
|||
|
{
|
|||
|
tnow = SysTick->VAL;
|
|||
|
if (tnow != told)
|
|||
|
{
|
|||
|
if (tnow < told)
|
|||
|
{
|
|||
|
tcnt += told - tnow; /* 这里注意一下SYSTICK是一个递减的计数器就可以了 */
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
tcnt += reload - tnow + told;
|
|||
|
}
|
|||
|
told = tnow;
|
|||
|
if (tcnt >= ticks)
|
|||
|
{
|
|||
|
break; /* 时间超过/等于要延迟的时间,则退出 */
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief 延时nms
|
|||
|
* @param nms: 要延时的ms数 (0< nms <= 65535)
|
|||
|
* @retval 无
|
|||
|
*/
|
|||
|
void delay_ms(uint16_t nms)
|
|||
|
{
|
|||
|
uint32_t repeat = nms / 540; /* 这里用540,是考虑到可能有超频应用, 比如248M的时候,delay_us最大只能延时541ms左右了 */
|
|||
|
uint32_t remain = nms % 540;
|
|||
|
|
|||
|
while (repeat)
|
|||
|
{
|
|||
|
delay_us(540 * 1000); /* 利用delay_us 实现 540ms 延时 */
|
|||
|
repeat--;
|
|||
|
}
|
|||
|
|
|||
|
if (remain)
|
|||
|
{
|
|||
|
delay_us(remain * 1000); /* 利用delay_us, 把尾数延时(remain ms)给做了 */
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief HAL库内部函数用到的延时
|
|||
|
* @note HAL库的延时默认用Systick,如果我们没有开Systick的中断会导致调用这个延时后无法退出
|
|||
|
* @param Delay : 要延时的毫秒数
|
|||
|
* @retval None
|
|||
|
*/
|
|||
|
void HAL_Delay(uint32_t Delay)
|
|||
|
{
|
|||
|
delay_ms(Delay);
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|