Skip to content

Commit 945a547

Browse files
authored
Merge branch 'cnlohr:master' into master
2 parents abdc53b + e45e721 commit 945a547

File tree

26 files changed

+8617
-103
lines changed

26 files changed

+8617
-103
lines changed

.github/gen_ldscript.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def get_ld_defines(chip_name: str):
3939
elif "f7" in chip_name:
4040
mcu_package = 1
4141
target_mcu_ld = 4
42+
elif chip_name.startswith("ch32l103"):
43+
target_mcu = chip_name.upper()[0:len("ch32l103")]
44+
if "f8" in chip_name:
45+
mcu_package = 1
46+
elif "k8" in chip_name:
47+
mcu_package = 1
48+
elif "c8" in chip_name:
49+
mcu_package = 1
50+
elif "g8" in chip_name:
51+
mcu_package = 1
52+
target_mcu_ld = 4
4253
elif chip_name.startswith("ch32v20"):
4354
target_mcu = chip_name.upper()[0:len("ch32v20x")]
4455
if "f8" in chip_name:

.github/workflows/build.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ jobs:
2727
echo "============================"
2828
(cd "$dir" && make V=1 -j3 "$(basename "$dir").elf" && riscv64-unknown-elf-size "$(basename "$dir").elf")
2929
'
30+
build-all-windows:
31+
runs-on: windows-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Install xPack GCC + Make
35+
shell: pwsh
36+
run: |
37+
./misc/install_xpack_gcc.ps1 -SkipPrompts
38+
- name: Build all examples
39+
shell: cmd
40+
run: |
41+
cd build_scripts
42+
build_all.cmd
3043
# Build using PlatformIO
3144
pio-build:
3245
strategy:

ch32fun/ch32fun.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ void __libc_init_array(void)
9292
#include <stdint.h>
9393
#include <ch32fun.h>
9494

95-
#define WEAK __attribute__((weak))
96-
9795
WEAK int errno;
9896

9997
static int __puts_uart( char *s, int len, void *buf )
@@ -1113,7 +1111,7 @@ asm volatile(
11131111
" mret\n" : : [main]"r"(main) );
11141112
}
11151113

1116-
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH5xx)
1114+
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH32L103) || defined(CH5xx)
11171115

11181116
void handle_reset( void )
11191117
{
@@ -1529,7 +1527,7 @@ void SetupDebugPrintf( void )
15291527
int WaitForDebuggerToAttach( int timeout_ms )
15301528
{
15311529

1532-
#if defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH571_CH573) || defined(CH582_CH583) || defined(CH591_CH592)
1530+
#if defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH32L103) || defined(CH571_CH573) || defined(CH582_CH583) || defined(CH591_CH592)
15331531
#define systickcnt_t uint64_t
15341532
#define SYSTICKCNT SysTick->CNT
15351533
#elif defined(CH32V10x) || defined(CH570_CH572) || defined(CH584_CH585)
@@ -1579,7 +1577,7 @@ void DelaySysTick( uint32_t n )
15791577
#if defined(CH32V003) || defined(CH32V00x)
15801578
uint32_t targend = SysTick->CNT + n;
15811579
while( ((int32_t)( SysTick->CNT - targend )) < 0 );
1582-
#elif defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH582_CH583) || defined(CH591_CH592)
1580+
#elif defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH32L103) || defined(CH582_CH583) || defined(CH591_CH592)
15831581
uint64_t targend = SysTick->CNT + n;
15841582
while( ((int64_t)( SysTick->CNT - targend )) < 0 );
15851583
#elif defined(CH32V10x) || defined(CH570_CH572) || defined(CH584_CH585)
@@ -1672,17 +1670,21 @@ void SystemInit( void )
16721670
SYS_SAFE_ACCESS(
16731671
R8_HFCK_PWR_CTRL |= RB_CLK_PLL_PON;
16741672
R8_FLASH_CFG = 0x01;
1675-
R8_FLASH_SCK |= 1<<4; //50M
1673+
R8_FLASH_SCK |= 1<<4;
16761674
);
16771675
}
1678-
else // 32M div
1676+
else
16791677
{
16801678
SYS_SAFE_ACCESS(
16811679
R8_FLASH_CFG = (sc & 0x1F) ? 0x02 : 0x07;
16821680
);
16831681
}
16841682
SYS_SAFE_ACCESS(
1683+
#ifdef CH570_CH572
16851684
R8_CLK_SYS_CFG = sc;
1685+
#else // CH584_585
1686+
R16_CLK_SYS_CFG = sc;
1687+
#endif
16861688
);
16871689
}
16881690
#else // ch5xx EXCEPT ch570/2 ch584/5
@@ -1696,15 +1698,19 @@ void SystemInit( void )
16961698
R32_CLK_SYS_CFG = (0 << 6) | (sc & 0x1f) | RB_TX_32M_PWR_EN | RB_PLL_PWR_EN;
16971699
);
16981700
ADD_N_NOPS(4);
1699-
R8_FLASH_CFG = 0X51;
1701+
SYS_SAFE_ACCESS(
1702+
R8_FLASH_CFG = 0x51;
1703+
);
17001704
}
17011705
else if(sc & 0x40) // PLL div
17021706
{
17031707
SYS_SAFE_ACCESS(
17041708
R32_CLK_SYS_CFG = (1 << 6) | (sc & 0x1f) | RB_TX_32M_PWR_EN | RB_PLL_PWR_EN;
17051709
);
17061710
ADD_N_NOPS(4);
1707-
R8_FLASH_CFG = 0x52;
1711+
SYS_SAFE_ACCESS(
1712+
R8_FLASH_CFG = 0x52;
1713+
);
17081714
}
17091715
else
17101716
{
@@ -1718,7 +1724,7 @@ void SystemInit( void )
17181724
);
17191725
#endif // switch between ch570/2 and other ch5xx
17201726
#elif defined(FUNCONF_USE_HSI) && FUNCONF_USE_HSI
1721-
#if defined(CH32V30x) || defined(CH32V20x) || defined(CH32V10x)
1727+
#if defined(CH32V30x) || defined(CH32V20x) || defined(CH32L103) || defined(CH32V10x)
17221728
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
17231729
#endif
17241730
#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL
@@ -1776,6 +1782,17 @@ void SystemInit( void )
17761782
#endif
17771783
#endif
17781784

