Mongoose LogoMongoose

Ethernet over USB explained

Ethernet over USB lets a microcontroller show up as a network card when you plug it into a laptop. The laptop gets an IP address from the device, you type that device's address into a browser, and its web dashboard opens. There is no driver to install, no app and no terminal. Most new devices do this with the USB NCM class (Network Control Model), which works out of the box on Windows 11, macOS and Linux.

This article follows what happens from the moment the cable goes in to the moment the page loads, then shows it running on three boards: a Seeed XIAO nRF52840, an STM32U5A5 Nucleo and a Raspberry Pi Pico 2 W.

What is Ethernet over USB?

It's a way to carry ordinary Ethernet frames inside USB transfers. Each end of the cable runs a normal TCP/IP stack, and to both stacks the USB cable looks like an Ethernet link. You will also see it called USB Ethernet, USB networking, CDC-NCM, or an Ethernet gadget on Linux.

USB has a few device classes for this job, and the device tells the host which one it speaks. NCM, the Network Control Model, is the newest, and it's the one that works without extra drivers on Windows 11, macOS and Linux. The older ECM (Ethernet Control Model) and Microsoft's RNDIS do the same job. If you are designing a device today, pick NCM, because whoever plugs it in can just go, whatever laptop they have.

What happens when you plug in the cable?

When the cable goes in, the laptop asks the device what it is. This is USB enumeration. The device answers that it is a network device of class NCM.

The laptop then adds a new network interface, just as if you had plugged in a network card. Windows calls it something like "Ethernet 2", macOS something like en7, and Linux usb0, or enx followed by the MAC address. At this point the new interface has no IP address yet.

Why do both sides look the same?

Both the laptop and the device have the same layers: a USB controller, a USB driver on top of it, a TCP/IP stack, and an application.

Laptop and microcontroller each with four layers: application, TCP/IP stack, USB driver and USB controller, connected by a USB cable

On the laptop, the operating system provides the USB driver and the TCP/IP stack, and the application is the browser. On the device, it's the microcontroller's USB controller and the firmware, with a web server as the application. The device's network stack adds an interface with a fixed IP address, for example 192.168.7.1.

How does the laptop get an IP address over USB?

The laptop does what it does on any new network: it sends a DHCP request.

Its USB driver puts the Ethernet frame with the DHCP request into a USB transfer, behind a small NCM header, and sends it down the cable:

NCM transfer from the laptop: Ethernet frame with a DHCP request and an NCM header

The device's USB driver takes the frame out of the transfer and hands it to the TCP/IP stack. A DHCP server built into the firmware answers with an address for the laptop, for example 192.168.7.2. The response travels back the same way:

NCM transfer from the device: NCM header and an Ethernet frame with the DHCP reply 192.168.7.2

The laptop assigns 192.168.7.2 to its new interface, and the link is ready.

One NCM transfer can also carry several Ethernet frames behind one header, which saves USB overhead when there is a lot of traffic:

One NCM transfer carrying an NCM header and two Ethernet frames

What happens after the laptop has an address?

From here it's ordinary networking. The browser speaks HTTP over TCP, and everything becomes Ethernet frames. The USB drivers chop them up and put them back together, and the TCP/IP stacks on both ends never know there was no real Ethernet in between. Everything above the driver works exactly as it does on a wired Ethernet board, which is described in STM32 Ethernet explained.

Can I get a serial console over the same cable?

Yes. One USB device can expose several functions at once, so the same device can also show up as a serial port (USB CDC-ACM). Over one cable you get the device's logs in a serial terminal and its web dashboard in the browser at the same time. This is very handy during development, and it means a product needs only one connector for both service access and debugging.

What can go wrong?

Two things to watch out for.

First, devices with the same fixed address clash if you plug several of them into one laptop. Every one of them says "I'm 192.168.7.1" on a different interface. If you expect people to connect more than one device at a time, give each device its own subnet.

Second, the device's DHCP server must not offer itself as a gateway. If it does, the laptop may send its internet traffic down the USB cable to a microcontroller that can't route it anywhere, and the user's internet connection stops working while the device is plugged in.

What does the firmware need for USB Ethernet?

Four pieces: a USB device driver that speaks NCM, a TCP/IP stack, a DHCP server and a web server.

Three ways to build USB Ethernet firmware: TinyUSB with lwIP, Zephyr with cdc-ncm-ethernet, and Mongoose as one library

