CH32V003 Functional minimum system - Page 1
Forum section: electronics / general

author: Bambur
author_rank: Regular Contributor
timestamp: August 01, 2023, 07:48:49 pm
content: 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.htmlDave's video featuring a popular evaluation board—https://youtube.com/watch?v=L9Wrv7nW-S8One more popular evaluation board—https://github.com/wuxx/nanoCH32V0032. Where to source from?https://www.lcsc.com/mobile/search?q=Ch32v003—payment/shipment impediments in my current regionWCH 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.htmlWCH Link product page—https://www.wch-ic.com/products/WCH-Link.htmlWCH official evaluation board and LinkE kit—https://m.aliexpress.com/item/1005005269690018.htmlVideo 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.pdfWCH single-wire debugging/programming protocol open implementation—https://github.com/openwch/ch32v003/tree/main/CH32V003_1Line_Base_on_CH32F103Based 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.html5. 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 windowThere 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.cThese 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.htmlQuinKe V2 Processor Manual—https://www.wch-ic.com/downloads/QingKeV2_Processor_Manual_PDF.htmlGithub—https://github.com/openwch/ch32v003/7. RISC-V ISA, Assembly, pseudoinstructions and funOver 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 resourceshttps://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.pdfAndrew Waterman’s thesis—https://www2.eecs.berkeley.edu/Pubs/TechRpts/2016/EECS-2016-1.htmlRISC-V Reader book (beta)https://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/books/rvbook.pdfhttps://github.com/Lingrui98/RISC-V-bookRISC-V Assembly, pseudoinstructionshttps://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=ROkjMXuR2u8BooksComputer 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 ReisIf 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!

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 01, 2023, 11:19:51 pm
content: Quote from: Bambur on August 01, 2023, 07:48:49 pmThat's pushing 'minimum'Another package that could manage this, is the TQFP-32. Not showing on the CH32V yet, but other vendors are offering the TQFP-32

author: brucehoult
author_rank: Super Contributor
timestamp: August 01, 2023, 11:43:33 pm
content: Quote from: Bambur on August 01, 2023, 07:48:49 pmIt's only $5 to $7 for 50 chips, depending on which package! Plus $5 shipping (to New Zealand).Quote15c-20c each instead of 10c-14c each. Sure, you save a couple of bucks, with $1.40-$2 for the 10 chips. Their shipping is $3.50 cheaper too.So for the 16 pin package it's $3.38 for 10 vs $12.08 for 50, delivered.But you still need to buy the SWI programmer.The $5.51 (plus $6.24 shipping) kit with a dev board, WCH-LinkE programmer, and five 16 pin bare chips is a good way to get started, with your preferred bare chip package too:https://www.aliexpress.us/item/1005004895791296.htmlYou gave a link for what looks like the same thing at "W Official Store", which is also your source for 10 chip packs. I don't know what that is!! It's cheaper than the WCH official store. But who are they? It is genuine stuff?"W" is slower delivery than official "WCH", which helps explain the cheaper shipping at least, maybe. WCH store I'm seeing Sep 19 delivery date, "W" October 2.Anyway, none of it is break-the-bank stuff.

author: Bambur
author_rank: Regular Contributor
timestamp: August 02, 2023, 02:03:00 pm
content: Bruce, thank you the reply! Are you the Bruce programming everything youcan think of since the late 70s? I am so excited you have picked up on a post of mine. I thank you for all your posts on the forums in the Internet! I read them with a great interest always.I don’t mean to argue your points above, but personally I wanted to spend/invest as little money as possible to this project initially, since I wasn’t 100% sure of the success. That’s why the way as described above. For my particular case it was the cheapest. Your mileage may vary.I don’t have high expectations being it a WCH Official Store, W Official Store or XYZ Self-Proclaimed Genuine Store at AliExpress. What I have received looks genuine enough for me in the scope of this little project. Everything has worked as expected so far. They positively surprised me with the delivery time. The package was delivered to my region in 12 days instead of an initial long delivery time of 1,5 month promised. For a large commercial project I would go a different way probably.Bruce, I want to use this opportunity and ask a question from you. What is a good starting point to learn GNU tool chain internals, Open OCD?

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 02, 2023, 08:49:56 pm
content: Can't understand why the CH32V003 hype is still going on when the py32f002 is cheaper, has twice ram & flash, more peripherals and infinitely more standarized (Cortex-M0+, SWD, simple uart programming).

author: brucehoult
author_rank: Super Contributor
timestamp: August 02, 2023, 11:13:17 pm
content: Quote from: DavidAlfa on August 02, 2023, 08:49:56 pmWhere? Assuming you're not buying 10,000 of them?Buying 10 looks several times more expensive, at least in the places I could find e.g 40c each here...https://www.aliexpress.us/i/1005005222571006.html$1.05 here...https://www.aliexpress.us/i/1005004843962870.htmlQuotePoint of fact: 50% more RAM and 25% more flash.QuoteProprietary ISA, not a standard.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 02, 2023, 11:33:00 pm
content: Lcsc? Starting from 11 cents buying 5.https://www.lcsc.com/product-detail/Microcontroller-Units-MCUs-MPUs-SOCs_PUYA-PY32F002AF15P6TU_C5292059.htmlStandard and propietary means nothing, right?It's about how well accepted something is in the industry, thus ensuring a broad ecosystem, and sure enough arm is everywhere, I can get anything at github made for a stm32 or similar mcu and easily port it unless using some specific peripheral.50% more RAM and 25% more flash "officially", but they're py32F030x6 dies, so they have the full 32/4KB and a bunch of extra peripherals.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 02, 2023, 11:42:21 pm
content: Quote from: DavidAlfa on August 02, 2023, 11:33:00 pm... but you cannotdesign tothat 'oh, look' nature - you must use the data sheet numbers, unless you are pure hobbist, then price does not matter at all..Many vendors release larger die, whilst the more-cost-effective die is being ramped.NXP had some tiny-die/package 'aren't we clever' parts that have faded.Other el-cheapo parts have appeared on lcsc, to vanish a few months later.

