From 77b3ad8385dfc03a9dbfa79187c17036fa0c78ad Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 11:46:04 +0800 Subject: [PATCH 1/7] eps_wifi: Optimize WiFi debug log 1. Add esp_wifi_statis_dump() 2. Optimize WiFi related debug log --- components/esp32/cpu_start.c | 6 +++++ components/esp32/include/esp_wifi.h | 11 +++++++++ components/esp32/include/esp_wifi_types.h | 6 +++++ components/esp32/wifi_init.c | 30 +++++++++++++++++++++++ 4 files changed, 53 insertions(+) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index b02462630f..713c830138 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -176,6 +176,12 @@ void IRAM_ATTR call_start_cpu0() } #endif +#ifdef CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ + ESP_EARLY_LOGI(TAG, "cpu freq: %d", CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ); +#else + ESP_EARLY_LOGI(TAG, "cpu freq: %d", CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ); +#endif + ESP_EARLY_LOGI(TAG, "Pro cpu up."); if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { const esp_app_desc_t *app_desc = esp_ota_get_app_description(); diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index f920c57bce..b49d62cda3 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -1144,6 +1144,17 @@ esp_err_t esp_wifi_set_inactive_time(wifi_interface_t ifx, uint16_t sec); */ esp_err_t esp_wifi_get_inactive_time(wifi_interface_t ifx, uint16_t *sec); +/** + * @brief Dump WiFi statistics + * + * @param modules statistic modules to be dumped + * + * @return + * - ESP_OK: succeed + * - others: failed + */ +esp_err_t esp_wifi_statis_dump(uint32_t modules); + #ifdef __cplusplus } #endif diff --git a/components/esp32/include/esp_wifi_types.h b/components/esp32/include/esp_wifi_types.h index f7786f5a02..30890af242 100644 --- a/components/esp32/include/esp_wifi_types.h +++ b/components/esp32/include/esp_wifi_types.h @@ -520,6 +520,12 @@ typedef struct { } data; /**< Configuration of ioctl command */ } wifi_ioctl_config_t; +#define WIFI_STATIS_BUFFER (1<<0) +#define WIFI_STATIS_RXTX (1<<1) +#define WIFI_STATIS_HW (1<<2) +#define WIFI_STATIS_DIAG (1<<3) +#define WIFI_STATIS_ALL (-1) + #ifdef __cplusplus } #endif diff --git a/components/esp32/wifi_init.c b/components/esp32/wifi_init.c index be9b4241d5..653b505397 100644 --- a/components/esp32/wifi_init.c +++ b/components/esp32/wifi_init.c @@ -98,6 +98,35 @@ static void esp_wifi_set_debug_log() } +static void esp_wifi_config_info(void) +{ +#ifdef CONFIG_ESP32_WIFI_RX_BA_WIN + ESP_LOGI(TAG, "rx ba win: %d", CONFIG_ESP32_WIFI_RX_BA_WIN); +#endif + ESP_LOGI(TAG, "tcpip mbox: %d", CONFIG_LWIP_TCPIP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "udp mbox: %d", CONFIG_LWIP_UDP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp mbox: %d", CONFIG_LWIP_TCP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp tx win: %d", CONFIG_LWIP_TCP_SND_BUF_DEFAULT); + ESP_LOGI(TAG, "tcp rx win: %d", CONFIG_LWIP_TCP_WND_DEFAULT); + ESP_LOGI(TAG, "tcp mss: %d", CONFIG_LWIP_TCP_MSS); + +#ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + ESP_LOGI(TAG, "WiFi/LWIP prefer SPIRAM"); +#endif + +#ifdef CONFIG_ESP32_WIFI_IRAM_OPT + ESP_LOGI(TAG, "WiFi IRAM OP enabled"); +#endif + +#ifdef CONFIG_ESP32_WIFI_RX_IRAM_OPT + ESP_LOGI(TAG, "WiFi RX IRAM OP enabled"); +#endif + +#ifdef CONFIG_LWIP_IRAM_OPTIMIZATION + ESP_LOGI(TAG, "LWIP IRAM OP enabled"); +#endif +} + esp_err_t esp_wifi_init(const wifi_init_config_t *config) { #ifdef CONFIG_PM_ENABLE @@ -116,6 +145,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) s_wifi_mac_time_update_cb = esp_wifi_internal_update_mac_time; } + esp_wifi_config_info(); return result; } From afad32da9e31436eb903bc4df7801c25ef40f52a Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 13:46:59 +0800 Subject: [PATCH 2/7] esp_wifi: set softap beacon DTIM count according to TSF timer --- components/esp32/include/esp_wifi_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/include/esp_wifi_types.h b/components/esp32/include/esp_wifi_types.h index 30890af242..9b6b08e777 100644 --- a/components/esp32/include/esp_wifi_types.h +++ b/components/esp32/include/esp_wifi_types.h @@ -216,7 +216,7 @@ typedef struct { wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */ uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */ uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */ - uint16_t beacon_interval; /**< Beacon interval, 100 ~ 60000 ms, default 100 ms */ + uint16_t beacon_interval; /**< Beacon interval which should be multiples of 100. Unit: TU(time unit, 1 TU = 1024 us). Range: 100 ~ 60000. Default value: 100 */ } wifi_ap_config_t; /** @brief STA configuration settings for the ESP32 */ From 022192411ea87a31193156bee0489af1195f0b96 Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 14:22:03 +0800 Subject: [PATCH 3/7] esp_wifi: remove wifi tx buffer limits --- components/esp32/Kconfig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index ff2f3ca84c..9e91c732ad 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -1100,8 +1100,8 @@ menu Wi-Fi config ESP32_WIFI_STATIC_RX_BUFFER_NUM int "Max number of WiFi static RX buffers" - range 2 25 if !WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST - range 8 25 if WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST + range 2 25 + default 10 if !WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST default 16 if WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST help @@ -1165,7 +1165,7 @@ menu Wi-Fi config ESP32_WIFI_STATIC_TX_BUFFER_NUM int "Max number of WiFi static TX buffers" depends on ESP32_WIFI_STATIC_TX_BUFFER - range 6 64 + range 1 64 default 16 help Set the number of WiFi static TX buffers. Each buffer takes approximately 1.6KB of RAM. @@ -1180,7 +1180,7 @@ menu Wi-Fi config ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM int "Max number of WiFi dynamic TX buffers" depends on ESP32_WIFI_DYNAMIC_TX_BUFFER - range 16 128 + range 1 128 default 32 help Set the number of WiFi dynamic TX buffers. The size of each dynamic TX buffer is not fixed, From 1e519370dd1c66acd09a5c0f783b395183805bc5 Mon Sep 17 00:00:00 2001 From: dongyou Date: Fri, 4 Dec 2020 19:56:10 +0800 Subject: [PATCH 4/7] Add documentation to avoid using WiFi modem sleep for WiFi/BT coexistence Remove description of unusable API esp_wifi_restart() Update esp_now_fetch_peer discription Update table format prblm for esp_wifi_set_max_tx_power() Update description for ssid and password --- components/esp32/esp_err_to_name.c | 2 +- components/esp32/include/esp_now.h | 2 +- components/esp32/include/esp_wifi.h | 32 ++++------------------------- docs/en/api-guides/wifi.rst | 8 +++----- 4 files changed, 9 insertions(+), 35 deletions(-) diff --git a/components/esp32/esp_err_to_name.c b/components/esp32/esp_err_to_name.c index 621454c924..8e80a7935f 100644 --- a/components/esp32/esp_err_to_name.c +++ b/components/esp32/esp_err_to_name.c @@ -329,7 +329,7 @@ static const esp_err_msg_t esp_err_msg_table[] = { ERR_TBL_IT(ESP_ERR_WIFI_POST), /* 12306 0x3012 Failed to post the event to WiFi task */ # endif # ifdef ESP_ERR_WIFI_INIT_STATE - ERR_TBL_IT(ESP_ERR_WIFI_INIT_STATE), /* 12307 0x3013 Invalod WiFi state when init/deinit is called */ + ERR_TBL_IT(ESP_ERR_WIFI_INIT_STATE), /* 12307 0x3013 Invalid WiFi state when init/deinit is called */ # endif # ifdef ESP_ERR_WIFI_STOP_STATE ERR_TBL_IT(ESP_ERR_WIFI_STOP_STATE), /* 12308 0x3014 Returned when WiFi is stopping */ diff --git a/components/esp32/include/esp_now.h b/components/esp32/include/esp_now.h index 981c9001e4..2f20c5d392 100644 --- a/components/esp32/include/esp_now.h +++ b/components/esp32/include/esp_now.h @@ -252,7 +252,7 @@ esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer); esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer); /** - * @brief Fetch a peer from peer list + * @brief Fetch a peer from peer list. Only return the peer which address is unicast, for the multicast/broadcast address, the function will ignore and try to find the next in the peer list. * * @param from_head fetch from head of list or not * @param peer peer information diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index b49d62cda3..ca07d2a8e3 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -85,7 +85,7 @@ extern "C" { #define ESP_ERR_WIFI_NOT_CONNECT (ESP_ERR_WIFI_BASE + 15) /*!< Station still in disconnect status */ #define ESP_ERR_WIFI_POST (ESP_ERR_WIFI_BASE + 18) /*!< Failed to post the event to WiFi task */ -#define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalod WiFi state when init/deinit is called */ +#define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalid WiFi state when init/deinit is called */ #define ESP_ERR_WIFI_STOP_STATE (ESP_ERR_WIFI_BASE + 20) /*!< Returned when WiFi is stopping */ /** @@ -304,7 +304,7 @@ esp_err_t esp_wifi_stop(void); * @brief Restore WiFi stack persistent settings to default values * * This function will reset settings made using the following APIs: - * - esp_wifi_get_auto_connect, + * - esp_wifi_set_bandwidth, * - esp_wifi_set_protocol, * - esp_wifi_set_config related * - esp_wifi_set_mode @@ -904,32 +904,8 @@ esp_err_t esp_wifi_set_vendor_ie_cb(esp_vendor_ie_cb_t cb, void *ctx); * @attention 3. Mapping Table {Power, max_tx_power} = {{8, 2}, {20, 5}, {28, 7}, {34, 8}, {44, 11}, * {52, 13}, {56, 14}, {60, 15}, {66, 16}, {72, 18}, {78, 20}}. * @attention 4. Param power unit is 0.25dBm, range is [8, 78] corresponding to 2dBm - 20dBm. - * @attention 5. Relationship between set value and actual value. As follows: - * +------------+--------------+ - * | set value | actual value | - * +============+==============+ - * | [8, 19] | 8 | - * +------------+--------------+ - * | [20, 27] | 20 | - * +------------+--------------+ - * | [28, 33] | 28 | - * +------------+--------------+ - * | [34, 43] | 34 | - * +------------+--------------+ - * | [44, 51] | 44 | - * +------------+--------------+ - * | [52, 55] | 52 | - * +------------+--------------+ - * | [56, 59] | 56 | - * +------------+--------------+ - * | [60, 65] | 60 | - * +------------+--------------+ - * | [66, 71] | 66 | - * +------------+--------------+ - * | [72, 77] | 72 | - * +------------+--------------+ - * | 78 | 78 | - * +------------+--------------+ + * @attention 5. Relationship between set value and actual value. As follows: {set value range, actual value} = {{[8, 19],8}, {[20, 27],20}, {[28, 33],28}, {[34, 43],34}, {[44, 51],44}, {[52, 55],52}, {[56, 59],56}, {[60, 65],60}, {[66, 71],66}, {[72, 77],72}, {78,78}} + * * @param power Maximum WiFi transmitting power. * * @return diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index 3d1917745c..5d175dda94 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -172,7 +172,7 @@ SYSTEM_EVENT_STA_DISCONNECTED ++++++++++++++++++++++++++++++++++++ This event can be generated in the following scenarios: - - When esp_wifi_disconnect(), or esp_wifi_stop(), or esp_wifi_deinit(), or esp_wifi_restart() is called and the station is already connected to the AP. + - When :cpp:func:`esp_wifi_disconnect()`, or :cpp:func:`esp_wifi_stop()`, or :cpp:func:`esp_wifi_deinit()` is called and the station is already connected to the AP. - When esp_wifi_connect() is called, but the Wi-Fi driver fails to set up a connection with the AP due to certain reasons, e.g. the scan fails to find the target AP, authentication times out, etc. If there are more than one AP with the same SSID, the disconnected event is raised after the station fails to connect all of the found APs. - When the Wi-Fi connection is disrupted because of specific reasons, e.g., the station continuously loses N beacons, the AP kicks off the station, the AP's authentication mode is changed, etc. @@ -234,7 +234,7 @@ SYSTEM_EVENT_AP_STADISCONNECTED This event can happen in the following scenarios: - The application calls esp_wifi_disconnect(), or esp_wifi_deauth_sta(), to manually disconnect the station. - - The Wi-Fi driver kicks off the station, e.g. because the AP has not received any packets in the past five minutes, etc. + - The Wi-Fi driver kicks off the station, e.g. because the AP has not received any packets in the past five minutes, etc. The time can be modified by :cpp:func:`esp_wifi_set_inactive_time`. - The station kicks off the AP. When this event happens, the event task will do nothing, but the application event callback needs to do something, e.g., close the socket which is related to this station, etc. @@ -1418,7 +1418,7 @@ In maximum power save mode, station wakes up every listen interval to receive be Call ``esp_wifi_set_ps(WIFI_PS_MIN_MODEM)`` to enable Modem-sleep minimum power save mode or ``esp_wifi_set_ps(WIFI_PS_MAX_MODEM)`` to enable Modem-sleep maximum power save mode after calling :cpp:func:`esp_wifi_init`. When station connects to AP, Modem-sleep will start. When station disconnects from AP, Modem-sleep will stop. -Call `esp_wifi_set_ps(WIFI_PS_MIN_MODEM)` to disable modem sleep entirely. This has much higher power consumption, but provides minimum latency for receiving Wi-Fi data in real time. When modem sleep is enabled, received Wi-Fi data can be delayed for as long as the DTIM period (minimum power save mode) or the listen interval (maximum power save mode). +Call ``esp_wifi_set_ps(WIFI_PS_NONE)`` to disable modem sleep entirely. This has much higher power consumption, but provides minimum latency for receiving Wi-Fi data in real time. When modem sleep is enabled, received Wi-Fi data can be delayed for as long as the DTIM period (minimum power save mode) or the listen interval (maximum power save mode). Disabling modem sleep entirely is not possible for Wi-Fi and Bluetooth coexist mode. The default Modem-sleep mode is WIFI_PS_MIN_MODEM. @@ -1465,8 +1465,6 @@ When the throughput is tested by iperf example, the sdkconfig is :idf_file:`exam Wi-Fi 80211 Packet Send --------------------------- -**Important notes: The API esp_wifi_80211_tx is not available in IDF 2.1, but will be so in the upcoming release.** - The esp_wifi_80211_tx API can be used to: - Send the beacon, probe request, probe response, action frame. From 42061dded1c1114369bd1098050181cb85c13a24 Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 15:49:58 +0800 Subject: [PATCH 5/7] esp_wifi: add limitation for setting maximum WiFi TX power Limit the WiFi TX power range to 10dBm ~ 20.5 dBm --- components/esp32/Kconfig | 2 +- components/esp32/phy_init_data.h | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 9e91c732ad..5ad4f26a99 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -1418,7 +1418,7 @@ menu PHY config ESP32_PHY_MAX_WIFI_TX_POWER int "Max WiFi TX power (dBm)" - range 0 20 + range 10 20 default 20 help Set maximum transmit power for WiFi radio. Actual transmit power for high diff --git a/components/esp32/phy_init_data.h b/components/esp32/phy_init_data.h index c44307505c..2cb12a7378 100644 --- a/components/esp32/phy_init_data.h +++ b/components/esp32/phy_init_data.h @@ -77,12 +77,12 @@ static const esp_phy_init_data_t phy_init_data= { { 0x18, 0x18, 0x18, - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 78), - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 72), - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 66), - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 60), - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 56), - LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 0, 52), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 78), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 72), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 66), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 60), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 56), + LIMIT(CONFIG_ESP32_PHY_MAX_TX_POWER * 4, 40, 52), 0, 1, 1, From 7b245e59c446ac3c1228087cdf7649bdfc5477d2 Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 15:40:14 +0800 Subject: [PATCH 6/7] esp_wifi: optimize WiFi TX performance --- components/esp32/Kconfig | 16 +++- components/esp32/event_default_handlers.c | 2 + components/esp32/include/esp_wifi.h | 20 +++-- components/esp32/include/esp_wifi_internal.h | 76 +++++++++++++++---- components/lwip/port/esp32/netif/wlanif.c | 25 +++++- .../tcpip_adapter/include/tcpip_adapter.h | 16 ++++ components/tcpip_adapter/tcpip_adapter_lwip.c | 10 +++ 7 files changed, 138 insertions(+), 27 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 5ad4f26a99..760bf94e9d 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -1177,6 +1177,19 @@ menu Wi-Fi layer can deliver frames faster than WiFi layer can transmit. In these cases, we may run out of TX buffers. + config ESP32_WIFI_CACHE_TX_BUFFER_NUM + int "Max number of WiFi cache TX buffers" + depends on (ESP32_SPIRAM_SUPPORT || ESP32S2_SPIRAM_SUPPORT) + range 16 128 + default 32 + help + Set the number of WiFi cache TX buffer number. + + For each TX packet from uplayer, such as LWIP etc, WiFi driver needs to allocate a static TX + buffer and makes a copy of uplayer packet. If WiFi driver fails to allocate the static TX buffer, + it caches the uplayer packets to a dedicated buffer queue, this option is used to configure the + size of the cached TX queue. + config ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM int "Max number of WiFi dynamic TX buffers" depends on ESP32_WIFI_DYNAMIC_TX_BUFFER @@ -1226,8 +1239,7 @@ menu Wi-Fi config ESP32_WIFI_RX_BA_WIN int "WiFi AMPDU RX BA window size" depends on ESP32_WIFI_AMPDU_RX_ENABLED - range 2 32 if !WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST - range 16 32 if WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST + range 2 32 default 6 if !WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST default 16 if WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST help diff --git a/components/esp32/event_default_handlers.c b/components/esp32/event_default_handlers.c index d1c2e13be1..fba4ffc68b 100644 --- a/components/esp32/event_default_handlers.c +++ b/components/esp32/event_default_handlers.c @@ -210,6 +210,7 @@ esp_err_t system_event_ap_start_handle_default(system_event_t *event) uint8_t ap_mac[6]; WIFI_API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)tcpip_adapter_ap_input), ESP_OK); + API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_AP, ap_mac), ESP_OK); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ap_ip); @@ -232,6 +233,7 @@ esp_err_t system_event_sta_start_handle_default(system_event_t *event) tcpip_adapter_ip_info_t sta_ip; uint8_t sta_mac[6]; + API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac), ESP_OK); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); tcpip_adapter_sta_start(sta_mac, &sta_ip); diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index ca07d2a8e3..e0e718d93b 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -87,6 +87,8 @@ extern "C" { #define ESP_ERR_WIFI_POST (ESP_ERR_WIFI_BASE + 18) /*!< Failed to post the event to WiFi task */ #define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalid WiFi state when init/deinit is called */ #define ESP_ERR_WIFI_STOP_STATE (ESP_ERR_WIFI_BASE + 20) /*!< Returned when WiFi is stopping */ +#define ESP_ERR_WIFI_NOT_ASSOC (ESP_ERR_WIFI_BASE + 21) /*!< The WiFi connection is not associated */ +#define ESP_ERR_WIFI_TX_DISALLOW (ESP_ERR_WIFI_BASE + 22) /*!< The WiFi TX is disallowed */ /** * @brief WiFi stack configuration parameters passed to esp_wifi_init call. @@ -100,12 +102,12 @@ typedef struct { int tx_buf_type; /**< WiFi TX buffer type */ int static_tx_buf_num; /**< WiFi static TX buffer number */ int dynamic_tx_buf_num; /**< WiFi dynamic TX buffer number */ + int cache_tx_buf_num; /**< WiFi TX cache buffer number */ int csi_enable; /**< WiFi channel state information enable flag */ int ampdu_rx_enable; /**< WiFi AMPDU RX feature enable flag */ int ampdu_tx_enable; /**< WiFi AMPDU TX feature enable flag */ int nvs_enable; /**< WiFi NVS flash enable flag */ int nano_enable; /**< Nano option for printf/scan family enable flag */ - int tx_ba_win; /**< WiFi Block Ack TX window size */ int rx_ba_win; /**< WiFi Block Ack RX window size */ int wifi_task_core_id; /**< WiFi Task Core ID */ int beacon_max_len; /**< WiFi softAP maximum length of the beacon */ @@ -119,6 +121,12 @@ typedef struct { #define WIFI_STATIC_TX_BUFFER_NUM 0 #endif +#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT) +#define WIFI_CACHE_TX_BUFFER_NUM CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM +#else +#define WIFI_CACHE_TX_BUFFER_NUM 0 +#endif + #ifdef CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM #define WIFI_DYNAMIC_TX_BUFFER_NUM CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM #else @@ -159,12 +167,6 @@ extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; #define WIFI_INIT_CONFIG_MAGIC 0x1F2F3F4F -#ifdef CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED -#define WIFI_DEFAULT_TX_BA_WIN CONFIG_ESP32_WIFI_TX_BA_WIN -#else -#define WIFI_DEFAULT_TX_BA_WIN 0 /* unused if ampdu_tx_enable == false */ -#endif - #ifdef CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED #define WIFI_DEFAULT_RX_BA_WIN CONFIG_ESP32_WIFI_RX_BA_WIN #else @@ -189,6 +191,8 @@ extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; #define WIFI_MGMT_SBUF_NUM 32 #endif +#define CONFIG_FEATURE_CACHE_TX_BUF_BIT (1<<1) + #define WIFI_INIT_CONFIG_DEFAULT() { \ .event_handler = &esp_event_send, \ .osi_funcs = &g_wifi_osi_funcs, \ @@ -198,12 +202,12 @@ extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; .tx_buf_type = CONFIG_ESP32_WIFI_TX_BUFFER_TYPE,\ .static_tx_buf_num = WIFI_STATIC_TX_BUFFER_NUM,\ .dynamic_tx_buf_num = WIFI_DYNAMIC_TX_BUFFER_NUM,\ + .cache_tx_buf_num = WIFI_CACHE_TX_BUFFER_NUM,\ .csi_enable = WIFI_CSI_ENABLED,\ .ampdu_rx_enable = WIFI_AMPDU_RX_ENABLED,\ .ampdu_tx_enable = WIFI_AMPDU_TX_ENABLED,\ .nvs_enable = WIFI_NVS_ENABLED,\ .nano_enable = WIFI_NANO_FORMAT_ENABLED,\ - .tx_ba_win = WIFI_DEFAULT_TX_BA_WIN,\ .rx_ba_win = WIFI_DEFAULT_RX_BA_WIN,\ .wifi_task_core_id = WIFI_TASK_CORE_ID,\ .beacon_max_len = WIFI_SOFTAP_BEACON_MAX_LEN, \ diff --git a/components/esp32/include/esp_wifi_internal.h b/components/esp32/include/esp_wifi_internal.h index 71d0a9c24f..957b243fea 100644 --- a/components/esp32/include/esp_wifi_internal.h +++ b/components/esp32/include/esp_wifi_internal.h @@ -100,15 +100,6 @@ typedef enum { */ esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config); -/** - * @brief get whether the wifi driver is allowed to transmit data or not - * - * @return - * - true : upper layer should stop to transmit data to wifi driver - * - false : upper layer can transmit data to wifi driver - */ -bool esp_wifi_internal_tx_is_stop(void); - /** * @brief free the rx buffer which allocated by wifi driver * @@ -119,18 +110,77 @@ void esp_wifi_internal_free_rx_buffer(void* buffer); /** * @brief transmit the buffer via wifi driver * + * This API makes a copy of the input buffer and then forwards the buffer + * copy to WiFi driver. * @param wifi_interface_t wifi_if : wifi interface id * @param void *buffer : the buffer to be tansmit * @param uint16_t len : the length of buffer * * @return - * - ERR_OK : Successfully transmit the buffer to wifi driver - * - ERR_MEM : Out of memory - * - ERR_IF : WiFi driver error - * - ERR_ARG : Invalid argument + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - ESP_ERR_NO_MEM: out of memory + * - ESP_ERR_WIFI_ARG: invalid argument + * - ESP_ERR_WIFI_IF : WiFi interface is invalid + * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode + * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started + * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started + * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated + * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication + * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task */ int esp_wifi_internal_tx(wifi_interface_t wifi_if, void *buffer, uint16_t len); +/** + * @brief The net stack buffer reference counter callback function + * + */ +typedef void (*wifi_netstack_buf_ref_cb_t)(void *netstack_buf); + +/** + * @brief The net stack buffer free callback function + * + */ +typedef void (*wifi_netstack_buf_free_cb_t)(void *netstack_buf); + +/** + * @brief transmit the buffer by reference via wifi driver + * + * This API firstly increases the reference counter of the input buffer and + * then forwards the buffer to WiFi driver. The WiFi driver will free the buffer + * after processing it. Use esp_wifi_internal_tx() if the uplayer buffer doesn't + * supports reference counter. + * + * @param wifi_if : wifi interface id + * @param buffer : the buffer to be tansmit + * @param len : the length of buffer + * @param netstack_buf : the netstack buffer related to bufffer + * + * @return + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - ESP_ERR_NO_MEM: out of memory + * - ESP_ERR_WIFI_ARG: invalid argument + * - ESP_ERR_WIFI_IF : WiFi interface is invalid + * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode + * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started + * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started + * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated + * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication + * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task + */ +esp_err_t esp_wifi_internal_tx_by_ref(wifi_interface_t ifx, void *buffer, size_t len, void *netstack_buf); + +/** + * @brief register the net stack buffer reference increasing and free callback + * + * @param ref : net stack buffer reference callback + * @param free: net stack buffer free callback + * + * @return + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - others : failed to register the callback + */ +esp_err_t esp_wifi_internal_reg_netstack_buf_cb(wifi_netstack_buf_ref_cb_t ref, wifi_netstack_buf_free_cb_t free); + /** * @brief The WiFi RX callback function * diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index ed5e13ed12..72941133d9 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -92,6 +92,15 @@ low_level_init(struct netif *netif) #endif } +static esp_err_t wifi_transmit_wrap(wifi_interface_t wifi_if, void *buffer, size_t len, void *netstack_buf) +{ +#if (CONFIG_ESP32_SPIRAM_SUPPORT) + return esp_wifi_internal_tx_by_ref(wifi_if, buffer, len, netstack_buf); +#else + return esp_wifi_internal_tx(wifi_if, buffer, len); +#endif +} + /** * This function should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf @@ -112,14 +121,14 @@ low_level_output(struct netif *netif, struct pbuf *p) { wifi_interface_t wifi_if = tcpip_adapter_get_esp_if(netif); struct pbuf *q = p; - err_t ret; + esp_err_t ret; if (wifi_if >= ESP_IF_MAX) { return ERR_IF; } if(q->next == NULL) { - ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len); + ret = wifi_transmit_wrap(wifi_if, q->payload, q->len, q); } else { LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug")); q = pbuf_alloc(PBUF_RAW_TX, p->tot_len, PBUF_RAM); @@ -129,11 +138,19 @@ low_level_output(struct netif *netif, struct pbuf *p) } else { return ERR_MEM; } - ret = esp_wifi_internal_tx(wifi_if, q->payload, q->len); + ret = wifi_transmit_wrap(wifi_if, q->payload, q->len, q); pbuf_free(q); } - return ret; + if (ret == ESP_OK) { + return ERR_OK; + } else if (ret == ESP_ERR_NO_MEM) { + return ERR_MEM; + } else if (ret == ESP_ERR_INVALID_ARG) { + return ERR_ARG; + } else { + return ERR_IF; + } } /** diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 848bf16adc..d78b06288a 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -676,6 +676,22 @@ esp_err_t tcpip_adapter_get_netif(tcpip_adapter_if_t tcpip_if, void ** netif); */ bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if); +/** + * @brief increase the reference counter of net stack buffer + * + * @param[in] netstack_buf the net stack buffer + * + */ +void esp_netif_netstack_buf_ref(void *netstack_buf); + +/** + * @brief free the netstack buffer + * + * @param[in] netstack_buf the net stack buffer + * + */ +void esp_netif_netstack_buf_free(void *netstack_buf); + #ifdef __cplusplus } #endif diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 03f9bfb64d..c632239bab 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -1285,4 +1285,14 @@ bool tcpip_adapter_is_netif_up(tcpip_adapter_if_t tcpip_if) } } +void esp_netif_netstack_buf_ref(void *pbuf) +{ + pbuf_ref(pbuf); +} + +void esp_netif_netstack_buf_free(void *pbuf) +{ + pbuf_free(pbuf); +} + #endif /* CONFIG_TCPIP_LWIP */ From 65a1e1269f8922a704e670e7e9d0226e8662faf0 Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 7 Dec 2020 17:51:44 +0800 Subject: [PATCH 7/7] esp_wifi: Update wifi lib 1. Optimize WiFi log 2. Optimize 4way handshake failure time 3. Fix eapol frames encryption during reauth 4. Set softap beacon DTIM count according to TSF timer 5. Remove wifi tx buffer limits 6. Remove coexist warnings in ESP_WIFI_MESH 7. Update some APIs descriptions, tables format and typos 8. Fix wifi tx all --- components/esp32/esp_err_to_name.c | 6 ++++++ components/esp32/event_default_handlers.c | 4 ++-- components/esp32/include/esp_wifi.h | 3 +++ components/esp32/lib | 2 +- components/esp32/wifi_init.c | 23 +++++++++++++++++------ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/components/esp32/esp_err_to_name.c b/components/esp32/esp_err_to_name.c index 8e80a7935f..4cfa11cb6c 100644 --- a/components/esp32/esp_err_to_name.c +++ b/components/esp32/esp_err_to_name.c @@ -333,6 +333,12 @@ static const esp_err_msg_t esp_err_msg_table[] = { # endif # ifdef ESP_ERR_WIFI_STOP_STATE ERR_TBL_IT(ESP_ERR_WIFI_STOP_STATE), /* 12308 0x3014 Returned when WiFi is stopping */ +# endif +# ifdef ESP_ERR_WIFI_NOT_ASSOC + ERR_TBL_IT(ESP_ERR_WIFI_NOT_ASSOC), /* 12309 0x3015 The WiFi connection is not associated */ +# endif +# ifdef ESP_ERR_WIFI_TX_DISALLOW + ERR_TBL_IT(ESP_ERR_WIFI_TX_DISALLOW), /* 12310 0x3016 The WiFi TX is disallowed */ # endif // components/esp32/include/esp_wps.h # ifdef ESP_ERR_WIFI_REGISTRAR diff --git a/components/esp32/event_default_handlers.c b/components/esp32/event_default_handlers.c index fba4ffc68b..0e03097812 100644 --- a/components/esp32/event_default_handlers.c +++ b/components/esp32/event_default_handlers.c @@ -210,7 +210,7 @@ esp_err_t system_event_ap_start_handle_default(system_event_t *event) uint8_t ap_mac[6]; WIFI_API_CALL_CHECK("esp_wifi_internal_reg_rxcb", esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_AP, (wifi_rxcb_t)tcpip_adapter_ap_input), ESP_OK); - API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); + WIFI_API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_AP, ap_mac), ESP_OK); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ap_ip); @@ -233,7 +233,7 @@ esp_err_t system_event_sta_start_handle_default(system_event_t *event) tcpip_adapter_ip_info_t sta_ip; uint8_t sta_mac[6]; - API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); + WIFI_API_CALL_CHECK("esp_wifi_internal_reg_netstack_buf_cb",esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free), ESP_OK); WIFI_API_CALL_CHECK("esp_wifi_mac_get", esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac), ESP_OK); tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &sta_ip); tcpip_adapter_sta_start(sta_mac, &sta_ip); diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index e0e718d93b..96772ab742 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -112,6 +112,7 @@ typedef struct { int wifi_task_core_id; /**< WiFi Task Core ID */ int beacon_max_len; /**< WiFi softAP maximum length of the beacon */ int mgmt_sbuf_num; /**< WiFi management short buffer number, the minimum value is 6, the maximum value is 32 */ + uint64_t feature_caps; /**< Enables additional WiFi features and capabilities */ int magic; /**< WiFi init magic number, it should be the last field */ } wifi_init_config_t; @@ -164,6 +165,7 @@ typedef struct { #endif extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; +extern uint64_t g_wifi_feature_caps; #define WIFI_INIT_CONFIG_MAGIC 0x1F2F3F4F @@ -212,6 +214,7 @@ extern const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs; .wifi_task_core_id = WIFI_TASK_CORE_ID,\ .beacon_max_len = WIFI_SOFTAP_BEACON_MAX_LEN, \ .mgmt_sbuf_num = WIFI_MGMT_SBUF_NUM, \ + .feature_caps = g_wifi_feature_caps, \ .magic = WIFI_INIT_CONFIG_MAGIC\ }; diff --git a/components/esp32/lib b/components/esp32/lib index e43116f1cb..80d7f67aab 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit e43116f1cbce75e70a3b0d66e900e404010493eb +Subproject commit 80d7f67aab9dc7331300476192a25fecc31e364f diff --git a/components/esp32/wifi_init.c b/components/esp32/wifi_init.c index 653b505397..c62f8d478a 100644 --- a/components/esp32/wifi_init.c +++ b/components/esp32/wifi_init.c @@ -32,6 +32,16 @@ /* mesh event callback handler */ mesh_event_cb_t g_mesh_event_cb = NULL; +/* Set additional WiFi features and capabilities */ +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) + CONFIG_FEATURE_CACHE_TX_BUF_BIT | +#endif +0; + #ifdef CONFIG_PM_ENABLE static esp_pm_lock_handle_t s_wifi_modem_sleep_lock; #endif @@ -40,6 +50,7 @@ static esp_pm_lock_handle_t s_wifi_modem_sleep_lock; wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb = NULL; static bool s_wifi_adc_xpd_flag; +static const char* TAG = "wifi_init"; static void __attribute__((constructor)) s_set_default_wifi_log_level() { @@ -103,12 +114,12 @@ static void esp_wifi_config_info(void) #ifdef CONFIG_ESP32_WIFI_RX_BA_WIN ESP_LOGI(TAG, "rx ba win: %d", CONFIG_ESP32_WIFI_RX_BA_WIN); #endif - ESP_LOGI(TAG, "tcpip mbox: %d", CONFIG_LWIP_TCPIP_RECVMBOX_SIZE); - ESP_LOGI(TAG, "udp mbox: %d", CONFIG_LWIP_UDP_RECVMBOX_SIZE); - ESP_LOGI(TAG, "tcp mbox: %d", CONFIG_LWIP_TCP_RECVMBOX_SIZE); - ESP_LOGI(TAG, "tcp tx win: %d", CONFIG_LWIP_TCP_SND_BUF_DEFAULT); - ESP_LOGI(TAG, "tcp rx win: %d", CONFIG_LWIP_TCP_WND_DEFAULT); - ESP_LOGI(TAG, "tcp mss: %d", CONFIG_LWIP_TCP_MSS); + ESP_LOGI(TAG, "tcpip mbox: %d", CONFIG_TCPIP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "udp mbox: %d", CONFIG_UDP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp mbox: %d", CONFIG_TCP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp tx win: %d", CONFIG_TCP_SND_BUF_DEFAULT); + ESP_LOGI(TAG, "tcp rx win: %d", CONFIG_TCP_WND_DEFAULT); + ESP_LOGI(TAG, "tcp mss: %d", CONFIG_TCP_MSS); #ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP ESP_LOGI(TAG, "WiFi/LWIP prefer SPIRAM");