cancel
Showing results for 
Search instead for 
Did you mean: 

DWT timer on STM32F4xx devices not working properly

tm3341
Associate II
Posted on March 29, 2015 at 13:28

Hi,

I've implemented DWT timer on my STM32F4 device. It works as expected, when I init it first time and upload to device. If I press reset button, then timer still works. But now catch. If I reupload program again, DWT timer stops working. I have no idea what is happening here, but when this happen, I have to remove power and plug again. Tested on boards below, always the same result and I don't know why: - STM32F4-Discovery - STM32F429-Discovery - F401/411-Nucleo - STM32439-Eval How to solve this problem? I found example from clive1 using variables with pointer. I've made some research in core_cm4.h function with defines and see direct access to this locations using pointers. My code looks like this (Keil uVision 5.14):

/* Clock speed in MHz */
uint16_t SystemMHz = 180;
/* Init function */
static
void TM_DELAY2_Init(void) {
/* Enable TRC */
CoreDebug->DEMCR &= ~0x01000000;
CoreDebug->DEMCR |= 0x01000000;
/* Reset counter */
DWT->CYCCNT = 0;
/* Enable counter */
DWT->CTRL &= ~0x00000001;
DWT->CTRL |= 0x00000001;
}
/* Delay function */
#define DelayMicros(micros) {DWT->CYCCNT = 0; 
while
(DWT->CYCCNT < (micros * SystemMHz));}
int main(void) { 
/* Initialize system */
SystemInit();
/* Initialize delays */
TM_DELAY2_Init();
/* Initialize LEDs */
TM_DISCO_LedInit();
while
(1) {
/* Toggle all leds */
TM_DISCO_LedToggle(LED_ALL);
/* Delay some microseconds */
DelayMicros(100000);
}
}

#stm32f4 #timer #cortex-m4 #dwt
3 REPLIES 3
Posted on March 29, 2015 at 15:12

Can you write to the counter when it's enabled? Is there some value to separating the AND and OR operations, rather than performing a single read-modify-write operation.

I don't clear the counter during normal operations, I read a starting value, and delta it, that way it's agnostic to the starting value, letting it free run, and allowing multiple timeouts to use the same timer in different threads.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tm3341
Associate II
Posted on March 29, 2015 at 16:00

Thanks for response, but I didn't get any successfull data.

According to the last code, I've made this modification:

/* Delay function */
void DelayMicros(uint32_t micros) {
__IO uint32_t c = DWT->CYCCNT;
do
{
} 
while
((DWT->CYCCNT - c) < (micros * SystemMHz));
}

But still no success. I have to remove power or press reset button before it starts working again.
tm3341
Associate II
Posted on March 30, 2015 at 14:20

Ok, the problem must be with ST-Link programmer.

I'm using the newest 23 I think it is.

Using Keil ULINK2 it worked perfect.