Friday, June 23rd 2017, 8:52am UTC+2

You are not logged in.

  • Login
  • Register

Razer6

Beginner

Date of registration: Jan 30th 2010

Posts: 6

1

Wednesday, February 8th 2017, 10:13am

DWT Cylce counter not working

I'm trying to get the DWT cycle counter working for a STM32 STM32F3 ARM Cortex CPU inside the Embedded Studio debugger. For this, I am using the following code.

C/C++ Source code

1
2
3
4
5
void main(void) {  int i;    volatile uint32_t *DWT_CONTROL = (uint32_t *) 0xE0001000;  volatile uint32_t *DWT_CYCCNT = (uint32_t *) 0xE0001004;  volatile uint32_t *DEMCR = (uint32_t *) 0xE000EDFC;  volatile uint32_t *LAR  = (uint32_t *) 0xE0001FB0;  
  *DEMCR = *DEMCR | 0x01000000;     // enable trace  *LAR = 0xC5ACCE55;                // <-- added unlock access to DWT (ITM, etc.)registers   *DWT_CYCCNT = 0;                  // clear DWT cycle counter  *DWT_CONTROL = *DWT_CONTROL | 1;  // enable DWT cycle counter
  for (i = 0; i < 100; i++) {    printf("Hello World %d!\n", i);  }
  uint32_t count = *DWT_CYCCNT;  printf("Count is %d\n", count);
  do {    i++;  } while (1);}


However, the cycle counter always returns 0 - in other words, it is not running.
Can someone help me to get this working inside the debugger?

Thanks and cheers,
Razer6

SEGGER - Johannes

Super Moderator

Date of registration: Aug 31st 2012

Posts: 350

2

Friday, February 10th 2017, 11:41am

Hi,

Your code looks correct.

Usually the cycle counter is already enabled after start, but it does not hurt to re-enable it.

In the status bar of Embedded Studio you can see the cycle counter, too (as xxx,xxx Cycles).
You could check if it is running there.
When you step through your code, you should see that it goes to 0 after *DWT_CYCCNT = 0; and continues running from there.

Regards
Johannes

Razer6

Beginner

Date of registration: Jan 30th 2010

Posts: 6

3

Friday, February 10th 2017, 2:28pm

Hi Johannes,


Your code looks correct.

Usually the cycle counter is already enabled after start, but it does not hurt to re-enable it.

Ok.


In the status bar of Embedded Studio you can see the cycle counter, too (as xxx,xxx Cycles).

Yes, this is working.


When you step through your code, you should see that it goes to 0 after *DWT_CYCCNT = 0; and continues running from there.

I cannot reproduce this. The cycle counter in the status bar increases when stepping over *DWT_CYCCNT = 0;
Have you tried this in Embedded Studio?

I do not want to use the status bar but rather want to print the cycles to the debug output window in order to be able to parse it for further processing.

Cheers,
Razer6

SEGGER - Johannes

Super Moderator

Date of registration: Aug 31st 2012

Posts: 350

4

Monday, February 13th 2017, 2:12pm

Hi,

The status bar shows the value as read from the cycle counter.

When it does not go to 0 after writing to DWT_CYCCNT, there must be something wrong with your application.

I tried the same in Embedded Studio. The status bar showed 0 and increased and the printf worked accordingly.

Regards
Johannes

Razer6

Beginner

Date of registration: Jan 30th 2010

Posts: 6

5

Monday, February 13th 2017, 3:26pm

Hi Johannes,

I cannot reproduce the counter to be working. I have a minimal not-working example in the attachment.
It's a solution for a generic ARM-Cortex-M3.

After the print loop, the program reads the current DWT counter and prints it.
I would expect this to print the current value as seen from the status bar. However, this prints: "Count is 0".
Furthermore, resetting the counter does not work for me.

Can you verify this with the solution project provided?

Razer
Razer6 has attached the following file:
  • dwt_cnt.zip (60.19 kB - 65 times downloaded - Last download: Today, 4:41am)

SEGGER - Johannes

Super Moderator

Date of registration: Aug 31st 2012

Posts: 350

6

Monday, February 13th 2017, 6:18pm

Hi,

The sample you provided runs in the simulator, which does not simulate the DWT.
But when I change it to run on actual hardware (Change Target Connection and Target Device Option) it works fine.

Regards
Johannes

Razer6

Beginner

Date of registration: Jan 30th 2010

Posts: 6

7

Tuesday, February 14th 2017, 10:05am

Hi,

Oh, that is the culprit. Thanks!
Is there a possibility to use the DWT cycle counter (or any other counter) in the simulator?

Best,
Razer6

SEGGER - Johannes

Super Moderator

Date of registration: Aug 31st 2012

Posts: 350

8

Wednesday, February 15th 2017, 9:46am

Hi,

The only peripheral which is simulated is the SysTick.
Everything else can only be used on hardware.

Regards
Johannes

LulaNord

Beginner

Date of registration: Apr 10th 2017

Posts: 1

9

Tuesday, April 11th 2017, 6:19pm

Hi...i am a new user here. As per my knowledge you should enable it again. In the status bar of Embedded Studio you can see the cycle counter.

You could check if it is running there.When you step through your code, you should see that it goes to 0 after *DWT_CYCCNT = 0; and continues running from there.