Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
monte-monte committed Mar 3, 2025
2 parents 7448e9e + 6da4fcc commit 2e9566c
Show file tree
Hide file tree
Showing 17 changed files with 4,941 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ minichlink/minichlink
minichlink/minichlink.so
compile_commands.json
.cache
ch32v003fun/generated_*.ld
ch32fun/generated_*.ld
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ch32v003fun
# ch32fun

An open source development environment (tooling, headers, examples) for the [CH32V003](http://www.wch-ic.com/products/CH32V003.html) (and many other chips within the CH32V family) with gcc-riscv64 that can be used in Windows (Native), Linux and/or WSL. The CH32V003 is 10-cent part with a RISC-V EC core that runs at 48MHz, has 16kB of flash and 2kB of RAM and a bunch of peripherals. It also comes in SOP-8, QFN-20 and SOIC packages. You can get the datasheet [here](http://www.wch-ic.com/downloads/CH32V003DS0_PDF.html).

The goal of this project is to develop the tooling and environment for efficient use of the CH32V003. Avoid complicated HALs, and unleash the hardware! The existing EVT is massive, and dev environment weighty. This project specifically avoids the HAL and makes it so you can just use the [TRM](http://www.wch-ic.com/downloads/CH32V003RM_PDF.html). In contrast, blinky is only 500 bytes with ch32v003fun, boots faster, and significantly simpler overall.
The goal of this project is to develop the tooling and environment for efficient use of the CH32V003. Avoid complicated HALs, and unleash the hardware! The existing EVT is massive, and dev environment weighty. This project specifically avoids the HAL and makes it so you can just use the [TRM](http://www.wch-ic.com/downloads/CH32V003RM_PDF.html). In contrast, blinky is only 500 bytes with ch32fun, boots faster, and significantly simpler overall.

ch32v003fun contains:
1. Examples using ch32v003fun, but not as many as using the HAL.
ch32fun contains:
1. Examples using ch32fun, but not as many as using the HAL. Most of which are centered around the ch32v003, but even many of those are compatible with other processors.
2. "minichlink" which uses the WCH CH-Link with libusb, for cross-platform use.
* An STM32F042 Programmer, the [NHC-Link042](https://github.com/NgoHungCuong/NHC-Link042)
* An ESP32S2 Programmer, the [esp32s2-funprog](https://github.com/cnlohr/esp32s2-cookbook/tree/master/ch32v003programmer)
Expand All @@ -14,7 +14,7 @@ ch32v003fun contains:
* Supports gdbserver-style-debugging for use with Visual Studio.
* Supports printf-over-single-wire. (At about 400kBaud)
3. An extra copy of libgcc so you can use unusual risc-v build chains, located in the `misc/libgcc.a`.
4. A folder named "ch32v003fun" containing a single self-contained source file and header file for compling apps for the ch32v003.
4. A folder named "ch32fun" containing a single self-contained source file and header file for compling apps for the ch32v003.
5. On some systems ability to "printf" back through
6. A demo bootloader.

Expand All @@ -24,7 +24,7 @@ Currently, there is experimental support for the ch32v103, ch32v203, ch32v208, c

## Getting Started

For installation / setup instructions, see the [wiki page here](https://github.com/cnlohr/ch32v003fun/wiki/Installation)
For installation / setup instructions, see the [wiki page here](https://github.com/cnlohr/ch32fun/wiki/Installation)

## Features!

Expand Down Expand Up @@ -58,11 +58,11 @@ The generated .bin is used by minichlink and the .hex file is compatible with th

## VSCode +/- PlatformIO

Note: PlatformIO is generally used for CI on this repo. While PlatformIO can be used to code and debug a ch32v003fun project inside VSCode, you can also do it completely without PlatformIO: For that, see [template project](https://github.com/cnlohr/ch32v003fun/tree/master/examples/template/.vscode).
Note: PlatformIO is generally used for CI on this repo. While PlatformIO can be used to code and debug a ch32fun project inside VSCode, you can also do it completely without PlatformIO: For that, see [template project](https://github.com/cnlohr/ch32fun/tree/master/examples/template/.vscode).

This project can be built, uploaded and debugged with VSCode and the PlatformIO extension. Simply clone and open this project in VSCode and have the PlatformIO extension installed. Use the [project environment switcher](https://docs.platformio.org/en/latest/integration/ide/vscode.html#project-tasks) to select a specific example, such as "blink".

To create a new PlatformIO firmware project based on ch32v003fun, simply use the [PlatformIO Home](https://docs.platformio.org/en/latest/home/index.html)'s "New Project" wizard and set "Framework" to "ch32v003fun". There are [various examples](https://github.com/Community-PIO-CH32V/platform-ch32v/tree/develop/examples/blinky-ch32v003fun).
To create a new PlatformIO firmware project based on ch32fun, simply use the [PlatformIO Home](https://docs.platformio.org/en/latest/home/index.html)'s "New Project" wizard and set "Framework" to "ch32v003fun". There are [various examples](https://github.com/Community-PIO-CH32V/platform-ch32v/tree/develop/examples/blinky-ch32v003fun).

![pio_proj](.github/pio_project_creation.png)

Expand All @@ -71,7 +71,7 @@ The equivalent [PlatformIO Core CLI](https://docs.platformio.org/en/latest/integ
pio init -b genericCH32V003F4P6 -O "framework = ch32v003fun"
```

If you want to add your code directly to ch32v003fun project, please expand the available environments in the `platformio.ini` as [per instructions](https://github.com/cnlohr/ch32v003fun/blob/49640fbccf231191aa83c6a2bbe9d385535b2d1e/platformio.ini#L48-L53).
If you want to add your code directly to ch32fun project, please expand the available environments in the `platformio.ini` as [per instructions](https://github.com/cnlohr/ch32fun/blob/49640fbccf231191aa83c6a2bbe9d385535b2d1e/platformio.ini#L48-L53).

Please also note that when you open this project inside PlatformIO, you can use the [project environment switcher](https://docs.platformio.org/en/latest/integration/ide/vscode.html#project-tasks) at the bottom of the VSCode taskbar to select a specific example folder. Otherwise, "Build" will by default build **all** examples.

Expand Down Expand Up @@ -105,7 +105,7 @@ Other third party tools are adding lots of examples, etc. See the following rep

## Support

You can open a github ticket or join my Discord in the #ch32v003fun channel. Right now invites are private, so just ping me @cnlohr for an invite link. My PMs are open.
You can open a github ticket or join my Discord in the #ch32fun channel. Right now invites are private, so just ping me @cnlohr for an invite link. My PMs are open.

### Footnotes/links

Expand Down
16 changes: 7 additions & 9 deletions ch32fun/ch32fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,7 @@ WEAK int puts(const char *s)

#define mini_strlen strlen

static int
mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
char *buffer)
int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer)
{
char *pbuffer = buffer;
int negative = 0;
Expand Down Expand Up @@ -775,7 +773,7 @@ extern uint32_t * _edata;

#if FUNCONF_DEBUG_HARDFAULT
#if FUNCONF_USE_DEBUGPRINTF
static void PrintN( uint32_t n )
void PrintHex( uint32_t n )
{
while( (*DMDATA0) & 0x80 );
// Write out character.
Expand All @@ -790,7 +788,7 @@ static void PrintN( uint32_t n )
}
}
#elif FUNCONF_USE_UARTPRINTF
static void PrintN( uint32_t n )
void PrintHex( uint32_t n )
{
putchar( ' ' );
putchar( '0' );
Expand All @@ -812,10 +810,10 @@ void DefaultIRQHandler( void )
#if FUNCONF_DEBUG_HARDFAULT && ( FUNCONF_USE_DEBUGPRINTF || FUNCONF_USE_UARTPRINTF )
//This is kind of like a crash handler.
//printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() );
PrintN( __get_MEPC() ); // "addr2line -e debugprintfdemo.elf 0x000007e6" ---> debugprintfdemo.c:45
PrintN( __get_MSTATUS() );
PrintN( __get_MTVAL() );
PrintN( __get_MCAUSE() );
PrintHex( __get_MEPC() ); // "addr2line -e debugprintfdemo.elf 0x000007e6" ---> debugprintfdemo.c:45
PrintHex( __get_MSTATUS() );
PrintHex( __get_MTVAL() );
PrintHex( __get_MCAUSE() );
#if FUNCONF_USE_DEBUGPRINTF
while( (*DMDATA0) & 0x80 );
*DMDATA0 = 0x0a85;
Expand Down
4 changes: 4 additions & 0 deletions ch32fun/ch32fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@ int WaitForDebuggerToAttach( int timeout_ms );
// Just a definition to the internal _write function.
int _write(int fd, const char *buf, int size);

// Print a hexadecimal value to the debug (or UART) depending on configuration.
void PrintHex( uint32_t n );

// Call this to busy-wait the polling of input.
void poll_input( void );

Expand All @@ -946,6 +949,7 @@ int mini_vsnprintf( char *buffer, unsigned int buffer_len, const char *fmt, va_l
int mini_vpprintf( int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va );
int mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...);
int mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...);
int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer);

#endif // __ASSEMBLER__

Expand Down
2 changes: 1 addition & 1 deletion ch32fun/ch32fun.ld
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ SECTIONS
.data :
{
. = ALIGN(4);
__global_pointer$ = . + 0x3fc; /* This gets set in the startup code. This allows -mrelax'd code to be smaller by acting as a sort of quick reference in the gp register. */
*(.gnu.linkonce.r.*)
*(.data .data.*)
*(.gnu.linkonce.d.*)
. = ALIGN(8);
PROVIDE( __global_pointer$ = . + 0x800 );
*(.sdata .sdata.*)
*(.sdata2*)
*(.gnu.linkonce.s.*)
Expand Down
16 changes: 13 additions & 3 deletions ch32fun/ch32fun.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Default prefix for Windows
ifeq ($(OS),Windows_NT)
PREFIX?=riscv64-unknown-elf
ifeq ($(shell which $(PREFIX)),)
PREFIX:=riscv-none-elf
endif
# Check if riscv64-unknown-elf-gcc exists
else ifneq ($(shell which riscv64-unknown-elf-gcc),)
PREFIX?=riscv64-unknown-elf
Expand Down Expand Up @@ -36,6 +39,8 @@ endif
CFLAGS?=-g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8
LDFLAGS+=-Wl,--print-memory-usage -Wl,-Map=$(TARGET).map

GCCVERSION13 := $(shell expr `$(PREFIX)-gcc -dumpversion | cut -f1 -d.` \>= 13)

ifeq ($(TARGET_MCU),CH32V003)
CFLAGS_ARCH+=-march=rv32ec -mabi=ilp32e -DCH32V003=1
GENERATED_LD_FILE?=$(CH32FUN)/generated_ch32v003.ld
Expand All @@ -46,7 +51,12 @@ else
MCU_PACKAGE?=1
ifeq ($(findstring CH32V00,$(TARGET_MCU)),CH32V00) # CH32V002, 4, 5, 6, 7
# Note: The CH32V003 is not a CH32V00x.
CFLAGS_ARCH+=-march=rv32eczmmul -mabi=ilp32e -DCH32V00x=1
ifeq "$(GCCVERSION13)" "1"
CFLAGS_ARCH+=-march=rv32ec_zmmul -mabi=ilp32e -DCH32V00x=1
else
CFLAGS_ARCH+=-march=rv32ec -mabi=ilp32e -DCH32V00x=1 # If not GCC 13 or higher, does not support zmmul as a command line
endif

ifeq ($(findstring CH32V002, $(TARGET_MCU)), CH32V002)
TARGET_MCU_LD:=5
else ifeq ($(findstring CH32V004, $(TARGET_MCU)), CH32V004)
Expand Down Expand Up @@ -189,7 +199,7 @@ else

LDFLAGS+=-lgcc
GENERATED_LD_FILE:=$(CH32FUN)/generated_$(TARGET_MCU_PACKAGE)_$(TARGET_MCU_MEMORY_SPLIT).ld
LINKER_SCRIPT:=$(GENERATED_LD_FILE)
LINKER_SCRIPT?=$(GENERATED_LD_FILE)
endif

CFLAGS+= \
Expand All @@ -205,7 +215,7 @@ FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)

$(TARGET).bin : $(TARGET).elf
$(PREFIX)-objdump -S $^ > $(TARGET).lst
$(PREFIX)-objcopy -O binary $< $(TARGET).bin
$(PREFIX)-objcopy $(OBJCOPY_FLAGS) -O binary $< $(TARGET).bin
$(PREFIX)-objcopy -O ihex $< $(TARGET).hex

ifeq ($(OS),Windows_NT)
Expand Down
Loading

0 comments on commit 2e9566c

Please sign in to comment.