What are the processes that run after a wake-up from deep sleep?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
appels
Posts: 13
Joined: Thu Dec 03, 2020 3:14 pm

What are the processes that run after a wake-up from deep sleep?

Post by appels » Mon Jan 11, 2021 1:21 pm

Hi,

I'm doing a thesis on the power consumption of MicroPython VS Arduino.
As a test I wrote a program that wakes the microcontroller up and puts it back to sleep (with a timer for 1s) and just keeps on doing this.
I used an ESP32_DevKitC_V4 with the WROOM-32 module.

This is the Arduino code:

Code: Select all

void setup() {
  esp_sleep_enable_timer_wakeup(1000000);
  esp_deep_sleep_start();
}

void loop() {}
And this is the MicroPython code:

Code: Select all

import machine
machine.deepsleep(1000)
When I measure the current (with an otii arc) I get a recurring pattern of the current doing something when the microcontroller is active and then the current being low and constant for 1 second during the deep sleep. When I zoom in on one active region I get the following graph (green = Arduino and red = MicroPython):

Image

Some notes:
- I wrote some letters in yellow on the image to refer to later.
- The deep sleep current is the same for both Arduino and MicroPython: 2.8 mA
- With the Arduino code it's active for 190 ms
- With the MicroPython code it's active for 690 ms
- I know that the code that I wrote only starts being executed µs before it goes back to sleep. (So right before D with Arduino and right before H with MicroPython) I know this because I tested this by adding some delay in the code before going to sleep and I would always see this graph + the delay in the active region.


My questions:
- In the beginning (from A to C) the current is the same. Is this because the microcontroller is in the bootloader? And if so does the bootloader stop at point C or point B (or another point).
- Why has the Arduino code a higher (max) current draw then the MicroPython code? (between C and D)

Most importantly:
- What processes are ran after the bootloader and before the (user) code is executed? And is it possible to determine when these start and stop on the graph?


Thanks in advance!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: What are the processes that run after a wake-up from deep sleep?

Post by jimmo » Mon Jan 11, 2021 11:46 pm

appels wrote:
Mon Jan 11, 2021 1:21 pm
My questions:
- In the beginning (from A to C) the current is the same. Is this because the microcontroller is in the bootloader? And if so does the bootloader stop at point C or point B (or another point).
Yes, not just the bootloader, but also the initialisation code for FreeRTOS.

Yes, any "application" built for the ESP32 with the ESP-IDF is actually just a small part of the overall firmware image.

Unlike (say) STM32, where MicroPython runs "bare metal", on ESP32 it's just a task being run by FreeRTOS.

The question you're really asking is "where is the point where "app_main" starts running (i.e. the point where ESP-IDF transfers control to the application).

I would recommend setting a pin high at the very start of app_main (on both MicroPython and Arduino) and you can confirm this. I would also strongly recommend adding a third comparison -- an app built directly with the IDF (e.g. one of their sample apps).
appels wrote:
Mon Jan 11, 2021 1:21 pm
- Why has the Arduino code a higher (max) current draw then the MicroPython code? (between C and D)
There could be lots of reasons, but one suggestion is that MicroPython runs almost entirely on one core, and it's the same core as the rest of the system services. (The ESP32 is dual core).

Perhaps Arduino runs the user code (i.e. the sketch) on the second core.
appels wrote:
Mon Jan 11, 2021 1:21 pm
Most importantly:
- What processes are ran after the bootloader and before the (user) code is executed? And is it possible to determine when these start and stop on the graph?
Yep, you'll need to instrument the IDF, Arduino, and MicroPython code. Either add something that will be obvious on the power draw (i.e. sleeps), or toggle pins.

It's also worth breaking down the various stages of the MicroPython initialisation until the user code runs. Also compare against frozen (i.e. no compilation or loading bytecode), vs .mpy (load bytecode only), vs .py (compile and load).

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: What are the processes that run after a wake-up from deep sleep?

Post by pythoncoder » Tue Jan 12, 2021 6:14 am

I have done similar measurements on a Pyboard 1.1.

The deepsleep current is very much lower. The total energy used and time spent after wakeup is much reduced if frozen bytecode is used: the compiler doesn't run, and the code runs directly from flash with no filesystem operations.
Peter Hinch
Index to my micropython libraries.

appels
Posts: 13
Joined: Thu Dec 03, 2020 3:14 pm

Re: What are the processes that run after a wake-up from deep sleep?

Post by appels » Tue Jan 12, 2021 10:00 am

pythoncoder wrote:
Tue Jan 12, 2021 6:14 am
I have done similar measurements on a Pyboard 1.1.

The deepsleep current is very much lower. The total energy used and time spent after wakeup is much reduced if frozen bytecode is used: the compiler doesn't run, and the code runs directly from flash with no filesystem operations.

Funny you should mention, that is the other board I'm comparing it with. And it has indeed a much lower sleep current and execution time.

Image

(blue = Arduino, yellow = MicroPython)

The deep sleep current is less than 40 µA.
Pyboard with MicroPython is active for 64 ms.
Pyboard with Arduino is active for 3-15 ms (depending where you chose to draw the end line, with the ripples)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: What are the processes that run after a wake-up from deep sleep?

Post by pythoncoder » Tue Jan 12, 2021 5:52 pm

Interesting that the active time of a Pyboard is under 1/10 of that of the ESP32. Presumably this is the startup and shutdown time of FreeRTOS.

Pyboard with Arduino? You may be the first to get Arduino code to run on a Pyboard. How did you go about it?

FWIW I have an application where three Pyboards in remote locations wake up once an hour and communicate with a fourth using NRF24L01+ radios. The remote units run for a year or two on a set of four AA cells.
Peter Hinch
Index to my micropython libraries.

Post Reply