dis/enable peripherals on idle/wake

gmenhennitt wrote on Tuesday, March 18, 2014:

We run our application under the MSP430X port of FreeRTOS 8.0.0 in tickless mode. When we are idle we want to disable some peripherals (I2C, UART, timers) to save power. That’s relatively easy to do by adding code to vApplicationIdleHook(). However, we also need to do the opposite - wake up the peripherals when starting to execute a task (i.e. on becoming not idle). I added the following chunk of code at the bottom of xTaskIncrementTick().

    if(xSwitchRequired)
    {
        extern void vApplicationNotIdleHook();
        vApplicationNotIdleHook();
    }

just before the line

    return xSwitchRequired;
}

Obviously, I add a new hook function vApplicationNotIdleHook() that does the enabling.

Could anybody please suggest whether this is going to achieve what I want? Is there a better way to do it?

Thanks for any help,
Graham

davedoors wrote on Tuesday, March 18, 2014:

You can copy the tickless idle implementations for other chips http://www.freertos.org/low-power-tickless-rtos.html these have pre and post sleep macros to enable disabling then reenabling peripherals as you are trying to do.

If you want to do something in the idle hook then you could do something like

//Stop context switches so the idle task continues running after sleep
vTaskSuspendAll();

//Turn off what needs turning off
disable_some_peripherals();

//Call your sleep function
wait_for_interrupt();

//Reenable what needs reenabling
enable_some_peripherals();

//Allow context switching again to yield
xTaskResumeAll();

A hook in xTaskIncrementTick() is not going to execute until the next tick time and not at all if the clock used to create the tick is one of the things that you turn off before sleeping. Better to add a hook in the traceTASK_SWITCHED_IN macros, or another trace macro that runs when you leave sleep. Another options would be to somehow signal a wake event from the interrupt that causes the wake.

gmenhennitt wrote on Wednesday, March 19, 2014:

Thanks for replying, Dave.

It looks like there are a few problems with my implementation. The foremost of these is that I wasn’t actually running in tickless mode. I didn’t realise that the MSP430 port doesn’t implement this internally - I need to implement portSUPPRESS_TICKS_AND_SLEEP() myself (which I haven’t). If/when I do that, I’ll come back to this.

Thanks again,
Graham