| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    This example code is in the Public Domain (or CC0 licensed, at your option.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Unless required by applicable law or agreed to in writing, this | 
					
						
							|  |  |  |    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | 
					
						
							|  |  |  |    CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include "protocol_examples_common.h"
 | 
					
						
							|  |  |  | #include "sdkconfig.h"
 | 
					
						
							|  |  |  | #include "esp_event.h"
 | 
					
						
							|  |  |  | #include "esp_wifi.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-04 13:58:29 +02:00
										 |  |  | #include "esp_wifi_default.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-17 11:50:37 +08:00
										 |  |  | #if CONFIG_EXAMPLE_CONNECT_ETHERNET
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #include "esp_eth.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-17 11:50:37 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #include "esp_log.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | #include "esp_netif.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  | #include "driver/gpio.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #include "freertos/FreeRTOS.h"
 | 
					
						
							|  |  |  | #include "freertos/task.h"
 | 
					
						
							|  |  |  | #include "freertos/event_groups.h"
 | 
					
						
							|  |  |  | #include "lwip/err.h"
 | 
					
						
							|  |  |  | #include "lwip/sys.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | #define GOT_IPV4_BIT BIT(0)
 | 
					
						
							|  |  |  | #define GOT_IPV6_BIT BIT(1)
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | #define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | #define CONNECTED_BITS (GOT_IPV4_BIT)
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static EventGroupHandle_t s_connect_event_group; | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | static esp_ip4_addr_t s_ip_addr; | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | static const char *s_connection_name; | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | static esp_netif_t *s_example_esp_netif = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | static esp_ip6_addr_t s_ipv6_addr; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const char *TAG = "example_connect"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* set up connection, Wi-Fi or Ethernet */ | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void start(void); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* tear down connection, release resources */ | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void stop(void); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | static void on_got_ip(void *arg, esp_event_base_t event_base, | 
					
						
							|  |  |  |                       int32_t event_id, void *event_data) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     ESP_LOGI(TAG, "Got IP event!"); | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr)); | 
					
						
							|  |  |  |     xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | static void on_got_ipv6(void *arg, esp_event_base_t event_base, | 
					
						
							|  |  |  |                         int32_t event_id, void *event_data) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     ESP_LOGI(TAG, "Got IPv6 event!"); | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr)); | 
					
						
							|  |  |  |     xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | esp_err_t example_connect(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (s_connect_event_group != NULL) { | 
					
						
							|  |  |  |         return ESP_ERR_INVALID_STATE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s_connect_event_group = xEventGroupCreate(); | 
					
						
							|  |  |  |     start(); | 
					
						
							| 
									
										
										
										
											2019-10-07 16:46:25 +02:00
										 |  |  |     ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop)); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     ESP_LOGI(TAG, "Waiting for IP"); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     xEventGroupWaitBits(s_connect_event_group, CONNECTED_BITS, true, true, portMAX_DELAY); | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Connected to %s", s_connection_name); | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "IPv4 address: " IPSTR, IP2STR(&s_ip_addr)); | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "IPv6 address: " IPV6STR, IPV62STR(s_ipv6_addr)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | esp_err_t example_disconnect(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (s_connect_event_group == NULL) { | 
					
						
							|  |  |  |         return ESP_ERR_INVALID_STATE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     vEventGroupDelete(s_connect_event_group); | 
					
						
							|  |  |  |     s_connect_event_group = NULL; | 
					
						
							|  |  |  |     stop(); | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Disconnected from %s", s_connection_name); | 
					
						
							|  |  |  |     s_connection_name = NULL; | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_WIFI
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | static void on_wifi_disconnect(void *arg, esp_event_base_t event_base, | 
					
						
							|  |  |  |                                int32_t event_id, void *event_data) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); | 
					
						
							| 
									
										
										
										
											2019-10-07 16:46:25 +02:00
										 |  |  |     esp_err_t err = esp_wifi_connect(); | 
					
						
							|  |  |  |     if (err == ESP_ERR_WIFI_NOT_STARTED) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(err); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | static void on_wifi_connect(void *esp_netif, esp_event_base_t event_base, | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |                             int32_t event_id, void *event_data) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     esp_netif_create_ip6_linklocal(esp_netif); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void start(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esp_netif_t* netif = esp_netif_new(&netif_config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(netif); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-15 19:49:45 +02:00
										 |  |  |     esp_netif_attach_wifi_station(netif); | 
					
						
							|  |  |  |     esp_wifi_set_default_wifi_sta_handlers(); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     s_example_esp_netif = netif; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL)); | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif)); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); | 
					
						
							|  |  |  |     wifi_config_t wifi_config = { | 
					
						
							|  |  |  |         .sta = { | 
					
						
							|  |  |  |             .ssid = CONFIG_EXAMPLE_WIFI_SSID, | 
					
						
							|  |  |  |             .password = CONFIG_EXAMPLE_WIFI_PASSWORD, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_start()); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_wifi_connect()); | 
					
						
							|  |  |  |     s_connection_name = CONFIG_EXAMPLE_WIFI_SSID; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void stop(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip)); | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-10-07 16:46:25 +02:00
										 |  |  |     esp_err_t err = esp_wifi_stop(); | 
					
						
							|  |  |  |     if (err == ESP_ERR_WIFI_NOT_INIT) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(err); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_wifi_deinit()); | 
					
						
							| 
									
										
										
										
											2019-09-04 13:58:29 +02:00
										 |  |  |     ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(s_example_esp_netif)); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     esp_netif_destroy(s_example_esp_netif); | 
					
						
							|  |  |  |     s_example_esp_netif = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif // CONFIG_EXAMPLE_CONNECT_WIFI
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Event handler for Ethernet events */ | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | static void on_eth_event(void *esp_netif, esp_event_base_t event_base, | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |                          int32_t event_id, void *event_data) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (event_id) { | 
					
						
							|  |  |  |     case ETHERNET_EVENT_CONNECTED: | 
					
						
							|  |  |  |         ESP_LOGI(TAG, "Ethernet Link Up"); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |         esp_netif_create_ip6_linklocal(esp_netif); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | static esp_eth_handle_t s_eth_handle = NULL; | 
					
						
							|  |  |  | static esp_eth_mac_t *s_mac = NULL; | 
					
						
							|  |  |  | static esp_eth_phy_t *s_phy = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void start(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_ETH(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esp_netif_t* netif = esp_netif_new(&netif_config); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(netif); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_eth_set_default_handlers(netif)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s_example_esp_netif = netif; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL)); | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, netif)); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); | 
					
						
							|  |  |  |     eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |     phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR; | 
					
						
							|  |  |  |     phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO; | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  | #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
 | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |     mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO; | 
					
						
							|  |  |  |     mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO; | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |     s_mac = esp_eth_mac_new_esp32(&mac_config); | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | #if CONFIG_EXAMPLE_ETH_PHY_IP101
 | 
					
						
							|  |  |  |     s_phy = esp_eth_phy_new_ip101(&phy_config); | 
					
						
							|  |  |  | #elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
 | 
					
						
							|  |  |  |     s_phy = esp_eth_phy_new_rtl8201(&phy_config); | 
					
						
							|  |  |  | #elif CONFIG_EXAMPLE_ETH_PHY_LAN8720
 | 
					
						
							|  |  |  |     s_phy = esp_eth_phy_new_lan8720(&phy_config); | 
					
						
							|  |  |  | #elif CONFIG_EXAMPLE_ETH_PHY_DP83848
 | 
					
						
							|  |  |  |     s_phy = esp_eth_phy_new_dp83848(&phy_config); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  | #elif CONFIG_EXAMPLE_USE_DM9051
 | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |     gpio_install_isr_service(0); | 
					
						
							|  |  |  |     spi_device_handle_t spi_handle = NULL; | 
					
						
							|  |  |  |     spi_bus_config_t buscfg = { | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |         .miso_io_num = CONFIG_EXAMPLE_DM9051_MISO_GPIO, | 
					
						
							|  |  |  |         .mosi_io_num = CONFIG_EXAMPLE_DM9051_MOSI_GPIO, | 
					
						
							|  |  |  |         .sclk_io_num = CONFIG_EXAMPLE_DM9051_SCLK_GPIO, | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |         .quadwp_io_num = -1, | 
					
						
							|  |  |  |         .quadhd_io_num = -1, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |     ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_DM9051_SPI_HOST, &buscfg, 1)); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |     spi_device_interface_config_t devcfg = { | 
					
						
							|  |  |  |         .command_bits = 1, | 
					
						
							|  |  |  |         .address_bits = 7, | 
					
						
							|  |  |  |         .mode = 0, | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |         .clock_speed_hz = CONFIG_EXAMPLE_DM9051_SPI_CLOCK_MHZ * 1000 * 1000, | 
					
						
							|  |  |  |         .spics_io_num = CONFIG_EXAMPLE_DM9051_CS_GPIO, | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |         .queue_size = 20 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |     ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_DM9051_SPI_HOST, &devcfg, &spi_handle)); | 
					
						
							| 
									
										
										
										
											2019-09-18 18:28:55 +08:00
										 |  |  |     /* dm9051 ethernet driver is based on spi driver */ | 
					
						
							|  |  |  |     eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle); | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  |     dm9051_config.int_gpio_num = CONFIG_EXAMPLE_DM9051_INT_GPIO; | 
					
						
							| 
									
										
										
										
											2019-09-18 18:28:55 +08:00
										 |  |  |     s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config); | 
					
						
							| 
									
										
										
										
											2019-06-25 19:36:56 +08:00
										 |  |  |     s_phy = esp_eth_phy_new_dm9051(&phy_config); | 
					
						
							| 
									
										
										
										
											2019-10-01 18:50:34 +02:00
										 |  |  | #elif CONFIG_EXAMPLE_USE_OPENETH
 | 
					
						
							|  |  |  |     phy_config.autonego_timeout_ms = 100; | 
					
						
							|  |  |  |     s_mac = esp_eth_mac_new_openeth(&mac_config); | 
					
						
							|  |  |  |     s_phy = esp_eth_phy_new_dp83848(&phy_config); | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-10-01 18:50:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle)); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     esp_netif_attach(netif, s_eth_handle); | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  |     s_connection_name = "Ethernet"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 16:33:30 +07:00
										 |  |  | static void stop(void) | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip)); | 
					
						
							|  |  |  | #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
 | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(esp_event_handler_unregister(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-04-10 16:24:50 +08:00
										 |  |  |     ESP_ERROR_CHECK(esp_eth_driver_uninstall(s_eth_handle)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(s_phy->del(s_phy)); | 
					
						
							|  |  |  |     ESP_ERROR_CHECK(s_mac->del(s_mac)); | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     esp_eth_clear_default_handlers(s_example_esp_netif); | 
					
						
							|  |  |  |     esp_netif_destroy(s_example_esp_netif); | 
					
						
							|  |  |  |     s_example_esp_netif = NULL; | 
					
						
							| 
									
										
										
										
											2018-11-21 00:40:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
 | 
					
						
							| 
									
										
										
										
											2019-08-31 16:08:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | esp_netif_t *get_example_netif(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return s_example_esp_netif; | 
					
						
							| 
									
										
										
										
											2019-11-14 12:03:14 +08:00
										 |  |  | } |