cancel
Showing results for 
Search instead for 
Did you mean: 

Hi all, I tried to implement in "Bluepill" the popular DWT base microseconds delay but it did nor work. I found in case of other types counter should be enabled. However in my CubeIDE cannot find DWT_LAR member of DWT. How can I make run in stm32f103?

IReta.2
Associate II
 
6 REPLIES 6

It's a magic access number/register combo,as I recall it's not needed on the CM3 processors.

Becomes a thing mainly on the CM7

This should work to start the cycle counter

volatile uint32_t *DWT_CONTROL = (uint32_t *) 0xE0001000;

volatile uint32_t *DWT_CYCCNT = (uint32_t *) 0xE0001004;

volatile uint32_t *DEMCR = (uint32_t *) 0xE000EDFC;

*DEMCR = *DEMCR | 0x01000000;

*DWT_CYCCNT = 0;

*DWT_CONTROL = *DWT_CONTROL | 1;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
IReta.2
Associate II

Thank you for the fast response. I will give try. However my actual code is similar, but hangs in 'while'' statement:

void DWT_Delay(uint32_t us) // microseconds
{
    uint32_t startTick = DWT->CYCCNT,
             delayTicks = us * (SystemCoreClock/1000000);
 
    while (DWT->CYCCNT - startTick < delayTicks);
}
 

IReta.2
Associate II

Many thanks for your answer. It WORKS!!!

Nikita91
Lead II

Using CMSIS files:

#if defined __CM7_REV
	DWT->LAR = 0xC5ACCE55U ;
#endif
DWT->CYCCNT = 0 ;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk ;

IReta.2
Associate II

Thank you for your response, but in case of Bluepill CubeIDE cannot find DWT->LAR member of DWT. The way Tesla DeLorean suggested is working at stm32f103, too.

This code works for all Cortex-M3/M4/M7

DWT->LAR is only compiled if M7 is used thanks to the conditional.