connect.c 8.6 KB

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