@@ -761,9 +761,12 @@ extern uint32_t * _edata;
761
761
// If you don't override a specific handler, it will just spin forever.
762
762
void DefaultIRQHandler ( void )
763
763
{
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
767
770
printf ( "DEAD MSTATUS:%08x MTVAL:%08x MCAUSE:%08x MEPC:%08x\n" , (int )__get_MSTATUS (), (int )__get_MTVAL (), (int )__get_MCAUSE (), (int )__get_MEPC () );
768
771
#endif
769
772
// Infinite Loop
@@ -1587,7 +1590,7 @@ static void internal_handle_input( volatile uint32_t * dmdata0 )
1587
1590
{
1588
1591
uint32_t dmd0 = * dmdata0 ;
1589
1592
int bytes = (dmd0 & 0x3f ) - 4 ;
1590
- if ( bytes > 0 )
1593
+ if ( bytes > 0 && bytes < 16 )
1591
1594
{
1592
1595
handle_debug_input ( bytes , ((uint8_t * )dmdata0 ) + 1 );
1593
1596
}
@@ -1609,14 +1612,18 @@ void poll_input( void )
1609
1612
// MSB .... LSB
1610
1613
// DMDATA0: char3 char2 char1 [status word]
1611
1614
// 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)
1614
1618
// 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.
1615
1622
// declare as weak to allow overriding.
1616
1623
WEAK int _write (int fd , const char * buf , int size )
1617
1624
{
1618
1625
(void )fd ;
1619
- if ( ! DidDebuggerAttach () ) return ;
1626
+ if ( ( * DMDATA0 & 0xc0 ) == 0xc0 ) return 0 ;
1620
1627
1621
1628
char buffer [4 ] = { 0 };
1622
1629
int place = 0 ;
@@ -1634,7 +1641,13 @@ WEAK int _write(int fd, const char *buf, int size)
1634
1641
if ( tosend > 7 ) tosend = 7 ;
1635
1642
1636
1643
while ( ( lastdmd = (* DMDATA0 ) ) & 0x80 )
1637
- if ( timeout -- == 0 ) return place ;
1644
+ {
1645
+ if ( timeout -- == 0 )
1646
+ {
1647
+ * DMDATA0 |= 0xc0 ;
1648
+ return 0 ;
1649
+ }
1650
+ }
1638
1651
1639
1652
if ( lastdmd ) internal_handle_input ( (uint32_t * )DMDATA0 );
1640
1653
@@ -1665,16 +1678,24 @@ WEAK int _write(int fd, const char *buf, int size)
1665
1678
// single to debug intf
1666
1679
WEAK int putchar (int c )
1667
1680
{
1668
- if ( ! DidDebuggerAttach () ) return ;
1681
+ if ( ( * DMDATA0 & 0xc0 ) == 0xc0 ) return 0 ;
1669
1682
1670
1683
int timeout = FUNCONF_DEBUGPRINTF_TIMEOUT ;
1671
1684
uint32_t lastdmd = 0 ;
1672
1685
1673
1686
while ( ( lastdmd = (* DMDATA0 ) ) & 0x80 )
1674
- if ( timeout -- == 0 ) return 0 ;
1687
+ {
1688
+ if ( timeout -- == 0 )
1689
+ {
1690
+ * DMDATA0 |= 0xc0 ;
1691
+ return 0 ;
1692
+ }
1693
+ }
1675
1694
1676
1695
// Simply seeking input.
1677
1696
if ( lastdmd ) internal_handle_input ( (uint32_t * )DMDATA0 );
1697
+
1698
+ // Write out character.
1678
1699
* DMDATA0 = 0x85 | ((const char )c <<8 );
1679
1700
return 1 ;
1680
1701
}
0 commit comments