Jelajahi Sumber

esp_wifi: synchronize Wi-Fi adapter between different chips

Support preferring to allocate Wi-Fi memory from PSRAM on ESP32-S3

Support Wi-Fi TX cache buffer on ESP32-S3
Xia Xiaotian 4 tahun lalu
induk
melakukan
ce8b996ca0

+ 1 - 1
components/esp_wifi/Kconfig

@@ -92,7 +92,7 @@ menu "Wi-Fi"
 
     config ESP32_WIFI_CACHE_TX_BUFFER_NUM
         int "Max number of WiFi cache TX buffers"
-        depends on (ESP32_SPIRAM_SUPPORT || ESP32S2_SPIRAM_SUPPORT)
+        depends on (ESP32_SPIRAM_SUPPORT || ESP32S2_SPIRAM_SUPPORT || ESP32S3_SPIRAM_SUPPORT)
         range 16 128
         default 32
         help

+ 2 - 2
components/esp_wifi/esp32/esp_adapter.c

@@ -255,7 +255,7 @@ static void semphr_delete_wrapper(void *semphr)
 
 static void wifi_thread_semphr_free(void* data)
 {
-    xSemaphoreHandle *sem = (xSemaphoreHandle*)(data);
+    SemaphoreHandle_t *sem = (SemaphoreHandle_t*)(data);
 
     if (sem) {
         vSemaphoreDelete(sem);
@@ -266,7 +266,7 @@ static void * wifi_thread_semphr_get_wrapper(void)
 {
     static bool s_wifi_thread_sem_key_init = false;
     static pthread_key_t s_wifi_thread_sem_key;
-    xSemaphoreHandle sem = NULL;
+    SemaphoreHandle_t sem = NULL;
 
     if (s_wifi_thread_sem_key_init == false) {
         if (0 != pthread_key_create(&s_wifi_thread_sem_key, wifi_thread_semphr_free)) {

+ 3 - 4
components/esp_wifi/esp32c3/esp_adapter.c

@@ -382,12 +382,12 @@ static void IRAM_ATTR timer_disarm_wrapper(void *timer)
     ets_timer_disarm(timer);
 }
 
-static void IRAM_ATTR timer_done_wrapper(void *ptimer)
+static void timer_done_wrapper(void *ptimer)
 {
     ets_timer_done(ptimer);
 }
 
-static void IRAM_ATTR timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
+static void timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
 {
     ets_timer_setfn(ptimer, pfunction, parg);
 }
@@ -435,13 +435,12 @@ static int get_time_wrapper(void *t)
     return os_get_time(t);
 }
 
-#define WIFI_LIGHT_SLEEP_CLK_WIDTH 12
 static uint32_t esp_clk_slowclk_cal_get_wrapper(void)
 {
     /* The bit width of WiFi light sleep clock calibration is 12 while the one of
      * system is 19. It should shift 19 - 12 = 7.
     */
-    return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - WIFI_LIGHT_SLEEP_CLK_WIDTH));
+    return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
 }
 
 static void * IRAM_ATTR malloc_internal_wrapper(size_t size)

+ 5 - 4
components/esp_wifi/esp32s2/esp_adapter.c

@@ -40,6 +40,7 @@
 #include "esp_phy_init.h"
 #include "esp32s2/clk.h"
 #include "soc/dport_reg.h"
+#include "soc/rtc.h"
 #include "soc/syscon_reg.h"
 #include "phy_init_data.h"
 #include "driver/periph_ctrl.h"
@@ -242,7 +243,7 @@ static void semphr_delete_wrapper(void *semphr)
 
 static void wifi_thread_semphr_free(void* data)
 {
-    xSemaphoreHandle *sem = (xSemaphoreHandle*)(data);
+    SemaphoreHandle_t *sem = (SemaphoreHandle_t*)(data);
 
     if (sem) {
         vSemaphoreDelete(sem);
@@ -253,7 +254,7 @@ static void * wifi_thread_semphr_get_wrapper(void)
 {
     static bool s_wifi_thread_sem_key_init = false;
     static pthread_key_t s_wifi_thread_sem_key;
-    xSemaphoreHandle sem = NULL;
+    SemaphoreHandle_t sem = NULL;
 
     if (s_wifi_thread_sem_key_init == false) {
         if (0 != pthread_key_create(&s_wifi_thread_sem_key, wifi_thread_semphr_free)) {
@@ -455,7 +456,7 @@ static uint32_t esp_clk_slowclk_cal_get_wrapper(void)
     /* The bit width of WiFi light sleep clock calibration is 12 while the one of
      * system is 19. It should shift 19 - 12 = 7.
     */
-    return (esp_clk_slowclk_cal_get() >> 7);
+    return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
 }
 
 static void * IRAM_ATTR malloc_internal_wrapper(size_t size)
@@ -536,7 +537,7 @@ static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t
 #endif
 }
 
-static int coex_wifi_release_wrapper(uint32_t event)
+static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
 {
 #if CONFIG_SW_COEXIST_ENABLE
     return coex_wifi_release(event);

+ 76 - 37
components/esp_wifi/esp32s3/esp_adapter.c

@@ -57,19 +57,43 @@ extern void wifi_apb80m_request(void);
 extern void wifi_apb80m_release(void);
 #endif
 
+/*
+ If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
+ If failed, try to allocate it in internal memory then.
+ */
 IRAM_ATTR void *wifi_malloc( size_t size )
 {
+#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
+    return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
+#else
     return malloc(size);
+#endif
 }
 
+/*
+ If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
+ If failed, try to allocate it in internal memory then.
+ */
 IRAM_ATTR void *wifi_realloc( void *ptr, size_t size )
 {
+#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
+    return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
+#else
     return realloc(ptr, size);
+#endif
 }
 
+/*
+ If CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP is enabled. Prefer to allocate a chunk of memory in SPIRAM firstly.
+ If failed, try to allocate it in internal memory then.
+ */
 IRAM_ATTR void *wifi_calloc( size_t n, size_t size )
 {
+#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
+    return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
+#else
     return calloc(n, size);
+#endif
 }
 
 static void * IRAM_ATTR wifi_zalloc_wrapper(size_t size)
@@ -87,14 +111,48 @@ wifi_static_queue_t* wifi_create_queue( int queue_len, int item_size)
         return NULL;
     }
 
+#if CONFIG_SPIRAM_USE_MALLOC
+
+    queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len*item_size), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
+    if (!queue->storage) {
+        goto _error;
+    }
+
+    queue->handle = xQueueCreateStatic( queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage));
+
+    if (!queue->handle) {
+        goto _error;
+    }
+
+    return queue;
+
+_error:
+    if (queue) {
+        if (queue->storage) {
+            free(queue->storage);
+        }
+
+        free(queue);
+    }
+
+    return NULL;
+#else
     queue->handle = xQueueCreate( queue_len, item_size);
     return queue;
+#endif
 }
 
 void wifi_delete_queue(wifi_static_queue_t *queue)
 {
     if (queue) {
         vQueueDelete(queue->handle);
+
+#if CONFIG_SPIRAM_USE_MALLOC
+        if (queue->storage) {
+            free(queue->storage);
+        }
+#endif
+
         free(queue);
     }
 }
@@ -136,7 +194,7 @@ static void set_isr_wrapper(int32_t n, void *f, void *arg)
 static void * spin_lock_create_wrapper(void)
 {
     portMUX_TYPE tmp = portMUX_INITIALIZER_UNLOCKED;
-    void *mux = malloc(sizeof(portMUX_TYPE));
+    void *mux = heap_caps_malloc(sizeof(portMUX_TYPE), MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
 
     if (mux) {
         memcpy(mux,&tmp,sizeof(portMUX_TYPE));
@@ -369,12 +427,12 @@ static void IRAM_ATTR timer_disarm_wrapper(void *timer)
     ets_timer_disarm(timer);
 }
 
-static void IRAM_ATTR timer_done_wrapper(void *ptimer)
+static void timer_done_wrapper(void *ptimer)
 {
     ets_timer_done(ptimer);
 }
 
-static void IRAM_ATTR timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
+static void timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
 {
     ets_timer_setfn(ptimer, pfunction, parg);
 }
@@ -407,14 +465,14 @@ static void IRAM_ATTR wifi_rtc_disable_iso_wrapper(void)
 #endif
 }
 
-static void IRAM_ATTR wifi_clock_enable_wrapper(void)
+static void wifi_clock_enable_wrapper(void)
 {
-    periph_module_enable(PERIPH_WIFI_MODULE);
+    wifi_module_enable();
 }
 
-static void IRAM_ATTR wifi_clock_disable_wrapper(void)
+static void wifi_clock_disable_wrapper(void)
 {
-    periph_module_disable(PERIPH_WIFI_MODULE);
+    wifi_module_disable();
 }
 
 static int get_time_wrapper(void *t)
@@ -422,13 +480,12 @@ static int get_time_wrapper(void *t)
     return os_get_time(t);
 }
 
-#define WIFI_LIGHT_SLEEP_CLK_WIDTH 12
 static uint32_t esp_clk_slowclk_cal_get_wrapper(void)
 {
     /* The bit width of WiFi light sleep clock calibration is 12 while the one of
      * system is 19. It should shift 19 - 12 = 7.
     */
-    return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - WIFI_LIGHT_SLEEP_CLK_WIDTH));
+    return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
 }
 
 static void * IRAM_ATTR malloc_internal_wrapper(size_t size)
