Files
checker_slave/source/rt_thread/core_delay.c

114 lines
1.9 KiB
C
Raw Normal View History

#include "stm32f10x.h"
2023-06-10 11:52:00 +08:00
#include "core_delay.h"
#include "rtthread.h"
#include <rthw.h>
#define DWT_CR *(__IO uint32_t *)0xE0001000
#define DWT_CYCCNT *(__IO uint32_t *)0xE0001004
#define DEM_CR *(__IO uint32_t *)0xE000EDFC
#define DEM_CR_TRCENA (1 << 24)
#define DWT_CR_CYCCNTENA (1 << 0)
//获取系统主频
2023-06-10 11:52:00 +08:00
static uint32_t get_sys_clocks_freq (void)
{
RCC_ClocksTypeDef t={0};
RCC_GetClocksFreq (&t);
return t.SYSCLK_Frequency;
}
int delay_init(void)
{
DEM_CR |= (uint32_t)DEM_CR_TRCENA;
DWT_CYCCNT = (uint32_t)0u;
DWT_CR |= (uint32_t)DWT_CR_CYCCNTENA;
return 0;
}
uint32_t delay_get_cnt(void)
2023-06-10 11:52:00 +08:00
{
return ((uint32_t)DWT_CYCCNT);
}
static uint32_t delay_get_tick(void)
{
return ((uint32_t)DWT_CYCCNT*1000/get_sys_clocks_freq());
}
// 获取当前微妙数
uint32_t delay_get_us(void)
{
return ((uint32_t)DWT_CYCCNT/(get_sys_clocks_freq()/1000000));
}
// 判断延时是否到,返回1已到0未到
int delay_check(uint32_t old,uint32_t wnd)
{
uint32_t now=delay_get_us();
if(now>=old){
if((now-old)>=wnd){
return 1;
}
}else{
if((UINT32_MAX - old + now)>=wnd){
return 1;
}
}
return 0;
}
2023-06-10 11:52:00 +08:00
// 最长可以延时59s
// 低于1ms的精确延时
2023-06-10 11:52:00 +08:00
void delay_us(uint32_t us)
{
uint32_t ticks;
uint32_t told, tnow, tcnt=0;
/* 需要的节拍数 */
2023-06-10 11:52:00 +08:00
ticks = us * (get_sys_clocks_freq() / 1000000);
tcnt = 0;
told = (uint32_t)delay_get_cnt();
while (1)
{
tnow = (uint32_t)delay_get_cnt();
if (tnow != told)
{
/* 32 位计数器是递增计数器 */
2023-06-10 11:52:00 +08:00
if (tnow > told)
{
tcnt += tnow - told;
}
/* 重新装载 */
2023-06-10 11:52:00 +08:00
else
{
tcnt += UINT32_MAX - told + tnow;
}
told = tnow;
/*时间超过/等于要延迟的时间,则退出 */
2023-06-10 11:52:00 +08:00
if (tcnt >= ticks)break;
}
}
}
// 实现rt_thread 的us级延时
2023-06-10 11:52:00 +08:00
void rt_hw_us_delay(rt_uint32_t us)
{
delay_us(us);
}