Skip to content

Commit

Permalink
Fix compile, and tweak wait for debugger work.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Nov 11, 2024
1 parent 493439f commit a2d7c23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions ch32v003fun/ch32v003fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ void DefaultIRQHandler( void )
#if FUNCONF_DEBUG_HARDFAULT
// Wait indefinitely for a debugger to attach.
while( !DidDebuggerAttach() );
printf( "DefaultIRQHandler MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() );
printf( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n", (int)__get_MSTATUS(), (int)__get_MTVAL(), (int)__get_MCAUSE(), (int)__get_MEPC() );
#endif
// Infinite Loop
asm volatile( "1: j 1b" );
Expand Down Expand Up @@ -1682,7 +1682,7 @@ WEAK int putchar(int c)
void SetupDebugPrintf( void )
{
// Clear out the sending flag.
*DMDATA1 = 0x0;
*DMDATA1 = 0x00;
*DMDATA0 = 0x80;
}

Expand All @@ -1706,7 +1706,7 @@ int WaitForDebuggerToAttach( int timeout_ms )

// Wait for the sentinel to become zero.
while( !DidDebuggerAttach() ) {
if( (SYSTICKCNT - start) > timeout ) return 1;
if( timeout_ms && (SYSTICKCNT - start) > timeout ) return 1;
}

return 0;
Expand Down
17 changes: 16 additions & 1 deletion ch32v003fun/ch32v003fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
7. Hardware MMIO structs, i.e.
SysTick->CNT = current system tick counter (can be Hclk or Hclk/8)
TIM2->CH1CVR = direct control over a PWM output

8. Default debug behavior, when semihosting:
a. You get access to DidDebuggerAttach() - so you can see if a debugger has attached.
b. WaitForDebuggerToAttach( int timeout_ms ) - if timeout_ms == 0, will wait for forever.
c. If a debugger has attached, printf will wait 120ms (configurable) to make sure it
doesn't drop data. Otherwise, printf fast-path's to exit. It will still do the string
formatting, but will not wait on output. Timeout is configured with
FUNCONF_DEBUGPRINTF_TIMEOUT
d. If you hard fault, it will wait indefinitely for a debugger to attach, once attached,
will printf the fault cause, and the memory address of the fault. Space can be saved
by setting FUNCONF_DEBUG_HARDFAULT to 0.
*/


Expand Down Expand Up @@ -13991,10 +14002,14 @@ void SystemInit(void);
void SetupUART( int uartBRR );

// Returns 1 if timeout reached, 0 otherwise.
// If timeout_ms == 0, wait indefinitely.
// Use DidDebuggerAttach() For a zero-wait way of seeing if it attached.
int WaitForDebuggerToAttach( int timeout_ms );

// Returns 1 if a debugger has activated the debug module.
static int DidDebuggerAttach() { return !*DMSTATUS_SENTINEL; }
#if defined(__riscv) || defined(__riscv__)
inline static int DidDebuggerAttach() { return !*DMSTATUS_SENTINEL; }
#endif

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

0 comments on commit a2d7c23

Please sign in to comment.