From fb4c42755516bce1ba2dfdf89026c458f1bca780 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu>
Date: Thu, 29 Feb 2024 09:29:53 +0100
Subject: [PATCH] [USART] Call Miosix IRQ handlers instead of accessing driver
 implementation details

In order to reduce the number of patches we make to the Miosix kernel, we're moving to calling Miosix USART IRQ handlers instead of accessing static port objects directly, which would require marking them for external linkage.
With this commit we are dropping support for USART ports higher than 3 when using the Miosix USART driver. For higher port counts, the boardcore driver must be used.
---
 src/shared/drivers/usart/USART.cpp | 63 ++++++++++++++----------------
 1 file changed, 29 insertions(+), 34 deletions(-)

diff --git a/src/shared/drivers/usart/USART.cpp b/src/shared/drivers/usart/USART.cpp
index 6f416be18..f5c787d6f 100644
--- a/src/shared/drivers/usart/USART.cpp
+++ b/src/shared/drivers/usart/USART.cpp
@@ -36,6 +36,11 @@
 Boardcore::USART *ports[N_USART_PORTS];
 
 #ifdef USART1
+/**
+ * \internal interrupt routine for usart1 miosix implementation
+ */
+void usart1irqImpl();
+
 /**
  * \internal Interrupt routine for usart1 actual implementation.
  */
@@ -43,12 +48,12 @@ void __attribute__((used)) usart1irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[0];
     if (port_boardcore)
+    {
         port_boardcore->IRQhandleInterrupt();
+    }
     else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[0];
-        if (port)
-            port->IRQhandleInterrupt();
+        usart1irqImpl();
     }
 }
 
@@ -64,6 +69,11 @@ void __attribute__((naked, used)) USART1_IRQHandler()
 #endif
 
 #ifdef USART2
+/**
+ * \internal interrupt routine for usart2 miosix implementation
+ */
+void usart2irqImpl();
+
 /**
  * \internal Interrupt routine for usart2 actual implementation.
  */
@@ -71,12 +81,12 @@ void __attribute__((used)) usart2irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[1];
     if (port_boardcore)
+    {
         port_boardcore->IRQhandleInterrupt();
+    }
     else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[1];
-        if (port)
-            port->IRQhandleInterrupt();
+        usart2irqImpl();
     }
 }
 
@@ -92,6 +102,11 @@ void __attribute__((naked, used)) USART2_IRQHandler()
 #endif
 
 #ifdef USART3
+/**
+ * \internal interrupt routine for usart3 miosix implementation
+ */
+void usart3irqImpl();
+
 /**
  * \internal Interrupt routine for usart3 actual implementation.
  */
@@ -99,12 +114,12 @@ void __attribute__((used)) usart3irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[2];
     if (port_boardcore)
+    {
         port_boardcore->IRQhandleInterrupt();
+    }
     else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[2];
-        if (port)
-            port->IRQhandleInterrupt();
+        usart3irqImpl();
     }
 }
 
@@ -127,12 +142,8 @@ void __attribute__((used)) uart4irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[3];
     if (port_boardcore)
-        port_boardcore->IRQhandleInterrupt();
-    else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[3];
-        if (port)
-            port->IRQhandleInterrupt();
+        port_boardcore->IRQhandleInterrupt();
     }
 }
 
@@ -155,12 +166,8 @@ void __attribute__((used)) uart5irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[4];
     if (port_boardcore)
-        port_boardcore->IRQhandleInterrupt();
-    else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[4];
-        if (port)
-            port->IRQhandleInterrupt();
+        port_boardcore->IRQhandleInterrupt();
     }
 }
 
@@ -183,12 +190,8 @@ void __attribute__((used)) usart6irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[5];
     if (port_boardcore)
-        port_boardcore->IRQhandleInterrupt();
-    else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[5];
-        if (port)
-            port->IRQhandleInterrupt();
+        port_boardcore->IRQhandleInterrupt();
     }
 }
 
@@ -211,12 +214,8 @@ void __attribute__((used)) uart7irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[6];
     if (port_boardcore)
-        port_boardcore->IRQhandleInterrupt();
-    else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[6];
-        if (port)
-            port->IRQhandleInterrupt();
+        port_boardcore->IRQhandleInterrupt();
     }
 }
 
@@ -239,12 +238,8 @@ void __attribute__((used)) uart8irqImplBoardcore()
 {
     Boardcore::USART *port_boardcore = ports[7];
     if (port_boardcore)
-        port_boardcore->IRQhandleInterrupt();
-    else
     {
-        miosix::STM32Serial *port = miosix::STM32Serial::ports[7];
-        if (port)
-            port->IRQhandleInterrupt();
+        port_boardcore->IRQhandleInterrupt();
     }
 }
 
-- 
GitLab