cancel
Showing results for 
Search instead for 
Did you mean: 

Systick sync with TIM3

Carl_G
Senior II

I configured STM32CubeMX to have a timebase source as SysTick. I'm using this for my scheduler. its running at 1ms. I also configured tim3 for 1ms sortof.

Carl_G_0-1750822395221.png

But for some reason this will not stay in synch with systick. They are drifting constantly apart.

Carl_G_1-1750822499426.png

The green lines are racing across my screen. Even though they are 1ms period as well. But obviously they couldn't be. I just don't understand why. Or maybe I have an issue with my data??

1 ACCEPTED SOLUTION

Accepted Solutions
waclawek.jan
Super User

I've just realized you're using the timer in up-down counting ("center aligned") mode.

There, indeed, the period is given by (PSC+1)*(ARR*2). See Center-aligned PWM waveforms (ARR=8) figure in TIM chapter in RM.

JW

View solution in original post

9 REPLIES 9
Carl_G
Senior II

I should add that if I set the ARR to 500 then they stay in synch.

waclawek.jan
Super User

Well, then SysTick divider is set incorrectly.

 

JW

Saket_Om
ST Employee

Hello @Carl_G 

Please specify the STM32 series you are working on. 

If the clock sources for SysTick and TIM3 are not derived from the same base clock or are configured differently, they may not stay synchronized

Note that there is a driver that can be used generate systick from timer (stm32xxxx_hal_timebase_tim_template.c)). To do this you need to choose TIMx as timebase in CubeMX. 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
Carl_G
Senior II

Stm32G071. I checked systick in software and the debugger and it's 15999. I want to believe it's an illusion on my oscilloscope but the fact that setting the period to 500 works is confusing.

TDK
Super User

Setting to 500 instead of 499 works? What is the time scale on the plot? Which clock is "correct" and which is not?

Show the clock configuration tab.

 

To me, looks like one clock is 2x the speed of the other clock which is easily explained by a misunderstanding somewhere.

If you feel a post has answered your question, please click "Accept as Solution".

That's just the way I have tested.

In my scheduler I set and output at the beginning and clear the output at the end of the 5ms task. That task is based on the Systick.

The other is Tim3 which is a center aligned PWM that I trigger an interrupt with. I don't actually use the PWM output.  I toggle a port inside of its interrupt.

Timing is not 2x what I want. Time is correct but one is slightly off. At these speeds I don't think I have any accurate way to know which timer is correct.

Carl_G_0-1750859366588.png

I also had a long argument with ChatGPT about this. It was unable to conceptualize that 500 is aligned and 500-1 is not. I'm expecting to learn something new here because I don't see how this can be happening. Out of phase, yes. Drift, no. Unless I am programmatically changing a timer somewhere.

LCE
Principal II

Time to check the actual ARR and systick register settings.

Can't always trust Cube. Although this one is surprising.

waclawek.jan
Super User

I've just realized you're using the timer in up-down counting ("center aligned") mode.

There, indeed, the period is given by (PSC+1)*(ARR*2). See Center-aligned PWM waveforms (ARR=8) figure in TIM chapter in RM.

JW

Carl_G
Senior II

 

Ahh you nailed it @waclawek.jan  I knew I was going to learn something. With the up / down counter I should not do the -1.