2015-03-29 04:28 AM
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
2015-03-29 06:12 AM
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.2015-03-29 07:00 AM
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.
2015-03-30 05:20 AM
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.