This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

EK-TM4C123GXL, count clock cycles and derive execution time

Hi, I am fairly new to MCU development. I was playing around with the TivaWare Getting Started Wiki site, which actually is terrific!

However, there is one (probably simple) task that I couldn't resolve yet. I want to let the Tiva run at 50 MHz, count the clock cycles for a given function and derive from it the absolute time ins ms. I know that it should be possible to derive this information from CCS, but I would like to do it algorithmically to get to know the board better. So right now I am setting up the clock to run at 50 MHz and I use the SysTickEnable() function to count the cycle. I am not entirely sure but the SysTick function is like a timer that is decremented and I can use SysTickPeriodSet() to define the start of the decrementation process? But in order to derive the absolut time from the counted cycles I would need to know the cycles per second right? Also isn't there a chance that the SysTick counter overflows (or underflows)? Can someone help me out with this or tell me if I am on the right track?

Thank you!

unsigned long clockRate;
unsigned long ulValue;

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                     SYSCTL_OSC_MAIN);
clockRate = SysCtlClockGet();
UARTprintf("Clock Rate %d\n",clockRate);
SysTickPeriodSet(1000);
SysTickEnable();
ulValue = SysTickValueGet(); 
UARTprintf("SysTickValue %d\n",ulValue);

  • Hello Richard

    The SysTickCounter can be made to work from either the system clock (50MHz in this case) or the PIOSC (16MHz). The SysTick timer is a simple timer which gives an interrupt when it counts down to 0 and then reloads.

    You may use it to read the execution time of a function but there would be a delay involved when you read the counter. So getting the exact number of clocks to execute a function may not be absolute, but close enough.

    Regards
    Amit
  • Hi Amit,

    after reading your post I think SysTick seems to be the wrong approach to count clock cycles, isn't it? Could you give me a hint how what to strive for to get this working?
  • Hello Richard,

    I am not the expert at profiling execution time of the API, but some of the simpler hooks I use is a GPIO toggle when entering and exiting a function. I am still to use the CCS profiler tool.

    Regards
    Amit
  • Richard Laurent said:
    SysTick seems to be the wrong approach to count clock cycles

    Indeed that's true.   Use of one of the (many) MCU's Timers often proves more secure and less subject to, "roll-over."   (i.e. SysTick is 24 bit counter, MCU Timers include 32 & 64 bit versions)

    There's still a faster, easier, far better means to, "Count clock cycles!"   This is "automatically" achieved via one of the PRO IDEs (which support ARM MCUs from multiple vendors - not just one) and have long pre-existed the one here - and simply out-perform!   Our firm employs J-Link (JTAG/SWD probe) and IAR.   Even the "free, code-size limited" version of IAR is able to record and display your desired "cycle count" - w/NO/ZERO code effort on your part.

    That said - so narrow a focus upon just (one) aspect of any MCU seems unusual - and may not prove best for your MCU exploration...

  • Hi,

    If you search the Tiva forum for keywords "execution time" you will find out this link, related to subject. It is based on internal Cortex-M4 register(s), dedicated to that purpose. The result is in clock cycles, so to you can easily compute the time, based on your setting of clock frequency.

    Amit, maybe the definition of DWT_O_CYCCNT could be included into the next Tiva version?

  • Hello Petrei,

    The next TivaWare is already on the door step. It may have to wait for the release after that.

    Regards
    Amit
  • One hopes, "On the door-step" differs from, "Check's in the mail..."

  • Hello cb1,

    Yes it does. There is back-end data for the server to be created to pick up the new release.

    Regards
    Amit
  • new version? O_O (what are the odds that I come to the forum after so much time and randomly stumble on this particular thread to find this statement?)
  • Luis Afonso said:
    What are the odds that I come to the forum after so much time and randomly stumble...

    What are the odds that a guy migrates from Brazil - lands in your country - wants to develop tech - and you meet him?

    Sometimes being active, aware, curious, and "mobile" - substantially lowers such odds!   You serve as proof of that - do you not?

    BTW - guys have "publicly" squawked here - saying that "Luis" is enforcing "entry rules/code-words" to his blog!   (say that isn't so...)