Advertisement
Guest User

ethernet

a guest
Jan 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include "esp_eth.h"
  2. #include "eth_phy/phy.h"
  3. #include "eth_phy/phy_lan8720.h"
  4. #include <WiFi.h>
  5. #include "IPAddress.h"
  6.  
  7. #define ETH_PHY_CONF phy_lan8720_default_ethernet_config
  8. #define ETH_PHY_ADDR PHY1
  9. #define PIN_PHY_POWER 17
  10. #define PIN_SMI_MDC 23
  11. #define PIN_SMI_MDIO 18
  12.  
  13.  
  14. static eth_config_t eth_config = ETH_PHY_CONF;
  15. boolean gotip = false;
  16.  
  17. static void ethernet_config_gpio(void){
  18. // RMII data pinek fixek:
  19. // CRS_DRV = GPIO27 PULL-UP 2,2k
  20. // TXD0 = GPIO19
  21. // TXD1 = GPIO22
  22. // TX_EN = GPIO21
  23. // RXD0 = GPIO25 PULL-UP 2,2k
  24. // RXD1 = GPIO26 PULL-UP 2,2k
  25. // CLK == GPIO0
  26. phy_rmii_configure_data_interface_pins();
  27. // MDC == GPIO 23
  28. // MDIO == GPIO 18 PULL-UP 2,2k
  29. phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
  30. }
  31.  
  32. static void ethernet_power_enable(bool enable){
  33. pinMode(PIN_PHY_POWER, OUTPUT);
  34. digitalWrite(PIN_PHY_POWER, enable);
  35. delay(1);// Kis delay hogy a power up/down működjön, min 300us
  36. }
  37.  
  38. extern void tcpipInit();
  39.  
  40. void ETH_begin(){
  41. eth_config.phy_addr = ETH_PHY_ADDR;
  42. eth_config.gpio_config = ethernet_config_gpio;
  43. eth_config.tcpip_input = tcpip_adapter_eth_input;
  44. eth_config.phy_power_enable = ethernet_power_enable;
  45. tcpipInit();
  46. esp_err_t err = esp_eth_init(&eth_config);
  47. if(!err){
  48. err = esp_eth_enable();
  49. }
  50. }
  51.  
  52. IPAddress ETH_localIP()
  53. {
  54. tcpip_adapter_ip_info_t ip;
  55. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  56. return IPAddress(ip.ip.addr);
  57. }
  58.  
  59. IPAddress ETH_subnetMask()
  60. {
  61. tcpip_adapter_ip_info_t ip;
  62. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  63. return IPAddress(ip.netmask.addr);
  64. }
  65.  
  66. IPAddress ETH_gatewayIP()
  67. {
  68. tcpip_adapter_ip_info_t ip;
  69. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
  70. return IPAddress(ip.gw.addr);
  71. }
  72.  
  73. bool ETH_setHostname(const char * hostname)
  74. {
  75. return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, hostname) == 0;
  76. }
  77.  
  78. bool ETH_fullDuplex()
  79. {
  80. return eth_config.phy_get_duplex_mode();
  81. }
  82.  
  83. uint8_t ETH_linkSpeed()
  84. {
  85. return eth_config.phy_get_speed_mode()?100:10;
  86. }
  87.  
  88. String ETH_macAddress(void)
  89. {
  90. uint8_t mac[6];
  91. char macStr[18] = { 0 };
  92. esp_eth_get_mac(mac);
  93. sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  94. return String(macStr);
  95. }
  96.  
  97. void WiFiEvent(WiFiEvent_t event){
  98. switch(event) {
  99. case SYSTEM_EVENT_ETH_START:
  100. //set eth hostname here
  101. ETH_setHostname("esp32-eth");
  102. break;
  103. case SYSTEM_EVENT_ETH_CONNECTED:
  104. //ethernet connected (if manual IP)
  105. break;
  106. case SYSTEM_EVENT_ETH_GOT_IP:
  107. //Serial.print("ETH IPv4: ");
  108. //Serial.println(ETH_localIP());
  109. gotip = true;
  110. //Serial.println("......................");
  111. break;
  112. case SYSTEM_EVENT_ETH_DISCONNECTED:
  113. //disconnected
  114. break;
  115. default:
  116. break;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement