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"
|
2019-11-13 23:03:14 -05:00
|
|
|
#include "driver/gpio.h"
|
2019-04-10 04:24:50 -04:00
|
|
|
#include "sdkconfig.h"
|
2020-11-06 03:06:20 -05:00
|
|
|
#if CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
#include "driver/spi_master.h"
|
|
|
|
#endif // CONFIG_ETH_USE_SPI_ETHERNET
|
2019-04-10 04:24:50 -04:00
|
|
|
|
|
|
|
static const char *TAG = "eth_example";
|
|
|
|
|
2021-08-03 07:34:52 -04:00
|
|
|
#if CONFIG_EXAMPLE_USE_SPI_ETHERNET
|
|
|
|
#define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num) \
|
|
|
|
do { \
|
|
|
|
eth_module_config[num].spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS ##num## _GPIO; \
|
|
|
|
eth_module_config[num].int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT ##num## _GPIO; \
|
|
|
|
eth_module_config[num].phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST ##num## _GPIO; \
|
|
|
|
eth_module_config[num].phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR ##num; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t spi_cs_gpio;
|
|
|
|
uint8_t int_gpio;
|
|
|
|
int8_t phy_reset_gpio;
|
|
|
|
uint8_t phy_addr;
|
|
|
|
}spi_eth_module_config_t;
|
|
|
|
#endif
|
|
|
|
|
2019-04-10 04:24:50 -04:00
|
|
|
/** 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
|
|
|
{
|
2019-11-26 04:48:38 -05:00
|
|
|
// Initialize TCP/IP network interface (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
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
|
|
|
// Create new default instance of esp-netif for Ethernet
|
2019-09-01 05:15:11 -04:00
|
|
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
|
2019-11-26 04:48:38 -05:00
|
|
|
esp_netif_t *eth_netif = esp_netif_new(&cfg);
|
2019-04-10 04:24:50 -04:00
|
|
|
|
2021-08-03 07:34:52 -04:00
|
|
|
// Init MAC and PHY configs to default
|
2019-04-10 04:24:50 -04:00
|
|
|
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
|
|
|
|
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
|
2021-08-03 07:34:52 -04:00
|
|
|
|
2019-11-13 23:03:14 -05:00
|
|
|
phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
|
|
|
|
phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;
|
2022-01-24 09:40:11 -05:00
|
|
|
eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
|
|
|
|
esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
|
|
|
|
esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;
|
|
|
|
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
|
2019-04-10 04:24:50 -04:00
|
|
|
#if CONFIG_EXAMPLE_ETH_PHY_IP101
|
|
|
|
esp_eth_phy_t *phy = esp_eth_phy_new_ip101(&phy_config);
|
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_RTL8201
|
|
|
|
esp_eth_phy_t *phy = esp_eth_phy_new_rtl8201(&phy_config);
|
2021-06-24 05:03:11 -04:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_LAN87XX
|
|
|
|
esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);
|
2019-04-10 04:24:50 -04:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_DP83848
|
|
|
|
esp_eth_phy_t *phy = esp_eth_phy_new_dp83848(&phy_config);
|
2021-11-16 10:18:43 -05:00
|
|
|
#elif CONFIG_EXAMPLE_ETH_PHY_KSZ80XX
|
|
|
|
esp_eth_phy_t *phy = esp_eth_phy_new_ksz80xx(&phy_config);
|
2019-06-25 07:36:56 -04:00
|
|
|
#endif
|
2021-08-03 07:34:52 -04:00
|
|
|
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, ð_handle));
|
|
|
|
/* attach Ethernet driver to TCP/IP stack */
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));
|
|
|
|
#endif //CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
|
|
|
|
|
|
|
#if CONFIG_EXAMPLE_USE_SPI_ETHERNET
|
|
|
|
// Create instance(s) of esp-netif for SPI Ethernet(s)
|
|
|
|
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
|
|
|
|
};
|
|
|
|
esp_netif_t *eth_netif_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
|
|
|
|
char if_key_str[10];
|
|
|
|
char if_desc_str[10];
|
|
|
|
char num_str[3];
|
|
|
|
for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
|
|
|
|
itoa(i, num_str, 10);
|
|
|
|
strcat(strcpy(if_key_str, "ETH_SPI_"), 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 = 30 - i;
|
|
|
|
eth_netif_spi[i] = esp_netif_new(&cfg_spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init MAC and PHY configs to default
|
|
|
|
eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
|
|
|
|
eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
|
|
|
|
|
|
|
|
// Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
|
2019-06-25 07:36:56 -04:00
|
|
|
gpio_install_isr_service(0);
|
2021-08-03 07:34:52 -04:00
|
|
|
|
|
|
|
// Init SPI bus
|
2019-06-25 07:36:56 -04:00
|
|
|
spi_bus_config_t buscfg = {
|
2020-11-06 03:06:20 -05:00
|
|
|
.miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
|
|
|
|
.mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
|
|
|
|
.sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
|
2019-06-25 07:36:56 -04:00
|
|
|
.quadwp_io_num = -1,
|
|
|
|
.quadhd_io_num = -1,
|
|
|
|
};
|
2021-08-03 07:34:52 -04:00
|
|
|
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
|
|
|
|
|
|
|
|
// Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
|
|
|
|
spi_eth_module_config_t spi_eth_module_config[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
|
|
|
|
INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
|
|
|
|
#if CONFIG_EXAMPLE_SPI_ETHERNETS_NUM > 1
|
|
|
|
INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 1);
|
|
|
|
#endif
|
2021-04-20 07:48:34 -04:00
|
|
|
|
2021-08-03 07:34:52 -04:00
|
|
|
// Configure SPI interface and Ethernet driver for specific SPI module
|
|
|
|
esp_eth_mac_t *mac_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
|
|
|
|
esp_eth_phy_t *phy_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM];
|
|
|
|
esp_eth_handle_t eth_handle_spi[CONFIG_EXAMPLE_SPI_ETHERNETS_NUM] = { NULL };
|
2022-06-23 06:13:50 -04:00
|
|
|
spi_device_interface_config_t spi_devcfg = {
|
2021-04-20 07:48:34 -04:00
|
|
|
.mode = 0,
|
|
|
|
.clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
|
|
|
|
.queue_size = 20
|
|
|
|
};
|
2021-08-03 07:34:52 -04:00
|
|
|
for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
|
|
|
|
// Set SPI module Chip Select GPIO
|
2022-06-23 06:13:50 -04:00
|
|
|
spi_devcfg.spics_io_num = spi_eth_module_config[i].spi_cs_gpio;
|
2021-08-03 07:34:52 -04:00
|
|
|
// Set remaining GPIO numbers and configuration used by the SPI module
|
|
|
|
phy_config_spi.phy_addr = spi_eth_module_config[i].phy_addr;
|
|
|
|
phy_config_spi.reset_gpio_num = spi_eth_module_config[i].phy_reset_gpio;
|
2022-06-23 06:13:50 -04:00
|
|
|
#if CONFIG_EXAMPLE_USE_KSZ8851SNL
|
|
|
|
eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
|
|
|
|
ksz8851snl_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
|
2021-08-03 07:34:52 -04:00
|
|
|
mac_spi[i] = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi);
|
|
|
|
phy_spi[i] = esp_eth_phy_new_ksz8851snl(&phy_config_spi);
|
2021-04-20 07:48:34 -04:00
|
|
|
#elif CONFIG_EXAMPLE_USE_DM9051
|
2022-06-23 06:13:50 -04:00
|
|
|
eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
|
2021-08-03 07:34:52 -04:00
|
|
|
dm9051_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
|
|
|
|
mac_spi[i] = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config_spi);
|
|
|
|
phy_spi[i] = esp_eth_phy_new_dm9051(&phy_config_spi);
|
2020-11-06 03:06:20 -05:00
|
|
|
#elif CONFIG_EXAMPLE_USE_W5500
|
2022-06-23 06:13:50 -04:00
|
|
|
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
|
2021-08-03 07:34:52 -04:00
|
|
|
w5500_config.int_gpio_num = spi_eth_module_config[i].int_gpio;
|
|
|
|
mac_spi[i] = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
|
|
|
|
phy_spi[i] = esp_eth_phy_new_w5500(&phy_config_spi);
|
2022-06-23 06:13:50 -04:00
|
|
|
#endif
|
2021-08-03 07:34:52 -04:00
|
|
|
esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi[i], phy_spi[i]);
|
|
|
|
ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi[i]));
|
|
|
|
|
|
|
|
/* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
|
2020-11-06 03:06:20 -05:00
|
|
|
02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.
|
2021-08-03 07:34:52 -04:00
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi[i], ETH_CMD_S_MAC_ADDR, (uint8_t[]) {
|
|
|
|
0x02, 0x00, 0x00, 0x12, 0x34, 0x56 + i
|
|
|
|
}));
|
|
|
|
|
|
|
|
// attach Ethernet driver to TCP/IP stack
|
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi[i], esp_eth_new_netif_glue(eth_handle_spi[i])));
|
|
|
|
}
|
|
|
|
#endif // CONFIG_ETH_USE_SPI_ETHERNET
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
2019-11-26 04:48:38 -05:00
|
|
|
/* start Ethernet driver state machine */
|
2021-08-03 07:34:52 -04:00
|
|
|
#if CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
2019-11-26 04:48:38 -05:00
|
|
|
ESP_ERROR_CHECK(esp_eth_start(eth_handle));
|
2021-08-03 07:34:52 -04:00
|
|
|
#endif // CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET
|
|
|
|
#if CONFIG_EXAMPLE_USE_SPI_ETHERNET
|
|
|
|
for (int i = 0; i < CONFIG_EXAMPLE_SPI_ETHERNETS_NUM; i++) {
|
|
|
|
ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi[i]));
|
|
|
|
}
|
|
|
|
#endif // CONFIG_EXAMPLE_USE_SPI_ETHERNET
|
2019-04-10 04:24:50 -04:00
|
|
|
}
|