From e8264746791c57a088f84c28f55d1df42d710b45 Mon Sep 17 00:00:00 2001 From: ecdr Date: Sat, 6 Sep 2014 15:01:44 -0700 Subject: [PATCH 1/6] Add ==, != operators --- DueTimer.h | 7 ++++++- README.md | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DueTimer.h b/DueTimer.h index dbbf11a..bb95444 100644 --- a/DueTimer.h +++ b/DueTimer.h @@ -71,6 +71,11 @@ class DueTimer double getFrequency(); long getPeriod(); + + inline __attribute__((always_inline)) bool operator== (const DueTimer& rhs) const + {return timer == rhs.timer; }; + inline __attribute__((always_inline)) bool operator!= (const DueTimer& rhs) const + {return timer != rhs.timer; }; }; // Just to call Timer.getAvailable instead of Timer::getAvailable() : @@ -93,4 +98,4 @@ extern DueTimer Timer8; #else #error Oops! Trying to include DueTimer on another device? -#endif \ No newline at end of file +#endif diff --git a/README.md b/README.md index c08de82..43ffcf6 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,12 @@ Timer.getAvailable().attachInterrupt(callback1).start(10); DueTimer::getAvailable().attachInterrupt(callback2).start(10); // Start timer on second available timer // And so on... + +DueTimer myTimer = Timer.getAvailable(); +if (myTimer != DueTimer(0)) +// Now we know that the timer returned is actually available +// Can compare timers using == or != + ``` ### Compatibility with Servo.h @@ -104,7 +110,7 @@ You will need uncommend the line in `DueTimer.h` in `DueTimer` folder inside the ### You don't need to know: -- `int timer` - Stores the object timer id (to acces Timers struct array). +- `int timer` - Stores the object timer id (to access Timers struct array). - `DueTimer(int _timer)` - Instantiate a new DueTimer object for Timer _timer (NOTE: All objects are already instantiated!). From 345b1772f0bb3aaff8e62c021a5b7d48fcdc1bca Mon Sep 17 00:00:00 2001 From: ppescher Date: Wed, 5 Oct 2016 17:26:27 +0200 Subject: [PATCH 2/6] added compatibility with SAM3A4C (used by OpenTracker) --- DueTimer.cpp | 28 +++++++++++++++++++--------- DueTimer.h | 8 ++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/DueTimer.cpp b/DueTimer.cpp index 950fffa..4d0c592 100644 --- a/DueTimer.cpp +++ b/DueTimer.cpp @@ -8,8 +8,6 @@ Released into the public domain. */ -#include -#if defined(_SAM3XA_) #include "DueTimer.h" const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { @@ -19,9 +17,11 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { {TC1,0,TC3_IRQn}, {TC1,1,TC4_IRQn}, {TC1,2,TC5_IRQn}, +#if NUM_TIMERS > 6 {TC2,0,TC6_IRQn}, {TC2,1,TC7_IRQn}, {TC2,2,TC8_IRQn}, +#endif }; // Fix for compatibility with Servo library @@ -34,14 +34,21 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { (void (*)()) 1, // Timer 3 - Occupied (void (*)()) 1, // Timer 4 - Occupied (void (*)()) 1, // Timer 5 - Occupied +#if NUM_TIMERS > 6 (void (*)()) 0, // Timer 6 (void (*)()) 0, // Timer 7 (void (*)()) 0 // Timer 8 +#endif }; #else void (*DueTimer::callbacks[NUM_TIMERS])() = {}; #endif -double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; +double DueTimer::_frequency[NUM_TIMERS] = { + -1,-1,-1,-1,-1,-1, +#if NUM_TIMERS > 6 + -1,-1,-1 +#endif + }; /* Initializing all timers, so you can use them like this: Timer0.start(); @@ -57,9 +64,11 @@ DueTimer Timer1(1); DueTimer Timer4(4); DueTimer Timer5(5); #endif +#if NUM_TIMERS > 6 DueTimer Timer6(6); DueTimer Timer7(7); DueTimer Timer8(8); +#endif DueTimer::DueTimer(unsigned short _timer) : timer(_timer){ /* @@ -160,7 +169,7 @@ uint8_t DueTimer::bestClock(double frequency, uint32_t& retRC){ float bestError = 9.999e99; do { - ticks = (float) VARIANT_MCK / frequency / (float) clockConfig[clkId].divisor; + ticks = (float) SystemCoreClock / frequency / (float) clockConfig[clkId].divisor; // error = abs(ticks - round(ticks)); error = clockConfig[clkId].divisor * abs(ticks - round(ticks)); // Error comparison needs scaling if (error < bestError) @@ -169,7 +178,7 @@ uint8_t DueTimer::bestClock(double frequency, uint32_t& retRC){ bestError = error; } } while (clkId-- > 0); - ticks = (float) VARIANT_MCK / frequency / (float) clockConfig[bestClock].divisor; + ticks = (float) SystemCoreClock / frequency / (float) clockConfig[bestClock].divisor; retRC = (uint32_t) round(ticks); return clockConfig[bestClock].flag; } @@ -204,16 +213,16 @@ DueTimer& DueTimer::setFrequency(double frequency){ switch (clock) { case TC_CMR_TCCLKS_TIMER_CLOCK1: - _frequency[timer] = (double)VARIANT_MCK / 2.0 / (double)rc; + _frequency[timer] = (double)SystemCoreClock / 2.0 / (double)rc; break; case TC_CMR_TCCLKS_TIMER_CLOCK2: - _frequency[timer] = (double)VARIANT_MCK / 8.0 / (double)rc; + _frequency[timer] = (double)SystemCoreClock / 8.0 / (double)rc; break; case TC_CMR_TCCLKS_TIMER_CLOCK3: - _frequency[timer] = (double)VARIANT_MCK / 32.0 / (double)rc; + _frequency[timer] = (double)SystemCoreClock / 32.0 / (double)rc; break; default: // TC_CMR_TCCLKS_TIMER_CLOCK4 - _frequency[timer] = (double)VARIANT_MCK / 128.0 / (double)rc; + _frequency[timer] = (double)SystemCoreClock / 128.0 / (double)rc; break; } @@ -293,6 +302,7 @@ void TC5_Handler(void){ DueTimer::callbacks[5](); } #endif +#if NUM_TIMERS > 6 void TC6_Handler(void){ TC_GetStatus(TC2, 0); DueTimer::callbacks[6](); diff --git a/DueTimer.h b/DueTimer.h index fdfe9c2..8d90bff 100644 --- a/DueTimer.h +++ b/DueTimer.h @@ -31,7 +31,11 @@ #endif +#if defined TC2 #define NUM_TIMERS 9 +#else +#define NUM_TIMERS 6 +#endif class DueTimer { @@ -54,9 +58,11 @@ class DueTimer friend void TC3_Handler(void); friend void TC4_Handler(void); friend void TC5_Handler(void); +#if NUM_TIMERS > 6 friend void TC6_Handler(void); friend void TC7_Handler(void); friend void TC8_Handler(void); +#endif static void (*callbacks[NUM_TIMERS])(); @@ -98,9 +104,11 @@ extern DueTimer Timer1; extern DueTimer Timer4; extern DueTimer Timer5; #endif +#if NUM_TIMERS > 6 extern DueTimer Timer6; extern DueTimer Timer7; extern DueTimer Timer8; +#endif #endif From b2a1c67c4b9c0bce4a4d36a82e93d7739e6f2ddb Mon Sep 17 00:00:00 2001 From: ppescher Date: Wed, 5 Oct 2016 17:47:48 +0200 Subject: [PATCH 3/6] updated library version and description --- library.json | 2 +- library.properties | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library.json b/library.json index c473e00..041832d 100644 --- a/library.json +++ b/library.json @@ -16,7 +16,7 @@ "type": "git", "url": "https://github.com/ivanseidel/DueTimer.git" }, - "version": "1.4.7", + "version": "1.4.8", "license": "MIT", "frameworks": "arduino", "platforms": "atmelsam", diff --git a/library.properties b/library.properties index da3eb6b..0218c04 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=DueTimer -version=1.4.7 +version=1.4.8 author=Ivan Seidel maintainer=Ivan Seidel sentence=Timer Library fully implemented for Arduino DUE -paragraph=There are 9 Timer objects already instantiated for you: Timer0, Timer1, Timer2, Timer3, Timer4, Timer5, Timer6, Timer7 and Timer8. +paragraph=There are 6 or 9 Timer objects already instantiated for you: Timer0, Timer1, Timer2, Timer3, Timer4, Timer5 and Timer6, Timer7, Timer8 where supported by the hardware. category=Timing url=https://github.com/ivanseidel/DueTimer architectures=sam From 13e90fef473b2b75ca7c2d349ff97f3fb5a6396f Mon Sep 17 00:00:00 2001 From: ppescher Date: Thu, 6 Apr 2017 15:33:02 +0200 Subject: [PATCH 4/6] reworked patch to meet ivanseidel suggestions --- DueTimer.cpp | 8 ++++---- DueTimer.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/DueTimer.cpp b/DueTimer.cpp index 4d0c592..8183fb1 100644 --- a/DueTimer.cpp +++ b/DueTimer.cpp @@ -43,12 +43,12 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { #else void (*DueTimer::callbacks[NUM_TIMERS])() = {}; #endif -double DueTimer::_frequency[NUM_TIMERS] = { - -1,-1,-1,-1,-1,-1, + #if NUM_TIMERS > 6 - -1,-1,-1 +double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; +#else +double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1}; #endif - }; /* Initializing all timers, so you can use them like this: Timer0.start(); diff --git a/DueTimer.h b/DueTimer.h index 8d90bff..79f5907 100644 --- a/DueTimer.h +++ b/DueTimer.h @@ -7,13 +7,13 @@ Released into the public domain. */ -#ifdef __arm__ +#include "Arduino.h" + +#if defined(_SAM3XA_) #ifndef DueTimer_h #define DueTimer_h -#include "Arduino.h" - #include /* From 18d9ad070524ecec331cae11d65fcad3d79964e0 Mon Sep 17 00:00:00 2001 From: Nishanth Samala Date: Wed, 22 Apr 2020 16:08:13 -0400 Subject: [PATCH 5/6] fixed a bit of accidentally removed code --- DueTimer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DueTimer.cpp b/DueTimer.cpp index a3afc52..c775538 100644 --- a/DueTimer.cpp +++ b/DueTimer.cpp @@ -46,8 +46,10 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { #endif #if NUM_TIMERS > 6 +double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; uint32_t DueTimer::_statusRegister[NUM_TIMERS] = {0,0,0,0,0,0,0,0}; #else +double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1}; uint32_t DueTimer::_statusRegister[NUM_TIMERS] = {0,0,0,0,0,0}; #endif From 9c3974ab7541688b8e58e02cc43fa147590ca752 Mon Sep 17 00:00:00 2001 From: Nishanth Samala Date: Wed, 22 Apr 2020 16:08:30 -0400 Subject: [PATCH 6/6] fix again --- DueTimer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DueTimer.cpp b/DueTimer.cpp index c775538..a43c354 100644 --- a/DueTimer.cpp +++ b/DueTimer.cpp @@ -49,7 +49,7 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = { double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1,-1,-1}; uint32_t DueTimer::_statusRegister[NUM_TIMERS] = {0,0,0,0,0,0,0,0}; #else -double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1}; +double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1}; uint32_t DueTimer::_statusRegister[NUM_TIMERS] = {0,0,0,0,0,0}; #endif