Skip to main content
Graduate
October 14, 2024
Question

Read Systick Timer Value Directly for 1ms Timebase

  • October 14, 2024
  • 4 replies
  • 3755 views

Hi, I am working on a project on the STM32H730 that runs a high priority audio interrupt thread as well as a lower priority asynchronous loop for user interface processing and a 1ms SysTick interrupt for user interface processes that need some accuracy in the timing.

I'm running into an issue where if my audio processing interrupt takes longer than 1ms (as is the case doing intensive processing at a 32kHz sample rate with a 128 sample buffer size) to execute SysTick interrupts no longer happen reliably once per ms. 

I imagine I can recreate this functionality by setting a timer to increment once per millisecond independent of the CPU and referencing this value whenever I need an accurate time reference. 

 

What function can I call to get the value of SysTick directly to compare to a global timer variable? Does this sound like the right approach? 

    This topic has been closed for replies.

    4 replies

    Graduate II
    October 14, 2024

    Hi,

    Are you using HAL and/or a RTOS?

    Kind regards
    Pedro

    EPala.2Author
    Graduate
    October 15, 2024

    HAL

    Graduate II
    October 15, 2024

    Hi,

    You'll most likely find that HAL is putting an overhead into your process you can't afford...

    I suggest using one of the hardware timers instead for your 1mS. Set the timer it up with MX using either HAL or LL, but use the timer's registers directly in your main() loop.

    I hope that helps.

    Kind regards
    Pedro

    Graduate II
    October 15, 2024

    You can change the interrupt priorities for each interrupt. Right now they are all defaulted to 0.

    KarlYamashita_0-1728951600414.png

     

    EPala.2Author
    Graduate
    October 15, 2024

    I don't necessarily want to set the 1ms tick function to a higher priority than the audio routine. The audio processing is very runtime critical so having a fully independent timer variable I can simply reference would work a lot better. 

    I understand I could probably make the 1ms tick function just increment the global timer variable with no other processing, and do anything relying on the millisecond time base in the asynchronous loop, but this would be functionally the same as having a fully independent timer peripheral increment once per 1ms while also adding interrupt overhead to my program. Seems more ideal in the long term to set it up with the independent timer and not use an interrupt.

    Graduate II
    October 16, 2024

    Could you divide down TIM2 or TIM5, via prescaler, so they free run at some rate you can use instead of SysTick?

    The Prescaler is only 16-bit, but you might be able to select the clocking source, or APB dividers, to get you to around 64 MHz, that you could divide down to 1 KHz, and have HAL_GetTick read the TIMx->CNT ?

    EPala.2Author
    Graduate
    October 17, 2024

    Is there an easy way to calculate the frequency or period of the timer? The options CubeMX gives for this are rather limited to say the least:

    As you can see, I can select between 3 clock sources but there's no indication of what frequencies these are running at, also no indication of the eventual resulting frequency after the prescaler, the clock ui also makes it totally unclear which of the many internally generated clock values are being used as the "Internal Clock".

    I also am unable to search for anything in the clock configuration tab, my only option is to squint at this super dense graph while looking for information that I don't even know will be there.

    EPala2_2-1729190756362.png

     

    EPala2_1-1729190728014.png

     

     

    Super User
    October 17, 2024

    If there's a 16-bit timer available, you can make a free running 1 ms counter without any interrupts. Just read the timer counter, from any context. Intervals will be limited to 16 bit ( ~ 65 seconds). With a 32-bit timer intervals will be much longer, if anyone ever needs this.

     

    EPala.2Author
    Graduate
    October 17, 2024

    Are you able to share what the MX configuration for setting up a 1ms timer looks like? 

    As I stated in my last post, the MX UI is unfortunately not very intuitive for how to achieve this. It does not display a clear period or frequency value for the clock source or for the timer post prescaler.

    Graduate II
    October 18, 2024

    @EPala.2 wrote:

    Are you able to share what the MX configuration for setting up a 1ms timer looks like? 

    As I stated in my last post, the MX UI is unfortunately not very intuitive for how to achieve this. It does not display a clear period or frequency value for the clock source or for the timer post prescaler.


    Your screen shot shows the APBx as 275MHz. Simple math.

    0.001(second) * 275MHz = 275000 (counter period).

     

    275000 is within 4294967295 (32bit counter) so prescaler can stay at zero.

    Auto-reload enabled, count down, NVIC enable, Update event.  

    If you want to count up, then that's simple math too.