阅读文档,要满足我们的需求,需要ble进入light-sleep模式,根据文档我们做了如下修改:
1.在开发板的GPIO0\GPIO1两个引脚上加入了外部32K的晶振,如附图所示:
2.已示例程序为基础:\ESP_IDF\examples\bluetooth\bluedroid\ble\gatt_server_service_table,
(1):Enable Power Management:
menuconfig ---> Component config ---> Power management --->
[*] Support for power management
(2):Enable Tickless Idle:
menuconfig ---> Component config ---> FreeRTOS --->
[*] Tickless idle support
(3) Minimum number of ticks to enter sleep mode for (NEW)
Note: Tickless idle needs to be enabled to allow automatic light sleep. FreeRTOS will enter light sleep if no tasks need to run
for 3(by default) ticks, that is 30ms if tick rate is 100Hz. Configure the FreeRTOS tick rate to be higher if you want to allow
shorter duration light sleep, for example:
menuconfig ---> Component config ---> FreeRTOS ->
(1000) Tick rate (Hz)
(3):Configure external 32.768Hz crystal as RTC clock source:
menuconfig ---> Component config ---> ESP32-specific --->
RTC clock source (External 32kHz crystal)
[*] Additional current for external 32kHz crystal
Note that the "additional current" option is a workaround for a hardware issue on ESP32 that the crystal can fail in oscillating.
Please enable this option when you use external 32kHz crystal. This hardware issue will be resolved in the next ECO chip.
(4):Enable Bluetooth modem sleep with external 32.768kHz crystal as low power clock:
menuconfig ---> Component config ---> Bluetooth ---> Bluetooth controller ---> MODEM SLEEP Options --->
[*] Bluetooth modem sleep
Bluetooth Modem sleep mode (ORIG mode(sleep with low power clock))
Bluetooth low power clock (External 32kHz crystal)
并在应用中增加了如下接口代码
Code: Select all
#include "esp_err.h"
#include "esp_pm.h"
esp_pm_config_esp32_t pm_config = {
.max_freq_mhz = 80, // e.g. 80, 160, 240
.min_freq_mhz = 10, // e.g. 40
.light_sleep_enable = true, // enable light sleep
};
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
另外有如下几个参数也在原有的example上做了修改:
Code: Select all
static esp_ble_adv_params_t adv_params = {
.adv_int_min =0x640,
.adv_int_max = 0x640,
.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,
};
Code: Select all
conn_params.latency = 0;
conn_params.max_int = 0x300; // max_int = 0x20*1.25ms = 40ms
conn_params.min_int = 0x10; // min_int = 0x10*1.25ms = 20ms
conn_params.timeout = 400; // timeout = 400*10ms = 4000ms
//start sent the update connection parameters to the peer device.
esp_ble_gap_update_conn_params(&conn_params);