5 years, 11 months ago.

Using Sleep, Deepsleep with MBED OS 5

I have spent a few days trying to find information/examples on a 'simple' sleep/deepsleep function using OS 5xxx.

The code snip explains where I'm going with this. I do not need threads or anything like that, just simply sleep the MCU as and when I decide.

I'm using the K22F target, using an earlier version MBED-2 (MBED-DEV, rev 148:21d94c4+), deepsleep was 4.6uA which is as specified in the RM.

Question is has low power now been removed from MBED?

If not how can I use this function without over complicating the code and burning through an excessive amount of flash and ultimately slow the whole process down.

MBED-2 similar example used 22k Flash, 1.6k Ram. This MBED-OS5xxx uses 39k Flash, 9.3k Ram. This seems rather a lot of memory resource.

OS5xxx Deepsleep test

#include "mbed.h"

// 19/1/2019
// MBED-OS 5346:a8fc0c33
// FRDM-K22F board, measuring MPU P3v3_K22F on jumper J15
// FRDM-K22F DAPLink firmware v244

// Compile, load code and power cycle the FRDM board.

DigitalOut led1(LED1);


int main()
{

    while (true) {
        
    
        led1=1;     // RED led off, no peripheral power used.
        
        
        sleep();    // enter sleep, deep sleep perhaps, we don't know or have any control here.
       
              
        wait(10.0);  // wait for 10 seconds in sleep/deepsleep ?  11.236mA 
        
        led1=0;     // after 2 seconds wake and turn RED led on.
        
        wait(2.0);  // wait for one second with RED led on.  11.236mA 
        
        // loop round..
        
        // code runs, led cycles correctly but no change in power
    
    }
    
}

3 Answers

5 years, 11 months ago.

In the latest mbed OS 5, there seems to be no support for the use of previous versions of sleep status. Look at the problem, someone has proposed a new sleep API, but I have not found any introduction about it. If you want to use it, it is recommended that you do not update all libraries, which is useful for some programs. Some time ago, I looked for a long time to use the sleep state, using the previous mbed OS2 sleep program to be able to use.

5 years, 11 months ago.

Hi Paul,

In Mbed OS5 power management is automatically handled by the kernel so there is no need to make a direct call to sleep(). The sleep / power management documentation and API can be found here:

Based upon the specific target and your use of peripherals, the core might enter regular sleep or deep sleep. In OS5 wait() has recently been deprecated and it might not put the core into sleep state so that is likely why you are seeing a high current reading (based on the comments in your code). The sleep_for() and sleep_until() functions of ThisThread should be used as shown here:

Regards,

Ralph, Team Mbed

Thanks Ralph, I'll take a look and have a go, I've got some 'Bigger' boards.

I think OS5 is really for the 'Big Guns' of the MCU world which is great for those applications and TBH these boards aren't low power orientated .

OS2 needs a bit of TLC to get it back up to speed for those like me that really do not want a thread based system, easy to manipulate and want to go low on the power side of things. Otherwise to be brutal if this is not going to happen I will leave MBED and go down the Arduino route. Something I do not want to do!

Edit...

I think I can see the problem, the K22F is no longer supported by the vendor, 155 warnings during compile. I tried the STM32L476RG and that actually works with my example. Ralph's suggestion nearly works apart from this depreciated part:

snip

thread.signal_set(STOP_FLAG);

That only had 55 warnings, some were unreachable, perhaps these are for testing/development.

Looks like its going in the right direction.

posted by Paul Staron 24 Jan 2019

Hi Paul,

We understand your concerns about the memory footprint and low power. You can trim down OS5 and even remove the RTOS. Here is a good article describing that:

In the near future we'll even be making it easier to do so, so please have a bit more patience.

posted by Ralph Fulchiero 24 Jan 2019
5 years, 11 months ago.

Hi Paul,

the new sleep() works as good as the old deepsleep. I run the STM32L4 with a very few µA. You need to be careful with Timers because they block deepsleep, use LowPowerTimers instead.

Regards Helmut

Hi Helmut,

I to use the L4 with great results, I didn't realize the K22F was no longer supported. Tried again with the L476RG and that does work with my basic example and Ralph's suggestion, but I get a strange 620us power surge every 877mS in the order of a few mA's. Also I notice that its not running on the HSE 8 MHz crystal which would suggest only 48MHz? Are you finding this?

Regards Paul

posted by Paul Staron 25 Jan 2019

At present I use the L433 and the L432 MCUs which works fine with sleep(). I use mbed version 2 for memory reasons. However the mbed-os code is identical with mbed-os 5, it is the same but has less features.

posted by Helmut Tschemernjak 25 Jan 2019

I may be wrong but I think OS5 is targeted for this 'Big Guys' M4's and M7's with big resources (Flash and Ram) which is great, but we still need something that does not have RTOS and can squeeze that flash to a minimum, which ultimately results in speed. Lets face it remove the seats and doors from your car and it goes like stink, I know I tried it a few years ago:)

posted by Paul Staron 25 Jan 2019

Hi Helmut, as I am also working on STM32L433 for low power consumption purpose. Would you please provide an example of it? Thanks and Regards

posted by minh nam agi 18 Aug 2019