1 year, 11 months ago.

Playing with sleep modes.

Why does this code perform as it does. It starts out in a sleep mode pulling around 600uA as measured on the CPU on a NUCLEO-L152RE. It seems to take close to 30 seconds for the thread waiting on an event to fire, turning the LED on. Once on, the MCU is pulling around 3.3 mA. However, it never goes back to sleep and stays on forever. It seems to me that it should loop back around and wait for another event to fire and go back to sleep mode until this happens.

#include "mbed.h"
DigitalOut pin(LED1);
LowPowerTicker ticker;
EventFlags event;
Thread t1;

void setLED() {
    while (true) {
        event.wait_any(0x01);
        pin = !pin;
    }
}

void blink() {
    event.set(0x01);
}

int main() {

    t1.start(setLED);
    ticker.attach(blink,5.0);

    while(1) {
        sleep();
    }
}

Replacing the call to sleep in the main thread with wait(osWaitForever) it performs as expected, however also never goes to sleep since sleep is not called. Is this a bug in mbed?

1 Answer

1 year, 11 months ago.

Hi Kevin,

The reason why there is a difference when using sleep() vs wait(osWaitForever) is that sleep will just do a normal sleep, while wait(osWaitForever) will try to do a deep sleep. Deep Sleep will shut down all the hardware except for the Low Power Tickers, while the normal sleep will use the microsecond tickers. The microsecond tickers will use a lot more power than the low power tickers, but it will be more accurate. However, since you trigger an interrupt every 5s, the lp power ticker would do just fine.

Please let me know if you have any questions!

- Peter Nguyen, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!

I'm still confused. Whether I do sleep or wait(osWaitForever) the timer should trigger blink, which then sends an event to a waiting thread to toggle the pin. When using sleep(), nothing happens. According to what you have described I should see the same behavior either way, just differences in power usage.

posted by Kevin McQuown 21 Aug 2018

Hi Kevin, Sorry, I forgot to mention that you need to initialize microsecond tickers for the sleep to work properly. Sleep() function rely on microsecond ticker and not low power ticker. Deep sleep is going to rely on lower power ticker.

-Peter

posted by Peter Nguyen 21 Aug 2018