Post

Build CH32V003 chip in Linux environment

Build CH32V003 chip in Linux environment

The CH32V003F4P6, developed by Qinheng (WCH), is a low-cost, compact 32-bit RISC-V MCU ideal for hobbyists, students, and embedded developers exploring the RISC-V architecture. Despite its affordability, the chip offers impressive features such as 2KB SRAM, 16KB flash, multiple timers, UART, I²C, and SPI.
However, developing for the CH32V series comes with a caveat: it uses a custom RISC-V toolchain and OpenOCD fork provided by WCH, meaning mainline GCC and OpenOCD do not support it out of the box. This blog walks you through building and flashing firmware for the CH32V003 in a Linux environment, without relying on the proprietary MountRiver IDE.


Hardware preparation

  • Qinheng CH32V003F4P6 development board
  • WCH-LinkE

Toolchain setup

Software Download from http://mounriver.com/download MRS_Toolchain_Linux_x64_V1.60.tar.xz, note that you need to use V1.60, the old version does not support CH32V003.

The current CH32V series development cannot use the public version RISC-V GCC and the public version of OpenOCD, because it contains the customization part of Qinheng, which is not supported in the open source project backbone.

After unzipping the toolchain

  • Refer to beforeinstall/start.sh to add the dynamic link library file and udev rule file.
  • Move the toolchain to an approriate location and change the owner to root to avoid accidental modifications(chown command) Export the project template:
    1
    
    host$ git clone https://gitee.com/iosetting/ch32v003-template.git
    

    Modify the configuration in the makefile based on your local environment

    1
    2
    
    host$ TOOL_CHAIN_PATH ?= /opt/gcc-riscv/riscv-wch-embedded-gcc-v1.60/bin
    host$ OPENOCD_PATH    ?= /opt/openocd/wch-openocd-v1.60/bin
    

    Compile and burn

    1
    2
    3
    4
    5
    
    host$ make clean
    host$ make
    host$ make flash
    host$ make erase
    host$ make reset
    

Compilation Params

The basic parameters of CH32V003 compilation are as follows, note that march and mabi, and CH32V103 are not the same, they are not run after compiling and writing with CH32V103 parameters, which are extracted from the compilation commands actually executed in MRS

1
2
3
4
5
6
7
8
9
10
11
CCFLAGS := -march=rv32ec \
           -mabi=ilp32e \
           -msmall-data-limit=0 \
           -msave-restore \
           -Os \
           -fmessage-length=0 \
           -fsigned-char \
           -ffunction-sections \
           -fdata-sections \
		   -fno-common \
           -Wunused -Wuninitialized -g
This post is licensed under CC BY 4.0 by the author.