安信可经验分享 | 仅保持蓝牙连接状态下的低功耗的实现,适用于ESP32/ESP32C3/ESP32S3系列模组二次开发

一、前言

安信可ESP32/ESP32C3/ESP32S3系列模组都有三种低功耗模式:
• Modem-sleep 模式:CPU 可运行,时钟频率可配置。Wi-Fi 及 Bluetooth LE 的基带和射频关闭,但 Wi-Fi
或 Bluetooth LE 可保持连接。
• Light-sleep 模式:CPU 暂停运行。任何唤醒事件(MAC、主机、RTC 定时器或外部中断)都会唤醒芯片。
Wi-Fi 或 Bluetooth LE 可保持连接。
• Deep-sleep 模式:CPU 和大部分外设都会掉电,只有 RTC 存储器处于工作状态。Wi-Fi 连接数据存储在RTC 中。

功耗Modem-sleep > Light-sleep > Deep-sleep(详细功耗数据见规格书);其中 Modem-sleep和 Light-sleep两种模式下可以保持 Wi-Fi或 Bluetooth LE 。

本文介绍Light-sleep下如何保持蓝牙连接。

二、硬件准备

ESP32/ESP32C3/ESP32S3系列模组进入Light-sleep都需要硬件上需要外加 32 kHz 的外部晶振,否则 Light-sleep 模式不会生效,32 kHz 的外部晶振具体接哪些引脚见规格书管脚描述。
32.768 kHz 晶振选择要求:
• 等效内阻 (ESR) ⩽ 70 kΩ;
• 两端负载电容值根据晶振的规格要求进行配置。
• 并联电阻 R4 用于偏置晶振电路,电阻值要求 5 MΩ < R10 ⩽ 10 MΩ,该电阻一般无需上件。
在这里插入图片描述

三、目标芯片选择

esp32系列模组:

 idf.py set-target esp32

esp32c3系列模组:

 idf.py set-target esp32c3 

esp32s3系列模组:

 idf.py set-target esp32s3

四、menuconfig配置项

在工程目录下运行

idf.py menuconfig

ESP32系列模组 操作说明

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

扫描二维码关注公众号,回复: 13773344 查看本文章

⚫ Component config → Bluetooth,勾选 Bluetooth

⚫ Component config → Bluetooth → Bluetooth controller→ MODEM SLEEP Options → 勾选 Bluetooth modem sleep

⚫ Component config → Bluetooth → Bluetooth controller → MODEM SLEEP Options → Bluetooth low power clock → External 32kHz crystal

⚫ Component config → Bluetooth → Bluedroid Options → Bluetooth Low Energy

⚫ Component config → Bluetooth → Bluedroid Options → 勾选 Include GATT server module (GATTS)

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

⚫ Component config → FreeRTOS → Tick rate (Hz) 改为 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

ESP32C3系列模组 操作说明

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

⚫ Component config → Bluetooth

⚫ 勾选 Bluetooth

⚫ Component config → Bluetooth → Bluetooth controller(ESP32C3 Bluetooth Low Energy) → MODEM SLEEP Options → 勾选 Bluetooth modem sleep

⚫ Component config → Bluetooth → Bluetooth controller(ESP32C3 Bluetooth Low Energy) → MODEM SLEEP Options → Bluetooth low power clock → External 32kHz crystal

⚫ Component config → Bluetooth → Bluedroid Options → Enable BLE 4.2 features

⚫ Component config → Bluetooth → Bluedroid Options → Bluetooth Low Energy

⚫ Component config → Bluetooth → Bluedroid Options → 勾选 Include GATT server module (GATTS)

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

⚫ Component config → FreeRTOS → Tick rate (Hz) 改为 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

ESP32S3系列模组 操作说明

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

⚫ Component config → Bluetooth

⚫ 勾选 Bluetooth

⚫ Component config → Bluetooth → Bluetooth controller→ MODEM SLEEP Options → 勾选 Bluetooth modem sleep

