---
title: "W5500 10BASE-T compliance test: how to transmit continuous test data (min IPG / random / all-ones)?"
url: "https://maker.wiznet.io/forum/16185"
markdown_url: "https://maker.wiznet.io/forum/16185/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "ciniml"
created: "2026-10-26T20:02:34+09:00"
last_activity: "2026-10-29T19:50:01+09:00"
language: "en"
tags: ["W5500"]
views: 6
replies: 2
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# W5500 10BASE-T compliance test: how to transmit continuous test data (min IPG / random / all-ones)?

## Question

Asked by ciniml on 2026-10-26 in Ethernet Chips.

Hello,

We are running IEEE 802.3 10BASE-T transmitter compliance tests (Keysight N5392A software + N5395 test fixture) on our product that uses the W5500 with an ESP32-S3 host over SPI. We could not find a way to make the W5500 transmit the continuous test signals that some tests expect. We would appreciate advice on how WIZnet did this.

**Our setup**

- PHY forced to 10BASE-T (PHYCFGR, auto-negotiation disabled). A link partner forced to 10M feeds link pulses into the DUT RX pair, because the W5500 does not transmit while the link is down.

- SOCK0 in MACRAW mode. We send 1514-byte broadcast frames with random (or all-ones) payload.

- To shorten the inter-frame gap, we write the next frame into the TX buffer beyond Sn_TX_WR while the current frame is still on the wire. Right after Sn_IR.SEND_OK, we update Sn_TX_WR and issue SEND. SEND and the SEND_OK clear go in one SPI burst (Sn_CR + Sn_IR).

**Result**

- About 754 frames/s, about 92 % line utilization. A frame checker shows an inter-frame gap of 77–121 µs (average about 106 µs), with no FCS errors.

Breakdown on our side:

- SEND → SEND_OK is about 1262 µs, against a wire time of 1220.8 µs.

- The host SPI transactions after SEND_OK take about 57 µs.

- Sn_TX_RD advances only at about the same time as SEND_OK, so we found no safe point to issue the next SEND earlier.

- In the MAU template (eye) test, the idle periods between frames appear as a flat line through the middle of the mask, and the test fails.

**Questions**

1. The "iEthernet W5500 Compliance Test Report 1.0.0 (MAR 2015)" says the results were measured on a PHY IP test chip, with frames sourced from SmartBits. For the W5500 itself, how did WIZnet generate the 10BASE-T transmitter test signals: random data for the template, jitter and TP_IDL tests, and all-ones for the harmonic content test? Is there a test firmware or procedure you can share?

2. Is there any test mode, or any undocumented register, that makes the W5500 PHY transmit a continuous pattern (random / all-ones / PRBS), or transmit without a link partner?

3. In MACRAW mode, is there any way to send back-to-back frames closer to the minimum IPG (9.6 µs)? For example, queuing more than one frame per SEND, or issuing the next SEND before SEND_OK. What is the typical internal latency from SEND to the start of transmission, and from the end of transmission to SEND_OK?

Thank you in advance.

## Replies

### Reply 1 by Grace_Koo, 2026-10-27

Hello,

Thank you for your detailed questions. Please see our answers below.

**1. How the compliance test data was generated**

For the W5500 compliance test, the test data was generated by an MCU.
The required data pattern differs by test item. For example, for the Jitter test and the TP_IDL test, the code selects and transmits a Random, All-One, or All-Zero pattern according to each test mode.
This data was transmitted in MACRAW mode, the same mode you are using.

**2. Test mode for transmitting without a link partner**

The W5500 does not have a separate PHY test mode that outputs an Ethernet pattern by itself without a link partner.
If a specific pattern must be output without a link partner, this function needs to be supported by the external test equipment.
Continuous patterns are also not generated internally by the PHY. They are transmitted by connecting an MCU to the W5500 over SPI, with the MCU continuously supplying the data.

**3. SEND command, SEND_OK, and minimum IPG**

Sending multiple Ethernet frames with a single SEND command is not supported.
SEND_OK is generated after the data delivered from the MCU over SPI has passed through the W5500 TX buffer and has been fully transmitted to the network.
Therefore, we do not recommend issuing the next SEND command before SEND_OK.
Also, since the W5500 PHY itself has no pattern generation function for compliance testing, it cannot generate continuous frames on its own at the minimum IPG of 9.6 µs.
The actual interval between frames depends on the MCU processing time, the SPI transfer speed, and the internal processing time of the W5500, so it is difficult to give a fixed value.
The actual transmission latency and inter-frame gap need to be measured in your MCU–SPI–W5500 environment.

