| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- /* 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"
- #include "esp_eth.h"
- #include "esp_log.h"
- #include "tcpip_adapter.h"
- #include "driver/gpio.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/event_groups.h"
- #include "lwip/err.h"
- #include "lwip/sys.h"
- #define GOT_IPV4_BIT BIT(0)
- #define GOT_IPV6_BIT BIT(1)
- #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
- #define CONNECTED_BITS (GOT_IPV4_BIT | GOT_IPV6_BIT)
- #else
- #define CONNECTED_BITS (GOT_IPV4_BIT)
- #endif
- static EventGroupHandle_t s_connect_event_group;
- static ip4_addr_t s_ip_addr;
- static const char *s_connection_name;
- #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
- static ip6_addr_t s_ipv6_addr;
- #endif
- static const char *TAG = "example_connect";
- /* set up connection, Wi-Fi or Ethernet */
- static void start();
- /* tear down connection, release resources */
- static void stop();
- static void on_got_ip(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
- 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
- static void on_got_ipv6(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
- 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
- esp_err_t example_connect()
- {
- if (s_connect_event_group != NULL) {
- return ESP_ERR_INVALID_STATE;
- }
- s_connect_event_group = xEventGroupCreate();
- start();
- 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;
- }
- esp_err_t example_disconnect()
- {
- 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
- static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
- esp_wifi_connect();
- }
- #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
- static void on_wifi_connect(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
- }
- #endif // CONFIG_EXAMPLE_CONNECT_IPV6
- static void start()
- {
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- 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
- ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL));
- 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,
- .scan_method = WIFI_ALL_CHANNEL_SCAN,
- },
- };
- 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(WIFI_IF_STA, &wifi_config));
- ESP_ERROR_CHECK(esp_wifi_start());
- esp_wifi_connect();
- s_connection_name = CONFIG_EXAMPLE_WIFI_SSID;
- }
- static void stop()
- {
- 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
- ESP_ERROR_CHECK(esp_wifi_stop());
- ESP_ERROR_CHECK(esp_wifi_deinit());
- }
- #endif // CONFIG_EXAMPLE_CONNECT_WIFI
- #ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
- #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
- /** Event handler for Ethernet events */
- static void on_eth_event(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- switch (event_id) {
- case ETHERNET_EVENT_CONNECTED:
- ESP_LOGI(TAG, "Ethernet Link Up");
- tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_ETH);
- break;
- default:
- break;
- }
- }
- #endif // CONFIG_EXAMPLE_CONNECT_IPV6
- 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;
- static void start()
- {
- ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &on_got_ip, NULL));
- #ifdef CONFIG_EXAMPLE_CONNECT_IPV6
- ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_CONNECTED, &on_eth_event, NULL));
- ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
- #endif
- eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
- eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
- phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
- phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
- #if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
- mac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
- mac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
- s_mac = esp_eth_mac_new_esp32(&mac_config);
- #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);
- #endif
- #elif CONFIG_EXAMPLE_USE_DM9051
- gpio_install_isr_service(0);
- spi_device_handle_t spi_handle = NULL;
- spi_bus_config_t buscfg = {
- .miso_io_num = CONFIG_EXAMPLE_DM9051_MISO_GPIO,
- .mosi_io_num = CONFIG_EXAMPLE_DM9051_MOSI_GPIO,
- .sclk_io_num = CONFIG_EXAMPLE_DM9051_SCLK_GPIO,
- .quadwp_io_num = -1,
- .quadhd_io_num = -1,
- };
- ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_DM9051_SPI_HOST, &buscfg, 1));
- spi_device_interface_config_t devcfg = {
- .command_bits = 1,
- .address_bits = 7,
- .mode = 0,
- .clock_speed_hz = CONFIG_EXAMPLE_DM9051_SPI_CLOCK_MHZ * 1000 * 1000,
- .spics_io_num = CONFIG_EXAMPLE_DM9051_CS_GPIO,
- .queue_size = 20
- };
- ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_EXAMPLE_DM9051_SPI_HOST, &devcfg, &spi_handle));
- /* dm9051 ethernet driver is based on spi driver */
- eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(spi_handle);
- dm9051_config.int_gpio_num = CONFIG_EXAMPLE_DM9051_INT_GPIO;
- s_mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
- s_phy = esp_eth_phy_new_dm9051(&phy_config);
- #endif
- esp_eth_config_t config = ETH_DEFAULT_CONFIG(s_mac, s_phy);
- ESP_ERROR_CHECK(esp_eth_driver_install(&config, &s_eth_handle));
- ESP_ERROR_CHECK(esp_eth_start(s_eth_handle));
- s_connection_name = "Ethernet";
- }
- static void stop()
- {
- 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
- ESP_ERROR_CHECK(esp_eth_stop(s_eth_handle));
- 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));
- }
- #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|