From bce0180be12e82ece34306c44fe6e8c4f138a856 Mon Sep 17 00:00:00 2001
From: Fabiano Riccardi <fabiuz4@hotmail.it>
Date: Thu, 27 Oct 2016 22:11:53 +0200
Subject: [PATCH] Little modification in TransceiverTimer, still not working,
 but RTC is still ok.

Signed-off-by: Fabiano Riccardi <fabiuz4@hotmail.it>
---
 .../interfaces-impl/gpio_timer.cpp            |  4 +-
 .../high_resolution_timer_base.cpp            | 41 ++++++++++++++-----
 .../high_resolution_timer_base.h              | 11 ++---
 .../interfaces-impl/rtc.h                     | 14 ++-----
 .../interfaces-impl/timer_interface.h         |  6 +--
 .../interfaces-impl/transceiver.cpp           |  2 +-
 .../interfaces-impl/transceiver.h             |  1 +
 .../interfaces-impl/transceiver_timer.cpp     |  8 +++-
 .../interfaces-impl/transceiver_timer.h       |  9 ++--
 miosix/config/Makefile.inc                    |  2 +-
 10 files changed, 58 insertions(+), 40 deletions(-)

diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/gpio_timer.cpp b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/gpio_timer.cpp
index 3ce195c6..b4efddb1 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/gpio_timer.cpp
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/gpio_timer.cpp
@@ -56,8 +56,8 @@ bool GPIOtimer::absoluteWaitTimeoutOrEvent(long long tick){
 	isInput=true;
     }
     b.cleanBufferGPIO();
