Mongoose LogoMongoose

Ethernet on nRF52840 over USB and BLE radio

The Nordic nRF52840 has no Ethernet port. It does have USB, and USB can present a device as an Ethernet adapter. With Mongoose, an nRF52840 board plugged into a laptop shows up as a network interface, runs a web server with HTTP and HTTPS, and serves a full device dashboard to a browser.

Then we take it one step further. The same board becomes a bridge between USB Ethernet and the nRF52840 radio, and a second board with only a radio for networking gets an IP address over the air, serves web pages and talks to an MQTT broker on the internet.

What you need

The board in the video is a Seeed XIAO nRF52840. Any nRF52840 board with a USB connector should work with small changes to the pin setup. The nRF52840 is a 64 MHz Cortex-M4F with 1 MB of flash and 256 KB of RAM, plus a 2.4 GHz radio that does Bluetooth LE and IEEE 802.15.4.

On the software side you need the ARM GCC toolchain, make, and adafruit-nrfutil for flashing over USB. The build environment guide covers the toolchain.

How Ethernet over USB works

USB defines several device classes, and one of them is CDC-NCM, the Network Control Model. A CDC-NCM device looks to the host like an ordinary Ethernet adapter. macOS and Linux have the driver built in, so nothing needs to be installed on the host.

Mongoose has a native USB device driver for the nRF52840. It implements CDC-NCM for Ethernet and CDC-ACM for a serial console, both on the same cable. It does not use TinyUSB or the Nordic SDK. The USB driver hands Ethernet frames to the Mongoose TCP/IP stack, the same way an Ethernet MAC driver does on an STM32.

Turning it on takes three lines in mongoose_config.h:

#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

A web server on the nRF52840

The USB example lives in tutorials/nordic/xiao-nrf52840/usb. To flash it, double tap the reset button to enter the bootloader, then run:

make flash PORT=/dev/cu.usbmodem1101

Use whatever serial port name your system gives the bootloader. After flashing, the LED starts to blink and the serial console shows the usual Mongoose startup log. The board takes the address 192.168.7.1 and serves DHCP to the host, so the laptop configures itself. Open http://192.168.7.1 in a browser and you get the current tick count and RAM statistics.

The firmware is as small as it gets. The Makefile pulls only the CMSIS headers from ARM and the device headers from Nordic. It compiles a small hand-written HAL that sets up things like GPIO, main.c and Mongoose. No RTOS, no vendor SDK. The main() function starts two listeners and falls into a bare-metal superloop:

struct mg_mgr mgr;
mg_mgr_init(&mgr);  // Also starts the USB driver, see mongoose_config.h
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
mg_http_listen(&mgr, "https://0.0.0.0", http_ev_handler, NULL);

for (;;) {
  mg_mgr_poll(&mgr, 0);
  blink_task();
}

HTTPS works out of the box, because Mongoose has a built-in TLS 1.3 stack. In the video the board uses about 7 KB of RAM serving plain HTTP, and about 20 KB while serving HTTPS.

Adding a full device dashboard

The Mongoose repository has a device dashboard tutorial that runs on a workstation with a plain make. Moving it onto the nRF52840 takes two extra source files in the Makefile, file_data.c with the packed web UI and dashboard.c with its API handlers:

SOURCES += $(MGDIR)/tutorials/device-dashboard/full/dashboard.c
SOURCES += $(MGDIR)/tutorials/device-dashboard/full/file_data.c

In main.c, the two listeners are replaced with mg_dash_init(&mgr), and the loop calls mg_dash_poll(&mgr). Rebuild, flash, refresh the browser, and the board serves the full dashboard over USB. The application code is the same one that runs on Linux, macOS or an STM32 with Ethernet. See the web dashboard guide for how the dashboard is built.

Ethernet over the BLE radio

The nRF52840 radio supports Bluetooth LE and 802.15.4. Mongoose has a radio driver that carries Ethernet frames over it, which we covered for 802.15.4 in Ethernet over 802.15.4 radio. The nRF52840 driver supports both modes. 802.15.4 runs at 250 kbit/s, while the BLE 2 Mbit/s mode is eight times faster, so the video uses BLE.

To be precise about what BLE means here: Mongoose uses the BLE 2 Mbit/s physical layer and sends its own frames. There is no Bluetooth LE protocol stack, pairing or GATT. A radio frame holds up to 251 bytes, so a full Ethernet frame goes out as a short burst of radio frames and gets reassembled on the other side. The stack above never sees the fragments.

The radio mode is one line in mongoose_config.h, set the same way on the bridge and on every node:

#define MG_RADIO MG_RADIO_BLE

The USB to BLE bridge

The bridge firmware is in tutorials/nordic/xiao-nrf52840/usb-to-ble-bridge. It runs two interfaces, USB Ethernet towards the laptop and the radio towards the nodes, and forwards frames between them at layer 2. It runs the radio network too: it admits nodes and gives each one a turn to send, so nodes do not talk over each other.

The node firmware is in tutorials/nordic/xiao-nrf52840/minimal. Its only network link is the radio. Its USB cable is used only for power and the serial log. It runs a web server and an MQTT client that connects to the public HiveMQ broker.

Since the bridge works at layer 2, the node asks for an address with plain DHCP, and the DHCP server has to be somewhere on the LAN. In the video it is macOS Internet Sharing on the USB Ethernet interface. Once sharing is on, the node gets 192.168.2.88, and the laptop can open its web page across the radio. The node uses about 17 KB of RAM.

To test MQTT, open the HiveMQ web client, subscribe to the device's topics with a wildcard, and publish a message to the topic the node listens on. The node replies, and the reply shows up in the web client. That message went from the browser to the HiveMQ cloud, back to the laptop, over USB to the bridge, and over the air to the node.

Plain IPv4, no Thread or Matter

The radio node talks to the internet with standard IPv4 over Ethernet frames. No Thread, no Matter, no 6LoWPAN and no IPv6. Thread and Matter make sense when you need their ecosystem. When all you need is a small device that serves a web page or publishes MQTT messages, they are a lot to bring along. Here the radio is just another network interface, and the same TCP/IP application code runs over USB, over the radio, or over wired Ethernet.

Try it

Clone the Mongoose repository and start with the USB example. If you are new to Mongoose, the getting started guide explains how the library fits into a firmware project. Mongoose is two files, mongoose.c and mongoose.h, and includes the TCP/IP stack, TLS 1.3, the HTTP and WebSocket server, MQTT and OTA updates.