I have decided to make this post about setting up a functional minimum system (AKA Blinky) with WCH CH32V003 RISC-V MCU, WCH LinkE in-circuit debugger/programmer and MounRiver IDE to help others starting with these MCU's. I am not a total newbie myself, however have had to spend some time surfing the Internet and digging out bits and pieces of the necessary information. Below is a compilation of my reasoning, steps and useful links I have collected so far.
Some discussions on the CH32V003
*
https://www.eevblog.com/forum/microcontrollers/wch-$0-10-risc-v-mcu/
*
https://www.eevblog.com/forum/microcontrollers/risc-v-from-wch-looks-interesting/*
https://www.reddit.com/r/RISCV/comments/y2nw0f/ch32v003_price_is_less_than_010/1. Which evaluation board and package option?
Evaluation boards are plenty and nice. However, they are based on TSSOP20/QFN20 package options. It is nice packages otherwise, provide the full set of outputs. First, it is hard to solder these with bare hands and naked eye free standing in a space without pads on a PCB. Second, in case of an unrecoverable MCU damage there is no way to replace the soldered down chip—there is no necessary tools in my possession currently apart from a simple soldering iron.
Considering the above I have decided to go with a bare packaged MCU to lower the initial costs of the enterprise and to stay on the safe side in case of an unrecoverable damage to the MCU. The SOP8 option has too few pins. The pins are highly multiplexed. Hence, CH32V003A4M6—the SOP16 option is a go. I wish there would be an old good DIP20 option as well.
CH32V003 product page—https://www.wch-ic.com/products/CH32V003.html
Dave's video featuring a popular evaluation board—https://youtube.com/watch?v=L9Wrv7nW-S8
One more popular evaluation board—https://github.com/wuxx/nanoCH32V003
2. Where to source from?
https://www.lcsc.com/mobile/search?q=Ch32v003—payment/shipment impediments in my current region
WCH Official Store—https://m.aliexpress.com/item/1005005036714708.html. All available package options. Minimum order quantity is of 50 pcs. 50 is too much for a little project like this.
W Official Store—https://m.aliexpress.com/item/1005005262154401.html. Same as the above, but the minimum order quantity is of 10 pcs. It's a go.
3. Which flashing protocol and in-circuit debugger-programmer?
There are many different kinds of WCH and non-WCH adapters available with different pricing all over the place—which does the job? Thanks to Patrick Yang's clarification (see the link below) CH32V003 uses a single-wire debugging/programming protocol supported by WCH LinkE (note the E in the product name, it is critical).
In my case the best offer price- and shipping-wise was by W Official Store again—https://m.aliexpress.com/item/1005005180653105.html
WCH Link product page—https://www.wch-ic.com/products/WCH-Link.html
WCH official evaluation board and LinkE kit—https://m.aliexpress.com/item/1005005269690018.html
Video of Patrick Yang talking about WCH single-wire debugging/programming protocol (see the video comments section as well for more insightful information)—https://youtube.com/watch?v=k1RkcYOANLM. I find the protocol very novel.
WCH single-wire debugging/programming protocol specification—https://github.com/openwch/ch32v003/blob/main/RISC-V%20QingKeV2%20Microprocessor%20Debug%20Manual.pdf
WCH single-wire debugging/programming protocol open implementation—https://github.com/openwch/ch32v003/tree/main/CH32V003_1Line_Base_on_CH32F103
Based on the above reasoning I have ordered and received WCH CH32V003A4M6, 10 pcs and WCH LinkE, 1 pcs. I got a WCH LinkE-R0-1v3 delivered in July, 2023. It is based on WCH CH32V305FBP6. The build quality is high. It seems the PCB designer paid attention layering out the USB D+ and D- differential pair traces with the correct impedance (of course it's by the looks only).
4. Which wire connections to make?
The wire connections in the order of execution are as follows.
Power supply bypass capacitor. Bend and form the leads of a ceramic capacitor of 0,1uF in such a way that it can be easily soldered under the pin 14 (VSS) and pin 15 (VDD) of SOP16 package without giving any mechanical stress to the pins (see the photo below). I had a 0,47uF laying around and used it.
Serial output connection. Cut appropriately and solder a wire to the pin 10 (PD6) of SOP16 package. Connect it to the pin RX of WCH LinkE. This line will output the serial data from the USART in CH32V003 MCU, e. g. ASCII characters produced by a function printf call. WCH LinkE has a handy built-in serial-to-USB function seen as a virtual COM-port by the operating system on a computer. MounRiver IDE has a terminal window, which can display the ASCII characters received over the serial link (see later).
LED and current limiting resistor. Observe the polarity soldering the LED. Solder the anode lead to the pin 9 (PD5) of SOP16 package. Solder the current limiting resistor of 1,5 kOhm to the cathode lead and the pin 14 (VSS) of SOP16 package. This way the LED will light up when PD5 drives the pin with the logic level high.
Power supply connection. Cut appropriately and solder wires to the power supply bypass capacitor leads. Connect the wire going to the pin 14 (VSS) of SOP16 package to the pin GND of LinkE. Connect the wire going to the pin 15 (VDD) of SOP16 package to the pin 3V3 of LinkE. These two wires will provide electrical power from LinkE to the MCU to run.
Single-wire debugging/programming connection. Cut appropriately and solder a wire to the pin 7 (PD1/SWIO) of SOP16 package. Connect the wire to the pin SWDIO/TMS of LinkE.
That is it. The functional minimum system is complete and ready, see the photo of my in-air-wire prototype. CH32V003 has an internal RC-oscillator and will run on it just fine.
CH32V003 datasheet for the pinout and detailed product information—https://www.wch-ic.com/downloads/CH32V003DS0_PDF.html
5. Which tool chain (compiler, assembler, linker, debugger), IDE and flashing utility?
MounRiver Studio based on Eclipse IDE with an integrated Open OCD can be used to develop code, flash and debug CH32V003 through WCH LinkE. It provides a complete tool chain required.
Download—http://mounriver.com/download. Make sure to download a full setup ZIP, not an update only. There is an option to select the English language on the site.
(Windows OS) Before the installation plug in LinkE to an USB port. The necessary drivers will be installed by MounRiver setup automatically.
In my case I had both LEDs red and blue on on WCH LinkE board initially. When the blue is on, the ARM mode is on. It is bad. As seen on the Internet, run WCH-LinkUtility found in the installation folder under ExTool\SWDTool, choose a WCH Link in the drop-down list (see the bottom part of the utility window), choose the mode WCH-LinkRV in the drop-down list below and click the button Set. There should be a success message and the blue being off. Now go to the menu Target and select Query Chip Info, which should update the table with the MCU UID and flash memory size in the right top corner of the utility window indicating WCH LinkE, its drivers, single-wire debugging/programming and power connections work fine.
6. Getting Blinky and serial output to terminal window
There is a supplemental documentation of the WCH official evaluation board provided along with the sample code—https://www.wch.cn/downloads/CH32V003EVT_ZIP.html.
There is EXAM\GPIO\GPIO_Toggle example, which blinks an LED infinitely.
In MounRiver go to the menu File, Open Projects from File System, point to the above-mentioned example and then click the button Finish.
Now do Project, Build All. It should build with 0 errors.
Have I used the official evaluation board with its connections, I would have not needed to change anything in the sample code. Since I have decided to use different pins for soldering convenience some changes in the code are needed.
LED is connected to the pin 9 (PD5) instead of PD0 in my case.
* change GPIO_Pin_0 to Pin_5 in the function GPIO_Toggle_INIT in main.c
* change GPIO_Pin_0 to Pin_5 in the arguments of the function GPIO_WriteBit call inside the infinite while-loop in the function main in main.c
These two changes will make the LED blinking. Do Project, Build All. Make sure it builds with 0 errors. Now do Run, Run As, OpenOCD MRS Debugging. The later will flash the code to the MCU and execute it. The LED should blink at the rate of 0,5 sec.
I have connected the pin 10 (PD6) to the pin RX of LinkE. It needs an MCU pin remapping to get the internal USART output TX on the chosen pin. The MCU register PCFR1 remaps the USART—the value in the bits 21 and 2, see the reference manual (link below). Locate USART_Printf_Init, in the function main in main.c, select the whole name, right click on it, do Open Declaration. Add the following line in the end of the function USART_Printf_Init just before the closing }:
AFIO->PCFR1 |= 1<<21;
Now find above the added line GPIO_Pin_5 and change it to Pin_6. Do Build All. It should build with 0 errors.
Do Window, Show View, Terminal. It will add a new tab in the bottom of the main MounRiver window. Now click the button Open a Terminal (it is on the same level as the tab Terminal, on the right). In the popped up dialog, choose Serial Terminal, the serial port assigned to LinkE (COM4 in my case) and click the button OK.
Now do Run, Run. There should the readable text printed in the tab Terminal.
Add a call to the function printf inside the infinite while-loop in the function main in main.c to get more printed to the terminal:
printf("Happy toggling. Ho ho ho!\r\n");
Build and run.
There you have it—a functional minimum system running on a RISC-V MCU.
CH32V003 reference manual—https://www.wch-ic.com/downloads/CH32V003RM_PDF.html
QuinKe V2 Processor Manual—https://www.wch-ic.com/downloads/QingKeV2_Processor_Manual_PDF.html
Github—https://github.com/openwch/ch32v003/
7. RISC-V ISA, Assembly, pseudoinstructions and fun
Over the past few years I wanted to have something inexpensive capable of executing the RISC-V Assembly and finally there I have it—the WCH CH32V003 MCU thanks to WCH! Now it is happy RICS-V Assembly hands-on coding time…
Online resources
https://en.m.wikipedia.org/wiki/RISC-VRISC-V ISA specifications—https://riscv.org/technical/specifications/
RISC-V greencard (reference card)—http://riscvbook.com/greencard-20181213.pdf
Andrew Waterman’s thesis—https://www2.eecs.berkeley.edu/Pubs/TechRpts/2016/EECS-2016-1.html
RISC-V Reader book (beta)
https://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/books/rvbook.pdfhttps://github.com/Lingrui98/RISC-V-bookRISC-V Assembly, pseudoinstructions
https://riscv-programming.org/book/riscv-book.htmlhttps://github.com/johnwinans/rvalphttps://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.mdYouTube (plenty)
https://youtube.com/watch?v=ROkjMXuR2u8Books
Computer Organization and Design RISC-V Edition, David Patterson et al.
The RISC-V Reader: An Open Architecture Atlas, David Patterson et al.
http://riscvbook.comRISC-V Assembly Language, Anthony J. Dos Reis
If you have any other useful links and resources on the subject, do share please. The more information, the better! Any input is welcome!
Hope this helps!