connect.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  13. #include "esp_eth.h"
  14. #endif
  15. #include "esp_log.h"
  16. #include "tcpip_adapter.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "freertos/event_groups.h"
  20. #include "lwip/err.h"
  21. #include "lwip/sys.h"
  22. #define GOT_IPV4_BIT BIT(0)
  23. #define GOT_IPV6_BIT BIT(1)
  24. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  25. #define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
  26. #else
  27. #define CONNECTED_BITS (GOT_IPV4_BIT)
  28. #endif
  29. static EventGroupHandle_t s_connect_event_group;
  30. static ip4_addr_t s_ip_addr;
  31. static const char *s_connection_name;
  32. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  33. static ip6_addr_t s_ipv6_addr;
  34. #endif
  35. static const char *TAG = "example_connect";
  36. /* set up connection, Wi-Fi or Ethernet */
  37. static void start(void);
  38. /* tear down connection, release resources */
  39. static void stop(void);
  40. static void on_got_ip(void *arg, esp_event_base_t event_base,
  41. int32_t event_id, void *event_data)
  42. {
  43. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  44. memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
  45. xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);
  46. }
  47. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  48. static void on_got_ipv6(void *arg, esp_event_base_t event_base,
  49. int32_t event_id, void *event_data)
  50. {
  51. ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  52. memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
  53. xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
  54. }
  55. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  56. esp_err_t example_connect(void)
  57. {
  58. if (s_connect_event_group != NULL) {
  59. return ESP_ERR_INVALID_STATE;
  60. }
  61. s_connect_event_group = xEventGroupCreate();
  62. start();
  63. xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY);
  64. ESP_LOGI(TAG, "Connected to %s", s_connection_name);
  65. ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr));
  66. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  67. ESP_LOGI(TAG, "IPv6 address: " IPV6STR, IPV62STR(s_ipv6_addr));
  68. #endif
  69. return ESP_OK;
  70. }
  71. esp_err_t example_disconnect(void)
  72. {
  73. if (s_connect_event_group == NULL) {
  74. return ESP_ERR_INVALID_STATE;
  75. }
  76. vEventGroupDelete(s_connect_event_group);
  77. s_connect_event_group = NULL;
  78. stop();
  79. ESP_LOGI(TAG, "Disconnected from %s", s_connection_name);
  80. s_connection_name = NULL;
  81. return ESP_OK;
  82. }
  83. #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
  84. static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
  85. int32_t event_id, void *event_data)
  86. {
  87. ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
  88. ESP_ERROR_CHECK(esp_wifi_connect());
  89. }
  90. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  91. static void on_wifi_connect(void *arg, esp_event_base_t event_base,
  92. int32_t event_id, void *event_data)
  93. {
  94. tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
  95. }
  96. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  97. static void start(void)
  98. {
  99. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  100. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  101. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
  102. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
  103. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  104. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL));
  105. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  106. #endif
  107. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  108. wifi_config_t wifi_config = {
  109. .sta = {
  110. .ssid = CONFIG_EXAMPLE_WIFI_SSID,
  111. .password = CONFIG_EXAMPLE_WIFI_PASSWORD,
  112. },
  113. };
  114. ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
  115. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  116. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  117. ESP_ERROR_CHECK(esp_wifi_start());
  118. ESP_ERROR_CHECK(esp_wifi_connect());
  119. s_connection_name = CONFIG_EXAMPLE_WIFI_SSID;
  120. }
  121. static void stop(void)
  122. {
  123. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
  124. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));
  125. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  126. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  127. ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
  128. #endif
  129. ESP_ERROR_CHECK(esp_wifi_stop());
  130. ESP_ERROR_CHECK(esp_wifi_deinit());
  131. }
  132. #endif // CONFIG_EXAMPLE_CONNECT_WIFI
  133. #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
  134. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  135. /** Event handler for Ethernet events */
  136. static void on_eth_event(void *arg, esp_event_base_t event_base,
  137. int32_t event_id, void *event_data)
  138. {
  139. switch (event_id) {
  140. case ETHERNET_EVENT_CONNECTED:
  141. ESP_LOGI(TAG, "Ethernet Link Up");
  142. tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. #endif // CONFIG_EXAMPLE_CONNECT_IPV6
  149. static esp_eth_handle_t s_eth_handle = NULL;
  150. static esp_eth_mac_t *s_mac = NULL;
  151. static esp_eth_phy_t *s_phy = NULL;
  152. static void start(void)
  153. {
  154. ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
  155. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
  156. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  157. ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL));
  158. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
  159. #endif
  160. eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  161. eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  162. #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
  163. s_mac = esp_eth_mac_new_esp32(&mac_config);
  164. #if CONFIG_EXAMPLE_ETH_PHY_IP101
  165. s_phy = esp_eth_phy_new_ip101(&phy_config);
  166. #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
  167. s_phy = esp_eth_phy_new_rtl8201(&phy_config);
  168. #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
  169. s_phy = esp_eth_phy_new_lan8720(&phy_config);
  170. #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
  171. s_phy = esp_eth_phy_new_dp83848(&phy_config);
  172. #endif
  173. #elif CONFIG_EXAMPLE_USE_SPI_ETHERNET
  174. gpio_install_isr_service(0);
  175. spi_device_handle_t spi_handle = NULL;
  176. spi_bus_config_t buscfg = {
  177. .miso_io_num = CONFIG_EXAMPLE_ETH_MISO_GPIO,
  178. .mosi_io_num = CONFIG_EXAMPLE_ETH_MOSI_GPIO,
  179. .sclk_io_num = CONFIG_EXAMPLE_ETH_SCLK_GPIO,
  180. .quadwp_io_num = -1,
  181. .quadhd_io_num = -1,
  182. };
  183. ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, 1));
  184. spi_device_interface_config_t devcfg = {
  185. .command_bits = 1,
  186. .address_bits = 7,
  187. .mode = 0,
  188. .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  189. .spics_io_num = CONFIG_EXAMPLE_ETH_CS_GPIO,
  190. .queue_size = 20
  191. };
  192. ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_ETH_SPI_HOST, &devcfg, &spi_handle));
  193. /* dm9051 ethernet driver is based on spi driver, so need to specify the spi handle */
  194. mac_config.spi_hdl = spi_handle;
  195. s_mac = esp_eth_mac_new_dm9051(&mac_config);
  196. s_phy = esp_eth_phy_new_dm9051(&phy_config);
  197. #endif
  198. esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
  199. ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
  200. s_connection_name = "Ethernet";
  201. }
  202. static void stop(void)
  203. {
  204. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip));
  205. #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
  206. ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
  207. ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event));
  208. #endif
  209. ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle));
  210. ESP_ERROR_CHECK(s_phy->del(s_phy));
  211. ESP_ERROR_CHECK(s_mac->del(s_mac));
  212. }
  213. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET