AnsweredAssumed Answered

DWT timer on STM32F4xx devices not working properly

Question asked by Majerle.Tilen on Mar 29, 2015
Latest reply on Mar 30, 2015 by Majerle.Tilen
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);
    }
}

Outcomes