Skip to content

Commit

Permalink
feat: Added support of SPI Ethernet PHY poll mode to ethernet_init co…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
bogdankolendovskyy committed Mar 6, 2024
1 parent e725fff commit 748d173
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
35 changes: 33 additions & 2 deletions ethernet_init/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ menu "Ethernet Configuration"

config ETHERNET_SPI_INT0_GPIO
int "Interrupt GPIO number SPI Ethernet module #1"
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
range -1 ENV_GPIO_IN_RANGE_MAX
default 4 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3
default 4 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C6
default 10 if IDF_TARGET_ESP32H2
help
Set the GPIO number used by the first SPI Ethernet module interrupt line.
Set -1 to use SPI Ethernet module in polling mode.

config ETHERNET_SPI_PHY_RST0_GPIO
int "PHY Reset GPIO number of SPI Ethernet Module #1"
Expand Down Expand Up @@ -247,13 +248,14 @@ menu "Ethernet Configuration"
config ETHERNET_SPI_INT1_GPIO
depends on ETHERNET_SPI_NUMBER > 1
int "Interrupt GPIO number SPI Ethernet module #2"
range ENV_GPIO_RANGE_MIN ENV_GPIO_IN_RANGE_MAX
range -1 ENV_GPIO_IN_RANGE_MAX
default 33 if IDF_TARGET_ESP32
default 5 if IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32S3 || IDF_TARGET_ESP32C2
default 5 if IDF_TARGET_ESP32C6
default 9 if IDF_TARGET_ESP32H2
help
Set the GPIO number used by the second SPI Ethernet module interrupt line.
Set -1 to use SPI Ethernet module in polling mode.

config ETHERNET_SPI_PHY_RST1_GPIO
depends on ETHERNET_SPI_NUMBER > 1
Expand Down Expand Up @@ -284,5 +286,34 @@ menu "Ethernet Configuration"
default "5a:bf:25:e0:41:01"
depends on (!ETHERNET_SPI_AUTOCONFIG_MAC_ADDR1) && (ETHERNET_SPI_NUMBER > 1)

config ETHERNET_SPI_POLLING0_MS_VAL
depends on ETHERNET_SPI_INT0_GPIO < 0
int "Polling period in msec of SPI Ethernet Module #1"
default 10
help
Set SPI Ethernet module polling period.

config ETHERNET_SPI_POLLING1_MS_VAL
depends on ETHERNET_SPI_NUMBER > 1 && ETHERNET_SPI_INT1_GPIO < 0
int "Polling period in msec of SPI Ethernet Module #2"
default 10
help
Set SPI Ethernet module polling period.

# Hidden variable to ensure that polling period option is visible only when interrupt is set disabled and
# it is set to known value (0) when interrupt is enabled at the same time.
config ETHERNET_SPI_POLLING0_MS
int
default ETHERNET_SPI_POLLING0_MS_VAL if ETHERNET_SPI_POLLING0_MS_VAL > 0
default 0

# Hidden variable to ensure that polling period option is visible only when interrupt is set disabled and
# it is set to known value (0) when interrupt is enabled at the same time.
config ETHERNET_SPI_POLLING1_MS
depends on ETHERNET_SPI_NUMBER > 1
int
default ETHERNET_SPI_POLLING1_MS_VAL if ETHERNET_SPI_POLLING1_MS_VAL > 0
default 0

endif # ETHERNET_SPI_SUPPORT
endmenu
23 changes: 14 additions & 9 deletions ethernet_init/ethernet_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
#define INTERNAL_ETHERNETS_NUM 0
#endif

#define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
do { \
#define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
do { \
eth_module_config[num].spi_cs_gpio = CONFIG_ETHERNET_SPI_CS ##num## _GPIO; \
eth_module_config[num].int_gpio = CONFIG_ETHERNET_SPI_INT ##num## _GPIO; \
eth_module_config[num].poll_period_ms = CONFIG_ETHERNET_SPI_POLLING ##num## _MS; \
eth_module_config[num].phy_reset_gpio = CONFIG_ETHERNET_SPI_PHY_RST ##num## _GPIO; \
eth_module_config[num].phy_addr = CONFIG_ETHERNET_SPI_PHY_ADDR ##num; \
} while(0)
Expand All @@ -50,7 +51,8 @@

typedef struct {
uint8_t spi_cs_gpio;
uint8_t int_gpio;
int8_t int_gpio;
uint32_t poll_period_ms;
int8_t phy_reset_gpio;
uint8_t phy_addr;
uint8_t *mac_addr;
Expand Down Expand Up @@ -277,18 +279,21 @@ static esp_eth_handle_t eth_init_spi(spi_eth_module_config_t *spi_eth_module_con
#if CONFIG_ETHERNET_USE_KSZ8851SNL
eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_ETHERNET_SPI_HOST, &spi_devcfg);
ksz8851snl_config.int_gpio_num = spi_eth_module_config->int_gpio;
dev->mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
dev->phy = esp_eth_phy_new_ksz8851snl(&phy_config);
sprintf(dev->dev_info.name, "KSZ8851SNL");
ksz8851snl_config.poll_period_ms = spi_eth_module_config->poll_period_ms;
dev_out->mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
dev_out->phy = esp_eth_phy_new_ksz8851snl(&phy_config);
sprintf(dev_out->dev_info.name, "KSZ8851SNL");
#elif CONFIG_ETHERNET_USE_DM9051
eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_ETHERNET_SPI_HOST, &spi_devcfg);
dm9051_config.int_gpio_num = spi_eth_module_config->int_gpio;
dev->mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
dev->phy = esp_eth_phy_new_dm9051(&phy_config);
sprintf(dev->dev_info.name, "DM9051");
dm9051_config.poll_period_ms = spi_eth_module_config->poll_period_ms;
dev_out->mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
dev_out->phy = esp_eth_phy_new_dm9051(&phy_config);
sprintf(dev_out->dev_info.name, "DM9051");
#elif CONFIG_ETHERNET_USE_W5500
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_ETHERNET_SPI_HOST, &spi_devcfg);
w5500_config.int_gpio_num = spi_eth_module_config->int_gpio;
w5500_config.poll_period_ms = spi_eth_module_config->poll_period_ms;
dev_out->mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
dev_out->phy = esp_eth_phy_new_w5500(&phy_config);
sprintf(dev_out->dev_info.name, "W5500");
Expand Down
2 changes: 1 addition & 1 deletion ethernet_init/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
idf:
version: '>=5.0'
version: '>=5.1'
lan867x:
override_path: "../lan867x"
description: This component initializes Ethernet driver based on Espressif IoT Development Framework Configuration.
Expand Down

0 comments on commit 748d173

Please sign in to comment.