Skip to content
AI6YPPublic

About

I2C expander based on RISCV microcontroller

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

expander

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 mode
  • lib/ — Universal (Browser + Node.js) WebUSB WCH-LinkE programmer and UART/99LED encoder API
  • bin/wch.js — Node.js CLI for flashing, unbricking, and driving chained boards via WCH-LinkE
  • examples/watch.js — Node.js HH:MM:SS digital clock example for chained 99LED boards
  • 99led/ — KiCad schematic, PCB, and gerbers

Hardware (99LED Board)

  • MCU: CH32V002F4P6 (TSSOP-20, 16 KB flash, 4 KB RAM, 256 B flash sectors) — pin-compatible with CH32V003F4P6.
  • Displays: 2 × 5161BS common-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 via BSHR.

Pin & Bit Mapping

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_1 is required above 15 MHz (SYSCLK_HZ = 24000000 in sw/ch32v00x.h).
  • GPIO speed: GPIO_Speed_2MHz (MODE=0b10) is not implemented on CH32V002; use GPIO_Speed_10MHz (MODE=0b01) or 50MHz (MODE=0b11).
  • PD7 is NRST by default; configure NRST as GPIO once (wch flash -D ... or wch nrst-gpio) so PD7 drives segment g of the left digit without resetting the MCU.

Daisy-Chain UART Protocol

  • Serial: 4800 baud, 8N1 (RX = PD1, TX = PD0; CH32V002 USART1 remap 2 / CH32V003 remap 1).
  • Frame:
    0xAA | len | b0 | b1 | [b2 | b3 | ...]
    
    • len: total payload bytes (2 * N for N chained boards, 2..254).
    • Each board consumes b0, b1 and applies them to its 16 outputs.
    • If len > 2, the board streams 0xAA | (len - 2) | b2 ... b_{len-1} out TX (PD0) via a 16-byte TX ring buffer (~2 byte-times latency per hop).
    • The last board (len == 2) keeps TX completely 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>, forwards AA 04 <MM0> <MM1> <HH0> <HH1>
  • Board 2 (middle) consumes <MM0> <MM1>, forwards AA 02 <HH0> <HH1>
  • Board 3 (leftmost) consumes <HH0> <HH1>, forwards nothing

Quick Start

Install Node dependencies

npm install

Build & Flash Firmware (sw/)

Requires 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=1

Because PD1 is shared between SWIO and USART1 RX (and UART mode runs at 1.5 MHz), run ./bin/wch.js unbrick before reflashing a board running UART mode. To program and send UART without swapping wires, keep WCH-LinkE SWDIO → PD1 and tie WCH-LinkE TX → PD1 through a 1 kΩ resistor.


CLI (bin/wch.js)

./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 boards

Examples:

./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"

Watch Example (examples/watch.js)

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 dots

JavaScript Library (lib/)

Browser (WebUSB + Web Serial)

lib/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();

Node.js (lib/node.js)

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();

Earlier Revisions


Credits

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).

About

I2C expander based on RISCV microcontroller

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages