Skip to content

Commit 97ae87d

Browse files
committed
wifi, bt: move esp_phy_common_clock_disable into periph_ctrl and put it into IRAM
Replace periph_module_enable/disable by periph_wifi_bt_common_module_enable which are in IRAM. AddIRAM_ATTR periph_ll_wifi_bt_module_enable_clk_clear_rstandIRAM_ATTR periph_ll_wifi_bt_module_disable_clk_set_rstto fit O0 optimization level. Delete duplicated spinlock and counter.
1 parent b3814a1 commit 97ae87d

File tree

5 files changed

+69
-57
lines changed

5 files changed

+69
-57
lines changed

components/driver/include/driver/periph_ctrl.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,29 @@ void periph_module_disable(periph_module_t periph);
6767
*/
6868
void periph_module_reset(periph_module_t periph);
6969

70+
/**
71+
* @brief enable wifi bt common module
72+
*
73+
* @note If wifi_bt_common_module_enable is called a number of times,
74+
* wifi_bt_common_module_disable has to be called the same number of times
75+
* in order to put the peripheral into disabled state.
76+
*
77+
* @return NULL
78+
*
79+
*/
80+
void wifi_bt_common_module_enable(void);
81+
82+
/**
83+
* @brief disable wifi bt common module
84+
*
85+
* @note If wifi_bt_common_module_enable is called a number of times,
86+
* wifi_bt_common_module_disable has to be called the same number of times
87+
* in order to put the peripheral into disabled state.
88+
*
89+
* @return NULL
90+
*
91+
*/
92+
void wifi_bt_common_module_disable(void);
7093

7194
#ifdef __cplusplus
7295
}

components/driver/periph_ctrl.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,23 @@ void periph_module_reset(periph_module_t periph)
4848
periph_ll_reset(periph);
4949
portEXIT_CRITICAL_SAFE(&periph_spinlock);
5050
}
51+
52+
IRAM_ATTR void wifi_bt_common_module_enable(void)
53+
{
54+
portENTER_CRITICAL_SAFE(&periph_spinlock);
55+
if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
56+
periph_ll_wifi_bt_module_enable_clk_clear_rst();
57+
}
58+
ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]++;
59+
portEXIT_CRITICAL_SAFE(&periph_spinlock);
60+
}
61+
62+
IRAM_ATTR void wifi_bt_common_module_disable(void)
63+
{
64+
portENTER_CRITICAL_SAFE(&periph_spinlock);
65+
ref_counts[PERIPH_WIFI_BT_COMMON_MODULE]--;
66+
if (ref_counts[PERIPH_WIFI_BT_COMMON_MODULE] == 0) {
67+
periph_ll_wifi_bt_module_disable_clk_set_rst();
68+
}
69+
portEXIT_CRITICAL_SAFE(&periph_spinlock);
70+
}

components/esp_wifi/src/phy_init.c

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ static uint32_t s_module_phy_rf_init = 0;
5656
/* Whether modem sleep is turned on */
5757
static volatile bool s_is_phy_rf_en = false;
5858

59-
#if CONFIG_IDF_TARGET_ESP32
60-
/* Whether WiFi/BT common clock enabled reference */
61-
static volatile int32_t s_common_clock_enable_ref = 0;
62-
63-
/* PHY spinlock mux */
64-
static portMUX_TYPE s_phy_spin_lock = portMUX_INITIALIZER_UNLOCKED;
65-
#endif
66-
6759
/* Bit mask of modules needing to enter modem sleep mode */
6860
static uint32_t s_modem_sleep_module_enter = 0;
6961

@@ -192,63 +184,16 @@ static inline void phy_update_wifi_mac_time(bool en_clock_stopped, int64_t now)
192184
}
193185
}
194186
}
195-
196-
IRAM_ATTR static inline void phy_spin_lock(void)
197-
{
198-
if (xPortInIsrContext()) {
199-
portENTER_CRITICAL_ISR(&s_phy_spin_lock);
200-
} else {
201-
portENTER_CRITICAL(&s_phy_spin_lock);
202-
}
203-
}
204-
205-
IRAM_ATTR static inline void phy_spin_unlock(void)
206-
{
207-
if (xPortInIsrContext()) {
208-
portEXIT_CRITICAL_ISR(&s_phy_spin_lock);
209-
} else {
210-
portEXIT_CRITICAL(&s_phy_spin_lock);
211-
}
212-
}
213187
#endif
214188

215189
IRAM_ATTR void esp_phy_common_clock_enable(void)
216190
{
217-
#if CONFIG_IDF_TARGET_ESP32
218-
phy_spin_lock();
219-
220-
if (s_common_clock_enable_ref == 0) {
221-
// Enable WiFi/BT common clock
222-
periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE);
223-
}
224-
225-
s_common_clock_enable_ref++;
226-
phy_spin_unlock();
227-
#else
228-
periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE);
229-
#endif
191+
wifi_bt_common_module_enable();
230192
}
231193

232194
IRAM_ATTR void esp_phy_common_clock_disable(void)
233195
{
234-
#if CONFIG_IDF_TARGET_ESP32
235-
phy_spin_lock();
236-
237-
if (s_common_clock_enable_ref > 0) {
238-
s_common_clock_enable_ref --;
239-
240-
if (s_common_clock_enable_ref == 0) {
241-
// Disable WiFi/BT common clock
242-
periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
243-
}
244-
} else {
245-
abort();
246-
}
247-
248-
phy_spin_unlock();
249-
#else
250-
periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
251-
#endif
196+
wifi_bt_common_module_disable();
252197
}
253198

254199
esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibration_mode_t mode,

components/soc/src/esp32/include/hal/clk_gate_ll.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ static inline void periph_ll_disable_clk_set_rst(periph_module_t periph)
242242
DPORT_SET_PERI_REG_MASK(periph_ll_get_rst_en_reg(periph), periph_ll_get_rst_en_mask(periph, false));
243243
}
244244

245+
static inline void IRAM_ATTR periph_ll_wifi_bt_module_enable_clk_clear_rst(void)
246+
{
247+
DPORT_SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
248+
DPORT_CLEAR_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 0);
249+
}
250+
251+
static inline void IRAM_ATTR periph_ll_wifi_bt_module_disable_clk_set_rst(void)
252+
{
253+
DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
254+
DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 0);
255+
}
256+
245257
static inline void periph_ll_reset(periph_module_t periph)
246258
{
247259
DPORT_SET_PERI_REG_MASK(periph_ll_get_rst_en_reg(periph), periph_ll_get_rst_en_mask(periph, false));

components/soc/src/esp32s2/include/hal/clk_gate_ll.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ static inline void periph_ll_disable_clk_set_rst(periph_module_t periph)
254254
DPORT_SET_PERI_REG_MASK(periph_ll_get_rst_en_reg(periph), periph_ll_get_rst_en_mask(periph, false));
255255
}
256256

257+
static inline void IRAM_ATTR periph_ll_wifi_bt_module_enable_clk_clear_rst(void)
258+
{
259+
DPORT_SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
260+
DPORT_CLEAR_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 0);
261+
}
262+
263+
static inline void IRAM_ATTR periph_ll_wifi_bt_module_disable_clk_set_rst(void)
264+
{
265+
DPORT_CLEAR_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_WIFI_BT_COMMON_M);
266+
DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG, 0);
267+
}
268+
257269
static inline void periph_ll_reset(periph_module_t periph)
258270
{
259271
DPORT_SET_PERI_REG_MASK(periph_ll_get_rst_en_reg(periph), periph_ll_get_rst_en_mask(periph, false));

0 commit comments

Comments
 (0)