1 year, 11 months ago.

How to get STM32L1 into sleep mode?

After watching the videos on this I was assuming that it should be supper simple to get the MCU into sleep mode. I am measuring the current draw on a NUCLEO-L152RE and am seeing 4.15mA using the following code.

#include <mbed.h>
int main() {
    while(1) {
        wait(osWaitForever);
    }
}

Since the idle thread has nothing to do, it should just enter deep sleep correct?

However this code shows the current draw drop to .69 mA. I didn't think we had to call sleep to get it to sleep with the new Mbed sleep capability?

#include <mbed.h>
int main() {
    while(1) {
        sleep();
        wait(osWaitForever);
    }
}

1 Answer

1 year, 11 months ago.

Hi Kevin,

There is a difference between sleep and wait. Sleep will actually turn off part of the MCU and only wake it back up when something triggers it, maybe a timer or an external device. Wait on the other does not turn off the MCU but it will either spin in a loop for a certain time or give up the MCU for different processes if you are running multiple threads.

Please let me know if you have any questions!

- Peter, team Mbed

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

Gotcha, so the main thread should call sleep. Then other threads that block based on an UART input such as getc will then allow the MCU to sleep until input is available?

posted by Kevin McQuown 16 Aug 2018

Well, it really depended on what you want to do. If your application is just waiting on an input that you don't know when it is coming in, then I would use sleep since it allowed your MCU to run at a lower power until it needs to wake up. Just make sure that you set up some wakeup interrupt that takes it out of sleep. On the other hand, I would use wait if you know how long the next input is.

-Peter

posted by Peter Nguyen 16 Aug 2018