Skip to content

Platform port: WCH CH32V003 (RV32EC, 48MHz, 16KB flash / 2KB SRAM) #3755

Open

Description

@zackees

Summary

Bring up FastLED on the WCH CH32V003 (RV32EC @ 48 MHz, 16 KB flash / 2 KB SRAM). Hardware is on the bench.

Today FastLED has zero WCH code. The only artifact in the tree is ci/boards/package_ch32v_index.json — a snapshot of WCH's Arduino board-manager index added in 5cf16c9157 (2025-10-24), file only, never wired to anything. Someone started this and stopped.

Build/flash tooling is already done on the fbuild side and is being validated against real silicon in FastLED/fbuild#1208 — that issue is a prerequisite for Phase 2 here. This issue covers only src/platforms/wch/.

Why this part is harder than a normal port

Three constraints that no currently-supported FastLED platform has all of:

  1. No hardware multiply. RV32EC is the E base + C only — no M extension. Every *, /, % becomes a libgcc call. scale8, blend8, nscale8x3, and the HSV→RGB paths are all multiply-heavy. This needs measuring before anything else.
  2. 2 KB SRAM / 16 KB flash. Below every supported part. The tiny memory class in ci/boards.py:255-300 is documented as <=1KB SRAM and holds the ATtinys; 16 KB flash is smaller than anything in the tree. FL_PLATFORM_HAS_TINY_MEMORY in src/fl/system/sketch_macros.h:55-76 is currently set only by FL_IS_AVR_ATTINY_TINY_MEMORY and will need extending.
  3. 16 registers, not 32 (ilp32e ABI). Register pressure in the timing-critical inner loop is real.

There is also a latent __riscv hazard: FastLED already treats RISC-V as a sub-branch of ESP (src/platforms/esp/32/core/delaycycles_riscv.h, interrupts/riscv.hpp, simd_riscv.hpp), reached via #elif defined(ESP32C3) || defined(ESP32C6) in src/platforms/delay.h:33 and src/platforms/delaycycles.h:26. This bit us once already — #2576/#2577 had to re-gate fast_isr.S on __riscv_atomic instead of bare __riscv because ESP32-C3 lacks the A extension. RV32EC lacks A and M. Every bare __riscv gate must be audited.

Template

The reference is the initial LPC845 port, commit ebd8791748 ("feat(platform): add LPC845 bit-bang clockless driver (#2836) (#2837)") — 9 files, 353 lines. src/platforms/arm/lpc/ has since grown to ~26 files, but that first commit is the minimal viable shape.

Required new files under src/platforms/wch/:

File Must define
is_ch32.h FL_IS_WCH, FL_IS_WCH_CH32V00X, FL_IS_WCH_CH32V003. Per #2999 every detection macro is FL_IS_-prefixed and rolls up to a family.
int.h integer typedefs
led_sysdefs_wch_ch32.h F_CPU, FASTLED_ALLOW_INTERRUPTS, FASTLED_USE_PROGMEM 0, FASTLED_NO_PINMAP, RoReg/RwReg, cli()/sei(), vendor header include
fastpin_wch_ch32.h _FL_DEFPIN table, HAS_HARDWARE_PIN_SUPPORT 1, port_ptr_t/port_t
clockless_wch_ch32.h FL_CLOCKLESS_CONTROLLER_DEFINED 1 + ClocklessController
fastled_wch_ch32.h aggregator
README.md required by hook

Existing files needing a new branch — all of these, because CH32V003 is a new architecture, not a new ARM chip:

  • src/platforms.h — ⚠️ the final #else is AVR, so an unhandled chip silently compiles against AVR headers rather than erroring
  • src/led_sysdefs.h — final #else is a hard #error
  • src/platforms/is_platform.h (include block, ~lines 141-156)
  • src/platforms/fastpin.h, src/platforms/int.h
  • src/platforms/delay.h, src/platforms/delaycycles.h — else falls to delaycycles_generic.h:16-22, a naive while(cycles--) nop;
  • src/platforms/clockless.h
  • src/platforms/ldf_headers.h, src/platforms/compile_test.cpp.hpp
  • src/platforms/_build.cpp.hpp

Detection macros available: the OpenWCH Arduino core / PlatformIO board record defines -DCH32V003 -DCH32V003F4 -DCH32V00X -DCH32V00x. Compiler-intrinsic: __riscv, __riscv_xlen == 32, __riscv_32e, __riscv_abi_rve, and the absence of __riscv_mul / __riscv_atomic.

Clockless output strategy

