cancel
Showing results for 
Search instead for 
Did you mean: 

Using CYCCNT in STM32G491RE

ali rostami
Associate III

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.

2 REPLIES 2
AScha.3
Chief III

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...:

AScha3_0-1687524400181.png

 

 

 

If you feel a post has answered your question, please click "Accept as Solution".
jose luu
Associate III

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