mirror of
				https://github.com/espressif/esp-idf.git
				synced 2025-11-04 00:51:42 +01:00 
			
		
		
		
	esp_eth: SPI Ethernet modules initialization simplification
This commit is contained in:
		@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,9 @@
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#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,                         \
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user