فهرست منبع

ethernet sample

Philippe G 3 سال پیش
والد
کامیت
97ae01b89f
3فایلهای تغییر یافته به همراه70 افزوده شده و 2 حذف شده
  1. 66 0
      components/wifi-manager/wifi_manager.c
  2. 2 1
      components/wifi-manager/wifi_manager.h
  3. 2 1
      main/esp_app_main.c

+ 66 - 0
components/wifi-manager/wifi_manager.c

@@ -46,6 +46,7 @@ Contains the freeRTOS task and all necessary support
 #include "tcpip_adapter.h"
 // IDF-V4++ #include "esp_netif.h"
 #include "esp_event.h"
+#include "esp_eth.h"
 #include "esp_wifi.h"
 #include "esp_wifi_types.h"
 #include "esp_log.h"
@@ -67,6 +68,7 @@ Contains the freeRTOS task and all necessary support
 
 #include "http_server_handlers.h"
 #include "monitor.h"
+#include "accessors.h"
 #include "globdefs.h"
 
 
@@ -264,6 +266,35 @@ void wifi_manager_reboot(reboot_type_t rtype){
 	//xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT); TODO: delete
 }
 
+/** Event handler for Ethernet events */
+static void eth_event_handler(void *arg, esp_event_base_t event_base,
+                              int32_t event_id, void *event_data)
+{
+    uint8_t mac_addr[6] = {0};
+    /* we can get the ethernet driver handle from event data */
+    esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
+
+    switch (event_id) {
+    case ETHERNET_EVENT_CONNECTED:
+        esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
+        ESP_LOGI(TAG, "Ethernet Link Up");
+        ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
+                 mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
+        break;
+    case ETHERNET_EVENT_DISCONNECTED:
+        ESP_LOGI(TAG, "Ethernet Link Down");
+        break;
+    case ETHERNET_EVENT_START:
+        ESP_LOGI(TAG, "Ethernet Started");
+        break;
+    case ETHERNET_EVENT_STOP:
+        ESP_LOGI(TAG, "Ethernet Stopped");
+        break;
+    default:
+        break;
+    }
+}
+
 void wifi_manager_init_wifi(){
 	/* event handler and event group for the wifi driver */
 	ESP_LOGD(TAG,   "Initializing wifi.  Creating event group");
@@ -285,6 +316,32 @@ void wifi_manager_init_wifi(){
     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
     ESP_LOGD(TAG,   "Initializing wifi. Starting wifi");
     ESP_ERROR_CHECK( esp_wifi_start() );
+	
+	//ETH
+	
+	eth_config_t const *eth =	config_eth_get( );
+	ESP_LOGE(TAG, "ETH MDC %d", eth->mdc);
+	ESP_LOGE(TAG, "ETH MDIO %d", eth->mdio);
+	ESP_LOGE(TAG, "ETH RST %d", eth->rst);
+	
+    ESP_ERROR_CHECK(tcpip_adapter_set_default_eth_handlers());
+    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
+
+    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
+    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
+    phy_config.phy_addr = 1;
+    phy_config.reset_gpio_num = eth->rst;
+
+    mac_config.smi_mdc_gpio_num = eth->mdc;
+    mac_config.smi_mdio_gpio_num = eth->mdio;
+    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
+    esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);
+
+    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
+    esp_eth_handle_t eth_handle = NULL;
+    ESP_ERROR_CHECK(esp_eth_driver_install(&config, &eth_handle));
+    ESP_ERROR_CHECK(esp_eth_start(eth_handle));	
+	//END_ETH
 
     taskYIELD();
     ESP_LOGD(TAG,   "Initializing wifi. done");
@@ -934,6 +991,7 @@ static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_
 				break;
 			case IP_EVENT_ETH_GOT_IP:
 				ESP_LOGD(TAG,   "IP_EVENT_ETH_GOT_IP");
+				wifi_manager_send_message(EVENT_ETH_GOT_IP, NULL);
 				break;
 			default:
 				break;
@@ -1324,6 +1382,7 @@ void wifi_manager( void * pvParameters ){
 
 			case ORDER_CONNECT_STA:
 				ESP_LOGD(TAG,   "MESSAGE: ORDER_CONNECT_STA - Begin");
+				break; // DOBREAK;
 
 				/* very important: precise that this connection attempt is specifically requested.
 				 * Param in that case is a boolean indicating if the request was made automatically
@@ -1575,6 +1634,13 @@ void wifi_manager( void * pvParameters ){
 				if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
 				break;
 
+					
+			case EVENT_ETH_GOT_IP:
+				ESP_LOGD(TAG,   "MESSAGE: EVENT_ETH_GOT_IP");
+				/* callback */
+				if(cb_ptr_arr[msg.code]) (*cb_ptr_arr[msg.code])(NULL);
+				break;
+
 			case EVENT_STA_GOT_IP:
 				ESP_LOGD(TAG,   "MESSAGE: EVENT_STA_GOT_IP");
 

+ 2 - 1
components/wifi-manager/wifi_manager.h

@@ -192,7 +192,8 @@ typedef enum message_code_t {
 	ORDER_RESTART_OTA_URL = 17,
 	ORDER_RESTART = 18,
 	ORDER_UPDATE_STATUS = 19,
-	MESSAGE_CODE_COUNT = 20 /* important for the callback array */
+	EVENT_ETH_GOT_IP = 20,
+	MESSAGE_CODE_COUNT = 21 /* important for the callback array */
 
 }message_code_t;
 

+ 2 - 1
main/esp_app_main.c

@@ -514,7 +514,8 @@ void app_main()
 	else {
 		ESP_LOGI(TAG,"Starting Wifi Manager");
 		wifi_manager_start();
-		wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
+		//wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
+		wifi_manager_set_callback(EVENT_ETH_GOT_IP, &cb_connection_got_ip);
 		wifi_manager_set_callback(EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
 		/* Start the telnet service after we are certain that the network stack has been properly initialized.
 		 * This can be either after we're started the AP mode, or after we've started the STA mode  */