Clean transition from automatic light sleep to deep sleep

boarchuz
Posts: 610
Joined: Tue Aug 21, 2018 5:28 am

Clean transition from automatic light sleep to deep sleep

Postby boarchuz » Fri Sep 06, 2019 8:23 pm

I'm wondering if there's a 'proper' way to do so. Without any intervention, the ESP32 will immediately wake after esp_deep_sleep_start() with reason: ESP_SLEEP_WAKEUP_TIMER (from the automatic light sleep scheduler).

I've been getting around this so far with a simple:
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
esp_deep_sleep_start();
This is fine for now as I don't need to use a timer wakeup from deep sleep, however I'm sure there must be a better way. Setting a new power management config with light sleep disabled has no effect.


boarchuz
Posts: 610
Joined: Tue Aug 21, 2018 5:28 am

Re: Clean transition from automatic light sleep to deep sleep

Postby boarchuz » Sat Sep 07, 2019 4:52 am

Doesn't appear to be.
Minimal:

Code: Select all

#include "sdkconfig.h"

#include "esp_sleep.h"
#include "esp_pm.h"

void app_main()
{
    printf("Wakeup cause: %d\n", esp_sleep_get_wakeup_cause());

    esp_pm_config_esp32_t pm_config_ls_enable = {
        .max_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ,
        .min_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ,
        .light_sleep_enable = true
    };
    ESP_ERROR_CHECK(esp_pm_configure(&pm_config_ls_enable));

    vTaskDelay(5000 / portTICK_PERIOD_MS);

    esp_pm_config_esp32_t pm_config_ls_disable = {
        .max_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ,
        .min_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ,
    };
    ESP_ERROR_CHECK(esp_pm_configure(&pm_config_ls_disable));

    vTaskDelay(1000 / portTICK_PERIOD_MS);
    
    printf("Sleeping...\n");
    esp_deep_sleep_start();
}
Relevant config: (PlatformIO)

Code: Select all

#define CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ 160
#define CONFIG_FREERTOS_HZ 100
#define CONFIG_PM_ENABLE 1
#define CONFIG_FREERTOS_USE_TICKLESS_IDLE 1
#define CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP 2
Log:

