From dceb8e12d7b0aeaa6034967a3767f4d9edf186df Mon Sep 17 00:00:00 2001
From: cnlohr <lohr85@gmail.com>
Date: Mon, 12 Feb 2024 05:33:10 -0500
Subject: [PATCH]  * Switch blink example to use ch32v003fun  * Unify linker
 scripts  * Switch Init section to be "used"  * Switch ch32v203 to be
 rv32imac, not imafc

---
 ch32v003fun/ch32v003fun.c   |   2 +-
 ch32v003fun/ch32v003fun.h   |  48 +++++++++-
 ch32v003fun/ch32v003fun.ld  |  37 +++++++
 ch32v003fun/ch32v10xfun.ld  | 179 ----------------------------------
 ch32v003fun/ch32v10xfun.mk  |   4 +-
 ch32v003fun/ch32v20xfun.ld  | 186 ------------------------------------
 ch32v003fun/ch32v20xfun.mk  |   8 +-
 ch32v003fun/ch32v30xfun.ld  | 179 ----------------------------------
 ch32v003fun/ch32v30xfun.mk  |   4 +-
 examples_v20x/blink/blink.c |  12 +--
 10 files changed, 97 insertions(+), 562 deletions(-)
 delete mode 100644 ch32v003fun/ch32v10xfun.ld
 delete mode 100644 ch32v003fun/ch32v20xfun.ld
 delete mode 100644 ch32v003fun/ch32v30xfun.ld

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index 3a39395..998632a 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -951,7 +951,7 @@ asm volatile(
 
 #elif defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
 
-void Init() 				   __attribute((section(".init")));
+void Init() 				   __attribute((section(".init"))) __attribute((used));
 void InterruptVector()         __attribute__((naked)) __attribute((section(".vector"))) __attribute((weak,alias("InterruptVectorDefault")));
 void InterruptVectorDefault()  __attribute__((naked)) __attribute((section(".vector")));
 
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index 0909961..09a2206 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -12256,9 +12256,55 @@ extern "C" {
 #define PD6 54
 #define PD7 55
 
+
+#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
+#define PA0 0
+#define PA3 3
+#define PA4 4
+#define PA5 5
+#define PA6 6
+#define PA7 7
+#define PA8 8
+#define PA9 9
+#define PA10 10
+#define PA11 11
+#define PA12 12
+#define PA13 13
+#define PA14 14
+#define PA15 15
+#define PB0 16
+#define PB1 17
+#define PB2 18
+#define PB3 19
+#define PB4 20
+#define PB5 21
+#define PB6 22
+#define PB7 23
+#define PB8 24
+#define PB9 25
+#define PB10 26
+#define PB11 27
+#define PB12 28
+#define PB13 29
+#define PB14 30
+#define PB15 31
+#endif
+
+
+// For pins, use things like PA8, PB15
+// For configuration, use things like GPIO_CFGLR_OUT_10Mhz_PP
+
 #define funDigitalWrite( pin, value ) { GpioOf( pin )->BSHR = 1<<((!(value))*16 + ((pin) & 0xf)); }
-#define funPinMode( pin, mode ) { GpioOf(pin)->CFGLR = (GpioOf(pin)->CFGLR & (~(0xf<<(4*((pin)&0xf))))) | ((mode)<<(4*((pin)&0xf))); }
+
+#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32V30x)
+#define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ); }
+#define funPinMode( pin, mode ) { *((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3)) = ( (*((&GpioOf(pin)->CFGLR)+((pin&0x8)>>3))) & (~(0xf<<(4*((pin)&0x7))))) | ((mode)<<(4*((pin)&0x7))); }
+#define funGpioInitB() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB ); }
+#else
 #define funGpioInitAll() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ); }
+#define funPinMode( pin, mode ) { GpioOf(pin)->CFGLR = (GpioOf(pin)->CFGLR & (~(0xf<<(4*((pin)&0xf))))) | ((mode)<<(4*((pin)&0xf))); }
+#endif
+
 #define funGpioInitA() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA ); }
 #define funGpioInitC() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC ); }
 #define funGpioInitD() { RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD ); }
diff --git a/ch32v003fun/ch32v003fun.ld b/ch32v003fun/ch32v003fun.ld
index b834063..108cdc9 100644
--- a/ch32v003fun/ch32v003fun.ld
+++ b/ch32v003fun/ch32v003fun.ld
@@ -2,8 +2,45 @@ ENTRY( InterruptVector )
 
 MEMORY
 {
+#if 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
+	// CH32V003
 	FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 16K
 	RAM (xrw)  : ORIGIN = 0x20000000, LENGTH = 2K
+#endif
 }
 
 SECTIONS
diff --git a/ch32v003fun/ch32v10xfun.ld b/ch32v003fun/ch32v10xfun.ld
deleted file mode 100644
index e4506fe..0000000
--- a/ch32v003fun/ch32v10xfun.ld
+++ /dev/null
@@ -1,179 +0,0 @@
-ENTRY( Init )
-
-__stack_size = 2048;
-
-PROVIDE( _stack_size = __stack_size );
-
-
-MEMORY
-{
-#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
-}
-
-
-SECTIONS
-{
-
-	.init :
-	{
-		_sinit = .;
-		. = ALIGN(4);
-		KEEP(*(SORT_NONE(.init)))
-		. = ALIGN(4);
-		_einit = .;
-	} >FLASH AT>FLASH
-
-    .vector :
-    {
-        *(.vector);
-	    . = ALIGN(64);
-    } >FLASH AT>FLASH
-
-	.text :
-	{
-		. = ALIGN(4);
-		*(.text)
-		*(.text.*)
-		*(.rodata)
-		*(.rodata*)
-		*(.gnu.linkonce.t.*)
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	.fini :
-	{
-		KEEP(*(SORT_NONE(.fini)))
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	PROVIDE( _etext = . );
-	PROVIDE( _eitcm = . );
-
-	.preinit_array  :
-	{
-	  PROVIDE_HIDDEN (__preinit_array_start = .);
-	  KEEP (*(.preinit_array))
-	  PROVIDE_HIDDEN (__preinit_array_end = .);
-	} >FLASH AT>FLASH
-
-	.init_array     :
-	{
-	  PROVIDE_HIDDEN (__init_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
-	  KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
-	  PROVIDE_HIDDEN (__init_array_end = .);
-	} >FLASH AT>FLASH
-
-	.fini_array     :
-	{
-	  PROVIDE_HIDDEN (__fini_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
-	  KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
-	  PROVIDE_HIDDEN (__fini_array_end = .);
-	} >FLASH AT>FLASH
-
-	.ctors          :
-	{
-	  /* gcc uses crtbegin.o to find the start of
-	     the constructors, so we make sure it is
-	     first.  Because this is a wildcard, it
-	     doesn't matter if the user does not
-	     actually link against crtbegin.o; the
-	     linker won't look for a file to match a
-	     wildcard.  The wildcard also means that it
-	     doesn't matter which directory crtbegin.o
-	     is in.  */
-	  KEEP (*crtbegin.o(.ctors))
-	  KEEP (*crtbegin?.o(.ctors))
-	  /* We don't want to include the .ctor section from
-	     the crtend.o file until after the sorted ctors.
-	     The .ctor section from the crtend file contains the
-	     end of ctors marker and it must be last */
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
-	  KEEP (*(SORT(.ctors.*)))
-	  KEEP (*(.ctors))
-	} >FLASH AT>FLASH
-
-	.dtors          :
-	{
-	  KEEP (*crtbegin.o(.dtors))
-	  KEEP (*crtbegin?.o(.dtors))
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
-	  KEEP (*(SORT(.dtors.*)))
-	  KEEP (*(.dtors))
-	} >FLASH AT>FLASH
-
-	.dalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_vma = .);
-	} >RAM AT>FLASH
-
-	.dlalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_lma = .);
-	} >FLASH AT>FLASH
-
-	.data :
-	{
-    	*(.gnu.linkonce.r.*)
-    	*(.data .data.*)
-    	*(.gnu.linkonce.d.*)
-		. = ALIGN(8);
-    	PROVIDE( __global_pointer$ = . + 0x800 );
-    	*(.sdata .sdata.*)
-		*(.sdata2.*)
-    	*(.gnu.linkonce.s.*)
-    	. = ALIGN(8);
-    	*(.srodata.cst16)
-    	*(.srodata.cst8)
-    	*(.srodata.cst4)
-    	*(.srodata.cst2)
-    	*(.srodata .srodata.*)
-    	. = ALIGN(4);
-		PROVIDE( _edata = .);
-	} >RAM AT>FLASH
-
-	.bss :
-	{
-		. = ALIGN(4);
-		PROVIDE( _sbss = .);
-  	    *(.sbss*)
-        *(.gnu.linkonce.sb.*)
-		*(.bss*)
-     	*(.gnu.linkonce.b.*)
-		*(COMMON*)
-		. = ALIGN(4);
-		PROVIDE( _ebss = .);
-	} >RAM AT>FLASH
-
-	PROVIDE( _end = _ebss);
-	PROVIDE( end = . );
-
-    .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
-    {
-        PROVIDE( _heap_end = . );
-        . = ALIGN(4);
-        PROVIDE(_susrstack = . );
-        . = . + __stack_size;
-        PROVIDE( _eusrstack = .);
-    } >RAM
-
-}
-
-
diff --git a/ch32v003fun/ch32v10xfun.mk b/ch32v003fun/ch32v10xfun.mk
index 997e7f1..2fde040 100644
--- a/ch32v003fun/ch32v10xfun.mk
+++ b/ch32v003fun/ch32v10xfun.mk
@@ -30,8 +30,8 @@ LINKER_SCRIPT:=$(GENERATED_LD_FILE)
 FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)
 
 $(GENERATED_LD_FILE) :
-	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE) $(CH32V003FUN)/ch32v10xfun.ld > $(GENERATED_LD_FILE)
+	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE) -DMCU_TYPE=CH32V10x $(CH32V003FUN)/ch32v003fun.ld > $(GENERATED_LD_FILE)
 
 $(TARGET).elf : $(GENERATED_LD_FILE) $(FILES_TO_COMPILE)
 	echo $(FILES_TO_COMPILE)
