92 lines
1.4 KiB
C
92 lines
1.4 KiB
C
![]() |
#include "stm32f4xx.h"
|
|||
|
#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)
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F><>ȡϵͳ<CFB5><CDB3>Ƶ
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static uint32_t delay_get_cnt(void)
|
|||
|
{
|
|||
|
return ((uint32_t)DWT_CYCCNT);
|
|||
|
}
|
|||
|
|
|||
|
static uint32_t delay_get_tick(void)
|
|||
|
{
|
|||
|
return ((uint32_t)DWT_CYCCNT*1000/get_sys_clocks_freq());
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>1ms<6D>ľ<EFBFBD>ȷ<EFBFBD><C8B7>ʱ
|
|||
|
void delay_us(uint32_t us)
|
|||
|
{
|
|||
|
uint32_t ticks;
|
|||
|
uint32_t told, tnow, tcnt=0;
|
|||
|
|
|||
|
/* <20><>Ҫ<EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
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 λ<><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
if (tnow > told)
|
|||
|
{
|
|||
|
tcnt += tnow - told;
|
|||
|
}
|
|||
|
/* <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0> */
|
|||
|
else
|
|||
|
{
|
|||
|
tcnt += UINT32_MAX - told + tnow;
|
|||
|
}
|
|||
|
|
|||
|
told = tnow;
|
|||
|
|
|||
|
/*ʱ<>䳬<EFBFBD><E4B3AC>/<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD>ӳٵ<D3B3>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD>˳<EFBFBD> */
|
|||
|
if (tcnt >= ticks)break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// ʵ<><CAB5>rt_thread <20><>us<75><73><EFBFBD><EFBFBD>ʱ
|
|||
|
void rt_hw_us_delay(rt_uint32_t us)
|
|||
|
{
|
|||
|
delay_us(us);
|
|||
|
}
|
|||
|
|