# MCH2022 badge Previous: [mch2022_coproc](../../apps/mch2022_coproc/README.rst) · Up: [Applications](index.md) · Next: [Reference](../dev/index.md) 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](../../apps/mch2022_coproc/README.rst) in [Applications](index.md). 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)](../../boards/badgeteam/mch2022_rp2040/doc/index.rst). Usage is in the READMEs of [mch2022_coproc](../../apps/mch2022_coproc/README.rst), [osmosdr_serial](../../apps/osmosdr_serial/README.rst) and [micropython](../../apps/micropython/README.rst). ## The RP2040 coprocessor Design note, 2026-11-03/04. [mch2022_coproc](../../apps/mch2022_coproc/README.rst) replaces the stock firmware of the badge's RP2040 (`boards/badgeteam/mch2022_rp2040`), so that [osmosdr_serial](../../apps/osmosdr_serial/README.rst) is no longer limited by the link. The app's README says how to build and install it; [MCH2022 badge (RP2040 coprocessor)](../../boards/badgeteam/mch2022_rp2040/doc/index.rst) 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](#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](../../apps/mch2022_coproc/README.rst); 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](../../apps/osmosdr_serial/README.rst) stopped at 2 Mbaud ([UARTs on DMA](#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](#usb-bulk-engine)). - The FPGA's fast line into that pipe, 15 Mbaud from the FPGA with the link bitstream ([The fast line](#the-fast-line)). - Registers 100 to 103, by which the ESP32 can tell it from the stock firmware ([I2C registers](#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](#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)](../../boards/badgeteam/mch2022_rp2040/doc/index.rst). - 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 SPI link One SPI frame (CS low) is one command. The opcodes, memory windows and registers are in `include/esp_sdr/esdr_fpga_regs.h`, generated from `gateware/esdr_fpga/regs.py`. | Opcode | Payload | |---|---| | 0x01 WRITE | address, 16-bit words | | 0x02 READ | address, count, two pad bytes; then the words come back | | 0x03 SAMPLES | format (WORD32, IQ10P, IQ8, IQ16; RESYNC), samples | | 0x04 DBI | flags (HOLD, NOCMD), command, data: the panel's traffic | | 0x05 WFLINE | one waterfall line of palette indices | #### Clock rates - Writes run at 40 MHz (about 5 MB/s), reads at 20 MHz. - MISO comes through the GPIO matrix, and at 26.7 MHz and above reads fail. - Reads must be read-only SPI chunks: the classic ESP32's DMA cannot do a write phase and a read phase in one half-duplex transaction (Zephyr commit "drivers: spi: esp32: no write phase before half-duplex reads"). ### 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](#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 ```sh nix-shell shell.nix cd gateware python3 -m esdr_fpga.build top --out