-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathDueTimer.h
122 lines (95 loc) · 2.83 KB
/
DueTimer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
DueTimer.h - DueTimer header file, definition of methods and attributes...
For instructions, go to https://github.com/ivanseidel/DueTimer
Created by Ivan Seidel Gomes, March, 2013.
Modified by Philipp Klaus, June 2013.
Released into the public domain.
*/
#include "Arduino.h"
#if defined(_SAM3XA_)
#ifndef DueTimer_h
#define DueTimer_h
#include <inttypes.h>
/*
This fixes compatibility for Arduono Servo Library.
Uncomment to make it compatible.
Note that:
+ Timers: 0,2,3,4,5 WILL NOT WORK, and will
neither be accessible by Timer0,...
*/
// #define USING_SERVO_LIB true
#ifdef USING_SERVO_LIB
#warning "HEY! You have set flag USING_SERVO_LIB. Timer0, 2,3,4 and 5 are not available"
#endif
#if defined TC2
#define NUM_TIMERS 9
#else
#define NUM_TIMERS 6
#endif
class DueTimer
{
protected:
// Represents the timer id (index for the array of Timer structs)
const unsigned short timer;
// Stores the object timer frequency
// (allows to access current timer period and frequency):
static double _frequency[NUM_TIMERS];
// Picks the best clock to lower the error
static uint8_t bestClock(double frequency, uint32_t& retRC);
// Make Interrupt handlers friends, so they can use callbacks
friend void TC0_Handler(void);
friend void TC1_Handler(void);
friend void TC2_Handler(void);
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])();
struct Timer
{
Tc *tc;
uint32_t channel;
IRQn_Type irq;
};
// Store timer configuration (static, as it's fixed for every object)
static const Timer Timers[NUM_TIMERS];
public:
static DueTimer getAvailable(void);
DueTimer(unsigned short _timer);
DueTimer& attachInterrupt(void (*isr)());
DueTimer& detachInterrupt(void);
DueTimer& start(double microseconds = -1);
DueTimer& stop(void);
DueTimer& setFrequency(double frequency);
DueTimer& setPeriod(double microseconds);
double getFrequency(void) const;
double getPeriod(void) const;
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() :
extern DueTimer Timer;
extern DueTimer Timer1;
// Fix for compatibility with Servo library
#ifndef USING_SERVO_LIB
extern DueTimer Timer0;
extern DueTimer Timer2;
extern DueTimer Timer3;
extern DueTimer Timer4;
extern DueTimer Timer5;
#endif
#if NUM_TIMERS > 6
extern DueTimer Timer6;
extern DueTimer Timer7;
extern DueTimer Timer8;
#endif
#endif
#else
#error Oops! Trying to include DueTimer on another device?
#endif