From 2553fb5845efbc8cb3ad2d6d4b72cf57fb86c4fc Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 15 Dec 2021 16:30:29 +0100 Subject: [PATCH] esp_eth: Make EMAC DMA burst size configurable Merges https://github.com/espressif/esp-idf/pull/7874 Closes https://github.com/espressif/esp-idf/issues/7380 --- components/esp_eth/include/esp_eth_mac.h | 69 ++++++++++++------- components/esp_eth/src/esp_eth_mac_esp.c | 25 ++++--- components/hal/emac_hal.c | 6 +- components/hal/include/hal/emac_hal.h | 4 +- .../protocol_examples_common/connect.c | 4 +- .../basic/main/ethernet_example_main.c | 4 +- .../enc28j60/main/enc28j60_example_main.c | 3 +- .../eth2ap/main/ethernet_example_main.c | 4 +- examples/ethernet/iperf/main/cmd_ethernet.c | 7 +- .../main/simple_sniffer_example_main.c | 4 +- tools/ci/check_copyright_ignore.txt | 1 - 11 files changed, 76 insertions(+), 55 deletions(-) diff --git a/components/esp_eth/include/esp_eth_mac.h b/components/esp_eth/include/esp_eth_mac.h index db462728a1..720f65c13a 100644 --- a/components/esp_eth/include/esp_eth_mac.h +++ b/components/esp_eth/include/esp_eth_mac.h @@ -1,16 +1,8 @@ -// Copyright 2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include @@ -365,6 +357,26 @@ typedef union { } rmii; /*!< EMAC RMII Clock Configuration */ } eth_mac_clock_config_t; +/** +* @brief Options of EMAC DMA burst len +*/ +typedef enum { + ETH_DMA_RX_BURST_LEN_32, + ETH_DMA_RX_BURST_LEN_16, + ETH_DMA_RX_BURST_LEN_8, +} eth_mac_dma_rx_burst_len_t; + +/** +* @brief EMAC specific configuration (sub-struct of Ethernet MAC config) +* +*/ +typedef struct { + int smi_mdc_gpio_num; /*!< SMI MDC GPIO number, set to -1 could bypass the SMI GPIO configuration */ + int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number, set to -1 could bypass the SMI GPIO configuration */ + eth_data_interface_t interface; /*!< EMAC Data interface to PHY (MII/RMII) */ + eth_mac_clock_config_t clock_config; /*!< EMAC Interface clock configuration */ + eth_mac_dma_rx_burst_len_t dma_rx_burst_len; /*!< EMAC DMA Rx burst len configuration */ +} eth_esp32_emac_t; /** * @brief Configuration of Ethernet MAC object @@ -374,11 +386,15 @@ typedef struct { uint32_t sw_reset_timeout_ms; /*!< Software reset timeout value (Unit: ms) */ uint32_t rx_task_stack_size; /*!< Stack size of the receive task */ uint32_t rx_task_prio; /*!< Priority of the receive task */ - int smi_mdc_gpio_num; /*!< SMI MDC GPIO number, set to -1 could bypass the SMI GPIO configuration */ - int smi_mdio_gpio_num; /*!< SMI MDIO GPIO number, set to -1 could bypass the SMI GPIO configuration */ uint32_t flags; /*!< Flags that specify extra capability for mac driver */ - eth_data_interface_t interface; /*!< EMAC Data interface to PHY (MII/RMII) */ - eth_mac_clock_config_t clock_config; /*!< EMAC Interface clock configuration */ + union { + eth_esp32_emac_t esp32_emac; /*!< ESP32's internal EMAC configuration */ + // Placeholder for another supported MAC configs + struct eth_custom_mac *p_custom_mac; /*!< Opaque pointer to an additional custom MAC config + * Note: This config could be used for IDF supported MAC + * as well as any additional MAC introduced in user-space + * */ + }; /*!< Union of mutually exclusive vendor specific MAC options */ } eth_mac_config_t; #define ETH_MAC_FLAG_WORK_WITH_CACHE_DISABLE (1 << 0) /*!< MAC driver can work when cache is disabled */ @@ -388,21 +404,24 @@ typedef struct { * @brief Default configuration for Ethernet MAC object * */ + #define ETH_MAC_DEFAULT_CONFIG() \ { \ .sw_reset_timeout_ms = 100, \ .rx_task_stack_size = 2048, \ .rx_task_prio = 15, \ - .smi_mdc_gpio_num = 23, \ - .smi_mdio_gpio_num = 18, \ .flags = 0, \ - .interface = EMAC_DATA_INTERFACE_RMII, \ - .clock_config = \ - { \ - .rmii = \ + .esp32_emac = { \ + .smi_mdc_gpio_num = 23, \ + .smi_mdio_gpio_num = 18, \ + .interface = EMAC_DATA_INTERFACE_RMII, \ + .clock_config = \ { \ - .clock_mode = EMAC_CLK_DEFAULT, \ - .clock_gpio = EMAC_CLK_IN_GPIO \ + .rmii = \ + { \ + .clock_mode = EMAC_CLK_DEFAULT, \ + .clock_gpio = EMAC_CLK_IN_GPIO \ + } \ } \ } \ } diff --git a/components/esp_eth/src/esp_eth_mac_esp.c b/components/esp_eth/src/esp_eth_mac_esp.c index 02df23aed4..f49603ba80 100644 --- a/components/esp_eth/src/esp_eth_mac_esp.c +++ b/components/esp_eth/src/esp_eth_mac_esp.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -61,6 +61,7 @@ typedef struct { #ifdef CONFIG_PM_ENABLE esp_pm_lock_handle_t pm_lock; #endif + eth_mac_dma_rx_burst_len_t dma_rx_burst_len; } emac_esp32_t; static esp_err_t esp_emac_alloc_driver_obj(const eth_mac_config_t *config, emac_esp32_t **emac_out_hdl, void **out_descriptors); @@ -342,8 +343,11 @@ static esp_err_t emac_esp32_init(esp_eth_mac_t *mac) emac_hal_reset_desc_chain(&emac->hal); /* init mac registers by default */ emac_hal_init_mac_default(&emac->hal); - /* init dma registers by default */ - emac_hal_init_dma_default(&emac->hal); + /* init dma registers by default with selected EMAC_RX_DMA_BURST */ + emac_hal_init_dma_default(&emac->hal, + emac->dma_rx_burst_len == ETH_DMA_RX_BURST_LEN_8 ? EMAC_LL_DMA_BURST_LENGTH_8BEAT : + emac->dma_rx_burst_len == ETH_DMA_RX_BURST_LEN_16 ? EMAC_LL_DMA_BURST_LENGTH_16BEAT : + EMAC_LL_DMA_BURST_LENGTH_32BEAT); /* get emac address from efuse */ ESP_GOTO_ON_ERROR(esp_read_mac(emac->addr, ESP_MAC_ETH), err, TAG, "fetch ethernet mac address failed"); /* set MAC address to emac register */ @@ -462,6 +466,7 @@ static esp_err_t esp_emac_alloc_driver_obj(const eth_mac_config_t *config, emac_ emac = calloc(1, sizeof(emac_esp32_t)); } ESP_GOTO_ON_FALSE(emac, ESP_ERR_NO_MEM, err, TAG, "no mem for esp emac object"); + emac->dma_rx_burst_len = config->esp32_emac.dma_rx_burst_len; /* alloc memory for ethernet dma descriptor */ uint32_t desc_size = CONFIG_ETH_DMA_RX_BUFFER_NUM * sizeof(eth_dma_rx_descriptor_t) + CONFIG_ETH_DMA_TX_BUFFER_NUM * sizeof(eth_dma_tx_descriptor_t); @@ -500,9 +505,9 @@ err: static esp_err_t esp_emac_config_data_interface(const eth_mac_config_t *config, emac_esp32_t *emac) { esp_err_t ret = ESP_OK; - switch (config->interface) { + switch (config->esp32_emac.interface) { case EMAC_DATA_INTERFACE_MII: - emac->clock_config = config->clock_config; + emac->clock_config = config->esp32_emac.clock_config; /* MII interface GPIO initialization */ emac_hal_iomux_init_mii(); /* Enable MII clock */ @@ -510,7 +515,7 @@ static esp_err_t esp_emac_config_data_interface(const eth_mac_config_t *config, break; case EMAC_DATA_INTERFACE_RMII: // by default, the clock mode is selected at compile time (by Kconfig) - if (config->clock_config.rmii.clock_mode == EMAC_CLK_DEFAULT) { + if (config->esp32_emac.clock_config.rmii.clock_mode == EMAC_CLK_DEFAULT) { #if CONFIG_ETH_RMII_CLK_INPUT #if CONFIG_ETH_RMII_CLK_IN_GPIO == 0 emac->clock_config.rmii.clock_mode = EMAC_CLK_EXT_IN; @@ -529,7 +534,7 @@ static esp_err_t esp_emac_config_data_interface(const eth_mac_config_t *config, #error "Unsupported RMII clock mode" #endif } else { - emac->clock_config = config->clock_config; + emac->clock_config = config->esp32_emac.clock_config; } /* RMII interface GPIO initialization */ emac_hal_iomux_init_rmii(); @@ -559,7 +564,7 @@ static esp_err_t esp_emac_config_data_interface(const eth_mac_config_t *config, } break; default: - ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "invalid EMAC Data Interface:%d", config->interface); + ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "invalid EMAC Data Interface:%d", config->esp32_emac.interface); } err: return ret; @@ -592,8 +597,8 @@ esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_mac_config_t *config) ESP_GOTO_ON_FALSE(ret_code == ESP_OK, NULL, err_interf, TAG, "config emac interface failed"); emac->sw_reset_timeout_ms = config->sw_reset_timeout_ms; - emac->smi_mdc_gpio_num = config->smi_mdc_gpio_num; - emac->smi_mdio_gpio_num = config->smi_mdio_gpio_num; + emac->smi_mdc_gpio_num = config->esp32_emac.smi_mdc_gpio_num; + emac->smi_mdio_gpio_num = config->esp32_emac.smi_mdio_gpio_num; emac->flow_control_high_water_mark = FLOW_CONTROL_HIGH_WATER_MARK; emac->flow_control_low_water_mark = FLOW_CONTROL_LOW_WATER_MARK; emac->use_apll = false; diff --git a/components/hal/emac_hal.c b/components/hal/emac_hal.c index 893a655975..97560d194b 100644 --- a/components/hal/emac_hal.c +++ b/components/hal/emac_hal.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -265,7 +265,7 @@ void emac_hal_enable_flow_ctrl(emac_hal_context_t *hal, bool enable) } } -void emac_hal_init_dma_default(emac_hal_context_t *hal) +void emac_hal_init_dma_default(emac_hal_context_t *hal, uint32_t dma_rx_burst_len) { /* DMAOMR Configuration */ /* Enable Dropping of TCP/IP Checksum Error Frames */ @@ -297,7 +297,7 @@ void emac_hal_init_dma_default(emac_hal_context_t *hal) /* Use Separate PBL */ emac_ll_use_separate_pbl_enable(hal->dma_regs, true); /* Set Rx/Tx DMA Burst Length */ - emac_ll_set_rx_dma_pbl(hal->dma_regs, EMAC_LL_DMA_BURST_LENGTH_8BEAT); + emac_ll_set_rx_dma_pbl(hal->dma_regs, dma_rx_burst_len); emac_ll_set_prog_burst_len(hal->dma_regs, EMAC_LL_DMA_BURST_LENGTH_32BEAT); /* Enable Enhanced Descriptor,8 Words(32 Bytes) */ emac_ll_enhance_desc_enable(hal->dma_regs, true); diff --git a/components/hal/include/hal/emac_hal.h b/components/hal/include/hal/emac_hal.h index 4f332811ab..6a2c21b243 100644 --- a/components/hal/include/hal/emac_hal.h +++ b/components/hal/include/hal/emac_hal.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -189,7 +189,7 @@ void emac_hal_set_csr_clock_range(emac_hal_context_t *hal, int freq); void emac_hal_init_mac_default(emac_hal_context_t *hal); -void emac_hal_init_dma_default(emac_hal_context_t *hal); +void emac_hal_init_dma_default(emac_hal_context_t *hal, uint32_t dma_rx_burst_len); void emac_hal_set_speed(emac_hal_context_t *hal, uint32_t speed); diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 50925b76a8..fb357dea66 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -379,8 +379,8 @@ static esp_netif_t *eth_start(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; s_mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 s_phy = esp_eth_phy_new_ip101(&phy_config); diff --git a/examples/ethernet/basic/main/ethernet_example_main.c b/examples/ethernet/basic/main/ethernet_example_main.c index e539af9f65..f1f20e3b18 100644 --- a/examples/ethernet/basic/main/ethernet_example_main.c +++ b/examples/ethernet/basic/main/ethernet_example_main.c @@ -101,8 +101,8 @@ void app_main(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; - mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); diff --git a/examples/ethernet/enc28j60/main/enc28j60_example_main.c b/examples/ethernet/enc28j60/main/enc28j60_example_main.c index ba021a92f7..05599b1c6f 100644 --- a/examples/ethernet/enc28j60/main/enc28j60_example_main.c +++ b/examples/ethernet/enc28j60/main/enc28j60_example_main.c @@ -101,8 +101,7 @@ void app_main(void) enc28j60_config.int_gpio_num = CONFIG_EXAMPLE_ENC28J60_INT_GPIO; eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - mac_config.smi_mdc_gpio_num = -1; // ENC28J60 doesn't have SMI interface - mac_config.smi_mdio_gpio_num = -1; + mac_config.p_custom_mac = NULL; // ENC28J60 MAC doesn't use any specific config esp_eth_mac_t *mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index 47ce52eabd..9a72ea9609 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -158,8 +158,8 @@ static void initialize_ethernet(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); diff --git a/examples/ethernet/iperf/main/cmd_ethernet.c b/examples/ethernet/iperf/main/cmd_ethernet.c index 07c47dbfeb..0e2a82e9ce 100644 --- a/examples/ethernet/iperf/main/cmd_ethernet.c +++ b/examples/ethernet/iperf/main/cmd_ethernet.c @@ -208,8 +208,8 @@ void register_ethernet(void) phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET - mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_EXAMPLE_ETH_PHY_IP101 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); @@ -293,8 +293,7 @@ void register_ethernet(void) eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle); enc28j60_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; - mac_config.smi_mdc_gpio_num = -1; // ENC28J60 doesn't have SMI interface - mac_config.smi_mdio_gpio_num = -1; + mac_config.p_custom_mac = NULL; // ENC28J60 MAC doesn't use any specific config esp_eth_mac_t *mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config); phy_config.autonego_timeout_ms = 0; // ENC28J60 doesn't support auto-negotiation diff --git a/examples/network/simple_sniffer/main/simple_sniffer_example_main.c b/examples/network/simple_sniffer/main/simple_sniffer_example_main.c index 5282200e39..935baa3397 100644 --- a/examples/network/simple_sniffer/main/simple_sniffer_example_main.c +++ b/examples/network/simple_sniffer/main/simple_sniffer_example_main.c @@ -135,8 +135,8 @@ static void initialize_eth(void) phy_config.phy_addr = CONFIG_SNIFFER_ETH_PHY_ADDR; phy_config.reset_gpio_num = CONFIG_SNIFFER_ETH_PHY_RST_GPIO; #if CONFIG_SNIFFER_USE_INTERNAL_ETHERNET - mac_config.smi_mdc_gpio_num = CONFIG_SNIFFER_ETH_MDC_GPIO; - mac_config.smi_mdio_gpio_num = CONFIG_SNIFFER_ETH_MDIO_GPIO; + mac_config.esp32_emac.smi_mdc_gpio_num = CONFIG_SNIFFER_ETH_MDC_GPIO; + mac_config.esp32_emac.smi_mdio_gpio_num = CONFIG_SNIFFER_ETH_MDIO_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config); #if CONFIG_SNIFFER_ETH_PHY_IP101 esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config); diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index a794be05c3..9589e413c7 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -439,7 +439,6 @@ components/esp_common/include/esp_compiler.h components/esp_common/include/esp_types.h components/esp_common/src/esp_err_to_name.c components/esp_common/test/test_attr.c -components/esp_eth/include/esp_eth_mac.h components/esp_eth/include/eth_phy_regs_struct.h components/esp_eth/src/dm9051.h components/esp_eth/src/esp_eth_mac_openeth.c