Skip to content

Commit f2790f9

Browse files
committed
Change behavior to:
1. Printf will wait up to 120ms at boot. 2. If printf has timed out, it will immediately continue. 3. If a debugger is reattached, it will keep printing. 4. If a debugger is attached, it will wait up to 120ms. Then continue at full speed.
1 parent a2d7c23 commit f2790f9

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

ch32v003fun/ch32v003fun.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,12 @@ extern uint32_t * _edata;
761761
// If you don't override a specific handler, it will just spin forever.
762762
void DefaultIRQHandler( void )
763763
{
764-
#if FUNCONF_DEBUG_HARDFAULT
765-
// Wait indefinitely for a debugger to attach.
766-
while( !DidDebuggerAttach() );
764+
#if FUNCONF_DEBUG_HARDFAULT && ( FUNCONF_USE_DEBUGPRINTF || FUNCONF_USE_UARTPRINTF )
765+
#if FUNCONF_USE_DEBUGPRINTF
766+
// Wait indefinitely for a printf to become clear.
767+
while( !DebugPrintfBufferFree() );
768+
769+
#endif
767770
printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() );
768771
#endif
769772
// Infinite Loop
@@ -1587,7 +1590,7 @@ static void internal_handle_input( volatile uint32_t * dmdata0 )
15871590
{
15881591
uint32_t dmd0 = *dmdata0;
15891592
int bytes = (dmd0 & 0x3f) - 4;
1590-
if( bytes > 0 )
1593+
if( bytes > 0 && bytes < 16 )
15911594
{
15921595
handle_debug_input( bytes, ((uint8_t*)dmdata0) + 1 );
15931596
}
@@ -1609,14 +1612,18 @@ void poll_input( void )
16091612
// MSB .... LSB
16101613
// DMDATA0: char3 char2 char1 [status word]
16111614
// where [status word] is:
1612-
// b7 = is a "printf" waiting?
1613-
// b0..b3 = # of bytes in printf (+4). (5 or higher indicates a print of some kind)
1615+
// bit 7 = is a "printf" waiting?
1616+
// bit 6 = printf has timed out.
1617+
// bit 0..bit 3 = # of bytes in printf (+4). (5 or higher indicates a print of some kind)
16141618
// note: if b7 is 0 in reply, but b0..b3 have >=4 then we received data from host.
1619+
// Special sentinel:
1620+
// status word = 0x80 = default at start
1621+
// status word = 0xcx = timed out.
16151622
// declare as weak to allow overriding.
16161623
WEAK int _write(int fd, const char *buf, int size)
16171624
{
16181625
(void)fd;
1619-
if( !DidDebuggerAttach() ) return;
1626+
if( ( *DMDATA0 & 0xc0 ) == 0xc0 ) return 0;
16201627

16211628
char buffer[4] = { 0 };
16221629
int place = 0;
@@ -1634,7 +1641,13 @@ WEAK int _write(int fd, const char *buf, int size)
16341641
if( tosend > 7 ) tosend = 7;
16351642

16361643
while( ( lastdmd = (*DMDATA0) ) & 0x80 )
1637-
if( timeout-- == 0 ) return place;
1644+
{
1645+
if( timeout-- == 0 )
1646+
{
1647+
*DMDATA0 |= 0xc0;
1648+
return 0;
1649+
}
1650+
}
16381651

16391652
if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 );
16401653

@@ -1665,16 +1678,24 @@ WEAK int _write(int fd, const char *buf, int size)
16651678
// single to debug intf
16661679
WEAK int putchar(int c)
16671680
{
1668-
if( !DidDebuggerAttach() ) return;
1681+
if( ( *DMDATA0 & 0xc0 ) == 0xc0 ) return 0;
16691682

16701683
int timeout = FUNCONF_DEBUGPRINTF_TIMEOUT;
16711684
uint32_t lastdmd = 0;
16721685

16731686
while( ( lastdmd = (*DMDATA0) ) & 0x80 )
1674-
if( timeout-- == 0 ) return 0;
1687+
{
1688+
if( timeout-- == 0 )
1689+
{
1690+
*DMDATA0 |= 0xc0;
1691+
return 0;
1692+
}
1693+
}
16751694

16761695
// Simply seeking input.
16771696
if( lastdmd ) internal_handle_input( (uint32_t*)DMDATA0 );
1697+
1698+
// Write out character.
16781699
*DMDATA0 = 0x85 | ((const char)c<<8);
16791700
return 1;
16801701
}

ch32v003fun/ch32v003fun.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
8. Default debug behavior, when semihosting:
5151
a. You get access to DidDebuggerAttach() - so you can see if a debugger has attached.
5252
b. WaitForDebuggerToAttach( int timeout_ms ) - if timeout_ms == 0, will wait for forever.
53-
c. If a debugger has attached, printf will wait 120ms (configurable) to make sure it
54-
doesn't drop data. Otherwise, printf fast-path's to exit. It will still do the string
53+
c. printf will wait 120ms (configurable) to make sure it doesn't drop data. Otherwise,
54+
printf will fast-path to exit after the first timeout. It will still do the string
5555
formatting, but will not wait on output. Timeout is configured with
56-
FUNCONF_DEBUGPRINTF_TIMEOUT
56+
FUNCONF_DEBUGPRINTF_TIMEOUT.
5757
d. If you hard fault, it will wait indefinitely for a debugger to attach, once attached,
5858
will printf the fault cause, and the memory address of the fault. Space can be saved
5959
by setting FUNCONF_DEBUG_HARDFAULT to 0.
@@ -14007,9 +14007,10 @@ void SetupUART( int uartBRR );
1400714007
int WaitForDebuggerToAttach( int timeout_ms );
1400814008

1400914009
// Returns 1 if a debugger has activated the debug module.
14010-
#if defined(__riscv) || defined(__riscv__)
14011-
inline static int DidDebuggerAttach() { return !*DMSTATUS_SENTINEL; }
14012-
#endif
14010+
#define DidDebuggerAttach() (!*DMSTATUS_SENTINEL)
14011+
14012+
// Returns 1 if a debugger has activated the debug module.
14013+
#define DebugPrintfBufferFree() (!(*DMDATA0 & 0x80))
1401314014

1401414015
// Just a definition to the internal _write function.
1401514016
int _write(int fd, const char *buf, int size);

examples/debugprintfdemo/debugprintfdemo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ int main()
1717
{
1818
SystemInit();
1919

20+
while( !DebugPrintfBufferFree() );
21+
2022
// Enable GPIOs
2123
RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC;
2224

examples/debugprintfdemo/funconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Though this should be on by default we can extra force it on.
55
#define FUNCONF_USE_DEBUGPRINTF 1
6-
#define FUNCONF_DEBUGPRINTF_TIMEOUT (1<<31) // Wait for a very very long time.
6+
//#define FUNCONF_DEBUGPRINTF_TIMEOUT (1<<31) // Optionally, wait for a very very long time on every printf.
77

88
#define CH32V003 1
99

0 commit comments

Comments
 (0)