openwch / ch32v003

CH32V003 is an ultra-cheap RISC-V MCU with 2KB SRAM, 16KB flash, and up to 18 GPIOs that sells for under $0.10
386 stars 56 forks source link

How is the flash programmed? #3

Open jonsmirl opened 2 years ago

jonsmirl commented 2 years ago

Does CH32V003 the on-chip boot code have the ability to flash the device, or is the only way to flash a new CH32V003 is via SWD? We would like to attach this chip to a larger CPU and allow that larger CPU to update the flash in the CH32V003.

fukunlab commented 1 year ago

Unfortunately WCH_Link (SDI) and BOOT protocol is not open, WCH_Link (SDI) but not SWD is the only way to flash CH32V003.

jonsmirl commented 1 year ago

We are using OTP chips currently. I wanted to evaluate switching to this chip to allow updating of our code. But we can't achieve that without the larger host CPU having some way to reflash the chip while in-circuit. Manufacturing is also an issue, the host CPU needs to initially flash the chip.

jonsmirl commented 1 year ago

Apparently Mounriver has released the protocol. https://github.com/kprasadvnsi/riscv-openocd-wch But this is not in a form where it can be used by a host CPU. Additional work is needed to extract out a minimal flash programming library.

martinribelotta commented 1 year ago

A reverse inginering of WCH (ch32v) SWD protocol is made by @fxsheep here https://github.com/fxsheep/openocd_wchlink-rv/wiki/WCH-RVSWD-protocol The flash algorithm for varios wch parts was found in @kprasadvnsi repo on https://github.com/kprasadvnsi/riscv-openocd-wch/blob/master/src/jtag/drivers/wlink.c#L110-L337 Maybe a new openocd with ch32v003 support is required but the MounRiver guys are receptive if you send an e-mail to support@mounriver.com But this require a lot of work for obtain a full floss toolchain to work...

@jonsmirl In my view, you have two ways to go:

jonsmirl commented 1 year ago

This is for a commercial product, so we don't have the luxury of spending a lot of time on open source code development. If there is no simple solution to the host being able to flash the chip we'll just continue using Padauk OTP parts which are not upgradable.

fukunlab commented 1 year ago

@jonsmirl So the product is

1. using a large host CPU is MCU to flash slave CH32V003, the host and slave MCU on the same PCB board. And you want to use host CPU loading programmer to CH32V003 by ETH , WIFI etc. at real time . Ans: Write a private protocol ISP 2. The host CPU or MCU characters a load programmer tools. CH32V003 is the only MCU on the PCB board.
_Ans: Use WCHLink, data transfer just one-wire completed loading , thus three pad (VCC, GND, DATA)should be reserved on the PCB board. If there no PC to do that, WCH have a loading tools with out PC

martinribelotta commented 1 year ago

If there no PC to do that, WCH have a loading tools with out PC

That sound interesting... some plans to publish this with the regular HAL/Examples?

martinribelotta commented 1 year ago

An other cuestio:

Is ok if I make a reverse engineering of the CH32V3xx and CH32V003xx SWD/SDI protocol?

josuah commented 1 year ago

Here is another discussion with multiple engineers interested in an open-source tool-chain to program the chips:

https://www.eevblog.com/forum/microcontrollers/wch-$0-10-risc-v-mcu/?all

claudioolmedo commented 1 year ago

Is it possible to use another CH32V3xx made with serial communication firmware to record another CH32V3xx? If it's that cheap you can have two chips for low-cost denveloper boards. Without having to buy the official WCH-LinkE, can anyone confirm if it's possible?

jonsmirl commented 1 year ago

WCH Link clones are $1.20.
https://www.aliexpress.us/item/3256804314210866.html

fxsheep commented 1 year ago

Since RVSWD is a synchronous protocol, I think it can be implemented as a custom transport+ bitbang driver in OpenOCD and reuse existing bitbang SWD hardware. This possibly reduces cost to 0.

martinribelotta commented 1 year ago

From Patrik twitter: image

If you can wait, they will post the documentation of the 1-wire swd protocol

zoobab commented 1 year ago

There is now a simple libusb program minichlink to flash it:

https://github.com/cnlohr/ch32v003fun

andelf commented 1 year ago

Checkout https://github.com/ch32-rs/wlink 😝

Dreagonmon commented 1 year ago

It seems the recent commit contains an example implement of using the one-wire procotol: https://github.com/openwch/ch32v003/tree/main/CH32V003_1Line_Base_on_CH32F103

perigoso commented 1 year ago

Checkout https://github.com/ch32-rs/wlink stuck_out_tongue_closed_eyes

ooh that might be handy, I'm planning to implement this debug protocol in Black Magic Debug, see blackmagic-debug/blackmagic/pull/1399, though for now I'm only working on the undocumented RVSWD (see also https://perigoso.github.io/rins and https://github.com/perigoso/sigrok-rvswd for RVSWD related things)

yrabbit commented 1 year ago

WCH Link clones are $1.20. https://www.aliexpress.us/item/3256804314210866.html

These are indeed cheap clones, but they are Link clones that cannot program the ch32v003 - you need LinkE for that

krasin commented 1 year ago

FIY: https://github.com/aappleby/PicoRVD is an example of how to flash CH32V003 from an RP2040. The code is fully open-source and should theoretically be adaptable to other uses, such as a host MCU flashing a peripheral CH32V003.

mrx23dot commented 1 year ago

Any WCH-Link alternatives support debugging over openocd? The official WCH-Link is slow as hell.

qiyongshuang commented 10 months ago

I noticed that there is an USART_IAP program in EVT(CH32V003EVT\EVT\EXAM\USART_IAP).The program is placed in the BOOT area. We can use the USART port to write Flash.