【极海APM32F407 Tiny Board】4、DWT实现延时函数
[复制链接]
uint32_t delay_init(void)
{
uint32_t c;
/* Enable TRC */
CoreDebug->DEMCR &= ~0x01000000;
CoreDebug->DEMCR |= 0x01000000;
/* Enable counter */
DWT->CTRL &= ~0x00000001;
DWT->CTRL |= 0x00000001;
/* Reset counter */
DWT->CYCCNT = 0;
/* Check if DWT has started */
c = DWT->CYCCNT;
/* 2 dummys */
__ASM volatile ("NOP");
__ASM volatile ("NOP");
/* Return difference, if result is zero, DWT has not started */
return (DWT->CYCCNT - c);
}
void delay_us(uint32_t us)
{
uint32_t start = DWT->CYCCNT;
/* Go to number of cycles for system */
us *= (SystemCoreClock / 1000000);
/* Delay till end */
while ((DWT->CYCCNT - start) < us);
}
void delay_ms(uint32_t ms)
{
uint32_t start = DWT->CYCCNT;
/* Go to number of cycles for system */
ms *= (SystemCoreClock / 1000);
/* Delay till end */
while ((DWT->CYCCNT - start) < ms);
}
DWT外设是M4内核的特性之一。
|