Skip to main content
Graduate II
June 1, 2024
Question

Periodic Timing on STM32: WKUP Interrupt vs. RTC ALARM A Mask

  • June 1, 2024
  • 1 reply
  • 1534 views

Hi,

I want to use the RTC timer on the STM32WB to run FreeRTOS in both normal and STOP modes (For HAL time base I use Systick). When the MCU has no work, it goes into STOP mode. If the MCU needs to wake up for timing, like from a FreeRTOS timer, I set the RTC ALARM A to wake it up. When the processor is running, it needs to tick for FreeRTOS every 1 ms.

My question is, should I use the WKUP (wake-up periodic interrupt) to tick every 1 ms, or set ALARM A with a mask for periodic timing like this:

LL_RTC_ALMA_SetMask(RTC, LL_RTC_ALMA_MASK_ALL);
LL_RTC_ALMA_SetSubSecondMask(RTC, 3);

 Which option is better? Or is there a downside to one of them?

It looks to me that every option is possible.

Best regards Jan

    This topic has been closed for replies.

    1 reply

    ST Employee
    June 5, 2024

    Hello @JR2963

    Typically, the RTC and WUT are used for wake-up intervals in the order of seconds or milliseconds but not as high as 1 kHz...

    If you use the RTC for the 1 ms tick(which is a precise method), waking up the MCU every ms will keep it active most of the time, increasing power consumption and defeating the purpose of entering stop mode in the first place 

    Consider using the Tickless idle mode,  you could set the RTC or WUT to wake up the MCU every 10 ms, when the MCU wakes up, it can handle multiple ticks simultaneously to catch up on the FreeRTOS timing, this will reduce consumption and still "normally" meet you requirements 

    Check:

     

    Explorer II
    June 6, 2024

    Hi @Sarra.S 

    Thank you for the comment. I am actually using tickless mode in FreeRTOS. My system is set up with the Systick timer (1 ms period) as the HAL base timer, with the highest possible IRQ priority (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY). All other interrupts (buttons, DMA, etc.) have a priority one level lower. This is because if I call a function like HAL_xy..() in an IRQ, some functions wait for a timeout which must be updated - thus, Systick triggers an interrupt every 1 ms. I hope my reasoning is correct.

    RTC: When FreeRTOS is running, it ticks every 1 ms using the RTC with the lowest possible priority (configLIBRARY_LOWEST_INTERRUPT_PRIORITY). When it goes to tickless mode, I set ALARM_A to the desired wake-up time. After waking up, I set the RTC back to a 1 ms tick.

    Now, how to set the RTC to tick every 1 ms? Here are my calculations:

    For ALARM_A with LSE at 32768 kHz (STM32WB):
    - PREDIV_S = 32767 -> 1 tick = 1/32768 = 30.5 µs
    - For 1 ms = 32768 * 0.001 = 32.768 ticks, rounded to 33 ticks
    - If we set the Subsecond mask to 5 with LL_RTC_ALMA_SetSubSecondMask(RTC, tickless_RTC_SS_MASK_BITS), we get periodic calls every 33 ticks (almost 1 ms)

    For WUT:
    - Source is LSE: 32768, DIV set to 16 -> clk = 2048 Hz, 1 tick = 1/2048 = 488.5 µs
    - Auto-reload 2-1 = 1, thus 2/2048 = 976 µs, almost 1 ms

    If we use internal RC at 32 kHz:
    For ALARM_A:
    - PREDIV_S = 31999 -> 1 tick = 1/32000 = 31.25 µs
    - For 1 ms = 32000 * 0.001 = exactly 32 ticks
    - If we set the Subsecond mask to 5 with LL_RTC_ALMA_SetSubSecondMask(RTC, tickless_RTC_SS_MASK_BITS), we get periodic calls every 32 ticks (exactly 1 ms)

    For WUT:
    - Source LSI: 32000, DIV = 16 -> clk = 2000 Hz, 1 tick = 1/2000 = 500 µs
    - Auto-reload 2-1, thus 2/2000 = exactly 1 ms


    If I haven't made a mistake, it should not matter whether I use WUT or ALARM_A for a 1 ms interval. The main factor is the clock source used: 32 kHz or 32.768 kHz. Is that correct?

    ST Employee
    June 6, 2024

    Hello @JJRR

    I took a look at your calculations, yes that's correct! 

    you'll have to base your choice on other factors such as power consumption, complexity, and availability of the RTC or WUT

    However, my point in the first comment was that waking up every 1 ms, even in tickless mode, may not provide significant power savings, I'm sure you are aware of that!