Code: Select all

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5336
load:0x40078000,len:8816
ho 0 tail 12 room 4
load:0x40080400,len:6412
entry 0x40080748
I (30) boot: ESP-IDF 3.30202.190627 2nd stage bootloader
I (30) boot: compile time 14:22:04
I (30) boot: Enabling RNG early entropy source...
I (32) boot: SPI Speed      : 40MHz
I (36) boot: SPI Mode       : DIO
I (39) boot: SPI Flash Size : 4MB
I (42) boot: Partition Table:
I (44) boot: ## Label            Usage          Type ST Offset   Length
I (51) boot:  0 nvs              WiFi data        01 02 00009000 00004000
I (57) boot:  1 otadata          OTA data         01 00 0000d000 00002000
I (64) boot:  2 phy_init         RF data          01 01 0000f000 00001000
I (70) boot:  3 factory          factory app      00 00 00010000 00120000
I (77) boot:  4 ota_0            OTA app          00 10 00130000 00120000
I (83) boot:  5 ota_1            OTA app          00 11 00250000 00120000
I (90) boot:  6 storage          Unknown data     01 82 00370000 00090000
I (96) boot: End of partition table
E (100) esp_image: image at 0x130000 has invalid magic byte
W (105) esp_image: image at 0x130000 has invalid SPI mode 255
W (110) esp_image: image at 0x130000 has invalid SPI size 15
E (116) boot: OTA app partition slot 0 is not bootable
I (120) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x071bc ( 29116) map
I (139) esp_image: segment 1: paddr=0x000171e4 vaddr=0x3ffbdb60 size=0x0221c (  8732) load
I (142) esp_image: segment 2: paddr=0x00019408 vaddr=0x40080000 size=0x00400 (  1024) load
I (145) esp_image: segment 3: paddr=0x00019810 vaddr=0x40080400 size=0x06800 ( 26624) load
I (163) esp_image: segment 4: paddr=0x00020018 vaddr=0x400d0018 size=0x14570 ( 83312) map
I (192) esp_image: segment 5: paddr=0x00034590 vaddr=0x40086c00 size=0x05cf0 ( 23792) load
I (202) esp_image: segment 6: paddr=0x0003a288 vaddr=0x400c0000 size=0x00064 (   100) load
I (209) boot: Loaded app from partition at offset 0x10000
I (209) boot: Disabling RNG early entropy source...
I (210) cpu_start: Pro cpu up.
I (211) cpu_start: Starting app cpu, entry point is 0x40081098
I (0) cpu_start: App cpu up.
I (220) heap_init: Initializing. RAM available for dynamic allocation:
I (226) heap_init: At 3FFAE6E0 len 0000F480 (61 KiB): DRAM
I (231) heap_init: At 3FFC11F0 len 0001EE10 (123 KiB): DRAM
I (236) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (242) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (247) heap_init: At 4008C8F0 len 00013710 (77 KiB): IRAM
I (252) cpu_start: Pro cpu start user code
I (270) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Wakeup cause: 0
I (271) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: ENABLED
I (5281) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: DISABLED
Sleeping...
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5336
load:0x40078000,len:8816
ho 0 tail 12 room 4
load:0x40080400,len:6412
entry 0x40080748
I (31) boot: ESP-IDF 3.30202.190627 2nd stage bootloader
I (31) boot: compile time 14:22:04
I (31) boot: Enabling RNG early entropy source...
I (33) boot: SPI Speed      : 40MHz
I (37) boot: SPI Mode       : DIO
I (40) boot: SPI Flash Size : 4MB
I (43) boot: Partition Table:
I (45) boot: ## Label            Usage          Type ST Offset   Length
I (52) boot:  0 nvs              WiFi data        01 02 00009000 00004000
I (58) boot:  1 otadata          OTA data         01 00 0000d000 00002000
I (65) boot:  2 phy_init         RF data          01 01 0000f000 00001000
I (71) boot:  3 factory          factory app      00 00 00010000 00120000
I (78) boot:  4 ota_0            OTA app          00 10 00130000 00120000
I (84) boot:  5 ota_1            OTA app          00 11 00250000 00120000
I (91) boot:  6 storage          Unknown data     01 82 00370000 00090000
I (97) boot: End of partition table
E (101) esp_image: image at 0x130000 has invalid magic byte
W (106) esp_image: image at 0x130000 has invalid SPI mode 255
W (111) esp_image: image at 0x130000 has invalid SPI size 15
E (117) boot: OTA app partition slot 0 is not bootable
I (122) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x071bc ( 29116) map
I (140) esp_image: segment 1: paddr=0x000171e4 vaddr=0x3ffbdb60 size=0x0221c (  8732) load
I (143) esp_image: segment 2: paddr=0x00019408 vaddr=0x40080000 size=0x00400 (  1024) load
I (146) esp_image: segment 3: paddr=0x00019810 vaddr=0x40080400 size=0x06800 ( 26624) load
I (164) esp_image: segment 4: paddr=0x00020018 vaddr=0x400d0018 size=0x14570 ( 83312) map
I (193) esp_image: segment 5: paddr=0x00034590 vaddr=0x40086c00 size=0x05cf0 ( 23792) load
I (203) esp_image: segment 6: paddr=0x0003a288 vaddr=0x400c0000 size=0x00064 (   100) 
I (210) boot: Loaded app from partition at offset 0x10000
I (210) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (212) cpu_start: Starting app cpu, entry point is 0x40081098
I (0) cpu_start: App cpu up.
I (221) heap_init: Initializing. RAM available for dynamic allocation:
I (227) heap_init: At 3FFAE6E0 len 0000F480 (61 KiB): DRAM
I (232) heap_init: At 3FFC11F0 len 0001EE10 (123 KiB): DRAM
I (237) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (242) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (248) heap_init: At 4008C8F0 len 00013710 (77 KiB): IRAM
I (253) cpu_start: Pro cpu start user code
I (270) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Wakeup cause: 4
I (272) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: ENABLED
I (5282) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: DISABLED
Sleeping...
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5336
load:0x40078000,len:8816
ho 0 tail 12 room 4
load:0x40080400,len:6412
entry 0x40080748
I (31) boot: ESP-IDF 3.30202.190627 2nd stage bootloader
I (31) boot: compile time 14:22:04
I (31) boot: Enabling RNG early entropy source...
I (33) boot: SPI Speed      : 40MHz
I (37) boot: SPI Mode       : DIO
I (40) boot: SPI Flash Size : 4MB
I (43) boot: Partition Table:
I (45) boot: ## Label            Usage          Type ST Offset   Length
I (52) boot:  0 nvs              WiFi data        01 02 00009000 00004000
I (58) boot:  1 otadata          OTA data         01 00 0000d000 00002000
I (65) boot:  2 phy_init         RF data          01 01 0000f000 00001000
I (71) boot:  3 factory          factory app      00 00 00010000 00120000
I (78) boot:  4 ota_0            OTA app          00 10 00130000 00120000
I (84) boot:  5 ota_1            OTA app          00 11 00250000 00120000
I (91) boot:  6 storage          Unknown data     01 82 00370000 00090000
I (97) boot: End of partition table
E (101) esp_image: image at 0x130000 has invalid magic byte
W (106) esp_image: image at 0x130000 has invalid SPI mode 255
W (111) esp_image: image at 0x130000 has invalid SPI size 15
E (117) boot: OTA app partition slot 0 is not bootable
I (122) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x071bc ( 29116) map
I (140) esp_image: segment 1: paddr=0x000171e4 vaddr=0x3ffbdb60 size=0x0221c (  8732) load
I (143) esp_image: segment 2: paddr=0x00019408 vaddr=0x40080000 size=0x00400 (  1024) load
I (146) esp_image: segment 3: paddr=0x00019810 vaddr=0x40080400 size=0x06800 ( 26624) load
I (164) esp_image: segment 4: paddr=0x00020018 vaddr=0x400d0018 size=0x14570 ( 83312) map
I (193) esp_image: segment 5: paddr=0x00034590 vaddr=0x40086c00 size=0x05cf0 ( 23792) load
I (203) esp_image: segment 6: paddr=0x0003a288 vaddr=0x400c0000 size=0x00064 (   100) 
I (210) boot: Loaded app from partition at offset 0x10000
I (210) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (212) cpu_start: Starting app cpu, entry point is 0x40081098
I (0) cpu_start: App cpu up.
I (221) heap_init: Initializing. RAM available for dynamic allocation:
I (227) heap_init: At 3FFAE6E0 len 0000F480 (61 KiB): DRAM
I (232) heap_init: At 3FFC11F0 len 0001EE10 (123 KiB): DRAM
I (237) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (242) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (248) heap_init: At 4008C8F0 len 00013710 (77 KiB): IRAM
I (253) cpu_start: Pro cpu start user code
I (270) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Wakeup cause: 4
I (272) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: ENABLED
I (5282) pm_esp32: Frequency switching config: CPU_MAX: 160, APB_MAX: 160, APB_MIN: 160, Light sleep: DISABLED
Sleeping...
ets Jun  8 2016 00:22:57
The time spent in deep sleep correlates directly with this delay "vTaskDelay(5000 / portTICK_PERIOD_MS);", ie. in the above log, the ESP32 was in deep sleep for ~5 seconds; if I change it to "vTaskDelay(30000 / portTICK_PERIOD_MS);" the ESP32 is in deep sleep for ~30 seconds.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Clean transition from automatic light sleep to deep sleep

Postby WiFive » Sat Sep 07, 2019 6:02 am

Ok yes I think clearing wakeup triggers before switching modes is standard like

https://github.com/espressif/esp-idf/bl ... tem.c#L262

Who is online

Users browsing this forum: dzungpv, ok-home and 117 guests