Skip to main content
Kmax18
Senior II
January 5, 2022
Question

Using two time bases, SysTick and general-purpose timer

  • January 5, 2022
  • 3 replies
  • 819 views

I am using a STM32G431RB MCU and the STM32Cube_FW_G4_V1.5.0 package for motion control. In addition to the usual SysTick 1ms time base a second, faster time base is needed to run control loops. I plan to use a general-purpose timer (e.g. TIM2) to implement the fast time base.

Question: the SysTick uses the HAL functions (HAL_IncTick etc.). Is it advised to implement custom functions for the fast time base, or are additional libraries available, HAL or others? Thank you.

This topic has been closed for replies.

3 replies

TDK
January 5, 2022

If you need a second timer, start one in free-running mode and read it when necessary. Generally this would be done independently from SysTick.

You can also use the free-running 32-bit DWT timer for maximum resolution.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Kmax18
Kmax18Author
Senior II
January 5, 2022

Hello TDK, thank you for your helpful and quick response. Please comment on this idea, which I think is not unusual: the callback function for the free-running timer executes the velocity control, updates the finite state machine, etc. The timer frequency (period) provides a precise loop update rate, let's say 2000 Hz. The main() loop is not needed for this. Thank you!

TDK
January 5, 2022
That is a valid and common approach to doing PID type control on a microcontroller.
"If you feel a post has answered your question, please click ""Accept as Solution""."