1785+
#if defined(CH32L103)
1786+
// Per TRM
1787+
#if FUNCONF_SYSTEM_CORE_CLOCK > 72000000
1788+
FLASH->ACTLR = FLASH_ACTLR_LATENCY_2;
1789+
#elif FUNCONF_SYSTEM_CORE_CLOCK > 40000000
1790+
FLASH->ACTLR = FLASH_ACTLR_LATENCY_1;
1791+
#else
1792+
FLASH->ACTLR = FLASH_ACTLR_LATENCY_0;
1793+
#endif
1794+
#endif
1795+
17791796
#ifndef CH5xx
17801797
RCC->INTR = 0x009F0000; // Clear PLL, CSSC, HSE, HSI and LSI ready flags.
17811798
#endif

ch32fun/ch32fun.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
*/
8787

8888
// Sanity check for when porting old code.
89-
#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x)
89+
#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH32L103)
9090
#if defined(CH32V003)
9191
#error Cannot define CH32V003 and another arch.
9292
#endif
@@ -157,7 +157,7 @@
157157
#define HSE_VALUE (24000000) // Value of the External oscillator in Hz, default
158158
#elif defined(CH32V10x)
159159
#define HSE_VALUE (8000000)
160-
#elif defined(CH32V20x)
160+
#elif defined(CH32V20x) || defined(CH32L103)
161161
#if defined(CH32V20x_D8) || defined(CH32V20x_D8W)
162162
#define HSE_VALUE (32000000)
163163
#else
@@ -173,12 +173,12 @@
173173
// Value of the Internal oscillator in Hz, default.
174174
#ifndef HSI_VALUE
175175
#if defined(CH32V003) || defined(CH32V00x)
176-
#define HSI_VALUE (24000000)
176+
#define HSI_VALUE (24000000)
177177
#elif defined(CH32X03x)
178178
#define HSI_VALUE (48000000)
179179
#elif defined(CH32V10x)
180180
#define HSI_VALUE (8000000)
181-
#elif defined(CH32V20x)
181+
#elif defined(CH32V20x) || defined(CH32L103)
182182
#define HSI_VALUE (8000000)
183183
#elif defined(CH32V30x)
184184
#define HSI_VALUE (8000000)
@@ -197,6 +197,9 @@
197197
#if defined(FUNCONF_USE_PLL) && FUNCONF_USE_PLL
198198
#if defined(CH32V10x)
199199
#define FUNCONF_PLL_MULTIPLIER 10 // Default: 8 * 10 = 80 MHz
200+
#elif defined(CH32L103)
201+
#define FUNCONF_PLL_MULTIPLIER 12 // Default: 8 * 12 = 96 MHz
202+
// Note: Can be overclocked to 144 MHz
200203
#elif defined(CH32V20x)
201204
#define FUNCONF_PLL_MULTIPLIER 18 // Default: 8 * 18 = 144 MHz
202205
#elif defined(CH32V30x)
@@ -299,6 +302,9 @@
299302

300303
#endif
301304

305+
#ifndef WEAK
306+
#define WEAK __attribute__((weak))
307+
#endif
302308

