2023-06-23 04:43 AM
Hi every one
I want to use DWT->CYCCNT in my software for STM32G491RE. I used these 2 lines to enable it:
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
But when I see with the debugger it seems that it does not write anything to these registers. AFAIK for Cortex M7 we also have a lock register that should be written with the unique value provided by the reference manual.
But here as we have cortex m4 I think we don't have such a thing, as I couldn't find this register in the CMSIS header file too.
Am I missing anything? What should I do?
By the way, I have to say that I can use CYCCNT while the debugger is connected. the problem is that I need to also use it when it is not connected to the debugger.
2023-06-23 05:48 AM - edited 2023-06-23 07:24 AM
its working fine on cortex M : STM32F303 here
#include <stdint.h>
volatile uint32_t count = 0;
// addresses of registers
volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000;
volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xE0001004;
volatile uint32_t *DEMCR = (uint32_t *)0xE000EDFC;
// enable the use DWT
*DEMCR = *DEMCR | 0x01000000;
// Reset cycle counter
*DWT_CYCCNT = 0;
// enable cycle counter
*DWT_CONTROL = *DWT_CONTROL | 1 ;
while(1)
{
// some code here
// .....
// number of cycles stored in count variable
count = *DWT_CYCCNT;
printf(" time cycles %ld \n", count);
output...:
2024-01-27 10:17 AM
Here is what I use on a STM32G474RE:
int main(void)
{
/* USER CODE BEGIN 1 */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
// Enable cycle counter
DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
/* USER CODE END 1 */
I am then able to use the DWT->CYCCNT with or without the debugger