2019-04-10 04:24:50 -04:00
|
|
|
/* Ethernet Basic Example
|
|
|
|
|
|
|
|
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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2019-09-01 05:15:11 -04:00
|
|
|
#include "esp_netif.h"
|
2019-04-10 04:24:50 -04:00
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
2022-07-22 11:13:04 -04:00
|
|
|
#include "ethernet_init.h"
|
2019-04-10 04:24:50 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
static const char *TAG = "eth_example";
|
|
|
|
|
|
|
|
/** 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)
|
|
|
|
{
|
2019-06-25 07:36:56 -04:00
|
|
|
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;
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
switch (event_id) {
|
|
|
|
case ETHERNET_EVENT_CONNECTED:
|
2019-06-25 07:36:56 -04:00
|
|
|
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
|
2019-04-10 04:24:50 -04:00
|
|
|
ESP_LOGI(TAG, "Ethernet Link Up");
|
2019-06-25 07:36:56 -04:00
|
|
|
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]);
|
2019-04-10 04:24:50 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Event handler for IP_EVENT_ETH_GOT_IP */
|
|
|
|
static void got_ip_event_handler(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;
|
2019-09-01 05:15:11 -04:00
|
|
|
const esp_netif_ip_info_t *ip_info = &event->ip_info;
|
2019-04-10 04:24:50 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Ethernet Got IP Address");
|
|
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
|
|
|
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
|
|
|
|
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
|
|
|
|
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
|
|
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2019-04-10 04:24:50 -04:00
|
|
|
{
|
2022-07-22 11:13:04 -04:00
|
|
|
// Initialize Ethernet driver
|
|
|
|
uint8_t eth_port_cnt = 0;
|
|
|
|
esp_eth_handle_t *eth_handles;
|
|
|
|
ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt));
|
|
|
|
|
|
|
|
// Initialize TCP/IP network interface aka the esp-netif (should be called only once in application)
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2019-11-26 04:48:38 -05:00
|
|
|
// Create default event loop that running in background
|
2019-04-10 04:24:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2021-08-03 07:34:52 -04:00
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
// Create instance(s) of esp-netif for Ethernet(s)
|
|
|
|
if (eth_port_cnt == 1) {
|
|
|
|
// Use ESP_NETIF_DEFAULT_ETH when just one Ethernet interface is used and you don't need to modify
|
|
|
|
// default esp-netif configuration parameters.
|
|
|
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
|
|
|
|
esp_netif_t *eth_netif = esp_netif_new(&cfg);
|
|
|
|
// Attach Ethernet driver to TCP/IP stack
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[0])));
|
|
|
|
} else {
|
|
|
|
// Use ESP_NETIF_INHERENT_DEFAULT_ETH when multiple Ethernet interfaces are used and so you need to modify
|
|
|
|
// esp-netif configuration parameters for each interface (name, priority, etc.).
|
|
|
|
esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
|
|
|
|
esp_netif_config_t cfg_spi = {
|
|
|
|
.base = &esp_netif_config,
|
|
|
|
.stack = ESP_NETIF_NETSTACK_DEFAULT_ETH
|
|
|
|
};
|
|
|
|
char if_key_str[10];
|
|
|
|
char if_desc_str[10];
|
|
|
|
char num_str[3];
|
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
|
|
|
itoa(i, num_str, 10);
|
|
|
|
strcat(strcpy(if_key_str, "ETH_"), num_str);
|
|
|
|
strcat(strcpy(if_desc_str, "eth"), num_str);
|
|
|
|
esp_netif_config.if_key = if_key_str;
|
|
|
|
esp_netif_config.if_desc = if_desc_str;
|
|
|
|
esp_netif_config.route_prio -= i*5;
|
|
|
|
esp_netif_t *eth_netif = esp_netif_new(&cfg_spi);
|
|
|
|
|
|
|
|
// Attach Ethernet driver to TCP/IP stack
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handles[i])));
|
|
|
|
}
|
2021-08-03 07:34:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register user defined event handers
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
|
|
|
|
|
2022-07-22 11:13:04 -04:00
|
|
|
// Start Ethernet driver state machine
|
|
|
|
for (int i = 0; i < eth_port_cnt; i++) {
|
|
|
|
ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
|
2021-08-03 07:34:52 -04:00
|
|
|
}
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|