The community-proven WS2812 path on this part is SPI + DMA, not bit-bang — at 48 MHz with no multiplier the bit window is tight. Prior art:

If bit-bang is attempted first, the model is src/platforms/arm/common/m0clockless_c.h, whose header comment (lines 143-155) explains why it uses compile-time NOP-counted delays rather than a cycle counter. Note its FL_ARM_M0_DELAY_LOOP variant exists precisely because ~48 unrolled delay sites cost too much SRAM on the 16 KB-SRAM LPC845 — on a 2 KB part the counted-loop variant is mandatory, not optional. CH32V003 does have a free-running 32-bit SysTick (STK_CNTL), unlike Cortex-M0's 24-bit down-counter, so a counter-based approach is worth measuring.

⚠️ If an SPI-DMA driver lands, it must be a single ChannelEngine handling both clockless and SPI modes — Capabilities(true, true), routing in show() on data->isSpi() vs data->isClockless(). Do not add a Bus::X_SPI enum value or a separate ChannelEngineXxxSPI. See src/fl/channels/README.md.

Step-by-step

Phase 0 — Feasibility spike (do this before writing a platform)

  • Compile a trivial sketch for genericCH32V003F4P6 and measure the flash cost of scale8 / nscale8x3 / hsv2rgb_rainbow with no hardware multiply. If the softmul calls blow the 16 KB budget, the port needs multiply-free variants and that changes the whole plan.
  • Measure baseline flash/RAM for FastLED core minus drivers. Establish how many LEDs actually fit in 2 KB.
  • Decide bit-bang vs SPI+DMA on measured numbers, not preference.
  • Audit every bare __riscv / __riscv_xlen gate in src/ for ESP-IDF assumptions.

Exit criteria: a written go/no-go with real byte counts.

Phase 1 — Platform skeleton (compile-only, no hardware)

  • src/platforms/wch/is_ch32.h + wire into is_platform.h.
  • led_sysdefs_wch_ch32.h, int.h, fastpin_wch_ch32.h, fastled_wch_ch32.h.
  • Add branches to platforms.h and led_sysdefs.h. Verify the AVR #else fallthrough is not being hit — assert on a WCH-only define in compile_test.
  • src/platforms/wch/README.md.
  • Extend sketch_macros.h memory tiering; add WCH to the FL_PLATFORM_HAS_LARGE_MEMORY 0 list (lines 88-103, already contains FL_IS_ARM_LPC).
  • FastPin toggles a pin. No LED driver yet.

Exit criteria: clean compile, and a scope-verified GPIO toggle at a known rate confirming F_CPU is right.

