How to get time intervals on STM32?

前端 未结 2 1352
旧时难觅i
旧时难觅i 2020-12-10 09:33

I want to measure how long does a single function take on STM32. The only thing I could find is SysTick_Handler. However, that is an periodic interrupt, but wha

相关标签:
2条回答
  • 2020-12-10 09:51

    First, enable the cycle counter once at startup:

    CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
    DWT->CYCCNT = 0;
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    

    then, you can access its value:

    unsigned long t1 = DWT->CYCCNT;
    /* do something */
    unsigned long t2 = DWT->CYCCNT;
    unsigned long diff = t2 - t1;
    

    It counts the elapsed cpu cycles, you have to divide it with the cpu clock frequency to get a value in seconds.

    As it's a 32 bit value, it can overflow quite fast at higher clock frequencies, e.g in 19.88 seconds at 216 MHz.

    0 讨论(0)
  • 2020-12-10 10:02

    If you've got HAL available try;

    int millis = HAL_GetTick();
    
    0 讨论(0)
提交回复
热议问题