English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X Ethernet

APIs and example programs related to Ethernet of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1
Note
The M5CoreP4X board variant already defines the Ethernet PHY, SMI, and RMII pins of CoreP4X in pins_arduino.h.

CoreP4X has an onboard IP101GRI 10/100M Ethernet PHY. The RMII signals are connected as follows:

Signal ESP32-P4 Pin
RMII MDC G31
RMII MDIO G36
RMII CLK G32
RMII RXDV G28
RMII RXD0 G29
RMII RXD1 G30
RMII TXEN G33
RMII TXD0 G41
RMII TXD1 G35
PHY RST G52

The RESET_N of the IP101GRI on CoreP4X is pulled down by default. Therefore, before calling ETH.begin(), drive G52 high and wait for the PHY to output the 50 MHz RMII clock.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#include <M5Unified.h>
#include <ETH.h>

enum EthState : uint8_t {
  ETH_STARTING,
  ETH_CONNECTED,
  ETH_GOT_IP,
  ETH_DISCONNECTED,
};

volatile bool eth_connected = false;
volatile EthState ethState = ETH_STARTING;
M5Canvas canvas(&M5.Display);

static void drawStatus(const char* title, const char* detail, uint16_t color)
{
  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);
  canvas.setTextColor(color, TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold24pt7b);
  canvas.drawString(title, 240, 190);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold12pt7b);
  canvas.drawString(detail, 240, 264);
  canvas.pushSprite(0, 0);
}

static void drawNetworkState(void)
{
  char detail[96];

  switch (ethState) {
    case ETH_CONNECTED:
      drawStatus("Link Connected", "Waiting for DHCP", TFT_CYAN);
      break;
    case ETH_GOT_IP:
      snprintf(detail, sizeof(detail), "IP: %s", ETH.localIP().toString().c_str());
      drawStatus("Ethernet Ready", detail, TFT_GREEN);
      break;
    case ETH_DISCONNECTED:
      drawStatus("Ethernet Offline", "Check cable and PHY", TFT_RED);
      break;
    case ETH_STARTING:
    default:
      drawStatus("Ethernet", "Starting PHY...", TFT_YELLOW);
      break;
  }
}

void onEvent(arduino_event_id_t event)
{
  switch (event) {
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      ETH.setHostname("corep4x");
      ethState = ETH_STARTING;
      break;
    case ARDUINO_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      ethState = ETH_CONNECTED;
      break;
    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.println("ETH Got IP");
      Serial.println(ETH);
      eth_connected = true;
      ethState = ETH_GOT_IP;
      break;
    case ARDUINO_EVENT_ETH_LOST_IP:
      Serial.println("ETH Lost IP");
      eth_connected = false;
      ethState = ETH_DISCONNECTED;
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      eth_connected = false;
      ethState = ETH_DISCONNECTED;
      break;
    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      eth_connected = false;
      ethState = ETH_DISCONNECTED;
      break;
    default:
      break;
  }
}

void testClient(const char* host, uint16_t port)
{
  Serial.print("\nconnecting to ");
  Serial.println(host);
  drawStatus("Network Test", host, TFT_CYAN);

  NetworkClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    drawStatus("Connect Failed", host, TFT_RED);
    return;
  }

  client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
  while (client.connected() && !client.available()) {
    delay(1);
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("closing connection\n");
  client.stop();
  drawStatus("HTTP Response", "See Serial Monitor", TFT_GREEN);
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());
  drawStatus("Ethernet", "Starting PHY...", TFT_YELLOW);

  Network.onEvent(onEvent);

  pinMode(ETH_PHY_POWER, OUTPUT);
  digitalWrite(ETH_PHY_POWER, LOW);
  delay(10);
  digitalWrite(ETH_PHY_POWER, HIGH);
  delay(50);

  ETH.begin(ETH_PHY_TYPE,
            ETH_PHY_ADDR,
            ETH_PHY_MDC,
            ETH_PHY_MDIO,
            ETH_PHY_POWER,
            ETH_CLK_MODE);
}

void loop()
{
  static EthState lastState = ETH_STARTING;
  static uint32_t lastTest = 0;
  const EthState state = ethState;

  if (state != lastState) {
    lastState = state;
    drawNetworkState();
    if (state == ETH_GOT_IP) {
      lastTest = millis();
    }
  }

  if (eth_connected && millis() - lastTest >= 10000) {
    lastTest = millis();
    testClient("baidu.com", 80);
  }

  delay(200);
}

After CoreP4X powers on, the screen displays PHY startup, link connection, the IP address obtained through DHCP, and network test results; the serial port also outputs detailed network information. The program accesses baidu.com every 10 seconds. If the connection fails, Connect Failed is displayed and connection failed is output.

Serial output example:

  • Network connection succeeded
ETH Started
ETH Connected
ETH Got IP
*eth0: <UP,100M,FULL_DUPLEX,AUTO,ADDR:0x1> (DHCPC,GARP,IP_MOD) PRIO: 50
      ether 80:F1:B2:DC:59:54
      inet 10.42.0.212 netmask 255.255.255.0 broadcast 10.42.0.255
      gateway 10.42.0.1 dns 10.42.0.1


connecting to baidu.com
HTTP/1.1 301 Moved Permanently
Location: https://www.baidu.com/
Date: Sat, 28 Nov 2026 03:14:00 GMT
Content-Length: 57
Content-Type: text/html; charset=utf-8

<a href="https://www.baidu.com/">Moved Permanently</a>.

closing connection
  • Network connection failed
ETH Started
ETH Connected
ETH Got IP
*eth0: <UP,100M,FULL_DUPLEX,AUTO,ADDR:0x1> (DHCPC,GARP,IP_MOD) PRIO: 50
      ether 80:F1:B2:DC:59:54
      inet 10.42.0.212 netmask 255.255.255.0 broadcast 10.42.0.255
      gateway 10.42.0.1 dns 10.42.0.1


connecting to baidu.com
connection failed
Page Tools
PDF
On This Page