2021-04-14 04:16 AM
Hello community,
I implemented a microsecond delay on a STM32L5 microprocessor using the "Data watchpoint and trace unit" (DWT). I am initialising the unit and count the DWT->CYCCNT down until my delay has passed.
This works totally fine when the Debugger (STLink) is connected. But if I run the programm without the debugger connected, than the DWT->CYCCNT is not increasing.
My initialisation code is the following:
uint32_t TM_DELAY_Init(void) {
uint32_t c;
/* Enable TRC */
CoreDebug->DEMCR &= ~0x01000000;
CoreDebug->DEMCR |= 0x01000000;
/* Enable counter */
DWT->CTRL &= ~0x00000001;
DWT->CTRL |= 0x00000001;
/* Reset counter */
DWT->CYCCNT = 0;
/* Check if DWT has started */
c = DWT->CYCCNT;
/* 2 dummys */
__ASM volatile ("NOP");
__ASM volatile ("NOP");
/* Return difference, if result is zero, DWT has not started */
return (DWT->CYCCNT - c);
}
My delay function is the following:
__STATIC_INLINE void Delay(__IO uint32_t micros) {
uint32_t start = DWT->CYCCNT;
/* Go to number of cycles for system */
micros *= (HAL_RCC_GetHCLKFreq() / 1000000);
/* Delay till end */
while ((DWT->CYCCNT - start) < micros); // error
// THE CODE DOENST REACH BELOW THE WHILE
}
The program .elf file is exactly the same with and without debugger.
I have read multiple online sources and noticed, that some sources also state, that you have to unlock the Lock-Access-Register DWT->LAR to make the DWT run without a debugger connected. This LAR-Register doesnt exsit for the L5-series. Adding the code
volatile uint32_t *LAR = (uint32_t *) 0xE0001FB0; // <-- added lock access register
*LAR = 0xC5ACCE55; // <-- added unlock access to DWT (ITM, etc.)registers
doesn't help.
I would appreciate any help.
Thanks in advance.