ESP32-Ethernet-04

1.ESP32的选型网址链接

模组概览|乐鑫科技 (espressif.com.cn)

2.参考ESP32-Ethernet-Kit v1.2 board上的RMII接口

3.ESP32外设以太网接口 参考esp32_datasheet_cn.pdf

4.所用ESP32器件和网络器件

ESP32-PICO-MINI-02 

5.创建工程

引脚对应

TX1GPIO22
TX-ENGPIO21
RX0GPIO25
nINT/RETCLKGPIO0
TX0GPIO19
RX1GPIO26
CRSGPIO27
MDCGPIO20
MDIOGPIO8
GNDGND
VCC3.3V

6.运行结果

7.代码整理

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_event.h"
#include "esp_log.h"
#include "ethernet_init.h"
#include "sdkconfig.h"
#include "nvs_flash.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)
{
    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;
	esp_netif_t *eth_netif=(esp_netif_t *)arg;
    switch (event_id) {
    case ETHERNET_EVENT_CONNECTED:
        /
        //设置静态IP如用DHCP注释掉即可
		esp_netif_dhcpc_stop(eth_netif);
        esp_netif_ip_info_t eth;
		esp_netif_set_ip4_addr(&eth.ip,192, 168, 1, 22);
   	    esp_netif_set_ip4_addr(&eth.gw,192, 168, 1, 1);
   	    esp_netif_set_ip4_addr(&eth.netmask, 255, 255, 255, 0);
    	esp_netif_set_ip_info(eth_netif,&eth);
        //
        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;
    }
}

//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, "~~~~~~~~~~~");
    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, "~~~~~~~~~~~");
}

void app_main(void)
{
	esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
	//初始化TCP/IP网络接口
    esp_netif_init();
    //创建一个在后台运行的默认事件循环
    esp_event_loop_create_default();

    //初始化MAC和PHY默认配置
	eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    
	//PHY地址和复位引脚配置
	phy_config.phy_addr = CONFIG_EXAMPLE_ETH_PHY_ADDR;
    phy_config.reset_gpio_num = CONFIG_EXAMPLE_ETH_PHY_RST_GPIO;

	//初始化EMAC默认配置
	eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
	
	//MDC的信号引脚
	esp32_emac_config.smi_mdc_gpio_num = CONFIG_EXAMPLE_ETH_MDC_GPIO;
	//MDIO的信号引脚
    esp32_emac_config.smi_mdio_gpio_num = CONFIG_EXAMPLE_ETH_MDIO_GPIO;

	 //创建MAC实例
    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);

	//创建PHY实例
	esp_eth_phy_t *phy = esp_eth_phy_new_lan87xx(&phy_config);

	//驱动程序安装完毕后,将得到驱动程序的句柄
	esp_eth_handle_t eth_handle = NULL;
	//应用默认驱动程序配置
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);
	//安装驱动程序
	esp_eth_driver_install(&config, &eth_handle);

	//应用以太网默认的网络接口配置
	esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();

	//为以太网驱动程序创建网络接口
	esp_netif_t *eth_netif = esp_netif_new(&cfg);

	//将以太网驱动程序连接至TCP/IP协议栈
	esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle));

    //注册以太网事件处理程序
    esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_netif);
	
	//注册用户定义的IP事件处理程序
    esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_netif);

	//启动以太网驱动程序状态机
    esp_eth_start(eth_handle);
}

8.测试结果

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值