eth_connect.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <string.h>
  7. #include "protocol_examples_common.h"
  8. #include "example_common_private.h"
  9. #include "esp_event.h"
  10. #include "esp_eth.h"
  11. #if CONFIG_ETH_USE_SPI_ETHERNET
  12. #include "driver/spi_master.h"
  13. #endif // CONFIG_ETH_USE_SPI_ETHERNET
  14. #include "esp_log.h"
  15. #include "driver/gpio.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "freertos/event_groups.h"
  19. static const char *TAG = "ethernet_connect";
  20. static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
  21. #if CONFIG_EXAMPLE_CONNECT_IPV6
  22. static SemaphoreHandle_t s_semph_get_ip6_addrs = NULL;
  23. #endif
  24. static esp_netif_t *eth_start(void);
  25. static void eth_stop(void);
  26. /** Event handler for Ethernet events */
  27. static void eth_on_got_ip(void *arg, esp_event_base_t event_base,
  28. int32_t event_id, void *event_data)
  29. {
  30. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  31. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) {
  32. return;
  33. }
  34. ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
  35. xSemaphoreGive(s_semph_get_ip_addrs);
  36. }
  37. #if CONFIG_EXAMPLE_CONNECT_IPV6
  38. static void eth_on_got_ipv6(void *arg, esp_event_base_t event_base,
  39. int32_t event_id, void *event_data)
  40. {
  41. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  42. if (!example_is_our_netif(EXAMPLE_NETIF_DESC_ETH, event->esp_netif)) {
  43. return;
  44. }
  45. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
  46. ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
  47. IPV62STR(event->ip6_info.ip), example_ipv6_addr_types_to_str[ipv6_type]);
  48. if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
  49. xSemaphoreGive(s_semph_get_ip6_addrs);
  50. }
  51. }
  52. static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
  53. int32_t event_id, void *event_data)
  54. {
  55. switch (event_id) {
  56. case ETHERNET_EVENT_CONNECTED:
  57. ESP_LOGI(TAG, "Ethernet Link Up");
  58. ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(esp_netif));
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  65. static esp_eth_handle_t s_eth_handle = NULL;
  66. static esp_eth_mac_t *s_mac = NULL;
  67. static esp_eth_phy_t *s_phy = NULL;
  68. static esp_eth_netif_glue_handle_t s_eth_glue = NULL;
  69. static esp_netif_t *eth_start(void)
  70. {
  71. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  72. // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
  73. esp_netif_config.if_desc = EXAMPLE_NETIF_DESC_ETH;
  74. esp_netif_config.route_prio = 64;
  75. esp_netif_config_t netif_config = {
  76. .base = &esp_netif_config,
  77. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  78. };
  79. esp_netif_t *netif = esp_netif_new(&netif_config);
  80. assert(netif);
  81. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  82. mac_config.rx_task_stack_size = CONFIG_EXAMPLE_ETHERNET_EMAC_TASK_STACK_SIZE;
  83. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  84. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  85. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  86. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  87. eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
  88. esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  89. esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  90. s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
  91. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  92. s_phy = esp_eth_phy_new_ip101(&phy_config);
  93. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  94. s_phy = esp_eth_phy_new_rtl8201(&phy_config);
  95. #elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
  96. s_phy = esp_eth_phy_new_lan87xx(&phy_config);
  97. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  98. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  99. #elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
  100. s_phy = esp_eth_phy_new_ksz80xx(&phy_config);
  101. #endif
  102. #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
  103. gpio_install_isr_service(0);
  104. spi_bus_config_t buscfg = {
  105. .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  106. .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  107. .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  108. .quadwp_io_num = -1,
  109. .quadhd_io_num = -1,
  110. };
  111. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  112. spi_device_interface_config_t spi_devcfg = {
  113. .mode = 0,
  114. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  115. .spics_io_num = CONFIG_EXAMPLE_ETH_SPI_CS_GPIO,
  116. .queue_size = 20
  117. };
  118. #if CONFIG_EXAMPLE_USE_DM9051
  119. /* dm9051 ethernet driver is based on spi driver */
  120. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  121. dm9051_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
  122. s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  123. s_phy = esp_eth_phy_new_dm9051(&phy_config);
  124. #elif CONFIG_EXAMPLE_USE_W5500
  125. /* w5500 ethernet driver is based on spi driver */
  126. eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  127. w5500_config.int_gpio_num = CONFIG_EXAMPLE_ETH_SPI_INT_GPIO;
  128. s_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  129. s_phy = esp_eth_phy_new_w5500(&phy_config);
  130. #endif
  131. #elif CONFIG_EXAMPLE_USE_OPENETH
  132. phy_config.autonego_timeout_ms = 100;
  133. s_mac = esp_eth_mac_new_openeth(&mac_config);
  134. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  135. #endif
  136. // Install Ethernet driver
  137. esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
  138. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_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(s_eth_handle, ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
  144. 0x02, 0x00, 0x00, 0x12, 0x34, 0x56
  145. }));
  146. #endif
  147. // combine driver with netif
  148. s_eth_glue = esp_eth_new_netif_glue(s_eth_handle);
  149. esp_netif_attach(netif, s_eth_glue);
  150. // Register user defined event handers
  151. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip, NULL));
  152. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  153. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
  154. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6, NULL));
  155. #endif
  156. esp_eth_start(s_eth_handle);
  157. return netif;
  158. }
  159. static void eth_stop(void)
  160. {
  161. esp_netif_t *eth_netif = get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH);
  162. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &eth_on_got_ip));
  163. #if CONFIG_EXAMPLE_CONNECT_IPV6
  164. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &eth_on_got_ipv6));
  165. ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
  166. #endif
  167. ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
  168. ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
  169. ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
  170. s_eth_handle = NULL;
  171. ESP_ERROR_CHECK(s_phy->del(s_phy));
  172. ESP_ERROR_CHECK(s_mac->del(s_mac));
  173. esp_netif_destroy(eth_netif);
  174. }
  175. esp_eth_handle_t get_example_eth_handle(void)
  176. {
  177. return s_eth_handle;
  178. }
  179. /* tear down connection, release resources */
  180. void example_ethernet_shutdown(void)
  181. {
  182. if (s_semph_get_ip_addrs == NULL) {
  183. return;
  184. }
  185. vSemaphoreDelete(s_semph_get_ip_addrs);
  186. s_semph_get_ip_addrs = NULL;
  187. #if CONFIG_EXAMPLE_CONNECT_IPV6
  188. vSemaphoreDelete(s_semph_get_ip6_addrs);
  189. s_semph_get_ip6_addrs = NULL;
  190. #endif
  191. eth_stop();
  192. }
  193. esp_err_t example_ethernet_connect(void)
  194. {
  195. #if CONFIG_EXAMPLE_CONNECT_IPV4
  196. s_semph_get_ip_addrs = xSemaphoreCreateBinary();
  197. if (s_semph_get_ip_addrs == NULL) {
  198. return ESP_ERR_NO_MEM;
  199. }
  200. #endif
  201. #if CONFIG_EXAMPLE_CONNECT_IPV6
  202. s_semph_get_ip6_addrs = xSemaphoreCreateBinary();
  203. if (s_semph_get_ip6_addrs == NULL) {
  204. vSemaphoreDelete(s_semph_get_ip_addrs);
  205. return ESP_ERR_NO_MEM;
  206. }
  207. #endif
  208. eth_start();
  209. ESP_LOGI(TAG, "Waiting for IP(s).");
  210. #if CONFIG_EXAMPLE_CONNECT_IPV4
  211. xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
  212. #endif
  213. #if CONFIG_EXAMPLE_CONNECT_IPV6
  214. xSemaphoreTake(s_semph_get_ip6_addrs, portMAX_DELAY);
  215. #endif
  216. return ESP_OK;
  217. }