author: brucehoult
author_rank: Super Contributor
timestamp: August 03, 2023, 12:23:16 am
content: Quote from: DavidAlfa on August 02, 2023, 11:33:00 pmCool. $0.62 for the product. Plus $3 handling fee, adding $0.60 per chip. Plus $4.17 cheapest shipping.Comes to about the same total buying 5 as for buying 10 from the Aliexpress link I gave above. And more than twice the price of buying 10 CH32V003 SOP16 ($3.38 incl shipping) at the link Bambur gave.https://m.aliexpress.com/item/1005005262154401.htmlI'm sure it's a great chip. But you're not being very successful in making the case that it's *cheaper*. At least in minimum quantities.QuoteMaybe the ones you got last month were, but there's zero guarantees the ones you buy next month will be.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 03, 2023, 12:49:01 am
content: But did you even read this thread?He's already complaining about buying 50 so this is purely diy level.Brucehold, asian imports are gone as we knew them.Forget those times where something was $1 + free shipping.The minimum is $13 for no handling fees, then $4.5 shipping.50 py32, a bunch of passives and jellybean parts, and you'll be there.I got 20 and if the new boards work as planned I'll probably buy another 150 or so, it'll be $20, beat that!Aliexpress is a stealer, very often every microgram mcu adds $0.20.This one sells lots but I would ask first for numbers!https://a.aliexpress.com/_EJPtW0R

author: brucehoult
author_rank: Super Contributor
timestamp: August 03, 2023, 06:34:19 am
content: Quote from: DavidAlfa on August 03, 2023, 12:49:01 amYes. And it's about CH32V003, not PUYA.QuoteYes, we're talking about DIY level.QuoteAccording to the "My Orders" page, I have made nine orders there since November.No problems at all.

author: mikerj
author_rank: Super Contributor
timestamp: August 03, 2023, 11:51:18 am
content: Quote from: brucehoult on August 03, 2023, 12:23:16 amHow are MOQs even relevant for such low cost devices? The only compelling reason to choose such devices over better documented and better supported options are the cost savings in volume. Selecting these devices for one off hobby projects is nonsense unless you are some kind of masochist.

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 03, 2023, 11:56:32 am
content: The py32 is greatly documented, has a decent sdk full of examples and a nice library (99.9% stolen from ST).Until now developing for it it's been a child's game.Yeah I though the same at first, bit I was greatly surprised.

author: asmi
author_rank: Super Contributor
timestamp: August 03, 2023, 02:26:11 pm
content: Quote from: DavidAlfa on August 02, 2023, 08:49:56 pmBecause it's an open RISC-V, not closed ARM. It's not about RAM, flash, peripherals or anything else. If you don't understand why is that such a big deal, you can get on with your support for IP theft, but not in this thread.

author: Bambur
author_rank: Regular Contributor
timestamp: August 03, 2023, 03:20:30 pm
content: Quote from: asmi on August 03, 2023, 02:26:11 pmExactly! Because it is RISC-VI may recommend to read the book The RISC-V Reader: An Open Architecture Atlas by David Patterson and Andrew Waterman. They cheat a bit here and there making the RISC-V to look better than it actually is, but they are pretty much on the point when it comes to pro's and con's.The RISC-V Reader: An Open Architecture Atlashttps://www.amazon.com/RISC-V-Reader-Open-Architecture-Atlas/dp/0999249118Free online beta-versionhttps://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/books/rvbook.pdf

author: DavidAlfa
author_rank: Super Contributor
timestamp: August 04, 2023, 04:21:43 am
content: Quote from: asmi on August 03, 2023, 02:26:11 pmHummm there's a Spanish quote that would translate as "Hearing the bell, but not knowing where".That's pretty much what's going on here.I said nothing about:- IP theft.- Long term support.- Production.- Made with newborns's bones to save packaging costs.DIY target doesn't care about all that, just put both in the balance and find out which one provides more bang for the buck and provides easiest/best support.

author: asmi
author_rank: Super Contributor
timestamp: August 04, 2023, 01:08:11 pm
content: Quote from: DavidAlfa on August 04, 2023, 04:21:43 amYes you did.Quote from: DavidAlfa on August 04, 2023, 04:21:43 amSpeak for yourself please. And wait until YOUR IP gets stolen, we'll see how are you going to feel about that then.

author: shabaz
author_rank: Frequent Contributor
timestamp: February 11, 2024, 07:10:56 am
content: Hi,I think there's a slight error in the original post.The text:Serial output connection. Cut appropriately and solder a wire to the pin 10 (PD6) of SOP16 package.should read (I believe):Serial output connection. Cut appropriately and solder a wire to the pin 9 (PD5) of SOP16 package.If this is incorrect, please let me know and I'll delete this message to prevent confusion for anyone following the original post. But I'm 95% certain (although it's late and I may have made an error!).EDIT: Looks like the original post is correct, since the OP is re-mapping the UART pins in the code, away from the default.