Best regards,

### Reply 2 (accepted solution) by ciniml, 2026-10-29

Thank you for the detailed answers. They match what we found, and we have now passed the 10BASE-T / 100BASE-TX compliance tests with this approach. In case it helps others, here is a minimal single-file ESP32-S3 program (ESP-IDF v5.5.4) that we used (attached: `w5500_txflood.c`).

It drives the W5500 directly (no esp_eth driver): SOCK0 in MACRAW mode with the full 16 KB TX buffer, PHY forced via PHYCFGR, and a serial console to select the PHY mode (`phy 10h|10f|100h|100f|auto`), traffic (`tx random|ones|stop`) and frame length.

**100BASE-TX:** no special firmware is needed. Forcing the PHY to 100M (PHYCFGR, auto-negotiation off) is enough. The IDLE signal is scrambled, so the W5500 continuously sends a random-like MLT-3 signal without any frames or link partner, and that signal can be used for all 100BASE-TX test items.

**10BASE-T:** with this method we could output the patterns required for the tests:

- random data sent back to back, for the TP_IDL, jitter and MAU template tests;

- maximum-length all-ones frames (the alternative pattern allowed by the test procedure), for the harmonic content test.

**Link partner (10BASE-T):** the W5500 does not transmit without link, so the frame-based 10BASE-T tests need a link partner forced to 10M without auto-negotiation. A forced link partner keeps sending link pulses, so the link comes up even through a test fixture that terminates the DUT's TX. A Raspberry Pi 4 Model B worked well for this: `sudo ethtool -s eth0 autoneg off speed 10 duplex half`. When forced, its PHY stays MDI and does not switch pairs, so the link stayed up during the whole measurement. We used a straight cable through the fixture.

Following your recommendation, SEND is always issued only after SEND_OK. We also confirmed that issuing SEND before SEND_OK corrupts the frame on the wire. To get close to the minimum IPG we:

1. write the next frame into the TX buffer after Sn_TX_WR while the current frame is being transmitted, and also update Sn_TX_WR at that time (this did not disturb the frame on the wire; FCS, length and payload were verified by an FPGA receiver);

2. after SEND_OK, write Sn_CR = SEND and clear SEND_OK in Sn_IR with one 2-byte SPI write;

3. use SPI without DMA and start each transfer by writing the SPI peripheral registers directly (the spi_master driver needs 20–40 µs even for a few bytes);

4. poll SEND_OK by re-triggering the same 1-byte read (~1.5 µs per poll);

5. keep the SEND_OK → SEND path in IRAM to avoid flash cache misses.

Measured with an FPGA receiver on 10BASE-T, 1514-byte frames (SPI clock 26.7 MHz, CPU 160 MHz): 812 frames/s; the IPG was between 10.6 and 11.2 µs for all 16,243 frames in 20 seconds (average 10.92 µs), with no FCS or payload errors. The all-ones pattern gave the same IPG. If Sn_TX_WR is written only after SEND_OK (as described in the datasheet), the IPG becomes 12.6–22.4 µs (average 14.05 µs). The IPG values cluster around ~10.7 and ~11.1 µs (0.4 µs = one nibble apart) even when SEND is issued earlier, which suggests the W5500 aligns the start of the next frame internally.

Thanks again for your support.

