cancel
Showing results for 
Search instead for 
Did you mean: 

Using two time bases, SysTick and general-purpose timer

Kmax18
Senior

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.

3 REPLIES 3
TDK
Guru

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
Senior

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!

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".