303309
#ifdef __cplusplus
304310
extern "C" {
@@ -377,6 +383,8 @@ typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
377383
#include "ch32x03xhw.h"
378384
#elif defined( CH32V10x )
379385
#include "ch32v10xhw.h"
386+
#elif defined( CH32L103 )
387+
#include "ch32l103hw.h"
380388
#elif defined( CH32V20x )
381389
#include "ch32v20xhw.h"
382390
#elif defined( CH32V30x )
@@ -912,7 +920,7 @@ RV_STATIC_INLINE void funPinMode(u32 pin, GPIOModeTypeDef mode)
912920
#if defined(CH32X03x)
913921
#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC ); }
914922
#define funPinMode( pin, mode ) { *((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3)) = ( (*((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3))) & (~(0xf<<(4*((pin)&0x7))))) | ((mode)<<(4*((pin)&0x7))); }
915-
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
923+
#elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x) || defined(CH32L103)
916924
#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ); }
917925
#define funPinMode( pin, mode ) { *((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3)) = ( (*((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3))) & (~(0xf<<(4*((pin)&0x7))))) | ((mode)<<(4*((pin)&0x7))); }
918926
#define funGpioInitB() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB ); }

ch32fun/ch32fun.mk

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
ifeq ($(OS),Windows_NT)
22
WHICH:=where
3+
NULLDEV:=nul
34
else
45
WHICH:=which
6+
NULLDEV:=/dev/null
57
endif
68

79
# Default/fallback prefix
810
PREFIX_DEFAULT:=riscv64-elf
911

10-
ifneq ($(shell $(WHICH) riscv64-unknown-elf-gcc),)
12+
ifneq ($(shell $(WHICH) riscv64-unknown-elf-gcc 2>$(NULLDEV)),)
1113
PREFIX_DEFAULT:=riscv64-unknown-elf
12-
else ifneq ($(shell $(WHICH) riscv-none-elf-gcc),)
14+
else ifneq ($(shell $(WHICH) riscv-none-elf-gcc 2>$(NULLDEV)),)
1315
PREFIX_DEFAULT:=riscv-none-elf
1416
endif
1517

@@ -42,7 +44,11 @@ endif
4244
CFLAGS?=-g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8
4345
LDFLAGS+=-Wl,--print-memory-usage -Wl,-Map=$(TARGET).map
4446

45-
GCCVERSION13 := $(shell expr `$(PREFIX)-gcc -dumpversion | cut -f1 -d.` \>= 13)
47+
# Get GCC major version in a shell-agnostic way
48+
GCCVERSION := $(shell $(PREFIX)-gcc -dumpversion)
49+
GCCMAJOR := $(firstword $(subst ., ,$(GCCVERSION)))
50+
# every major version below 13 maps to 0, anything 13 or above maps to 1
51+
GCCVERSION13 := $(if $(filter 1 2 3 4 5 6 7 8 9 10 11 12,$(GCCMAJOR)),0,1)
4652

4753
ifeq ($(findstring CH32V00,$(TARGET_MCU)),CH32V00) # CH32V002, 3, 4, 5, 6, 7
4854
MCU_PACKAGE?=1
@@ -115,6 +121,24 @@ else ifeq ($(findstring CH32X03,$(TARGET_MCU)),CH32X03) # CH32X033, X035
115121
endif
116122

117123
TARGET_MCU_LD:=4
124+
else ifeq ($(findstring CH32L103,$(TARGET_MCU)),CH32L103) # CH32L103
125+
TARGET_MCU_PACKAGE?=CH32L103C8T6
126+
CFLAGS_ARCH+=-march=rv32imac \
127+
-mabi=ilp32 \
128+
-DCH32L103=1
129+
130+
# MCU Flash/RAM split
131+
ifeq ($(findstring F8, $(TARGET_MCU_PACKAGE)), F8)
132+
MCU_PACKAGE:=1
133+
else ifeq ($(findstring K8, $(TARGET_MCU_PACKAGE)), K8)
134+
MCU_PACKAGE:=1
135+
else ifeq ($(findstring C8, $(TARGET_MCU_PACKAGE)), C8)
136+
MCU_PACKAGE:=1
137+
else ifeq ($(findstring G8, $(TARGET_MCU_PACKAGE)), G8)
138+
MCU_PACKAGE:=1
139+
endif
140+
141+
TARGET_MCU_LD:=4
118142
else ifeq ($(findstring CH32V20,$(TARGET_MCU)),CH32V20) # CH32V203
119143
TARGET_MCU_PACKAGE?=CH32V203F6P6
120144
CFLAGS_ARCH+= -march=rv32imac \

0 commit comments

Comments
 (0)