DWT Cylce counter not working

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • 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 Source Code

    1. 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;
    2. *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
    3. for (i = 0; i < 100; i++) { printf("Hello World %d!\n", i); }
    4. uint32_t count = *DWT_CYCCNT; printf("Count is %d\n", count);
    5. 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
  • 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
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi Johannes,

    SEGGER - Johannes wrote:


    Your code looks correct.

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

    Ok.

    SEGGER - Johannes wrote:


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

    Yes, this is working.

    SEGGER - Johannes wrote:


    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
  • 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
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • 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
    Files
    • dwt_cnt.zip

      (60.19 kB, downloaded 672 times, last: )
  • 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
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi,

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

    Regards
    Johannes
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • 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.