@@ -452,29 +509,6 @@ static void * IRAM_ATTR zalloc_internal_wrapper(size_t size)
     return ptr;
 }
 
-static esp_err_t nvs_open_wrapper(const char* name, uint32_t open_mode, nvs_handle_t *out_handle)
-{
-    return nvs_open(name,(nvs_open_mode_t)open_mode, out_handle);
-}
-
-static void esp_log_writev_wrapper(uint32_t level, const char *tag, const char *format, va_list args)
-{
-    return esp_log_writev((esp_log_level_t)level,tag,format,args);
-}
-
-static void esp_log_write_wrapper(uint32_t level,const char *tag,const char *format, ...)
-{
-    va_list list;
-    va_start(list, format);
-    esp_log_writev((esp_log_level_t)level, tag, format, list);
-    va_end(list);
-}
-
-static esp_err_t esp_read_mac_wrapper(uint8_t* mac, uint32_t type)
-{
-    return esp_read_mac(mac, (esp_mac_type_t)type);
-}
-
 static int coex_init_wrapper(void)
 {
 #if CONFIG_SW_COEXIST_ENABLE
@@ -641,6 +675,11 @@ static void IRAM_ATTR esp_empty_wrapper(void)
 
 }
 
+int32_t IRAM_ATTR coex_is_in_isr_wrapper(void)
+{
+    return !xPortCanYield();
+}
+
 wifi_osi_funcs_t g_wifi_osi_funcs = {
     ._version = ESP_WIFI_OS_ADAPTER_VERSION,
     ._env_is_chip = env_is_chip_wrapper,
@@ -697,7 +736,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = {
     ._phy_disable = esp_phy_disable,
     ._phy_enable = esp_phy_enable,
     ._phy_update_country_info = esp_phy_update_country_info,
-    ._read_mac = esp_read_mac_wrapper,
+    ._read_mac = esp_read_mac,
     ._timer_arm = timer_arm_wrapper,
     ._timer_disarm = timer_disarm_wrapper,
     ._timer_done = timer_done_wrapper,
@@ -715,7 +754,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = {
     ._nvs_get_u8 = nvs_get_u8,
     ._nvs_set_u16 = nvs_set_u16,
     ._nvs_get_u16 = nvs_get_u16,
-    ._nvs_open = nvs_open_wrapper,
+    ._nvs_open = nvs_open,
     ._nvs_close = nvs_close,
     ._nvs_commit = nvs_commit,
     ._nvs_set_blob = nvs_set_blob,
@@ -725,8 +764,8 @@ wifi_osi_funcs_t g_wifi_osi_funcs = {
     ._get_time = get_time_wrapper,
     ._random = os_random,
     ._slowclk_cal_get = esp_clk_slowclk_cal_get_wrapper,
-    ._log_write = esp_log_write_wrapper,
-    ._log_writev = esp_log_writev_wrapper,
+    ._log_write = esp_log_write,
+    ._log_writev = esp_log_writev,
     ._log_timestamp = esp_log_timestamp,
     ._malloc_internal =  malloc_internal_wrapper,
     ._realloc_internal = realloc_internal_wrapper,
@@ -769,7 +808,7 @@ coex_adapter_funcs_t g_coex_adapter_funcs = {
     ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
     ._semphr_take = semphr_take_wrapper,
     ._semphr_give = semphr_give_wrapper,
-    ._is_in_isr = xPortInIsrContext,
+    ._is_in_isr = coex_is_in_isr_wrapper,
     ._malloc_internal =  malloc_internal_wrapper,
     ._free = free,
     ._esp_timer_get_time = esp_timer_get_time,

+ 1 - 1
components/esp_wifi/include/esp_wifi.h

@@ -124,7 +124,7 @@ typedef struct {
 #define WIFI_STATIC_TX_BUFFER_NUM 0
 #endif
 
-#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT)
+#if (CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT)
 #define WIFI_CACHE_TX_BUFFER_NUM  CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM
 #else
 #define WIFI_CACHE_TX_BUFFER_NUM  0

+ 1 - 1
components/esp_wifi/src/wifi_init.c

@@ -53,7 +53,7 @@ uint64_t g_wifi_feature_caps =
 #if CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE
     CONFIG_FEATURE_WPA3_SAE_BIT |
 #endif
-#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT)
+#if (CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT)
     CONFIG_FEATURE_CACHE_TX_BUF_BIT |
 #endif
 #if CONFIG_ESP_WIFI_FTM_INITIATOR_SUPPORT

+ 1 - 1
components/esp_wifi/src/wifi_netif.c

@@ -66,7 +66,7 @@ static esp_err_t wifi_transmit(void *h, void *buffer, size_t len)
 static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
 {
     wifi_netif_driver_t driver = h;
-#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT)
+#if (CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT)
     return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf);
 #else
     return esp_wifi_internal_tx(driver->wifi_if, buffer, len);

+ 3 - 0
components/soc/esp32c3/include/soc/soc_caps.h

@@ -128,6 +128,9 @@
 #define SOC_PHY_DIG_REGS_MEM_SIZE       (21*4)
 #define SOC_MAC_BB_PD_MEM_SIZE          (192*4)
 
+/*--------------- WIFI LIGHT SLEEP CLOCK WIDTH CAPS --------------------------*/
+#define SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH  (12)
+
 /*-------------------------- SPI MEM CAPS ---------------------------------------*/
 #define SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE                (1)
 #define SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND                  (1)

+ 3 - 0
components/soc/esp32s2/include/soc/soc_caps.h

@@ -303,6 +303,9 @@
 /*--------------- PHY REGISTER AND MEMORY SIZE CAPS --------------------------*/
 #define SOC_PHY_DIG_REGS_MEM_SIZE       (21*4)
 
+/*--------------- WIFI LIGHT SLEEP CLOCK WIDTH CAPS --------------------------*/
+#define SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH  (12)
+
 /*-------------------------- SPI MEM CAPS ---------------------------------------*/
 #define SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE                (1)
 #define SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND                  (1)

+ 3 - 0
components/soc/esp32s3/include/soc/soc_caps.h

@@ -170,6 +170,9 @@
 #define SOC_PHY_DIG_REGS_MEM_SIZE       (21*4)
 #define SOC_MAC_BB_PD_MEM_SIZE          (192*4)
 
+/*--------------- WIFI LIGHT SLEEP CLOCK WIDTH CAPS --------------------------*/
+#define SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH  (12)
+
 /*-------------------------- SPI MEM CAPS ---------------------------------------*/
 #define SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE                (1)
 #define SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND                  (1)