MCH2022 badge

Previous: mch2022_coproc · Up: Applications · Next: Reference

The MCH2022 badge carries an ESP32, an iCE40UP5K FPGA and an RP2040. This page is the design of what runs on the two chips beside the ESP32, the RP2040’s firmware and the FPGA’s gateware, and of the links that carry the ESP32’s samples to the PC. It follows mch2022_coproc in Applications.

What each chip does on the board, the RP2040’s flash layout and the ways in and out of its bootloader are on the board page, MCH2022 badge (RP2040 coprocessor).

Usage is in the READMEs of mch2022_coproc, osmosdr_serial and micropython.

The RP2040 coprocessor

Design note, 2026-11-03/04. mch2022_coproc replaces the stock firmware of the badge’s RP2040 (boards/badgeteam/mch2022_rp2040), so that osmosdr_serial is no longer limited by the link. The app’s README says how to build and install it; MCH2022 badge (RP2040 coprocessor) has the ways back to the stock firmware.

Why replace the stock firmware

  • The ESP32 reaches USB through the RP2040’s UART bridge.

  • The stock firmware polls both UARTs from one main loop, without DMA, with their 32-byte FIFOs and 256-byte USB buffers.

  • 2 Mbaud worked, at 200 kB/s; 3 Mbaud got no answer.

  • At 2 MS/s the ESP32’s library makes some 676 kS/s on average (2044 samples a 3.025 ms burst, Burst timing on the ESP32), 1.35 MB/s even as 8-bit pairs.

Compatibility with the stock firmware

Everything that talks to the stock firmware goes on working, whatever it runs on. The interfaces kept are listed in mch2022_coproc; this section has their details.

USB descriptors

  • The strings “Badge.team” and “MCH2022 badge”, and the flash’s unique ID as serial number, so the /dev/serial/by-id names stay.

  • The configuration descriptor is the stock one byte for byte: CDC ACM on interfaces 0 to 3 with its endpoints, WebUSB on 4 and 5.

  • It differs in the new interface 6, the string indexes that interface shifts, and 64 instead of 32 bytes on the WebUSB endpoints.

  • The binary object store is the same.

  • The Microsoft OS 2.0 set has a third function, the first two unchanged.

WebUSB requests

The firmware answers the WebUSB class requests 0x22 to 0x27 and stalls the rest.

Reset by DTR and RTS

  • The stock sequences reset the ESP32, in application or download mode, with the reset lock.

  • west flash connects in 3.6 s (20 of 20) and writes as fast as with the stock firmware.

  • COPROC_RESET_CIRCUIT is the usual two-transistor circuit instead: esptool’s first sequence works and it connects in 1.4 s (20 of 20).

I2C registers

  • The I2C coprocessor keeps all its registers.

  • Register 0 still reads 0x0C: the launcher replaces anything below that and reads MSC registers above it.

  • Registers 100 to 103 read 0x5A, the API’s version and feature bits.

Stock bugs not repeated

  • IO0 stayed low after a download reset until the next normal one (it is also the codec’s clock).

  • GPIO_IN always read 0.

  • The backlight’s register read 0 until written.

  • A WS2812 trigger with the mode off hung with more than four LEDs.

The RP2040 firmware

Zephyr on one core, from the flash behind the badge’s bootloader. What it adds to the stock firmware (include/esp_sdr/mch2022_coproc.h):

  • Both UARTs on DMA, so nothing is lost at the ESP32’s 5 Mbaud. The stock firmware polls them, and osmosdr_serial stopped at 2 Mbaud (UARTs on DMA).

  • Interface 6, a bulk pipe to the ESP32’s UART with requests for the chip itself: counters, log, resets, the ROM’s USB bootloader (USB bulk engine).

  • The FPGA’s fast line into that pipe, 15 Mbaud from the FPGA with the link bitstream (The fast line).

  • Registers 100 to 103, by which the ESP32 can tell it from the stock firmware (I2C registers).

UARTs on DMA

  • Zephyr’s PL011 driver sets up the line; uart_dma.c owns DMACR and DR through the hal’s DMA calls.

  • Zephyr’s DMA driver has no ring mode, and its devicetree node stays off so that it does not take channels.

  • Receive runs for good into a ring: 16 KiB for the ESP32, 4 KiB for the FPGA. The channel’s remaining count says how far it got.

  • Transmit takes what lies together in a 4 KiB ring; its end interrupt starts the next piece.

USB bulk engine

usb_vendor.c and usb_dev.c make every function one class on one bulk engine. A thread (bridge.c) moves the data, woken by completions, by the UARTs’ DMA and every millisecond for what the UARTs bring. What follows was learnt on the badge.

CDC buffers

  • Zephyr’s CDC ACM class sends one packet per buffer and goes through a thread for each.

  • That misses the frame nearly every time: 69 kB/s where the UART brings 200.

  • The CDC ports here send buffers of 1 KiB, re-armed per packet in the interrupt.

