From b9baf49782a296a347c3d3bc662437a6174e224b Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Mon, 25 Sep 2023 20:59:32 +0800 Subject: [PATCH 01/10] fix(pm): add mac/bb power down/up prepare for fix esp32c6 pll issue --- .../include/esp_private/sleep_modem.h | 18 ++++++++++++++++++ components/esp_hw_support/sleep_modem.c | 19 +++++++++++++++++++ components/esp_hw_support/sleep_modes.c | 8 ++++++++ components/esp_hw_support/sleep_retention.c | 3 --- components/esp_pm/pm_impl.c | 5 +++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/components/esp_hw_support/include/esp_private/sleep_modem.h b/components/esp_hw_support/include/esp_private/sleep_modem.h index fe8e3ea220..b1df6f00e5 100644 --- a/components/esp_hw_support/include/esp_private/sleep_modem.h +++ b/components/esp_hw_support/include/esp_private/sleep_modem.h @@ -40,6 +40,24 @@ void mac_bb_power_up_cb_execute(void); #endif // CONFIG_MAC_BB_PD +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +/** + * @brief MAC and baseband power down operation + * + * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband + * power up and backup prepare operations. + */ +void 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 mac_bb_power_up_prepare(void); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + #if SOC_PM_SUPPORT_PMU_MODEM_STATE /** diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 7e963aa64d..9e2e4ab366 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -390,3 +390,22 @@ static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_f } } #endif + +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +static bool s_modem_sleep = false; +void IRAM_ATTR mac_bb_power_down_prepare(void) +{ + if (s_modem_sleep == false) { + sleep_retention_do_extra_retention(true); // backup + s_modem_sleep = true; + } +} + +void IRAM_ATTR mac_bb_power_up_prepare(void) +{ + if (s_modem_sleep) { + sleep_retention_do_extra_retention(false); // restore + s_modem_sleep = false; + } +} +#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG */ diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 4d03e5eecb..5136d6f980 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -624,6 +624,14 @@ static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t m // Will switch to XTAL turn down MSPI speed mspi_timing_change_speed_mode_cache_safe(true); + /* + Since the modem requires a PLL clock to access modem REG, + it is necessary to back up the mac/bb REG before disabling the PLL clock. + */ +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + mac_bb_power_down_prepare(); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + // Save current frequency and switch to XTAL rtc_cpu_freq_config_t cpu_freq_config; rtc_clk_cpu_freq_get_config(&cpu_freq_config); diff --git a/components/esp_hw_support/sleep_retention.c b/components/esp_hw_support/sleep_retention.c index 95e90d5e07..6d5cf2cfd8 100644 --- a/components/esp_hw_support/sleep_retention.c +++ b/components/esp_hw_support/sleep_retention.c @@ -505,10 +505,8 @@ 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; @@ -525,7 +523,6 @@ void sleep_retention_do_extra_retention(bool backup_or_restore) } } int refs = s_retention.extra_refs; - _lock_release_recursive(&s_retention.lock); assert(refs >= 0 && refs <= cnt_modules); } diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index c594a3d3db..2c28e6d696 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -999,6 +999,11 @@ void IRAM_ATTR esp_pm_impl_isr_hook(void) } #else leave_idle(); + /* it is necessary to restore the mac/bb REG before scheduling other tasks in FreeRTOS.*/ +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + mac_bb_power_up_prepare(); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT && portNUM_PROCESSORS == 2 #if CONFIG_FREERTOS_SMP portRESTORE_INTERRUPTS(state); From 15e1c8d3ca0c687b758114a3ce3b1f16e09da5bc Mon Sep 17 00:00:00 2001 From: cjin Date: Tue, 26 Sep 2023 16:36:06 +0800 Subject: [PATCH 02/10] feat(ble): remove software regdma opt in bt --- components/bt/controller/esp32c6/bt.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index a58c92c99b..d0f4ea0d32 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -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; @@ -950,11 +944,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; From ff6992806458874cdc6028d052c562f7a0cec2a9 Mon Sep 17 00:00:00 2001 From: cjin Date: Tue, 26 Sep 2023 16:44:27 +0800 Subject: [PATCH 03/10] feat(pm): change power down prepare position --- components/esp_hw_support/sleep_modes.c | 8 -------- components/esp_pm/pm_impl.c | 7 +++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 5136d6f980..4d03e5eecb 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -624,14 +624,6 @@ static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t m // Will switch to XTAL turn down MSPI speed mspi_timing_change_speed_mode_cache_safe(true); - /* - Since the modem requires a PLL clock to access modem REG, - it is necessary to back up the mac/bb REG before disabling the PLL clock. - */ -#if SOC_PM_RETENTION_HAS_CLOCK_BUG - mac_bb_power_down_prepare(); -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG - // Save current frequency and switch to XTAL rtc_cpu_freq_config_t cpu_freq_config; rtc_clk_cpu_freq_get_config(&cpu_freq_config); diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index 2c28e6d696..a8cfcf2f75 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -967,6 +967,13 @@ void esp_pm_impl_idle_hook(void) && !periph_should_skip_light_sleep() #endif ) { + /* + Since the modem requires a PLL clock to access modem REG, + it is necessary to back up the mac/bb REG before disabling the PLL clock. + */ +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + mac_bb_power_down_prepare(); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG esp_pm_lock_release(s_rtos_lock_handle[core_id]); s_core_idle[core_id] = true; } From f428f9d8c05c8cd5b65367a339c5b6f1768e39b3 Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Tue, 26 Sep 2023 19:19:45 +0800 Subject: [PATCH 04/10] fix(pm): remove unuse extra_refs related code --- .../include/esp_private/sleep_modem.h | 2 +- .../include/esp_private/sleep_retention.h | 2 -- components/esp_hw_support/sleep_retention.c | 25 ++----------------- 3 files changed, 3 insertions(+), 26 deletions(-) diff --git a/components/esp_hw_support/include/esp_private/sleep_modem.h b/components/esp_hw_support/include/esp_private/sleep_modem.h index b1df6f00e5..4ba885bf2a 100644 --- a/components/esp_hw_support/include/esp_private/sleep_modem.h +++ b/components/esp_hw_support/include/esp_private/sleep_modem.h @@ -45,7 +45,7 @@ void mac_bb_power_up_cb_execute(void); * @brief MAC and baseband power down operation * * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband - * power up and backup prepare operations. + * power down and backup prepare operations. */ void mac_bb_power_down_prepare(void); diff --git a/components/esp_hw_support/include/esp_private/sleep_retention.h b/components/esp_hw_support/include/esp_private/sleep_retention.h index e64a95a9c0..007f973f42 100644 --- a/components/esp_hw_support/include/esp_private/sleep_retention.h +++ b/components/esp_hw_support/include/esp_private/sleep_retention.h @@ -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 /** diff --git a/components/esp_hw_support/sleep_retention.c b/components/esp_hw_support/sleep_retention.c index 6d5cf2cfd8..48fddb29db 100644 --- a/components/esp_hw_support/sleep_retention.c +++ b/components/esp_hw_support/sleep_retention.c @@ -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) @@ -509,30 +505,13 @@ void sleep_retention_do_extra_retention(bool backup_or_restore) s_retention.highpri > SLEEP_RETENTION_REGDMA_LINK_LOWEST_PRIORITY) { 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; - 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 From 15df836ab4535f0b51dcd8569763ea6524bdae22 Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Wed, 11 Oct 2023 14:51:35 +0800 Subject: [PATCH 05/10] fix(ieee802154): add skip light sleep for esp32c6 light sleep --- components/ieee802154/driver/esp_ieee802154_dev.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/components/ieee802154/driver/esp_ieee802154_dev.c b/components/ieee802154/driver/esp_ieee802154_dev.c index f0db360374..13873c5650 100644 --- a/components/ieee802154/driver/esp_ieee802154_dev.c +++ b/components/ieee802154/driver/esp_ieee802154_dev.c @@ -30,6 +30,7 @@ #include "esp_pm.h" #include "esp_private/esp_clk.h" #include "esp_private/sleep_retention.h" +#include "esp_private/pm_impl.h" static bool s_rf_closed = false; #if SOC_PM_RETENTION_HAS_CLOCK_BUG #define IEEE802154_LINK_OWNER ENTRY(3) @@ -777,6 +778,17 @@ esp_err_t ieee802154_receive_at(uint32_t time) return ESP_OK; } +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG +static bool IRAM_ATTR sleep_modem_ieee802154_modem_state_skip_light_sleep(void) +{ + bool skip = false; + if (esp_ieee802154_get_state() != ESP_IEEE802154_RADIO_SLEEP) { + skip = true; + } + return skip; +} +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG + static esp_err_t ieee802154_sleep_init(void) { esp_err_t err = ESP_OK; @@ -788,6 +800,9 @@ 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"); +#if SOC_PM_RETENTION_HAS_CLOCK_BUG + esp_pm_register_skip_light_sleep_callback(sleep_modem_ieee802154_modem_state_skip_light_sleep); +#endif #endif return err; } From c1798adac680ec323ac22af7444fcad61120a374 Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 13 Oct 2023 18:22:35 +0800 Subject: [PATCH 06/10] change(pm): add pm lock count check before reg backup --- components/esp_pm/pm_impl.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index a8cfcf2f75..7760ae53d2 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -196,6 +196,9 @@ static const char* TAG = "pm"; static void do_switch(pm_mode_t new_mode); static void leave_idle(void); static void on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us); +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +static size_t esp_pm_get_total_lock_count(void); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg) { @@ -972,7 +975,9 @@ void esp_pm_impl_idle_hook(void) it is necessary to back up the mac/bb REG before disabling the PLL clock. */ #if SOC_PM_RETENTION_HAS_CLOCK_BUG - mac_bb_power_down_prepare(); + if (esp_pm_get_total_lock_count() <= portNUM_PROCESSORS) { + mac_bb_power_down_prepare(); + } #endif // SOC_PM_RETENTION_HAS_CLOCK_BUG esp_pm_lock_release(s_rtos_lock_handle[core_id]); s_core_idle[core_id] = true; @@ -1037,3 +1042,16 @@ void esp_pm_impl_waiti(void) esp_cpu_wait_for_intr(); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE } + +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +static size_t esp_pm_get_total_lock_count(void) +{ + size_t all_mode_lock_counts = 0; + portENTER_CRITICAL(&s_switch_lock); + for (int i = 0; i < ARRAY_SIZE(s_mode_lock_counts); i++) { + all_mode_lock_counts += s_mode_lock_counts[i]; + } + portEXIT_CRITICAL(&s_switch_lock); + return all_mode_lock_counts; +} +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG From 3292ee7039d227357c1d82239b0c74455167b726 Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Mon, 16 Oct 2023 18:47:01 +0800 Subject: [PATCH 07/10] fix(ieee802154): remove skip light sleep callback --- components/ieee802154/driver/esp_ieee802154_dev.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/components/ieee802154/driver/esp_ieee802154_dev.c b/components/ieee802154/driver/esp_ieee802154_dev.c index 13873c5650..f0db360374 100644 --- a/components/ieee802154/driver/esp_ieee802154_dev.c +++ b/components/ieee802154/driver/esp_ieee802154_dev.c @@ -30,7 +30,6 @@ #include "esp_pm.h" #include "esp_private/esp_clk.h" #include "esp_private/sleep_retention.h" -#include "esp_private/pm_impl.h" static bool s_rf_closed = false; #if SOC_PM_RETENTION_HAS_CLOCK_BUG #define IEEE802154_LINK_OWNER ENTRY(3) @@ -778,17 +777,6 @@ esp_err_t ieee802154_receive_at(uint32_t time) return ESP_OK; } -#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG -static bool IRAM_ATTR sleep_modem_ieee802154_modem_state_skip_light_sleep(void) -{ - bool skip = false; - if (esp_ieee802154_get_state() != ESP_IEEE802154_RADIO_SLEEP) { - skip = true; - } - return skip; -} -#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE && SOC_PM_RETENTION_HAS_CLOCK_BUG - static esp_err_t ieee802154_sleep_init(void) { esp_err_t err = ESP_OK; @@ -800,9 +788,6 @@ 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"); -#if SOC_PM_RETENTION_HAS_CLOCK_BUG - esp_pm_register_skip_light_sleep_callback(sleep_modem_ieee802154_modem_state_skip_light_sleep); -#endif #endif return err; } From e98e2916017b642c2ae0557536dda17208fd77a6 Mon Sep 17 00:00:00 2001 From: cjin Date: Tue, 31 Oct 2023 17:19:53 +0800 Subject: [PATCH 08/10] fix(pm): switch root clk src to PLL for modem reg opt and added callback --- components/bt/controller/esp32c6/bt.c | 13 +++++ .../include/esp_private/sleep_modem.h | 25 ++++++++-- .../esp_hw_support/port/esp32c6/rtc_clk.c | 17 +++++++ components/esp_hw_support/sleep_modem.c | 48 ++++++++++++++++--- components/esp_pm/pm_impl.c | 30 ------------ components/soc/esp32c6/include/soc/rtc.h | 13 +++++ .../nimble/power_save/sdkconfig.40m.esp32c6 | 1 + .../power_save/sdkconfig.defaults.esp32c6 | 1 + 8 files changed, 108 insertions(+), 40 deletions(-) diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index d0f4ea0d32..347eb8f30c 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -564,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 + esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, + 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 + esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, + 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 */ @@ -586,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 + esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, + 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(); diff --git a/components/esp_hw_support/include/esp_private/sleep_modem.h b/components/esp_hw_support/include/esp_private/sleep_modem.h index 4ba885bf2a..209c129161 100644 --- a/components/esp_hw_support/include/esp_private/sleep_modem.h +++ b/components/esp_hw_support/include/esp_private/sleep_modem.h @@ -9,6 +9,7 @@ #include #include "sdkconfig.h" #include "esp_err.h" +#include "esp_sleep.h" #ifdef __cplusplus extern "C" { @@ -40,9 +41,27 @@ void mac_bb_power_up_cb_execute(void); #endif // CONFIG_MAC_BB_PD -#if SOC_PM_RETENTION_HAS_CLOCK_BUG +#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD /** - * @brief MAC and baseband power down operation + * @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 esp_pm_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 esp_pm_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. @@ -56,7 +75,7 @@ void mac_bb_power_down_prepare(void); * power up and restore prepare operations. */ void mac_bb_power_up_prepare(void); -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD #if SOC_PM_SUPPORT_PMU_MODEM_STATE diff --git a/components/esp_hw_support/port/esp32c6/rtc_clk.c b/components/esp_hw_support/port/esp32c6/rtc_clk.c index 53644617ca..91a5470efd 100644 --- a/components/esp_hw_support/port/esp32c6/rtc_clk.c +++ b/components/esp_hw_support/port/esp32c6/rtc_clk.c @@ -425,3 +425,20 @@ bool rtc_dig_8m_enabled(void) * TODO: update the library to use rtc_clk_xtal_freq_get */ rtc_xtal_freq_t rtc_get_xtal(void) __attribute__((alias("rtc_clk_xtal_freq_get"))); + +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool)) +{ + 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); +} +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 9e2e4ab366..68b6eca5a2 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -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]; @@ -391,12 +398,37 @@ static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_f } #endif -#if SOC_PM_RETENTION_HAS_CLOCK_BUG -static bool s_modem_sleep = false; +#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD +void esp_pm_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 esp_pm_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); + +} + void IRAM_ATTR mac_bb_power_down_prepare(void) { if (s_modem_sleep == false) { - sleep_retention_do_extra_retention(true); // backup + 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; } } @@ -404,8 +436,10 @@ void IRAM_ATTR mac_bb_power_down_prepare(void) void IRAM_ATTR mac_bb_power_up_prepare(void) { if (s_modem_sleep) { - sleep_retention_do_extra_retention(false); // restore + 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 */ +#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD */ diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index 7760ae53d2..c594a3d3db 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -196,9 +196,6 @@ static const char* TAG = "pm"; static void do_switch(pm_mode_t new_mode); static void leave_idle(void); static void on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us); -#if SOC_PM_RETENTION_HAS_CLOCK_BUG -static size_t esp_pm_get_total_lock_count(void); -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg) { @@ -970,15 +967,6 @@ void esp_pm_impl_idle_hook(void) && !periph_should_skip_light_sleep() #endif ) { - /* - Since the modem requires a PLL clock to access modem REG, - it is necessary to back up the mac/bb REG before disabling the PLL clock. - */ -#if SOC_PM_RETENTION_HAS_CLOCK_BUG - if (esp_pm_get_total_lock_count() <= portNUM_PROCESSORS) { - mac_bb_power_down_prepare(); - } -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG esp_pm_lock_release(s_rtos_lock_handle[core_id]); s_core_idle[core_id] = true; } @@ -1011,11 +999,6 @@ void IRAM_ATTR esp_pm_impl_isr_hook(void) } #else leave_idle(); - /* it is necessary to restore the mac/bb REG before scheduling other tasks in FreeRTOS.*/ -#if SOC_PM_RETENTION_HAS_CLOCK_BUG - mac_bb_power_up_prepare(); -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG - #endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT && portNUM_PROCESSORS == 2 #if CONFIG_FREERTOS_SMP portRESTORE_INTERRUPTS(state); @@ -1042,16 +1025,3 @@ void esp_pm_impl_waiti(void) esp_cpu_wait_for_intr(); #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE } - -#if SOC_PM_RETENTION_HAS_CLOCK_BUG -static size_t esp_pm_get_total_lock_count(void) -{ - size_t all_mode_lock_counts = 0; - portENTER_CRITICAL(&s_switch_lock); - for (int i = 0; i < ARRAY_SIZE(s_mode_lock_counts); i++) { - all_mode_lock_counts += s_mode_lock_counts[i]; - } - portEXIT_CRITICAL(&s_switch_lock); - return all_mode_lock_counts; -} -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG diff --git a/components/soc/esp32c6/include/soc/rtc.h b/components/soc/esp32c6/include/soc/rtc.h index 716424dcc0..b77ebff590 100644 --- a/components/soc/esp32c6/include/soc/rtc.h +++ b/components/soc/esp32c6/include/soc/rtc.h @@ -481,6 +481,19 @@ bool rtc_dig_8m_enabled(void); uint32_t rtc_clk_freq_cal(uint32_t cal_val); +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +/** + * @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 + */ +void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool)); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + // -------------------------- CLOCK TREE DEFS ALIAS ---------------------------- // **WARNING**: The following are only for backwards compatibility. // Please use the declarations in soc/clk_tree_defs.h instead. diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 b/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 index 885ab812bc..83adc91d0c 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.40m.esp32c6 @@ -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 diff --git a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 index fdf2ca28b4..ff9c87fcbc 100644 --- a/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 +++ b/examples/bluetooth/nimble/power_save/sdkconfig.defaults.esp32c6 @@ -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 # From 5a77f87986215fa74e7c19f633958baa4ccbe494 Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Wed, 1 Nov 2023 12:01:01 +0800 Subject: [PATCH 09/10] feat(ieee802154): register power_down/power_up callback for esp32c6 --- components/ieee802154/driver/esp_ieee802154_dev.c | 13 +++++++++++++ examples/openthread/.build-test-rules.yml | 11 +++++++++-- .../ot_sleepy_device/light_sleep/README.md | 5 ++--- .../light_sleep/sdkconfig.ci.sleepy_c6 | 2 +- .../light_sleep/sdkconfig.defaults.esp32c6 | 7 +++++++ .../light_sleep/sdkconfig.defaults.esp32h2 | 1 + examples/openthread/pytest_otbr.py | 4 ++++ 7 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32c6 create mode 100644 examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32h2 diff --git a/components/ieee802154/driver/esp_ieee802154_dev.c b/components/ieee802154/driver/esp_ieee802154_dev.c index f0db360374..a11aca6aef 100644 --- a/components/ieee802154/driver/esp_ieee802154_dev.c +++ b/components/ieee802154/driver/esp_ieee802154_dev.c @@ -30,6 +30,7 @@ #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) @@ -613,6 +614,12 @@ void ieee802154_enable(void) void ieee802154_disable(void) { modem_clock_module_disable(ieee802154_periph.module); +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD + esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, + mac_bb_power_up_prepare); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE ieee802154_set_state(IEEE802154_STATE_DISABLE); } @@ -788,6 +795,12 @@ 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"); + +#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD + esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, + mac_bb_power_up_prepare); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD + #endif return err; } diff --git a/examples/openthread/.build-test-rules.yml b/examples/openthread/.build-test-rules.yml index 49f5744824..12ab9b55c3 100644 --- a/examples/openthread/.build-test-rules.yml +++ b/examples/openthread/.build-test-rules.yml @@ -45,8 +45,15 @@ examples/openthread/ot_rcp: 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: IDF_TARGET in ["esp32h2", "esp32c6"] <<: [*openthread_dependencies, *openthread_sleep_dependencies] diff --git a/examples/openthread/ot_sleepy_device/light_sleep/README.md b/examples/openthread/ot_sleepy_device/light_sleep/README.md index ebcf8eae27..d49a314b5e 100644 --- a/examples/openthread/ot_sleepy_device/light_sleep/README.md +++ b/examples/openthread/ot_sleepy_device/light_sleep/README.md @@ -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. diff --git a/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.ci.sleepy_c6 b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.ci.sleepy_c6 index 80eea90801..f0198c840e 100644 --- a/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.ci.sleepy_c6 +++ b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.ci.sleepy_c6 @@ -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 diff --git a/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32c6 b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32c6 new file mode 100644 index 0000000000..2c90c3e7ee --- /dev/null +++ b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32c6 @@ -0,0 +1,7 @@ +CONFIG_IDF_TARGET="esp32c6" + +# +# Sleep Config +# +CONFIG_ESP_PHY_MAC_BB_PD=y +# end of Sleep Config diff --git a/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32h2 b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32h2 new file mode 100644 index 0000000000..ba2980822c --- /dev/null +++ b/examples/openthread/ot_sleepy_device/light_sleep/sdkconfig.defaults.esp32h2 @@ -0,0 +1 @@ +CONFIG_IDF_TARGET="esp32h2" diff --git a/examples/openthread/pytest_otbr.py b/examples/openthread/pytest_otbr.py index 349cf19f04..23c267e30b 100644 --- a/examples/openthread/pytest_otbr.py +++ b/examples/openthread/pytest_otbr.py @@ -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")}', From a0ecd497257cfcad079c983f15c8fb4db659662e Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Mon, 6 Nov 2023 21:04:59 +0800 Subject: [PATCH 10/10] feat(pm): change power up/down register callback name --- components/bt/controller/esp32c6/bt.c | 12 +++--- .../include/esp_private/sleep_modem.h | 8 ++-- .../esp_hw_support/port/esp32c6/rtc_clk.c | 17 -------- components/esp_hw_support/sleep_modem.c | 41 ++++++++++++++++--- .../ieee802154/driver/esp_ieee802154_dev.c | 27 ++++++------ components/soc/esp32c6/include/soc/rtc.h | 13 ------ examples/openthread/.build-test-rules.yml | 6 +-- 7 files changed, 61 insertions(+), 63 deletions(-) diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index 347eb8f30c..2d6e88c554 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -566,8 +566,8 @@ esp_err_t controller_sleep_init(void) } #if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD - esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, - mac_bb_power_up_prepare); + 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; @@ -576,8 +576,8 @@ error: #if CONFIG_FREERTOS_USE_TICKLESS_IDLE #if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD - esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, - mac_bb_power_up_prepare); + 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); @@ -596,8 +596,8 @@ void controller_sleep_deinit(void) { #if CONFIG_FREERTOS_USE_TICKLESS_IDLE #if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD - esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, - mac_bb_power_up_prepare); + 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(); diff --git a/components/esp_hw_support/include/esp_private/sleep_modem.h b/components/esp_hw_support/include/esp_private/sleep_modem.h index 209c129161..7922c24fc8 100644 --- a/components/esp_hw_support/include/esp_private/sleep_modem.h +++ b/components/esp_hw_support/include/esp_private/sleep_modem.h @@ -48,7 +48,7 @@ void mac_bb_power_up_cb_execute(void); * @param pd_cb function to call when power down * @param pu_cb function to call when power up */ -void esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb, +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); /** @@ -57,7 +57,7 @@ void esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb * @param pd_cb function to call when power down * @param pu_cb function to call when power up */ -void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb, +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); /** @@ -66,7 +66,7 @@ void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_ * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband * power down and backup prepare operations. */ -void mac_bb_power_down_prepare(void); +void sleep_modem_mac_bb_power_down_prepare(void); /** * @brief MAC and baseband power up operation @@ -74,7 +74,7 @@ void mac_bb_power_down_prepare(void); * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband * power up and restore prepare operations. */ -void mac_bb_power_up_prepare(void); +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 diff --git a/components/esp_hw_support/port/esp32c6/rtc_clk.c b/components/esp_hw_support/port/esp32c6/rtc_clk.c index 91a5470efd..53644617ca 100644 --- a/components/esp_hw_support/port/esp32c6/rtc_clk.c +++ b/components/esp_hw_support/port/esp32c6/rtc_clk.c @@ -425,20 +425,3 @@ bool rtc_dig_8m_enabled(void) * TODO: update the library to use rtc_clk_xtal_freq_get */ rtc_xtal_freq_t rtc_get_xtal(void) __attribute__((alias("rtc_clk_xtal_freq_get"))); - -#if SOC_PM_RETENTION_HAS_CLOCK_BUG -void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool)) -{ - 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); -} -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 68b6eca5a2..828f3a455b 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -399,8 +399,8 @@ 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 esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb, - mac_bb_power_up_cb_t pu_cb) +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) { @@ -410,8 +410,8 @@ void esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb _lock_release(&s_modem_prepare_lock); } -void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb, - mac_bb_power_up_cb_t pu_cb) +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); @@ -423,7 +423,36 @@ void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_ } -void IRAM_ATTR mac_bb_power_down_prepare(void) +/** + * @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, @@ -433,7 +462,7 @@ void IRAM_ATTR mac_bb_power_down_prepare(void) } } -void IRAM_ATTR mac_bb_power_up_prepare(void) +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, diff --git a/components/ieee802154/driver/esp_ieee802154_dev.c b/components/ieee802154/driver/esp_ieee802154_dev.c index a11aca6aef..4ba5dbd867 100644 --- a/components/ieee802154/driver/esp_ieee802154_dev.c +++ b/components/ieee802154/driver/esp_ieee802154_dev.c @@ -26,7 +26,7 @@ #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" @@ -37,7 +37,7 @@ static bool s_rf_closed = false; #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 @@ -614,12 +614,12 @@ void ieee802154_enable(void) void ieee802154_disable(void) { modem_clock_module_disable(ieee802154_periph.module); -#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE #if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD - esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, - mac_bb_power_up_prepare); + 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 // CONFIG_FREERTOS_USE_TICKLESS_IDLE +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE ieee802154_set_state(IEEE802154_STATE_DISABLE); } @@ -797,32 +797,31 @@ static esp_err_t ieee802154_sleep_init(void) ESP_LOGI(IEEE802154_TAG, "ieee802154 mac sleep retention initialization"); #if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD - esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_prepare, - mac_bb_power_up_prepare); + 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 +#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) diff --git a/components/soc/esp32c6/include/soc/rtc.h b/components/soc/esp32c6/include/soc/rtc.h index b77ebff590..716424dcc0 100644 --- a/components/soc/esp32c6/include/soc/rtc.h +++ b/components/soc/esp32c6/include/soc/rtc.h @@ -481,19 +481,6 @@ bool rtc_dig_8m_enabled(void); uint32_t rtc_clk_freq_cal(uint32_t cal_val); -#if SOC_PM_RETENTION_HAS_CLOCK_BUG -/** - * @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 - */ -void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool)); -#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG - // -------------------------- CLOCK TREE DEFS ALIAS ---------------------------- // **WARNING**: The following are only for backwards compatibility. // Please use the declarations in soc/clk_tree_defs.h instead. diff --git a/examples/openthread/.build-test-rules.yml b/examples/openthread/.build-test-rules.yml index 12ab9b55c3..0ad7d9b1a8 100644 --- a/examples/openthread/.build-test-rules.yml +++ b/examples/openthread/.build-test-rules.yml @@ -33,12 +33,12 @@ 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 @@ -55,5 +55,5 @@ examples/openthread/ot_sleepy_device/deep_sleep: examples/openthread/ot_sleepy_device/light_sleep: enable: - - if: IDF_TARGET in ["esp32h2", "esp32c6"] + - if: SOC_IEEE802154_SUPPORTED == 1 <<: [*openthread_dependencies, *openthread_sleep_dependencies]