Skip to main content
David Pekin
Senior
January 16, 2018
Solved

stm32F3 clock tic reading

  • January 16, 2018
  • 4 replies
  • 1324 views
Posted on January 16, 2018 at 01:33

Hello,

I've got a simple control loop and I need to track the elapsed time of each loop.  Duration will be anything from 2u-sec to 500 u-sec.  I'm wondering if there's a system clock tic register or countdown timer I can read each time around the loop to calculate the elapsed time.  I'm sure I could setup a dedicated timer and reset it each time through but thought there might be an easier way reading some SysClock register.   Any thoughts on this?

Thanks

    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    Posted on January 16, 2018 at 02:52

    DWT_CYCCNT ?

    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 *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register

    uint32_t x, y delta;

    *SCB_DEMCR |= 0x01000000;

    *DWT_CYCCNT = 0; // reset the counter

    *DWT_CONTROL |= 1 ; // enable the counter

    ...

        x = *DWT_CYCCNT;

    ...

    Code Under Test

    ...

        y = *DWT_CYCCNT;

    delta = (y - x); // Clock ticks at CPU frequency, plus couple cycles of overhead, time 100 or 1000 iterations to make that insignificant

    4 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    January 16, 2018
    Posted on January 16, 2018 at 02:52

    DWT_CYCCNT ?

    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 *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register

    uint32_t x, y delta;

    *SCB_DEMCR |= 0x01000000;

    *DWT_CYCCNT = 0; // reset the counter

    *DWT_CONTROL |= 1 ; // enable the counter

    ...

        x = *DWT_CYCCNT;

    ...

    Code Under Test

    ...

        y = *DWT_CYCCNT;

    delta = (y - x); // Clock ticks at CPU frequency, plus couple cycles of overhead, time 100 or 1000 iterations to make that insignificant

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    David Pekin
    Senior
    January 16, 2018
    Posted on January 16, 2018 at 22:48

    Thanks Clive.  I added that code and the counter value never changes (at least as viewed from a debugging breakpoint).   Does the setup have to be done each time?  I should be able to read the cycle count register each iteration without re-enabling anything, right? I can save the count value last time through and calculate the delta betweeen the last iteration and now (taking into account wraps). 

    I've not been able to find those register addresses in the F303RE manual (RM0316) or in any of the source files.  Is there any documentation on this?

    Thanks again.

    Tesla DeLorean
    Guru
    January 16, 2018
    Posted on January 16, 2018 at 23:54

    The count will wrap in just shy of a minute at 72 MHz

    If you do the math as described the unsigned numbers will handle a start/stop that span the wrapping point without additional code changes.

    The CYCCNT is part of the DWT (debug/watchpoint/trace) unit of the micro-controller. The ST CM3/CM4 designs all have this unit. You enable the counter once, it free runs from then on. Check the ARM TRM for details of the CYCCNT registers and associated ones.

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