From 78121972495669910eac11370925d32b7d4bdd72 Mon Sep 17 00:00:00 2001
From: CNLohr <lohr85@gmail.com>
Date: Fri, 12 May 2023 02:26:46 -0400
Subject: [PATCH] Allow SYSTICK to be at HCLK instead of HCLK/8.

---
 ch32v003fun/ch32v003fun.c |  2 +-
 ch32v003fun/ch32v003fun.h | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index cd0d32e..056f3cd 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -802,7 +802,7 @@ asm volatile(
 	bne a1, a2, 1b\n\
 2:\n" );
 
-	SysTick->CTLR = 1;
+	SETUP_SYSTICK_HCLK
 
 	// set mepc to be main as the root app.
 asm volatile(
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index f75a27e..3273ce8 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -4819,8 +4819,19 @@ static inline uint32_t __get_SP(void)
 extern "C" {
 #endif
 
+// You can use SYSTICK_USE_HCLK, if you do, you will have a high-resolution
+// however it will limit your max delay to 44 seconds before it will wrap
+// around.  You must also call SETUP_SYSTICK_HCLK.
+
+#ifdef SYSTICK_USE_HCLK
+#define DELAY_US_TIME ((SYSTEM_CORE_CLOCK)/1000000)
+#define DELAY_MS_TIME ((SYSTEM_CORE_CLOCK)/1000)
+#define SETUP_SYSTICK_HCLK SysTick->CTLR = 5;
+#else // Use systick = hclk/8
 #define DELAY_US_TIME ((SYSTEM_CORE_CLOCK)/8000000)
 #define DELAY_MS_TIME ((SYSTEM_CORE_CLOCK)/8000)
+#define SETUP_SYSTICK_HCLK SysTick->CTLR = 1;
+#endif
 
 #if defined(__riscv) || defined(__riscv__) || defined( CH32V003FUN_BASE )
 
-- 
GitLab