Skip to content

Commit c3d688e

Browse files
authored
Merge pull request #54 from ppescher/master
added compatibility with SAM3A4C (used by OpenTracker)
2 parents 1d29e01 + 13e90fe commit c3d688e

4 files changed

Lines changed: 31 additions & 13 deletions

File tree

DueTimer.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
Released into the public domain.
99
*/
1010

11-
#include <Arduino.h>
12-
#if defined(_SAM3XA_)
1311
#include "DueTimer.h"
1412

1513
const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = {
@@ -19,9 +17,11 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = {
1917
{TC1,0,TC3_IRQn},
2018
{TC1,1,TC4_IRQn},
2119
{TC1,2,TC5_IRQn},
20+
#if NUM_TIMERS > 6
2221
{TC2,0,TC6_IRQn},
2322
{TC2,1,TC7_IRQn},
2423
{TC2,2,TC8_IRQn},
24+
#endif
2525
};
2626

2727
// Fix for compatibility with Servo library
@@ -34,14 +34,21 @@ const DueTimer::Timer DueTimer::Timers[NUM_TIMERS] = {
3434
(void (*)()) 1, // Timer 3 - Occupied
3535
(void (*)()) 1, // Timer 4 - Occupied
3636
(void (*)()) 1, // Timer 5 - Occupied
37+
#if NUM_TIMERS > 6
3738
(void (*)()) 0, // Timer 6
3839
(void (*)()) 0, // Timer 7
3940
(void (*)()) 0 // Timer 8
41+
#endif
4042
};
4143
#else
4244
void (*DueTimer::callbacks[NUM_TIMERS])() = {};
4345
#endif
46+
47+
#if NUM_TIMERS > 6
4448
double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
49+
#else
50+
double DueTimer::_frequency[NUM_TIMERS] = {-1,-1,-1,-1,-1,-1};
51+
#endif
4552

4653
/*
4754
Initializing all timers, so you can use them like this: Timer0.start();
@@ -57,9 +64,11 @@ DueTimer Timer1(1);
5764
DueTimer Timer4(4);
5865
DueTimer Timer5(5);
5966
#endif
67+
#if NUM_TIMERS > 6
6068
DueTimer Timer6(6);
6169
DueTimer Timer7(7);
6270
DueTimer Timer8(8);
71+
#endif
6372

6473
DueTimer::DueTimer(unsigned short _timer) : timer(_timer){
6574
/*
@@ -160,7 +169,7 @@ uint8_t DueTimer::bestClock(double frequency, uint32_t& retRC){
160169
float bestError = 9.999e99;
161170
do
162171
{
163-
ticks = (float) VARIANT_MCK / frequency / (float) clockConfig[clkId].divisor;
172+
ticks = (float) SystemCoreClock / frequency / (float) clockConfig[clkId].divisor;
164173
// error = abs(ticks - round(ticks));
165174
error = clockConfig[clkId].divisor * abs(ticks - round(ticks)); // Error comparison needs scaling
166175
if (error < bestError)
@@ -169,7 +178,7 @@ uint8_t DueTimer::bestClock(double frequency, uint32_t& retRC){
169178
bestError = error;
170179
}
171180
} while (clkId-- > 0);
172-
ticks = (float) VARIANT_MCK / frequency / (float) clockConfig[bestClock].divisor;
181+
ticks = (float) SystemCoreClock / frequency / (float) clockConfig[bestClock].divisor;
173182
retRC = (uint32_t) round(ticks);
174183
return clockConfig[bestClock].flag;
175184
}
@@ -204,16 +213,16 @@ DueTimer& DueTimer::setFrequency(double frequency){
204213

205214
switch (clock) {
206215
case TC_CMR_TCCLKS_TIMER_CLOCK1:
207-
_frequency[timer] = (double)VARIANT_MCK / 2.0 / (double)rc;
216+
_frequency[timer] = (double)SystemCoreClock / 2.0 / (double)rc;
208217
break;
209218
case TC_CMR_TCCLKS_TIMER_CLOCK2:
210-
_frequency[timer] = (double)VARIANT_MCK / 8.0 / (double)rc;
219+
_frequency[timer] = (double)SystemCoreClock / 8.0 / (double)rc;
211220
break;
212221
case TC_CMR_TCCLKS_TIMER_CLOCK3:
213-
_frequency[timer] = (double)VARIANT_MCK / 32.0 / (double)rc;
222+
_frequency[timer] = (double)SystemCoreClock / 32.0 / (double)rc;
214223
break;
215224
default: // TC_CMR_TCCLKS_TIMER_CLOCK4
216-
_frequency[timer] = (double)VARIANT_MCK / 128.0 / (double)rc;
225+
_frequency[timer] = (double)SystemCoreClock / 128.0 / (double)rc;
217226
break;
218227
}
219228

@@ -293,6 +302,7 @@ void TC5_Handler(void){
293302
DueTimer::callbacks[5]();
294303
}
295304
#endif
305+
#if NUM_TIMERS > 6
296306
void TC6_Handler(void){
297307
TC_GetStatus(TC2, 0);
298308
DueTimer::callbacks[6]();

DueTimer.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
Released into the public domain.
88
*/
99

10-
#ifdef __arm__
10+
#include "Arduino.h"
11+
12+
#if defined(_SAM3XA_)
1113

1214
#ifndef DueTimer_h
1315
#define DueTimer_h
1416

15-
#include "Arduino.h"
16-
1717
#include <inttypes.h>
1818

1919
/*
@@ -31,7 +31,11 @@
3131
#endif
3232

3333

34+
#if defined TC2
3435
#define NUM_TIMERS 9
36+
#else
37+
#define NUM_TIMERS 6
38+
#endif
3539

3640
class DueTimer
3741
{
@@ -54,9 +58,11 @@ class DueTimer
5458
friend void TC3_Handler(void);
5559
friend void TC4_Handler(void);
5660
friend void TC5_Handler(void);
61+
#if NUM_TIMERS > 6
5762
friend void TC6_Handler(void);
5863
friend void TC7_Handler(void);
5964
friend void TC8_Handler(void);
65+
#endif
6066

6167
static void (*callbacks[NUM_TIMERS])();
6268

@@ -98,9 +104,11 @@ extern DueTimer Timer1;
98104
extern DueTimer Timer4;
99105
extern DueTimer Timer5;
100106
#endif
107+
#if NUM_TIMERS > 6
101108
extern DueTimer Timer6;
102109
extern DueTimer Timer7;
103110
extern DueTimer Timer8;
111+
#endif
104112

105113
#endif
106114

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"type": "git",
1717
"url": "https://github.com/ivanseidel/DueTimer.git"
1818
},
19-
"version": "1.4.7",
19+
"version": "1.4.8",
2020
"license": "MIT",
2121
"frameworks": "arduino",
2222
"platforms": "atmelsam",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version=1.4.8
33
author=Ivan Seidel <ivanseidel@gmail.com>
44
maintainer=Ivan Seidel <ivanseidel@gmail.com>
55
sentence=Timer Library fully implemented for Arduino DUE
6-
paragraph=There are 9 Timer objects already instantiated for you: Timer0, Timer1, Timer2, Timer3, Timer4, Timer5, Timer6, Timer7 and Timer8.
6+
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.
77
category=Timing
88
url=https://github.com/ivanseidel/DueTimer
99
architectures=sam

0 commit comments

Comments
 (0)