Anxinke experience sharing | The realization of low power consumption in the state of WiFi staying connected, suitable for the secondary development of ESP32/ESP32C3/ESP32S3 series modules

I. Introduction

ESP32/ESP32C3/ESP32S3 series modules have three low-power modes:
• Modem-sleep mode: The CPU can run and the clock frequency can be configured. Wi-Fi and Bluetooth LE baseband and radio are turned off, but Wi-Fi
or Bluetooth LE can remain connected.
• Light-sleep mode: CPU suspends operation. Any wake-up event (MAC, host, RTC timer or external interrupt) will wake up the chip.
Wi-Fi or Bluetooth LE to stay connected.
• Deep-sleep mode: The CPU and most peripherals are powered down, only the RTC memory is active. Wi-Fi connection data is stored in the
RTC.
Power consumption Modem-sleep > Light-sleep > Deep-sleep (see the specification for detailed power consumption data); in which, Wi-Fi or Bluetooth LE can be maintained in Modem-sleep and Light-sleep modes.
This article introduces how to keep WIFI connection under Light-sleep.

2. Hardware preparation

ESP32/ESP32C3/ESP32S3 series modules do not require a 32 kHz external crystal oscillator, but using a 32 kHz external crystal oscillator will consume much less power than without an external 32 kHz crystal oscillator. See the pin description in the specification for the specific pins of the 32 kHz external crystal oscillator.
32.768 kHz crystal oscillator selection requirements:
• Equivalent internal resistance (ESR) ⩽ 70 kΩ;
• The load capacitance value at both ends is configured according to the crystal oscillator specifications.
• The parallel resistor R4 is used to bias the crystal oscillator circuit. The resistance value is required to be 5 MΩ < R10 ⩽ 10 MΩ. Generally, this resistor does not need an upper part.
insert image description here

3. Target chip selection

esp32 series modules:

 idf.py set-target esp32

esp32c3 series modules:

 idf.py set-target esp32c3 

esp32s3 series modules:

 idf.py set-target esp32s3

Four, menuconfig configuration items

Run in the project directory

idf.py menuconfig

ESP32 series module operation instructions

⚫ Component config → ESP32-Specific → RTC clock source → External 32kHz crystal

⚫ Component config → Power Management → 勾选 Support for power management

⚫ Component config → FreeRTOS → Tick rate (Hz) changed to 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

ESP32C3 series module operation instructions

⚫ Component config → ESP32C3-Specific → RTC clock source → External 32kHz crystal

⚫ Component config → Power Management → 勾选 Support for power management

⚫ Component config → FreeRTOS → Tick rate (Hz) changed to 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

ESP32S3 series module operation instructions

⚫ Component config → ESP32S3-Specific → RTC clock source → External 32kHz crystal

⚫ Component config → Power Management → 勾选 Support for power management

⚫ Component config → FreeRTOS → Tick rate (Hz) changed to 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

5. Code

5.1 Initialize power management:

#if CONFIG_PM_ENABLE
    // Configure dynamic frequency scaling:
    // maximum and minimum frequencies are set in sdkconfig,
    // automatic light sleep is enabled if tickless idle support is enabled.
#if CONFIG_IDF_TARGET_ESP32
    esp_pm_config_esp32_t pm_config = {
    
    
#elif CONFIG_IDF_TARGET_ESP32S2
    esp_pm_config_esp32s2_t pm_config = {
    
    
#elif CONFIG_IDF_TARGET_ESP32C3
    esp_pm_config_esp32c3_t pm_config = {
    
    
#elif CONFIG_IDF_TARGET_ESP32S3
    esp_pm_config_esp32s3_t pm_config = {
    
    
#endif
            .max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
            .min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
            .light_sleep_enable = true
#endif
    };
    ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
#endif // CONFIG_PM_ENABLE

5.2 Set Listen-Interval

Before setting up, let's understand the four basic concepts of wifi power saving mode:

1、TIM(traffic indication message)

There is a TIM information in each Beacon frame, which is mainly used by the AP to announce which STA under its jurisdiction has information currently cached in the AP, and the TIM contains a Bitmap control field, which is a maximum of 251 bytes , each bit maps to one STA, and when it is 1, it means that the STA corresponding to this bit has information in the cache of the AP.

2、DTIM(Delivery Traffic Indication Message )

DTIM is used for multi-point applications in traditional power saving mode, that is, the AP sends multicast traffic according to the interval by setting the interval of DTIM (the default is a beacon time, 100ms).
This value will not affect the transmission of unicast traffic. It will not be affected if users who do not enable PS use multicast, but it will affect the transmission of multicast data received by users who have enabled PS. If it is set too small, it will not work. If the power saving effect is too large, it may affect the quality of multicast communication. This process is a trial-error adjustment process. It can only be adjusted one by one to achieve the best power saving effect. It can achieve the best power saving effect without affecting the application.
DTIM=1 means that each beacon contains DTIM, DTIM=2 means that every two beacon contains one DTIM, and so on.

3. Beacon-Interval (beacon interval)

This value becomes larger, which helps the client to save power.
This value becomes smaller, which helps to improve the connection speed of the client side. Reduced the buffer frame load of the base station.
The general default is 100mS.

4. Listen-Interval, (STA is the period during which the Client receives Beacon)

The period of the AP broadcasting Beacon is Beacon-Interval, and the STA can freely choose an integer multiple of Beacon-Interval as its own Listen-Interval, such as 10.

The STA receives the Beacon every Listen-Interval and decodes the TIM in it. If the TIM indicates that there is no data buffer, the STA can immediately switch to the Doze state. If the TIM indicates that it has data buffer, the STA will send a campaign control packet Poll to the AP. After receiving the Poll, the AP can send a data packet buffered for it to the source STA of the Poll.

After understanding the above concepts, we set the value of listen_interval in the code

static void wifi_power_save(void)
{
    
    
    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
    
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
		wifi_config_t wifi_config = {
    
    
   		 .sta = {
    
    
         	.ssid = "AIOT@Aithinker",    //设置WiFi名称
         	.password = "12345678",	//设置WiFi密码
         	.listen_interval = 10,  //listen_interval=10 即10个Beacon-Interval,每个Beacon-Interval默认为100ms
   		 },
	};	
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());

    ESP_LOGI(TAG, "esp_wifi_set_ps().");
    esp_wifi_set_ps(WIFI_PS_MAX_MODEM);
}

Complete example code:
https://github.com/espressif/esp-idf/tree/master/examples/wifi/power_save

6. Power consumption test

The following data is obtained by plugging in a 32.768K crystal oscillator in a shielded room

series module ESP32 ESP32C3 ESP32S3
DTIM10 power consumption 1.4ma 681ua 1.1ma

insert image description here

insert image description here

insert image description here
Contact us
Official website: https://www.ai-thinker.com
DOCS development: https://docs.ai-thinker.com
Official forum: http://bbs.ai-thinker.com
Technical support: support@aithinker .com

Guess you like

Origin blog.csdn.net/Boantong_/article/details/123873182