fix(pm): add mac/bb power down/up prepare for fix esp32c6 pll issue

* switch root clk src to PLL for modem reg opt and added callback
* register power_down/power_up callback in ieee802154 driver for esp32c6
* remove software regdma opt in bt
This commit is contained in:
xiaqilin 2023-09-25 20:59:32 +08:00
parent c7a270f83d
commit 6ffc6a40a7
14 changed files with 183 additions and 56 deletions

View File

@ -488,9 +488,6 @@ IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg)
}
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
r_ble_rtc_wake_up_state_clr();
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
sleep_retention_do_extra_retention(true);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
esp_phy_disable(PHY_MODEM_BT);
#ifdef CONFIG_PM_ENABLE
@ -507,9 +504,6 @@ IRAM_ATTR void controller_wakeup_cb(void *arg)
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_acquire(s_pm_lock);
r_ble_rtc_wake_up_state_clr();
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG
sleep_retention_do_extra_retention(false);
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG */
#endif //CONFIG_PM_ENABLE
esp_phy_enable(PHY_MODEM_BT);
s_ble_active = true;
@ -570,12 +564,21 @@ esp_err_t controller_sleep_init(void)
if (rc != ESP_OK) {
goto error;
}
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
sleep_modem_register_mac_bb_module_prepare_callback(sleep_modem_mac_bb_power_down_prepare,
sleep_modem_mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
return rc;
error:
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
sleep_modem_unregister_mac_bb_module_prepare_callback(sleep_modem_mac_bb_power_down_prepare,
sleep_modem_mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
esp_sleep_disable_bt_wakeup();
esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set);
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
@ -592,6 +595,10 @@ error:
void controller_sleep_deinit(void)
{
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
sleep_modem_unregister_mac_bb_module_prepare_callback(sleep_modem_mac_bb_power_down_prepare,
sleep_modem_mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
r_ble_rtc_wake_up_state_clr();
esp_sleep_disable_bt_wakeup();
sleep_modem_ble_mac_modem_state_deinit();
@ -950,11 +957,6 @@ esp_err_t esp_bt_controller_disable(void)
esp_pm_lock_release(s_pm_lock);
#endif // CONFIG_PM_ENABLE
s_ble_active = false;
} else {
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
/* Avoid consecutive backup of register cause assertion */
sleep_retention_module_deinit();
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
}
ble_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;
return ESP_OK;

View File

@ -9,6 +9,7 @@
#include <stdbool.h>
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_sleep.h"
#ifdef __cplusplus
extern "C" {
@ -40,6 +41,42 @@ void mac_bb_power_up_cb_execute(void);
#endif // CONFIG_MAC_BB_PD
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
/**
* @brief Register sleep prepare callback for Bluetooth/IEEE802154 MAC and baseband
*
* @param pd_cb function to call when power down
* @param pu_cb function to call when power up
*/
void sleep_modem_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb);
/**
* @brief Unregister sleep prepare callback for Bluetooth/IEEE802154 MAC and baseband
*
* @param pd_cb function to call when power down
* @param pu_cb function to call when power up
*/
void sleep_modem_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb);
/**
* @brief MAC and baseband power up operation
*
* In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband
* power down and backup prepare operations.
*/
void sleep_modem_mac_bb_power_down_prepare(void);
/**
* @brief MAC and baseband power up operation
*
* In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband
* power up and restore prepare operations.
*/
void sleep_modem_mac_bb_power_up_prepare(void);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#if SOC_PM_SUPPORT_PMU_MODEM_STATE
/**

View File

@ -108,8 +108,6 @@ void sleep_retention_entries_get(sleep_retention_entries_t *entries);
* or false for restore to register from memory
*/
void sleep_retention_do_extra_retention(bool backup_or_restore);
void sleep_retention_module_deinit(void);
#endif
/**

View File

@ -28,6 +28,7 @@
#if SOC_PM_SUPPORT_PMU_MODEM_STATE
#include "soc/pmu_reg.h"
#include "esp_private/esp_pau.h"
#include "esp_private/esp_pmu.h"
#endif
static __attribute__((unused)) const char *TAG = "sleep_modem";
@ -36,9 +37,15 @@ static __attribute__((unused)) const char *TAG = "sleep_modem";
static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_freq_mhz);
#endif
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
static bool s_modem_sleep = false;
static uint8_t s_modem_prepare_ref = 0;
static _lock_t s_modem_prepare_lock;
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#if CONFIG_MAC_BB_PD
#define MAC_BB_POWER_DOWN_CB_NO (2)
#define MAC_BB_POWER_UP_CB_NO (2)
#define MAC_BB_POWER_DOWN_CB_NO (3)
#define MAC_BB_POWER_UP_CB_NO (3)
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];
@ -390,3 +397,78 @@ static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_f
}
}
#endif
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
void sleep_modem_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb)
{
_lock_acquire(&s_modem_prepare_lock);
if (s_modem_prepare_ref++ == 0) {
esp_register_mac_bb_pd_callback(pd_cb);
esp_register_mac_bb_pu_callback(pu_cb);
}
_lock_release(&s_modem_prepare_lock);
}
void sleep_modem_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb)
{
_lock_acquire(&s_modem_prepare_lock);
assert(s_modem_prepare_ref);
if (--s_modem_prepare_ref == 0) {
esp_unregister_mac_bb_pd_callback(pd_cb);
esp_unregister_mac_bb_pu_callback(pu_cb);
}
_lock_release(&s_modem_prepare_lock);
}
/**
* @brief Switch root clock source to PLL do retention and switch back
*
* This function is used when Bluetooth/IEEE802154 module requires register backup/restore, this function
* is called ONLY when SOC_PM_RETENTION_HAS_CLOCK_BUG is set.
* @param backup true for backup, false for restore
* @param cpu_freq_mhz cpu frequency to do retention
* @param do_retention function for retention
*/
static void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool))
{
#if SOC_PM_SUPPORT_PMU_MODEM_STATE
if (pmu_sleep_pll_already_enabled()) {
return;
}
#endif
rtc_cpu_freq_config_t config, pll_config;
rtc_clk_cpu_freq_get_config(&config);
rtc_clk_cpu_freq_mhz_to_config(cpu_freq_mhz, &pll_config);
rtc_clk_cpu_freq_set_config(&pll_config);
if (do_retention) {
(*do_retention)(backup);
}
rtc_clk_cpu_freq_set_config(&config);
}
void IRAM_ATTR sleep_modem_mac_bb_power_down_prepare(void)
{
if (s_modem_sleep == false) {
rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(true,
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
sleep_retention_do_extra_retention);
s_modem_sleep = true;
}
}
void IRAM_ATTR sleep_modem_mac_bb_power_up_prepare(void)
{
if (s_modem_sleep) {
rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(false,
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
sleep_retention_do_extra_retention);
s_modem_sleep = false;
}
}
#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD */

