From c475f52ece26645fbc067d156c111cc2b934b3b6 Mon Sep 17 00:00:00 2001 From: Jens Nolte <jens@nightmarestudio.de> Date: Fri, 19 Jun 2020 02:27:57 +0200 Subject: [PATCH] Unbreak ethernet (lan8720) --- qthing/network/broken/ethernet_lan8720.cpp | 119 --------------------- qthing/network/ethernet_lan8720.cpp | 92 ++++++++++++++++ 2 files changed, 92 insertions(+), 119 deletions(-) delete mode 100644 qthing/network/broken/ethernet_lan8720.cpp create mode 100644 qthing/network/ethernet_lan8720.cpp diff --git a/qthing/network/broken/ethernet_lan8720.cpp b/qthing/network/broken/ethernet_lan8720.cpp deleted file mode 100644 index 37b861b..0000000 --- a/qthing/network/broken/ethernet_lan8720.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include <stdio.h> -#include <string.h> -#include "environment.h" -#include "device/device_config.h" - -#include <network.h> - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include <lwip/sockets.h> - -#include "esp_system.h" -#include "esp_err.h" -#include "esp_event_loop.h" -#include "esp_event.h" -#include "esp_attr.h" -#include "esp_log.h" -#include "esp_eth.h" - -#include "rom/ets_sys.h" -#include "rom/gpio.h" - -#include "soc/dport_reg.h" -#include "soc/io_mux_reg.h" -#include "soc/rtc_cntl_reg.h" -#include "soc/gpio_reg.h" -#include "soc/gpio_sig_map.h" - -#include "tcpip_adapter.h" -#include "nvs_flash.h" -#include "driver/gpio.h" - -#include "soc/emac_ex_reg.h" -#include "driver/periph_ctrl.h" - - -#include "eth_phy/phy_lan8720.h" - -#define DEFAULT_ETHERNET_PHY_CONFIG phy_lan8720_default_ethernet_config - -static const char *TAG = "ethernet"; - -#define PIN_PHY_POWER GPIO_NUM_18 -#define PIN_SMI_MDC GPIO_NUM_16 -#define PIN_SMI_MDIO GPIO_NUM_17 - -static void phy_device_power_enable_via_gpio(bool enable) -{ - assert(DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable); - - if (!enable) { - /* Do the PHY-specific power_enable(false) function before powering down */ - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(false); - } - - gpio_pad_select_gpio(PIN_PHY_POWER); - gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT); - if(enable == true) { - gpio_set_level(PIN_PHY_POWER, 1); - ESP_LOGD(TAG, "phy_device_power_enable(TRUE)"); - } else { - gpio_set_level(PIN_PHY_POWER, 0); - ESP_LOGD(TAG, "phy_device_power_enable(FALSE)"); - } - - // Allow the power up/down to take effect, min 300us - vTaskDelay(1); - - if (enable) { - /* Run the PHY-specific power on operations now the PHY has power */ - DEFAULT_ETHERNET_PHY_CONFIG.phy_power_enable(true); - } -} - -static void eth_gpio_config_rmii(void) -{ - // RMII data pins are fixed: - // TXD0 = GPIO19 - // TXD1 = GPIO22 - // TX_EN = GPIO21 - // RXD0 = GPIO25 - // RXD1 = GPIO26 - // CLK == GPIO0 - phy_rmii_configure_data_interface_pins(); - // MDC and MDIO can be configured - phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); -} - -void enable_ethernet_lan8720() -{ - initialize_network(); - - #ifdef ETH_STATIC_IP - #ifdef ETH_IP - tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH); - tcpip_adapter_ip_info_t ipInfo; - inet_pton(AF_INET, ETH_IP, &ipInfo.ip); - inet_pton(AF_INET, ETH_GATEWAY, &ipInfo.gw); - inet_pton(AF_INET, ETH_NETMASK, &ipInfo.netmask); - tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &ipInfo); - #endif - #endif - - eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; - config.phy_addr = PHY1; - config.gpio_config = eth_gpio_config_rmii; - config.tcpip_input = tcpip_adapter_eth_input; - config.clock_mode = ETH_CLOCK_GPIO0_IN; - config.phy_power_enable = phy_device_power_enable_via_gpio; - - esp_err_t ret = ESP_OK; - ret = esp_eth_init(&config); - if (ret == ESP_OK) { - esp_eth_enable(); - } - else { - ESP_LOGE(TAG, "Ethernet could not be initialized (%i)", ret); - } -} \ No newline at end of file diff --git a/qthing/network/ethernet_lan8720.cpp b/qthing/network/ethernet_lan8720.cpp new file mode 100644 index 0000000..e274cdb --- /dev/null +++ b/qthing/network/ethernet_lan8720.cpp @@ -0,0 +1,92 @@ +#include <stdio.h> +#include <string.h> + +#include "device_config.h" +#include "driver/gpio.h" +#include "environment.h" +#include "error.hpp" +#include "esp_eth.h" +#include "esp_event.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "network.hpp" +#include "tcpip_adapter.h" + +static const char* TAG = "ethernet"; + +// RMII data pins are fixed: +// TXD0 = GPIO19 +// TXD1 = GPIO22 +// TX_EN = GPIO21 +// RXD0 = GPIO25 +// RXD1 = GPIO26 +// CLK == GPIO0 + +constexpr gpio_num_t PIN_PHY_POWER = GPIO_NUM_18; +constexpr gpio_num_t PIN_SMI_MDC = GPIO_NUM_16; +constexpr gpio_num_t PIN_SMI_MDIO = GPIO_NUM_17; + +/** Event handler for Ethernet events */ +static void eth_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { + uint8_t mac_addr[6] = {0}; + esp_eth_handle_t eth_handle = *(esp_eth_handle_t*)event_data; + + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr); + ESP_LOGI(TAG, "Ethernet Link Up"); + ESP_LOGI(TAG, "Ethernet MAC address %02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], + mac_addr[3], mac_addr[4], mac_addr[5]); + break; + default: + break; + } +} + +void qthing::enable_ethernet_lan8720() { + qthing::initialize_network(); + + // Enable phy clock + gpio_pad_select_gpio(PIN_PHY_POWER); + gpio_set_direction(PIN_PHY_POWER, GPIO_MODE_OUTPUT); + gpio_set_level(PIN_PHY_POWER, 1); + // Allow the power up/down to take effect, min 300us + vTaskDelay(1); + + try { + // Not required here, happens in qthing::initialize_network(); + // ESP_ERROR_CHECK(esp_event_loop_create_default()); + errorCodeToException(tcpip_adapter_set_default_eth_handlers()); + errorCodeToException(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL)); + +#ifdef ETH_STATIC_IP +#ifdef ETH_IP + tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH); + tcpip_adapter_ip_info_t ipInfo; + inet_pton(AF_INET, ETH_IP, &ipInfo.ip); + inet_pton(AF_INET, ETH_GATEWAY, &ipInfo.gw); + inet_pton(AF_INET, ETH_NETMASK, &ipInfo.netmask); + tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &ipInfo); +#endif +#endif + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy_config.phy_addr = 1; + phy_config.reset_gpio_num = -1; + mac_config.smi_mdc_gpio_num = PIN_SMI_MDC; + mac_config.smi_mdio_gpio_num = PIN_SMI_MDIO; + esp_eth_mac_t* mac = esp_eth_mac_new_esp32(&mac_config); + esp_eth_phy_t* phy = esp_eth_phy_new_lan8720(&phy_config); + + esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy); + esp_eth_handle_t eth_handle = NULL; + errorCodeToException(esp_eth_driver_install(&config, ð_handle)); + /* start Ethernet driver state machine */ + errorCodeToException(esp_eth_start(eth_handle)); + + } catch (EspError& err) { + ESP_LOGE(TAG, "Ethernet could not be initialized (%s)", err.what()); + } +} -- GitLab