stm32f103xx-hal icon indicating copy to clipboard operation
stm32f103xx-hal copied to clipboard

MonoTimer does not work without debugger connected

Open TheZoq2 opened this issue 7 years ago • 5 comments

My current project reads a MonoTimer in order to keep track of signals which have been received. These signals are then sent over serial to a pc. This works flawlessly when the stlink debugger is connected, however without it connected the value read is always 0.

I assume this is because monotimer uses the "debug watch trace" peripheral which judging by the name means that it doesn't work without a debugger. Perhaps this should be specified in the documentation, or perhaps mono timer could be rewritten to use something that works without the debugger.

One thing to note is that the frequency reading still works.

TheZoq2 avatar Apr 25 '18 09:04 TheZoq2

Having the same issue, but only works if it just has been flashed by the debugger. Unplugging, and plugging it back to the debugger causes the MonoTimer to always read 0 ticks elapsed, until reflashed.

kellerkindt avatar Sep 01 '18 21:09 kellerkindt

@TheZoq2 Got it running with the following workaround, which I call before MonoTimer::new: unsafe { *(0xE000EDFC as *mut u32) |= 0x01000000; } See example C Code here and here, especially this code section:

#define DEMCR_TRCENA    0x01000000

/* Core Debug registers */
#define DEMCR           (*((volatile uint32_t *)0xE000EDFC))
...
/* Enable DWT */
DEMCR |= DEMCR_TRCENA; 
*DWT_CYCCNT = 0;             
/* Enable CPU cycle counter */
DWT_CTRL |= CYCCNTENA;

There is no such equivalent of the /* Enable DWT */ DEMCR |= DEMCR_TRCENA; in the rust call in dwt.enable_cycle_counter(); in MonoTimer::new, it only results in /* Enable CPU cycle counter */ DWT_CTRL |= CYCCNTENA;

kellerkindt avatar Sep 01 '18 22:09 kellerkindt

Cool, I have since moved on to another solution without the MonoTimer but this might come in useful in the future.

A more rusty approach would be to use the demcr register here https://japaric.github.io/stm32f103xx-hal/cortex_m/peripheral/struct.DCB.html#structfield.demcr to avoid having a raw pointer. Might be worth making a pull request to fix it

TheZoq2 avatar Sep 02 '18 08:09 TheZoq2

A more rusty approach

Haha, definitely. I just needed some kind of a solution yesterday evening :) Interesting that you found the demcr register, although I searched for it with the github search. Might have missed cortex_m crate though. I am still a bit lost in the rust abstractions for the microcontrollers...

kellerkindt avatar Sep 02 '18 08:09 kellerkindt

I just noticed that MonoTimer seems to be stopped, while I writing something to hstdout.

tib888 avatar Sep 08 '18 21:09 tib888