View File

@ -94,15 +94,11 @@ typedef struct {
uint32_t modules;
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
#define EXTRA_LINK_NUM (REGDMA_LINK_ENTRY_NUM - 1)
int extra_refs;
#endif
} sleep_retention_t;
static DRAM_ATTR __attribute__((unused)) sleep_retention_t s_retention = {
.highpri = (uint8_t)-1, .modules = 0
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
, .extra_refs = 0
#endif
};
#define SLEEP_RETENTION_ENTRY_BITMAP_MASK (BIT(REGDMA_LINK_ENTRY_NUM) - 1)
@ -505,37 +501,17 @@ uint32_t IRAM_ATTR sleep_retention_get_modules(void)
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
void sleep_retention_do_extra_retention(bool backup_or_restore)
{
_lock_acquire_recursive(&s_retention.lock);
if (s_retention.highpri < SLEEP_RETENTION_REGDMA_LINK_HIGHEST_PRIORITY ||
s_retention.highpri > SLEEP_RETENTION_REGDMA_LINK_LOWEST_PRIORITY) {
_lock_release_recursive(&s_retention.lock);
return;
}
const uint32_t clk_bug_modules = SLEEP_RETENTION_MODULE_BLE_MAC | SLEEP_RETENTION_MODULE_802154_MAC;
const int cnt_modules = __builtin_popcount(clk_bug_modules & s_retention.modules);
// Set extra linked list head pointer to hardware
pau_regdma_set_extra_link_addr(s_retention.lists[s_retention.highpri].entries[EXTRA_LINK_NUM]);
if (backup_or_restore) {
if (s_retention.extra_refs++ == (cnt_modules - 1)) {
pau_regdma_trigger_extra_link_backup();
}
pau_regdma_trigger_extra_link_backup();
} else {
if (--s_retention.extra_refs == (cnt_modules - 1)) {
pau_regdma_trigger_extra_link_restore();
}
pau_regdma_trigger_extra_link_restore();
}
int refs = s_retention.extra_refs;
_lock_release_recursive(&s_retention.lock);
assert(refs >= 0 && refs <= cnt_modules);
}
void sleep_retention_module_deinit(void)
{
_lock_acquire_recursive(&s_retention.lock);
if (s_retention.extra_refs) {
s_retention.extra_refs--;
}
_lock_release_recursive(&s_retention.lock);
}
#endif

View File

