From 5690d3ffde30e95b4d6a3c93d5b054868ea3c59c Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Thu, 23 Mar 2023 08:49:20 +0100
Subject: [PATCH] Remove code duplication, improve encapsulation

---
 .../interfaces-impl/portability.cpp           | 19 +------------------
 .../interfaces-impl/portability_impl.h        |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 .../common/interfaces-impl/portability.cpp    | 19 +------------------
 .../common/interfaces-impl/portability_impl.h |  1 +
 miosix/interfaces/portability.h               |  7 -------
 miosix/kernel/kernel.cpp                      | 12 ++++++++++++
 miosix/kernel/kernel.h                        | 11 ++++++++---
 miosix/kernel/scheduler/timer_interrupt.h     |  2 +-
 28 files changed, 45 insertions(+), 227 deletions(-)

diff --git a/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability.cpp b/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability.cpp
index 257a931f..f2d690bf 100644
--- a/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability.cpp
+++ b/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability.cpp
@@ -37,10 +37,6 @@
 
 using namespace std;
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -53,7 +49,7 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
@@ -73,19 +69,6 @@ extern "C" void kernel_SWI_Routine()
     restoreContext();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[13] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {    
     //Jump to reset vector
diff --git a/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability_impl.h b/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability_impl.h
index eade54a3..5fef5e0c 100644
--- a/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability_impl.h
+++ b/miosix/arch/arm7_lpc2000/lpc2138_miosix_board/interfaces-impl/portability_impl.h
@@ -62,6 +62,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=13; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability.cpp
index ad5c2f77..fd0448af 100644
--- a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability.cpp
@@ -48,10 +48,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -64,23 +60,10 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability_impl.h
index c3e7e37d..841bed9e 100644
--- a/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM0_stm32f0/common/interfaces-impl/portability_impl.h
@@ -55,6 +55,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability.cpp
index 31b1c2e0..652d6c0a 100644
--- a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability.cpp
@@ -48,10 +48,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -64,23 +60,10 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability_impl.h
index 6e8561c4..3fc6c8d3 100644
--- a/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM3_efm32gg/common/interfaces-impl/portability_impl.h
@@ -56,6 +56,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability.cpp
index d901c53a..a130ee17 100644
--- a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability.cpp
@@ -48,10 +48,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -64,23 +60,10 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability_impl.h
index 2b6773e5..1455471a 100644
--- a/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM3_stm32f1/common/interfaces-impl/portability_impl.h
@@ -55,6 +55,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability.cpp
index 7907b5d1..c8ec4821 100644
--- a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability_impl.h
index 1ab67d48..31aa36da 100644
--- a/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM3_stm32f2/common/interfaces-impl/portability_impl.h
@@ -56,6 +56,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability.cpp
index 65d23656..0c60ba1a 100644
--- a/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability.cpp
@@ -48,10 +48,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -64,23 +60,10 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability_impl.h
index 2b6773e5..1455471a 100644
--- a/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM3_stm32l1/common/interfaces-impl/portability_impl.h
@@ -55,6 +55,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability.cpp
index 8723b8f2..125de5f9 100644
--- a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability.cpp
@@ -50,10 +50,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -66,23 +62,10 @@ namespace miosix_private {
 void ISR_yield() __attribute__((noinline));
 void ISR_yield()
 {
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     miosix::Scheduler::IRQfindNextThread();
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability_impl.h
index fa63ba40..fe5ab1db 100644
--- a/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM4_atsam4l/common/interfaces-impl/portability_impl.h
@@ -54,6 +54,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability.cpp
index 97da99e9..7b83129c 100644
--- a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability_impl.h
index 90488186..fe69a39e 100644
--- a/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM4_stm32f3/common/interfaces-impl/portability_impl.h
@@ -60,6 +60,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
index 6ca667d9..41ce36e2 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability_impl.h
index 29a75e5e..032962b0 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability_impl.h
@@ -60,6 +60,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability.cpp
index 6ca667d9..41ce36e2 100644
--- a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability_impl.h
index 29a75e5e..032962b0 100644
--- a/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM4_stm32l4/common/interfaces-impl/portability_impl.h
@@ -60,6 +60,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability.cpp
index ad4b3e99..63198acc 100644
--- a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability_impl.h
index 29a75e5e..032962b0 100644
--- a/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM7_stm32f7/common/interfaces-impl/portability_impl.h
@@ -60,6 +60,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability.cpp
index ad4b3e99..63198acc 100644
--- a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability.cpp
@@ -53,10 +53,6 @@ void SVC_Handler()
     restoreContext();
 }
 
-namespace miosix {
-extern volatile Thread *cur;///\internal Do not use outside the kernel
-}
-
 namespace miosix_private {
 
 /**
@@ -97,7 +93,7 @@ void ISR_yield()
         }
     }
     #endif // WITH_PROCESSES
-    IRQstackOverflowCheck();
+    miosix::Thread::IRQstackOverflowCheck();
     
     #ifdef WITH_PROCESSES
     //If processes are enabled, check the content of r3. If zero then it
@@ -114,19 +110,6 @@ void ISR_yield()
     #endif //WITH_PROCESSES
 }
 
-void IRQstackOverflowCheck()
-{
-    const unsigned int watermarkSize=miosix::WATERMARK_LEN/sizeof(unsigned int);
-    for(unsigned int i=0;i<watermarkSize;i++)
-    {
-        if(miosix::cur->watermark[i]!=miosix::WATERMARK_FILL)
-            miosix::errorHandler(miosix::STACK_OVERFLOW);
-    }
-    if(miosix::cur->ctxsave[0] < reinterpret_cast<unsigned int>(
-            miosix::cur->watermark+watermarkSize))
-        miosix::errorHandler(miosix::STACK_OVERFLOW);
-}
-
 void IRQsystemReboot()
 {
     NVIC_SystemReset();
diff --git a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability_impl.h b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability_impl.h
index 29a75e5e..032962b0 100644
--- a/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability_impl.h
+++ b/miosix/arch/cortexM7_stm32h7/common/interfaces-impl/portability_impl.h
@@ -60,6 +60,7 @@
 extern "C" {
 extern volatile unsigned int *ctxsave;
 }
+const int stackPtrOffsetInCtxsave=0; ///< Allows to locate the stack pointer
 
 /**
  * \internal
diff --git a/miosix/interfaces/portability.h b/miosix/interfaces/portability.h
index 256a78ea..22aa758a 100644
--- a/miosix/interfaces/portability.h
+++ b/miosix/interfaces/portability.h
@@ -228,13 +228,6 @@ inline void portableSwitchToUserspace();
 
 #endif //WITH_PROCESSES
 
-/**
- * \internal
- * Used before every context switch to check if the stack of the thread has
- * overflowed must be called before IRQfindNextThread().
- */
-void IRQstackOverflowCheck();
-
 /**
  * \internal
  * Called by miosix::start_kernel to handle the architecture-specific part of
diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp
index 479ca479..a7329888 100755
--- a/miosix/kernel/kernel.cpp
+++ b/miosix/kernel/kernel.cpp
@@ -684,6 +684,18 @@ bool Thread::IRQreportFault(const miosix_private::FaultData& fault)
 
 #endif //WITH_PROCESSES
 
+void Thread::IRQstackOverflowCheck()
+{
+    const unsigned int watermarkSize=WATERMARK_LEN/sizeof(unsigned int);
+    for(unsigned int i=0;i<watermarkSize;i++)
+    {
+        if(cur->watermark[i]!=WATERMARK_FILL) errorHandler(STACK_OVERFLOW);
+    }
+    if(cur->ctxsave[stackPtrOffsetInCtxsave] <
+        reinterpret_cast<unsigned int>(cur->watermark+watermarkSize))
+        errorHandler(STACK_OVERFLOW);
+}
+
 void Thread::threadLauncher(void *(*threadfunc)(void*), void *argv)
 {
     void *result=0;
diff --git a/miosix/kernel/kernel.h b/miosix/kernel/kernel.h
index 58ece03c..3a6d5dbb 100755
--- a/miosix/kernel/kernel.h
+++ b/miosix/kernel/kernel.h
@@ -746,7 +746,14 @@ public:
     static bool IRQreportFault(const miosix_private::FaultData& fault);
     
     #endif //WITH_PROCESSES
-	
+
+    /**
+     * \internal
+     * Used before every context switch to check if the stack of the thread
+     * being preempted has overflowed
+     */
+    static void IRQstackOverflowCheck();
+
 private:
     //Unwanted methods
     Thread(const Thread& p);///< No public copy constructor
@@ -1031,8 +1038,6 @@ private:
     #endif //WITH_CPU_TIME_COUNTER
     
     //friend functions
-    //Needs access to watermark, ctxsave
-    friend void miosix_private::IRQstackOverflowCheck();
     //Need access to status
     friend void IRQaddToSleepingList(SleepData *x);
     //Needs access to status
diff --git a/miosix/kernel/scheduler/timer_interrupt.h b/miosix/kernel/scheduler/timer_interrupt.h
index 0928443e..347e9bf3 100644
--- a/miosix/kernel/scheduler/timer_interrupt.h
+++ b/miosix/kernel/scheduler/timer_interrupt.h
@@ -44,7 +44,7 @@ extern bool IRQwakeThreads(long long currentTime);///\internal Do not use outsid
  */
 inline void IRQtimerInterrupt(long long currentTime)
 {
-    miosix_private::IRQstackOverflowCheck();
+    Thread::IRQstackOverflowCheck();
     bool hptw = IRQwakeThreads(currentTime);
     if(currentTime >= Scheduler::IRQgetNextPreemption() || hptw)
     {
-- 
GitLab