```cpp
// w5500_txflood.c - Continuous test traffic from a W5500 for Ethernet PHY compliance testing (ESP32-S3)
//
// The W5500 has no PHY test mode, so the test signals are produced like this:
//   - 100BASE-TX (all items):           only force 100M ("phy 100f"). No frames, link partner or special
//     firmware are needed: the IDLE signal is scrambled, so the W5500 sends a random-like MLT-3 signal
//     continuously by itself. Any firmware that writes PHYCFGR to a forced 100M mode is enough.
//   - 10BASE-T link pulses (NLP):       force 10M. No link partner needed.
//   - 10BASE-T template / jitter / TP_IDL / harmonics: force 10M and send frames back to back
//     (random payload, or all-ones for the harmonic test).
// The W5500 only transmits while the link is up. For the frame tests, use a link partner forced to the same
// speed without auto-negotiation. A forced link partner keeps sending link pulses / IDLE without waiting for
// the DUT, so the link comes up even through a fixture that terminates the DUT's TX.
// A Raspberry Pi 4 Model B works as the link partner: "sudo ethtool -s eth0 autoneg off speed 10 duplex half".
// When forced, it stays MDI and does not switch pairs, so the link stays up while frames are sent.
// Match the pair direction with the cable (straight through the fixture in our setup).
//
// With this method we produced, and passed the compliance tests with:
//   - "tx random": random data back to back for the 10BASE-T TP_IDL, jitter and MAU template tests.
//   - "tx ones": maximum-length all-ones frames (the allowed alternative pattern) for the 10BASE-T
//     harmonic content test.
//
// How the frames get close to the minimum IPG (9.6 us). Measured with an FPGA receiver on 10BASE-T,
// 1514-byte frames, SPI clock 26.7 MHz, CPU 160 MHz: 812 frames/s, every IPG 10.6-11.2 us (average 10.92 us,
// 16243 frames in 20 s, no FCS or payload errors; same for all-ones). With PREWRITE_TX_WR 0 the IPG is
// 12.6-22.4 us (average 14.05 us). The plain "write, SEND, wait for SEND_OK" loop gives about 930 us.
//   1. SOCK0 is opened in MACRAW mode with the whole 16 KB TX buffer. While one frame is on the wire, the
//      next frame is written into the TX buffer after Sn_TX_WR, and Sn_TX_WR is also updated (PREWRITE_TX_WR).
//      SEND itself is only issued after SEND_OK. Writing behind the frame on the wire does not disturb it
//      (checked with an FPGA receiver: FCS, length and payload all correct). Issuing SEND before SEND_OK
//      does corrupt the frame on the wire, so this program never does that.
//   2. After SEND_OK, a single 2-byte SPI write sets Sn_CR = SEND and clears SEND_OK in Sn_IR.
//   3. SPI does not go through the spi_master driver per transfer. spi_master spends 20-40 us on a
//      transfer of a few bytes. The bus is initialized by spi_master without DMA, then each transfer
//      (max 64 bytes) is started by writing the SPI peripheral registers directly.
//   4. SEND_OK is polled by re-triggering the same 1-byte read (about 1.5 us per poll). A full read with
//      setup took about 4.5 us, and that detection delay showed up directly in the IPG.
//   5. The path from SEND_OK detection to SEND is in IRAM. When it ran from flash, cache misses delayed SEND
//      by 2-6 us for some frames.
// The W5500 seems to align the start of the next frame to its internal 4-bit timing (0.4 us steps on
// 10BASE-T). The IPG stays at about 10.7 or 11.1 us even when SEND arrives earlier.
//
// Console commands (UART or USB-Serial-JTAG console, 115200, line terminated):
//   phy auto|10h|10f|100h|100f    PHY mode (h = half, f = full duplex; not auto = auto-negotiation off)
//   tx stop|random|ones           traffic (broadcast frames, EtherType 0x88B5, xorshift32 or 0xFF payload)
//   len <64-1514>                 frame length without FCS (the W5500 appends the FCS)
// A status line (link, frames/s) is printed every second. The PHY starts forced to 10M full duplex with
// traffic stopped. If the first command after opening the serial port is not recognized, send an empty
// line first (the first bytes are sometimes lost).
//
// Tested with ESP-IDF v5.5.4, ESP32-S3 at 160 MHz, W5500 on its own SPI bus (SPI3) at 33 MHz requested.
// Uses spi_ll / spi_dev_t directly, so other IDF versions or chips may need small changes.
// Build: a normal ESP-IDF project with this file as the only source in main/
//        (main/CMakeLists.txt: idf_component_register(SRCS "w5500_txflood.c")).

#include <inttypes.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_cpu.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_task_wdt.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/spi_ll.h"

#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#include "driver/usb_serial_jtag.h"
#include "driver/usb_serial_jtag_vfs.h"
#else
#include "driver/uart.h"
#include "driver/uart_vfs.h"
#endif

// ==== Board configuration (change for your board) ====================================================
#ifndef W5500_SPI_HOST
#define W5500_SPI_HOST SPI3_HOST  // the W5500 must be the only device on this bus (the bus is held forever)
#endif
#ifndef W5500_PIN_SCLK
#define W5500_PIN_SCLK 12
#endif
#ifndef W5500_PIN_MOSI
#define W5500_PIN_MOSI 11
#endif
#ifndef W5500_PIN_MISO
#define W5500_PIN_MISO 13
#endif
#ifndef W5500_PIN_CS
#define W5500_PIN_CS 10
#endif
#ifndef W5500_PIN_RST
#define W5500_PIN_RST 9  // -1 if not connected
#endif
#ifndef W5500_SPI_CLOCK_HZ
#define W5500_SPI_CLOCK_HZ (33 * 1000 * 1000)
#endif

// 1 = write Sn_TX_WR while the previous frame is still on the wire (shortest IPG).
// 0 = write Sn_TX_WR after SEND_OK, as the datasheet describes (one more SPI write in the gap).
#ifndef PREWRITE_TX_WR
#define PREWRITE_TX_WR 1
#endif

static const char* TAG = "w5500-txflood";

// ==== W5500 registers ================================================================================
// SPI frame: 16-bit offset, 8-bit control (BSB[4:0] << 3 | RWB << 2 | OM = 00 variable length), data.
#define BSB_COMMON 0x00
#define BSB_SOCK_REG(n) ((uint8_t)((n) * 4 + 1))
#define BSB_SOCK_TX(n) ((uint8_t)((n) * 4 + 2))
#define BSB_SOCK_RX(n) ((uint8_t)((n) * 4 + 3))
#define CTRL_WRITE 0x04

#define REG_MR 0x0000
#define REG_SHAR 0x0009
#define REG_PHYCFGR 0x002E
#define REG_VERSIONR 0x0039
#define SN_MR 0x0000
#define SN_CR 0x0001
#define SN_IR 0x0002
#define SN_SR 0x0003
#define SN_RXBUF_SIZE 0x001E
#define SN_TXBUF_SIZE 0x001F
#define SN_TX_WR 0x0024

#define SN_MR_MACRAW 0x04
#define SN_MR_MFEN 0x80
#define SN_CR_OPEN 0x01
#define SN_CR_CLOSE 0x10
#define SN_CR_SEND 0x20
#define SN_IR_SEND_OK 0x10
#define SN_SR_MACRAW 0x42

#define CYCLES_PER_US CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ
#define SEND_DONE_TIMEOUT_US 20000  // a 1514-byte frame takes about 1.23 ms on 10BASE-T

// ==== Direct SPI =====================================================================================
static spi_dev_t* s_hw;
static portMUX_TYPE s_spi_mux = portMUX_INITIALIZER_UNLOCKED;
// Register values that do not change between transfers, captured on the first direct transfer.
// Reading peripheral registers is slow, so later transfers only write them.
static bool s_regs_valid;
static uint32_t s_user_base;   // user without usr_mosi / usr_miso
static uint32_t s_user2_base;  // user2 without usr_command_value
static uint32_t s_cmd_base;    // cmd without update / usr
// The read currently set up for spi_poll(). Any other transfer invalidates it.
static bool s_poll_armed;
static uint16_t s_poll_offset;
static uint8_t s_poll_ctrl;

// One transfer of up to 64 bytes. spi_master must have done one transfer on this device first, so clock,
// mode and CS are already configured. Call inside s_spi_mux.
static IRAM_ATTR void spi_xfer(uint16_t offset, uint8_t ctrl, void* data, uint32_t len) {
    spi_dev_t* hw = s_hw;
    const bool write = (ctrl & CTRL_WRITE) != 0;
    const uint32_t bits = 8 * len;
    s_poll_armed = false;
    if (!s_regs_valid) {
        spi_ll_set_command_bitlen(hw, 16);
        spi_ll_set_addr_bitlen(hw, 8);
        spi_ll_set_dummy(hw, 0);
        s_user_base = hw->user.val & ~(SPI_USR_MOSI | SPI_USR_MISO);
        s_user2_base = hw->user2.val & ~SPI_USR_COMMAND_VALUE;
        s_cmd_base = hw->cmd.val & ~(SPI_UPDATE | SPI_USR);
        s_regs_valid = true;
    }
    hw->dma_int_clr.val = SPI_TRANS_DONE_INT_CLR;
    hw->ms_dlen.val = bits - 1;
    hw->user2.val = s_user2_base | __builtin_bswap16(offset);  // command phase is sent low byte first
    hw->addr = (uint32_t)ctrl << 24;                            // 8-bit address phase comes from the MSB
    hw->user.val = s_user_base | SPI_USR_MOSI | (write ? 0 : SPI_USR_MISO);
    if (write) {
        uint8_t buf[SOC_SPI_MAXIMUM_BUFFER_SIZE];  // spi_ll_write_buffer reads whole 32-bit words
        memcpy(buf, data, len);
        spi_ll_write_buffer(hw, buf, bits);
    }
    hw->cmd.val = s_cmd_base | SPI_UPDATE;
    while (hw->cmd.val & SPI_UPDATE) {
    }
    hw->cmd.val = s_cmd_base | SPI_USR;
    while (!spi_ll_usr_is_done(hw)) {
    }
    if (!write) {
        spi_ll_read_buffer(hw, data, bits);
    }
}

// Read or write any length. The offset advances per 64-byte chunk (the W5500 wraps buffer offsets itself).
static IRAM_ATTR void w5500_access(uint16_t offset, uint8_t ctrl, void* data, uint32_t len) {
    uint8_t* p = data;
    uint32_t done = 0;
    while (done < len) {
        const uint32_t n = (len - done < SOC_SPI_MAXIMUM_BUFFER_SIZE) ? len - done : SOC_SPI_MAXIMUM_BUFFER_SIZE;
        portENTER_CRITICAL(&s_spi_mux);
        spi_xfer((uint16_t)(offset + done), ctrl, p + done, n);
        portEXIT_CRITICAL(&s_spi_mux);
        done += n;
    }
}

static IRAM_ATTR void w5500_read(uint8_t bsb, uint16_t offset, void* data, uint32_t len) {
    w5500_access(offset, (uint8_t)(bsb << 3), data, len);
}

static IRAM_ATTR void w5500_write(uint8_t bsb, uint16_t offset, const void* data, uint32_t len) {
    w5500_access(offset, (uint8_t)(bsb << 3) | CTRL_WRITE, (void*)data, len);
}

static uint8_t w5500_read8(uint8_t bsb, uint16_t offset) {
    uint8_t v = 0;
    w5500_read(bsb, offset, &v, 1);
    return v;
}

static void w5500_write8(uint8_t bsb, uint16_t offset, uint8_t v) {
    w5500_write(bsb, offset, &v, 1);
}

// Poll a 1-byte register until (value & mask) != 0. Only the first poll sets the transfer up; later polls
// just restart it and read the result. Returns the cycle count right after the read that saw the bit,
// or 0 on timeout.
static IRAM_ATTR uint32_t w5500_poll(uint8_t bsb, uint16_t offset, uint8_t mask, uint32_t start_cycles,
                                     uint32_t timeout_cycles) {
    const uint8_t ctrl = (uint8_t)(bsb << 3);
    spi_dev_t* hw = s_hw;
    while (true) {
        uint8_t value = 0;
        portENTER_CRITICAL(&s_spi_mux);
        if (!s_poll_armed || s_poll_offset != offset || s_poll_ctrl != ctrl) {
            spi_xfer(offset, ctrl, &value, 1);
            s_poll_armed = true;
            s_poll_offset = offset;
            s_poll_ctrl = ctrl;
        } else {
            hw->dma_int_clr.val = SPI_TRANS_DONE_INT_CLR;
            hw->cmd.val = s_cmd_base | SPI_USR;
            while (!spi_ll_usr_is_done(hw)) {
            }
            spi_ll_read_buffer(hw, &value, 8);
        }
        portEXIT_CRITICAL(&s_spi_mux);
        const uint32_t now = esp_cpu_get_cycle_count();
        if (value & mask) {
            return now != 0 ? now : 1;
        }
        if (now - start_cycles > timeout_cycles) {
            return 0;
        }
    }
}

// ==== W5500 setup ====================================================================================
enum { PHY_AUTO, PHY_10H, PHY_10F, PHY_100H, PHY_100F, PHY_MODES };
static const char* const PHY_NAMES[PHY_MODES] = {"auto", "10h", "10f", "100h", "100f"};
static const uint8_t PHY_OPMDC[PHY_MODES] = {7, 0, 1, 2, 3};  // PHYCFGR[5:3]

static void spi_init(void) {
    if (W5500_PIN_RST >= 0) {
        gpio_reset_pin(W5500_PIN_RST);
        gpio_set_direction(W5500_PIN_RST, GPIO_MODE_OUTPUT);
        gpio_set_level(W5500_PIN_RST, 0);
        vTaskDelay(pdMS_TO_TICKS(2));
        gpio_set_level(W5500_PIN_RST, 1);
        vTaskDelay(pdMS_TO_TICKS(50));
    }
    const spi_bus_config_t bus = {
        .sclk_io_num = W5500_PIN_SCLK,
        .mosi_io_num = W5500_PIN_MOSI,
        .miso_io_num = W5500_PIN_MISO,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
    };
    ESP_ERROR_CHECK(spi_bus_initialize(W5500_SPI_HOST, &bus, SPI_DMA_DISABLED));
    const spi_device_interface_config_t dev = {
        .command_bits = 16,
        .address_bits = 8,
        .mode = 0,
        .clock_speed_hz = W5500_SPI_CLOCK_HZ,
        .spics_io_num = W5500_PIN_CS,
        .queue_size = 1,
    };
    spi_device_handle_t handle;
    ESP_ERROR_CHECK(spi_bus_add_device(W5500_SPI_HOST, &dev, &handle));
    ESP_ERROR_CHECK(spi_device_acquire_bus(handle, portMAX_DELAY));  // never released: spi_master stays off the bus
    int khz = 0;
    spi_device_get_actual_freq(handle, &khz);

    // One transfer through spi_master applies clock, mode and CS to the peripheral. Later transfers are direct.
    spi_transaction_t t = {
        .flags = SPI_TRANS_USE_RXDATA,
        .cmd = REG_VERSIONR,
        .addr = BSB_COMMON << 3,
        .length = 8,
    };
    ESP_ERROR_CHECK(spi_device_polling_transmit(handle, &t));
    s_hw = SPI_LL_GET_HW(W5500_SPI_HOST);
    ESP_LOGI(TAG, "W5500 VERSIONR=0x%02x (expected 0x04), SPI %d kHz", t.rx_data[0], khz);
}

static void sock0_open_macraw(void) {
    w5500_write8(BSB_SOCK_REG(0), SN_CR, SN_CR_CLOSE);
    while (w5500_read8(BSB_SOCK_REG(0), SN_CR) != 0) {
    }
    w5500_write8(BSB_SOCK_REG(0), SN_MR, SN_MR_MACRAW | SN_MR_MFEN);
    w5500_write8(BSB_SOCK_REG(0), SN_CR, SN_CR_OPEN);
    while (w5500_read8(BSB_SOCK_REG(0), SN_CR) != 0) {
    }
    const uint8_t sr = w5500_read8(BSB_SOCK_REG(0), SN_SR);
    if (sr != SN_SR_MACRAW) {
        ESP_LOGE(TAG, "SOCK0 did not open in MACRAW mode (Sn_SR=0x%02x)", sr);
    }
}

static void w5500_init(const uint8_t mac[6]) {
    w5500_write8(BSB_COMMON, REG_MR, 0x80);  // software reset
    while (w5500_read8(BSB_COMMON, REG_MR) & 0x80) {
    }
    w5500_write(BSB_COMMON, REG_SHAR, mac, 6);
    // All 16 KB of TX and RX buffer to SOCK0 (two max-size frames must fit in the TX buffer).
    for (int n = 1; n < 8; n++) {
        w5500_write8(BSB_SOCK_REG(n), SN_TXBUF_SIZE, 0);
        w5500_write8(BSB_SOCK_REG(n), SN_RXBUF_SIZE, 0);
    }
    w5500_write8(BSB_SOCK_REG(0), SN_TXBUF_SIZE, 16);
    w5500_write8(BSB_SOCK_REG(0), SN_RXBUF_SIZE, 16);
}

// PHYCFGR: RST=bit7 (0 = reset), OPMD=bit6 (1 = use OPMDC), OPMDC=bit5:3. Then reopen SOCK0 so the
// TX pointers start clean.
static void phy_apply(int mode) {
    const uint8_t cfg = (uint8_t)(0x40 | (PHY_OPMDC[mode] << 3));
    w5500_write8(BSB_COMMON, REG_PHYCFGR, cfg);
    vTaskDelay(pdMS_TO_TICKS(10));
    w5500_write8(BSB_COMMON, REG_PHYCFGR, cfg | 0x80);
    sock0_open_macraw();
    ESP_LOGI(TAG, "phy %s", PHY_NAMES[mode]);
}

// ==== Traffic ========================================================================================
enum { TX_STOP, TX_RANDOM, TX_ONES };
static const char* const TX_NAMES[] = {"stop", "random", "ones"};

static atomic_int s_phy_request = PHY_10F;  // applied by tx_task; -1 = nothing pending
static atomic_int s_tx_mode = TX_STOP;
static atomic_int s_frame_len = 1514;
static atomic_uint s_tx_total;
static atomic_uint s_phycfgr;  // last PHYCFGR value read by tx_task

static uint32_t s_prng = 0x12345678;  // any non-zero seed
static void fill_payload(uint8_t* p, size_t len, int mode) {
    if (mode == TX_ONES) {
        memset(p, 0xFF, len);
        return;
    }
    for (size_t i = 0; i < len; i += 4) {
        uint32_t x = s_prng;
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        s_prng = x;
        memcpy(p + i, &x, (len - i < 4) ? len - i : 4);
    }
}

static IRAM_ATTR void write_tx_wr(uint16_t wr) {
    const uint8_t be[2] = {(uint8_t)(wr >> 8), (uint8_t)wr};
    w5500_write(BSB_SOCK_REG(0), SN_TX_WR, be, 2);
}

// Only this task touches the W5500 after start-up. It busy-waits for SEND_OK, so it is pinned to core 1
// and the core 1 idle task is removed from the task watchdog.
static IRAM_ATTR void tx_task(void* arg) {
    const uint8_t* mac = arg;
    static uint8_t frame[1514];
    int built_mode = -1;
    int built_len = 0;
    bool synced = false;     // tx_wr matches the chip
    bool in_flight = false;  // a frame was sent and its SEND_OK has not been seen yet
    uint16_t tx_wr = 0;
    uint32_t sent_at = 0;
    uint32_t count = 0;
    int64_t last_warn = 0;

    while (true) {
        const int phy = atomic_exchange(&s_phy_request, -1);
        const int mode = atomic_load(&s_tx_mode);
        if (phy >= 0 || mode == TX_STOP) {
            if (in_flight) {
                (void)w5500_poll(BSB_SOCK_REG(0), SN_IR, SN_IR_SEND_OK, sent_at,
                                 SEND_DONE_TIMEOUT_US * CYCLES_PER_US);
                in_flight = false;
            }
            if (phy >= 0) {
                phy_apply(phy);
                synced = false;
            }
            if (mode == TX_STOP) {
                atomic_store(&s_phycfgr, w5500_read8(BSB_COMMON, REG_PHYCFGR));
                vTaskDelay(pdMS_TO_TICKS(50));
                continue;
            }
        }

        if (!synced) {
            const uint8_t phycfgr = w5500_read8(BSB_COMMON, REG_PHYCFGR);
            atomic_store(&s_phycfgr, phycfgr);
            if (!(phycfgr & 0x01)) {
                vTaskDelay(pdMS_TO_TICKS(100));  // the W5500 does not transmit without link
                continue;
            }
            uint8_t be[2];
            w5500_read(BSB_SOCK_REG(0), SN_TX_WR, be, 2);
            tx_wr = (uint16_t)((be[0] << 8) | be[1]);
            w5500_write8(BSB_SOCK_REG(0), SN_IR, SN_IR_SEND_OK);
            synced = true;
        }

        // Prepare the next frame while the previous one is on the wire.
        const int len = atomic_load(&s_frame_len);
        if (mode != built_mode || len != built_len) {
            memset(frame, 0xFF, 6);  // broadcast
            memcpy(frame + 6, mac, 6);
            frame[12] = 0x88;  // IEEE 802 Local Experimental EtherType 1
            frame[13] = 0xB5;
            fill_payload(frame + 14, len - 14, mode);
            built_mode = mode;
            built_len = len;
        } else if (mode == TX_RANDOM) {
            fill_payload(frame + 14, len - 14, mode);
        }
        w5500_write(BSB_SOCK_TX(0), tx_wr, frame, len);
        const uint16_t next_wr = (uint16_t)(tx_wr + len);
        if (PREWRITE_TX_WR) {
            write_tx_wr(next_wr);
        }
        if ((count & 255) == 0) {
            atomic_store(&s_phycfgr, w5500_read8(BSB_COMMON, REG_PHYCFGR));
        }

        if (in_flight) {
            if (w5500_poll(BSB_SOCK_REG(0), SN_IR, SN_IR_SEND_OK, sent_at, SEND_DONE_TIMEOUT_US * CYCLES_PER_US) ==
                0) {
                const int64_t now = esp_timer_get_time();
                if (now - last_warn > 5000000) {
                    ESP_LOGW(TAG, "SEND_OK timeout (link down?)");
                    last_warn = now;
                }
                synced = false;
                in_flight = false;
                continue;
            }
        }
        if (!PREWRITE_TX_WR) {
            write_tx_wr(next_wr);
        }
        // Sn_CR and Sn_IR are adjacent: SEND and clearing the previous SEND_OK in one SPI write.
        const uint8_t cr_ir[2] = {SN_CR_SEND, SN_IR_SEND_OK};
        w5500_write(BSB_SOCK_REG(0), SN_CR, cr_ir, 2);
        sent_at = esp_cpu_get_cycle_count();
        tx_wr = next_wr;
        in_flight = true;
        count++;
        atomic_fetch_add(&s_tx_total, 1);
    }
}

// ==== Console ========================================================================================
static void console_init(void) {
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
    usb_serial_jtag_driver_config_t cfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&cfg));
    usb_serial_jtag_vfs_use_driver();
#else
    ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0));
    uart_vfs_dev_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
#endif
    setvbuf(stdin, NULL, _IONBF, 0);
}

static int find_name(const char* const* names, int n, const char* s) {
    for (int i = 0; i < n; i++) {
        if (strcmp(names[i], s) == 0) {
            return i;
        }
    }
    return -1;
}

static void console_task(void* arg) {
    char line[64];
    while (fgets(line, sizeof(line), stdin) != NULL) {
        char cmd[16] = "";
        char val[16] = "";
        if (sscanf(line, "%15s %15s", cmd, val) < 1) {
            continue;
        }
        int i = -1;
        if (strcmp(cmd, "phy") == 0 && (i = find_name(PHY_NAMES, PHY_MODES, val)) >= 0) {
            atomic_store(&s_phy_request, i);
        } else if (strcmp(cmd, "tx") == 0 && (i = find_name(TX_NAMES, 3, val)) >= 0) {
            atomic_store(&s_tx_mode, i);
        } else if (strcmp(cmd, "len") == 0 && sscanf(val, "%d", &i) == 1 && i >= 64 && i <= 1514) {
            atomic_store(&s_frame_len, i);
        } else {
            printf("commands: phy auto|10h|10f|100h|100f, tx stop|random|ones, len <64-1514>\n");
        }
    }
    vTaskDelete(NULL);
}

static void status_task(void* arg) {
    uint32_t last = atomic_load(&s_tx_total);
    while (true) {
        vTaskDelay(pdMS_TO_TICKS(1000));
        const uint32_t total = atomic_load(&s_tx_total);
        const uint32_t p = atomic_load(&s_phycfgr);
        printf("link %s %s %s | tx %s %d B | %" PRIu32 " frames/s, total %" PRIu32 "\n", (p & 1) ? "up" : "down",
               (p & 2) ? "100M" : "10M", (p & 4) ? "full" : "half", TX_NAMES[atomic_load(&s_tx_mode)],
               atomic_load(&s_frame_len), total - last, total);
        last = total;
    }
}

void app_main(void) {
    static uint8_t mac[6];
    esp_read_mac(mac, ESP_MAC_ETH);
    spi_init();
    w5500_init(mac);
    // The busy-waiting tx_task would starve the core 1 idle task: watch only the core 0 idle task.
    const esp_task_wdt_config_t wdt = {
        .timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000,
        .idle_core_mask = 1 << 0,
        .trigger_panic = false,
    };
    (void)esp_task_wdt_reconfigure(&wdt);
    console_init();
    xTaskCreatePinnedToCore(tx_task, "tx", 4096, mac, configMAX_PRIORITIES - 2, NULL, 1);
    xTaskCreatePinnedToCore(console_task, "console", 4096, NULL, 5, NULL, 0);
    xTaskCreatePinnedToCore(status_task, "status", 3072, NULL, 4, NULL, 0);
}
```

---

Source: https://maker.wiznet.io/forum/16185