You can build that from open source parts, for example TinyUSB for the USB side and lwIP for TCP/IP, and then add the DHCP and web servers yourself. You can use what your RTOS provides: Zephyr, for one, has an NCM Ethernet class called cdc-ncm-ethernet that plugs into its own network stack. You can buy a commercial USB and TCP/IP stack. Or you can use a library that has all four built in. Here we use Mongoose, which has the USB driver, TCP/IP stack, DHCP server and web server in two files, mongoose.c and mongoose.h.

For a broader comparison of embedded network stacks, see the embedded TCP/IP stack comparison.

How do I enable USB Ethernet in Mongoose?

It's a few lines in mongoose_config.h. This is the configuration for the Seeed XIAO nRF52840 from the video:

#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_USB_NRF52 1  // nRF52840 USB controller
#define MG_ENABLE_USB_ETHERNET 1      // USB NCM Ethernet interface
#define MG_ENABLE_USB_SERIAL 1        // USB serial log console

// Static device address; the driver serves DHCP to the USB host. Remove
// these to get an address over DHCP instead, e.g. from Internet Sharing
#define MG_TCPIP_IP MG_IPV4(192, 168, 7, 1)
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)

The first line enables the built-in TCP/IP stack, the second picks the USB controller driver, and the next two turn on the NCM network interface and the serial log console. The fixed address makes the device serve DHCP to the laptop. mg_mgr_init() starts the USB driver, and the usual mg_mgr_poll() loop does the rest, so the application code is the same as for any other Mongoose network interface.

Other boards differ only in the USB driver settings. The RP2040 and RP2350 use MG_ENABLE_DRIVER_USB_RP. The STM32U5 uses MG_ENABLE_DRIVER_USB_DWC2, plus a few lines that select its high-speed USB port.

Does it really work on different microcontrollers?

Here is the Seeed XIAO nRF52840. Plug it in, and a new network interface, en17, shows up on the Mac with the address 192.168.7.2. Open 192.168.7.1 in a browser, and the device answers with its dashboard:

Seeed XIAO nRF52840 serving a web dashboard at 192.168.7.1 over USB Ethernet, with macOS interface en17 at 192.168.7.2

The same application code on an STM32U5A5 Nucleo board gives the same result:

STM32U5A5 Nucleo board serving the same web dashboard over USB Ethernet

The nRF52840 case is covered in more detail, including a bridge from USB to the BLE radio, in Ethernet on nRF52840 over USB and BLE radio.

Why does USB Ethernet matter for industrial devices?

Industrial devices need setup and service access, often in places with no network, or before the device is on any network at all. The usual answers are a serial port with a terminal program, or a vendor app that has to be installed on the technician's laptop. With USB Ethernet and a web UI, you plug in a cable and open a browser.

Here is a Raspberry Pi Pico 2 W with no WiFi configured. Over USB we open its dashboard, enter the network name and password, and it joins. The status line shows the address it got on WiFi, and the serial log in the same cable shows the DHCP exchange:

Raspberry Pi Pico 2 W WiFi settings dashboard opened over USB Ethernet, connected to WiFi with IP 192.168.0.150

The device now has two network interfaces: USB, which is always there, and WiFi, which runs a small HTTP server once it's connected. The example keeps the WiFi settings in RAM. A real product would save them to flash.

How do I try it on my own board?

Every example from this article is in the Mongoose repository. The XIAO nRF52840 one is in tutorials/nordic/xiao-nrf52840/usb, and the STM32 NUCLEO-U5A5ZJ-Q one in tutorials/stm32/nucleo-u5a5zj-q/minimal-usb. For the Raspberry Pi Pico 2 there is tutorials/rp/usb, and the Pico 2 W WiFi setup from the last demo is in tutorials/rp/usb-w.

Build and flash one, plug the board into your laptop, and open http://192.168.7.1. To change what the dashboard shows, follow the web dashboard guide. If you have a question, start a discussion on GitHub.

Summary

Ethernet over USB carries Ethernet frames inside USB transfers, so a microcontroller looks like a network card to the laptop. NCM is the class to use, because Windows 11, macOS and Linux support it without extra drivers. The device keeps a fixed address such as 192.168.7.1 and runs a DHCP server that gives the laptop an address on the same subnet, without offering itself as a gateway. The same cable can carry a serial console as well. On the firmware side you need an NCM USB driver, a TCP/IP stack, a DHCP server and a web server, and Mongoose has all four built in, so USB Ethernet comes down to a few lines of configuration.