iTranslated by AI
Blinking an LED with Rust on the CH32V003J4M6 Microcontroller
While I was playing around with microcontrollers recently, a colleague recommended the CH32V003—a 32-bit RISC-V microcontroller that looked small and fun—so I decided to try blinking an LED with Rust.

(The LED has a built-in resistor. Also, I apologize that the microcontroller is plugged into the breakout board upside down.)
Items purchased from Akizuki Denshi
- 32-bit RISC-V Microcontroller CH32V003J4M6
- SOP8 (1.27mm) DIP Conversion Board, Gold Flash (9 pieces)
- WCH-LinkE Emulator
Compiling Rust for CH32V003 support
Since Rust does not (yet) natively support the CH32V003, I had to put in some effort by referring to an article from around March 2023 titled Rust on the CH32V003.
Specifically, I compiled the Rust for CH3200V3 provided by a group working on Rust support for the CH32 series.
For the specific steps, I followed Part 1: Custom Rust toolchain from the aforementioned article.
$ git clone https://github.com/ch32-rs/rust.git
$ cd rust
$ git submodule update --init --recursive
$ vi config.toml
# copied from config.example.toml
change-id = 118703
# Use defaults for codegen affecting custom builds
profile = "codegen"
[llvm]
# Use our own LLVM build instead of downloading from CI
download-ci-llvm = false
[rust]
# Enable building LLD for our target as well
lld = true
$ python x.py build
...
Build completed successfully in 0:07:57
$ rustup toolchain link custom-rv32ec $PWD/build/host/stage1
Creating a Rust Project and Compiling for CH32V003
Project Creation and Configuration
$ cargo new
ch32v003j4m6-led
$ cd ch32v003j4m6-led
Next, write the minimum amount of code needed.
#![no_std]
#![no_main]
use riscv_rt::entry;
use panic_halt as _;
#[entry]
fn main() -> ! {
loop {}
}
Referring to Part 2: Boot and blink, configure the project so it can be built.
[package]
name = "ch32v003j4m6-led"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[patch.crates-io]
riscv-rt = { git = "https://github.com/9names/riscv-rt", branch = "rv32e" }
[dependencies]
panic-halt = "0.2.0"
riscv-rt = "0.11.0"
[profile.dev]
opt-level = "s"
[build]
target = "riscv32ec-unknown-none-elf"
[unstable]
build-std = ["core", "compiler_builtins"]
PROVIDE(_hart_stack_size = 1K);
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 16K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 2K
}
REGION_ALIAS("REGION_TEXT", FLASH);
REGION_ALIAS("REGION_RODATA", FLASH);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);
REGION_ALIAS("REGION_HEAP", RAM);
REGION_ALIAS("REGION_STACK", RAM);
fn main() {
// Tell rustc to pass linker scripts to LLD
println!("cargo:rustc-link-arg=-Tmemory.x");
println!("cargo:rustc-link-arg=-Tlink.x");
// Rerun this script only when necesary
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
}
Build
$ cargo +custom-rv32ec build
Create the binary to copy to the actual hardware.
$ llvm-objcopy -O binary target/riscv32ec-unknown-none-elf/debug/ch32v003j4m6-led out.bin
Writing to the Device
Preparing the WCH-LinkE

Connect the WCH-LinkE and CH32V003.
- 3V3 -> Pin 4 (bottom left)
- GND -> Pin 2 (second from top left)
- SWDIO -> Pin 8 (top right)
Preparing the Flashing Software
I used minichlink provided by ch32v003fun.
$ git clone https://github.com/cnlohr/ch32v003fun.git
$ cd minichlink
$ make
$ ./minichlink
Warning: found at least one WCH-LinkE in ARM programming mode. To use it with minichlink, you need to change it to RISC-V mode as per https://github.com/cnlohr/ch32v003fun/issues/227
Error: Could not initialize any supported programmers
Error: Could not initialize any supported programmers
Changing the WCH-LinkE Mode
When I plugged the WCH-LinkE into my PC, the blue LED was lit, so I had to drill a hole in the side of the case and press the button labeled ModeS while plugging it into the USB port.
It seems like you're supposed to open the case to do this, but I couldn't get it open.
Writing the Program to the CH32V003
$ ./minichlink -w ...../out.bin flash -b
Found WCH Link
WCH Programmer is LinkE version 2.9
Chip Type: 003
Setup success
Flash Storage: 16 kB
Part UUID : bb-71-ab-cd-23-99-bc-5b
PFlags : ff-ff-ff-ff
Part Type (B): 07-13-bb-91
Read protection: disabled
Interface Setup
Image written.
Blinking the LED
Referring to Part 2: Boot and blink, let's program the LED blink.
#![no_std]
#![no_main]
use riscv_rt::entry;
use panic_halt as _;
#[entry]
fn main() -> ! {
let RCC_APB2PCENR: *mut u32 = 0x4002_1018 as _;
let GPIOC_CFGLR: *mut u32 = 0x4001_1000 as _;
let GPIOC_OUTDR: *mut u32 = 0x4001_100C as _;
unsafe {
// Enable clocks to the GPIOC bank
RCC_APB2PCENR.write_volatile(0b1_0000);
// Set pin 1 to output
GPIOC_CFGLR.write_volatile(0b0001_0000);
loop {
// Set pin 1 to high
GPIOC_OUTDR.write_volatile(0b1_0);
for _ in 0..1_000_000 {
core::hint::black_box(()); // Do nothing, but keep the loop
}
// Set pin 1 to low
GPIOC_OUTDR.write_volatile(0b0_0);
for _ in 0..1_000_000 {
core::hint::black_box(()); // Do nothing, but keep the loop
}
}
}
}
Wiring and Powering On
Connect the LED from Pin PC1 (bottom right) of the microcontroller to GND.
Connect a button cell battery from VDD to GND of the microcontroller.
Conclusion
I managed to blink an LED with Rust on a tiny RISC-V microcontroller called the CH32V003.
It took quite a bit of trial and error, but when summarized as just the correct steps, it looks quite straightforward.
The source code is available here:
I hope I can build something fun with this!
I've written a follow-up article here:
Discussion