Skip to content

Commit

Permalink
Merge pull request #54 from ppescher/master
Browse files Browse the repository at this point in the history
added compatibility with SAM3A4C (used by OpenTracker)
  • Loading branch information
ivanseidel authored Apr 30, 2018
2 parents 1d29e01 + 13e90fe commit c3d688e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
26 changes: 18 additions & 8 deletions DueTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
Released into the public domain.
*/

#include <Arduino.h>
#if defined(_SAM3XA_)
#include "DueTimer.h"

const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = {
Expand All @@ -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
Expand All @@ -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

#if NUM_TIMERS > 6
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();
Expand All @@ -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){
/*
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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]();
Expand Down
14 changes: 11 additions & 3 deletions DueTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <inttypes.h>

/*
Expand All @@ -31,7 +31,11 @@
#endif


#if defined TC2
#define NUM_TIMERS 9
#else
#define NUM_TIMERS 6
#endif

class DueTimer
{
Expand All @@ -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])();

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version=1.4.8
author=Ivan Seidel <[email protected]>
maintainer=Ivan Seidel <[email protected]>
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

0 comments on commit c3d688e

Please sign in to comment.