OUT transfers

  • The controller ends an OUT transfer at a short packet or a full buffer.

  • Linux’s cdc-acm and the WebUSB tools send no zero-length packet after a write of whole packets. With larger buffers the end of such a write waited for more to come.

  • So their OUT buffers are one packet each: with a thread between packets, 150 to 190 kB/s, enough for esptool’s 92.

  • Interface 6’s host ends every write with a short packet and gets 1 KiB buffers: 448 kB/s at 5 Mbaud.

IN double buffering

An IN endpoint with one buffer is empty when the host’s next token comes before the interrupt has refilled it. The host gets a NAK and comes back much later.

IN endpoint

Throughput

One buffer

520 to 660 kB/s

Two buffers, an interrupt per pair (TinyUSB’s way: empty after each pair)

553 kB/s

Two buffers, an interrupt for each (CONFIG_UDC_RPI_PICO_DOUBLE_BUFFER_BULK_IN, in our Zephyr)

928 kB/s

The bridge thread

  • The thread moves what is there when it looks and then looks again, rather than until a ring is empty.

  • Bytes can come as fast as it takes them, in buffers of a few bytes: at 2 Mbaud it once stayed in that loop for a whole 10 s transfer, and the watchdog rebooted the badge.

  • A closed CDC port is not told apart from a slow one on USB: what nobody reads for 200 ms is dropped, so the next reader gets no old news.

I2C registers and the FPGA reset

regmap.c and i2c_regs.c serve the I2C coprocessor:

  • Bytes come and go in the driver’s interrupt.

  • The effects of writes wait in a work item until the stop, in ascending register order, as in the stock firmware.

  • CRESET moves at the stop itself.

  • CDONE is read when the ESP32 asks, which it does right after the bitstream’s last byte.

The DesignWare race

  • The interrupt can be 100 us late, with code running from flash; where that comes from was not found.

  • That is three bytes at 400 kHz, and the ESP32’s load writes, reads and writes with little time between.

  • The DesignWare driver then took one transaction’s pointer for the last one’s data, in 5 to 15 % of the boots, which left the FPGA unloaded.

  • Our Zephyr tells transactions apart by the flag on a first byte.

SELECT, boot and watchdog

SELECT

SELECT (qspi_select.c) is the flash’s chip select. It can only be read from RAM with every interrupt off and the pin let go, some 30 us every 30 ms. DMA goes on meanwhile.

Boot

  • The badge’s bootloader starts the image; its start conditions are on MCH2022 badge (RP2040 coprocessor).

  • The watchdog’s scratch registers are read and cleared first thing: the bootloader looks at them, and a crash leaves its record there (register 18).

  • The ESP32 stays in reset until the I2C target runs.

Watchdog

  • 2 s, fed by the bridge thread, the lowest of the firmware’s threads.

  • Anything that hangs reboots the badge, the ESP32 with it, rather than need its power cut.

  • A DMA abort that waited for ever did exactly that on the desk before the watchdog was there.

  • It is off for the ROM’s bootloader.

The FPGA accelerator

The badge carries an iCE40UP5K next to its ESP32. The gateware in gateware/ (Amaranth) makes it the badge’s display controller and spectrum engine:

  • The ESP32 sends I/Q samples and its user interface over one SPI port.

  • The FPGA computes the spectrum and draws a spectrum trace and a waterfall straight to the ILI9341, at the panel’s rate.

What runs where

ESP32

FPGA

Panel

Zephyr’s ILI9341 driver and LVGL, through esdr,mch2022-fpga-dbi

8080 bus, passthrough, its own regions

Samples

bursts from lib/esp_sdr, sent as they are (WORD32) or packed

8192-sample ring

Spectrum

settings in dB (esdr_fpga_dsp_config())

window, FFT 16..512, power, log2, average, columns

Graphics

palette, region placement

waterfall ring (64 KiB), trace, rendering per TE

The panel’s bus

  • The FPGA owns the panel’s bus while the ESP32 holds LCD_MODE (IO26) high.

  • The ESP32 then floats its D/C and CS, which meet the FPGA’s WR and CS through 47 ohm, and keeps the panel’s reset.

  • Without a bitstream the panel controller drives the panel over SPI as before.

The DSP chain

Per frame:

  1. The window (Q15, from esp_sdr_window()) and a halving.

  2. A radix-2 FFT that halves every stage.

  3. L = 256 log2 |X|^2 plus 512 per halving plus an offset that puts a full-scale tone at 0 dB.

  4. The average s += F(L - s), from a table that makes it the linear exponential average of esdr.psd().

  5. Every few frames, the columns (the max of their bins, with a start bin and step for zoom), scaled into a waterfall line and the trace’s rows.

