Some ARM Cortex-Ms have a DWT (Data Watchpoint and Trace) unit implemented, and it has a nice feature in that unit that counts execution cycles. The DWT is usually implemented on most Cortex-M3, M4, and M7 devices, including the NXP Kinetis or LPC devices.
Execution profiling tools like the SEGGER SystemView are using it to measure the time used for code execution. This post is about how to use it directly from the application code or to enable cycle counting and inspect it during debugging.
Registers and Access Functions
The DWT is usually implemented in Cortex-M3 or higher, but not on Cortex-M0(+). To use the feature, I need to have access to several debug registers. You might use CMSIS-Core header files for this, but as there are very few registers, in case CMSIS-Core is not used, here are the needed defines I’m going to use:
/* DWT (Data Watchpoint and Trace) registers, only exists on ARM Cortex with a DWT unit */ /*!< DWT Control register */ #define KIN1_DWT_CONTROL (*((volatile uint32_t*)0xE0001000)) /*!< CYCCNTENA bit in DWT_CONTROL register */ #define KIN1_DWT_CYCCNTENA_BIT (1UL<<0) /*!< DWT Cycle Counter register */ #define KIN1_DWT_CYCCNT (*((volatile uint32_t*)0xE0001004)) /*!< DEMCR: Debug Exception and Monitor Control Register */ #define KIN1_DEMCR (*((volatile uint32_t*)0xE000EDFC)) /*!< Trace enable bit in DEMCR register */ #define KIN1_TRCENA_BIT (1UL<<24)
To use the registers, I have defined a set of ‘function-like’ macros I can use in my application code:
/*!< TRCENA: Enable trace and debug block DEMCR (Debug Exception and Monitor Control Register */ #define KIN1_InitCycleCounter() \ KIN1_DEMCR |= KIN1_TRCENA_BIT /*!< Reset cycle counter */ #define KIN1_ResetCycleCounter() \ KIN1_DWT_CYCCNT = 0 /*!< Enable cycle counter */ #define KIN1_EnableCycleCounter() \ KIN1_DWT_CONTROL |= KIN1_DWT_CYCCNTENA_BIT /*!< Disable cycle counter */ #define KIN1_DisableCycleCounter() \ KIN1_DWT_CONTROL &= ~KIN1_DWT_CYCCNTENA_BIT /*!< Read cycle counter register */ #define KIN1_GetCycleCounter() \ KIN1_DWT_CYCCNT
To use the cycle counting feature, the DWT has to be configured and enabled. If you are connecting to the target with a debugger, then this is usually already enabled by the debugger. To make it work with no debug session active, I have to initialize it in the code first.
uint32_t cycles; /* number of cycles */ KIN1_InitCycleCounter(); /* enable DWT hardware */ KIN1_ResetCycleCounter(); /* reset cycle counter */ KIN1_EnableCycleCounter(); /* start counting */ foo(); /* call function and count cycles */ cycles = KIN1_GetCycleCounter(); /* get cycle counter */ KIN1_DisableCycleCounter(); /* disable counting if not used any more */
Cycle Counter With a Debugger
Monitoring the cycle counter during a debug session is easy: Add the following expression to the ‘Expressions’ view:
With this, it shows the current cycle counter:
To make it even easier to use, I have extended the KinetisTools component with the needed macros and functions. This component will be available with the next release:
If your ARM Cortex-M has a DWT, you can use the cycle counter to measure the cycles spent executing code. That could be used for delay loops or to measure execution time.
- DEMCR register: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337e/CEGHJDCF.html
- DWT Registers: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0439b/BABJFFGJ.html
- DWT Control Register: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0337e/ch11s05s01.html