Cycle Counting on an ARM Cortex-M With DWT

DZone 's Guide to

Cycle Counting on an ARM Cortex-M With DWT

You can tap into cycle counting on most ARM Cortex-Ms using the built-in Data Watchpoint and Trace feature, which lets you measure the cycles spent executing code.

· IoT Zone ·
Free Resource

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.

Image title

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() \

/*!< Reset cycle counter */
#define KIN1_ResetCycleCounter() \

/*!< Enable cycle counter */
#define KIN1_EnableCycleCounter() \

/*!< Disable cycle counter */
#define KIN1_DisableCycleCounter() \

/*!< Read cycle counter register */
#define KIN1_GetCycleCounter() \

Typical Usage

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:

(*((volatile uint32_t*)0xE0001004))

With this, it shows the current cycle counter:

Cycle Counter in Expressions View

Cycle counter in expressions view

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:

Cycle Counting Functions

Cycle counting functions


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.

Happy cycling!


arm cortex-m, cycle counting, dwt, iot, tutorial

Published at DZone with permission of Erich Styger , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}