model.py is the reference, bit for bit.

Throughput on the badge

Measured on the badge (N = 512, 30 MHz):

frames per second

about 1860 (950 kS/s)

a full-scale tone at +fs/8

column 200 of 320, 0.1 dB

radio bursts of 16380 at 40 MS/s, WORD32

73 per second: capture 0.43 ms, send 13.3 ms

the same, IQ10P / IQ8

102 / 127 per second (packing 1.1 / 0.8 ms)

  • With interrupt-driven SPI (CONFIG_SPI_ESP32_INTERRUPT, which the driver implies) the CPU is free while a frame goes out.

  • The FFT, not the link, limits the throughput.

The renderer

  • Two regions, a waterfall and a spectrum trace, each placed in screen pixels.

  • Every pixel is a palette index: 248 colours for the waterfall, the rest for the trace and centre line.

  • The renderer writes with the host’s MADCTL minus MV, so a write runs along a native panel row.

  • It does one memory write per row, so the host gets the bus between any two rows when it has bytes queued or holds the bus (DBI flag HOLD, set by the panel controller from CASET/PASET against the bytes written).

  • A full 320x240 screen takes about 10.5 ms; on the TE edge (88 per second on this panel) that is 44 renders per second.

Sample formats

gateware/tools/bfp_eval.py compares formats on real bursts (fpga dump): eight bursts at 16, 40 and 80 MS/s, spectra at N = 512 against the lossless 10 bits, worst case:

Format

Bits per pair

Floor shift

Worst error, top 40 dB

Weak tone’s margin lost

IQ10P

20

0

0

0

BFP8 (16 pairs share an exponent)

16.5

0.28 dB

0.41 dB

0.1 dB

IQ8

16

0.73 dB

1.20 dB

0.1 dB

BFP6

12.5

3.0 dB

3.7 dB

1.5 dB

BFP4

8.5

13.1 dB

10.8 dB

8.8 dB

NVFP4 (E2M1, E4M3 scale per 16)

9

13.1 dB

10.7 dB

8.7 dB

4-bit

8

17.0 dB

16.7 dB

7.5 dB

Only BFP8 stays within 0.5 dB of the 10 bits, for 17 % fewer bytes. With the FFT as the limit (Throughput on the badge) that buys nothing, so the gateware has IQ10P and IQ8 and no block formats.

Fitting the UP5K

  • The gateware fills 4982 of 5280 logic cells (all 4 SPRAMs, 18 of 30 block RAMs, 3 of 8 multipliers) at 32 MHz.

  • To fit, the FFT stops at 512 points (the screen has 320 columns), and it halves every stage instead of keeping a block exponent.

  • The trace has no fill or grid.

  • The test source (a tone generator) is gone: the ESP32 makes test samples.

  • The UART probe line is a build variant (debug).

  • New features need cells found first: synth_ice40 -noflatten with yosys stat shows them per module.

The LED

  • The LED fades on the UP5K’s hard PWM (SB_LEDDA_IP), which the driver sets up over one register at boot.

  • Colour and brightness come from CONFIG_ESDR_FPGA_LED_RED, _GREEN and _BLUE.

  • They stay out of the bitstream on purpose: this close to full, new constants alone moved the clock from 32.1 to 28.75 MHz, which fails, and the seed moves it as far (29.2 to 32.25 MHz).

Building and loading

nix-shell shell.nix
cd gateware
python3 -m esdr_fpga.build top --out <dir>     # bitstream <dir>/top/top.bin, utilisation, fmax
python3 -m pytest tests                       # simulations against model.py
python3 -m esdr_fpga.regs > ../include/esp_sdr/esdr_fpga_regs.h

Loading the bitstream

  • CONFIG_ESDR_FPGA_IMAGE embeds a bitstream; the driver loads it at boot (38 ms).

  • Zephyr’s lattice,ice40-fpga driver configures the FPGA at 25 MHz, the configuration port’s limit in the data sheet; frames run at 40 MHz.

  • The image goes over SPI3 with SS low.

  • The board holds the FPGA in reset until a load (creset-hold).

  • CRESET (the RP2040’s register 5, bit 0) and CDONE go through a GPIO driver on the RP2040. When CRESET moves and CDONE is read is in I2C registers and the FPGA reset.

fpga_demo

apps/fpga_demo has shell commands for:

  • the link (fpga test);

  • graphics from software (fpga gfx);

  • the DSP on samples made on the ESP32 (fpga dsp);

  • built with -DEXTRA_CONF_FILE=radio.conf, the badge’s radio (fpga radio, fpga dump).

Integration test

tests/integration/esdr_fpga checks loading, the link, the panel and the DSP on the badge, with the bitstream from ESDR_FPGA_IMAGE. How it is deployed is in Testing.

Measurements

Stream rates

