Last active
July 1, 2024 08:56
-
-
Save glegrain/3f4410b280db568981301be2f9fa1fab to your computer and use it in GitHub Desktop.
Cortex-M Cycle Counter counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uint32_t cycles; | |
cycles = 0; | |
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; | |
#ifdef STM32F7 | |
DWT->LAR = 0xC5ACCE55; | |
#endif | |
DWT->CYCCNT = 0; | |
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; | |
/* Process ... */ | |
DWT->CTRL &= !DWT_CTRL_CYCCNTENA_Msk; | |
cycles = DWT->CYCCNT; | |
float seconds = (float) cycles / HAL_RCC_GetSysClockFreq(); | |
// float seconds = (float) cycles / SystemCoreClock; | |
printf("%d cycles ", (int) cycles); | |
printf("(%0.4f ms @ %0.1f MHz).\n", (float) seconds * 1000.0f, SystemCoreClock / 1e6f); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment