Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v30x, v20x, v10x support #276

Merged
merged 47 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
38224d2
Add linker file, Makefile and basic header
AlexanderMandera Jun 16, 2023
50cece1
Merge branch 'cnlohr:master' into v203
AlexanderMandera Jun 16, 2023
fa0acbb
Add ch32v203fun.c and example
AlexanderMandera Jun 17, 2023
89d9bee
Merge remote-tracking branch 'origin/master' into v203
AlexanderMandera Jun 17, 2023
6ac9a03
Add ch32v203fun.c and example (not working yet)
AlexanderMandera Jun 17, 2023
45ddfc7
Blink works!
AlexanderMandera Jun 19, 2023
8eef7f8
Merge branch 'master' into v203
AlexanderMandera Jun 22, 2023
2e80141
Use 144 MHz HSI
AlexanderMandera Jun 22, 2023
397ccb9
Add SystemInit144HSE
AlexanderMandera Jun 22, 2023
eeff5bf
Merge ch32v203fun into ch32v003fun
AlexanderMandera Jun 23, 2023
d472fb5
Fix error with cpp_virtual_methods
AlexanderMandera Jun 23, 2023
60770d7
Fix minichlink build
AlexanderMandera Jun 23, 2023
b332b60
Add 30x support, remove ch32v203fun folder
AlexanderMandera Jun 24, 2023
c6f59a2
Fix Delay_SysTick
AlexanderMandera Jun 24, 2023
a859e50
Fix compiler errors
AlexanderMandera Jun 24, 2023
957c880
Add support for V10x
AlexanderMandera Jun 26, 2023
e6c2a75
Fix selection of flash loader
AlexanderMandera Jun 26, 2023
1c004cb
Change reboot sequence
AlexanderMandera Jun 26, 2023
28ccdcd
Revert "Change reboot sequence"
AlexanderMandera Jun 26, 2023
dccab94
Change DMI OP checks
AlexanderMandera Jun 26, 2023
665db23
Fix compiler error
AlexanderMandera Jun 26, 2023
e08eda4
Add untested V10x support
AlexanderMandera Jun 26, 2023
bcaf958
Cleanup, add comments
AlexanderMandera Jun 27, 2023
c01ffcd
Add 80MHz init for V103
AlexanderMandera Jun 27, 2023
94a38f2
Merge branch 'minichlink-v10x' into v203
AlexanderMandera Jun 27, 2023
25fa1c8
Merge branch 'master' into v203
AlexanderMandera Jun 28, 2023
b8a008e
Add funconfig.h files
AlexanderMandera Jun 28, 2023
bf1d011
SystemInit for 10x/20x/30x
AlexanderMandera Jun 28, 2023
45d5cca
Fix SystemInit for 10x/20x/30x, add TINYVECTOR for non-V003
AlexanderMandera Jun 28, 2023
e24fc6f
Fix minichlink compilation
AlexanderMandera Jun 28, 2023
50e4bf8
Fix wrong #endif
AlexanderMandera Jun 28, 2023
b1e4f6d
Try to fix SystemInit, change pin for blink example
AlexanderMandera Jun 28, 2023
585d91a
Add dynamic MCU and package selection
AlexanderMandera Jun 29, 2023
9154358
Fix HSE initialization
AlexanderMandera Jun 29, 2023
52af4f8
Merge branch 'master' into v203
AlexanderMandera Jun 29, 2023
91e9f2e
Fix typo, remove debug message
AlexanderMandera Jun 29, 2023
069162e
Add generated LD files to .gitignore
AlexanderMandera Aug 11, 2023
74a2220
Merge remote-tracking branch 'alex/other-chip-support' into other-chi…
AlexanderMandera Aug 11, 2023
452e299
Merge branch 'cnlohr:master' into other-chip-support
AlexanderMandera Oct 11, 2023
a7462c4
Merge remote-tracking branch 'cnlohr/master' into other-chip-support
AlexanderMandera Jan 8, 2024
24f5709
Merge branch 'other-chip-support' of https://github.com/AlexanderMand…
cnlohr Feb 12, 2024
0ab937f
Fix up a few parameters / clock issues on the ch32v203
cnlohr Feb 12, 2024
dceb8e1
* Switch blink example to use ch32v003fun
cnlohr Feb 12, 2024
fca0738
Fixup ld files for 003
cnlohr Feb 12, 2024
bddf3b9
Fix order of operations for compiling compiled linker script.
cnlohr Feb 12, 2024
ccba923
* Cleanup ld script generation
cnlohr Feb 12, 2024
a48be5e
Merge pull request #277 from cnlohr/master
cnlohr Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ minichlink/minichlink.so
compile_commands.json
.clangd
.cache
ch32v003fun/generated_*.ld
467 changes: 441 additions & 26 deletions ch32v003fun/ch32v003fun.c

