Does HAL_GetTick() return ticks or milliseconds? (and how to measure in microseconds)

后端 未结 6 1237
一整个雨季
一整个雨季 2021-02-07 11:56

I\'m new to using HAL functions. The description of the function HAL_GetTick() says that it \"provides a tick value in millisecond

6条回答
  •  [愿得一人]
    2021-02-07 12:38

    I had the same problem, but then found a library function in PlatformIO, which returns the microseconds.

    uint32_t getCurrentMicros(void)
    {
      /* Ensure COUNTFLAG is reset by reading SysTick control and status register */
      LL_SYSTICK_IsActiveCounterFlag();
      uint32_t m = HAL_GetTick();
      const uint32_t tms = SysTick->LOAD + 1;
      __IO uint32_t u = tms - SysTick->VAL;
      if (LL_SYSTICK_IsActiveCounterFlag()) {
        m = HAL_GetTick();
        u = tms - SysTick->VAL;
      }
      return (m * 1000 + (u * 1000) / tms);
    }
    

    It is located in ~/.platformio/packages/framework-arduinoststm32/libraries/SrcWrapper/src/stm32/clock.c

    But it looks like STM32CubeIde doesn't have it, so I just copied it from PlatformIO. Also I had to copy the LL_SYSTICK_IsActiveCounterFlag() function:

    static inline uint32_t LL_SYSTICK_IsActiveCounterFlag(void)
    {
      return ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == (SysTick_CTRL_COUNTFLAG_Msk));
    }
    

提交回复
热议问题