From b439a6016da7d0a8fe4be625f647239f2ddb9b0e Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Tue, 21 Jan 2025 13:56:39 +0100
Subject: [PATCH] Print IRQ number in unexpectedInterrupt for debugging
 purposes as suggested by Emilio Corigliano

---
 .../arch/cpu/common/cortexMx_interrupts.cpp   | 21 +++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/miosix/arch/cpu/common/cortexMx_interrupts.cpp b/miosix/arch/cpu/common/cortexMx_interrupts.cpp
index 10e311bf..2270f4bc 100644
--- a/miosix/arch/cpu/common/cortexMx_interrupts.cpp
+++ b/miosix/arch/cpu/common/cortexMx_interrupts.cpp
@@ -76,11 +76,11 @@ const unsigned int numInterrupts=MIOSIX_NUM_PERIPHERAL_IRQ;
 /**
  * \internal
  * To enable interrupt registration at run-time and interrupt arg pointer
- * passing, we use one pf these structs per peripheral interrupt allocated in RAM
+ * passing, we use one of these structs per peripheral interrupt allocated in RAM
  */
 struct IrqForwardingEntry
 {
-    //NOTE: a constexpr constructos can be used to perform static initialization
+    //NOTE: a constexpr constructor can be used to perform static initialization
     //of the table entries, but this uses a lot of Flash memory to store the
     //initialization values. We thus opted to leave the table uninitialized
     //and add the IRQinitIrqTable function that provides space efficient
@@ -244,8 +244,12 @@ void IRQinitIrqTable() noexcept
 
     for(unsigned int i=0;i<numInterrupts;i++)
     {
-        //Initialize all interrupts to a default priority and handler
+        //Initialize all interrupts to a default priority and handler and store
+        //the interrupt number as arg for debugging use in unexpectedInterrupt
         irqForwardingTable[i].handler=&unexpectedInterrupt;
+        #ifdef WITH_ERRLOG
+        irqForwardingTable[i].arg=reinterpret_cast<void*>(i);
+        #endif //WITH_ERRLOG
         NVIC_SetPriority(static_cast<IRQn_Type>(i),defaultIrqPriority);
     }
 }
@@ -266,7 +270,9 @@ void IRQunregisterIrq(unsigned int id, void (*handler)(void*), void *arg) noexce
     || irqForwardingTable[id].arg!=arg)
         errorHandler(INTERRUPT_REGISTRATION_ERROR);
     irqForwardingTable[id].handler=unexpectedInterrupt;
-    irqForwardingTable[id].arg=nullptr;
+    #ifdef WITH_ERRLOG
+    irqForwardingTable[id].arg=reinterpret_cast<void*>(id);
+    #endif //WITH_ERRLOG
     NVIC_DisableIRQ(static_cast<IRQn_Type>(id));
     NVIC_ClearPendingIRQ(static_cast<IRQn_Type>(id));
 }
@@ -669,10 +675,13 @@ void __attribute__((naked)) PendSV_Handler()
     restoreContext();
 }
 
-static void unexpectedInterrupt(void*)
+static void unexpectedInterrupt(void* arg)
 {
     #ifdef WITH_ERRLOG
-    IRQerrorLog("\r\n***Unexpected peripheral interrupt\r\n");
+    auto entryNum=reinterpret_cast<unsigned int>(arg);
+    IRQerrorLog("\r\n***Caught unregistered interrupt number ");
+    printUnsignedInt(entryNum);
+    IRQerrorLog("\r\n");
     #endif //WITH_ERRLOG
     IRQsystemReboot();
 }
-- 
GitLab