├── .gitmodules ├── LICENSE ├── README.md ├── assets ├── erase_by_nrst.png ├── erase_by_poweroff.png └── select_ch32v30x.png ├── firmwares ├── Firmware_Link.old │ ├── FIRMWARE_CH32V203.bin │ ├── FIRMWARE_CH32V305.bin │ ├── FIRMWARE_CH549.bin │ ├── FIRMWARE_DAP_CH549.bin │ ├── WCH-DAPLink_APP_IAP.bin │ ├── WCH-LinkE-APP-IAP.bin │ ├── WCH-Link_APP_IAP_ARM.bin │ ├── WCH-Link_APP_IAP_RV.bin │ ├── firmware_version.txt │ ├── flash-wch-linke.sh │ └── wchlink.wcfg └── Firmware_Link │ ├── FIRMWARE_CH32V203.bin │ ├── FIRMWARE_CH32V208.bin │ ├── FIRMWARE_CH32V305.bin │ ├── FIRMWARE_CH549.bin │ ├── FIRMWARE_DAP_CH549.bin │ ├── WCH-DAPLink_APP_IAP.bin │ ├── WCH-LinkE-APP-IAP.bin │ ├── WCH-LinkW-APP-IAP.bin │ ├── WCH-Link_APP_IAP_ARM.bin │ ├── WCH-Link_APP_IAP_RV.bin │ ├── firmware_version.txt │ ├── flash-wch-linke.sh │ └── wchlink.wcfg └── wch-riscv.cfg /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/.gitmodules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 cjacker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Opensource toolchain for WCH CH32V RISC-V 32bit MCU 2 | 3 | WCH CH32V series is a family of General-Purpose 32bit RISC-V MCU, please refer to [official website](http://www.wch-ic.com/products/categories/47.html?pid=5) for more infomations. 4 | 5 | # Table of contents 6 | - [Hardware prerequist](#hardware-prerequist) 7 | - [Toolchain overview](#toolchain-overview) 8 | - [RISC-V GNU Toolchain](#risc-v-gnu-toolchain) 9 | + [Building from source](#building-from-source) 10 | + [Use prebuilt toolchain](#use-prebuilt-toolchain) 11 | - [SDK](#sdk) 12 | + [For CH32V](#for-ch32v-evt-packages) 13 | + [For CH5XX RISC-V BLE](#for-ch5xx-risc-v-ble) 14 | - [Programming](#programming) 15 | + [ISP programming](#isp-programming) 16 | + [WCH-LinkE programming](#wch-linke-programming) 17 | - [Debugging](#debugging) 18 | - [Project templates](#project-templates) 19 | + ch32v003evt 20 | + ch32x035evt 21 | + ch32v103evt 22 | + ch32l103evt 23 | + ch32v20xevt 24 | + ch32v307evt 25 | + ch573evt 26 | + ch583evt 27 | + ch585evt 28 | + ch592evt 29 | - [How to rescue a bricked CH32V and WCH-LinkE](#how-to-rescue-bricked-ch32v) 30 | - [How to update firmware of WCH-Link/E](#how-to-update-firmware-of-wch-linke) 31 | 32 | # Hardware prerequist 33 | 34 | * CH32V/X/L board 35 | * WCH-LinkE(ch32v305fbp6 based) or WCH-Link(ch549 based), with latest firmware 36 | 37 | **Note:** WCH-Link does not support 1-wire SDI interface used by ch32v003, you need a WCH-LinkE to program/debug ch32v003. 38 | 39 | WCH have an official online store on AliExpress, you can buy the EVT boards and WCH-LinkE from it. 40 | 41 | # Toolchain overview 42 | 43 | * Compiler: gcc 44 | * SDK: WCH official EVT source package 45 | * Programmer: 46 | - [wchisp](https://github.com/ch32-rs/wchisp) for ISP mode 47 | - [wlink](https://github.com/ch32-rs/wlink) for WCH-LinkE. 48 | - [WCH OpenOCD](https://github.com/cjacker/wch-openocd) for WCH-LinkE. 49 | * Debugger: WCH OpenOCD and gdb 50 | 51 | # RISC-V GNU Toolchain 52 | 53 | CH32V is well supported by [riscv-gnu-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain). 54 | 55 | The RISC-V GNU toolchain, which contains compilers and linkers like gcc and g++ as well as helper programs like objcopy and size is available from [riscv-gnu-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain). There are also some prebuilt release provided by nuclei or other teams, such as 'xpack', so you can either build it yourself or download a prebuilt release. 56 | 57 | ## Building from source 58 | 59 | If you want to use a prebuilt release, just ignore this section. 60 | 61 | Building a cross compile gnu toolchain was difficult long time ago, you need to understand and use configuration options very carefully. [riscv-gnu-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain) provided a simpler way to help us building a workable toolchain. It supports two build modes: a generic ELF/Newlib toolchain and a more sophisticated Linux-ELF/glibc toolchain. we only need the 'generic ELF/Newlib toolchain' for CH32V. 62 | 63 | ``` 64 | git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git 65 | cd riscv-gnu-toolchain 66 | mkdir build 67 | cd build 68 | ../configure --prefix=/opt/riscv-gnu-toolchain --disable-linux --with-abi=ilp32 --with-arch=rv32imac 69 | make -j 70 | make install 71 | ``` 72 | **NOTE**, for ch32v003, you should build gcc with `--with-abi=ilp32e --with-arch=rv32ec_zicsr`. 73 | 74 | It will be installed to "/opt/riscv-gnu-toolchain" dir, and the [target triplet](https://wiki.osdev.org/Target_Triplet) of gcc should be 'riscv32-unknown-elf-'. 75 | 76 | Since the prefix is not set to standard dir, you need add '/opt/riscv-gnu-toolchain/bin' to PATH env. for example, for bash, add it to ~/.bashrc: 77 | ``` 78 | export PATH=/opt/riscv-gnu-toolchain/bin:$PATH 79 | ``` 80 | 81 | ## Use prebuilt toolchain 82 | 83 | ### MRS toolchain 84 | MounRiver Studio (WCH official IDE) provide a standalone linux toolchain that you can use directly, especially for some chips with WCH private RISCV instructions, for example CH584/585, you have to use [MRS toolchain](http://www.mounriver.com/download). Up to now, the currect version is v1.92.1, you can download it from [here](http://file-oss.mounriver.com/tools/MRS_Toolchain_Linux_x64_V1.92.1.tar.xz). 85 | 86 | Download and extract it as: 87 | ``` 88 | wget http://file-oss.mounriver.com/tools/MRS_Toolchain_Linux_x64_V1.92.1.tar.xz 89 | sudo mkdir -p /opt/MRS_toolchain 90 | sudo tar xf MRS_Toolchain_Linux_x64_V1.92.1.tar.xz -C /opt/MRS_toolchain --strip-components=1 91 | ``` 92 | 93 | And add `/opt/MRS_toolchain/RISC-V_Embedded_GCC12/bin` to your PATH env. 94 | 95 | **NOTE**, the target triplet of MRS toolchain is **`riscv-none-elf`**. 96 | 97 | 98 | ### xpack riscv toolchain 99 | [xpack-dev-tools](https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/) provde a prebuilt toolchain for riscv. you can download it from https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/. The lastest version is '14.2.0', download and extract it as: 100 | 101 | ``` 102 | sudo mkdir -p /opt/xpack-riscv-toolchain 103 | sudo tar xf xpack-riscv-none-elf-gcc-14.2.0-2-linux-x64.tar.gz -C /opt/xpack-riscv-toolchain --strip-components=1 104 | ``` 105 | 106 | And add `/opt/xpack-riscv-toolchain/bin` to PATH env according to your shell. 107 | 108 | **NOTE**, the target triplet of xpack riscv toolchain is **`riscv-none-elf`** and it can support ch32v003 development. 109 | 110 | # SDK 111 | 112 | If you used to do stm32 programming, you will feel very familiar with CH32V SDK. 113 | 114 | WCH provide evaluate source code package for every CH32V MCU model and licensed under Apache-2.0, you can download them from WCH official website according to your MCU model. 115 | 116 | For ch32v003, https://www.wch.cn/downloads/CH32V003EVT_ZIP.html 117 | 118 | For ch32x035, https://www.wch.cn/downloads/CH32X035EVT_ZIP.html 119 | 120 | For ch32v103, http://www.wch.cn/downloads/CH32V103EVT_ZIP.html. 121 | 122 | For ch32l103, https://www.wch.cn/downloads/CH32L103EVT_ZIP.html 123 | 124 | For ch32v20x, https://www.wch.cn/downloads/CH32V20xEVT_ZIP.html 125 | 126 | For ch32v30x, http://www.wch.cn/downloads/CH32V307EVT_ZIP.html 127 | 128 | For ch571/573, https://www.wch.cn/downloads/CH573EVT_ZIP.html 129 | 130 | For ch581/582/583, https://www.wch.cn/downloads/CH583EVT_ZIP.html 131 | 132 | For ch584/585, https://www.wch.cn/downloads/CH585EVT_ZIP.html 133 | 134 | For ch591/592, https://www.wch.cn/downloads/CH592EVT_ZIP.html 135 | 136 | These evt packages contains core SDK and a lot of demo routines but lack Makefile support, here provide [ch32v evt project template and convertor](https://github.com/cjacker/ch32v_evt_makefile_gcc_project_template) and [ch5xx evt project template and convertor](https://github.com/cjacker/ch5xx_riscv_ble_evt_makefile_gcc_project_template) to convert EVT packages to makefile project. 137 | 138 | ## For CH32V EVT Packages 139 | 140 | ``` 141 | git clone https://github.com/cjacker/ch32v_evt_makefile_gcc_project_template 142 | cd ch32v_evt_makefile_gcc_project_template 143 | ./generate_project_from_evt.sh 144 | ``` 145 | the `` can be obtained with running `./generate_project_from_evt.sh` without any args. 146 | 147 | Then type `make` to build the project. 148 | 149 | After building complete, you will get `build/.elf`, `build/.hex` and `build/part.bin`, which can be used for debugging and programming later. 150 | 151 | 152 | ## For CH5XX RISC-V BLE 153 | 154 | ``` 155 | git clone https://github.com/cjacker/ch5xx_riscv_ble_evt_makefile_gcc_project_template 156 | cd ch5xx_riscv_ble_evt_makefile_gcc_project_template 157 | ./generate_project_from_evt.sh 158 | ``` 159 | 160 | the `` can be obtained with running `./generate_project_from_evt.sh` without any args. 161 | 162 | Then type `make` to build the project. 163 | 164 | After building complete, you will get `build/.elf`, `build/.hex` and `build/.bin`, which can be used for debugging and programming later. 165 | 166 | **Note 1:** These demo only blink LED connect to PA8. 167 | 168 | **Note 2:** These evt packages are partial opensourced, there is no source of ISP static library. 169 | 170 | **Note 3:** **For ch584/585, you have to use [MRS toolchain](http://file-oss.mounriver.com/tools/MRS_Toolchain_Linux_x64_V1.92.1.tar.xz) to support 'mcpy' instruction.** 171 | 172 | # Programming 173 | 174 | ## ISP programming 175 | 176 | ISP programming doesn't need a WCH-LinkE adapter, it program the target device via USB port directly. 177 | 178 | ### with wchisp 179 | 180 | The best opensource WCH ISP tool is [wchisp](https://github.com/ch32-rs/wchisp), which support more parts than other opensource solutions. 181 | 182 | **Installation:** 183 | 184 | ``` 185 | git clone https://github.com/ch32-rs/wchisp.git 186 | cd wchisp 187 | cargo build --release 188 | sudo install -m0755 target/release/wchisp /usr/bin/wchisp 189 | ``` 190 | 191 | **Programming:** 192 | 193 | You need enter ISP mode first. please find the 'BOOT0' and 'RESET' button on your development board 194 | 195 | - hold the 'BOOT0' button down and power on the device (connect the device to USB port). 196 | 197 | or 198 | 199 | - connect your development board directly to Linux PC USB port. 200 | - hold the BOOT0 button down, press the RESET button then release it, after a while (about 1 second), release BOOT0 button. 201 | 202 | Then 203 | 204 | - run `lsusb`, you will find something like '4348:55e0 WinChipHead'. 205 | 206 | After enter ISP mode, you can program the target board as: 207 | 208 | ``` 209 | sudo wchisp flash build/ch32v.bin 210 | ``` 211 | 212 | You may need to press 'RESET' button to reset the board after programming. 213 | 214 | A forked version of [ch55xtool](https://github.com/karlp/ch552tool) can also support program WCH CH32V103/307, you can have a try yourself. 215 | 216 | ## WCH-LinkE programming 217 | 218 | CH32V103/203/208/305/307 use a proprietary 2-wire debugging interface named 'RVSWD' and requires 219 | a special (but very cheap) usb programmer/debugger named 'WCH-Link' or 'WCH-LinkE', and the software support was implemented in WCH forked OpenOCD as 'wlink' interface. 220 | 221 | When CH32V003 released, A new 1-wire interface named 'SDI' was introduced with CH32V003, Only 'WCH-LinkE' can support SDI interface. 222 | 223 | Before programming with WCH-LinkE, please wire up 'WCH-LinkE' with target board (pins as same as SWD). for CH32V003, only the 'PD1 / SWDIO' pin is needed. 224 | 225 | Since WCH-LinkE support dual mode (RV and DAP), please make sure your WCH-LinkE adapter is in RV mode. refer to next section to learn how to switch mode of WCH-LinkE with [wlink](https://github.com/ch32-rs/wlink). 226 | 227 | And if you encounter any problem when using WCH-LinkE, you should try to update its firmware first. Please refer to [How to update firmware of WCH-Link/E](#how-to-update-firmware-of-wch-linke) to update the firmware. 228 | 229 | ### with wlink 230 | 231 | [wlink](https://github.com/ch32-rs/wlink/) is a command line tool work with WCH-LinkE programmer/debugger. 232 | 233 | wlink features: 234 | - Flash firmware, support Intel HEX, ELF and raw binary format 235 | - Erase chip 236 | - Halt, resume, reset support 237 | - Read chip info 238 | - Read chip memory(flash) 239 | - Read/write chip register - very handy for debugging 240 | - Code-Protect & Code-Unprotect for supported chips 241 | - Enable or Disable 3.3V, 5V output 242 | - SDI print support, requires 2.10+ firmware 243 | - Serial port watching for a smooth development experience 244 | 245 | **Installation:** 246 | ``` 247 | cargo install --git https://github.com/ch32-rs/wlink 248 | ``` 249 | 250 | **WCH-LinkE mode switch:** 251 | 252 | Switch WCH-LinkE to RV mode: 253 | 254 | ``` 255 | wlink mode-switch --rv 256 | ``` 257 | 258 | **Programming:** 259 | ``` 260 | wlink flash firmware.bin 261 | ``` 262 | 263 | ### with OpenOCD 264 | 265 | `wlink` is good enough to work with WCH-Link/E, this section is not recommended. 266 | 267 | I put the latest source of WCH official OpenOCD [here](https://github.com/cjacker/wch-openocd). It can work with all known version of WCH-LinkE and support all WCH CH32V/L/X series MCUs. 268 | 269 | **Installation:** 270 | 271 | ``` 272 | git clone https://github.com/cjacker/wch-openocd 273 | cd wch-openocd 274 | 275 | ./configure --prefix=/opt/wch-openocd --program-prefix=wch- --enable-wlinke --disable-ch347 --disable-linuxgpiod --disable-werror 276 | 277 | make 278 | sudo make install 279 | ``` 280 | 281 | After installation finished, add '/opt/wch-openocd/bin' to PATH env. 282 | 283 | 284 | **Programming:** 285 | 286 | ``` 287 | # to erase all 288 | sudo wch-openocd -f wch-riscv.cfg -c init -c halt -c "flash erase_sector wch_riscv 0 last " -c exit 289 | # to program and verify 290 | sudo wch-openocd -f wch-riscv.cfg -c init -c halt -c "program xxx.hex\bin\elf verify" -c exit 291 | # to verify 292 | sudo wch-openocd -f wch-riscv.cfg -c init -c halt -c "verify_image xxx.hex\bin\elf" -c exit 293 | # to reset/resume 294 | sudo wch-openocd -f wch-riscv.cfg -c init -c halt -c "wlink_reset_resume" -c exit 295 | ``` 296 | 297 | # Debugging 298 | 299 | Launch OpenOcd to connect to the target device: 300 | ``` 301 | sudo wch-openocd -f wch-riscv.cfg 302 | ``` 303 | Open another terminal and run: 304 | ``` 305 | riscv-none-elf-gdb ./build/ch32v103.elf 306 | (gdb) target remote :3333 307 | Remote debugging using :3333 308 | _start () at Startup/startup_ch32v10x.S:15 309 | 15 j handle_reset 310 | (gdb) load 311 | Loading section .init, size 0x38 lma 0x0 312 | Loading section .vector, size 0x108 lma 0x38 313 | Loading section .text, size 0x211c lma 0x140 314 | Loading section .data, size 0x84 lma 0x225c 315 | Start address 0x00000000, load size 8928 316 | Transfer rate: 3 KB/sec, 2232 bytes/write. 317 | (gdb) 318 | ``` 319 | 320 | Then you can set breakpoints, run, step, continue as usual gdb. 321 | 322 | # Project templates 323 | 324 | The pre-converted project templates from WCH official EVT packages and supported parts: 325 | 326 | - [ch32v003evt_gcc_makefile](https://github.com/cjacker/ch32v003evt_gcc_makefile) 327 | + ch32v003j4m6 328 | + ch32v003a4m6 329 | + ch32v003f4u6 330 | + ch32v003f4p6 331 | - [ch32x035evt_gcc_makefile](https://github.com/cjacker/ch32x035evt_gcc_makefile) 332 | + ch32x035r8t6 333 | + ch32x035c8t6 334 | + ch32x035g8u6 335 | + ch32x035g8r6 336 | + ch32x035f8u6 337 | + ch32x035f7p6 338 | + ch32x033f8p6 339 | - [ch32v103evt_gcc_makefile](https://github.com/cjacker/ch32v103evt_gcc_makefile) 340 | + ch32v103c6t6 341 | + ch32v103c8u6 342 | + ch32v103c8t6 343 | + ch32v103r8t6 344 | - [ch32l103evt_gcc_makefile](https://github.com/cjacker/ch32l103evt_gcc_makefile) 345 | + ch32l103f8p6 346 | + ch32l103f8u6 347 | + ch32l103g8r6 348 | + ch32l103k8u6 349 | + ch32l103c8t6 350 | - [ch32v20xevt_gcc_makefile](https://github.com/cjacker/ch32v20xevt_gcc_makefile) 351 | + ch32v203f6p6 352 | + ch32v203g6u6 353 | + ch32v203k6t6 354 | + ch32v203c6t6 355 | + ch32v203f8p6 356 | + ch32v203f8u6 357 | + ch32v203g8r6 358 | + ch32v203k8t6 359 | + ch32v203c8t6 360 | + ch32v203c8u6 361 | + ch32v203rbt6 362 | + ch32v208gbu6 363 | + ch32v208cbu6 364 | + ch32v208rbt6 365 | + ch32v208wbu6 366 | - [ch32v307evt_gcc_makefile](https://github.com/cjacker/ch32v307evt_gcc_makefile) 367 | + ch32v303cbt6 368 | + ch32v303rbt6 369 | + ch32v303rct6 370 | + ch32v303vct6 371 | + ch32v305fbp6 372 | + ch32v305rbt6 373 | + ch32v307rct6 374 | + ch32v307wcu6 375 | + ch32v307vct6 376 | - [ch573evt](https://github.com/cjacker/ch573evt_gcc_makefile) 377 | + ch573 378 | + ch571 379 | - [ch583evt](https://github.com/cjacker/ch583evt_gcc_makefile) 380 | + ch583 381 | + ch582 382 | + ch581 383 | - [ch585evt](https://github.com/cjacker/ch583evt_gcc_makefile) 384 | + ch585 385 | + ch584 386 | - [ch592evt](https://github.com/cjacker/ch592evt_gcc_makefile) 387 | + ch592 388 | + ch591 389 | 390 | # How to rescue bricked CH32V 391 | 392 | **NOTE: below steps can also rescue a bricked WCH-LinkE.** 393 | 394 | If accidently programing wrong firmware to target board, the SWDIO/SWCLK pins may be occupied for other purpose, the target board may not be probed and programmed by WCH-LinkE anymore. 395 | 396 | Please try to activate ISP mode first, it may still work. If the target board don't have ISP port exported or ISP not works anymore, you have a bricked CH32V now... 397 | 398 | To rescue a bricked CH32V, you need to erase all code flash. 399 | 400 | ## Clear all code flash by pin NRST 401 | 402 | Wire up WCH-LinkE and bricked CH32V as: 403 | ``` 404 | +------------+ +---------------+ 405 | | |----3v3-----| | 406 | | | | | 407 | | |----gnd-----| | 408 | | | | | 409 | | WCH-LinkE |----dio-----| bricked CH32V | 410 | | | | | 411 | | |----clk-----| | 412 | | | | | 413 | | |----nrst----| | 414 | +------------+ +---------------+ 415 | ``` 416 | 417 | Run: 418 | 419 | ``` 420 | wlink erase --method pin-rst --speed low --chip 421 | ``` 422 | 423 | Possible value of chip type: CH32V103, CH57X, CH56X, CH32V20X, CH32V30X, CH582, CH32V003, CH8571, CH59X, CH643, CH32X035, CH32L103, CH641, CH585, CH564, CH32V007, CH645, CH32V317 424 | 425 | You can also use [WCH-LinkUtility](https://www.wch.cn/downloads/WCH-LinkUtility_ZIP.html) if you have windows system. 426 | 427 | Open WCH LinkUtility and select the correct MCU series as: 428 | 429 |

430 |

431 | 432 | Then click 'Clear All Code Flash By Pin NRST' will erase code flash of bricked CH32V: 433 | 434 |

435 |

436 | 437 | If the bricked board did not export NRST pin, you can solder a wire to MCU NRST pin directly. 438 | 439 | ## Clear all code flash by power off 440 | 441 | This way don't require NRST pin, wire up WCH-LinkE and bricked CH32V as: 442 | ``` 443 | +------------+ +---------------+ 444 | | | | | 445 | | | | | 446 | | |----dio-----| | 447 | | | | | 448 | | WCH-LinkE | | bricked CH32V | 449 | | | | | 450 | | |----clk-----| | 451 | | | | | 452 | | | | | 453 | +------------+ +---------------+ 454 | ``` 455 | 456 | Run: 457 | 458 | ``` 459 | wlink erase --method power-off --speed low --chip 460 | ``` 461 | 462 | Possible value of chip type: CH32V103, CH57X, CH56X, CH32V20X, CH32V30X, CH582, CH32V003, CH8571, CH59X, CH643, CH32X035, CH32L103, CH641, CH585, CH564, CH32V007, CH645, CH32V317 463 | 464 | Then power up the bricked CH32V as quick as possible after the command excuted, for example, plug the usb cable to PC host quickly. You may need to try several times to succeed and it will erase all code flash of bricked CH32V. 465 | 466 | You can also use [WCH-LinkUtility](https://www.wch.cn/downloads/WCH-LinkUtility_ZIP.html) if you have windows system. 467 | 468 | Open WCH LinkUtility and click 'Clear All Code Flash By Power Off'. 469 | 470 |

471 |

472 | 473 | 474 | # How to update firmware of WCH-Link/E 475 | 476 | **NOTE:** If you have windows system, you can update WCH-Link/WCH-LinkE firmware online using IAP method by [WCH-LinkUtility](https://www.wch.cn/downloads/WCH-LinkUtility_ZIP.html). For linux, you should use [wlink-iap](https://github.com/cjacker/wlink-iap). 477 | 478 | If you don't use windows, please follow below steps to update the firmware of WCH-Link/LinkE: 479 | 480 | ## Get the latest firmware 481 | 482 | Official firmwares from WCH can be extracted from [WCH-LinkUtility](https://www.wch.cn/downloads/WCH-LinkUtility_ZIP.html). Download and extract it, find the `Firmware_Link` dir. I also put a copy of v2.8 and v2.15 firmwares in this repo. 483 | 484 | ``` 485 | Firmware_Link/ 486 | ├── WCH-LinkE-APP-IAP.bin # firmware for WCH-LinkE 487 | ├── WCH-Link_APP_IAP_RV.bin # RV SWD firmware for WCH-Link 488 | ├── WCH-Link_APP_IAP_ARM.bin # ARM DAP firmware for WCH-Link 489 | ├── FIRMWARE_CH32V305.bin # firmware of WCH-LinkE, for IAP 490 | ├── FIRMWARE_CH549.bin # RV SWD firmware of WCH-Link, for IAP. 491 | ├── FIRMWARE_DAP_CH549.bin # ARM DAP firmware of WCH-Link, for IAP. 492 | 493 | ``` 494 | 495 | At first, WCH-Link can also toggle DAP/RV mode by a button or software like WCH-LinkE, but due to the firmware size increased, it doesn't support dual mode anymore. MounRiver Studio also flash the corresponding firmware everytime when you toggle the mode of WCH-Link(without E). 496 | 497 | ## Update firmware of WCH-Link/WCH-LinkE using IAP method 498 | 499 | **[wlink-iap](https://github.com/cjacker/wlink-iap)** is a cli tool to upgrade / downgrade WCH-LinkE firmware online using IAP method under linux. 500 | 501 | You don't have to enter ISP mode or buy another WCH-LinkE to upgrade the firmwares. 502 | 503 | The usage is very simple, just Plug the to-be-updated WCH-Link/LinkE to PC USB port and run: 504 | 505 | ``` 506 | wlink-iap -f FIRMWARE_XXXX.bin 507 | ``` 508 | **NOTE:** You should use IAP App firmwares (filename start with 'FIRMWARE_') with wlink-iap. 509 | 510 | ## Update firmware of WCH-Link 511 | 512 | WCH-Link use CH549 MCU, it can be programmed by ISP under linux. 513 | 514 | To program CH549, we need install [ch552tool](https://github.com/MarsTechHAN/ch552tool) first. And short DP pin (P5.1) and 3v3 VCC pin when power up to enter ISP mode. 515 | 516 | After enter ISP mode, `lsusb` like: 517 | 518 | ``` 519 | Bus 001 Device 020: ID 4348:55e0 WinChipHead 520 | ``` 521 | 522 | Then erase the flash as: 523 | ``` 524 | # sudo ch55xtool -e -c 525 | Found CH549 with SubId:18 526 | BTVER:02.40 527 | UID:64-63-49-43-00-00-AD-A6 528 | Erasing chip data flash. Done. 529 | Erasing chip flash. Done. 530 | Finalize communication. Done. 531 | ``` 532 | 533 | Then program `WCH-Link_APP_IAP_RV.bin` or `WCH-Link_APP_IAP_ARM.bin` as: 534 | ``` 535 | sudo ch55xtool -f WCH-Link_APP_IAP_RV.bin 536 | Found CH549 with SubId:18 537 | BTVER:02.40 538 | UID:64-63-49-43-00-00-AD-A6 539 | Erasing chip flash. Done. 540 | Flashing chip. Done. 541 | Finalize communication. Done. 542 | ``` 543 | 544 | ## Update firmware of WCH-LinkE 545 | 546 | WCH-LinkE use CH32V305fbp6, you need another workable WCH-LinkE to program it. 547 | 548 | Wire up WCH-LinkE and the target WCH-LinkE as: 549 | ``` 550 | +------------+ +-----------+ 551 | | |----3v3-----| | 552 | | | | | 553 | | |----gnd-----| | 554 | | | | target | 555 | | WCH-LinkE |----dio-----| WCH-LinkE | 556 | | | | to be | 557 | | |----clk-----| update | 558 | | | | | 559 | | |----nrst----| | 560 | +------------+ +-----------+ 561 | ``` 562 | 563 | Hold the "IAP" button of "target WCH-LinkE" down and plug WCH-LinkE to PC USB port, it will force the "target WCH-LinkE" enter IAP mode. 564 | 565 | > IAP mode is not required to program the target WCH-LinkE, but under IAP mode, the target WCH-LinkE released the ocuppied pins and make itself able to be programmed as common CH32V dev board. 566 | 567 | Official WCH-LinkE has a plastic case, it prevent user to press the IAP button accidently unless break the case, you can use [wlink-iap](https://github.com/cjacker/wlink-iap) to switch WCH-LinkE to IAP mode from commandline. 568 | 569 | To enter IAP mode, plug the "target WCH-LinkE" to PC USB port: 570 | ``` 571 | wlink-iap -i 572 | ``` 573 | to quit IAP mode: 574 | ``` 575 | wlink-iap -q 576 | ``` 577 | 578 | After switch "target WCH-LinkE" to IAP mode, unplug it. Then you don't need to hold the IAP button of "target WCH-LinkE" down when plug another WCH-LinkE directly to PC USB port. 579 | 580 | Then update the "target WCH-LinkE" firmware as: 581 | 582 | ``` 583 | # erase the code flash by pin-rst 584 | wlink erase --method pin-rst --speed low --chip CH32V30X 585 | # write the latest firmware, note the firmware I used here. 586 | wlink flash WCH-LinkE-APP-IAP.bin 587 | ``` 588 | -------------------------------------------------------------------------------- /assets/erase_by_nrst.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/assets/erase_by_nrst.png -------------------------------------------------------------------------------- /assets/erase_by_poweroff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/assets/erase_by_poweroff.png -------------------------------------------------------------------------------- /assets/select_ch32v30x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/assets/select_ch32v30x.png -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/FIRMWARE_CH32V203.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/FIRMWARE_CH32V203.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/FIRMWARE_CH32V305.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/FIRMWARE_CH32V305.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/FIRMWARE_CH549.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/FIRMWARE_CH549.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/FIRMWARE_DAP_CH549.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/FIRMWARE_DAP_CH549.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/WCH-DAPLink_APP_IAP.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/WCH-DAPLink_APP_IAP.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/WCH-LinkE-APP-IAP.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/WCH-LinkE-APP-IAP.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/WCH-Link_APP_IAP_ARM.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/WCH-Link_APP_IAP_ARM.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/WCH-Link_APP_IAP_RV.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link.old/WCH-Link_APP_IAP_RV.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/firmware_version.txt: -------------------------------------------------------------------------------- 1 | v28 -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/flash-wch-linke.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | wlink erase --method pin-rst --speed low --chip CH32V30X 3 | wlink flash WCH-LinkE-APP-IAP.bin 4 | 5 | -------------------------------------------------------------------------------- /firmwares/Firmware_Link.old/wchlink.wcfg: -------------------------------------------------------------------------------- 1 | [WCHLINK] 2 | LinkName=Default 3 | 4 | [Default] 5 | CH549Ver_RV=28 6 | CH549Ver_ARM=28 7 | CH32V307Ver=28 8 | CH32V208Ver=28 9 | CH32V203Ver=28 -------------------------------------------------------------------------------- /firmwares/Firmware_Link/FIRMWARE_CH32V203.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/FIRMWARE_CH32V203.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/FIRMWARE_CH32V208.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/FIRMWARE_CH32V208.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/FIRMWARE_CH32V305.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/FIRMWARE_CH32V305.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/FIRMWARE_CH549.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/FIRMWARE_CH549.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/FIRMWARE_DAP_CH549.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/FIRMWARE_DAP_CH549.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/WCH-DAPLink_APP_IAP.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/WCH-DAPLink_APP_IAP.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/WCH-LinkE-APP-IAP.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/WCH-LinkE-APP-IAP.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/WCH-LinkW-APP-IAP.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/WCH-LinkW-APP-IAP.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/WCH-Link_APP_IAP_ARM.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/WCH-Link_APP_IAP_ARM.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/WCH-Link_APP_IAP_RV.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cjacker/opensource-toolchain-ch32v/d7e632a6fbc25a5fb2428716a861eba9431290d5/firmwares/Firmware_Link/WCH-Link_APP_IAP_RV.bin -------------------------------------------------------------------------------- /firmwares/Firmware_Link/firmware_version.txt: -------------------------------------------------------------------------------- 1 | v36 -------------------------------------------------------------------------------- /firmwares/Firmware_Link/flash-wch-linke.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | wlink erase --method pin-rst --speed low --chip CH32V30X 3 | wlink flash WCH-LinkE-APP-IAP.bin 4 | 5 | -------------------------------------------------------------------------------- /firmwares/Firmware_Link/wchlink.wcfg: -------------------------------------------------------------------------------- 1 | [WCHLINK] 2 | LinkName=Default 3 | 4 | [Default] 5 | CH549Ver_RV=32 6 | CH549Ver_ARM=31 7 | CH32V307Ver=35 8 | CH32V208Ver=33 9 | CH32V203Ver=32 -------------------------------------------------------------------------------- /wch-riscv.cfg: -------------------------------------------------------------------------------- 1 | #interface wlink 2 | adapter driver wlinke 3 | adapter speed 6000 4 | transport select sdi 5 | 6 | wlink_set_address 0x00000000 7 | set _CHIPNAME wch_riscv 8 | sdi newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00001 9 | 10 | set _TARGETNAME $_CHIPNAME.cpu 11 | 12 | target create $_TARGETNAME.0 wch_riscv -chain-position $_TARGETNAME 13 | $_TARGETNAME.0 configure -work-area-phys 0x20000000 -work-area-size 10000 -work-area-backup 1 14 | set _FLASHNAME $_CHIPNAME.flash 15 | 16 | flash bank $_FLASHNAME wch_riscv 0x00000000 0 0 0 $_TARGETNAME.0 17 | 18 | echo "Ready for Remote Connections" 19 | --------------------------------------------------------------------------------