osmosdr_serial by its average rate of samples, in 4 to 14 s at the desk (all blocks whole):

Stream

Plain, 2 Mbaud

Framed, 2 Mbaud

Framed, 5 Mbaud

Framed, FPGA

2 MS/s

46.4 kS/s

82.2 kS/s

157.8 kS/s

449.6 kS/s

1 MS/s

44.8 kS/s

77.7 kS/s

139.7 kS/s

415.8 kS/s

500 kS/s

22.8 kS/s

70.4 kS/s

113.6 kS/s

236.0 kS/s

250 kS/s

11.5 kS/s

59.1 kS/s

82.6 kS/s

126.1 kS/s

125 kS/s

not measured (a single failed start, see A failed start)

44.7 kS/s

53.0 kS/s

64.8 kS/s

  • The FPGA’s column is through SoapySDR.

  • The other columns were measured before the burst cycle got shorter (Burst timing on the ESP32). At 2 MS/s they now reach 89.1 kS/s plain and 178.9 kS/s framed at 5 Mbaud.

Through the FPGA

  • At 2 MS/s through the FPGA, 450 kS/s, some 900 kB/s, which is USB’s limit: the ESP32 could give 670 kS/s.

  • A minute through SoapySDR: 443 kS/s, none dropped.

  • A minute with every frame checked: no bad or lost frame, no framing error, the ready line never low.

  • Another figure for the fast line: with the link bitstream, about 700 kB/s of osmosdr_serial’s blocks, where the UART takes 300. It disagrees with the 450 kS/s, some 900 kB/s, measured above.

Burst timing on the ESP32

Below 2 MS/s the ESP32 sets the pace. What a burst takes (16380 samples at 16 MS/s, in microseconds):

2 MS/s

1 MS/s

500 kS/s

125 kS/s

capture (the engine, no CPU)

1022

1024

1023

1017

CIC

1720

1270

1043

869

samples into a block

283

142

72

19

COBS and CRC, while the capture runs

627

318

161

42

SPI, by DMA, while the rest runs

871

455

250

95

  • So a burst is capture, CIC and packing in a row, as long as encoding fits inside the capture.

  • The CIC costs 12 cycles an input and 105 an output of its 240 MHz. The outputs’ share is register pressure: it has six integrators and six comb states.

  • The CIC is the next thing to go after, or the second core.

Burst time cuts

It was 5.9 ms a burst at 2 MS/s before these changes, each measured with cycle counters on the badge:

  • The ESP32’s SPI driver spun through every transfer: its interrupt mode set the interrupt flag itself and did the whole transfer inside the handler, 860 us with the CPU stopped. It now starts a transfer and goes on at the hardware’s interrupt.

  • A 64-bit multiply per CIC output went to libgcc (now mulsh, the result the same for every factor).

  • A function call per sample into the block: 117 cycles a sample, now 33.

  • CRC-32 by the nibble (now the ROM’s, by the byte) and COBS through memory (now in registers).

  • A frame encoded anew for every look at the credit (twice a burst at the top rate).

  • A burst’s last 4 samples as a frame of their own (bursts are whole blocks now).

  • SoapySDR reading one transfer at a time, which left USB idle for a tenth of its time (now 4 queued, and decoding in a thread of its own).

The SPI write beside the capture

  • The SPI write now runs while the next capture does, both by DMA into or out of internal RAM.

  • A tone through the chain (the WHY badge’s C6, 900 kHz off) would show a lost word as a phase step of 0.35 rad inside a burst. It shows the same as with the write spinning, no overlap.

  • The largest step in a burst is 0.45 rad median, 0.54 at most in 5000 bursts (its transmitter’s own sidebands). With one word’s step added in software, the same bursts show 0.56 and up in 95 % of them.

Checks and open points

A failed start

  • One start of the plain transport failed once with SET_FREQ: Invalid argument, right after a 250 kS/s run.

  • 46 later starts did not fail, 16 of them in that same order.

  • 30 before and 30 after the idle sleep went from 1 ms (200 bytes at 2 Mbaud, against the ESP32’s FIFO of 128) to 100 us, so that change is reasoned, not proven.

Checked by hand

  • Every button with SELECT and the joystick as the ESP32 reads them (twice).

  • The launcher by joystick after a cold start.

  • The backlight’s steps.

  • The IR LED in a phone’s camera.

Checked by test

  • integration.esdr_fpga passes (4 of 4) through this firmware.

  • The FPGA’s bitstream loaded at 240 of 240 resets of the ESP32 under that test’s image.

  • It loaded at 24 of 24 under MicroPython (12 resets, 12 reboots of the RP2040).

Not checked

For want of hardware:

  • WS2812: no LEDs on the badge’s SAO port.

  • The FPGA’s UART loopback: it needs a bitstream that talks.

Previous: mch2022_coproc · Up: Applications · Next: Reference