From 20b663d235986dfebc37fa9b247f2bc1c1bd995d Mon Sep 17 00:00:00 2001
From: Sasan Golchin <ahmad.golchin@mail.polimi.it>
Date: Tue, 27 Sep 2016 12:00:52 +0200
Subject: [PATCH] USE_CSTIMER FIXED: cstimer became default timing tool for
 wandstem and stm32f407discovery board and so the macro USE_CSTIMER is
 eliminated

---
 debugpin.h                                    |  7 +-
 .../common/interfaces-impl/portability.cpp    | 27 +------
 .../efm32gg332f1024_wandstem/board_settings.h |  2 -
 .../board_settings.h                          |  2 -
 miosix/kernel/kernel.cpp                      | 76 -------------------
 5 files changed, 7 insertions(+), 107 deletions(-)

diff --git a/debugpin.h b/debugpin.h
index 5349383e..f4cca499 100644
--- a/debugpin.h
+++ b/debugpin.h
@@ -5,8 +5,13 @@
 
 #include "miosix/interfaces/gpio.h"
 
+#ifdef _BOARD_STM32F4DISCOVERY
 typedef miosix::Gpio<GPIOA_BASE, 1> debug1;
-typedef miosix::Gpio<GPIOA_BASE,15> debug2;
+typedef miosix::Gpio<GPIOA_BASE, 15> debug2;
+#elif _BOARD_WANDSTEM
+typedef miosix::Gpio<GPIOC_BASE, 2> debug1; // on the board: gpio7
+typedef miosix::Gpio<GPIOC_BASE, 7> debug2; // on the board: gpio9
+#endif
 
 template<typename T>
 class HighPin
diff --git a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
index 6f548766..c01d8c82 100644
--- a/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
+++ b/miosix/arch/cortexM4_stm32f4/common/interfaces-impl/portability.cpp
@@ -50,12 +50,6 @@
 void SysTick_Handler()   __attribute__((naked));
 void SysTick_Handler()
 {
-#ifndef USE_CSTIMER
-    saveContext();
-    //Call ISR_preempt(). Name is a C++ mangled name.
-    asm volatile("bl _ZN14miosix_private11ISR_preemptEv");
-    restoreContext();
-#endif
 }
 
 /**
@@ -95,21 +89,6 @@ void TIM3_IRQHandler()
 
 namespace miosix_private {
 
-#ifndef USE_CSTIMER
-/**
- * \internal
- * Called by the timer interrupt, preempt to next thread
- * Declared noinline to avoid the compiler trying to inline it into the caller,
- * which would violate the requirement on naked functions. Function is not
- * static because otherwise the compiler optimizes it out...
- */
-void ISR_preempt() __attribute__((noinline));
-void ISR_preempt()
-{
-    miosix::IRQtimerInterrupt(0 /*TODO dummy parameter */);
-}
-#endif //USE_CSTIMER
-
 /**
  * \internal
  * Called by the software interrupt, yield to next thread
@@ -308,11 +287,7 @@ void IRQportableStartKernel()
     NVIC_SetPriority(SVCall_IRQn,3);//High priority for SVC (Max=0, min=15)
     NVIC_SetPriority(SysTick_IRQn,3);//High priority for SysTick (Max=0, min=15)
     NVIC_SetPriority(MemoryManagement_IRQn,2);//Higher priority for MemoryManagement (Max=0, min=15)
-#ifndef USE_CSTIMER
-    SysTick->LOAD=SystemCoreClock/miosix::TICK_FREQ;
-    SysTick->CTRL=SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk |
-            SysTick_CTRL_CLKSOURCE_Msk;
-#endif
+    
     #ifdef WITH_PROCESSES
     miosix::IRQenableMPUatBoot();
     #endif //WITH_PROCESSES
diff --git a/miosix/config/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/board_settings.h b/miosix/config/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/board_settings.h
index b40896c9..44ff1253 100644
--- a/miosix/config/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/board_settings.h
+++ b/miosix/config/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/board_settings.h
@@ -43,8 +43,6 @@
  */
 #define WANDSTEM_HW_REV 14
 
