From 24b5f60b34afff8a0bee667fae3f9cf50e18fb9a Mon Sep 17 00:00:00 2001 From: kerms Date: Mon, 18 Dec 2023 16:48:34 +0800 Subject: [PATCH] migrate to idfv5.1.2 (netif, wifi_event) --- components/DAP/include/swo.h | 4 +- components/DAP/source/SWO.c | 4 +- .../elaphureLink/elaphureLink_protocol.h | 4 +- main/idf_component.yml | 17 ++++ main/kcp_server.c | 3 +- main/main.c | 2 +- main/tcp_netconn.c | 4 +- main/tcp_server.c | 4 +- main/uart_bridge.c | 2 +- main/wifi_handle.c | 84 +++++++++++-------- 10 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 main/idf_component.yml diff --git a/components/DAP/include/swo.h b/components/DAP/include/swo.h index b7d1ff5..5e8b6d9 100644 --- a/components/DAP/include/swo.h +++ b/components/DAP/include/swo.h @@ -1,7 +1,9 @@ #ifndef __SWO_H__ #define __SWO_H__ -typedef void * EventGroupHandle_t; +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" // event group bits #define SWO_GOT_DATA 0x00000001 diff --git a/components/DAP/source/SWO.c b/components/DAP/source/SWO.c index 4e2fcdc..d2ae268 100644 --- a/components/DAP/source/SWO.c +++ b/components/DAP/source/SWO.c @@ -44,9 +44,7 @@ #include "components/DAP/include/swo.h" #include "esp_err.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" + EventGroupHandle_t kSwoThreadEventGroup; diff --git a/components/elaphureLink/elaphureLink_protocol.h b/components/elaphureLink/elaphureLink_protocol.h index 751c024..b17f2c1 100644 --- a/components/elaphureLink/elaphureLink_protocol.h +++ b/components/elaphureLink/elaphureLink_protocol.h @@ -15,7 +15,7 @@ typedef struct { uint32_t el_link_identifier; uint32_t command; - uint32_t el_proxy_version + uint32_t el_proxy_version; } __attribute__((packed)) el_request_handshake; @@ -23,7 +23,7 @@ typedef struct { uint32_t el_link_identifier; uint32_t command; - uint32_t el_dap_version + uint32_t el_dap_version; } __attribute__((packed)) el_response_handshake; diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..6571fdc --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,17 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/mdns: "*" + ## Required IDF version + idf: + version: ">=4.1.0" + # # Put list of dependencies here + # # For components maintained by Espressif: + # component: "~1.0.0" + # # For 3rd party components: + # username/component: ">=1.0.0,<2.0.0" + # username2/component2: + # version: "~1.0.0" + # # For transient dependencies `public` flag can be set. + # # `public` flag doesn't have an effect dependencies of the `main` component. + # # All dependencies of `main` are public by default. + # public: true diff --git a/main/kcp_server.c b/main/kcp_server.c index bfbf4be..99b6214 100644 --- a/main/kcp_server.c +++ b/main/kcp_server.c @@ -21,7 +21,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" @@ -157,6 +157,7 @@ void kcp_server_task() case ACCEPTING: kState = ATTACHING; + __attribute__((fallthrough)); case ATTACHING: attach((uint8_t *)kcp_buffer, ret); break; diff --git a/main/main.c b/main/main.c index 63a1b6f..3564615 100644 --- a/main/main.c +++ b/main/main.c @@ -26,7 +26,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" diff --git a/main/tcp_netconn.c b/main/tcp_netconn.c index 1374871..950fe98 100644 --- a/main/tcp_netconn.c +++ b/main/tcp_netconn.c @@ -27,7 +27,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "freertos/queue.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -177,7 +177,7 @@ void tcp_netconn_task() { case ACCEPTING: kState = ATTACHING; - + __attribute__((fallthrough)); case ATTACHING: attach((uint8_t *)buffer, len_buf); kState = EMULATING; diff --git a/main/tcp_server.c b/main/tcp_server.c index 2e44308..1850da8 100644 --- a/main/tcp_server.c +++ b/main/tcp_server.c @@ -22,7 +22,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" @@ -147,7 +147,7 @@ void tcp_server_task(void *pvParameters) { case ACCEPTING: kState = ATTACHING; - + __attribute__((fallthrough)); case ATTACHING: // elaphureLink handshake if (el_handshake_process(kSock, tcp_rx_buffer, len) == 0) { diff --git a/main/uart_bridge.c b/main/uart_bridge.c index 453f33d..9465785 100644 --- a/main/uart_bridge.c +++ b/main/uart_bridge.c @@ -38,7 +38,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #include "freertos/queue.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "driver/uart.h" diff --git a/main/wifi_handle.c b/main/wifi_handle.c index 9559002..5c82aa5 100644 --- a/main/wifi_handle.c +++ b/main/wifi_handle.c @@ -1,8 +1,11 @@ -#include "sdkconfig.h" - #include #include #include +#include + +#ifdef CONFIG_EXAMPLE_IPV6 +#include +#endif #include "main/wifi_configuration.h" #include "main/uart_bridge.h" @@ -14,7 +17,7 @@ #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" -#include "esp_event_loop.h" +#include "esp_event.h" #include "esp_log.h" #ifdef CONFIG_IDF_TARGET_ESP8266 @@ -28,6 +31,7 @@ #endif static EventGroupHandle_t wifi_event_group; +static esp_netif_t *sta_netif; static int ssid_index = 0; const int IPV4_GOTIP_BIT = BIT0; @@ -37,30 +41,32 @@ const int IPV6_GOTIP_BIT = BIT1; static void ssid_change(); -static esp_err_t event_handler(void *ctx, system_event_t *event) { - /* For accessing reason codes in case of disconnection */ - system_event_info_t *info = &event->event_info; - - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: +static void event_handler(void *handler_arg __attribute__((unused)), + esp_event_base_t event_base __attribute__((unused)), + int32_t event_id, + void *event_data) +{ + switch (event_id) { + case WIFI_EVENT_STA_START: esp_wifi_connect(); break; - case SYSTEM_EVENT_STA_CONNECTED: + case WIFI_EVENT_STA_CONNECTED: #ifdef CONFIG_EXAMPLE_IPV6 /* enable ipv6 */ - tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA); + esp_netif_create_ip6_linklocal(sta_netif); #endif break; - case SYSTEM_EVENT_STA_GOT_IP: - GPIO_SET_LEVEL_HIGH(PIN_LED_WIFI_STATUS); - + case IP_EVENT_STA_GOT_IP: { + ip_event_got_ip_t *event = event_data; + GPIO_SET_LEVEL_HIGH(PIN_LED_WIFI_STATUS); xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT); - os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa((const ip4_addr_t *) &event->ip_info.ip)); break; - case SYSTEM_EVENT_STA_DISCONNECTED: - GPIO_SET_LEVEL_LOW(PIN_LED_WIFI_STATUS); - - os_printf("Disconnect reason : %d\r\n", (int)info->disconnected.reason); + } + case WIFI_EVENT_STA_DISCONNECTED: { + wifi_event_sta_disconnected_t *event = event_data; + GPIO_SET_LEVEL_LOW(PIN_LED_WIFI_STATUS); + os_printf("Disconnect reason : %d\r\n", event->reason); #ifdef CONFIG_IDF_TARGET_ESP8266 if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) { @@ -79,18 +85,20 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) { uart_bridge_close(); #endif break; - case SYSTEM_EVENT_AP_STA_GOT_IP6: + } + case IP_EVENT_GOT_IP6: { #ifdef CONFIG_EXAMPLE_IPV6 + ip_event_got_ip6_t *event = event_data; xEventGroupSetBits(wifi_event_group, IPV6_GOTIP_BIT); os_printf("SYSTEM_EVENT_STA_GOT_IP6\r\n"); - char *ip6 = ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip); + char *ip6 = ip6addr_ntoa((ip6_addr_t *) &event->ip6_info); os_printf("IPv6: %s\r\n", ip6); #endif + } default: break; } - return ESP_OK; } static void ssid_change() { @@ -127,29 +135,31 @@ void wifi_init(void) { GPIO_FUNCTION_SET(PIN_LED_WIFI_STATUS); GPIO_SET_DIRECTION_NORMAL_OUT(PIN_LED_WIFI_STATUS); - tcpip_adapter_init(); + esp_netif_init(); + wifi_event_group = xEventGroupCreate(); + + ESP_ERROR_CHECK(esp_event_loop_create_default()); + sta_netif = esp_netif_create_default_wifi_sta(); + assert(sta_netif); #if (USE_STATIC_IP == 1) - tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_STA); + esp_netif_dhcpc_stop(sta_netif); - tcpip_adapter_ip_info_t ip_info; - -#define MY_IP4_ADDR(...) IP4_ADDR(__VA_ARGS__) - MY_IP4_ADDR(&ip_info.ip, DAP_IP_ADDRESS); - MY_IP4_ADDR(&ip_info.gw, DAP_IP_GATEWAY); - MY_IP4_ADDR(&ip_info.netmask, DAP_IP_NETMASK); -#undef MY_IP4_ADDR - - tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); + esp_netif_ip_info_t ip_info; + esp_netif_set_ip4_addr(&ip_info.ip, DAP_IP_ADDRESS); + esp_netif_set_ip4_addr(&ip_info.gw, DAP_IP_GATEWAY); + esp_netif_set_ip4_addr(&ip_info.netmask, DAP_IP_NETMASK); + esp_netif_set_ip_info(sta_netif, &ip_info); #endif // (USE_STATIC_IP == 1) - wifi_event_group = xEventGroupCreate(); - - ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_ERROR_CHECK(esp_event_handler_instance_register( + WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL)); + ESP_ERROR_CHECK(esp_event_handler_instance_register( + IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // os_printf("Setting WiFi configuration SSID %s...\r\n", wifi_config.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));