@@ -32,6 +32,7 @@ extern "C"
3232// UNDONE: Switch back to the USB/CDC based serial port.
3333#undef Serial
3434#define Serial Serial1
35+ #define MRI_SERIAL_IRQ USART1_IRQn
3536
3637
3738// Configuration Parameters
@@ -111,6 +112,8 @@ volatile uint32_t mriThreadOrigHardFault;
111112volatile uint32_t mriThreadOrigMemManagement;
112113volatile uint32_t mriThreadOrigBusFault;
113114volatile uint32_t mriThreadOrigUsageFault;
115+ // Address of the original Serial peripheral ISR to be hooked.
116+ void (*g_origSerialISR)(void );
114117
115118// UNDONE: For debugging. If different than g_haltedThreadId then the mriMain() thread was signalled when the previous
116119// instance was still running.
@@ -142,23 +145,27 @@ static void setDebugActiveFlag();
142145static void clearDebugActiveFlag ();
143146static bool isThreadMode (uint32_t excReturn);
144147static bool hasEncounteredDebugEvent ();
148+ static bool hasControlCBeenDetected ();
145149static void recordAndClearFaultStatusBits ();
146150static void wakeMriMainToDebugCurrentThread ();
147151static void stopSingleStepping ();
148152static void recordAndSwitchFaultHandlersToDebugger ();
153+ static void hookSerialISR ();
149154static bool isDebugThreadActive ();
150155static void setFaultDetectedFlag ();
151156static bool isImpreciseBusFault ();
152157static void advancePCToNextInstruction (uint32_t excReturn, uint32_t psp, uint32_t msp);
153158static uint32_t * threadSP (uint32_t excReturn, uint32_t psp, uint32_t msp);
154159static bool isInstruction32Bit (uint16_t firstWordOfInstruction);
160+ static void serialISRHook ();
161+ static bool isDebuggerActive ();
162+ static void setControlCFlag ();
155163
156164
157165
158166
159167ThreadMRI::ThreadMRI ()
160168{
161- mriInit (" " );
162169}
163170
164171
@@ -169,6 +176,9 @@ bool ThreadMRI::begin()
169176 return false ;
170177 }
171178
179+ // UNDONE: Move into constructor.
180+ mriInit (" " );
181+
172182 static uint64_t stack[MRI_THREAD_STACK_SIZE];
173183 static osRtxThread_t threadTcb;
174184 static const osThreadAttr_t threadAttr =
@@ -341,6 +351,7 @@ void Platform_Init(Token* pParameterTokens)
341351 g_enableDWTandFPB = 0 ;
342352 mriThreadSingleStepThreadId = NULL ;
343353 recordAndSwitchFaultHandlersToDebugger ();
354+ hookSerialISR ();
344355}
345356
346357static void recordAndSwitchFaultHandlersToDebugger ()
@@ -357,6 +368,12 @@ static void recordAndSwitchFaultHandlersToDebugger()
357368 NVIC_SetVector (DebugMonitor_IRQn, (uint32_t )mriDebugMonitorHandlerStub);
358369}
359370
371+ static void hookSerialISR ()
372+ {
373+ g_origSerialISR = (void (*)(void ))NVIC_GetVector (MRI_SERIAL_IRQ);
374+ NVIC_SetVector (MRI_SERIAL_IRQ, (uint32_t )serialISRHook);
375+ }
376+
360377
361378
362379
@@ -457,7 +474,7 @@ extern "C" void mriDebugMonitorHandler(uint32_t excReturn)
457474 // DebugMon is running at such low priority that we should be getting ready to return to thread mode.
458475 }
459476
460- if (!hasEncounteredDebugEvent ()) {
477+ if (!hasEncounteredDebugEvent () && ! hasControlCBeenDetected () ) {
461478 if (g_enableDWTandFPB) {
462479 enableDWTandITM ();
463480 enableFPB ();
@@ -528,6 +545,11 @@ static bool hasEncounteredDebugEvent()
528545
529546}
530547
548+ static bool hasControlCBeenDetected ()
549+ {
550+ return mriCortexMState.flags & CORTEXM_FLAGS_CTRL_C;
551+ }
552+
531553static void recordAndClearFaultStatusBits ()
532554{
533555 mriCortexMState.exceptionNumber = getCurrentlyExecutingExceptionNumber ();
@@ -595,7 +617,6 @@ static uint32_t* threadSP(uint32_t excReturn, uint32_t psp, uint32_t msp)
595617 return (uint32_t *)sp;
596618}
597619
598-
599620static bool isInstruction32Bit (uint16_t firstWordOfInstruction)
600621{
601622 uint16_t maskedOffUpper5BitsOfWord = firstWordOfInstruction & 0xF800 ;
@@ -606,3 +627,24 @@ static bool isInstruction32Bit(uint16_t firstWordOfInstruction)
606627 maskedOffUpper5BitsOfWord == 0xF000 ||
607628 maskedOffUpper5BitsOfWord == 0xF800 );
608629}
630+
631+
632+ static void serialISRHook ()
633+ {
634+ g_origSerialISR ();
635+ if (!isDebuggerActive () && Serial.available () > 0 ) {
636+ // Pend a halt into the debug monitor now that there is data from GDB ready to be read by it.
637+ setControlCFlag ();
638+ setMonitorPending ();
639+ }
640+ }
641+
642+ static bool isDebuggerActive ()
643+ {
644+ return mriCortexMState.flags & CORTEXM_FLAGS_ACTIVE_DEBUG;
645+ }
646+
647+ static void setControlCFlag ()
648+ {
649+ mriCortexMState.flags |= CORTEXM_FLAGS_CTRL_C;
650+ }
0 commit comments