-#define USE_CSTIMER //FIXME: remove when cstimer becomes default
-
 namespace miosix {
 
 /**
diff --git a/miosix/config/arch/cortexM4_stm32f4/stm32f407vg_stm32f4discovery/board_settings.h b/miosix/config/arch/cortexM4_stm32f4/stm32f407vg_stm32f4discovery/board_settings.h
index 324982df..27b69de5 100644
--- a/miosix/config/arch/cortexM4_stm32f4/stm32f407vg_stm32f4discovery/board_settings.h
+++ b/miosix/config/arch/cortexM4_stm32f4/stm32f407vg_stm32f4discovery/board_settings.h
@@ -30,8 +30,6 @@
 
 #include "util/version.h"
 
-#define USE_CSTIMER //FIXME: remove when cstimer becomes default
-
 /**
  * \internal
  * Versioning for board_settings.h for out of git tree projects
diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp
index 13018e2c..2bda2f37 100644
--- a/miosix/kernel/kernel.cpp
+++ b/miosix/kernel/kernel.cpp
@@ -69,10 +69,6 @@ IntrusiveList<SleepData> *sleepingList=nullptr;///list of sleeping threads
 ///Contains head of the sleeping list in terms of timer's ticks
 long long firstSleepItemTicks = std::numeric_limits<long long>::max();
 
-#ifndef USE_CSTIMER
-static volatile long long tick=0;///<\internal Kernel tick
-#endif //USE_CSTIMER
-
 ///\internal !=0 after pauseKernel(), ==0 after restartKernel()
 volatile int kernel_running=0;
 
@@ -255,23 +251,7 @@ bool isKernelRunning()
 
 long long getTick()
 {
-    #ifndef USE_CSTIMER
-    /*
-     * Reading a volatile 64bit integer on a 32bit platform with interrupts
-     * enabled is tricky because the operation is not atomic, so we need to
-     * read it twice to see if we were interrupted in the middle of the read
-     * operation.
-     */
-    long long a,b;
-    for(;;)
-    {
-        a=static_cast<long long>(tick);
-        b=static_cast<long long>(tick);
-        if(a==b) return a;
-    }
-    #else //USE_CSTIMER
     return tc->tick2ns(ContextSwitchTimer::instance().getCurrentTick())/preemptionPeriodNs;
-    #endif //USE_CSTIMER
 }
 
 /**
@@ -307,29 +287,6 @@ void IRQaddToSleepingList(SleepData *x)
  */
 bool IRQwakeThreads(long long currentTick)
 {
-    #ifndef USE_CSTIMER
-    tick++;//Increment tick
-    
-    //If no item in list, return
-    if(sleepingList->empty()) return false;
-    
-    bool result=false;
-    //Since list is sorted, if we don't need to wake the first element
-    //we don't need to wake the other too
-    for(auto it = sleepingList->begin() ; it != sleepingList->end() ;)
-    {
-        if(tick < (*it)->wakeup_time) break;
-        if((*it)->p == nullptr) ++it; //Only csRecord has p==nullptr
-        else {
-            (*it)->p->flags.IRQsetSleep(false); //Wake thread
-            it = sleepingList->erase(it);
-            result = true;
-        }
-    }
-    return result;
-    
-    #else //USE_CSTIMER
-
     //If no item in list, return
     if(sleepingList->empty())
         return false;
@@ -352,7 +309,6 @@ bool IRQwakeThreads(long long currentTick)
     else
         firstSleepItemTicks = tc->ns2tick(sleepingList->front()->wakeup_time);
     return result;
-    #endif //USE_CSTIMER
 }
 
 /*
@@ -415,7 +371,6 @@ bool Thread::testTerminate()
     return const_cast<Thread*>(cur)->flags.isDeleting();
 }
 
-#ifdef USE_CSTIMER
 void Thread::nanoSleep(long long ns)
 {
     if(ns==0) return; //TODO: should be (ns &lt; resolution + epsilon)
@@ -444,46 +399,15 @@ void Thread::nanoSleepUntil(long long absoluteTime)
     Thread::yield();
     
 }
-#endif
 
 void Thread::sleep(unsigned int ms)
 {
-#ifdef USE_CSTIMER
     nanoSleep(mul32x32to64(ms,1000000));
-#else
-    if(ms==0) return;
-    //pauseKernel() here is not enough since even if the kernel is stopped
-    //the tick isr will wake threads, modifying the sleepingList
-    {
-        FastInterruptDisableLock lock;
-        SleepData d;
-        d.p=const_cast<Thread*>(cur);
-        if(((ms*TICK_FREQ)/1000)>0) d.wakeup_time=getTick()+(ms*TICK_FREQ)/1000;
-        //If tick resolution is too low, wait one tick
-        else d.wakeup_time=getTick()+1;
-        IRQaddToSleepingList(&d);//Also sets SLEEP_FLAG
-    }
-    Thread::yield();
-#endif
 }
 
 void Thread::sleepUntil(long long absoluteTime)
 {
-#ifdef USE_CSTIMER
     nanoSleepUntil(absoluteTime * 1000000);
-#else
-    //pauseKernel() here is not enough since even if the kernel is stopped
-    //the tick isr will wake threads, modifying the sleepingList
-    {
-        FastInterruptDisableLock lock;
-        if(absoluteTime<=getTick()) return; //Wakeup time in the past, return
-        SleepData d;
-        d.p=const_cast<Thread*>(cur);
-        d.wakeup_time=absoluteTime;
-        IRQaddToSleepingList(&d);//Also sets SLEEP_FLAG
-    }
-    Thread::yield();
-#endif
 }
 
 Thread *Thread::getCurrentThread()
-- 
GitLab