Topic : Counting the number of cycles to excecute the code in Ride7

Forum : ARM

Original Post
Post Information Post
January 30, 2011 - 5:23pm

I want to know if there is a way to find out how many cycles does the code take to excecute. Actually I have some
data processing functions and I want to know how many cycles does it take to process the data.

Toolchain : Ride7
Processor : STM32F107

Thx In Advance
Lokesh Siddhu

Post Information Post
January 31, 2011 - 10:03am
Raisonance Support Team

Hi Lokesh,

We will soon have some new features in Ride7-ARM that will permit direct count of the CPU cycles.

Meanwhile, here is a neat trick: You can use the Cortex-M3's internal cycles counter. You just need to set a bit to enable it, then you can read the number of executed cycles directly as a 32-bit word.

Here is a code sample that does it:

volatile unsigned long ctr = 0;  /* This is the cycles counter */
int main( void )
/* Define the cycle counter registers */
#define CYCLESCNT       (*(volatile unsigned long*)0xE0001004)
#define DWTCR           (*(volatile unsigned long*)0xE0001000)
#define DWTCR_CYCCNTENA 0x00000001

    /* Cycles counter enable: Set bit 0 of DWT Control Register */


    /* Read the cycles counter */
    ctr = CYCLESCNT;


This is a quick and dirty piece of code. It may be very helpful, however we give absolutely no guarantee that it will be fully functional, and you really should think twice when activating this feature in your project.

Other nice features of this counter are that:
- It is read-write, so you can imagine reading the value upon entry of an ISR, and write it back at the end of the ISR, so that the ISR cycles are not accounted.
- It is initialized to 0, so you can just read the counter value at the end of an execution, and it will give a good measure of the application execution time (useful for benchmarking).

Best Regards,

February 1, 2011 - 1:48pm

Thanks it works fine.

Lokesh Siddhu