From 53813599e96e2a3476d60021ded4faaed621d664 Mon Sep 17 00:00:00 2001 From: Zhang Jun Hao Date: Mon, 12 Aug 2019 14:06:25 +0800 Subject: [PATCH] feat(wifi): refactor and add more wifi feature for 11n certificate --- components/esp8266/Kconfig | 99 ++++++++++++++++++- components/esp8266/include/esp_wifi.h | 89 ++++++++++++----- .../include/internal/esp_wifi_internal.h | 4 +- components/esp8266/source/esp_wifi.c | 9 ++ components/esp8266/source/phy.h | 2 + components/esp8266/source/phy_init.c | 20 ++-- components/esp8266/source/startup.c | 22 ++++- 7 files changed, 204 insertions(+), 41 deletions(-) diff --git a/components/esp8266/Kconfig b/components/esp8266/Kconfig index 67bdcdb4..9b8d8e76 100644 --- a/components/esp8266/Kconfig +++ b/components/esp8266/Kconfig @@ -323,7 +323,7 @@ config ESP_ERR_TO_NAME_LOOKUP endmenu -menu WIFI +menu Wi-Fi config SCAN_AP_MAX int "Max scan AP number" @@ -336,12 +336,84 @@ config SCAN_AP_MAX User can use own function "esp_wifi_scan_get_ap_num_max" to determin how many AP to scan , too. config WIFI_TX_RATE_SEQUENCE_FROM_HIGH - bool "Set wifi tx rate from 54M to 1M" + bool "Set WiFi TX rate from 54M to 1M" default y help - If this option is enabled, Wifi will try to send packets first from high rate(54M). If it fails, it will + If this option is enabled, WiFi will try to send packets first from high rate(54M). If it fails, it will try at low rate until the transmission is successful. +config ESP8266_WIFI_QOS_ENABLED + bool "WiFi QoS" + default n + help + Select this option to enable QoS feature + +config ESP8266_WIFI_AMPDU_RX_ENABLED + bool "WiFi AMPDU RX" + default n + help + Select this option to enable AMPDU RX feature + +config ESP8266_WIFI_RX_BA_WIN_SIZE + int "WiFi AMPDU RX BA window size" + depends on ESP8266_WIFI_AMPDU_RX_ENABLED + range 0 16 + default 6 + help + Set the size of WiFi Block Ack RX window. Generally a bigger value means higher throughput and better + compatibility but more memory. + +config ESP8266_WIFI_AMSDU_ENABLED + bool "WiFi AMSDU RX" + default n + help + Select this option to enable AMSDU feature + +config ESP8266_WIFI_RX_BUFFER_NUM + int "Max number of WiFi RX buffers" + range 14 28 + default 16 + help + Set the number of WiFi RX buffers. Each buffer takes approximately ESP8266_WIFI_RX_BUFFER_LEN bytes of RAM. + + WiFi hardware use these buffers to receive all 802.11 frames. + A higher number may allow higher throughput but increases memory use. If ESP8266_WIFI_AMPDU_RX_ENABLED + is enabled, this value is recommended to set equal or bigger than 8*1024/ESP8266_WIFI_RX_BUFFER_LEN in order + to achieve better throughput and compatibility with both stations and APs. + Most of time we should NOT change the default value unless special reason, e.g. test the maximum UDP RX + throughput with iperf etc. For iperf test in shieldbox, the recommended value is 26. + +config ESP8266_WIFI_LEFT_CONTINUOUS_RX_BUFFER_NUM + int "The min number of WiFi continuous RX buffer" + range 0 16 + default 4 + help + Set the number of WiFi continuous RX buffer num. + The smaller the value, the easier RX hang will appear. Most of time we should NOT change the default + value unless special reason. + +config ESP8266_WIFI_RX_PKT_NUM + int "Max number of WiFi RX packets" + range 4 16 + default 7 + help + Set the max number of signle WiFi packet. + +config ESP8266_WIFI_TX_PKT_NUM + int "Max number of WiFi TX packets" + range 4 16 + default 6 + help + Set the number of WiFi TX packets. Each buffer takes approximately 1.6KB of RAM. + For some applications especially UDP applications, the upper layer can deliver frames faster than WiFi + layer can transmit. In these cases, we may run out of TX packets. + +config ESP8266_WIFI_NVS_ENABLED + bool "WiFi NVS flash" + default y + help + Select this option to enable WiFi NVS flash + config ESP8266_WIFI_DEBUG_LOG_ENABLE bool "Enable WiFi debug log" default n @@ -463,6 +535,27 @@ config ESP8266_WIFI_DEBUG_LOG_SUBMODULE_WPS help When this option is enabled, log for wps module will be enabled. +config ESP8266_WIFI_DEBUG_LOG_SUBMODULE_AMPDU + depends on ESP8266_WIFI_DEBUG_LOG_SUBMODULE + bool "ampdu" + default n + help + When this option is enabled, log for ampdu module will be enabled. + +config ESP8266_WIFI_DEBUG_LOG_SUBMODULE_AMSDU + depends on ESP8266_WIFI_DEBUG_LOG_SUBMODULE + bool "amsdu" + default n + help + When this option is enabled, log for amsdu module will be enabled. + +config ESP8266_WIFI_DEBUG_LOG_SUBMODULE_FRAG + depends on ESP8266_WIFI_DEBUG_LOG_SUBMODULE + bool "fragment" + default n + help + When this option is enabled, log for frag module will be enabled. + endmenu menu PHY diff --git a/components/esp8266/include/esp_wifi.h b/components/esp8266/include/esp_wifi.h index 8379a19a..9bee0568 100644 --- a/components/esp8266/include/esp_wifi.h +++ b/components/esp8266/include/esp_wifi.h @@ -97,40 +97,77 @@ extern "C" { * @brief WiFi stack configuration parameters passed to esp_wifi_init call. */ typedef struct { - system_event_handler_t event_handler; /**< WiFi event handler */ - void* osi_funcs; /**< WiFi OS functions */ - int static_rx_buf_num; /**< WiFi static RX buffer number */ - int dynamic_rx_buf_num; /**< WiFi dynamic RX buffer number */ - 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 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 magic; /**< WiFi init magic number, it should be the last field */ + system_event_handler_t event_handler; /**< WiFi event handler */ + void* osi_funcs; /**< WiFi OS functions */ + uint8_t qos_enable; /**< WiFi QOS feature enable flag */ + uint8_t ampdu_rx_enable; /**< WiFi AMPDU RX feature enable flag */ + uint8_t rx_ba_win; /**< WiFi Block Ack RX window size */ + uint8_t rx_ampdu_buf_num; /**< WiFi AMPDU RX buffer number */ + uint32_t rx_ampdu_buf_len; /**< WiFi AMPDU RX buffer length */ + uint32_t rx_max_single_pkt_len; /**< WiFi RX max single packet size */ + uint32_t rx_buf_len; /**< WiFi RX buffer size */ + uint8_t amsdu_rx_enable; /**< WiFi AMSDU RX feature enable flag */ + uint8_t rx_buf_num; /**< WiFi RX buffer number */ + uint8_t rx_pkt_num; /**< WiFi RX packet number */ + uint8_t left_continuous_rx_buf_num; /**< WiFi Rx left continuous rx buffer number */ + uint8_t tx_buf_num; /**< WiFi TX buffer number */ + uint8_t nvs_enable; /**< WiFi NVS flash enable flag */ + uint8_t nano_enable; /**< Nano option for printf/scan family enable flag */ + uint32_t magic; /**< WiFi init magic number, it should be the last field */ } wifi_init_config_t; +#if CONFIG_ESP8266_WIFI_AMPDU_RX_ENABLED +#define WIFI_AMPDU_RX_ENABLED 1 +#define WIFI_AMPDU_RX_BA_WIN CONFIG_ESP8266_WIFI_RX_BA_WIN_SIZE +#define WIFI_RX_MAX_SINGLE_PKT_LEN 1600 +#else +#define WIFI_AMPDU_RX_ENABLED 0 +#define WIFI_AMPDU_RX_BA_WIN 0 /* unused if ampdu_rx_enable == false */ +#define WIFI_RX_MAX_SINGLE_PKT_LEN (1600 - 524) +#endif +#define WIFI_AMPDU_RX_AMPDU_BUF_LEN 72 +#define WIFI_AMPDU_RX_AMPDU_BUF_NUM 3 +#define WIFI_HW_RX_BUFFER_LEN 524 + +#if CONFIG_ESP8266_WIFI_QOS_ENABLED +#define WIFI_QOS_ENABLED 1 +#else +#define WIFI_QOS_ENABLED 0 +#endif + +#if CONFIG_ESP8266_WIFI_AMSDU_ENABLED +#define WIFI_AMSDU_RX_ENABLED 1 +#undef WIFI_RX_MAX_SINGLE_PKT_LEN +#define WIFI_RX_MAX_SINGLE_PKT_LEN 3000 +#else +#define WIFI_AMSDU_RX_ENABLED 0 +#endif + +#if CONFIG_ESP8266_WIFI_NVS_ENABLED +#define WIFI_NVS_ENABLED 1 +#else +#define WIFI_NVS_ENABLED 0 +#endif + #define WIFI_INIT_CONFIG_MAGIC 0x1F2F3F4F #define WIFI_INIT_CONFIG_DEFAULT() { \ .event_handler = &esp_event_send, \ .osi_funcs = NULL, \ - .static_rx_buf_num = 5,\ - .dynamic_rx_buf_num = 0,\ - .tx_buf_type = 0,\ - .static_tx_buf_num = 6,\ - .dynamic_tx_buf_num = 0,\ - .csi_enable = 0,\ - .ampdu_rx_enable = 0,\ - .ampdu_tx_enable = 0,\ - .nvs_enable = 1,\ + .qos_enable = WIFI_QOS_ENABLED,\ + .ampdu_rx_enable = WIFI_AMPDU_RX_ENABLED,\ + .rx_ampdu_buf_len = WIFI_AMPDU_RX_AMPDU_BUF_LEN,\ + .rx_ampdu_buf_num = WIFI_AMPDU_RX_AMPDU_BUF_NUM,\ + .amsdu_rx_enable = WIFI_AMSDU_RX_ENABLED,\ + .rx_ba_win = WIFI_AMPDU_RX_BA_WIN,\ + .rx_max_single_pkt_len = WIFI_RX_MAX_SINGLE_PKT_LEN,\ + .rx_buf_len = WIFI_HW_RX_BUFFER_LEN,\ + .rx_buf_num = CONFIG_ESP8266_WIFI_RX_BUFFER_NUM,\ + .left_continuous_rx_buf_num = CONFIG_ESP8266_WIFI_LEFT_CONTINUOUS_RX_BUFFER_NUM,\ + .rx_pkt_num = CONFIG_ESP8266_WIFI_RX_PKT_NUM,\ + .tx_buf_num = CONFIG_ESP8266_WIFI_TX_PKT_NUM,\ + .nvs_enable = WIFI_NVS_ENABLED,\ .nano_enable = 0,\ - .tx_ba_win = 0,\ - .rx_ba_win = 0,\ .magic = WIFI_INIT_CONFIG_MAGIC\ }; diff --git a/components/esp8266/include/internal/esp_wifi_internal.h b/components/esp8266/include/internal/esp_wifi_internal.h index 99a39584..3087f9d5 100644 --- a/components/esp8266/include/internal/esp_wifi_internal.h +++ b/components/esp8266/include/internal/esp_wifi_internal.h @@ -53,7 +53,9 @@ typedef enum { #define WIFI_LOG_SUBMODULE_MAC (1<<9) #define WIFI_LOG_SUBMODULE_WPA (1<<10) #define WIFI_LOG_SUBMODULE_WPS (1<<11) - +#define WIFI_LOG_SUBMODULE_AMPDU (1<<12) +#define WIFI_LOG_SUBMODULE_AMSDU (1<<13) +#define WIFI_LOG_SUBMODULE_FRAG (1<<14) /** * @brief Set WIFI received TCP/IP data cache ram type * diff --git a/components/esp8266/source/esp_wifi.c b/components/esp8266/source/esp_wifi.c index 058b11b8..810bc168 100644 --- a/components/esp8266/source/esp_wifi.c +++ b/components/esp8266/source/esp_wifi.c @@ -76,6 +76,15 @@ static void esp_wifi_set_debug_log() #endif #if CONFIG_ESP8266_WIFI_DEBUG_LOG_SUBMODULE_WPS wifi_log_submodule |= WIFI_LOG_SUBMODULE_WPS; +#endif +#if CONFIG_ESP8266_WIFI_DEBUG_LOG_SUBMODULE_AMPDU + wifi_log_submodule |= WIFI_LOG_SUBMODULE_AMPDU; +#endif +#if CONFIG_ESP8266_WIFI_DEBUG_LOG_SUBMODULE_AMSDU + wifi_log_submodule |= WIFI_LOG_SUBMODULE_AMSDU; +#endif +#if CONFIG_ESP8266_WIFI_DEBUG_LOG_SUBMODULE_FRAG + wifi_log_submodule |= WIFI_LOG_SUBMODULE_FRAG; #endif esp_wifi_internal_set_log_level(wifi_log_level); esp_wifi_internal_set_log_mod(wifi_log_submodule); diff --git a/components/esp8266/source/phy.h b/components/esp8266/source/phy.h index 32c5376d..dd2a6716 100644 --- a/components/esp8266/source/phy.h +++ b/components/esp8266/source/phy.h @@ -29,6 +29,8 @@ void phy_afterwake_set_rfoption(uint8_t); void write_data_to_rtc(uint8_t *); void get_data_from_rtc(uint8_t *); +int register_chipv6_phy(uint8_t* ); +void phy_disable_agc(); uint8_t chip_init(uint8_t* init_data, uint8_t *mac, uint32_t uart_baudrate); diff --git a/components/esp8266/source/phy_init.c b/components/esp8266/source/phy_init.c index 3f981412..ff1d9e39 100644 --- a/components/esp8266/source/phy_init.c +++ b/components/esp8266/source/phy_init.c @@ -68,12 +68,7 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibrat { esp_err_t status = ESP_OK; uint8_t sta_mac[6]; - uint8_t* local_init_data = calloc(1, 256); -#ifdef CONFIG_CONSOLE_UART_BAUDRATE - const uint32_t uart_baudrate = CONFIG_CONSOLE_UART_BAUDRATE; -#else - const uint32_t uart_baudrate = 74880; // ROM default baudrate -#endif + uint8_t *local_init_data = calloc(1, 256); memcpy(local_init_data, init_data->params, 128); @@ -89,9 +84,16 @@ esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibrat } esp_efuse_mac_get_default(sta_mac); - chip_init(local_init_data, sta_mac, uart_baudrate); - ESP_LOGI(TAG, "phy ver: %d_%d", (READ_PERI_REG(0x6000107C) >> 16) & 0xFFF, READ_PERI_REG(0x6000107C) >> 28); - get_data_from_rtc((uint8_t*)calibration_data); + + int ret = register_chipv6_phy(local_init_data); + if (ret) { + ESP_LOGI(TAG, "phy register error, ret:%d", ret); + } + + phy_disable_agc(); + + ESP_LOGI(TAG, "phy ver: %d_%d", (READ_PERI_REG(0x6000107C)>>16)&0xFFF, READ_PERI_REG(0x6000107C)>>28); + get_data_from_rtc((uint8_t *)calibration_data); memcpy(rx_gain_dc_table, calibration_data->rx_gain_dc_table, 4 * 125); phy_rx_gain_dc_table = rx_gain_dc_table; diff --git a/components/esp8266/source/startup.c b/components/esp8266/source/startup.c index 0f7c6358..7153e96d 100644 --- a/components/esp8266/source/startup.c +++ b/components/esp8266/source/startup.c @@ -30,6 +30,7 @@ #include "internal/esp_wifi_internal.h" #include "internal/esp_system_internal.h" #include "esp8266/eagle_soc.h" +#include "esp8266/uart_register.h" #include "FreeRTOS.h" #include "task.h" @@ -47,6 +48,7 @@ extern int wifi_timer_init(void); extern int wifi_nvs_init(void); extern esp_err_t esp_pthread_init(void); extern void phy_get_bb_evm(void); +extern void uart_div_modify(uint8_t uart_no, uint16_t DivLatchValue); static inline int should_load(uint32_t load_addr) { @@ -61,6 +63,19 @@ static inline int should_load(uint32_t load_addr) return 1; } +static inline void uart_init() +{ +#ifdef CONFIG_CONSOLE_UART_BAUDRATE + const uint32_t uart_baudrate = CONFIG_CONSOLE_UART_BAUDRATE; +#else + const uint32_t uart_baudrate = 74880; // ROM default baudrate +#endif + while (READ_PERI_REG(UART_STATUS(0)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S)); + while (READ_PERI_REG(UART_STATUS(1)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S)); + uart_div_modify(0, UART_CLK_FREQ / uart_baudrate); + uart_div_modify(1, UART_CLK_FREQ / uart_baudrate); +} + static void user_init_entry(void *param) { void (**func)(void); @@ -76,13 +91,16 @@ static void user_init_entry(void *param) phy_get_bb_evm(); + /*enable tsf0 interrupt for pwm*/ + REG_WRITE(PERIPHS_DPORT_BASEADDR, (REG_READ(PERIPHS_DPORT_BASEADDR) & ~0x1F) | 0x1); + REG_WRITE(INT_ENA_WDEV, REG_READ(INT_ENA_WDEV) | WDEV_TSF0_REACH_INT); + assert(nvs_flash_init() == 0); - assert(wifi_nvs_init() == 0); assert(rtc_init() == 0); assert(mac_init() == 0); + uart_init(); assert(base_gpio_init() == 0); esp_phy_load_cal_and_init(0); - assert(wifi_timer_init() == 0); esp_wifi_set_rx_pbuf_mem_type(WIFI_RX_PBUF_DRAM);