ESP32 外挂W5500以太网,如何设置静态IP地址

xieshuxiang
Posts: 3
Joined: Mon Nov 28, 2022 10:05 am

ESP32 外挂W5500以太网,如何设置静态IP地址

Postby xieshuxiang » Mon Nov 28, 2022 10:33 am

  1.  
  2.  
  3. #define INIT_SPI_ETH_MODULE_CONFIG(eth_module_config, num)                     \
  4.     do                                                                         \
  5.     {                                                                          \
  6.         eth_module_config.spi_cs_gpio = CONFIG_ETH_SPI_CS##num##_GPIO;         \
  7.         eth_module_config.int_gpio = CONFIG_ETH_SPI_INT##num##_GPIO;           \
  8.         eth_module_config.phy_reset_gpio = CONFIG_ETH_SPI_PHY_RST##num##_GPIO; \
  9.         eth_module_config.phy_addr = CONFIG_ETH_SPI_PHY_ADDR##num;             \
  10.     } while (0)
  11.  
  12. typedef struct
  13. {
  14.     uint8_t spi_cs_gpio;
  15.     uint8_t int_gpio;
  16.     int8_t phy_reset_gpio;
  17.     uint8_t phy_addr;
  18. } spi_eth_module_config_t;
  19.  
  20. esp_ip4_addr_t ip_tmp;
  21.  
  22. /** Event handler for Ethernet events */
  23. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  24.                               int32_t event_id, void *event_data)
  25. {
  26.     uint8_t mac_addr[6] = {0};
  27.     /* we can get the ethernet driver handle from event data */
  28.     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  29.  
  30.     switch (event_id)
  31.     {
  32.     case ETHERNET_EVENT_CONNECTED:
  33.         esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  34.         ESP_LOGI(TAG, "Ethernet Link Up");
  35.         ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  36.                  mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  37.         break;
  38.     case ETHERNET_EVENT_DISCONNECTED:
  39.         ESP_LOGI(TAG, "Ethernet Link Down");
  40.         break;
  41.     case ETHERNET_EVENT_START:
  42.         ESP_LOGI(TAG, "Ethernet Started");
  43.         break;
  44.     case ETHERNET_EVENT_STOP:
  45.         ESP_LOGI(TAG, "Ethernet Stopped");
  46.         break;
  47.     default:
  48.         break;
  49.     }
  50. }
  51.  
  52. /** Event handler for IP_EVENT_ETH_GOT_IP */
  53. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  54.                                  int32_t event_id, void *event_data)
  55. {
  56.     ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  57.     const esp_netif_ip_info_t *ip_info = &event->ip_info;
  58.  
  59.     ESP_LOGI(TAG, "Ethernet Got IP Address");
  60.     ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  61.     ip_tmp = ip_info->ip;
  62.     ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  63.     ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  64.     //  http_init();
  65. }
  66.  
  67. void bsp_ethernet_init(void)
  68. {
  69.     ESP_LOGI(TAG, "%s", __func__);
  70.  
  71.     // Initialize TCP/IP network interface (should be called only once in application)
  72.     ESP_ERROR_CHECK(esp_netif_init());
  73.  
  74.     // Create instance(s) of esp-netif for SPI Ethernet(s)
  75.     esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  76.     esp_netif_config_t cfg_spi = {
  77.         .base = &esp_netif_config,
  78.         .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH};
  79.  
  80.     esp_netif_t *eth_netif_spi = NULL;
  81.  
  82.     esp_netif_config.if_key = IF_KEY_STR;
  83.     esp_netif_config.if_desc = IF_DESC_STR;
  84.     esp_netif_config.route_prio = 30;
  85.     eth_netif_spi = esp_netif_new(&cfg_spi);
  86.  
  87.     //设置静态IP地址
  88.     esp_netif_dhcps_stop(eth_netif_spi);
  89.     char *ip = "192.168.1.168";
  90.     char *gateway = "192.168.1.1";
  91.     char *netmask = "255.255.0.0";
  92.  
  93.     esp_netif_ip_info_t ip_info;
  94.     memset(&ip_info, 0, sizeof(esp_netif_ip_info_t));
  95.     ip_info.ip.addr = esp_ip4addr_aton((const char *)ip);
  96.     ip_info.ip.addr = esp_ip4addr_aton((const char *)gateway);
  97.     ip_info.ip.addr = esp_ip4addr_aton((const char *)netmask);
  98.     if (esp_netif_set_ip_info(eth_netif_spi, &ip_info))
  99.         ESP_LOGI(TAG, "Set static IP OK");
  100.     else
  101.         ESP_LOGI(TAG, "Set static IP error");
  102.  
  103.     // Init MAC and PHY configs to default
  104.     eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
  105.     eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
  106.  
  107.     // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  108.     gpio_install_isr_service(0);
  109.  
  110.     // Init SPI bus
  111.     spi_device_handle_t spi_handle = NULL;
  112.  
  113.     spi_bus_config_t buscfg = {
  114.         .miso_io_num = CONFIG_ETH_SPI_MISO_GPIO,
  115.         .mosi_io_num = CONFIG_ETH_SPI_MOSI_GPIO,
  116.         .sclk_io_num = CONFIG_ETH_SPI_SCLK_GPIO,
  117.         .quadwp_io_num = -1,
  118.         .quadhd_io_num = -1,
  119.     };
  120.  
  121.     ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  122.  
  123.     // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  124.     spi_eth_module_config_t spi_eth_module_config;
  125.     INIT_SPI_ETH_MODULE_CONFIG(spi_eth_module_config, 0);
  126.  
  127.     // Configure SPI interface and Ethernet driver for specific SPI module
  128.     esp_eth_mac_t *mac_spi;
  129.     esp_eth_phy_t *phy_spi;
  130.     esp_eth_handle_t eth_handle_spi = NULL;
  131.  
  132.     spi_device_interface_config_t devcfg = {
  133.         .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
  134.         .address_bits = 8,  // Actually it's the control phase in W5500 SPI frame
  135.         .mode = 0,
  136.         .clock_speed_hz = CONFIG_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  137.         .queue_size = 20};
  138.  
  139.     // Set SPI module Chip Select GPIO
  140.     devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
  141.  
  142.     ESP_ERROR_CHECK(spi_bus_add_device(CONFIG_ETH_SPI_HOST, &devcfg, &spi_handle));
  143.  
  144.     // w5500 ethernet driver is based on spi driver
  145.     eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  146.  
  147.     // Set remaining GPIO numbers and configuration used by the SPI module
  148.     w5500_config.int_gpio_num = spi_eth_module_config.int_gpio;
  149.     phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
  150.     phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
  151.  
  152.     mac_spi = esp_eth_mac_new_w5500(&w5500_config, &mac_config_spi);
  153.     phy_spi = esp_eth_phy_new_w5500(&phy_config_spi);
  154.  
  155.     esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
  156.     ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi));
  157.  
  158.     /* The SPI Ethernet module might not have a burned factory MAC address, we cat to set it manually.
  159.    02:00:00 is a Locally Administered OUI range so should not be used except when testing on a LAN under your control.*/
  160.     ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, (uint8_t[]){0xB0, 0x8E, 0x1A, 0x37, 0x00, 0x01}));
  161.  
  162.     //  attach Ethernet driver to TCP/IP stack
  163.     ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
  164.  
  165.     // Register user defined event handers
  166.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
  167.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
  168.  
  169.     /* start Ethernet driver state machine */
  170.     ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
  171. }
  172.  
我是在这里设置的静态IP地址,运行后发现还是动态IP,是不是方法不对,ESP32如何设置W5500采用静态IP地址工作呢
有关ESP32挂载W5500的使用具体看看哪方面的资料。

Who is online

Users browsing this forum: No registered users and 18 guests