Large diffs are not rendered by default.

7,295 changes: 7,262 additions & 33 deletions ch32v003fun/ch32v003fun.h

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions ch32v003fun/ch32v003fun.ld
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,45 @@ ENTRY( InterruptVector )

MEMORY
{
#if TARGET_MCU == CH32V003
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 16K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 2K
#elif TARGET_MCU == CH32V10x
#if MCU_PACKAGE == 1
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
#elif MCU_PACKAGE == 2
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
#else
#error "Unknown MCU package"
#endif
#elif TARGET_MCU == CH32V20x
#if MCU_PACKAGE == 1
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
#elif MCU_PACKAGE == 2
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
#elif MCU_PACKAGE == 3
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 128K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
#else
#error "Unknown MCU package"
#endif
#elif TARGET_MCU == CH32V30x
#if MCU_PACKAGE == 1
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
#elif MCU_PACKAGE == 2
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 128K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
#else
#error "Unknown MCU package"
#endif
#else
#error "Unknown MCU target"
#endif
}

SECTIONS
Expand Down
49 changes: 43 additions & 6 deletions ch32v003fun/ch32v003fun.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ else
endif
endif

TARGET_MCU?=CH32V003
TARGET_EXT?=c

CH32V003FUN?=../../ch32v003fun
MINICHLINK?=$(CH32V003FUN)/../minichlink

Expand All @@ -22,16 +25,43 @@ CFLAGS+= \
-nostdlib \
-I. -Wall $(EXTRA_CFLAGS)

LINKER_SCRIPT?=$(CH32V003FUN)/ch32v003fun.ld
WRITE_SECTION?=flash
SYSTEM_C?=$(CH32V003FUN)/ch32v003fun.c

ifeq ($(TARGET_MCU),CH32V003)
CFLAGS+= \
-g -Os -flto -ffunction-sections \
-static-libgcc \
-march=rv32ec \
-mabi=ilp32e \
-I/usr/include/newlib \
-I$(CH32V003FUN)/../extralibs \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V003=1 \
-I. -Wall $(EXTRA_CFLAGS)

GENERATED_LD_FILE?=$(CH32V003FUN)/generated_ch32v003.ld
LINKER_SCRIPT?=$(GENERATED_LD_FILE)
else
ifeq ($(findstring CH32V10,$(TARGET_MCU)),CH32V10)
include $(CH32V003FUN)/ch32v10xfun.mk
else ifeq ($(findstring CH32V20,$(TARGET_MCU)),CH32V20)
include $(CH32V003FUN)/ch32v20xfun.mk
else ifeq ($(findstring CH32V30,$(TARGET_MCU)),CH32V30)
include $(CH32V003FUN)/ch32v30xfun.mk
else
$(error Unknown MCU $(TARGET_MCU))
endif
endif

LDFLAGS+=-T $(LINKER_SCRIPT) -Wl,--gc-sections -L$(CH32V003FUN)/../misc -lgcc

WRITE_SECTION?=flash
SYSTEM_C?=$(CH32V003FUN)/ch32v003fun.c
TARGET_EXT?=c
ifeq ($(TARGET_MCU), CH32V003)

$(TARGET).elf : $(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)
$(PREFIX)-gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)

endif

$(TARGET).bin : $(TARGET).elf
$(PREFIX)-size $^
Expand Down Expand Up @@ -68,6 +98,13 @@ clangd_clean :

FLASH_COMMAND?=$(MINICHLINK)/minichlink -w $< $(WRITE_SECTION) -b

$(GENERATED_LD_FILE) :
$(PREFIX)-gcc -E -P -x c -DTARGET_MCU=$(TARGET_MCU) $(CH32V003FUN)/ch32v003fun.ld > $(GENERATED_LD_FILE)