Phase 2 — Clockless driver (needs hardware + fbuild#1208)

  • Implement clockless_wch_ch32.h per the Phase 0 decision.
  • Validate WS2812 timing on a scope: T0H/T1H/reset within spec across the full byte range.
  • Confirm behaviour with interrupts enabled and disabled.
  • Run a real strip; check for the classic first-pixel-green / phantom-pixel timing artifacts.

Exit criteria: a stable multi-pixel rainbow on physical WS2812s.

Phase 3 — Integrate

  • ci/boards.py: add a Board entry. Model on the LPC precedent (lines 1205-1260) — platform="https://github.com/Community-PIO-CH32V/platform-ch32v.git", platform_needs_install=True, framework="arduino". Set memory_class per Phase 1.
  • .github/workflows/build_ch32v003.yml from the template. Note ci/boards/package_ch32v_index.json already exists and is consumed by build_template_custom_board.yml:41-44.
  • platformio.ini, ci/ci-compile.py.
  • Add a WCH entry to the ci/bloat.py chip map (it already has "esp32c3": ("riscv", "esp32c3")).
  • compile_test.hpp, _build.cpp.hpp.
  • Decide whether to add channel_drivers_wch.impl.hpp. Not required — the #else in channel_drivers.impl.cpp.hpp routes to shared/channel_drivers_null.impl.hpp (a no-op) and the classic ClocklessController path works fine. The cheap win if wanted is BusTraits<Bus::BIT_BANG> from src/platforms/shared/bitbang/bus_traits.h; model on src/platforms/arm/lpc/channel_drivers_lpc.impl.hpp (39 lines).

Open question — toolchain

The OpenWCH Arduino core ships GCC 8.2.0 (riscv-none-embed-gcc) because it uses WCH's proprietary xw compressed-instruction extension (-march=rv32ecxw), which upstream GCC cannot consume. GCC 8.2.0 is a serious liability for a modern C++ library, and there is no arm64 macOS build.

fbuild instead ships xPack riscv-none-elf-gcc 14.2.0-3 with -march=rv32ec_zicsr -mabi=ilp32e — no xw, matching what ch32fun does. That is the path fbuild#1208 validates and the recommended default here.

⚠️ Note _zicsr is mandatory from GCC 12 — GCC ≤11 folded CSR ops into the base ISA, so a bare -march=rv32ec on a modern toolchain silently loses csrr/csrw. This is the single most common CH32V003 build failure. Also verify the chosen xPack build actually ships an rv32ec/ilp32e libgcc multilib.

Tension to resolve: ci/boards.py assumes framework="arduino", which via PlatformIO pulls GCC 8.2.0 and rv32ecxw. Either accept GCC 8.2.0 for the PIO CI path, or override the toolchain in the board definition.

References

Activity

  1. zackees commented on Jul 28, 2026

    @zackees
    MemberAuthor

    Tracked by meta issue FastLED/fbuild#1209 (CH32V003 bring-up: this + FastLED/fbuild#1208).

    Note on sequencing: Phase 0 (the multiply-free feasibility spike) is not blocked — it needs no hardware and no fbuild work. It should start in parallel with fbuild#1208 Phase 0/1, since a bad result there changes the shape of this whole port. Only Phase 2 (clockless driver) depends on fbuild#1208 reaching a verified flash.

  2. zackees commented on Jul 29, 2026

    @zackees
    MemberAuthor

    Data point from the fbuild side (FastLED/fbuild#1208 Phase 1, now verified on a real toolchain install):

    A bare blink built with the OpenWCH Arduino core for genericCH32V003F4P6 costs:

    Used Budget
    Flash 8896 B 16384 B (54.3%)
    RAM 1180 B 2048 B (57.6%)

    Toolchain confirmed as genuine RV32EC — the ELF reports Flags: 0x9, RVC, RVE, soft-float ABI, so -march=rv32ec_zicsr -mabi=ilp32e really is resolving the E-base multilib rather than silently falling back to rv32i.

    This tightens Phase 0's feasibility question considerably. The Arduino core alone leaves roughly 7.5 KB flash and 868 bytes SRAM for FastLED plus the sketch. Phase 0 should measure against that headroom, not against the raw 16 KB / 2 KB figures in the issue body.

    868 bytes of SRAM also constrains the driver choice directly: at 3 bytes per pixel a full framebuffer is ~100 px before anything else is allocated, which is an independent argument for the chunked / double-buffered streaming approach ch32fun's ws2812b_dma_spi_led_driver.h uses rather than a resident buffer.

  3. zackees commented on Aug 20, 2026

    @zackees
    MemberAuthor

    Burn-down disposition: DEFERRED ? platform port and required probe are both absent

    Current master still has no src/platforms/wch/, no CH32V003 Board entry, no detection/dispatch branch, and no clockless or unified SPI-DMA engine. The only in-tree artifact remains ci/boards/package_ch32v_index.json.

    The fbuild prerequisite made useful progress in FastLED/fbuild#1208:

    • xPack GCC 14.2.0-3 successfully builds a genuine RV32EC/ilp32e image.
    • The bare Arduino blink is 8,896/16,384 B flash and 1,180/2,048 B RAM, leaving only about 7.5 KB flash and 868 bytes SRAM for FastLED plus the sketch.
    • Wlink acquisition, error handling, flash-readback, and core-executing HIL tests are implemented.

    But the hardware prerequisite remains unmet. Fresh fbuild port scan has no 1A86:8010/8012 WCH-LinkE. The fbuild investigation established that the CH32V003 board's USB connector is power-only and no WCH programmer has ever been attached to this host; direct USB cannot flash this MCU.

    Remaining work is the complete Phase 0?3 program: measure FastLED math/code-size feasibility under the 868-byte headroom, audit RISC-V ISA gates, create the WCH platform skeleton, choose and implement a bit-bang or streaming SPI-DMA driver, then scope and strip-validate it on silicon before adding CI.

    Resume when a WCH-LinkE is connected and wired to 3V3/GND/SWIO, with a CH32V003 target and scope/strip available. Run fbuild#1208's readback/core/blink sequence first; only then make the feasibility go/no-go and start the FastLED port. This is not safely reducible to one source-only PR in the current bench state.

  4. added
    deferred
    on Aug 20, 2026
  5. dfleck commented on Nov 3, 2026

    @dfleck

    I'm looking forward to FastLED support for CH32 chips. BTW the CH32V002 chip is almost as inexpensive, has twice the SRAM, and the _zmmul multiply instructions (but no divide).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    deferred

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions