connect.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
  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 <string.h>
  8. #include "protocol_examples_common.h"
  9. #include "sdkconfig.h"
  10. #include "esp_event.h"
  11. #include "esp_wifi.h"
  12. #include "esp_wifi_default.h"
  13. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  14. #include "esp_eth.h"
  15. #endif
  16. #include "esp_log.h"
  17. #include "esp_netif.h"
  18. #include "driver/gpio.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "freertos/event_groups.h"
  22. #include "lwip/err.h"
  23. #include "lwip/sys.h"
  24. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  25. #define MAX_IP6_ADDRS_PER_NETIF (5)
  26. #define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces*2)
  27. #if defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_LOCAL_LINK)
  28. #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
  29. #elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_GLOBAL)
  30. #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_GLOBAL
  31. #elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_SITE_LOCAL)
  32. #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_SITE_LOCAL
  33. #elif defined(CONFIG_EXAMPLE_CONNECT_IPV6_PREF_UNIQUE_LOCAL)
  34. #define EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_UNIQUE_LOCAL
  35. #endif // if-elif CONFIG_EXAMPLE_CONNECT_IPV6_PREF_...
  36. #else
  37. #define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (s_active_interfaces)
  38. #endif
  39. #if CONFIG_EXAMPLE_WIFI_SCAN_METHOD_FAST
  40. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_FAST_SCAN
  41. #elif CONFIG_EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL
  42. #define EXAMPLE_WIFI_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  43. #endif
  44. #if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
  45. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  46. #elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
  47. #define EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  48. #endif
  49. #if CONFIG_EXAMPLE_WIFI_AUTH_OPEN
  50. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_OPEN
  51. #elif CONFIG_EXAMPLE_WIFI_AUTH_WEP
  52. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WEP
  53. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_PSK
  54. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_PSK
  55. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_PSK
  56. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
  57. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA_WPA2_PSK
  58. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
  59. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_ENTERPRISE
  60. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_ENTERPRISE
  61. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA3_PSK
  62. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA3_PSK
  63. #elif CONFIG_EXAMPLE_WIFI_AUTH_WPA2_WPA3_PSK
  64. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_WPA3_PSK
  65. #elif CONFIG_EXAMPLE_WIFI_AUTH_WAPI_PSK
  66. #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
  67. #endif
  68. static int s_active_interfaces = 0;
  69. static xSemaphoreHandle s_semph_get_ip_addrs;
  70. static esp_ip4_addr_t s_ip_addr;
  71. static esp_netif_t *s_example_esp_netif = NULL;
  72. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  73. static esp_ip6_addr_t s_ipv6_addr;
  74. /* types of ipv6 addresses to be displayed on ipv6 events */
  75. static const char *s_ipv6_addr_types[] = {
  76. "ESP_IP6_ADDR_IS_UNKNOWN",
  77. "ESP_IP6_ADDR_IS_GLOBAL",
  78. "ESP_IP6_ADDR_IS_LINK_LOCAL",
  79. "ESP_IP6_ADDR_IS_SITE_LOCAL",
  80. "ESP_IP6_ADDR_IS_UNIQUE_LOCAL",
  81. "ESP_IP6_ADDR_IS_IPV4_MAPPED_IPV6"
  82. };
  83. #endif
  84. static const char *TAG = "example_connect";
  85. #if CONFIG_EXAMPLE_CONNECT_WIFI
  86. static esp_netif_t* wifi_start(void);
  87. static void wifi_stop(void);
  88. #endif
  89. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  90. static esp_netif_t* eth_start(void);
  91. static void eth_stop(void);
  92. #endif
  93. /**
  94. * @brief Checks the netif description if it contains specified prefix.
  95. * All netifs created withing common connect component are prefixed with the module TAG,
  96. * so it returns true if the specified netif is owned by this module
  97. */
  98. static bool is_our_netif(const char *prefix, esp_netif_t *netif)
  99. {
  100. return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix)-1) == 0;
  101. }
  102. /* set up connection, Wi-Fi and/or Ethernet */
  103. static void start(void)
  104. {
  105. #if CONFIG_EXAMPLE_CONNECT_WIFI
  106. s_example_esp_netif = wifi_start();
  107. s_active_interfaces++;
  108. #endif
  109. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  110. s_example_esp_netif = eth_start();
  111. s_active_interfaces++;
  112. #endif
  113. #if CONFIG_EXAMPLE_CONNECT_WIFI && CONFIG_EXAMPLE_CONNECT_ETHERNET
  114. /* if both intefaces at once, clear out to indicate that multiple netifs are active */
  115. s_example_esp_netif = NULL;
  116. #endif
  117. s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
  118. }
  119. /* tear down connection, release resources */
  120. static void stop(void)
  121. {
  122. #if CONFIG_EXAMPLE_CONNECT_WIFI
  123. wifi_stop();
  124. s_active_interfaces--;
  125. #endif
  126. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  127. eth_stop();
  128. s_active_interfaces--;
  129. #endif
  130. }
  131. static void on_got_ip(void *arg, esp_event_base_t event_base,
  132. int32_t event_id, void *event_data)
  133. {
  134. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  135. if (!is_our_netif(TAG, event->esp_netif)) {
  136. ESP_LOGW(TAG, "Got IPv4 from another interface \"%s\": ignored", esp_netif_get_desc(event->esp_netif));
  137. return;
  138. }
  139. ESP_LOGI(TAG, "Got IPv4 event: Interface \"%s\" address: " IPSTR, esp_netif_get_desc(event->esp_netif), IP2STR(&event->ip_info.ip));
  140. memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
  141. xSemaphoreGive(s_semph_get_ip_addrs);
  142. }
  143. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  144. static void on_got_ipv6(void *arg, esp_event_base_t event_base,
  145. int32_t event_id, void *event_data)
  146. {
  147. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  148. if (!is_our_netif(TAG, event->esp_netif)) {
  149. ESP_LOGW(TAG, "Got IPv6 from another netif: ignored");
  150. return;
  151. }
  152. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&event->ip6_info.ip);
  153. ESP_LOGI(TAG, "Got IPv6 event: Interface \"%s\" address: " IPV6STR ", type: %s", esp_netif_get_desc(event->esp_netif),
  154. IPV62STR(event->ip6_info.ip), s_ipv6_addr_types[ipv6_type]);
  155. if (ipv6_type == EXAMPLE_CONNECT_PREFERRED_IPV6_TYPE) {
  156. memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
  157. xSemaphoreGive(s_semph_get_ip_addrs);
  158. }
  159. }
  160. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  161. esp_err_t example_connect(void)
  162. {
  163. if (s_semph_get_ip_addrs != NULL) {
  164. return ESP_ERR_INVALID_STATE;
  165. }
  166. start();
  167. ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
  168. ESP_LOGI(TAG, "Waiting for IP(s)");
  169. for (int i=0; i<NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
  170. xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
  171. }
  172. // iterate over active interfaces, and print out IPs of "our" netifs
  173. esp_netif_t *netif = NULL;
  174. esp_netif_ip_info_t ip;
  175. for (int i=0; i<esp_netif_get_nr_of_ifs(); ++i) {
  176. netif = esp_netif_next(netif);
  177. if (is_our_netif(TAG, netif)) {
  178. ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
  179. ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));
  180. ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
  181. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  182. esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
  183. int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
  184. for (int j=0; j< ip6_addrs; ++j) {
  185. esp_ip6_addr_type_t ipv6_type = esp_netif_ip6_get_addr_type(&(ip6[j]));
  186. ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s", IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]);
  187. }
  188. #endif
  189. }
  190. }
  191. return ESP_OK;
  192. }
  193. esp_err_t example_disconnect(void)
  194. {
  195. if (s_semph_get_ip_addrs == NULL) {
  196. return ESP_ERR_INVALID_STATE;
  197. }
  198. vSemaphoreDelete(s_semph_get_ip_addrs);
  199. s_semph_get_ip_addrs = NULL;
  200. stop();
  201. ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop));
  202. return ESP_OK;
  203. }
  204. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  205. static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
  206. int32_t event_id, void *event_data)
  207. {
  208. ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
  209. esp_err_t err = esp_wifi_connect();
  210. if (err == ESP_ERR_WIFI_NOT_STARTED) {
  211. return;
  212. }
  213. ESP_ERROR_CHECK(err);
  214. }
  215. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  216. static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base,
  217. int32_t event_id, void *event_data)
  218. {
  219. esp_netif_create_ip6_linklocal(esp_netif);
  220. }
  221. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  222. static esp_netif_t* wifi_start(void)
  223. {
  224. char *desc;
  225. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  226. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  227. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  228. // Prefix the interface description with the module TAG
  229. // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
  230. asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
  231. esp_netif_config.if_desc = desc;
  232. esp_netif_config.route_prio = 128;
  233. esp_netif_t *netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
  234. free(desc);
  235. esp_wifi_set_default_wifi_sta_handlers();
  236. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
  237. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
  238. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  239. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
  240. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  241. #endif
  242. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  243. wifi_config_t wifi_config = {
  244. .sta = {
  245. .ssid = CONFIG_EXAMPLE_WIFI_SSID,
  246. .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
  247. .scan_method = EXAMPLE_WIFI_SCAN_METHOD,
  248. .sort_method = EXAMPLE_WIFI_CONNECT_AP_SORT_METHOD,
  249. .threshold.rssi = CONFIG_EXAMPLE_WIFI_SCAN_RSSI_THRESHOLD,
  250. .threshold.authmode = EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD,
  251. },
  252. };
  253. ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
  254. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  255. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  256. ESP_ERROR_CHECK(esp_wifi_start());
  257. esp_wifi_connect();
  258. return netif;
  259. }
  260. static void wifi_stop(void)
  261. {
  262. esp_netif_t *wifi_netif = get_example_netif_from_desc("sta");
  263. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
  264. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));
  265. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  266. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  267. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
  268. #endif
  269. esp_err_t err = esp_wifi_stop();
  270. if (err == ESP_ERR_WIFI_NOT_INIT) {
  271. return;
  272. }
  273. ESP_ERROR_CHECK(err);
  274. ESP_ERROR_CHECK(esp_wifi_deinit());
  275. ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(wifi_netif));
  276. esp_netif_destroy(wifi_netif);
  277. s_example_esp_netif = NULL;
  278. }
  279. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  280. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  281. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  282. /** Event handler for Ethernet events */
  283. static void on_eth_event(void *esp_netif, esp_event_base_t event_base,
  284. int32_t event_id, void *event_data)
  285. {
  286. switch (event_id) {
  287. case ETHERNET_EVENT_CONNECTED:
  288. ESP_LOGI(TAG, "Ethernet Link Up");
  289. esp_netif_create_ip6_linklocal(esp_netif);
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  296. static esp_eth_handle_t s_eth_handle = NULL;
  297. static esp_eth_mac_t *s_mac = NULL;
  298. static esp_eth_phy_t *s_phy = NULL;
  299. static void *s_eth_glue = NULL;
  300. static esp_netif_t* eth_start(void)
  301. {
  302. char *desc;
  303. esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  304. // Prefix the interface description with the module TAG
  305. // Warning: the interface desc is used in tests to capture actual connection details (IP, gw, mask)
  306. asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
  307. esp_netif_config.if_desc = desc;
  308. esp_netif_config.route_prio = 64;
  309. esp_netif_config_t netif_config = {
  310. .base = &esp_netif_config,
  311. .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
  312. };
  313. esp_netif_t *netif = esp_netif_new(&netif_config);
  314. assert(netif);
  315. free(desc);
  316. // Set default handlers to process TCP/IP stuffs
  317. ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif));
  318. // Register user defined event handers
  319. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
  320. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  321. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif));
  322. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  323. #endif
  324. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  325. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  326. phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
  327. phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
  328. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  329. mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
  330. mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
  331. s_mac = esp_eth_mac_new_esp32(&mac_config);
  332. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  333. s_phy = esp_eth_phy_new_ip101(&phy_config);
  334. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  335. s_phy = esp_eth_phy_new_rtl8201(&phy_config);
  336. #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
  337. s_phy = esp_eth_phy_new_lan8720(&phy_config);
  338. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  339. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  340. #endif
  341. #elif CONFIG_EXAMPLE_USE_DM9051
  342. gpio_install_isr_service(0);
  343. spi_device_handle_t spi_handle = NULL;
  344. spi_bus_config_t buscfg = {
  345. .miso_io_num = CONFIG_EXAMPLE_DM9051_MISO_GPIO,
  346. .mosi_io_num = CONFIG_EXAMPLE_DM9051_MOSI_GPIO,
  347. .sclk_io_num = CONFIG_EXAMPLE_DM9051_SCLK_GPIO,
  348. .quadwp_io_num = -1,
  349. .quadhd_io_num = -1,
  350. };
  351. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_DM9051_SPI_HOST, &buscfg, 1));
  352. spi_device_interface_config_t devcfg = {
  353. .command_bits = 1,
  354. .address_bits = 7,
  355. .mode = 0,
  356. .clock_speed_hz = CONFIG_EXAMPLE_DM9051_SPI_CLOCK_MHZ * 1000 * 1000,
  357. .spics_io_num = CONFIG_EXAMPLE_DM9051_CS_GPIO,
  358. .queue_size = 20
  359. };
  360. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_DM9051_SPI_HOST, &devcfg, &spi_handle));
  361. /* dm9051 ethernet driver is based on spi driver */
  362. eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
  363. dm9051_config.int_gpio_num = CONFIG_EXAMPLE_DM9051_INT_GPIO;
  364. s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
  365. s_phy = esp_eth_phy_new_dm9051(&phy_config);
  366. #elif CONFIG_EXAMPLE_USE_OPENETH
  367. phy_config.autonego_timeout_ms = 100;
  368. s_mac = esp_eth_mac_new_openeth(&mac_config);
  369. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  370. #endif
  371. // Install Ethernet driver
  372. esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
  373. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
  374. // combine driver with netif
  375. s_eth_glue = esp_eth_new_netif_glue(s_eth_handle);
  376. esp_netif_attach(netif, s_eth_glue);
  377. esp_eth_start(s_eth_handle);
  378. return netif;
  379. }
  380. static void eth_stop(void)
  381. {
  382. esp_netif_t *eth_netif = get_example_netif_from_desc("eth");
  383. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip));
  384. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  385. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  386. ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
  387. #endif
  388. ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
  389. ESP_ERROR_CHECK(esp_eth_del_netif_glue(s_eth_glue));
  390. ESP_ERROR_CHECK(esp_eth_clear_default_handlers(eth_netif));
  391. ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
  392. ESP_ERROR_CHECK(s_phy->del(s_phy));
  393. ESP_ERROR_CHECK(s_mac->del(s_mac));
  394. esp_netif_destroy(eth_netif);
  395. s_example_esp_netif = NULL;
  396. }
  397. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  398. esp_netif_t *get_example_netif(void)
  399. {
  400. return s_example_esp_netif;
  401. }
  402. esp_netif_t *get_example_netif_from_desc(const char *desc)
  403. {
  404. esp_netif_t *netif = NULL;
  405. char *expected_desc;
  406. asprintf(&expected_desc, "%s: %s", TAG, desc);
  407. while ((netif = esp_netif_next(netif)) != NULL) {
  408. if (strcmp(esp_netif_get_desc(netif), expected_desc) == 0) {
  409. free(expected_desc);
  410. return netif;
  411. }
  412. }
  413. free(expected_desc);
  414. return netif;
  415. }