cancel
Showing results for 
Search instead for 
Did you mean: 

DWT_CYCCNT does not seems to count cycle

leonardo
Associate III

I'm using follow code:

volatile unsigned int *DWT_CYCCNT  = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *DWT_LAR   = (volatile unsigned int *)0xE0001FB0; //address of the register
volatile unsigned int *SCB_DEMCR  = (volatile unsigned int *)0xE000EDFC; //address of the register
  
  *SCB_DEMCR |= 0x01000000;
  *DWT_LAR = 0xC5ACCE55; // unlock
  *DWT_CYCCNT = 0; // reset the counter
  *DWT_CONTROL |= 1 ; // enable the counter
  uint32_t start=0, end=0;
  uint32_t array[100];
  uint32_t idx=0;
 
  *DWT_CYCCNT = 0;
  start = *DWT_CYCCNT;
  idx=1;
  end = *DWT_CYCCNT;
  idx=0;

I put a breackpoint on line 18 and I get:

start=9

end=22

*DWT_CYCCNT = 29

I'm expecting that start could be something like 2 and end something like 4 or 6. Why I get this results?

Is DWT working in an stm32f722?

thank

4 REPLIES 4
TDK
Guru

These are C statements, not assembly. Examine the disassembly to see what instructions are between start and end. Cache and flash wait states can also have an effect.

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

Time 100 or 1000 iterations of the code to benchmark, subtract out overhead of empty vs test loop

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The problem I see is a difference between cortex M4 (stm32f411) and M7 (stm32f722)

On M4 I can run the code posted and I get exactly 2 cycle, I can run this other code:

	*DWT_CYCCNT = 0;
	uint_test=1;	// 2 ciclos
	*DWT_CYCCNT = 0;
	float_test=1;	// 2 ciclos
	*DWT_CYCCNT = 0;
	float_test=3.4f * 1.000054f;
	*DWT_CYCCNT = 0;
	float_test=3.4 * float_test;

and get exactly what I expect on debug viewer.

But on M7 I get very wrong values.

I can't find on stm32f722 reference manual the implementation of DWT_CYCCNT. So I'm wondering about the working of this module on M7

In fact, DWT_CYCNT is changing ther values (counting) without code line 6 to 9 on first post. It seems like the DWT is configured and enabled by default? My project is generated using cubemx tool.

I'm very confused.

I create a new empty project using SystemWorkbench IDE. I don't include HAL library, just startup file.

Then create a main.c file and write:

volatile unsigned int *DWT_CYCCNT  = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *DWT_LAR   = (volatile unsigned int *)0xE0001FB0; //address of the register
volatile unsigned int *SCB_DEMCR  = (volatile unsigned int *)0xE000EDFC; //address of the register
 
int main(void)
{
//	*SCB_DEMCR |= 0x01000000;
//	*DWT_LAR = 0xC5ACCE55; // unlock
//	*DWT_CYCCNT = 0; // reset the counter
//	*DWT_CONTROL |= 1 ; // enable the counter
 
	int uint_test=0;
	float float_test =0.0f;
	float f1=3.14f, f2=3.15f;
 
	*DWT_CYCCNT = 0;
	uint_test=1;	// 2 ciclos
	*DWT_CYCCNT = 0;
	float_test=1;	// 2 ciclos
	*DWT_CYCCNT = 0;
	float_test=3.4f * 1.000054f;
	*DWT_CYCCNT = 0;
	float_test=3.4 * float_test;
	*DWT_CYCCNT = 0;
	float_test=f1 * f2;	
	*DWT_CYCCNT = 0;
}

I get wrong values on expression viewer (debug and step by step) and get HardFault on line 26! what am I missing????

Same code on a fresh project running on stm32f411 (with lines 8..11 uncommented) get right values for DWT_CYCCNT but HardFault on line 26.