$(TARGET).elf : $(FILES_TO_COMPILE) $(LINKER_SCRIPT)
$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)

cv_flash : $(TARGET).bin
make -C $(MINICHLINK) all
$(FLASH_COMMAND)
Expand Down
31 changes: 31 additions & 0 deletions ch32v003fun/ch32v10xfun.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
TARGET_MCU_PACKAGE?=CH32V103R8T6
MCU_PACKAGE?=1

CFLAGS+= \
-g -Os -flto -ffunction-sections -fdata-sections \
-static-libgcc \
-march=rv32imac \
-mabi=ilp32 \
-msmall-data-limit=8 \
-mno-save-restore \
-fmessage-length=0 \
-fsigned-char \
-I/usr/include/newlib \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V10x=1 \
-I. -Wall

# MCU Flash/RAM split
ifeq ($(findstring R8, $(TARGET_MCU_PACKAGE)), R8)
MCU_PACKAGE:=1
else ifeq ($(findstring C8, $(TARGET_MCU_PACKAGE)), C8)
MCU_PACKAGE:=1
else ifeq ($(findstring C6, $(TARGET_MCU_PACKAGE)), C6)
MCU_PACKAGE:=2
endif

GENERATED_LD_FILE:=$(CH32V003FUN)/generated_$(TARGET_MCU_PACKAGE).ld
LINKER_SCRIPT:=$(GENERATED_LD_FILE)
FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)

59 changes: 59 additions & 0 deletions ch32v003fun/ch32v20xfun.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
TARGET_MCU_PACKAGE?=CH32V203F8P6
MCU_PACKAGE?=1

CFLAGS+= \
-g -Os -flto -ffunction-sections -fdata-sections \
-static-libgcc \
-march=rv32imac \
-mabi=ilp32 \
-msmall-data-limit=8 \
-mno-save-restore \
-fmessage-length=0 \
-fsigned-char \
-I/usr/include/newlib \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V20x=1 \
-I. -Wall

# MCU Flash/RAM split
ifeq ($(findstring F8, $(TARGET_MCU_PACKAGE)), F8)
MCU_PACKAGE:=1
else ifeq ($(findstring G8, $(TARGET_MCU_PACKAGE)), G8)
MCU_PACKAGE:=1
else ifeq ($(findstring K8, $(TARGET_MCU_PACKAGE)), K8)
MCU_PACKAGE:=1
else ifeq ($(findstring C8, $(TARGET_MCU_PACKAGE)), C8)
MCU_PACKAGE:=1
else ifeq ($(findstring F6, $(TARGET_MCU_PACKAGE)), F6)
MCU_PACKAGE:=2
else ifeq ($(findstring G6, $(TARGET_MCU_PACKAGE)), G6)
MCU_PACKAGE:=2
else ifeq ($(findstring K6, $(TARGET_MCU_PACKAGE)), K6)
MCU_PACKAGE:=2
else ifeq ($(findstring C6, $(TARGET_MCU_PACKAGE)), C6)
MCU_PACKAGE:=2
else ifeq ($(findstring RB, $(TARGET_MCU_PACKAGE)), RB)
MCU_PACKAGE:=3
else ifeq ($(findstring GB, $(TARGET_MCU_PACKAGE)), GB)
MCU_PACKAGE:=3
else ifeq ($(findstring CB, $(TARGET_MCU_PACKAGE)), CB)
MCU_PACKAGE:=3
else ifeq ($(findstring WB, $(TARGET_MCU_PACKAGE)), WB)
MCU_PACKAGE:=3
endif

# Package
ifeq ($(findstring 203RB, $(TARGET_MCU_PACKAGE)), 203RB)
CFLAGS+=-DCH32V20x_D8
else ifeq ($(findstring 208, $(TARGET_MCU_PACKAGE)), 208)
CFLAGS+=-DCH32V20x_D8W
else
CFLAGS+=-DCH32V20x_D6
endif

GENERATED_LD_FILE:=$(CH32V003FUN)/generated_$(TARGET_MCU_PACKAGE).ld
LINKER_SCRIPT:=$(GENERATED_LD_FILE)
FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)


44 changes: 44 additions & 0 deletions ch32v003fun/ch32v30xfun.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TARGET_MCU_PACKAGE?=CH32V307VCT6
MCU_PACKAGE?=1

