From 38efaf04756845be2c51756c277b06720b87bd12 Mon Sep 17 00:00:00 2001
From: cnlohr <lohr85@gmail.com>
Date: Fri, 21 Apr 2023 07:02:03 -0400
Subject: [PATCH] Allow ch32v003fun apps to use the debug registers.

---
 ch32v003fun/ch32v003fun.c |  9 ++++++---
 ch32v003fun/ch32v003fun.h | 10 +++++++++-
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/ch32v003fun/ch32v003fun.c b/ch32v003fun/ch32v003fun.c
index 262e4cd..6972e55 100644
--- a/ch32v003fun/ch32v003fun.c
+++ b/ch32v003fun/ch32v003fun.c
@@ -897,9 +897,12 @@ int putchar(int c)
 	return 1;
 }
 #else
-// For debug writing to the debug interface.
-#define DMDATA0 ((volatile uint32_t*)0xe00000f4)
-#define DMDATA1 ((volatile uint32_t*)0xe00000f8)
+
+//           MSB .... LSB
+// DMDATA0: char3 char2 char1 [status word]
+// where [status word] is:
+//   b7 = is a "printf" waiting?
+//   b0..b3 = # of bytes in printf (+4).  (4 or higher indicates a print of some kind)
 
 int _write(int fd, const char *buf, int size)
 {
diff --git a/ch32v003fun/ch32v003fun.h b/ch32v003fun/ch32v003fun.h
index 696a45f..7cd507c 100644
--- a/ch32v003fun/ch32v003fun.h
+++ b/ch32v003fun/ch32v003fun.h
@@ -4823,8 +4823,17 @@ extern "C" {
 #define DELAY_MS_TIME (SYSTEM_CORE_CLOCK / 8000)
 
 #if defined(__riscv) || defined(__riscv__)
+
+// Stuff that can only be compiled on device (not for the programmer, or other host programs)
+
 void handle_reset()            __attribute__((naked)) __attribute((section(".text.handle_reset"))) __attribute__((used));
 void DefaultIRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute__((naked)) __attribute__((used));
+
+
+// For debug writing to the debug interface.
+#define DMDATA0 ((volatile uint32_t*)0xe00000f4)
+#define DMDATA1 ((volatile uint32_t*)0xe00000f8)
+
 #endif
 
 void DelaySysTick( uint32_t n );
@@ -4845,7 +4854,6 @@ void SystemInit24HSI( void );  // No PLL, just raw internal RC oscillator.
 void SystemInitHSE( int HSEBYP );
 void SystemInitHSEPLL( int HSEBYP );
 
-
 #define UART_BAUD_RATE 115200
 #define OVER8DIV 4
 #define INTEGER_DIVIDER (((25 * (APB_CLOCK)) / ((OVER8DIV) * (UART_BAUD_RATE))))
-- 
GitLab