From 93a2348f691c8bf04e2411aa05a5a4dcb12da8aa Mon Sep 17 00:00:00 2001
From: Fabiano Riccardi <fabiuz4@hotmail.it>
Date: Sun, 13 Nov 2016 13:07:54 +0100
Subject: [PATCH] Added the timeout feauture for GPIOTimer and Transceiver
 timer. Improvement in the test for the transceiver, now the exception are
 correctly managed and the test will continue forever

Signed-off-by: Fabiano Riccardi <fabiuz4@hotmail.it>
---
 .../high_resolution_timer_base.cpp            | 62 +++++++++++--------
 .../high_resolution_timer_base.h              |  2 +-
 .../interfaces-impl/transceiver.cpp           |  2 +-
 .../interfaces-impl/transceiver_timer.cpp     |  5 +-
 4 files changed, 41 insertions(+), 30 deletions(-)

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 3b1b9638..e8289c73 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
@@ -32,7 +32,6 @@
 #include "kernel/timeconversion.h"
 #include "gpio_timer.h"
 #include "transceiver_timer.h"
-#include "virtual_high_resolution_timer_base.cpp"
 #include "../../../../debugpin.h"
 
 using namespace miosix;
@@ -195,7 +194,10 @@ void __attribute__((used)) cstirqhnd2(){
     if ((TIMER2->IEN & TIMER_IEN_CC0) && (TIMER2->IF & TIMER_IF_CC0) ){
 	TIMER2->IEN &= ~ TIMER_IEN_CC0;
 	TIMER2->IFC = TIMER_IFC_CC0;
-	
+        //disable the timeout
+	TIMER2->IEN &= ~ TIMER_IEN_CC1;
+	TIMER2->IFC = TIMER_IFC_CC1;
+        
 	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 | TIMER2->CC[0].CCV;;
@@ -210,7 +212,8 @@ void __attribute__((used)) cstirqhnd2(){
     if ((TIMER2->IEN & TIMER_IEN_CC1) && (TIMER2->IF & TIMER_IF_CC1) ){
 	TIMER2->IFC = TIMER_IFC_CC1;
 	
-	if(TIMER2->CC[2].CTRL & TIMER_CC_CTRL_MODE_OUTPUTCOMPARE){
+        //if PEN, it means that we want to trigger, otherwise we are in timeout for input/capture
+	if(TIMER2->ROUTE & TIMER_ROUTE_CC1PEN){
 	    if(faseTransceiver==0){
 		//get nextInterrupt
 		long long t=ms32chkp[0]|TIMER2->CC[1].CCV;
@@ -230,6 +233,7 @@ void __attribute__((used)) cstirqhnd2(){
 	    long long diff=t-IRQgetTick();
 	    if(diff<0){
 		TIMER2->IEN &= ~TIMER_IEN_CC1;
+                //Disable the input capture interrupt
 		TIMER2->IEN &= ~TIMER_IEN_CC0;
 		TIMER2->IFC = TIMER_IFC_CC0;
 		//Reactivating the thread that is waiting for the event, WITHOUT changing the tWaiting
@@ -340,9 +344,10 @@ void HighResolutionTimerBase::enableCC2InterruptTim1(bool enable){
 }
 
 void HighResolutionTimerBase::enableCC0InterruptTim2(bool enable){
-    if(enable)
+    if(enable){
+        TIMER2->IFC= TIMER_IF_CC0;
         TIMER2->IEN|=TIMER_IEN_CC0;
-    else
+    }else
         TIMER2->IEN&=~TIMER_IEN_CC0;
 }
 
@@ -475,22 +480,9 @@ void HighResolutionTimerBase::setModeGPIOTimer(bool input){
     } 
 }
 
-void HighResolutionTimerBase::cleanBufferGPIO(){
-    falseRead(&TIMER3->CC[2].CCV);
-    falseRead(&TIMER1->CC[2].CCV);
-    falseRead(&TIMER3->CC[2].CCV);
-    falseRead(&TIMER1->CC[2].CCV);
-}
-
-void HighResolutionTimerBase::cleanBufferTrasceiver(){
-    falseRead(&TIMER3->CC[0].CCV);
-    falseRead(&TIMER2->CC[0].CCV);
-    falseRead(&TIMER3->CC[0].CCV);
-    falseRead(&TIMER2->CC[0].CCV);
-}
-
 void HighResolutionTimerBase::setModeTransceiverTimer(bool input){
-	//For input capture feature:
+    if(input){	
+        //For input capture feature:
 	//Connect TIMER2->CC0 to pin PA8 aka excChB
 	TIMER2->ROUTE |= TIMER_ROUTE_CC0PEN
 		| TIMER_ROUTE_LOCATION_LOC0;	
@@ -510,15 +502,29 @@ void HighResolutionTimerBase::setModeTransceiverTimer(bool input){
 			|   TIMER_CC_CTRL_INSEL_PRS
 			|   TIMER_CC_CTRL_ICEDGE_RISING
 			|   TIMER_CC_CTRL_MODE_INPUTCAPTURE;
-    
+        
+        TIMER2->CC[1].CTRL = TIMER_CC_CTRL_MODE_OUTPUTCOMPARE;
+        TIMER2->ROUTE &= ~TIMER_ROUTE_CC1PEN; //used as timeout the incoming event
+    }else{
 	//For output capture feature:
 	//Connect TIMER2->CC1 to pin PA9 aka stxon
-	if(input){
-	    TIMER2->ROUTE |= TIMER_ROUTE_CC1PEN; //used to trigger at a give time on the stxon
-	}else{
-	    TIMER2->ROUTE &= ~TIMER_ROUTE_CC1PEN; //used as timeout the incoming event
-	}
+        TIMER2->ROUTE |= TIMER_ROUTE_CC1PEN; //used to trigger at a give time on the stxon
 	TIMER2->CC[1].CTRL = TIMER_CC_CTRL_MODE_OUTPUTCOMPARE;
+    }
+}
+
+void HighResolutionTimerBase::cleanBufferGPIO(){
+    falseRead(&TIMER3->CC[2].CCV);
+    falseRead(&TIMER1->CC[2].CCV);
+    falseRead(&TIMER3->CC[2].CCV);
+    falseRead(&TIMER1->CC[2].CCV);
+}
+
+void HighResolutionTimerBase::cleanBufferTrasceiver(){
+    falseRead(&TIMER3->CC[0].CCV);
+    falseRead(&TIMER2->CC[0].CCV);
+    falseRead(&TIMER3->CC[0].CCV);
+    falseRead(&TIMER2->CC[0].CCV);
 }
 
 WaitResult HighResolutionTimerBase::IRQsetGPIOtimeout(long long tick){
@@ -630,4 +636,6 @@ HighResolutionTimerBase::HighResolutionTimerBase() {
 
 HighResolutionTimerBase::~HighResolutionTimerBase() {
     delete tc;
-}
\ No newline at end of file
+}
+
+int HighResolutionTimerBase::aux=0;
\ No newline at end of file
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 d4f79423..84eb4f34 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
@@ -112,7 +112,7 @@ class HighResolutionTimerBase {
         WaitResult IRQsetTransceiverTimeout(long long tick);
 
         virtual ~HighResolutionTimerBase();
-
+        static int aux;
     protected:
         HighResolutionTimerBase();
         static const unsigned int freq;
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 72426b1c..f0234364 100644
--- a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp
+++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/transceiver.cpp
@@ -77,7 +77,7 @@ const auto timePerByte=32000;
 /// Time to send the first part of each packet (4 bytes preamble + 1 byte SFD)
 const auto preambleSfdTime=timePerByte*5;
 
-/// Timeout from sending STXON to when the SFD should be sent
+/// Timeout from sending STXON to when the SFD should be sent, around 500us
 const auto sfdTimeout=slack+turnaround+preambleSfdTime;
 
 /// Timeout from an SFD to when the maximum length packet should end
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 895e53f6..8c6b3e76 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
@@ -67,7 +67,8 @@ bool TransceiverTimer::absoluteWaitTrigger(long long tick){
 bool TransceiverTimer::absoluteWaitTimeoutOrEvent(long long tick){
     FastInterruptDisableLock dLock;
     if(b.IRQsetTransceiverTimeout(tick)==WaitResult::WAKEUP_IN_THE_PAST){
-	return true;
+	HighResolutionTimerBase::aux=1;
+        return true;
     }
     b.setModeTransceiverTimer(true);
     b.cleanBufferTrasceiver();
@@ -83,8 +84,10 @@ bool TransceiverTimer::absoluteWaitTimeoutOrEvent(long long tick){
     } while(tWaiting && tick>b.getCurrentTick());
     
     if(tWaiting==nullptr){
+        HighResolutionTimerBase::aux=2;
 	return false;
     }else{
+        HighResolutionTimerBase::aux=3;
 	return true;
     }
 }
-- 
GitLab