From 90a0a83c0fdf8272e3477266bda5ee0f0183def6 Mon Sep 17 00:00:00 2001 From: Li Shuai Date: Fri, 20 Aug 2021 10:15:48 +0800 Subject: [PATCH] light sleep: separate sleep wifi/bt mac bb function --- components/esp_hw_support/CMakeLists.txt | 1 + .../include/esp_private/sleep_mac_bb.h | 43 +++++++ components/esp_hw_support/sleep_mac_bb.c | 108 ++++++++++++++++++ components/esp_hw_support/sleep_modes.c | 91 +-------------- 4 files changed, 154 insertions(+), 89 deletions(-) create mode 100644 components/esp_hw_support/include/esp_private/sleep_mac_bb.h create mode 100644 components/esp_hw_support/sleep_mac_bb.c diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index 58c8726ecd..077e6f6191 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -16,6 +16,7 @@ if(NOT BOOTLOADER_BUILD) "mac_addr.c" "sleep_modes.c" "sleep_gpio.c" + "sleep_mac_bb.c" "regi2c_ctrl.c") list(APPEND requires esp_ipc) else() diff --git a/components/esp_hw_support/include/esp_private/sleep_mac_bb.h b/components/esp_hw_support/include/esp_private/sleep_mac_bb.h new file mode 100644 index 0000000000..6b4019639d --- /dev/null +++ b/components/esp_hw_support/include/esp_private/sleep_mac_bb.h @@ -0,0 +1,43 @@ +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once +#include +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file sleep_mac_bb.h + * + * This file contains declarations of MAC and baseband power consumption related functions in light sleep mode. + */ + +#if CONFIG_MAC_BB_PD + +/** + * @brief A callback function completes MAC and baseband power down operation + * + * In light sleep mode, execute Wi-Fi and Bluetooth module MAC and baseband + * power down and backup register configuration information operations. + */ +void mac_bb_power_down_cb_execute(void); + +/** + * @brief A callback function completes MAC and baseband power up operation + * + * In light sleep mode, execute Wi-Fi and Bluetooth module MAC and baseband + * power up and restore register configuration information operations. + */ +void mac_bb_power_up_cb_execute(void); + +#endif // CONFIG_MAC_BB_PD + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_hw_support/sleep_mac_bb.c b/components/esp_hw_support/sleep_mac_bb.c new file mode 100644 index 0000000000..4c53852070 --- /dev/null +++ b/components/esp_hw_support/sleep_mac_bb.c @@ -0,0 +1,108 @@ +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#include "esp_attr.h" +#include "esp_sleep.h" +#include "soc/soc_caps.h" +#include "esp_private/sleep_mac_bb.h" +#include "sdkconfig.h" + +#if CONFIG_MAC_BB_PD + +#define MAC_BB_POWER_DOWN_CB_NO (2) +#define MAC_BB_POWER_UP_CB_NO (2) + +static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO]; +static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO]; + +esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb) +{ + int index = MAC_BB_POWER_DOWN_CB_NO; + for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) { + if (s_mac_bb_power_down_cb[i] == cb) { + return ESP_OK; + } + + if (s_mac_bb_power_down_cb[i] == NULL) { + index = i; + } + } + + if (index < MAC_BB_POWER_DOWN_CB_NO) { + s_mac_bb_power_down_cb[index] = cb; + return ESP_OK; + } + + return ESP_ERR_NO_MEM; +} + +esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb) +{ + for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) { + if (s_mac_bb_power_down_cb[i] == cb) { + s_mac_bb_power_down_cb[i] = NULL; + return ESP_OK; + } + } + return ESP_ERR_INVALID_STATE; +} + +void IRAM_ATTR mac_bb_power_down_cb_execute(void) +{ + for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) { + if (s_mac_bb_power_down_cb[i]) { + s_mac_bb_power_down_cb[i](); + } + } +} + +esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb) +{ + int index = MAC_BB_POWER_UP_CB_NO; + for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) { + if (s_mac_bb_power_up_cb[i] == cb) { + return ESP_OK; + } + + if (s_mac_bb_power_up_cb[i] == NULL) { + index = i; + } + } + + if (index < MAC_BB_POWER_UP_CB_NO) { + s_mac_bb_power_up_cb[index] = cb; + return ESP_OK; + } + + return ESP_ERR_NO_MEM; +} + +esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb) +{ + for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) { + if (s_mac_bb_power_up_cb[i] == cb) { + s_mac_bb_power_up_cb[i] = NULL; + return ESP_OK; + } + } + return ESP_ERR_INVALID_STATE; +} + +void IRAM_ATTR mac_bb_power_up_cb_execute(void) +{ + for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) { + if (s_mac_bb_power_up_cb[i]) { + s_mac_bb_power_up_cb[i](); + } + } +} + +#endif ///CONFIG_MAC_BB_PD diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index b3b98bc9ab..be331d05e1 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -61,12 +61,14 @@ #include "esp32s3/rom/cache.h" #include "esp32s3/rom/rtc.h" #include "soc/extmem_reg.h" +#include "esp_private/sleep_mac_bb.h" #elif CONFIG_IDF_TARGET_ESP32C3 #include "esp32c3/clk.h" #include "esp32c3/rom/cache.h" #include "esp32c3/rom/rtc.h" #include "soc/extmem_reg.h" #include "esp_heap_caps.h" +#include "esp_private/sleep_mac_bb.h" #elif CONFIG_IDF_TARGET_ESP32H2 #include "esp32h2/clk.h" #include "esp32h2/rom/cache.h" @@ -184,95 +186,6 @@ static void touch_wakeup_prepare(void); static void esp_deep_sleep_wakeup_prepare(void); #endif -#if CONFIG_MAC_BB_PD -#define MAC_BB_POWER_DOWN_CB_NO 2 -#define MAC_BB_POWER_UP_CB_NO 2 -static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO]; -static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO]; - -esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb) -{ - int index = MAC_BB_POWER_DOWN_CB_NO; - for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) { - if (s_mac_bb_power_down_cb[i] == cb) { - return ESP_OK; - } - - if (s_mac_bb_power_down_cb[i] == NULL) { - index = i; - } - } - - if (index < MAC_BB_POWER_DOWN_CB_NO) { - s_mac_bb_power_down_cb[index] = cb; - return ESP_OK; - } - - return ESP_ERR_NO_MEM; -} - -esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb) -{ - for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) { - if (s_mac_bb_power_down_cb[i] == cb) { - s_mac_bb_power_down_cb[i] = NULL; - return ESP_OK; - } - } - return ESP_ERR_INVALID_STATE; -} - -static IRAM_ATTR void mac_bb_power_down_cb_execute(void) -{ - for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) { - if (s_mac_bb_power_down_cb[i]) { - s_mac_bb_power_down_cb[i](); - } - } -} - -esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb) -{ - int index = MAC_BB_POWER_UP_CB_NO; - for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) { - if (s_mac_bb_power_up_cb[i] == cb) { - return ESP_OK; - } - - if (s_mac_bb_power_up_cb[i] == NULL) { - index = i; - } - } - - if (index < MAC_BB_POWER_UP_CB_NO) { - s_mac_bb_power_up_cb[index] = cb; - return ESP_OK; - } - - return ESP_ERR_NO_MEM; -} - -esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb) -{ - for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) { - if (s_mac_bb_power_up_cb[i] == cb) { - s_mac_bb_power_up_cb[i] = NULL; - return ESP_OK; - } - } - return ESP_ERR_INVALID_STATE; -} - -static IRAM_ATTR void mac_bb_power_up_cb_execute(void) -{ - for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) { - if (s_mac_bb_power_up_cb[i]) { - s_mac_bb_power_up_cb[i](); - } - } -} -#endif ///CONFIG_MAC_BB_PD - /* Wake from deep sleep stub See esp_deepsleep.h esp_wake_deep_sleep() comments for details. */