I have a question about preventing (“turning off”) entry to sleep during specific idle periods. For example, suppose I am performing a network provisioning task which involves a lot of waiting during which the MCU could go to sleep. But, for simplicity and to prevent continuous sleep-wake cycles, I wish to have the idle task run (skipping entry to sleep mode) until the network provisioning completes or timesout. Essentially I would like to control (select) either entry to low-power sleep, or simply remain idling.
Here is a little background on my project: I am using Keil uVision v5.20 for development with FreeRTOS v9.0.0 on STM32L471 ARM Cortex M4 MCU. My RTOS project defines 5 tasks, 4 of which use a Queue and wait for events to process, and the fifth uses Task Notifications to block until something happens. Some relevant configuration:
#define configOVERRIDE_DEFAULT_TICK_CONFIGURTION 1 #define configUSE_TICKLESS_IDLE 2 #define configUSE_IDLE_HOOK 0
I use 32.768kHz oscillator to drive low-power timer and generate the RTOS tick. I have the RTOS tick independent of the MCU SYSCLK so I can achieve longer sleep periods, and also I need to keep SYSCLK running at a low frequency while in SLEEP. To do all this I had to implemented my own portSUPPRESS_TICKS_AND_SLEEP() for tickless idle.
Back to the question of selecting either idle or sleep: I am thinking of introducing a simple global variable into the idle task which will determine whether the tickless idle code is skipped or run.
I think it’s pretty simple but wouldn’t mind a little feedback about this approach or more generally how to control entry to sleep mode. (For example, if portSUPPRESS_TICKS_AND_SLEEP() is to be skipped, perhaps I should call vApplicationIdleHook() to increment an idle time counter that could be cleared when any task wakes. It could be used to ensure a “minimum idle time” before allowing entry to sleep in general circumstances.)
Thanks for any suggestions!