不占用定时器和Systick的us级延时函数
来自国外TM大神的函数库,不占用定时器和Systick的延时函数,利用了DWT:uint32_t TM_DELAY_Init(void) {
#if !defined(STM32F0xx)
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);
#else
/* Return OK */
return 1;
#endif
}
__STATIC_INLINE void Delay(__IO uint32_t micros) {
#if !defined(STM32F0xx)
uint32_t start = DWT->CYCCNT;
/* Go to number of cycles for system */
micros *= (HAL_RCC_GetHCLKFreq() / 1000000);
/* Delay till end */
while ((DWT->CYCCNT - start) < micros);
#else
/* Go to clock cycles */
micros *= (SystemCoreClock / 1000000) / 5;
/* Wait till done */
while (micros--);
#endif
}
国外人学艺精,值得学习
页:
[1]