forked from rookie/FrequencyTimer2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FrequencyTimer2.cpp
225 lines (198 loc) · 5.19 KB
/
FrequencyTimer2.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
FrequencyTimer2.h - A frequency generator and interrupt generator library
Author: Jim Studt, [email protected]
Copyright (c) 2007 David A. Mellis. All right reserved.
http://www.arduino.cc/playground/Code/FrequencyTimer2
Version 2 - updated by Paul Stoffregen, [email protected]
for compatibility with newer hardware and Arduino 1.0
Modified by Tim Barrass 2013,
-added support for ATMEGA32U4 processors (Leonardo,Teensy2.0)
using Timer 4 instead of Timer 2
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <FrequencyTimer2.h>
#include <avr/interrupt.h>
void (*FrequencyTimer2::onOverflow)() = 0;
uint8_t FrequencyTimer2::enabled = 0;
#if defined(TIMER2_COMPA_vect)
ISR(TIMER2_COMPA_vect)
#elif defined(TIMER2_COMP_vect)
ISR(TIMER2_COMP_vect)
#elif defined(TIMER4_COMPA_vect)
ISR(TIMER4_COMPA_vect)
#else
#error "This board does not have a hardware timer which is compatible with FrequencyTimer2"
void dummy_function(void)
#endif
{
static uint8_t inHandler = 0; // protect us from recursion if our handler enables interrupts
if ( !inHandler && FrequencyTimer2::onOverflow) {
inHandler = 1;
(*FrequencyTimer2::onOverflow)();
inHandler = 0;
}
}
void FrequencyTimer2::setOnOverflow( void (*func)() )
{
FrequencyTimer2::onOverflow = func;
#if defined(TIMSK2)
// enable compare match interrupt
if ( func) TIMSK2 |= _BV(OCIE2A);
else TIMSK2 &= ~_BV(OCIE2A);
#elif defined(TIMSK)
if ( func) TIMSK |= _BV(OCIE2);
else TIMSK &= ~_BV(OCIE2);
#elif defined(TIMSK4)
// output compare interrupt enable 4A
if ( func) TIMSK4 = _BV(OCIE4A);
else TIMSK4 = 0;
#endif
}
void FrequencyTimer2::setPeriod(unsigned long period)
{
uint8_t pre, top;
if ( period == 0) period = 1;
period *= clockCyclesPerMicrosecond();
period /= 2; // we work with half-cycles before the toggle
#if defined(TCCR2A) || defined(TCCR2)
if ( period <= 256) {
pre = 1;
top = period-1;
} else if ( period <= 256L*8) { // this for AUDIO_RATE 16384, pre=2 is a bitfield 010 which means prescaler = 8
pre = 2;
top = period/8-1;
} else if ( period <= 256L*32) {
pre = 3;
top = period/32-1;
} else if ( period <= 256L*64) {
pre = 4;
top = period/64-1;
} else if ( period <= 256L*128) {
pre = 5;
top = period/128-1;
} else if ( period <= 256L*256) {
pre = 6;
top = period/256-1;
} else if ( period <= 256L*1024) {
pre = 7;
top = period/1024-1;
} else {
pre = 7;
top = 255;
}
#elif defined(TCCR4A)
unsigned long prescaler = 1;
for(int i=1; i<=16; i++){
if ( period <= 256*prescaler){
pre = i;
top = period/prescaler -1;
break;
}
prescaler *= 2;
}
//pre = 4; // this for AUDIO_RATE 16384, pre=4 is a bitfield 100 which means prescaler = 8
//top = period/8-1;
#endif
#if defined(TCCR2A)
TCCR2B = 0;
TCCR2A = 0;
TCNT2 = 0;
#if defined(ASSR) && defined(AS2)
ASSR &= ~_BV(AS2); // use clock, not T2 pin
#endif
OCR2A = top;
//Clear Timer on Compare Match (CTC) mode
TCCR2A = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM2A0) : 0));
TCCR2B = pre;
#elif defined(TCCR2)
TCCR2 = 0;
TCNT2 = 0;
ASSR &= ~_BV(AS2); // use clock, not T2 pin
OCR2 = top;
TCCR2 = (_BV(WGM21) | ( FrequencyTimer2::enabled ? _BV(COM20) : 0) | pre);
#elif defined(TCCR4A) // TB2013 for 32u4 (leonardo,teensy)
TCCR4A = 0;
TCCR4B = 0;
TCCR4C = 0;
TCCR4D = 0;
TCCR4E = 0;
TCNT4 = 0;
OCR4C= top; // Table 15-19
//TCCR4A = _BV(COM4A1);
TCCR4B = pre ;
TIMSK4 = FrequencyTimer2::enabled ? _BV(OCIE4A) : 0;
#endif
}
unsigned long FrequencyTimer2::getPeriod()
{
#if defined(TCCR2B)
uint8_t p = (TCCR2B & 7);
unsigned long v = OCR2A;
#elif defined(TCCR)
uint8_t p = (TCCR2 & 7);
unsigned long v = OCR2;
#elif defined(TCCR4B)
uint8_t p = (TCCR4B & 7);
unsigned long v = OCR4C;
#endif
uint8_t shift;
switch(p) {
case 0 ... 1:
shift = 0;
break;
case 2:
shift = 3;
break;
case 3:
shift = 5;
break;
case 4:
shift = 6;
break;
case 5:
shift = 7;
break;
case 6:
shift = 8;
break;
case 7:
shift = 10;
break;
}
return (((v+1) << (shift+1)) + 1) / clockCyclesPerMicrosecond(); // shift+1 converts from half-period to period
}
void FrequencyTimer2::enable()
{
FrequencyTimer2::enabled = 1;
#if defined(TCCR2A)
//Toggle OC2A on Compare Match
TCCR2A |= _BV(COM2A0);
#elif defined(TCCR2)
TCCR2 |= _BV(COM20);
#elif defined(TCCR4A)
//TCCR4A |= _BV(COM4A0);
TIMSK4 = _BV(OCIE4A);
#endif
}
void FrequencyTimer2::disable()
{
FrequencyTimer2::enabled = 0;
#if defined(TCCR2A)
TCCR2A &= ~_BV(COM2A0);
#elif defined(TCCR2)
TCCR2 &= ~_BV(COM20);
#elif defined(TCCR4A)
//TCCR4A &= ~_BV(COM4A0);
TIMSK4 = 0;
#endif
}