We are looking for reducing power consumption in our PIC24F freeRTos application.
For reducing power consumption, we are currently looking for using the configUSETICKLESSIDLE freeRtos option. Since the current pic24f port does not support it yet, I was looking at adding the required portSUPPRESSTICKSAND_SLEEP() macro myself based on instructions provided on the freeRtos Low Power website (http://www.freertos.org/low-power-tickless-rtos.html).
There something I must be missing though. On that website, it says:
To avoid race conditions the RTOS scheduler is suspended before portSUPPRESSTICKSANDSLEEP() is called, and resumed when portSUPPRESSTICKSANDSLEEP() completes. This ensures application tasks cannot execute between the microcontroller exiting its low power state and portSUPPRESSTICKSAND_SLEEP() completing its execution.
It also says in the example implementation of portSUPPRESSTICKSANDSLEEP():
>/* Determine how long the microcontroller was actually in a low power
state for, which will be less than xExpectedIdleTime if the
microcontroller was brought out of low power mode by an interrupt
other than that configured by the vSetWakeTimeInterrupt() call.
Note that the scheduler is suspended before
portSUPPRESSTICKSANDSLEEP() is called, and resumed when
portSUPPRESSTICKSAND_SLEEP() returns. Therefore no other tasks will
execute until this function completes. */
ulLowPowerTimeAfterSleep = ulGetExternalTime();
Finally, from the vTaskSuspendAll() description:
>API functions that have the potential to cause a context switch (for example, vTaskDelayUntil(), xQueueSend(), etc.) must not be called while the scheduler is suspended.
Based on these previous citations, I can't understand how it can work when an other interrupt wakes up the cpu. Let's say I have a uart RX interrupt waking up the CPU, which calls taskYIELD() as recommended by freeRtos guidelines about ISR. Then it would perform a task context change going against the vTaskSuspendAll() documentation, right? Am I missing something or we really cannot use portSUPPRESSTICKSAND_SLEEP() when we have interrupts using freeRtos services that may provoke a task context?
Thanks for your help
When the scheduler is suspended interrupts remain enabled. When the
interrupt comes in the interrupt service routine executes and calls for a
re-schedule to switch to another task. The re-schedule code then
executes, but seeing the scheduler is suspended it does not actually
perform the context switch, but instead holds it pending. The ISR then
exits back to the task code. The task, which was in the
supporess-ticks-and-sleep function, exits the function, at which point the
scheduler is unsuspended. Inside the function that unsuspends the
scheduler it is recognised that a context switch is pending, and the
context switch is performed immediately that the scheduler is no longer
Does that answer your question?
Ok, makes sense! So if my understanding is correct, the statement in the vTaskSuspendAll() api doc
>API functions that have the potential to cause a context switch (for example, vTaskDelayUntil(), xQueueSend(), etc.) must not be called while the scheduler is suspended
is inaccurate since task context switch will simply pend until scheduler is resumed?
I think the limitation is that the task that calls vTaskSuspendAll() shouldn't call something that will cause a context switch, but FreeRTOS handles the cases where the request occurs inside an ISR.
One way to think of it is it is contradictory to both request no scheduling and at the same time do an operation that needs to schedule another task to perform.
Ok I understand. Many thanks for your clarifications!
No, the statement is correct, API functions that have the potential to
cause a context switch must not be called when the scheduler is
suspended as they function will return when it should not and there will
be a logic error. That is very different to an interrupt attempting to
cause a context switch, and having the context switch held pending.