Skip to content

Commit aed7b4f

Browse files
authored
ETH: Support physical address auto detection (espressif#9313)
* feat(eth): Support phy address auto detection * fix(eth): add phy_address check
1 parent ebca505 commit aed7b4f

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

Diff for: libraries/Ethernet/src/ETH.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,16 @@ bool ETHClass::ethDetachBus(void * bus_pointer){
8282
}
8383

8484
#if CONFIG_ETH_USE_ESP32_EMAC
85-
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clock_mode)
85+
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clock_mode)
8686
{
8787
esp_err_t ret = ESP_OK;
8888
if(_esp_netif != NULL){
8989
return true;
9090
}
91+
if(phy_addr < ETH_PHY_ADDR_AUTO){
92+
log_e("Invalid PHY address: %d, set to ETH_PHY_ADDR_AUTO for auto detection", phy_addr);
93+
return false;
94+
}
9195
perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET_RMII, ETHClass::ethDetachBus);
9296
perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET_CLK, ETHClass::ethDetachBus);
9397
perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET_MCD, ETHClass::ethDetachBus);
@@ -168,7 +172,7 @@ bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, i
168172
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
169173
ret = esp_eth_driver_install(&eth_config, &_eth_handle);
170174
if(ret != ESP_OK){
171-
log_e("SPI Ethernet driver install failed: %d", ret);
175+
log_e("Ethernet driver install failed: %d", ret);
172176
return false;
173177
}
174178
if(_eth_handle == NULL){
@@ -340,7 +344,7 @@ esp_err_t ETHClass::eth_spi_write(uint32_t cmd, uint32_t addr, const void *data,
340344
}
341345
#endif
342346

343-
bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst,
347+
bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
344348
#if ETH_SPI_SUPPORTS_CUSTOM
345349
SPIClass *spi,
346350
#endif
@@ -360,6 +364,10 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
360364
#endif
361365
return false;
362366
}
367+
if(phy_addr < ETH_PHY_ADDR_AUTO){
368+
log_e("Invalid PHY address: %d, set to ETH_PHY_ADDR_AUTO for auto detection", phy_addr);
369+
return false;
370+
}
363371

364372
perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET_SPI, ETHClass::ethDetachBus);
365373

@@ -625,13 +633,13 @@ bool ETHClass::beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq,
625633
}
626634

627635
#if ETH_SPI_SUPPORTS_CUSTOM
628-
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz){
636+
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz){
629637

630638
return beginSPI(type, phy_addr, cs, irq, rst, &spi, -1, -1, -1, SPI2_HOST, spi_freq_mhz);
631639
}
632640
#endif
633641

634-
bool ETHClass::begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi, uint8_t spi_freq_mhz){
642+
bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck, int miso, int mosi, uint8_t spi_freq_mhz){
635643

636644
return beginSPI(type, phy_addr, cs, irq, rst,
637645
#if ETH_SPI_SUPPORTS_CUSTOM

Diff for: libraries/Ethernet/src/ETH.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ typedef enum { ETH_CLOCK_GPIO0_IN, ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ET
8787
#define ETH_PHY_SPI_FREQ_MHZ 20
8888
#endif /* ETH_PHY_SPI_FREQ_MHZ */
8989

90+
#define ETH_PHY_ADDR_AUTO ESP_ETH_PHY_ADDR_AUTO
91+
9092
typedef enum {
9193
#if CONFIG_ETH_USE_ESP32_EMAC
9294
ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_RTL8201, ETH_PHY_DP83848, ETH_PHY_KSZ8041, ETH_PHY_KSZ8081,
@@ -109,12 +111,12 @@ class ETHClass {
109111
~ETHClass();
110112

111113
#if CONFIG_ETH_USE_ESP32_EMAC
112-
bool begin(eth_phy_type_t type, uint8_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clk_mode);
114+
bool begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clk_mode);
113115
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
114116
#if ETH_SPI_SUPPORTS_CUSTOM
115-
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);
117+
bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);
116118
#endif
117-
bool begin(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck=-1, int miso=-1, int mosi=-1, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);
119+
bool begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck=-1, int miso=-1, int mosi=-1, uint8_t spi_freq_mhz=ETH_PHY_SPI_FREQ_MHZ);
118120

119121
bool begin(){
120122
#if defined(ETH_PHY_TYPE) && defined(ETH_PHY_ADDR)
@@ -208,7 +210,7 @@ class ETHClass {
208210
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
209211

210212
static bool ethDetachBus(void * bus_pointer);
211-
bool beginSPI(eth_phy_type_t type, uint8_t phy_addr, int cs, int irq, int rst,
213+
bool beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst,
212214
#if ETH_SPI_SUPPORTS_CUSTOM
213215
SPIClass * spi,
214216
#endif

0 commit comments

Comments
 (0)