From 526d9c5af29c8a6aedf7ff33c71c2991489fc46a Mon Sep 17 00:00:00 2001
From: cnlohr <lohr85@gmail.com>
Date: Fri, 19 Apr 2024 02:10:48 -0700
Subject: [PATCH] Get systick up and running + debug printf + input.

---
 ch32v003fun/ch32v003fun.c                     |  1 -
 ch32v003fun/ch32v003fun.h                     | 15 +++++++-
 examples_x035/debugprintfdemo/Makefile        | 10 +++++
 .../debugprintfdemo/debugprintfdemo.c         | 37 +++++++++++++++++++
 examples_x035/debugprintfdemo/funconfig.h     |  9 +++++
 5 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 examples_x035/debugprintfdemo/Makefile
 create mode 100644 examples_x035/debugprintfdemo/debugprintfdemo.c
 create mode 100644 examples_x035/debugprintfdemo/funconfig.h

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index 4af6ef5..252899e 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -1015,7 +1015,6 @@ asm volatile(
 #endif
 );
 
-
 #if defined( FUNCONF_SYSTICK_USE_HCLK ) && FUNCONF_SYSTICK_USE_HCLK
 	SysTick->CTLR = 5;
 #else
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index 572f5c7..f7823c2 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -11710,7 +11710,20 @@ typedef struct
 	__IO uint64_t CMP;
 } SysTick_Type;
 
-#elif defined(CH32V10x) || defined(CH32X03x)
+#elif defined(CH32X03x)
+
+/* memory mapped structure for SysTick */
+typedef struct
+{
+  __IO uint32_t CTLR;
+  __IO uint32_t SR;
+  __IO uint32_t CNTL;
+  __IO uint32_t CNTH;
+  __IO uint32_t CMPL;
+  __IO uint32_t CMPH;
+} SysTick_Type;
+
+#elif defined(CH32V10x)
 
 /* memory mapped structure for SysTick */
 typedef struct
diff --git a/examples_x035/debugprintfdemo/Makefile b/examples_x035/debugprintfdemo/Makefile
new file mode 100644
index 0000000..8a29f56
--- /dev/null
+++ b/examples_x035/debugprintfdemo/Makefile
@@ -0,0 +1,10 @@
+all : flash
+
+TARGET:=debugprintfdemo
+TARGET_MCU:=CH32X035
+
+include ../../ch32v003fun/ch32v003fun.mk
+
+flash : cv_flash
+clean : cv_clean
+
diff --git a/examples_x035/debugprintfdemo/debugprintfdemo.c b/examples_x035/debugprintfdemo/debugprintfdemo.c
new file mode 100644
index 0000000..3c65c32
--- /dev/null
+++ b/examples_x035/debugprintfdemo/debugprintfdemo.c
@@ -0,0 +1,37 @@
+/* Small example showing how to use the SWIO programming pin to 
+   do printf through the debug interface */
+
+#include "ch32v003fun.h"
+#include <stdio.h>
+
+uint32_t count;
+
+int last = 0;
+void handle_debug_input( int numbytes, uint8_t * data )
+{
+	last = data[0];
+	count += numbytes;
+}
+
+int main()
+{
+	SystemInit();
+
+	funGpioInitAll();
+
+	funPinMode( PA0, GPIO_CFGLR_OUT_10Mhz_PP );
+
+	while(1)
+	{
+		GPIOA->BSHR = 1;	 // Turn on GPIOs
+		printf( "+%lu %lu\n", count++ );
+		Delay_Ms(100);
+		int i;
+		for( i = 0; i < 10000; i++ )
+			poll_input();
+		GPIOA->BSHR = (1<<16); // Turn off GPIODs
+		printf( "-%lu[%c]\n", count++, last );
+		Delay_Ms(100);
+	}
+}
+
diff --git a/examples_x035/debugprintfdemo/funconfig.h b/examples_x035/debugprintfdemo/funconfig.h
new file mode 100644
index 0000000..a8a70b3
--- /dev/null
+++ b/examples_x035/debugprintfdemo/funconfig.h
@@ -0,0 +1,9 @@
+#ifndef _FUNCONFIG_H
+#define _FUNCONFIG_H
+
+// Though this should be on by default we can extra force it on.
+#define FUNCONF_USE_DEBUGPRINTF 1
+#define FUNCONF_DEBUGPRINTF_TIMEOUT (1<<31) // Wait for a very very long time.
+
+#endif
+
-- 
GitLab