Daisy-chainable 16-port UART-to-GPIO expander on WCH RISC-V microcontrollers (CH32V002 / CH32V003), designed for the 99LED 2×(7-segment + dot) common-anode display module.
Includes:
sw/— Zero-dependency CH32V002/CH32V003 firmware, daisy-chain UART expander + segment test modelib/— Universal (Browser + Node.js) WebUSB WCH-LinkE programmer and UART/99LED encoder APIbin/wch.js— Node.js CLI for flashing, unbricking, and driving chained boards via WCH-LinkEexamples/watch.js— Node.jsHH:MM:SSdigital clock example for chained 99LED boards99led/— KiCad schematic, PCB, and gerbers
- MCU:
CH32V002F4P6(TSSOP-20, 16 KB flash, 4 KB RAM, 256 B flash sectors) — pin-compatible withCH32V003F4P6. - Displays: 2 ×
5161BScommon-anode 7-segment + dot indicators (0= LED ON,1= LED OFF), 330 Ω series resistor arrays (RN1–RN4). - Chain topology: Right-to-left cascade —
RX(PD1, pin 18) on right header (J4),TX(PD0, pin 8) on left header (J3).[Host TX] -> [RX (Board 1, rightmost) TX] -> [RX (Board 2) TX] -> ... -> [RX (Board N, leftmost) TX] - Low-noise RF discipline: Runs at
1.5 MHz(HSI/16, no PLL) in UART mode, sleeps in__WFI()between UART interrupts, and updates only changed GPIO ports atomically viaBSHR.
Each board consumes 2 bytes (b0, b1) mapped directly to hardware GPIO ports:
| Byte / Bits | Hardware Port | Mask / Shift |
|---|---|---|
b0[7:0] |
PC7..PC0 |
GPIOC mask 0xFF, shift 0 |
b1[7:2] |
PD7..PD2 |
GPIOD mask 0xFC, shift 0 (PD0/PD1 preserved for UART) |
b1[1:0] |
PA2..PA1 |
GPIOA mask 0x06, shift +1 |
99LED 5161BS segment bit indices (0..15 in b0 | (b1 << 8)):
- Left digit:
a=13 (PD5),b=12 (PD4),c=8 (PA1),d=9 (PA2),e=0 (PC0),f=14 (PD6),g=15 (PD7),dp=3 (PC3) - Right digit:
a=11 (PD3),b=10 (PD2),c=4 (PC4),d=2 (PC2),e=1 (PC1),f=7 (PC7),g=6 (PC6),dp=5 (PC5)
CH32V002 silicon notes
- Flash latency:
FLASH_ACTLR_LATENCY_1is required above 15 MHz (SYSCLK_HZ = 24000000insw/ch32v00x.h).- GPIO speed:
GPIO_Speed_2MHz(MODE=0b10) is not implemented on CH32V002; useGPIO_Speed_10MHz(MODE=0b01) or50MHz(MODE=0b11).PD7isNRSTby default; configure NRST as GPIO once (wch flash -D ...orwch nrst-gpio) soPD7drives segmentgof the left digit without resetting the MCU.
- Serial:
4800baud,8N1(RX = PD1,TX = PD0; CH32V002USART1remap 2 / CH32V003 remap 1). - Frame:
0xAA | len | b0 | b1 | [b2 | b3 | ...]len: total payload bytes (2 * NforNchained boards,2..254).- Each board consumes
b0, b1and applies them to its 16 outputs. - If
len > 2, the board streams0xAA | (len - 2) | b2 ... b_{len-1}outTX (PD0)via a 16-byte TX ring buffer (~2 byte-times latency per hop). - The last board (
len == 2) keepsTXcompletely silent. - IDLE line resync: any inter-frame gap (> 1 byte time, ~2 ms) resets the parser to state 0; payload bytes may freely contain
0xAA.
Example for 3 chained boards ([HH][MM][SS] right-to-left):
- Host sends:
AA 06 <SS0> <SS1> <MM0> <MM1> <HH0> <HH1> - Board 1 (rightmost) consumes
<SS0> <SS1>, forwardsAA 04 <MM0> <MM1> <HH0> <HH1> - Board 2 (middle) consumes
<MM0> <MM1>, forwardsAA 02 <HH0> <HH1> - Board 3 (leftmost) consumes
<HH0> <HH1>, forwards nothing
npm installRequires only riscv64-unknown-elf-gcc (zero external SDK dependencies).
# Build & flash normal daisy-chain UART expander mode (TEST_MODE=0)
make -C sw clean && make -C sw build TEST_MODE=0
./bin/wch.js unbrick
./bin/wch.js flash -D sw/main.bin
# Build & flash segment test mode (TEST_MODE=1, walks 1 segment ON at a time)
make -C sw clean && make -C sw flash TEST_MODE=1Because
PD1is shared betweenSWIOandUSART1 RX(and UART mode runs at 1.5 MHz), run./bin/wch.js unbrickbefore reflashing a board running UART mode. To program and send UART without swapping wires, keep WCH-LinkESWDIO→PD1and tie WCH-LinkETX→PD1through a1 kΩresistor.
./bin/wch.js list # List connected WCH-Link USB devices
./bin/wch.js info [--json] # Show programmer + target MCU info
./bin/wch.js flash [-D] <file.bin|hex> # Flash, verify, reboot (-D: NRST->GPIO first)
./bin/wch.js nrst-gpio # Configure NRST as GPIO (mass-erases flash)
./bin/wch.js unbrick [002|003] # Hardware power-off mass-erase unbrick (default: 002)
./bin/wch.js reboot # Reset and resume target MCU
./bin/wch.js send <hex...> # Send framed UART payload (0xAA <len> <bytes...>)
./bin/wch.js send --raw <hex...> # Send raw UART bytes
./bin/wch.js show <text> [--boards N] # Display digits/dots on chained 99LED boardsExamples:
./bin/wch.js send 00 00 # 1 board: all 16 segments ON
./bin/wch.js send ff ff # 1 board: all 16 segments OFF
./bin/wch.js show 42 # 1 board: display "42"
./bin/wch.js show 12.34.56 # 3 boards: display "12.34.56"Displays HH:MM:SS with 1 Hz blinking dot separators across chained 99LED boards:
./examples/watch.js # 3 boards: HH.MM.SS
./examples/watch.js --boards 2 # 2 boards: HH.MM
./examples/watch.js --boards 1 # 1 board: SS.
./examples/watch.js --no-blink # steady separator dotslib/index.js has zero Node.js dependencies and works directly in modern browsers:
import {
wchlink,
WCH_LINK_FILTERS,
parseIntelHex,
encodeChain99Led,
openWebSerial
} from './lib/index.js';
// Flash via WebUSB
const device = await navigator.usb.requestDevice({filters: WCH_LINK_FILTERS});
const link = await wchlink({device, onProgress: console.log});
await link.writeImage(firmwareUint8Array);
await link.close();
// Drive chained 99LED boards via Web Serial
const serial = await openWebSerial({baudRate: 4800});
await serial.sendPayload(encodeChain99Led('12.34.56', {boards: 3}));
await serial.close();import {encodeChain99Led} from './lib/index.js';
import {openWchLink, loadFirmwareFile, openNodeSerial} from './lib/node.js';
// Flash via Node WebUSB (`usb`)
const link = await openWchLink();
await link.writeImage(await loadFirmwareFile('sw/main.bin'));
await link.close();
// Drive chained 99LED boards via WCH-LinkE serial (/dev/ttyACM*)
const serial = await openNodeSerial({baudRate: 4800});
await serial.sendPayload(encodeChain99Led('12.34.56', {boards: 3}));
await serial.close();- Over I2C: https://github.com/AI6YP/XAPOH/tree/trunk/sw-v3/expander
- Over UART: https://github.com/AI6YP/XAPOH/tree/trunk/sw-v4
Heavily influenced by ch32fun (CNLohr et al.) — used as reference for both the bare-metal CH32V002/CH32V003 startup/MMIO setup (sw/) and the WCH-LinkE programming protocol (lib/wchlink.js).