From bb6c6aff36ab608db39e91c81c746b137fe5038c Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Thu, 23 Jun 2022 10:13:50 +0000 Subject: [PATCH] esp_eth: SPI Ethernet modules initialization simplification --- components/esp_eth/CMakeLists.txt | 6 +- components/esp_eth/include/esp_eth_mac.h | 45 ++++++++----- components/esp_eth/src/esp_eth_mac_dm9051.c | 15 ++++- .../esp_eth/src/esp_eth_mac_ksz8851snl.c | 6 +- components/esp_eth/src/esp_eth_mac_w5500.c | 15 ++++- docs/en/api-reference/network/esp_eth.rst | 12 ++-- docs/en/migration-guides/ethernet.rst | 8 ++- .../protocol_examples_common/connect.c | 21 ++---- .../basic/main/ethernet_example_main.c | 66 +++---------------- .../eth_enc28j60/esp_eth_enc28j60.h | 16 +++-- .../eth_enc28j60/esp_eth_mac_enc28j60.c | 16 ++++- .../enc28j60/main/enc28j60_example_main.c | 9 +-- .../eth2ap/main/ethernet_example_main.c | 33 ++-------- examples/ethernet/iperf/main/cmd_ethernet.c | 58 ++++------------ .../network/bridge/main/bridge_example_main.c | 62 ++++------------- .../main/simple_sniffer_example_main.c | 34 ++-------- 16 files changed, 146 insertions(+), 276 deletions(-) diff --git a/components/esp_eth/CMakeLists.txt b/components/esp_eth/CMakeLists.txt index b5c0b9dcac..de61f33b56 100644 --- a/components/esp_eth/CMakeLists.txt +++ b/components/esp_eth/CMakeLists.txt @@ -51,5 +51,9 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS ${include} - REQUIRES "esp_event" # For using "ESP_EVENT_DECLARE_BASE" in header file + REQUIRES esp_event # For using "ESP_EVENT_DECLARE_BASE" in header file PRIV_REQUIRES ${priv_requires}) + +if(CONFIG_ETH_USE_SPI_ETHERNET) + idf_component_optional_requires(PUBLIC driver) +endif() diff --git a/components/esp_eth/include/esp_eth_mac.h b/components/esp_eth/include/esp_eth_mac.h index 2183d1fac3..dfc1af2c73 100644 --- a/components/esp_eth/include/esp_eth_mac.h +++ b/components/esp_eth/include/esp_eth_mac.h @@ -8,6 +8,9 @@ #include #include "esp_eth_com.h" #include "sdkconfig.h" +#if CONFIG_ETH_USE_SPI_ETHERNET +#include "driver/spi_master.h" +#endif #ifdef __cplusplus extern "C" { @@ -473,18 +476,20 @@ esp_eth_mac_t *esp_eth_mac_new_esp32(const eth_esp32_emac_config_t *esp32_config * */ typedef struct { - void *spi_hdl; /*!< Handle of SPI device driver */ - int int_gpio_num; /*!< Interrupt GPIO number */ + spi_host_device_t spi_host_id; /*!< SPI peripheral */ + spi_device_interface_config_t *spi_devcfg; /*!< SPI device configuration */ + int int_gpio_num; /*!< Interrupt GPIO number */ } eth_dm9051_config_t; /** * @brief Default DM9051 specific configuration * */ -#define ETH_DM9051_DEFAULT_CONFIG(spi_device) \ - { \ - .spi_hdl = spi_device, \ - .int_gpio_num = 4, \ +#define ETH_DM9051_DEFAULT_CONFIG(spi_host, spi_devcfg_p) \ + { \ + .spi_host_id = spi_host, \ + .spi_devcfg = spi_devcfg_p, \ + .int_gpio_num = 4, \ } /** @@ -506,18 +511,20 @@ esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, * */ typedef struct { - void *spi_hdl; /*!< Handle of SPI device driver */ - int int_gpio_num; /*!< Interrupt GPIO number */ + spi_host_device_t spi_host_id; /*!< SPI peripheral */ + spi_device_interface_config_t *spi_devcfg; /*!< SPI device configuration */ + int int_gpio_num; /*!< Interrupt GPIO number */ } eth_w5500_config_t; /** * @brief Default W5500 specific configuration * */ -#define ETH_W5500_DEFAULT_CONFIG(spi_device) \ - { \ - .spi_hdl = spi_device, \ - .int_gpio_num = 4, \ +#define ETH_W5500_DEFAULT_CONFIG(spi_host, spi_devcfg_p) \ + { \ + .spi_host_id = spi_host, \ + .spi_devcfg = spi_devcfg_p, \ + .int_gpio_num = 4, \ } /** @@ -539,18 +546,20 @@ esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, con * */ typedef struct { - void *spi_hdl; /*!< Handle of SPI device driver */ - int int_gpio_num; /*!< Interrupt GPIO number */ + spi_host_device_t spi_host_id; /*!< SPI peripheral */ + spi_device_interface_config_t *spi_devcfg; /*!< SPI device configuration */ + int int_gpio_num; /*!< Interrupt GPIO number */ } eth_ksz8851snl_config_t; /** * @brief Default KSZ8851SNL specific configuration * */ -#define ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_device) \ - { \ - .spi_hdl = spi_device, \ - .int_gpio_num = 14, \ +#define ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_host, spi_devcfg_p) \ + { \ + .spi_host_id = spi_host, \ + .spi_devcfg = spi_devcfg_p, \ + .int_gpio_num = 14, \ } /** diff --git a/components/esp_eth/src/esp_eth_mac_dm9051.c b/components/esp_eth/src/esp_eth_mac_dm9051.c index 6fa25d4517..605010f72e 100644 --- a/components/esp_eth/src/esp_eth_mac_dm9051.c +++ b/components/esp_eth/src/esp_eth_mac_dm9051.c @@ -739,6 +739,7 @@ static esp_err_t emac_dm9051_del(esp_eth_mac_t *mac) { emac_dm9051_t *emac = __containerof(mac, emac_dm9051_t, parent); vTaskDelete(emac->rx_task_hdl); + spi_bus_remove_device(emac->spi_hdl); vSemaphoreDelete(emac->spi_lock); free(emac); return ESP_OK; @@ -754,10 +755,22 @@ esp_eth_mac_t *esp_eth_mac_new_dm9051(const eth_dm9051_config_t *dm9051_config, ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "calloc emac failed"); /* dm9051 receive is driven by interrupt only for now*/ ESP_GOTO_ON_FALSE(dm9051_config->int_gpio_num >= 0, NULL, err, TAG, "error interrupt gpio number"); + /* SPI device init */ + spi_device_interface_config_t spi_devcfg; + memcpy(&spi_devcfg, dm9051_config->spi_devcfg, sizeof(spi_device_interface_config_t)); + if (dm9051_config->spi_devcfg->command_bits == 0 && dm9051_config->spi_devcfg->address_bits == 0) { + /* configure default SPI frame format */ + spi_devcfg.command_bits = 1; + spi_devcfg.address_bits = 7; + } else { + ESP_GOTO_ON_FALSE(dm9051_config->spi_devcfg->command_bits == 1 || dm9051_config->spi_devcfg->address_bits == 7, + NULL, err, TAG, "incorrect SPI frame format (command_bits/address_bits)"); + } + ESP_GOTO_ON_FALSE(spi_bus_add_device(dm9051_config->spi_host_id, &spi_devcfg, &emac->spi_hdl) == ESP_OK, + NULL, err, TAG, "adding device to SPI host #%d failed", dm9051_config->spi_host_id + 1); /* bind methods and attributes */ emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms; emac->int_gpio_num = dm9051_config->int_gpio_num; - emac->spi_hdl = dm9051_config->spi_hdl; emac->parent.set_mediator = emac_dm9051_set_mediator; emac->parent.init = emac_dm9051_init; emac->parent.deinit = emac_dm9051_deinit; diff --git a/components/esp_eth/src/esp_eth_mac_ksz8851snl.c b/components/esp_eth/src/esp_eth_mac_ksz8851snl.c index 4990d758ee..f91803c283 100644 --- a/components/esp_eth/src/esp_eth_mac_ksz8851snl.c +++ b/components/esp_eth/src/esp_eth_mac_ksz8851snl.c @@ -571,6 +571,7 @@ static esp_err_t emac_ksz8851_del(esp_eth_mac_t *mac) { emac_ksz8851snl_t *emac = __containerof(mac, emac_ksz8851snl_t, parent); vTaskDelete(emac->rx_task_hdl); + spi_bus_remove_device(emac->spi_hdl); vSemaphoreDelete(emac->spi_lock); heap_caps_free(emac->rx_buffer); heap_caps_free(emac->tx_buffer); @@ -666,9 +667,12 @@ esp_eth_mac_t *esp_eth_mac_new_ksz8851snl(const eth_ksz8851snl_config_t *ksz8851 emac = calloc(1, sizeof(emac_ksz8851snl_t)); ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "no mem for MAC instance"); + /* SPI device init */ + ESP_GOTO_ON_FALSE(spi_bus_add_device(ksz8851snl_config->spi_host_id, ksz8851snl_config->spi_devcfg, &emac->spi_hdl) == ESP_OK, + NULL, err, TAG, "adding device to SPI host #%d failed", ksz8851snl_config->spi_host_id + 1); + emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms; emac->int_gpio_num = ksz8851snl_config->int_gpio_num; - emac->spi_hdl = ksz8851snl_config->spi_hdl; emac->parent.set_mediator = emac_ksz8851_set_mediator; emac->parent.init = emac_ksz8851_init; emac->parent.deinit = emac_ksz8851_deinit; diff --git a/components/esp_eth/src/esp_eth_mac_w5500.c b/components/esp_eth/src/esp_eth_mac_w5500.c index 9917e03669..ef0d2528a3 100644 --- a/components/esp_eth/src/esp_eth_mac_w5500.c +++ b/components/esp_eth/src/esp_eth_mac_w5500.c @@ -619,6 +619,7 @@ static esp_err_t emac_w5500_del(esp_eth_mac_t *mac) { emac_w5500_t *emac = __containerof(mac, emac_w5500_t, parent); vTaskDelete(emac->rx_task_hdl); + spi_bus_remove_device(emac->spi_hdl); vSemaphoreDelete(emac->spi_lock); free(emac); return ESP_OK; @@ -633,10 +634,22 @@ esp_eth_mac_t *esp_eth_mac_new_w5500(const eth_w5500_config_t *w5500_config, con ESP_GOTO_ON_FALSE(emac, NULL, err, TAG, "no mem for MAC instance"); /* w5500 driver is interrupt driven */ ESP_GOTO_ON_FALSE(w5500_config->int_gpio_num >= 0, NULL, err, TAG, "invalid interrupt gpio number"); + /* SPI device init */ + spi_device_interface_config_t spi_devcfg; + memcpy(&spi_devcfg, w5500_config->spi_devcfg, sizeof(spi_device_interface_config_t)); + if (w5500_config->spi_devcfg->command_bits == 0 && w5500_config->spi_devcfg->address_bits == 0) { + /* configure default SPI frame format */ + spi_devcfg.command_bits = 16; // Actually it's the address phase in W5500 SPI frame + spi_devcfg.address_bits = 8; // Actually it's the control phase in W5500 SPI frame + } else { + ESP_GOTO_ON_FALSE(w5500_config->spi_devcfg->command_bits == 16 || w5500_config->spi_devcfg->address_bits == 8, + NULL, err, TAG, "incorrect SPI frame format (command_bits/address_bits)"); + } + ESP_GOTO_ON_FALSE(spi_bus_add_device(w5500_config->spi_host_id, &spi_devcfg, &emac->spi_hdl) == ESP_OK, + NULL, err, TAG, "adding device to SPI host #%d failed", w5500_config->spi_host_id + 1); /* bind methods and attributes */ emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms; emac->int_gpio_num = w5500_config->int_gpio_num; - emac->spi_hdl = w5500_config->spi_hdl; emac->parent.set_mediator = emac_w5500_set_mediator; emac->parent.init = emac_w5500_init; emac->parent.deinit = emac_w5500_deinit; diff --git a/docs/en/api-reference/network/esp_eth.rst b/docs/en/api-reference/network/esp_eth.rst index 56bec1fff3..534e664348 100644 --- a/docs/en/api-reference/network/esp_eth.rst +++ b/docs/en/api-reference/network/esp_eth.rst @@ -253,18 +253,15 @@ SPI-Ethernet Module .quadhd_io_num = -1, }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); - // Allocate SPI device from the bus - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, + // Configure SPI device + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config); @@ -272,8 +269,7 @@ SPI-Ethernet Module .. note:: * When creating MAC and PHY instance for SPI-Ethernet modules (e.g. DM9051), the constructor function must have the same suffix (e.g. `esp_eth_mac_new_dm9051` and `esp_eth_phy_new_dm9051`). This is because we don't have other choices but the integrated PHY. - * We have to create an SPI device handle firstly and then pass it to the MAC constructor function. More instructions on creating SPI device handle, please refer to :doc:`SPI Master <../peripherals/spi_master>`. - * The SPI device configuration (i.e. `spi_device_interface_config_t`) can be different for other Ethernet modules. Please check out your module's spec and the examples in esp-idf. + * The SPI device configuration (i.e. `spi_device_interface_config_t`) may slightly differ for other Ethernet modules or to meet SPI timing on specific PCB. Please check out your module's spec and the examples in esp-idf. Install Driver diff --git a/docs/en/migration-guides/ethernet.rst b/docs/en/migration-guides/ethernet.rst index 15b3cd05d7..4e56339d38 100644 --- a/docs/en/migration-guides/ethernet.rst +++ b/docs/en/migration-guides/ethernet.rst @@ -47,6 +47,12 @@ ESP NETIF Glue Event Handlers ----------------------------- ``esp_eth_set_default_handlers()`` and ``esp_eth_clear_default_handlers()`` functions were removed. Registration of the default IP layer handlers for Ethernet is now handled automatically. If users have already followed the recommendation to fully initialize the Ethernet driver and network interface prior to registering their Ethernet/IP event handlers, then no action is required (except for deleting the affected functions). Otherwise, users should ensure that they register the user event handlers as the last thing prior to starting the Ethernet driver. + PHY Address Auto-detect ----------------------- -Ethernet PHY address auto-detect function ``esp_eth_detect_phy_addr`` was renamed to :cpp:func:`esp_eth_phy_802_3_detect_phy_addr` and its header declaration was moved to :component_file:`esp_eth/include/esp_eth_phy_802_3.h`. \ No newline at end of file +Ethernet PHY address auto-detect function ``esp_eth_detect_phy_addr`` was renamed to :cpp:func:`esp_eth_phy_802_3_detect_phy_addr` and its header declaration was moved to :component_file:`esp_eth/include/esp_eth_phy_802_3.h`. + + +SPI-Ethernet Modules Initialization +----------------------------------- +SPI-Ethernet Modules Initialization was simplified. It is not needed to create an SPI device handle by calling :cpp:func:`spi_bus_add_device` prior SPI-Ethernet MAC instance creation since it is done internally now. The configuration structures :cpp:class:`eth_dm9051_config_t`, :cpp:class:`eth_w5500_config_t` and :cpp:class:`eth_ksz8851snl_config_t` members were updated to include SPI device configuration (to be able to fine tune SPI timig which may be dependend on PCB design, for instance). See :doc:`Ethernet <../api-reference/network/esp_eth>` to find SPI-Ethernet Module initialization example. diff --git a/examples/common_components/protocol_examples_common/connect.c b/examples/common_components/protocol_examples_common/connect.c index 5f648bc59b..07e74de0da 100644 --- a/examples/common_components/protocol_examples_common/connect.c +++ b/examples/common_components/protocol_examples_common/connect.c @@ -397,7 +397,6 @@ static esp_netif_t *eth_start(void) #endif #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET gpio_install_isr_service(0); - spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, @@ -406,33 +405,21 @@ static esp_netif_t *eth_start(void) .quadhd_io_num = -1, }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1)); -#if CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); +#if CONFIG_EXAMPLE_USE_DM9051 /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); s_phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); s_phy = esp_eth_phy_new_w5500(&phy_config); diff --git a/examples/ethernet/basic/main/ethernet_example_main.c b/examples/ethernet/basic/main/ethernet_example_main.c index 3698fe7f0a..1a21b98b2e 100644 --- a/examples/ethernet/basic/main/ethernet_example_main.c +++ b/examples/ethernet/basic/main/ethernet_example_main.c @@ -152,7 +152,6 @@ void app_main(void) gpio_install_isr_service(0); // Init SPI bus - spi_device_handle_t spi_handle[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL }; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, @@ -173,82 +172,33 @@ void app_main(void) esp_eth_mac_t *mac_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM]; esp_eth_phy_t *phy_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM]; esp_eth_handle_t eth_handle_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL }; -#if CONFIG_EXAMPLE_USE_KSZ8851SNL - spi_device_interface_config_t devcfg = { + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .queue_size = 20 }; - for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) { // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i])); - // KSZ8851SNL ethernet driver is based on spi driver - eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle[i]); - + spi_devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; // Set remaining GPIO numbers and configuration used by the SPI module - ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio; phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; - +#if CONFIG_EXAMPLE_USE_KSZ8851SNL + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); + ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio; mac_spi[i] = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi); phy_spi[i] = esp_eth_phy_new_ksz8851snl(&phy_config_spi); - } #elif CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .queue_size = 20 - }; - - for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) { - // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i])); - // dm9051 ethernet driver is based on spi driver - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle[i]); - - // Set remaining GPIO numbers and configuration used by the SPI module + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = spi_eth_module_config[i].int_gpio; - phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; - phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; - mac_spi[i] = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config_spi); phy_spi[i] = esp_eth_phy_new_dm9051(&phy_config_spi); - } #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .queue_size = 20 - }; - - for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) { - // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle[i])); - // w5500 ethernet driver is based on spi driver - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle[i]); - - // Set remaining GPIO numbers and configuration used by the SPI module + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio; - phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr; - phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio; - mac_spi[i] = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi); phy_spi[i] = esp_eth_phy_new_w5500(&phy_config_spi); - } -#endif //CONFIG_EXAMPLE_USE_W5500 - - for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) { +#endif esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]); ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi[i])); diff --git a/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_enc28j60.h b/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_enc28j60.h index db7907404f..4ed39b30e4 100644 --- a/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_enc28j60.h +++ b/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_enc28j60.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 */ @@ -21,8 +21,9 @@ extern "C" { * */ typedef struct { - spi_device_handle_t spi_hdl; /*!< Handle of SPI device driver */ - int int_gpio_num; /*!< Interrupt GPIO number */ + spi_host_device_t spi_host_id; /*!< SPI peripheral */ + spi_device_interface_config_t *spi_devcfg; /*!< SPI device configuration */ + int int_gpio_num; /*!< Interrupt GPIO number */ } eth_enc28j60_config_t; /** @@ -40,10 +41,11 @@ typedef enum { * @brief Default ENC28J60 specific configuration * */ -#define ETH_ENC28J60_DEFAULT_CONFIG(spi_device) \ - { \ - .spi_hdl = spi_device, \ - .int_gpio_num = 4, \ +#define ETH_ENC28J60_DEFAULT_CONFIG(spi_host, spi_devcfg_p) \ + { \ + .spi_host_id = spi_host, \ + .spi_devcfg = spi_devcfg_p, \ + .int_gpio_num = 4, \ } /** diff --git a/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_mac_enc28j60.c b/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_mac_enc28j60.c index b159093655..d0e0c39592 100644 --- a/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_mac_enc28j60.c +++ b/examples/ethernet/enc28j60/components/eth_enc28j60/esp_eth_mac_enc28j60.c @@ -1038,6 +1038,7 @@ static esp_err_t emac_enc28j60_del(esp_eth_mac_t *mac) { emac_enc28j60_t *emac = __containerof(mac, emac_enc28j60_t, parent); vTaskDelete(emac->rx_task_hdl); + spi_bus_remove_device(emac->spi_hdl); vSemaphoreDelete(emac->spi_lock); vSemaphoreDelete(emac->reg_trans_lock); vSemaphoreDelete(emac->tx_ready_sem); @@ -1055,12 +1056,25 @@ esp_eth_mac_t *esp_eth_mac_new_enc28j60(const eth_enc28j60_config_t *enc28j60_co MAC_CHECK(emac, "calloc emac failed", err, NULL); /* enc28j60 driver is interrupt driven */ MAC_CHECK(enc28j60_config->int_gpio_num >= 0, "error interrupt gpio number", err, NULL); + /* SPI device init */ + spi_device_interface_config_t spi_devcfg; + memcpy(&spi_devcfg, enc28j60_config->spi_devcfg, sizeof(spi_device_interface_config_t)); + if (enc28j60_config->spi_devcfg->command_bits == 0 && enc28j60_config->spi_devcfg->address_bits == 0) { + /* configure default SPI frame format */ + spi_devcfg.command_bits = 3; + spi_devcfg.address_bits = 5; + } else { + MAC_CHECK(enc28j60_config->spi_devcfg->command_bits == 3 || enc28j60_config->spi_devcfg->address_bits == 5, + "incorrect SPI frame format (command_bits/address_bits)", err, NULL); + } + MAC_CHECK(spi_bus_add_device(enc28j60_config->spi_host_id, &spi_devcfg, &emac->spi_hdl) == ESP_OK, + "adding device to SPI host #%d failed", err, NULL, enc28j60_config->spi_host_id + 1); + emac->last_bank = 0xFF; emac->next_packet_ptr = ENC28J60_BUF_RX_START; /* bind methods and attributes */ emac->sw_reset_timeout_ms = mac_config->sw_reset_timeout_ms; emac->int_gpio_num = enc28j60_config->int_gpio_num; - emac->spi_hdl = enc28j60_config->spi_hdl; emac->parent.set_mediator = emac_enc28j60_set_mediator; emac->parent.init = emac_enc28j60_init; emac->parent.deinit = emac_enc28j60_deinit; diff --git a/examples/ethernet/enc28j60/main/enc28j60_example_main.c b/examples/ethernet/enc28j60/main/enc28j60_example_main.c index fa28770d18..fe893a9d33 100644 --- a/examples/ethernet/enc28j60/main/enc28j60_example_main.c +++ b/examples/ethernet/enc28j60/main/enc28j60_example_main.c @@ -84,9 +84,7 @@ void app_main(void) }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ENC28J60_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); /* ENC28J60 ethernet driver is based on spi driver */ - spi_device_interface_config_t devcfg = { - .command_bits = 3, - .address_bits = 5, + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ENC28J60_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ENC28J60_CS_GPIO, @@ -94,10 +92,7 @@ void app_main(void) .cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(CONFIG_EXAMPLE_ENC28J60_SPI_CLOCK_MHZ), }; - spi_device_handle_t spi_handle = NULL; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ENC28J60_SPI_HOST, &devcfg, &spi_handle)); - - eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle); + eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(CONFIG_EXAMPLE_ENC28J60_SPI_HOST, &spi_devcfg); enc28j60_config.int_gpio_num = CONFIG_EXAMPLE_ENC28J60_INT_GPIO; eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); diff --git a/examples/ethernet/eth2ap/main/ethernet_example_main.c b/examples/ethernet/eth2ap/main/ethernet_example_main.c index a5ca36fe1f..b98e49c18b 100644 --- a/examples/ethernet/eth2ap/main/ethernet_example_main.c +++ b/examples/ethernet/eth2ap/main/ethernet_example_main.c @@ -175,7 +175,6 @@ static void initialize_ethernet(void) #endif #elif CONFIG_ETH_USE_SPI_ETHERNET gpio_install_isr_service(0); - spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, @@ -185,46 +184,24 @@ static void initialize_ethernet(void) }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); -#if CONFIG_EXAMPLE_USE_KSZ8851SNL - spi_device_interface_config_t devcfg = { + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* KSZ8851SNL ethernet driver is based on spi driver */ - eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); +#if CONFIG_EXAMPLE_USE_KSZ8851SNL + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); ksz8851snl_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config); #elif CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config); diff --git a/examples/ethernet/iperf/main/cmd_ethernet.c b/examples/ethernet/iperf/main/cmd_ethernet.c index b9a133bfb0..6200955743 100644 --- a/examples/ethernet/iperf/main/cmd_ethernet.c +++ b/examples/ethernet/iperf/main/cmd_ethernet.c @@ -237,7 +237,6 @@ void register_ethernet(void) #endif #elif CONFIG_ETH_USE_SPI_ETHERNET gpio_install_isr_service(0); - spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO, @@ -247,67 +246,32 @@ void register_ethernet(void) }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); -#if CONFIG_EXAMPLE_USE_KSZ8851SNL - spi_device_interface_config_t devcfg = { + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* KSZ8851SNL ethernet driver is based on spi driver */ - eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); +#if CONFIG_EXAMPLE_USE_KSZ8851SNL + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); ksz8851snl_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config); #elif CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config); #elif CONFIG_EXAMPLE_USE_ENC28J60 - /* ENC28J60 ethernet driver is based on spi driver */ - spi_device_interface_config_t devcfg = { - .command_bits = 3, - .address_bits = 5, - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO, - .queue_size = 20, - .cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ) - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - - eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(spi_handle); + spi_devcfg.cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time(CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ); + eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); enc28j60_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO; - 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 phy_config.reset_gpio_num = -1; // ENC28J60 doesn't have a pin to reset internal PHY esp_eth_phy_t *phy = esp_eth_phy_new_enc28j60(&phy_config); @@ -322,16 +286,16 @@ void register_ethernet(void) ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) { 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 })); +#endif +#if CONFIG_EXAMPLE_USE_ENC28J60 && CONFIG_EXAMPLE_ENC28J60_DUPLEX_FULL + eth_duplex_t duplex = ETH_DUPLEX_FULL; + ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_DUPLEX_MODE, &duplex)); #endif ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle))); ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL)); ESP_ERROR_CHECK(esp_eth_start(eth_handle)); -#if CONFIG_EXAMPLE_USE_ENC28J60 && CONFIG_EXAMPLE_ENC28J60_DUPLEX_FULL - enc28j60_set_phy_duplex(phy, ETH_DUPLEX_FULL); -#endif - eth_control_args.control = arg_str1(NULL, NULL, "", "Get info of Ethernet"); eth_control_args.end = arg_end(1); const esp_console_cmd_t cmd = { diff --git a/examples/network/bridge/main/bridge_example_main.c b/examples/network/bridge/main/bridge_example_main.c index 6defd6575e..f4f9d02bdc 100644 --- a/examples/network/bridge/main/bridge_example_main.c +++ b/examples/network/bridge/main/bridge_example_main.c @@ -138,75 +138,35 @@ esp_eth_handle_t eth_init_spi(spi_eth_module_config_t *spi_eth_module_config, ui eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + // Set module specific PHY config + phy_config.phy_addr = spi_eth_module_config->phy_addr; + phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio; + // Configure SPI interface and Ethernet driver for specific SPI module esp_eth_mac_t *mac; esp_eth_phy_t *phy; - spi_device_handle_t spi_handle; -#if CONFIG_EXAMPLE_USE_KSZ8851SNL - spi_device_interface_config_t devcfg = { + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .queue_size = 20 + .queue_size = 20, + .spics_io_num = spi_eth_module_config->spi_cs_gpio }; - - // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - // KSZ8851SNL ethernet driver is based on spi driver - eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); - - // Set remaining GPIO numbers and configuration used by the SPI module +#if CONFIG_EXAMPLE_USE_KSZ8851SNL + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); ksz8851snl_config.int_gpio_num = spi_eth_module_config->int_gpio; - phy_config.phy_addr = spi_eth_module_config->phy_addr; - phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio; mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config); phy = esp_eth_phy_new_ksz8851snl(&phy_config); #elif CONFIG_EXAMPLE_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .queue_size = 20 - }; - - // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - // dm9051 ethernet driver is based on spi driver - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); - - // Set remaining GPIO numbers and configuration used by the SPI module + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = spi_eth_module_config->int_gpio; - phy_config.phy_addr = spi_eth_module_config->phy_addr; - phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio; mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_EXAMPLE_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .queue_size = 20 - }; - - // Set SPI module Chip Select GPIO - devcfg.spics_io_num = spi_eth_module_config->spi_cs_gpio; - - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle)); - // w5500 ethernet driver is based on spi driver - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); - - // Set remaining GPIO numbers and configuration used by the SPI module + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = spi_eth_module_config->int_gpio; - phy_config.phy_addr = spi_eth_module_config->phy_addr; - phy_config.reset_gpio_num = spi_eth_module_config->phy_reset_gpio; mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); phy = esp_eth_phy_new_w5500(&phy_config); 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 1e12dde279..ee0c6efa18 100644 --- a/examples/network/simple_sniffer/main/simple_sniffer_example_main.c +++ b/examples/network/simple_sniffer/main/simple_sniffer_example_main.c @@ -152,7 +152,6 @@ static void initialize_eth(void) #endif #elif CONFIG_ETH_USE_SPI_ETHERNET gpio_install_isr_service(0); - spi_device_handle_t spi_handle = NULL; spi_bus_config_t buscfg = { .miso_io_num = CONFIG_SNIFFER_ETH_SPI_MISO_GPIO, .mosi_io_num = CONFIG_SNIFFER_ETH_SPI_MOSI_GPIO, @@ -161,47 +160,24 @@ static void initialize_eth(void) .quadhd_io_num = -1, }; ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_SNIFFER_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO)); - -#if CONFIG_SNIFFER_USE_KSZ8851SNL - spi_device_interface_config_t devcfg = { + spi_device_interface_config_t spi_devcfg = { .mode = 0, .clock_speed_hz = CONFIG_SNIFFER_ETH_SPI_CLOCK_MHZ * 1000 * 1000, .spics_io_num = CONFIG_SNIFFER_ETH_SPI_CS_GPIO, .queue_size = 20 }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SNIFFER_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* KSZ8851SNL ethernet driver is based on spi driver */ - eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(spi_handle); +#if CONFIG_SNIFFER_USE_KSZ8851SNL + eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_SNIFFER_ETH_SPI_HOST, &spi_devcfg); ksz8851snl_config.int_gpio_num = CONFIG_SNIFFER_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_ksz8851snl(&phy_config); #elif CONFIG_SNIFFER_USE_DM9051 - spi_device_interface_config_t devcfg = { - .command_bits = 1, - .address_bits = 7, - .mode = 0, - .clock_speed_hz = CONFIG_SNIFFER_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_SNIFFER_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SNIFFER_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* dm9051 ethernet driver is based on spi driver */ - eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); + eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_SNIFFER_ETH_SPI_HOST, &spi_devcfg); dm9051_config.int_gpio_num = CONFIG_SNIFFER_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config); #elif CONFIG_SNIFFER_USE_W5500 - spi_device_interface_config_t devcfg = { - .command_bits = 16, // Actually it's the address phase in W5500 SPI frame - .address_bits = 8, // Actually it's the control phase in W5500 SPI frame - .mode = 0, - .clock_speed_hz = CONFIG_SNIFFER_ETH_SPI_CLOCK_MHZ * 1000 * 1000, - .spics_io_num = CONFIG_SNIFFER_ETH_SPI_CS_GPIO, - .queue_size = 20 - }; - ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_SNIFFER_ETH_SPI_HOST, &devcfg, &spi_handle)); - /* w5500 ethernet driver is based on spi driver */ - eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle); + eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_SNIFFER_ETH_SPI_HOST, &spi_devcfg); w5500_config.int_gpio_num = CONFIG_SNIFFER_ETH_SPI_INT_GPIO; esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config); esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);