-    b.setCCInterrupt2(false);
-    b.setCCInterrupt2Tim1(true);
+    b.enableCC2Interrupt(false);
+    b.enableCC2InterruptTim1(true);
     
     do {
         tWaitingGPIO=Thread::IRQgetCurrentThread();
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.cpp b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.cpp
index 0291fd6f..5b200785 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.cpp
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.cpp
@@ -83,10 +83,21 @@ inline void interruptGPIOTimerRoutine(){
     
 }
 
+/*
+ * FIXME: The following codition could be always true, they aren't exclusive
+ */
 inline void interruptTransceiverTimerRoutine(){
     if(TIMER2->CC[1].CTRL & TIMER_CC_CTRL_MODE_OUTPUTCOMPARE){
 	TIMER2->CC[1].CTRL = (TIMER2->CC[1].CTRL & ~_TIMER_CC_CTRL_CMOA_MASK) | TIMER_CC_CTRL_CMOA_CLEAR;
 	TIMER2->CC[1].CCV = static_cast<unsigned short>(TIMER2->CNT+10);//static_cast<unsigned int>(tick & 0xFFFF);
+    }else if(TIMER1->CC[0].CTRL & TIMER_CC_CTRL_MODE_INPUTCAPTURE){
+	ms32chkp[0]=ms32time;
+	//really in the past, the overflow of TIMER3 is occurred but the timer wasn't updated
+	long long a=ms32chkp[0] | TIMER3->CC[0].CCV<<16 | TIMER1->CC[0].CCV;;
+	long long c=IRQgetTick();
+	if(a-c< -48000000){ 
+	    ms32chkp[0]+=overflowIncrement;
+	}
     }
     
     //Reactivating the thread that is waiting for the event.
@@ -235,27 +246,27 @@ long long HighResolutionTimerBase::IRQgetCurrentTick(){
     return IRQgetTick();
 }
 
-void HighResolutionTimerBase::setCCInterrupt0(bool enable){
+void HighResolutionTimerBase::enableCC0Interrupt(bool enable){
     if(enable){
         TIMER3->IEN|=TIMER_IEN_CC0;
     }else{
         TIMER3->IEN&=~TIMER_IEN_CC0;
     }
 }
-void HighResolutionTimerBase::setCCInterrupt1(bool enable){
+void HighResolutionTimerBase::enableCC1Interrupt(bool enable){
     if(enable)
         TIMER3->IEN|=TIMER_IEN_CC1;
     else
         TIMER3->IEN&=~TIMER_IEN_CC1;
 }
-void HighResolutionTimerBase::setCCInterrupt2(bool enable){
+void HighResolutionTimerBase::enableCC2Interrupt(bool enable){
     if(enable)
         TIMER3->IEN|=TIMER_IEN_CC2;
     else
         TIMER3->IEN&=~TIMER_IEN_CC2;
 }
 
-void HighResolutionTimerBase::setCCInterrupt2Tim1(bool enable){
+void HighResolutionTimerBase::enableCC2InterruptTim1(bool enable){
     if(enable){
 	TIMER1->IFC= TIMER_IF_CC2;
         TIMER1->IEN|=TIMER_IEN_CC2;
@@ -263,13 +274,21 @@ void HighResolutionTimerBase::setCCInterrupt2Tim1(bool enable){
         TIMER1->IEN&=~TIMER_IEN_CC2;
 }
 
-void HighResolutionTimerBase::setCCInterrupt0Tim2(bool enable){
+void HighResolutionTimerBase::enableCC0InterruptTim2(bool enable){
     if(enable)
         TIMER2->IEN|=TIMER_IEN_CC0;
     else
         TIMER2->IEN&=~TIMER_IEN_CC0;
 }
 
+void HighResolutionTimerBase::enableCC1InterruptTim2(bool enable){
+    if(enable){
+	TIMER2->IFC= TIMER_IF_CC1;
+        TIMER2->IEN|=TIMER_IEN_CC1;
+    }else
+        TIMER2->IEN&=~TIMER_IEN_CC1;
+}
+
 long long HighResolutionTimerBase::getCurrentTick(){
     bool interrupts=areInterruptsEnabled();
     //TODO: optimization opportunity, if we can guarantee that no call to this
@@ -285,18 +304,18 @@ long long HighResolutionTimerBase::getCurrentTick(){
 WaitResult HighResolutionTimerBase::IRQsetNextTransceiverInterrupt(long long tick){
     long long curTick = IRQgetTick(); // This require almost 1us about 50ticks
     long long diff=tick-curTick;
-    
+    tick--;
     // 150 are enough to make sure that this routine ends and the timer IEN is enabled. 
     //NOTE: this is really dependent on compiler, optimization and other stuff
-    if(diff>150){
+    if(diff>200){
 	faseTransceiver=0;
-	unsigned short t1=static_cast<unsigned short>((tick & 0xFFFF)-1);
+	unsigned short t1=static_cast<unsigned short>(tick & 0xFFFF);
 	//ms32chkp[0] is going to store even the middle part, because we don't need to use TIMER3
 	ms32chkp[0] = tick & (upperMask | 0xFFFF0000);
 	TIMER2->CC[1].CCV = t1;
 
-	TIMER2->IFC = TIMER_IFC_CC1;
-	TIMER2->IEN |= TIMER_IEN_CC1;
+	enableCC1InterruptTim2(true);
+	diff=tick-IRQgetTick();
 	//0xFFFF because it's the roundtrip of timer
 	if(diff<=0xFFFF){
 	    TIMER2->CC[1].CTRL = (TIMER2->CC[1].CTRL & ~_TIMER_CC_CTRL_CMOA_MASK) | TIMER_CC_CTRL_CMOA_SET;
@@ -345,7 +364,7 @@ WaitResult HighResolutionTimerBase::IRQsetNextGPIOInterrupt(long long tick){
 	ms32chkp[2] = tick & (upperMask | 0xFFFF0000);
 	TIMER1->CC[2].CCV = t1;
 
-	setCCInterrupt2Tim1(true);
+	enableCC2InterruptTim1(true);
 	diff=tick-IRQgetTick();
 	//0xFFFF because it's the roundtrip of timer
 	if(diff<=0xFFFF){
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.h
index 4ae549c6..eaf93bab 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.h
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/high_resolution_timer_base.h
@@ -72,11 +72,12 @@ public:
      */
     long long IRQgetCurrentTick();
     
-    void setCCInterrupt0(bool enable);
-    void setCCInterrupt1(bool enable);
-    void setCCInterrupt2(bool enable);
-    void setCCInterrupt2Tim1(bool enable);
-    void setCCInterrupt0Tim2(bool enable);
+    void enableCC0Interrupt(bool enable);
+    void enableCC1Interrupt(bool enable);
+    void enableCC2Interrupt(bool enable);
+    void enableCC2InterruptTim1(bool enable);
+    void enableCC0InterruptTim2(bool enable);
+    void enableCC1InterruptTim2(bool enable);
     void setModeGPIOTimer(bool input);
     void setModeTransceiverTimer(bool input);
     
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/rtc.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/rtc.h
index 81191d51..94c2b3f1 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/rtc.h
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/rtc.h
@@ -28,8 +28,8 @@
  *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
  ***************************************************************************/
 
-#ifndef TIMER_H
-#define TIMER_H
+#ifndef RTC_H
+#define RTC_H
 
 #include "timer_interface.h"
 #include <kernel/timeconversion.h>
@@ -158,14 +158,6 @@ private:
     TimeConversion tc; ///< Class for converting from nanoseconds to ticks
 };
 
-/**
- * \return the timer used by the transceiver
- */
-inline HardwareTimer& getTransceiverTimer()
-{
-    return Rtc::instance();
-}
-
 } //namespace miosix
 
-#endif //TIMER_H
+#endif //RTC_H
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/timer_interface.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/timer_interface.h
index b1834680..d5a6b8fd 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/timer_interface.h
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/timer_interface.h
@@ -11,8 +11,8 @@
  * Created on October 17, 2016, 10:27 AM
  */
 
-#ifndef TIMERINTERFACE_H
-#define TIMERINTERFACE_H
+#ifndef TIMER_INTERFACE_H
+#define TIMER_INTERFACE_H
 
 #include <kernel/timeconversion.h>
 
@@ -113,5 +113,5 @@ namespace miosix {
     };
 }
 
-#endif /* TIMERINTERFACE_H */
+#endif /* TIMER_INTERFACE_H */
 
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp
index 9e0214b6..5b4dbade 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp
@@ -401,7 +401,7 @@ CC2520StatusBitmask Transceiver::commandStrobe(CC2520Command cmd)
 
 Transceiver::Transceiver()
     : pm(PowerManager::instance()), spi(Spi::instance()),
-      timer(getTransceiverTimer()), state(CC2520State::DEEPSLEEP),
+      timer(Rtc::instance()), state(CC2520State::DEEPSLEEP),
       waiting(nullptr)
 {
     registerGpioIrq(internalSpi::miso::getPin(),GpioIrqEdge::RISING,
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.h
index a6a7c2f1..3cff537e 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.h
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.h
@@ -34,6 +34,7 @@
 #include "power_manager.h"
 #include "spi.h"
 #include "rtc.h"
+#include "transceiver_timer.h"
 
 namespace miosix {
 
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.cpp b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.cpp
index f4e5c4d3..40cb5bde 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.cpp
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.cpp
@@ -57,8 +57,8 @@ bool TransceiverTimer::absoluteWaitTimeoutOrEvent(long long tick){
     }
     
     b.setModeTransceiverTimer(true);
-    b.setCCInterrupt0(false);
-    b.setCCInterrupt0Tim2(true);
+    b.enableCC0Interrupt(false);
+    b.enableCC0InterruptTim2(true);
     do {
         tWaiting=Thread::IRQgetCurrentThread();
         Thread::IRQwait();
@@ -75,6 +75,10 @@ bool TransceiverTimer::absoluteWaitTimeoutOrEvent(long long tick){
     }
 }
 
+bool TransceiverTimer::waitTimeoutOrEvent(long long tick){
+    return absoluteWaitTimeoutOrEvent(b.getCurrentTick()+tick);
+}
+
 long long TransceiverTimer::tick2ns(long long tick){
     return tc.tick2ns(tick);
 }
diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.h
index f1e9ea61..99250e96 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.h
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver_timer.h
@@ -14,8 +14,8 @@
 #include "high_resolution_timer_base.h"
 #include "timer_interface.h"
 
-#ifndef RADIO_TIMER_H
-#define RADIO_TIMER_H
+#ifndef TRANSCEIVER_TIMER_H
+#define TRANSCEIVER_TIMER_H
 
 namespace miosix{
     class TransceiverTimer : public HardwareTimer {
@@ -49,9 +49,10 @@ namespace miosix{
             HighResolutionTimerBase& b;
             TimeConversion tc;
     };
-}
 
+} //miosix namespace
 
 
-#endif /* RADIO_TIMER_H */
+
+#endif /* TRANSCEIVER_TIMER_H */
 
diff --git a/miosix/config/Makefile.inc b/miosix/config/Makefile.inc
index b7bcef27..35a7f64b 100644
--- a/miosix/config/Makefile.inc
+++ b/miosix/config/Makefile.inc
@@ -2116,7 +2116,7 @@ else ifeq ($(ARCH),cortexM3_efm32gg)
         $(BOARD_INC)/interfaces-impl/transceiver.cpp    \
         $(BOARD_INC)/interfaces-impl/cstimer.cpp        \
         $(BOARD_INC)/interfaces-impl/gpio_timer.cpp     \
-        $(BOARD_INC)/interfaces-impl/radio_timer.cpp
+        $(BOARD_INC)/interfaces-impl/transceiver_timer.cpp
 
         ## Add a #define to allow querying board name
         CFLAGS_BASE   += -DEFM32GG332F1024 -D_BOARD_WANDSTEM
-- 
GitLab