Systick sync with TIM3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-24 8:36 PM - last edited on ‎2025-06-25 1:18 AM by Andrew Neil
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.
But for some reason this will not stay in synch with systick. They are drifting constantly apart.
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??
Solved! Go to Solution.
- Labels:
-
SysTick
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 7:25 AM - edited ‎2025-06-25 7:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-24 8:37 PM
I should add that if I set the ARR to 500 then they stay in synch.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-24 10:03 PM
Well, then SysTick divider is set incorrectly.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 2:11 AM
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.
Saket_Om
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 3:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 6:31 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 6:56 AM
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 7:22 AM
Time to check the actual ARR and systick register settings.
Can't always trust Cube. Although this one is surprising.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 7:25 AM - edited ‎2025-06-25 7:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-06-25 7:49 AM
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.
