STM32 gotchas
118. DWT_CYCCNT does not work in some sleep modes

DWT (Debug Watchpoint and Trace) unit is one of the debug units of the Cortex-M processor core. In Cortex-M3/M4/M7, it contains a 32-but counter, DWT_CYCCNT, primarily intended to provide a source for timestamps for the tracing functions. However, as it provides 32-bit resolution and runs at system clock frequency, and as it is accessible to the processor (with some added obstacles in the 'H7 family, but we are not going to discuss that here), users like to use it for general timing functions, too.

In this thread on STM32 forum, user VMiki.1 reported, that DWT_CYCCNT does not run, while the microcontroller is in some of the low-power (sleep) modes.

Experiment on STM32F429 revealed, that if SCB_SCR.SLEEPDEEP=0, DWT_CYCCNT continues to run at the system clock frequency even after executing WFE. If SCB_SCR.SLEEPDEEP=1, after WFE DWT_CYCCNT stops running until wakeup; but if DBGMCU_CR.DBG_STOP is set (which enables debugger to keep running while debugging sleep modes at the cost of higher power consumption), DWT_CYCCNT continues to run at cca 16MHz, probably HSI clock.