-	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
\ No newline at end of file
+	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
diff --git a/ch32v003fun/ch32v20xfun.ld b/ch32v003fun/ch32v20xfun.ld
deleted file mode 100644
index 2400a0b..0000000
--- a/ch32v003fun/ch32v20xfun.ld
+++ /dev/null
@@ -1,186 +0,0 @@
-ENTRY( Init )
-
-__stack_size = 2048;
-
-PROVIDE( _stack_size = __stack_size );
-
-MEMORY
-{
-#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
-
-}
-
-
-SECTIONS
-{
-
-	.init :
-	{
-		_sinit = .;
-		. = ALIGN(4);
-		KEEP(*(SORT_NONE(.init)))
-		. = ALIGN(4);
-		_einit = .;
-	} >FLASH AT>FLASH
-
-  .vector :
-  {
-      *(.vector);
-	  . = ALIGN(64);
-  } >FLASH AT>FLASH
-
-	.text :
-	{
-		. = ALIGN(4);
-		*(.text)
-		*(.text.*)
-		*(.rodata)
-		*(.rodata*)
-		*(.glue_7)
-		*(.glue_7t)
-		*(.gnu.linkonce.t.*)
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	.fini :
-	{
-		KEEP(*(SORT_NONE(.fini)))
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	PROVIDE( _etext = . );
-	PROVIDE( _eitcm = . );
-
-	.preinit_array  :
-	{
-	  PROVIDE_HIDDEN (__preinit_array_start = .);
-	  KEEP (*(.preinit_array))
-	  PROVIDE_HIDDEN (__preinit_array_end = .);
-	} >FLASH AT>FLASH
-
-	.init_array     :
-	{
-	  PROVIDE_HIDDEN (__init_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
-	  KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
-	  PROVIDE_HIDDEN (__init_array_end = .);
-	} >FLASH AT>FLASH
-
-	.fini_array     :
-	{
-	  PROVIDE_HIDDEN (__fini_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
-	  KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
-	  PROVIDE_HIDDEN (__fini_array_end = .);
-	} >FLASH AT>FLASH
-
-	.ctors          :
-	{
-	  /* gcc uses crtbegin.o to find the start of
-	     the constructors, so we make sure it is
-	     first.  Because this is a wildcard, it
-	     doesn't matter if the user does not
-	     actually link against crtbegin.o; the
-	     linker won't look for a file to match a
-	     wildcard.  The wildcard also means that it
-	     doesn't matter which directory crtbegin.o
-	     is in.  */
-	  KEEP (*crtbegin.o(.ctors))
-	  KEEP (*crtbegin?.o(.ctors))
-	  /* We don't want to include the .ctor section from
-	     the crtend.o file until after the sorted ctors.
-	     The .ctor section from the crtend file contains the
-	     end of ctors marker and it must be last */
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
-	  KEEP (*(SORT(.ctors.*)))
-	  KEEP (*(.ctors))
-	} >FLASH AT>FLASH
-
-	.dtors          :
-	{
-	  KEEP (*crtbegin.o(.dtors))
-	  KEEP (*crtbegin?.o(.dtors))
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
-	  KEEP (*(SORT(.dtors.*)))
-	  KEEP (*(.dtors))
-	} >FLASH AT>FLASH
-
-	.dalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_vma = .);
-	} >RAM AT>FLASH
-
-	.dlalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_lma = .);
-	} >FLASH AT>FLASH
-
-	.data :
-	{
-    	*(.gnu.linkonce.r.*)
-    	*(.data .data.*)
-    	*(.gnu.linkonce.d.*)
-		. = ALIGN(8);
-    	PROVIDE( __global_pointer$ = . + 0x800 );
-    	*(.sdata .sdata.*)
-		*(.sdata2.*)
-    	*(.gnu.linkonce.s.*)
-    	. = ALIGN(8);
-    	*(.srodata.cst16)
-    	*(.srodata.cst8)
-    	*(.srodata.cst4)
-    	*(.srodata.cst2)
-    	*(.srodata .srodata.*)
-    	. = ALIGN(4);
-		PROVIDE( _edata = .);
-	} >RAM AT>FLASH
-
-	.bss :
-	{
-		. = ALIGN(4);
-		PROVIDE( _sbss = .);
-  	    *(.sbss*)
-        *(.gnu.linkonce.sb.*)
-		*(.bss*)
-     	*(.gnu.linkonce.b.*)
-		*(COMMON*)
-		. = ALIGN(4);
-		PROVIDE( _ebss = .);
-	} >RAM AT>FLASH
-
-	PROVIDE( _end = _ebss);
-	PROVIDE( end = . );
-
-    .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
-    {
-        PROVIDE( _heap_end = . );
-        . = ALIGN(4);
-        PROVIDE(_susrstack = . );
-        . = . + __stack_size;
-        PROVIDE( _eusrstack = .);
-    } >RAM
-
-}
-
-
diff --git a/ch32v003fun/ch32v20xfun.mk b/ch32v003fun/ch32v20xfun.mk
index 4775ac1..9fb171a 100644
--- a/ch32v003fun/ch32v20xfun.mk
+++ b/ch32v003fun/ch32v20xfun.mk
@@ -4,8 +4,8 @@ MCU_PACKAGE?=1
 CFLAGS+= \
 	-g -Os -flto -ffunction-sections -fdata-sections \
 	-static-libgcc \
