jefftenney / LPTIM-Tick

FreeRTOS Tick/Tickless via LPTIM
42 stars 2 forks source link

Sleep indefinitely when all tasks are suspended using `eNoTasksWaitingTimeout` #20

Open timrid opened 1 week ago

timrid commented 1 week ago

I have an application in which the controller should sleep most of the time and only needs to be woken up by external interrupts. In my application, all tasks are then suspended and wait indefinitely until an interrupt occurs. Then, however, the LPTIM does not have to run and generate an interrupt every 2 seconds, as this is bad for power consumption.

If all tasks are suspended, FreeRTOS recognizes this and returns eNoTasksWaitingTimeout when eTaskConfirmSleepModeStatus() is called. However, as I currently see it in your code, this flag is not evaluated. Is it possible to add this feature?

jefftenney commented 1 week ago

Thanks @timrid for the suggestion. Can you give me some details about your application? Specifically, what is your energy consumption in between the 2-second wakeups? Do you have an RTC? Hypothetically if lptimTick.c did stop LPTIM for eNoTasksWaitingTimeout, would you prefer it also stop LSE? Do you have any need to timestamp external events with the FreeRTOS tick count?

Olstyle commented 1 week ago

From a theoretical point of view it sounds like a useful improvement to deactivate LPTIM in that case. But I think from an application point of view, if your are really fighting for the last uA, you will mainly want a callback there which can then be used to switch to Standby or Shutdown mode. Actually I do have applications I still want to send further down to standby mode which could use that callback.

jefftenney commented 1 week ago

@Olstyle thanks for the comment -- I agree.

In Stop mode, turning off LPTIM only saves 0.15 uA, and that includes the wake-ups every 2 seconds. A typical STM32L Stop 2 mode draws ~1.1 uA, and a real-life system usually draws a few more uA for the complete application circuit in its quiescent state and for self discharge of the battery. Shaving 0.15 uA off the total 3 or 4 uA might not be worthwhile. And by keeping LPTIM going, we can support users who expect the FreeRTOS tick count to measure real time.

Now if we look beyond Stop mode, the picture changes a little. Standby or Shutdown modes can arguably move the needle by shaving off nearly all of the Stop mode current. With the existing FreeRTOS interface for configPRE_SLEEP_PROCESSING(), the developer would call eTaskConfirmSleepModeStatus() from within the pre-sleep macro. The result of that function call would dictate the selection of either stop mode or standby/shutdown mode.