make ETH work

This commit is contained in:
me-no-dev
2020-01-27 16:22:07 +02:00
parent ee535efb5c
commit 92db9730e0
3 changed files with 161 additions and 20 deletions

View File

@ -21,8 +21,11 @@
#include "ETH.h" #include "ETH.h"
#include "esp_system.h" #include "esp_system.h"
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
#include "esp_event.h"
#include "esp_eth.h" #include "esp_eth.h"
#include "esp_eth_phy.h" #include "esp_eth_phy.h"
#include "esp_eth_mac.h"
#include "esp_eth_com.h"
#else #else
#include "eth_phy/phy.h" #include "eth_phy/phy.h"
#include "eth_phy/phy_tlk110.h" #include "eth_phy/phy_tlk110.h"
@ -33,7 +36,62 @@
extern void tcpipInit(); extern void tcpipInit();
#ifndef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
/**
* @brief Callback function invoked when lowlevel initialization is finished
*
* @param[in] eth_handle: handle of Ethernet driver
*
* @return
* - ESP_OK: process extra lowlevel initialization successfully
* - ESP_FAIL: error occurred when processing extra lowlevel initialization
*/
//static esp_err_t on_lowlevel_init_done(esp_eth_handle_t eth_handle){
//#define PIN_PHY_POWER 2
// pinMode(PIN_PHY_POWER, OUTPUT);
// digitalWrite(PIN_PHY_POWER, HIGH);
// delay(100);
// return ESP_OK;
//}
/**
* @brief Callback function invoked when lowlevel deinitialization is finished
*
* @param[in] eth_handle: handle of Ethernet driver
*
* @return
* - ESP_OK: process extra lowlevel deinitialization successfully
* - ESP_FAIL: error occurred when processing extra lowlevel deinitialization
*/
//static esp_err_t on_lowlevel_deinit_done(esp_eth_handle_t eth_handle){
// return ESP_OK;
//}
// Event handler for Ethernet
void ETHClass::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
((ETHClass*)(arg))->eth_link = ETH_LINK_UP;
break;
case ETHERNET_EVENT_DISCONNECTED:
((ETHClass*)(arg))->eth_link = ETH_LINK_DOWN;
break;
case ETHERNET_EVENT_START:
((ETHClass*)(arg))->started = true;
break;
case ETHERNET_EVENT_STOP:
((ETHClass*)(arg))->started = false;
break;
default:
break;
}
}
#else
static int _eth_phy_mdc_pin = -1; static int _eth_phy_mdc_pin = -1;
static int _eth_phy_mdio_pin = -1; static int _eth_phy_mdio_pin = -1;
static int _eth_phy_power_pin = -1; static int _eth_phy_power_pin = -1;
@ -57,7 +115,11 @@ static void _eth_phy_power_enable(bool enable)
} }
#endif #endif
ETHClass::ETHClass():initialized(false),started(false),staticIP(false) ETHClass::ETHClass()
:initialized(false)
,staticIP(false)
,started(false)
,eth_link(ETH_LINK_DOWN)
{ {
} }
@ -66,7 +128,79 @@ ETHClass::~ETHClass()
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type){ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type){
return false;
tcpipInit();
esp_event_loop_create_default();
tcpip_adapter_set_default_eth_handlers();
esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, this);
//ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
mac_config.smi_mdc_gpio_num = mdc;
mac_config.smi_mdio_gpio_num = mdio;
//mac_config.sw_reset_timeout_ms = 1000;
esp_eth_mac_t *eth_mac = NULL;
#if CONFIG_ETH_SPI_ETHERNET_DM9051
if(type == ETH_PHY_DM9051){
return false;//todo
} else {
#endif
eth_mac = esp_eth_mac_new_esp32(&mac_config);
#if CONFIG_ETH_SPI_ETHERNET_DM9051
}
#endif
if(eth_mac == NULL){
log_e("esp_eth_mac_new_esp32 failed");
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:
eth_phy = esp_eth_phy_new_lan8720(&phy_config);
break;
case ETH_PHY_TLK110:
eth_phy = esp_eth_phy_new_ip101(&phy_config);
break;
case ETH_PHY_RTL8201:
eth_phy = esp_eth_phy_new_rtl8201(&phy_config);
break;
case ETH_PHY_DP83848:
eth_phy = esp_eth_phy_new_dp83848(&phy_config);
break;
#if CONFIG_ETH_SPI_ETHERNET_DM9051
case ETH_PHY_DM9051:
eth_phy = esp_eth_phy_new_dm9051(&phy_config);
break;
#endif
default:
break;
}
if(eth_phy == NULL){
log_e("esp_eth_phy_new failed");
return false;
}
eth_handle = NULL;
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(eth_mac, eth_phy);
//eth_config.on_lowlevel_init_done = on_lowlevel_init_done;
//eth_config.on_lowlevel_deinit_done = on_lowlevel_deinit_done;
if(esp_eth_driver_install(&eth_config, &eth_handle) != ESP_OK || eth_handle == NULL){
log_e("esp_eth_driver_install failed");
return false;
}
if(esp_eth_start(eth_handle) != ESP_OK){
log_e("esp_eth_start failed");
return false;
}
return true;
} }
#else #else
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 ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode)
@ -255,7 +389,7 @@ bool ETHClass::setHostname(const char * hostname)
bool ETHClass::fullDuplex() bool ETHClass::fullDuplex()
{ {
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
return true;//todo return true;//todo: do not see an API for this
#else #else
return eth_config.phy_get_duplex_mode(); return eth_config.phy_get_duplex_mode();
#endif #endif
@ -264,7 +398,7 @@ bool ETHClass::fullDuplex()
bool ETHClass::linkUp() bool ETHClass::linkUp()
{ {
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
return true;//todo return eth_link == ETH_LINK_UP;
#else #else
return eth_config.phy_check_link(); return eth_config.phy_check_link();
#endif #endif
@ -273,7 +407,9 @@ bool ETHClass::linkUp()
uint8_t ETHClass::linkSpeed() uint8_t ETHClass::linkSpeed()
{ {
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
return 100;//ToDo eth_speed_t link_speed;
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &link_speed);
return (link_speed == ETH_SPEED_10M)?10:100;
#else #else
return eth_config.phy_get_speed_mode()?100:10; return eth_config.phy_get_speed_mode()?100:10;
#endif #endif
@ -293,13 +429,13 @@ IPv6Address ETHClass::localIPv6()
return IPv6Address(addr.addr); return IPv6Address(addr.addr);
} }
uint8_t * macAddress(uint8_t* mac) uint8_t * ETHClass::macAddress(uint8_t* mac)
{ {
if(!mac){ if(!mac){
return NULL; return NULL;
} }
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
//ToDo esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac);
#else #else
esp_eth_get_mac(mac); esp_eth_get_mac(mac);
#endif #endif
@ -308,13 +444,9 @@ uint8_t * macAddress(uint8_t* mac)
String ETHClass::macAddress(void) String ETHClass::macAddress(void)
{ {
uint8_t mac[6] = {0,0,0,0,0,0};//ToDo uint8_t mac[6] = {0,0,0,0,0,0};
char macStr[18] = { 0 }; char macStr[18] = { 0 };
#ifdef ESP_IDF_VERSION_MAJOR macAddress(mac);
//ToDo
#else
esp_eth_get_mac(mac);
#endif
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(macStr); return String(macStr);
} }

View File

@ -45,24 +45,27 @@
#define ETH_PHY_MDIO 18 #define ETH_PHY_MDIO 18
#endif #endif
#ifndef ESP_IDF_VERSION_MAJOR
#ifndef ETH_CLK_MODE #ifndef ETH_CLK_MODE
#ifdef ESP_IDF_VERSION_MAJOR
#define ETH_CLK_MODE ESP_ETH_CLOCK_GPIO0_IN
#else
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN #define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#endif #endif
#endif #endif
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX } eth_phy_type_t; typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_RTL8201, ETH_PHY_DP83848, ETH_PHY_DM9051, ETH_PHY_MAX } eth_phy_type_t;
class ETHClass { class ETHClass {
private: private:
bool initialized; bool initialized;
bool started;
bool staticIP; bool staticIP;
#ifdef ESP_IDF_VERSION_MAJOR #ifdef ESP_IDF_VERSION_MAJOR
esp_eth_config_t eth_config; esp_eth_handle_t eth_handle;
protected:
bool started;
eth_link_t eth_link;
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
#else #else
bool started;
eth_config_t eth_config; eth_config_t eth_config;
#endif #endif
public: public:

View File

@ -123,6 +123,12 @@ void tcpipInit(){
static bool initialized = false; static bool initialized = false;
if(!initialized && _start_network_event_task()){ if(!initialized && _start_network_event_task()){
initialized = true; initialized = true;
#ifdef ESP_IDF_VERSION_MAJOR
uint8_t mac[8];
if(esp_efuse_mac_get_default(mac) == ESP_OK){
esp_base_mac_addr_set(mac);
}
#endif
tcpip_adapter_init(); tcpip_adapter_init();
} }
} }