ethernet_example_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Ethernet Basic Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_netif.h"
  12. #include "esp_eth.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "driver/gpio.h"
  16. #include "sdkconfig.h"
  17. #if CONFIG_ETH_USE_SPI_ETHERNET
  18. #include "driver/spi_master.h"
  19. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  20. static const char *TAG = "eth_example";
  21. /** Event handler for Ethernet events */
  22. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  23. int32_t event_id, void *event_data)
  24. {
  25. uint8_t mac_addr[6] = {0};
  26. /* we can get the ethernet driver handle from event data */
  27. esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  28. switch (event_id) {
  29. case ETHERNET_EVENT_CONNECTED:
  30. esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  31. ESP_LOGI(TAG, "Ethernet Link Up");
  32. ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  33. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  34. break;
  35. case ETHERNET_EVENT_DISCONNECTED:
  36. ESP_LOGI(TAG, "Ethernet Link Down");
  37. break;
  38. case ETHERNET_EVENT_START:
  39. ESP_LOGI(TAG, "Ethernet Started");
  40. break;
  41. case ETHERNET_EVENT_STOP:
  42. ESP_LOGI(TAG, "Ethernet Stopped");
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. /** Event handler for IP_EVENT_ETH_GOT_IP */
  49. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  50. int32_t event_id, void *event_data)
  51. {
  52. ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
  53. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  54. ESP_LOGI(TAG, "Ethernet Got IP Address");
  55. ESP_LOGI(TAG, "~~~~~~~~~~~");
  56. ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  57. ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  58. ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  59. ESP_LOGI(TAG, "~~~~~~~~~~~");
  60. }
  61. void app_main(void)
  62. {
  63. // Initialize TCP/IP network interface (should be called only once in application)
  64. ESP_ERROR_CHECK(esp_netif_init());
  65. // Create default event loop that running in background
  66. ESP_ERROR_CHECK(esp_event_loop_create_default());
  67. esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  68. esp_netif_t *eth_netif = esp_netif_new(&cfg);
  69. // Set default handlers to process TCP/IP stuffs
  70. ESP_ERROR_CHECK(esp_eth_set_default_handlers(eth_netif));
  71. // Register user defined event handers
  72. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  73. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  74. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  75. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  76. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  77. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  78. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  79. mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  80. mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  81. esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
  82. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  83. esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
  84. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  85. esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
  86. #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
  87. esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
  88. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  89. esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
  90. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ8041
  91. esp_eth_phy_t *phy = esp_eth_phy_new_ksz8041(&phy_config);
  92. #endif
  93. #elif CONFIG_ETH_USE_SPI_ETHERNET
  94. gpio_install_isr_service(0);
  95. spi_device_handle_t spi_handle = NULL;
  96. spi_bus_config_t buscfg = {
  97. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  98. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  99. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  100. .quadwp_io_num = -1,
  101. .quadhd_io_num = -1,
  102. };
  103. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
  104. #if CONFIG_EXAMPLE_USE_DM9051
  105. spi_device_interface_config_t devcfg = {
  106. .command_bits = 1,
  107. .address_bits = 7,
  108. .mode = 0,
  109. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  110. .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
  111. .queue_size = 20
  112. };
  113. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  114. /* dm9051 ethernet driver is based on spi driver */
  115. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
  116. dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
  117. esp_eth_mac_t *mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  118. esp_eth_phy_t *phy = esp_eth_phy_new_dm9051(&phy_config);
  119. #elif CONFIG_EXAMPLE_USE_W5500
  120. spi_device_interface_config_t devcfg = {
  121. .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
  122. .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
  123. .mode = 0,
  124. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  125. .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
  126. .queue_size = 20
  127. };
  128. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  129. /* w5500 ethernet driver is based on spi driver */
  130. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  131. w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
  132. esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  133. esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
  134. #endif
  135. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  136. esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
  137. esp_eth_handle_t eth_handle = NULL;
  138. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
  139. #if !CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  140. /* The SPI Ethernet module might doesn't have a burned factory MAC address, we cat to set it manually.
  141. 02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
  142. */
  143. ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
  144. 0x02, 0x00, 0x00, 0x12, 0x34, 0x56
  145. }));
  146. #endif
  147. /* attach Ethernet driver to TCP/IP stack */
  148. ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
  149. /* start Ethernet driver state machine */
  150. ESP_ERROR_CHECK(esp_eth_start(eth_handle));
  151. }