From b75429b1a87c0beb9a804064894403ab3361cc8a Mon Sep 17 00:00:00 2001
From: cnlohr <lohr85@gmail.com>
Date: Tue, 13 Feb 2024 03:35:04 -0500
Subject: [PATCH] * Found a slightly more robus reset mechanism for programming
 * Output UART PRINTF on a default Port A9

---
 ch32v003fun/ch32v003fun.c  | 10 +++++++++-
 ch32v003fun/ch32v003fun.h  |  6 ++++++
 minichlink/pgm-wch-linke.c |  3 ++-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index c6582da..b933308 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -1211,13 +1211,21 @@ void handle_reset( void )
 #if defined( FUNCONF_USE_UARTPRINTF ) && FUNCONF_USE_UARTPRINTF
 void SetupUART( int uartBRR )
 {
+#ifdef CH32V003
 	// Enable GPIOD and UART.
 	RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1;
 
 	// Push-Pull, 10MHz Output, GPIO D5, with AutoFunction
 	GPIOD->CFGLR &= ~(0xf<<(4*5));
 	GPIOD->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*5);
-	
+#else
+	RCC->APB2PCENR |= RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1;
+
+	// Push-Pull, 10MHz Output, GPIO A9, with AutoFunction
+	GPIOA->CFGHR &= ~(0xf<<(4*1));
+	GPIOA->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*1);
+#endif
+
 	// 115200, 8n1.  Note if you don't specify a mode, UART remains off even when UE_Set.
 	USART1->CTLR1 = USART_WordLength_8b | USART_Parity_No | USART_Mode_Tx;
 	USART1->CTLR2 = USART_StopBits_1;
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index cf6e7e3..ca69c05 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -30,6 +30,12 @@
 		poll_input, handle_debug_input may be used with semihsoting.
 
 		Note: Semihosting appears to currently only work on the CH32V003 (may be improved in future)
+
+	For UART printf, on:
+		CH32V003, Port D5, 115200 8n1
+		CH32V203, Port A9, 115200 8n1
+
+	Modifications can be made to SetupUart, or your own version as desired.
 */
 
 
diff --git a/minichlink/pgm-wch-linke.c b/minichlink/pgm-wch-linke.c
index 36f4143..b37ae82 100644
--- a/minichlink/pgm-wch-linke.c
+++ b/minichlink/pgm-wch-linke.c
@@ -337,9 +337,11 @@ static int LESetupInterface( void * d )
 				return -1;
 
 			wch_link_multicommands( (libusb_device_handle *)dev, 1, 4, "\x81\x0d\x01\x13" ); // Try forcing reset line low.
+			wch_link_command( (libusb_device_handle *)dev, "\x81\x0d\x01\xff", 4, 0, 0, 0); //Exit programming
 
 			if( already_tried_reset > 3 )
 			{
+				MCF.DelayUS( iss, 5000 );
 				wch_link_command( dev, "\x81\x0d\x01\x03", 4, (int*)&transferred, rbuff, 1024 ); // Reply: Ignored, 820d050900300500
 			}
 			else
@@ -347,7 +349,6 @@ static int LESetupInterface( void * d )
 				MCF.DelayUS( iss, 5000 );
 			}
 
-			wch_link_multicommands( (libusb_device_handle *)dev, 3, 4, "\x81\x0b\x01\x01", 4, "\x81\x0d\x01\x02", 4, "\x81\x0d\x01\xff" );
 			wch_link_multicommands( (libusb_device_handle *)dev, 1, 4, "\x81\x0d\x01\x14" ); // Release reset line.
 			wch_link_multicommands( (libusb_device_handle *)dev, 3, 4, "\x81\x0b\x01\x01", 4, "\x81\x0d\x01\x02", 4, "\x81\x0d\x01\xff" );
 			already_tried_reset++;
-- 
GitLab