@ -26,17 +26,18 @@
#include "esp_attr.h"
#include "esp_phy_init.h"
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
#include "esp_pm.h"
#include "esp_private/esp_clk.h"
#include "esp_private/sleep_retention.h"
#include "esp_private/sleep_modem.h"
static bool s_rf_closed = false;
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
#define IEEE802154_LINK_OWNER ENTRY(3)
#else
#define IEEE802154_LINK_OWNER ENTRY(0) | ENTRY(2)
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
#endif
#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
#define CCA_DETECTION_TIME 8
@ -613,6 +614,12 @@ void ieee802154_enable(void)
void ieee802154_disable(void)
{
modem_clock_module_disable(ieee802154_periph.module);
#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
sleep_modem_unregister_mac_bb_module_prepare_callback(sleep_modem_mac_bb_power_down_prepare,
sleep_modem_mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
ieee802154_set_state(IEEE802154_STATE_DISABLE);
}
@ -788,28 +795,33 @@ static esp_err_t ieee802154_sleep_init(void)
err = sleep_retention_entries_create(ieee802154_mac_regs_retention, ARRAY_SIZE(ieee802154_mac_regs_retention), REGDMA_LINK_PRI_7, SLEEP_RETENTION_MODULE_802154_MAC);
ESP_RETURN_ON_ERROR(err, IEEE802154_TAG, "failed to allocate memory for ieee802154 mac retention");
ESP_LOGI(IEEE802154_TAG, "ieee802154 mac sleep retention initialization");
#endif
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
sleep_modem_register_mac_bb_module_prepare_callback(sleep_modem_mac_bb_power_down_prepare,
sleep_modem_mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
return err;
}
IRAM_ATTR static void ieee802154_rf_disable(void)
{
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
if (s_rf_closed == false) {
esp_phy_disable(PHY_MODEM_IEEE802154);
s_rf_closed = true;
}
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
}
IRAM_ATTR static void ieee802154_rf_enable(void)
{
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
if (s_rf_closed) {
esp_phy_enable(PHY_MODEM_IEEE802154);
s_rf_closed = false;
}
#endif //CONFIG_FREERTOS_USE_TICKLESS_IDLE
#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE
}
esp_err_t ieee802154_sleep(void)

View File

@ -21,4 +21,5 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
# Sleep Config
#
CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y
CONFIG_ESP_PHY_MAC_BB_PD=y
# end of Sleep Config

View File

@ -21,6 +21,7 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
# Sleep Config
#
CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y
CONFIG_ESP_PHY_MAC_BB_PD=y
# end of Sleep Config
#

View File

@ -33,20 +33,27 @@ examples/openthread/ot_br:
examples/openthread/ot_cli:
enable:
- if: IDF_TARGET in ["esp32h2", "esp32c6"]
- if: SOC_IEEE802154_SUPPORTED == 1
<<: *openthread_dependencies
examples/openthread/ot_rcp:
enable:
- if: IDF_TARGET in ["esp32h2", "esp32c6"]
- if: SOC_IEEE802154_SUPPORTED == 1
disable_test:
- if: IDF_TARGET == "esp32h2"
temporary: true
reason: only test on esp32c6
<<: *openthread_dependencies
# To add support for the ESP32-C6 in TZ-302
examples/openthread/ot_sleepy_device:
examples/openthread/ot_sleepy_device/deep_sleep:
enable:
- if: IDF_TARGET in ["esp32h2"]
disable_test:
- if: IDF_TARGET == "esp32h2"
temporary: true
reason: Unsupport
examples/openthread/ot_sleepy_device/light_sleep:
enable:
- if: SOC_IEEE802154_SUPPORTED == 1
<<: [*openthread_dependencies, *openthread_sleep_dependencies]

View File

@ -1,6 +1,5 @@
| Supported Targets | ESP32-H2 |
| ----------------- | -------- |
| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |
# OpenThread Sleepy Device Example
The example demonstrates the Thread Sleepy End Device (SED), the device will enter [Light Sleep mode](https://docs.espressif.com/projects/esp-idf/en/latest/esp32h2/api-reference/system/sleep_modes.html#sleep-modes) during idle state.

View File

@ -3,4 +3,4 @@ CONFIG_IDF_TARGET_ESP32C6=y
CONFIG_OPENTHREAD_NETWORK_CHANNEL=12
CONFIG_OPENTHREAD_NETWORK_MASTERKEY="aabbccddeeff00112233445566778899"
CONFIG_ESP_SLEEP_DEBUG=y
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
CONFIG_LOG_MAXIMUM_LEVEL_DEBUG=y

View File

@ -0,0 +1,7 @@
CONFIG_IDF_TARGET="esp32c6"
#
# Sleep Config
#
CONFIG_ESP_PHY_MAC_BB_PD=y
# end of Sleep Config

View File

@ -0,0 +1 @@
CONFIG_IDF_TARGET="esp32h2"

View File

@ -558,6 +558,10 @@ def test_TCP_NAT64(Init_interface:bool, dut: Tuple[IdfDut, IdfDut, IdfDut]) -> N
@pytest.mark.openthread_sleep
@pytest.mark.parametrize(
'config, count, app_path, target', [
('cli_h2|sleepy_c6', 2,
f'{os.path.join(os.path.dirname(__file__), "ot_cli")}'
f'|{os.path.join(os.path.dirname(__file__), "ot_sleepy_device")}',
'esp32h2|esp32c6'),
('cli_c6|sleepy_h2', 2,
f'{os.path.join(os.path.dirname(__file__), "ot_cli")}'
f'|{os.path.join(os.path.dirname(__file__), "ot_sleepy_device")}',