Skip to main content
Graduate II
June 25, 2025
Solved

Systick sync with TIM3

  • June 25, 2025
  • 8 replies
  • 603 views

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??

    This topic has been closed for replies.
    Best answer by waclawek.jan

    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

    8 replies

    Carl_GAuthor
    Graduate II
    June 25, 2025

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

    Super User
    June 25, 2025

    Well, then SysTick divider is set incorrectly.

     

    JW

    Technical Moderator
    June 25, 2025

    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. 

     

    Carl_GAuthor
    Graduate II
    June 25, 2025

    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.

    Super User
    June 25, 2025

    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.

    Carl_GAuthor
    Graduate II
    June 25, 2025

    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.

    Graduate II
    June 25, 2025

    Time to check the actual ARR and systick register settings.

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

    Super User
    June 25, 2025

    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_GAuthor
    Graduate II
    June 25, 2025

     

    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.