-	-march=rv32imafc \
-	-mabi=ilp32f \
+	-march=rv32imac \
+	-mabi=ilp32 \
 	-msmall-data-limit=8 \
 	-mno-save-restore \
 	-fmessage-length=0 \
@@ -57,8 +57,8 @@ LINKER_SCRIPT:=$(GENERATED_LD_FILE)
 FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)
 
 $(GENERATED_LD_FILE) :
-	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE) $(CH32V003FUN)/ch32v20xfun.ld > $(GENERATED_LD_FILE)
+	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE) -DMCU_TYPE=CH32V20x $(CH32V003FUN)/ch32v003fun.ld > $(GENERATED_LD_FILE)
 
 $(TARGET).elf : $(GENERATED_LD_FILE) $(FILES_TO_COMPILE)
 	echo $(FILES_TO_COMPILE)
-	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
\ No newline at end of file
+	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
diff --git a/ch32v003fun/ch32v30xfun.ld b/ch32v003fun/ch32v30xfun.ld
deleted file mode 100644
index 3fca444..0000000
--- a/ch32v003fun/ch32v30xfun.ld
+++ /dev/null
@@ -1,179 +0,0 @@
-ENTRY( Init )
-
-__stack_size = 2048;
-
-PROVIDE( _stack_size = __stack_size );
-
-
-MEMORY
-{
-#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
-}
-
-
-SECTIONS
-{
-
-	.init :
-	{
-		_sinit = .;
-		. = ALIGN(4);
-		KEEP(*(SORT_NONE(.init)))
-		. = ALIGN(4);
-		_einit = .;
-	} >FLASH AT>FLASH
-
-  .vector :
-  {
-      *(.vector);
-	  . = ALIGN(64);
-  } >FLASH AT>FLASH
-
-	.text :
-	{
-		. = ALIGN(4);
-		*(.text)
-		*(.text.*)
-		*(.rodata)
-		*(.rodata*)
-		*(.gnu.linkonce.t.*)
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	.fini :
-	{
-		KEEP(*(SORT_NONE(.fini)))
-		. = ALIGN(4);
-	} >FLASH AT>FLASH
-
-	PROVIDE( _etext = . );
-	PROVIDE( _eitcm = . );
-
-	.preinit_array  :
-	{
-	  PROVIDE_HIDDEN (__preinit_array_start = .);
-	  KEEP (*(.preinit_array))
-	  PROVIDE_HIDDEN (__preinit_array_end = .);
-	} >FLASH AT>FLASH
-
-	.init_array     :
-	{
-	  PROVIDE_HIDDEN (__init_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
-	  KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
-	  PROVIDE_HIDDEN (__init_array_end = .);
-	} >FLASH AT>FLASH
-
-	.fini_array     :
-	{
-	  PROVIDE_HIDDEN (__fini_array_start = .);
-	  KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
-	  KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
-	  PROVIDE_HIDDEN (__fini_array_end = .);
-	} >FLASH AT>FLASH
-
-	.ctors          :
-	{
-	  /* gcc uses crtbegin.o to find the start of
-	     the constructors, so we make sure it is
-	     first.  Because this is a wildcard, it
-	     doesn't matter if the user does not
-	     actually link against crtbegin.o; the
-	     linker won't look for a file to match a
-	     wildcard.  The wildcard also means that it
-	     doesn't matter which directory crtbegin.o
-	     is in.  */
-	  KEEP (*crtbegin.o(.ctors))
-	  KEEP (*crtbegin?.o(.ctors))
-	  /* We don't want to include the .ctor section from
-	     the crtend.o file until after the sorted ctors.
-	     The .ctor section from the crtend file contains the
-	     end of ctors marker and it must be last */
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
-	  KEEP (*(SORT(.ctors.*)))
-	  KEEP (*(.ctors))
-	} >FLASH AT>FLASH
-
-	.dtors          :
-	{
-	  KEEP (*crtbegin.o(.dtors))
-	  KEEP (*crtbegin?.o(.dtors))
-	  KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
-	  KEEP (*(SORT(.dtors.*)))
-	  KEEP (*(.dtors))
-	} >FLASH AT>FLASH
-
-	.dalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_vma = .);
-	} >RAM AT>FLASH
-
-	.dlalign :
-	{
-		. = ALIGN(4);
-		PROVIDE(_data_lma = .);
-	} >FLASH AT>FLASH
-
-	.data :
-	{
-    	*(.gnu.linkonce.r.*)
-    	*(.data .data.*)
-    	*(.gnu.linkonce.d.*)
-		. = ALIGN(8);
-    	PROVIDE( __global_pointer$ = . + 0x800 );
-    	*(.sdata .sdata.*)
-		*(.sdata2.*)
-    	*(.gnu.linkonce.s.*)
-    	. = ALIGN(8);
-    	*(.srodata.cst16)
-    	*(.srodata.cst8)
-    	*(.srodata.cst4)
-    	*(.srodata.cst2)
-    	*(.srodata .srodata.*)
-    	. = ALIGN(4);
-		PROVIDE( _edata = .);
-	} >RAM AT>FLASH
-
-	.bss :
-	{
-		. = ALIGN(4);
-		PROVIDE( _sbss = .);
-  	    *(.sbss*)
-        *(.gnu.linkonce.sb.*)
-		*(.bss*)
-     	*(.gnu.linkonce.b.*)
-		*(COMMON*)
-		. = ALIGN(4);
-		PROVIDE( _ebss = .);
-	} >RAM AT>FLASH
-
-	PROVIDE( _end = _ebss);
-	PROVIDE( end = . );
-
-    .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
-    {
-        PROVIDE( _heap_end = . );
-        . = ALIGN(4);
-        PROVIDE(_susrstack = . );
-        . = . + __stack_size;
-        PROVIDE( _eusrstack = .);
-    } >RAM
-
-}
-
-
diff --git a/ch32v003fun/ch32v30xfun.mk b/ch32v003fun/ch32v30xfun.mk
index 25f1528..ecf61f4 100644
--- a/ch32v003fun/ch32v30xfun.mk
+++ b/ch32v003fun/ch32v30xfun.mk
@@ -43,8 +43,8 @@ LINKER_SCRIPT:=$(GENERATED_LD_FILE)
 FILES_TO_COMPILE:=$(SYSTEM_C) $(TARGET).$(TARGET_EXT) $(ADDITIONAL_C_FILES)
 
 $(GENERATED_LD_FILE) :
