diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.cpp b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f1f23e71dde64937ba148b74d0b2ed87f32e6aa --- /dev/null +++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.cpp @@ -0,0 +1,100 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: VirtualHighResolutionTimerBase.cpp + * Author: fabiuz + * + * Created on November 10, 2016, 9:28 PM + */ + +#include "virtual_high_resolution_timer_base.h" +#include "../../../../debugpin.h" + +namespace miosix{ + +long long VirtualHighResolutionTimerBase::getCurrentTick(){ + FastInterruptDisableLock dLock; + long long vhtCount=HighResolutionTimerBase::getCurrentTick(); + return vhtCount-vhtSyncPointVht+vhtBase+vhtOffset; +} + +void VirtualHighResolutionTimerBase::setCurrentTick(long long value) +{ + //We don't actually overwrite the RTC in this case, as the VHT + //is a bit complicated, so we translate between the two times at + //the boundary of this class. + FastInterruptDisableLock dLock; + long long vhtCount= HighResolutionTimerBase::getCurrentTick(); + //vhtOffset+=value - (vhtCount -vhtSyncPointVht+vhtBase+vhtOffset); + vhtOffset=value-vhtCount+vhtSyncPointVht-vhtBase; +} + +/** + * This takes 17us to 48us + */ +void VirtualHighResolutionTimerBase::resync(){ + { + FastInterruptDisableLock dLock; + HighPin<debug1> x; + int prev=loopback32KHzIn::value(); + for(;;){ + int curr=loopback32KHzIn::value(); + if(curr-prev==-1) break; + prev=curr; + } + int high=TIMER3->CNT; + TIMER2->CC[2].CTRL |= TIMER_CC_CTRL_MODE_INPUTCAPTURE; + + //Wait until we capture the first raising edge in the 32khz wave, + //it takes max half period, less than 16us -->732.4ticks @48000000Hz + while((TIMER2->IF & TIMER_IF_CC2)==0) ; + + //Creating the proper value of vhtSyncPointVht + int high2=TIMER3->CNT; + TIMER2->CC[2].CTRL &= ~_TIMER_CC_CTRL_MODE_MASK; + //This is the exact point when the rtc is incremented to the actual value + TIMER2->IFC =TIMER_IFC_CC2; + if(high==high2){ + vhtSyncPointVht = high<<16 | TIMER2->CC[2].CCV; //FIXME:: add the highest part + }else{ + // 10000 are really enough to be sure that we are in the new window + if(TIMER2->CC[2].CCV < 735){ + vhtSyncPointVht = high2<<16 | TIMER2->CC[2].CCV; + }else{ + vhtSyncPointVht = high<<16 | TIMER2->CC[2].CCV; + } + } + vhtSyncPointVht=TIMER3->CNT | TIMER2->CC[2].CCV; + + vhtSyncPointRtc=rtc.IRQgetValue(); + { + unsigned long long conversion=vhtSyncPointRtc; + conversion*=HighResolutionTimerBase::freq; + conversion+=HighResolutionTimerBase::freq/2; //Round to nearest + conversion/=HighResolutionTimerBase::freq; + vhtBase=conversion; + } + } +} + +VirtualHighResolutionTimerBase& VirtualHighResolutionTimerBase::instance(){ + static VirtualHighResolutionTimerBase vhrtb; + return vhrtb; +} + +VirtualHighResolutionTimerBase::VirtualHighResolutionTimerBase(): rtc(Rtc::instance()) { + //the base class is already started + + TIMER2->CC[2].CTRL=TIMER_CC_CTRL_ICEDGE_RISING + | TIMER_CC_CTRL_FILT_DISABLE + | TIMER_CC_CTRL_INSEL_PIN + | TIMER_CC_CTRL_MODE_OFF; +} + +}//namespace miosix + + diff --git a/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.h b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.h new file mode 100644 index 0000000000000000000000000000000000000000..5337918f57f3e3a592eb31fe3433c921123a4b4a --- /dev/null +++ b/miosix/arch/cortexM3_efm32gg/efm32gg332f1024_wandstem/interfaces-impl/virtual_high_resolution_timer_base.h @@ -0,0 +1,47 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: VirtualHighResolutionTimerBase.h + * Author: fabiuz + * + * Created on November 10, 2016, 9:28 PM + */ + +#ifndef VIRTUALHIGHRESOLUTIONTIMERBASE_H +#define VIRTUALHIGHRESOLUTIONTIMERBASE_H + +#include "high_resolution_timer_base.h" +#include "rtc.h" + +namespace miosix{ + class VirtualHighResolutionTimerBase : public HighResolutionTimerBase{ + public: + static VirtualHighResolutionTimerBase& instance(); + /** + These 4 variables are used to manage the correction of the timers. + * vhtBase (high frequency): keeps the last sync point + * vhtSyncPointRtc (low frequency): keeps the last sync point, just a simple conversion from vhtBase + * vhtSyncPointVht (high frequency: keeps the precise value of last sync point + * vhtOffset (high frequency): keeps the difference between the actual time and the counter value + */ + static long long vhtBase; + static long long vhtSyncPointRtc; + static long long vhtSyncPointVht; + static long long vhtOffset; + long long getCurrentTick(); + void setCurrentTick(long long value); + void resync(); + private: + VirtualHighResolutionTimerBase(); + int syncVhtRtcPeriod; + Rtc& rtc; + }; +} + + +#endif /* VIRTUALHIGHRESOLUTIONTIMERBASE_H */ +