One of the major concerns for embedded devices is the power consumption. If the device you’re designing will be battery powered, it’s indeed important to reduce as much as possible its power consumption to maximize the autonomy (= the working time before it’s necessary to replace or recharge the battery).
The esp32 chip offers 5 different power modes. The “normal” mode is named active mode; in this mode all the features of the chip are available. By starting to reduce the CPU speed and disabling some peripherals and cores, the chip switches to different power saving modes, as summarized in the following diagram:
In this first post about the esp32 power saving modes, I’ll explain the deep sleep mode.
The esp-idf framework actually supports two power saving modes: light sleep and deep sleep. Between the two, the deep sleep mode is the one which offers greater energy savings. In this mode, are turned off:
- both the CPUs
- most of the RAM memory
- all the peripherals
by default are instead kept active:
- the RTC controller
- the RTC peripherals, including the ULP coprocessor
- the RTC memories (slow e fast)
You can put the chip in deep sleep with the esp_deep_sleep_start() method, while it’s possible to wake up it via different events:
When the chip wakes up from deep sleep, a new boot sequence is performed. It’s therefore very important to understand that the execution of your program does not restart at the point where the esp_deep_sleep_start() method is called.
Let’s see how to configure and use two wake up events; in a future post I’ll write about touch pad and ULP.
The simplest wake up event is for sure the one which leverages a timer of the RTC controller. Thanks to the method:
esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
you can wake up the esp32 chip after the specified number of milliseconds. The method must be called before entering the deep sleep mode:
// wakeup after 10 seconds esp_sleep_enable_timer_wakeup(10000000); esp_deep_sleep_start();
In a previous post I’ve already blogged about the possibility to receive interrupts when a digital pin of the chip changes its status. We can leverage a similar functionality to wake up the chip from sleep.
With the method
esp_err_t esp_sleep_enable_ext0_wakeup(gpio_num_t gpio_num, int level)
you can enable the wake up if the specified pin (gpio_num) changes its status (level).
You can only use the pins with RTC function (0, 2, 4, 12-15, 25-27, 32-39) and the possible levels are 0 (= low) or 1 (high). If, for example, you want to wake up the chip from deep sleep if pin 4 has a low level, you’ll write:
The framework also offers a method to monitor different pins:
esp_err_t esp_sleep_enable_ext1_wakeup(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode)
the pins (this method also accepts only the ones specified above) must be specified in a bitmask and the wakeup modes are:
- ESP_EXT1_WAKEUP_ALL_LOW = wakeup when all the pins are low
- ESP_EXT1_WAKEUP_ANY_HIGH = wakeup when at least one pin is high
After the wake up…
If you configured more than one wake up event, you can know which specific event woke up the chip with:
The possible constants are:
For the ext1_wakeup event, a specific method is available to get the bitmask of the pins:
As explained above, in deep sleep mode the content of the RTC fast and RTC slow memories is preserved. You can therefore use those memory segments to store data that must be retained during the sleep.
To ask the compiler to store a variable in the RTC slow memory, you can use the RTC_DATA_ATTR attribute, or the RTC_RODATA_ATTR one if the variable is read only:
RTC_DATA_ATTR static time_t last;
I wrote a program (its source code is in my Github repository) to demonstrate the deep sleep mode and two different wake up events:
[youtube id=”j69Ulck-1Qs” width=”600″ height=”350″]