From e5ef53404effbe3540ad1faf56424cbc1201221e Mon Sep 17 00:00:00 2001
From: Carsten Thiele <software@carsten-thiele.de>
Date: Tue, 21 Mar 2023 22:31:54 +0100
Subject: [PATCH] configurable HSITRIM Value in SystemInit48HSI()

---
 ch32v003fun/ch32v003fun.c | 8 ++++----
 ch32v003fun/ch32v003fun.h | 3 +++
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index 90ecd03..0ef8f65 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -807,10 +807,10 @@ asm volatile(
 void SystemInit48HSI( void )
 {
 	// Values lifted from the EVT.  There is little to no documentation on what this does.
-	RCC->CFGR0 = RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2; // PLLCLK = HSI * 2 = 48 MHz; HCLK = SYSCLK = APB1
-	RCC->CTLR  |= RCC_HSION | RCC_PLLON;              // Use HSI, but enable PLL.
-	FLASH->ACTLR = FLASH_ACTLR_LATENCY_1;             // 1 Cycle Latency
-	RCC->INTR  = 0x009F0000;                          // Clear PLL, CSSC, HSE, HSI and LSI ready flags.
+	RCC->CFGR0 = RCC_HPRE_DIV1 | RCC_PLLSRC_HSI_Mul2;     // PLLCLK = HSI * 2 = 48 MHz; HCLK = SYSCLK = APB1
+	RCC->CTLR  |= RCC_HSION | RCC_PLLON | (3 << HSITRIM); // Use HSI, but enable PLL.
+	FLASH->ACTLR = FLASH_ACTLR_LATENCY_1;                 // 1 Cycle Latency
+	RCC->INTR  = 0x009F0000;                              // Clear PLL, CSSC, HSE, HSI and LSI ready flags.
 
 	// From SetSysClockTo_48MHZ_HSI
 	while((RCC->CTLR & RCC_PLLRDY) == 0);                                      // Wait till PLL is ready
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index 9f0e97a..628640f 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -56,6 +56,9 @@ extern "C" {
 
 #define HSI_VALUE                 ((uint32_t)24000000) /* Value of the Internal oscillator in Hz */
 
+#ifndef HSITRIM
+    #define HSITRIM 0x10
+#endif
 
 /* Interrupt Number Definition, according to the selected device */
 typedef enum IRQn
-- 
GitLab