From 8b34c6b1cd88af35abaa677fdacdcd075cd27016 Mon Sep 17 00:00:00 2001
From: Imbus <>
Date: Sun, 9 Feb 2025 15:12:27 +0100
Subject: [PATCH] Working

---
 Makefile    | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 blink.c     | 35 +++++++++++++++++++++++++++++
 funconfig.h |  7 ++++++
 3 files changed, 107 insertions(+)
 create mode 100644 Makefile
 create mode 100644 blink.c
 create mode 100644 funconfig.h

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9cc111a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,65 @@
+REV := master
+BASE := https://raw.githubusercontent.com/cnlohr/ch32v003fun/$(REV)/ch32fun
+CURL_FLAGS := -O -\# --fail --location --tlsv1.2 --proto =https --max-time 300
+
+TARGET = blink
+TARGET_MCU?=CH32V003
+NEWLIB=/usr/arm-none-eabi/include
+CFLAGS=-g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8
+LDFLAGS+=-Wl,--print-memory-usage -Wl,-Map=$(TARGET).map
+PREFIX := riscv64-linux-gnu
+CC := $(PREFIX)-gcc
+
+# riscv64-linux-gnu-gcc -E -P -x c -DTARGET_MCU=CH32V003 -DMCU_PACKAGE= -DTARGET_MCU_LD=0 -DTARGET_MCU_MEMORY_SPLIT= ../../ch32fun/ch32fun.ld > ../../ch32fun/generated_ch32v003.ld
+# riscv64-linux-gnu-gcc -o blink.elf ../../ch32fun/ch32fun.c blink.c   -g -Os -flto -ffunction-sections -fdata-sections -fmessage-length=0 -msmall-data-limit=8 -march=rv32ec -mabi=ilp32e -DCH32V003=1 -static-libgcc -I/usr/arm-none-eabi/include -I../../ch32fun/../extralibs -I../../ch32fun -nostdlib -I. -Wall  -Wl,--print-memory-usage -Wl,-Map=blink.map -L../../ch32fun/../misc -lgcc -T ../../ch32fun/generated_ch32v003.ld -Wl,--gc-sections
+
+# Correct?
+EXTFLAGS := -march=rv32ec -mabi=ilp32e -DCH32V003=1 -static-libgcc -I/usr/arm-none-eabi/include -nostdlib -I. -Wall -L. -lgcc -T ch32v003.ld -Wl,--gc-sections
+
+default: $(TARGET).bin
+
+$(TARGET).elf: ch32fun.c blink.c | ch32v003.ld ch32fun.h ch32v003hw.h
+	@echo CC $@
+	@$(CC) $(CFLAGS) $(LDFLAGS) $(EXTFLAGS) -o $@ $^
+
+ch32v003.ld: ch32fun.ld
+	@riscv64-linux-gnu-gcc -E -P -x c -DTARGET_MCU=$(TARGET_MCU) -DMCU_PACKAGE= -DTARGET_MCU_LD=0 -DTARGET_MCU_MEMORY_SPLIT= $< > $@
+
+ch32fun.ld:
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.ld
+
+ch32fun.c:
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.c
+
+ch32v003hw.h:
+	@curl $(CURL_FLAGS) $(BASE)/ch32v003hw.h
+
+ch32fun.h:
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.h
+
+deps:
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.ld
+	@curl $(CURL_FLAGS) $(BASE)/ch32v003hw.h
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.c
+	@curl $(CURL_FLAGS) $(BASE)/ch32fun.h
+
+$(TARGET).bin : $(TARGET).elf
+	$(PREFIX)-objdump -S $^ > $(TARGET).lst
+	$(PREFIX)-objcopy -O binary $< $(TARGET).bin
+	$(PREFIX)-objcopy -O ihex $< $(TARGET).hex
+
+flash:
+	minichlink -w $(TARGET).bin flash -b
+
+clean:
+	rm -f ch32*.[ch]
+	rm -f *.ld
+	rm -f *.hex
+	rm -f *.bin
+	rm -f *.map
+	rm -f *.lst
+	rm -f *.elf
+	rm -f *.ld
+
+.PHONY: clean flash deps default
+
diff --git a/blink.c b/blink.c
new file mode 100644
index 0000000..b44ceba
--- /dev/null
+++ b/blink.c
@@ -0,0 +1,35 @@
+#include "ch32fun.h"
+#include <stdio.h>
+
+// use defines to make more meaningful names for our GPIO pins
+#define PIN_1 PD0
+#define PIN_K PD4
+#define PIN_BOB PD6
+#define PIN_KEVIN PC0
+
+int main()
+{
+	SystemInit();
+
+	// Enable GPIOs
+	funGpioInitAll();
+	
+	funPinMode( PIN_1,     GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
+	funPinMode( PIN_K,     GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
+	funPinMode( PIN_BOB,   GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
+	funPinMode( PIN_KEVIN, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
+
+	while(1)
+	{
+		funDigitalWrite( PIN_1,     FUN_HIGH );
+		funDigitalWrite( PIN_K,     FUN_HIGH );
+		funDigitalWrite( PIN_BOB,   FUN_HIGH );
+		funDigitalWrite( PIN_KEVIN, FUN_HIGH );
+		Delay_Ms( 250 );
+		funDigitalWrite( PIN_1,     FUN_LOW );
+		funDigitalWrite( PIN_K,     FUN_LOW );
+		funDigitalWrite( PIN_BOB,   FUN_LOW );
+		funDigitalWrite( PIN_KEVIN, FUN_LOW );
+		Delay_Ms( 250 );
+	}
+}
diff --git a/funconfig.h b/funconfig.h
new file mode 100644
index 0000000..998cf76
--- /dev/null
+++ b/funconfig.h
@@ -0,0 +1,7 @@
+#ifndef _FUNCONFIG_H
+#define _FUNCONFIG_H
+
+#define CH32V003           1
+
+#endif
+