⚫ Component config → Bluetooth → Bluetooth controller → MODEM SLEEP Options → Bluetooth low power clock → External 32kHz crystal

⚫ Component config → Bluetooth → Bluedroid Options → Enable BLE 4.2 features

⚫ Component config → Bluetooth → Bluedroid Options → Bluetooth Low Energy

⚫ Component config → Bluetooth → Bluedroid Options → 勾选 Include GATT server module (GATTS)

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

⚫ Component config → FreeRTOS → Tick rate (Hz) 改为 1000

⚫ Component config → FreeRTOS → 勾选 Tickless idle support

五、设置广播和连接参数

初始化电源管理:

#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_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

设置广播间隔:
在程序中有个结构体 adv_params ,其中有两个变量 .adv_int_min 和 .adv_int_max,这 两个参数影响着广播间隔,可设置范围为 0x20~0x4000,广播时间间隔为 Time = N * 0.625 ms。

static esp_ble_adv_params_t adv_params = {
    
    
    .adv_int_min        = 0x640,
    .adv_int_max        = 0x640,   //1s
    .adv_type           = ADV_TYPE_IND,
    .own_addr_type      = BLE_ADDR_TYPE_PUBLIC,
    .channel_map        = ADV_CHNL_ALL,
    .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};

连接成功后更新连接间隔,连接间隔为 Time = N * 1.25ms。

esp_ble_conn_update_params_t conn_params = {
    
    0};
memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t));
/* For the IOS system, please reference the apple official documents about the ble connection parameters restrictions. */
conn_params.latency = 0;
conn_params.max_int = 0x320;    
conn_params.min_int = 0x320;    // 连接间隔 0x320*1.25ms = 1000ms
conn_params.timeout = 400;    // timeout = 400*10ms = 4000ms
ESP_LOGI(TAG, "ESP_GATTS_CONNECT_EVT, conn_id %d, remote %02x:%02x:%02x:%02x:%02x:%02x:",
        param->connect.conn_id,
        param->connect.remote_bda[0], param->connect.remote_bda[1], param->connect.remote_bda[2],
        param->connect.remote_bda[3], param->connect.remote_bda[4], param->connect.remote_bda[5]);
gl_profile_tab[PROFILE_A_APP_ID].conn_id = param->connect.conn_id;
//start sent the update connection parameters to the peer device.
esp_ble_gap_update_conn_params(&conn_params);

完整示例代码:

git clone https://e.coding.net/axk/ai-thinker-open-sdk/BLE_POWER_SAVE.git    //基于esp-idf-release/v4.4分支

六、功耗测试

部分app不支持模组设置的连接间隔,建议使用nRF connet App做连接测试。
功耗设置相关:

  1. 广播间隔越长功耗越低,从手机App发起连接到连接成功的时间越长
  2. 连接间隔越长功耗越低,丢包的概率越高

用户需要根据自己的实际应用场景来设置合适的广播间隔和连接间隔。这里设置几组不同的广播间隔和连接间隔测试功耗,得到的平均功耗如下 (办公室环境测试所得,不同环境测试数据有差异):

系列模组 / 广播间隔 100ms 500ms 1000ms 2000ms
ESP32 9.5ma 2.7ma 1.75ma 1.3ma
ESP32C3 6.3ma 1.4ma 820ua 500ua
ESP32S3 9ma 2ma 1ma 700ua
系列模组 / 连接间隔 100ms 500ms 1000ms 2000ms
ESP32 6.6ma 2.0ma 1.6ma 1.2ma
ESP32C3 4.2ma 1.0ma 657ua 458ua
ESP32S3 6.5ma 1.5ma 948ua 640ua

部分功耗数据图
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
联系我们
官方官网:https://www.ai-thinker.com
开发DOCS:https://docs.ai-thinker.com
官方论坛:http://bbs.ai-thinker.com
技术支持:[email protected]

猜你喜欢

转载自blog.csdn.net/Boantong_/article/details/123793362