Search This Blog

Wednesday, May 8, 2024

CH32V Development with Visual Studio Code/Platform IO: Set up and first impression

Prerequisite:

  • Buy the WCH LinkE Emulation Debugger Module from the usual places from China for less than $5.  You won't even get to the bootstrap loader on the CH32V003 without one as there are no hardware bootstrap pins.  For higher end chips, the bootloader can download from USB using WCHISPTool.



Don't be temped by the cheaper older WCH Link module.  It is powered by CH549 (8051 core with 60K FLASH/2K RAM) It is not useful here but good for hacking it into CMSIS-DAP.

LinkE module is the one you want to get.  Powered by CH32V305 with 128K FLASH/32K RAM.  It supports for the low pin count parts that uses a single wire debug interface e.g. CH32V003 and newer chips e.g. CH32X035. 
  • Download: WCH-Link Local Burning Tool: WCH-LinkUtility.ZIP from this page.  It'll also install a COM port driver.

Select WCH-LinkRV mode, hit Set.  The blue LED turns off.  This is now the default mode.
Hit Refresh, it now shows up as RISC-V Link.

 
Note: 
    1. "Enable SDI Printf" option allows you to send debug messages on a virtual serial port piggy back on the SDI debugger interface (i.e. Semihosting). This is useful for low pin count parts. The support functions are described inside debug.h  See: https://www.cnblogs.com/liaigu/p/17628184.html  (use Chrome for translation)
    2. WCH-LinkDAP-WINUSB mode is for debugging with CH32F (Arm) series.  
    3. You can flash a new firmware and turning LinkE into a high-speed JTAG bridge similar to CH347 for DSP/FPGA using WCHLinkEJtagUpdTool.ZIP.  You'll need to re-flash the LinkE firmware to switch back.

Install IDE

  • Optional: Change PlatformIO Install Directory to path other than under your Windows user profile directory.  
Reference: https://community.platformio.org/t/vscode-install-directory/12325/2
Setting the PLATFORMIO_CORE_DIR pointing to my folder where the policies allow execution solves the issue. I set this value inside the standard Windows uservariables and then after this run the normal package installation process within VSCode.

Under Windows Advance User Setting/Environment Variable/User variable.  The highlighted path is where you want it.


Use Take Ownership on the directory if it is under "program files" to allow for write permission there.


  •  Optional: Change default project directory

See https://community.platformio.org/t/how-to-change-default-new-project-location/2828/18 

Hit the terminal icon on bottom to bring up this terminal.  For some reasons, this one works properly.

Type in the following and your directory path.  Add quotes around the path if there are any spaces.

pio settings set projects_dir  your_directory_path


PlatformIO.ini

There is a nasty surprise in the system_ch32v00x.c file. This is part of the startup code that is provided and set the SYSCLK prior to passing control to main(). 


The problem is that the default setting is for an external crystal connected to PA1 and PA2 pins.  If that assumption is incorrect, the 'RCC_HSERDY' bit will never be set. Their code doesn't handle the error properly.  Personally I prefer not having these hidden setting things up if I have to fix and handle the fail conditions.


If you don't want to edit the "system_ch32v00x.c" file, you can add a build_flags entries in the platformio.ini to add a compiler #define value.   You can use any of the listed options (line #20 - #25). I added the following:

build_flags = -D SYSCLK_FREQ_24MHZ_HSI

Creating a New Project


  • Select Platform IO on the side bar (Ant Icon), hit "Create New Project".  Give it a name.
  • Select "Generic CH32V003F4P6 (W.CH)" if you are using your own design or "CH32V003F4P6-EVT-R0 (W.CH)" for the eval board.
  • There is choice for "Noneos-SDK" for bare metal or "Arduino" platform.  The Ch32V003 part is too small for other RTOS platforms.


My bare metal Blinky.  It toggles at 2MHz.

The debugger allows for the either source code level view or assembly, but not both.  The peripheral register  are shown grouped in fields and can be shown/modified when the program is paused or in single step.

Assembly language view but it doesn't have the source code reference like the other IDE I have used.

Some of the things I like about modern IDE over basic text editors.


It shows the macro definition, what it expands to and the enum values when you mouse over it.


Popup on mouse over function
 with doxygen comment - WCH HAL function

Update:

Other Links

https://github.com/gicking/CH32V003_examples - Port of original CH32V003 examples by WCH for PlatformIO.