I tried to impement simple delay function using DWT block in stm32f103rb it looks like this
''' /* Use DWT_Delay_Init (); and DWT_Delay_us (microseconds) in the main */
__STATIC_INLINE void DWT_Delay_us(volatile uint32_t microseconds)
{
volatile uint32_t clk_cycle_start = DWT->CYCCNT;
/* Go to number of cycles for system */
microseconds *= ((SYSCLK_FREQ_72) / 1000000);
/* Delay till end */
while ((DWT->CYCCNT - clk_cycle_start) < microseconds);
}
uint32_t DWT_Delay_Init(void) {
/* Disable TRC */
CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk; // ~0x01000000;
/* Enable TRC */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; // 0x01000000;
/* Disable clock cycle counter */
DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk; //~0x00000001;
/* Enable clock cycle counter */
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; //0x00000001;
/* Reset the clock cycle counter value */
DWT->CYCCNT = 0;
/* 3 NO OPERATION instructions */
__ASM volatile ("NOP");
__ASM volatile ("NOP");
__ASM volatile ("NOP");
/* Check if clock cycle counter has started */
if(DWT->CYCCNT)
{
return 0; /*clock cycle counter started*/
}
else
{
return 1; /*clock cycle counter not started*/
}
}'''
And it does not work! Can you help me what is wrong?
User contributions licensed under CC BY-SA 3.0