CFLAGS+= \
-g -Os -flto -ffunction-sections -fdata-sections \
-static-libgcc \
-march=rv32imafc \
-mabi=ilp32f \
-msmall-data-limit=8 \
-mno-save-restore \
-fmessage-length=0 \
-fsigned-char \
-I/usr/include/newlib \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V30x=1 \
-I. -Wall

# MCU Flash/RAM split
ifeq ($(findstring RC, $(TARGET_MCU_PACKAGE)), RC)
MCU_PACKAGE:=1
else ifeq ($(findstring VC, $(TARGET_MCU_PACKAGE)), VC)
MCU_PACKAGE:=1
else ifeq ($(findstring WC, $(TARGET_MCU_PACKAGE)), WC)
MCU_PACKAGE:=1
else ifeq ($(findstring CB, $(TARGET_MCU_PACKAGE)), CB)
MCU_PACKAGE:=2
else ifeq ($(findstring FB, $(TARGET_MCU_PACKAGE)), FB)
MCU_PACKAGE:=2
else ifeq ($(findstring RB, $(TARGET_MCU_PACKAGE)), RB)
MCU_PACKAGE:=2
endif

# Package
ifeq ($(findstring 303, $(TARGET_MCU_PACKAGE)), 303)
CFLAGS+=-DCH32V30x_D8
else
CFLAGS+=-DCH32V30x_D8C
endif

GENERATED_LD_FILE:=$(CH32V003FUN)/generated_$(TARGET_MCU_PACKAGE).ld
LINKER_SCRIPT:=$(GENERATED_LD_FILE)
FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)

Binary file modified examples/blink/blink.bin
Binary file not shown.
1 change: 1 addition & 0 deletions examples/cpp_virtual_methods/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CFLAGS:= \
-I/usr/include/newlib \
-I$(CH32V003FUN) \
-nostdlib \
-DCH32V003 \
-I. -Wall

CFLAGS+=-fno-rtti -DCPLUSPLUS
Expand Down
11 changes: 11 additions & 0 deletions examples_v10x/blink/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all : flash

TARGET:=blink
TARGET_MCU:=CH32V103

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean


23 changes: 23 additions & 0 deletions examples_v10x/blink/blink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "ch32v003fun.h"
#include <stdio.h>

int main()
{
SystemInit();

// Enable GPIOs
//RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC;
RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;

// GPIO A0 Push-Pull
GPIOA->CFGLR &= ~(0xf<<(4*0));
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*0);

while(1)
{
GPIOA->BSHR = (1<<0); // Turn on GPIO
Delay_Ms( 1000 );
GPIOA->BSHR = (1<<(16+0)); // Turn off GPIO
Delay_Ms( 1000 );
}
}
7 changes: 7 additions & 0 deletions examples_v10x/blink/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

//#define CH32V10x 1

#endif

11 changes: 11 additions & 0 deletions examples_v20x/blink/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all : flash

TARGET:=blink
TARGET_MCU:=CH32V203

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean


19 changes: 19 additions & 0 deletions examples_v20x/blink/blink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "ch32v003fun.h"
#include <stdio.h>

int main()
{
SystemInit();

funGpioInitAll();

funPinMode( PA15, GPIO_CFGLR_OUT_10Mhz_PP );

while(1)
{
funDigitalWrite( PA15, FUN_HIGH ); // Turn on GPIO
Delay_Ms( 1000 );
funDigitalWrite( PA15, FUN_LOW ); // Turn off GPIO
Delay_Ms( 1000 );
}
}
7 changes: 7 additions & 0 deletions examples_v20x/blink/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

// #define CH32V20x 1

#endif

11 changes: 11 additions & 0 deletions examples_v20x/mcotest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all : flash

TARGET:=mcotest
TARGET_MCU:=CH32V203

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean


7 changes: 7 additions & 0 deletions examples_v20x/mcotest/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

// #define CH32V20x 1

#endif

18 changes: 18 additions & 0 deletions examples_v20x/mcotest/mcotest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "ch32v003fun.h"
#include <stdio.h>

int main()
{
SystemInit();

funGpioInitAll();

funPinMode( PA8, GPIO_CFGLR_OUT_50Mhz_AF_PP );

RCC->CFGR0 = RCC_CFGR0_MCO_PLL;

while(1)
{
}
}

Loading
Loading