From ed4680cb34e50c7981cb1b9a10fd371c6f686813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 8 Jun 2023 14:24:50 +0200 Subject: [PATCH 1/4] Peripheral manager implemented --- cores/esp32/esp32-hal-periman.h | 3 ++ libraries/Ethernet/src/ETH.cpp | 70 +++++++++++++++++++++++++++++++-- libraries/Ethernet/src/ETH.h | 13 +++++- 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/cores/esp32/esp32-hal-periman.h b/cores/esp32/esp32-hal-periman.h index c435f93a4a8..8786ab1c737 100644 --- a/cores/esp32/esp32-hal-periman.h +++ b/cores/esp32/esp32-hal-periman.h @@ -57,6 +57,9 @@ typedef enum { #endif #if SOC_USB_SERIAL_JTAG_SUPPORTED || SOC_USB_OTG_SUPPORTED ESP32_BUS_TYPE_USB, // IO is used as USB pin +#endif +#if CONFIG_IDF_TARGET_ESP32 + ESP32_BUS_TYPE_ETHERNET, // IO is used as ETHERNET-RMII pin #endif ESP32_BUS_TYPE_MAX } peripheral_bus_type_t; diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index 784db0e48c7..5ba920b18d4 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -23,7 +23,6 @@ #if ESP_IDF_VERSION_MAJOR > 3 #include "esp_event.h" #include "esp_eth.h" - #include "esp_eth_phy.h" #include "esp_eth_mac.h" #include "esp_eth_com.h" #if CONFIG_IDF_TARGET_ESP32 @@ -32,6 +31,7 @@ //#include "soc/io_mux_reg.h" //#include "hal/gpio_hal.h" #endif + #include "esp32-hal-periman.h" #else #include "eth_phy/phy.h" #include "eth_phy/phy_tlk110.h" @@ -225,19 +225,28 @@ ETHClass::ETHClass() #if ESP_IDF_VERSION_MAJOR > 3 ,eth_handle(NULL) #endif - ,started(false) + ,_started(false) { } ETHClass::~ETHClass() {} +bool ETHClass::ethDetachBus(void * bus_pointer){ + ETHClass *bus = (ETHClass *) bus_pointer; + if(bus->_started) { + bus->end(); + } + return true; +} + bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode, bool use_mac_from_efuse) { #if ESP_IDF_VERSION_MAJOR > 3 if(esp_netif != NULL){ return true; } + perimanSetBusDeinit(ESP32_BUS_TYPE_ETHERNET, ETHClass::ethDetachBus); eth_clock_mode = clock_mode; tcpipInit(); @@ -267,6 +276,14 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ mac_config.smi_mdc_gpio_num = mdc; mac_config.smi_mdio_gpio_num = mdio; + _pin_mcd = mdc; + _pin_mdio = mdio; + _pin_rmii_clock = mac_config.clock_config.rmii.clock_gpio; + + if(!perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_mdio, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + eth_mac_config_t eth_mac_config = ETH_MAC_DEFAULT_CONFIG(); eth_mac_config.sw_reset_timeout_ms = 1000; @@ -281,9 +298,15 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ return false; } + _pin_power = power; + if(_pin_power != -1){ + if(!perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + } + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.phy_addr = phy_addr; phy_config.reset_gpio_num = power; + esp_eth_phy_t *eth_phy = NULL; switch(type){ case ETH_PHY_LAN8720: @@ -347,6 +370,14 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ log_e("esp_eth_start failed"); return false; } + _started = true; + + if(!perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_mdio, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(_pin_power != -1){ + if(!perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + } #else esp_err_t err; if(initialized){ @@ -355,7 +386,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ log_e("esp_eth_enable error: %d", err); return false; } - started = true; + _started = true; return true; } _eth_phy_mdc_pin = mdc; @@ -401,7 +432,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ if(err){ log_e("esp_eth_enable error: %d", err); } else { - started = true; + _started = true; return true; } } else { @@ -413,6 +444,11 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ delay(50); return true; + +err: + log_e("Failed to set all pins bus to ETHERNET"); + ETHClass::ethDetachBus((void *)(this)); + return false; } bool ETHClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) @@ -615,4 +651,30 @@ String ETHClass::macAddress(void) return String(macStr); } +#if ESP_IDF_VERSION_MAJOR > 3 + +void ETHClass::end(void) +{ + esp_err_t err = ESP_OK; + // stop Ethernet driver + if(esp_eth_stop(eth_handle) != ESP_OK) { + log_e("Failed to stop Ehternet"); + return; + } + if(esp_eth_driver_uninstall(eth_handle) != ESP_OK) { + log_e("Failed to stop Ehternet"); + return; + } + _started = false; + + perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_mdio, ESP32_BUS_TYPE_INIT, NULL); + if(_pin_power != -1){ + perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_INIT, NULL); + } +} + +#endif + ETHClass ETH; diff --git a/libraries/Ethernet/src/ETH.h b/libraries/Ethernet/src/ETH.h index c811ddbf103..5b3373ece90 100644 --- a/libraries/Ethernet/src/ETH.h +++ b/libraries/Ethernet/src/ETH.h @@ -66,10 +66,14 @@ class ETHClass { esp_netif_t *esp_netif; protected: - bool started; + bool _started; + int8_t _pin_mcd = -1; + int8_t _pin_mdio = -1; + int8_t _pin_power = -1; + int8_t _pin_rmii_clock = -1; static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data); #else - bool started; + bool _started; eth_config_t eth_config; #endif public: @@ -102,8 +106,13 @@ class ETHClass { uint8_t * macAddress(uint8_t* mac); String macAddress(); + void end(); + friend class WiFiClient; friend class WiFiServer; + + private: + static bool ethDetachBus(void * bus_pointer); }; extern ETHClass ETH; From 2f57e1f92afbfe033bba830b5d592f50a4c6cb38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 8 Jun 2023 14:44:38 +0200 Subject: [PATCH 2/4] remove unused variable --- libraries/Ethernet/src/ETH.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index 5ba920b18d4..f47c177d171 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -655,8 +655,6 @@ String ETHClass::macAddress(void) void ETHClass::end(void) { - esp_err_t err = ESP_OK; - // stop Ethernet driver if(esp_eth_stop(eth_handle) != ESP_OK) { log_e("Failed to stop Ehternet"); return; From adbc792dd817056cad0c0e5f5c4f54bddd57c2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Tue, 13 Jun 2023 08:38:40 +0200 Subject: [PATCH 3/4] Add all RMII pins --- libraries/Ethernet/src/ETH.cpp | 23 +++++++++++++++++++++++ libraries/Ethernet/src/ETH.h | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index f47c177d171..6abf1fd2bb2 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -288,6 +288,13 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ eth_mac_config.sw_reset_timeout_ms = 1000; eth_mac = esp_eth_mac_new_esp32(&mac_config, ð_mac_config); + + if(!perimanSetPinBus(ETH_RMII_TX_EN, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(ETH_RMII_TX0, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(ETH_RMII_TX1, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(ETH_RMII_RX0, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(ETH_RMII_RX1_EN, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(ETH_RMII_CRS_DV, ESP32_BUS_TYPE_INIT, NULL)){ return false; } #endif #if CONFIG_ETH_SPI_ETHERNET_DM9051 } @@ -375,6 +382,14 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ if(!perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } if(!perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } if(!perimanSetPinBus(_pin_mdio, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + + if(!perimanSetPinBus(ETH_RMII_TX_EN, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(ETH_RMII_TX0, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(ETH_RMII_TX1, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(ETH_RMII_RX0, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(ETH_RMII_RX1_EN, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(!perimanSetPinBus(ETH_RMII_CRS_DV, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } + if(_pin_power != -1){ if(!perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_ETHERNET, (void *)(this))){ goto err; } } @@ -668,6 +683,14 @@ void ETHClass::end(void) perimanSetPinBus(_pin_rmii_clock, ESP32_BUS_TYPE_INIT, NULL); perimanSetPinBus(_pin_mcd, ESP32_BUS_TYPE_INIT, NULL); perimanSetPinBus(_pin_mdio, ESP32_BUS_TYPE_INIT, NULL); + + perimanSetPinBus(ETH_RMII_TX_EN, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(ETH_RMII_TX0, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(ETH_RMII_TX1, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(ETH_RMII_RX0, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(ETH_RMII_RX1_EN, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(ETH_RMII_CRS_DV, ESP32_BUS_TYPE_INIT, NULL); + if(_pin_power != -1){ perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_INIT, NULL); } diff --git a/libraries/Ethernet/src/ETH.h b/libraries/Ethernet/src/ETH.h index 5b3373ece90..31981ec2198 100644 --- a/libraries/Ethernet/src/ETH.h +++ b/libraries/Ethernet/src/ETH.h @@ -26,6 +26,14 @@ #include "esp_eth.h" #include "esp_netif.h" +//Dedicated GPIOs for RMII +#define ETH_RMII_TX_EN 21 +#define ETH_RMII_TX0 19 +#define ETH_RMII_TX1 22 +#define ETH_RMII_RX0 25 +#define ETH_RMII_RX1_EN 26 +#define ETH_RMII_CRS_DV 27 + #ifndef ETH_PHY_ADDR #define ETH_PHY_ADDR 0 #endif From f91483956d3c336e9303a26906dd0e42812f8151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 14 Jun 2023 08:12:13 +0200 Subject: [PATCH 4/4] fix typo --- libraries/Ethernet/src/ETH.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index 6abf1fd2bb2..c50b3f3721e 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -675,7 +675,7 @@ void ETHClass::end(void) return; } if(esp_eth_driver_uninstall(eth_handle) != ESP_OK) { - log_e("Failed to stop Ehternet"); + log_e("Failed to stop Ethernet"); return; } _started = false;