基于官方案例ESP-IDF v4.4.2/example/ethernet/basic修改。
使用静态IP地址上网,设置静态地址时需要先调用下面接口来disable客服端dhcp。然后设置静态IP地址,设置静态IP的位置要对,在此感谢嘉友创信息科技开源小步。
esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif);
注意这两者的区别,我刚开始没注意写错了。
esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif);
完整代码如下
头文件定义如下,根据实际选用的SPI设置。
#define CONFIG_ETH_SPI_HOST 2
#define CONFIG_ETH_SPI_CLOCK_MHZ 12
#define CONFIG_ETH_SPI_SCLK_GPIO 18
#define CONFIG_ETH_SPI_MOSI_GPIO 23
#define CONFIG_ETH_SPI_MISO_GPIO 19
#define CONFIG_ETH_SPI_CS0_GPIO 5
#define CONFIG_ETH_SPI_INT0_GPIO 21
#define CONFIG_ETH_SPI_PHY_RST0_GPIO -1
#define CONFIG_ETH_SPI_PHY_ADDR0 1
#define IF_KEY_STR "ETH_SPI"
#define IF_DESC_STR "eth"
void bsp_ethernet_init(void)
{
ESP_LOGI(TAG, "%s", __func__);
// Initialize TCP/IP network interface (should be called only once in application)
ESP_ERROR_CHECK(esp_netif_init());
// 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_config.if_key = IF_KEY_STR;
esp_netif_config.if_desc = IF_DESC_STR;
esp_netif_config.route_prio = 30;
esp_netif_t *eth_netif_spi = NULL;
eth_netif_spi = 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
gpio_install_isr_service(0);
// Init SPI bus
spi_device_handle_t spi_handle = NULL;
spi_bus_config_t buscfg = {
.miso_io_num = CONFIG_ETH_SPI_MISO_GPIO,
.mosi_io_num = CONFIG_ETH_SPI_MOSI_GPIO,
.sclk_io_num = CONFIG_ETH_SPI_SCLK_GPIO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_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;
INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
// Configure SPI interface and Ethernet driver for specific SPI module
esp_eth_mac_t *mac_spi;
esp_eth_phy_t *phy_spi;
esp_eth_handle_t eth_handle_spi = NULL;
spi_device_interface_config_t devcfg = {
.command_bits = 16, // Actually it's the address phase in W5500 SPI frame
.address_bits = 8, // Actually it's the control phase in W5500 SPI frame
.mode = 0,
.clock_speed_hz = CONFIG_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
.queue_size = 20};
// Set SPI module Chip Select GPIO
devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_ETH_SPI_HOST, &devcfg, &spi_handle));
// w5500 ethernet driver is based on spi driver
eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
// Set remaining GPIO numbers and configuration used by the SPI module
w5500_config.int_gpio_num = spi_eth_module_config.int_gpio;
phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
ESP_ERROR_CHECK(esp_eth_driver_install(ð_config_spi, ð_handle_spi));
/* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.*/
ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, (uint8_t[]){0xB0, 0x8E, 0x1A, 0x37, 0x00, 0x01}));
// attach Ethernet driver to TCP/IP stack
ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
#if 1 // 设置静态IP地址
esp_netif_dhcpc_stop(eth_netif_spi); //注意接口不能用错
char *ip = "192.168.1.168";
char *gateway = "192.168.1.1";
char *netmask = "255.255.0.0";
esp_netif_ip_info_t ip_info;
memset(&ip_info, 0, sizeof(esp_netif_ip_info_t));
ip_info.ip.addr = esp_ip4addr_aton((const char *)ip);
ip_info.netmask.addr = esp_ip4addr_aton((const char *)netmask);
ip_info.gw.addr = esp_ip4addr_aton((const char *)gateway);
esp_err_t err = 0;
err = esp_netif_set_ip_info(eth_netif_spi, &ip_info);
if (ESP_OK == err)
ESP_LOGI(TAG, "Set static IP OK");
else
ESP_LOGI(TAG, "Set static IP error: %d", err);
#endif
// 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));
/* start Ethernet driver state machine */
ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
// web_server = start_webserver();//使用web访问,配置参数
}
/** 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;
}
}
/** 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;
const esp_netif_ip_info_t *ip_info = &event->ip_info;
ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ip_tmp = ip_info->ip;
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
// http_init();//成功获取到IP地址后,执行http上报
}
输出如下,成功获取到IP地址
网页配置如下