-	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE) $(CH32V003FUN)/ch32v30xfun.ld > $(GENERATED_LD_FILE)
+	$(PREFIX)-gcc -E -P -x c -DMCU_PACKAGE=$(MCU_PACKAGE)  -DMCU_TYPE=CH32V20x $(CH32V003FUN)/ch32v003fun.ld > $(GENERATED_LD_FILE)
 
 
 $(TARGET).elf : $(GENERATED_LD_FILE) $(FILES_TO_COMPILE)
-	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
\ No newline at end of file
+	$(PREFIX)-gcc -o $@ $(FILES_TO_COMPILE) $(CFLAGS) $(LDFLAGS)
diff --git a/examples_v20x/blink/blink.c b/examples_v20x/blink/blink.c
index 7ac84c2..f3e2186 100644
--- a/examples_v20x/blink/blink.c
+++ b/examples_v20x/blink/blink.c
@@ -5,19 +5,15 @@ int main()
 {
 	SystemInit();
 
-	// Enable GPIOs
-	//RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC;
-	RCC->APB2PCENR |= RCC_APB2Periph_GPIOA;
+	funGpioInitAll();
 
-	// GPIO A15 Push-Pull
-	GPIOA->CFGHR &= ~((uint32_t)0x0F<<(4*7));
-	GPIOA->CFGHR |= (uint32_t)(GPIO_Speed_10MHz | GPIO_CNF_OUT_PP)<<(4*7);
+	funPinMode( PA15, GPIO_CFGLR_OUT_10Mhz_PP );
 
 	while(1)
 	{
-		GPIOA->BSHR = (1<<15);	 // Turn on GPIO
+		funDigitalWrite( PA15, FUN_HIGH );	 // Turn on GPIO
 		Delay_Ms( 1000 );
-		GPIOA->BSHR = (1<<(16+15)); // Turn off GPIO
+		funDigitalWrite( PA15, FUN_LOW );	 // Turn off GPIO
 		Delay_Ms( 1000 );
 	}
 }
-- 
GitLab