Skip to content

Instantly share code, notes, and snippets.

@glegrain
Last active July 1, 2024 08:56
Show Gist options
  • Save glegrain/3f4410b280db568981301be2f9fa1fab to your computer and use it in GitHub Desktop.
Save glegrain/3f4410b280db568981301be2f9fa1fab to your computer and use it in GitHub Desktop.
Cortex-M Cycle Counter counter
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