If your Cortex M microcontroller have DWT (Data Watchpoint and Trace) unit, you can use its register to count the number of cycles in which some code is executed. This could be useful for performance measuring. Simple cycle counter on ARM microcontroller having DWT unit could be implemented like this:
#include <stdint.h>
volatile uint32_t count = 0;
// addresses of registers
volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000;
volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xE0001004;
volatile uint32_t *DEMCR = (uint32_t *)0xE000EDFC;
// enable the use DWT
*DEMCR = *DEMCR | 0x01000000;
// Reset cycle counter
*DWT_CYCCNT = 0;
// enable cycle counter
*DWT_CONTROL = *DWT_CONTROL | 1 ;
// some code here
// .....
// number of cycles stored in count variable
count = *DWT_CYCCNT;
Tried on TI's Stellaris M3 and M4 family. Here is the list of the DWT Registers. Detailed description of these registers and DEMCR (Debug Exception and Monitor Control Register).
#include <stdint.h>
volatile uint32_t count = 0;
// addresses of registers
volatile uint32_t *DWT_CONTROL = (uint32_t *)0xE0001000;
volatile uint32_t *DWT_CYCCNT = (uint32_t *)0xE0001004;
volatile uint32_t *DEMCR = (uint32_t *)0xE000EDFC;
// enable the use DWT
*DEMCR = *DEMCR | 0x01000000;
// Reset cycle counter
*DWT_CYCCNT = 0;
// enable cycle counter
*DWT_CONTROL = *DWT_CONTROL | 1 ;
// some code here
// .....
// number of cycles stored in count variable
count = *DWT_CYCCNT;
Tried on TI's Stellaris M3 and M4 family. Here is the list of the DWT Registers. Detailed description of these registers and DEMCR (Debug Exception and Monitor Control Register).