From d1150ea46256cb60c1e926d4d5c1968f2aa9f1f0 Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Thu, 12 May 2016 16:46:16 +0200
Subject: [PATCH] Removed redundant call to ISR_preempt in the call graph of
 the timer interrupt

---
 .../common/interfaces-impl/cstimer.cpp                 | 10 +++++-----
 .../common/interfaces-impl/portability.cpp             |  2 ++
 miosix/interfaces/cstimer.h                            |  2 +-
 miosix/kernel/scheduler/tick_interrupt.h               |  4 ++++
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/cstimer.cpp b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/cstimer.cpp
index e95fd9d8..14160656 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/cstimer.cpp
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/cstimer.cpp
@@ -3,6 +3,7 @@
 #include "interfaces/portability.h"
 #include "kernel/kernel.h"
 #include "kernel/logging.h"
+#include "kernel/scheduler/tick_interrupt.h"
 #include <cstdlib>
 
 using namespace miosix;
@@ -12,10 +13,6 @@ void IRQaddToSleepingList(SleepData *x);
 extern SleepData *sleeping_list;
 }
 
-namespace miosix_private {
-void ISR_preempt();
-}
-
 //TODO: comment me
 static const uint32_t threshold = 0xffffffff/4*3;
 static long long ms32time = 0; //most significant 32 bits of counter
@@ -83,7 +80,7 @@ void __attribute__((used)) cstirqhnd()
                 csRecord.wakeup_time += CST_QUANTUM;
                 IRQaddToSleepingList(&csRecord); //It would also set the next timer interrupt
             }
-            miosix_private::ISR_preempt();
+            IRQtickInterrupt();
         }
 
     }
@@ -127,6 +124,9 @@ long long ContextSwitchTimer::getNextInterrupt() const
 long long ContextSwitchTimer::getCurrentTick() const
 {
     bool interrupts=areInterruptsEnabled();
+    //TODO: optimization opportunity, if we can guarantee that no call to this
+    //function occurs before kernel is started, then we can use
+    //fastInterruptDisable())
     if(interrupts) disableInterrupts();
     long long result=IRQgetTick();
     if(interrupts) enableInterrupts();
diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
index 7600d8d7..b1d5c2c0 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
@@ -95,6 +95,7 @@ void TIM3_IRQHandler()
 
 namespace miosix_private {
 
+#ifndef USE_CSTIMER
 /**
  * \internal
  * Called by the timer interrupt, preempt to next thread
@@ -108,6 +109,7 @@ void ISR_preempt()
     IRQstackOverflowCheck();
     miosix::IRQtickInterrupt();
 }
+#endif //USE_CSTIMER
 
 /**
  * \internal
diff --git a/miosix/interfaces/cstimer.h b/miosix/interfaces/cstimer.h
index f7e3f0e9..f1133782 100644
--- a/miosix/interfaces/cstimer.h
+++ b/miosix/interfaces/cstimer.h
@@ -3,7 +3,7 @@
 
 namespace miosix {
 
-#define CST_QUANTUM 42000//84000 ///FIXME: remove
+#define CST_QUANTUM 84000 ///FIXME: remove
 
 /**
  * This class is a low level interface to a hardware timer, that is used as
diff --git a/miosix/kernel/scheduler/tick_interrupt.h b/miosix/kernel/scheduler/tick_interrupt.h
index 84d096b2..0a2bbf2a 100644
--- a/miosix/kernel/scheduler/tick_interrupt.h
+++ b/miosix/kernel/scheduler/tick_interrupt.h
@@ -29,6 +29,7 @@
 #define	TICK_INTERRUPT_H
 
 #include "config/miosix_settings.h"
+#include "interfaces/portability.h"
 #include "scheduler.h"
 
 namespace miosix {
@@ -43,6 +44,9 @@ extern bool IRQwakeThreads();///\internal Do not use outside the kernel
 
 inline void IRQtickInterrupt()
 {
+    #ifdef USE_CSTIMER
+    miosix_private::IRQstackOverflowCheck();
+    #endif //USE_CSTIMER
     bool woken=IRQwakeThreads();//Increment tick and wake threads,if any
     (void)woken; //Avoid unused variable warning.
 
-- 
GitLab