-
Notifications
You must be signed in to change notification settings - Fork 18
/
timer_setup.c
414 lines (360 loc) · 13.3 KB
/
timer_setup.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// based on
// https://github.com/arduino/Arduino/blob/ide-1.5.x/libraries/Servo/src/avr/Servo.cpp
// https://github.com/arduino/Arduino/blob/ide-1.5.x/libraries/Servo/src/avr/ServoTimers.h
// see also
// http://www.robotshop.com/letsmakerobots/arduino-101-timers-and-interrupts
// http://playground.arduino.cc/Code/Timer1
// from Arduino Servo.h:
/*
Servo.h - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
Copyright (c) 2009 Michael Margolis. All right reserved.
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
*/
//************************************************************************
// arduino-timer-api edits and additions:
// Anton Moiseev, 2017
//************************************************************************
#ifdef ARDUINO_ARCH_AVR
#include "timer_setup.h"
#include <avr/interrupt.h>
/*
* Defines for 16 bit timers used with Servo library
*
* If _useTimerX is defined then TimerX is a 16 bit timer on the current board
* timer16_Sequence_t enumerates the sequence that the timers should be allocated
* _Nbr_16timers indicates how many 16 bit timers are available.
*/
/**
* AVR Only definitions
* --------------------
*/
// Say which 16 bit timers can be used and in what order
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define _useTimer5
#define _useTimer1
#define _useTimer3
#define _useTimer4
typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t;
const int _TIMER1 = _timer1;
const int _TIMER3 = _timer3;
const int _TIMER4 = _timer4;
const int _TIMER5 = _timer5;
const int TIMER_DEFAULT = _timer5; //TIMER5;
#elif defined(__AVR_ATmega32U4__)
#define _useTimer1
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;
const int _TIMER1 = _timer1;
const int TIMER_DEFAULT = _timer1; //TIMER1;
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
#define _useTimer3
#define _useTimer1
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t;
const int _TIMER1 = _timer1;
const int _TIMER3 = _timer3;
const int TIMER_DEFAULT = _timer3; //TIMER3;
#elif defined(__AVR_ATmega128__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega2561__)
#define _useTimer3
#define _useTimer1
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t;
const int _TIMER1 = _timer1;
const int _TIMER3 = _timer3;
const int TIMER_DEFAULT = _timer3; //TIMER3;
#else // everything else
#define _useTimer1
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;
const int _TIMER1 = _timer1;
const int TIMER_DEFAULT = _timer1; //TIMER1;
#endif
// Define timer prescaler options:
// CS12 CS11 CS10
const int TIMER_PRESCALER_1_1 = 1; // 001
const int TIMER_PRESCALER_1_8 = 2; // 010
const int TIMER_PRESCALER_1_64 = 3; // 011
const int TIMER_PRESCALER_1_256 = 4; // 100
const int TIMER_PRESCALER_1_1024 = 5; // 101
#ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform
// Interrupt handlers for Arduino
#if defined(_useTimer1)
SIGNAL (TIMER1_COMPA_vect) {
timer_handle_interrupts(_timer1);
}
#endif
#if defined(_useTimer3)
SIGNAL (TIMER3_COMPA_vect) {
timer_handle_interrupts(_timer3);
}
#endif
#if defined(_useTimer4)
SIGNAL (TIMER4_COMPA_vect) {
timer_handle_interrupts(_timer4);
}
#endif
#if defined(_useTimer5)
SIGNAL (TIMER5_COMPA_vect) {
timer_handle_interrupts(_timer5);
}
#endif
#elif defined WIRING
// Interrupt handlers for Wiring
#if defined(_useTimer1)
void Timer1Service() {
timer_handle_interrupts(_timer1);
}
#endif
#if defined(_useTimer3)
void Timer3Service() {
timer_handle_interrupts(_timer3);
}
#endif
#endif
// Core funcs
/**
* Init ISR (Interrupt service routine) for the timer and start timer.
*
* http://www.robotshop.com/letsmakerobots/arduino-101-timers-and-interrupts
* 1. CPU frequency 16Mhz for Arduino
* 2. maximum timer counter value (256 for 8bit, 65536 for 16bit timer)
* 3. Divide CPU frequency through the choosen prescaler (16000000 / 256 = 62500)
* 4. Divide result through the desired frequency (62500 / 2Hz = 31250)
* 5. Verify the result against the maximum timer counter value (31250 < 65536 success).
* If fail, choose bigger prescaler.
*
* Example: to set timer clock period to 20ms (50 operations per second == 50Hz) on 16MHz CPU
* use prescaler 1:8 (TIMER_PRESCALER_1_8) and adjustment=40000-1:
* 16000000/8/50=40000, minus 1 cause count from zero.
*
* Timer interrupt handler timer_handle_interrupts would be called every 20ms (50 times per second)
* in this case.
*
* @param timer
* system timer id: use TIMER_DEFAULT for default timer
* or TIMER3, TIMER4 or TIMER5 for specific timer
* @param prescaler
* timer prescaler (1, 8, 64, 256, 1024),
* use constants: PRESCALER_1, PRESCALER_8,
* PRESCALER_64, PRESCALER_256, PRESCALER_1024
* @param adjustment
* adjustment divider after timer prescaled - timer compare match value
*/
void timer_init_ISR(int timer, int prescaler, unsigned int adjustment) {
#if defined (_useTimer1)
if(timer == _timer1) {
// CS12 CS11 CS10
// 1/1: 001
// 1/8: 010
// 1/64: 011
// 1/256: 100
// 1/1024: 101
unsigned char prescalerBits = 0;
if(prescaler == TIMER_PRESCALER_1_1) {
prescalerBits = _BV(CS10);
} else if(prescaler == TIMER_PRESCALER_1_8) {
prescalerBits = _BV(CS11);
} else if(prescaler == TIMER_PRESCALER_1_64) {
prescalerBits = _BV(CS11) | _BV(CS10);
} else if(prescaler == TIMER_PRESCALER_1_256) {
prescalerBits = _BV(CS12);
} else if(prescaler == TIMER_PRESCALER_1_1024) {
prescalerBits = _BV(CS12) | _BV(CS10);
} else {
// let it be no prescaler by default:
prescalerBits = _BV(CS10);
}
TCCR1A = 0; // normal counting mode
TCCR1B = 0; // timer mode:
TCCR1B |= _BV(WGM12); // CTC mode
TCCR1B |= prescalerBits;// set prescaler
OCR1A = adjustment; // compare match register
TCNT1 = 0; // clear the timer count
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
TIFR |= _BV(OCF1A); // clear any pending interrupts
TIMSK |= _BV(OCIE1A); // enable the output compare interrupt
#else
// here if not ATmega8 or ATmega128
TIFR1 |= _BV(OCF1A); // clear any pending interrupts
TIMSK1 |= _BV(OCIE1A); // enable the output compare interrupt
#endif
#if defined(WIRING)
timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service);
#endif
}
#endif
#if defined (_useTimer3)
if(timer == _timer3) {
// CS32 CS31 CS30
// 1/1: 001
// 1/8: 010
// 1/64: 011
// 1/256: 100
// 1/1024: 101
unsigned char prescalerBits = 0;
if(prescaler == TIMER_PRESCALER_1_1) {
prescalerBits = _BV(CS30);
} else if(prescaler == TIMER_PRESCALER_1_8) {
prescalerBits = _BV(CS31);
} else if(prescaler == TIMER_PRESCALER_1_64) {
prescalerBits = _BV(CS31) | _BV(CS30);
} else if(prescaler == TIMER_PRESCALER_1_256) {
prescalerBits = _BV(CS32);
} else if(prescaler == TIMER_PRESCALER_1_1024) {
prescalerBits = _BV(CS32) | _BV(CS30);
} else {
// let it be no prescaler by default:
prescalerBits = _BV(CS30);
}
TCCR3A = 0; // normal counting mode
TCCR3B = 0; // timer mode:
TCCR3B |= _BV(WGM32); // CTC mode
TCCR3B |= prescalerBits;// set prescaler
OCR3A = adjustment; // compare match register
TCNT3 = 0; // clear the timer count
#if defined(__AVR_ATmega128__)
TIFR |= _BV(OCF3A); // clear any pending interrupts
ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt
#else
TIFR3 |= _BV(OCF3A); // clear any pending interrupts
TIMSK3 |= _BV(OCIE3A); // enable the output compare interrupt
#endif
#if defined(WIRING)
timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only
#endif
}
#endif
#if defined (_useTimer4)
if(timer == _timer4) {
// CS42 CS41 CS40
// 1/1: 001
// 1/8: 010
// 1/64: 011
// 1/256: 100
// 1/1024: 101
unsigned char prescalerBits;
if(prescaler == TIMER_PRESCALER_1_1) {
prescalerBits = _BV(CS40);
} else if(prescaler == TIMER_PRESCALER_1_8) {
prescalerBits = _BV(CS41);
} else if(prescaler == TIMER_PRESCALER_1_64) {
prescalerBits = _BV(CS41) | _BV(CS40);
} else if(prescaler == TIMER_PRESCALER_1_256) {
prescalerBits = _BV(CS42);
} else if(prescaler == TIMER_PRESCALER_1_1024) {
prescalerBits = _BV(CS42) | _BV(CS40);
} else {
// let it be no prescaler by default,
// the timer would work, but result would be wrong anyway
prescalerBits = _BV(CS40);
}
TCCR4A = 0; // normal counting mode
TCCR4B = 0; // timer mode:
TCCR4B |= _BV(WGM42); // CTC mode
TCCR4B |= prescalerBits;// set prescaler
OCR4A = adjustment; // compare match register
TCNT4 = 0; // clear the timer count
TIFR4 |= _BV(OCF4A); // clear any pending interrupts
TIMSK4 |= _BV(OCIE4A); // enable the output compare interrupt
}
#endif
#if defined (_useTimer5)
if(timer == _timer5) {
// CS52 CS51 CS50
// 1/1: 001
// 1/8: 010
// 1/64: 011
// 1/256: 100
// 1/1024: 101
unsigned char prescalerBits = 0;
if(prescaler == TIMER_PRESCALER_1_1) {
prescalerBits = _BV(CS50);
} else if(prescaler == TIMER_PRESCALER_1_8) {
prescalerBits = _BV(CS51);
} else if(prescaler == TIMER_PRESCALER_1_64) {
prescalerBits = _BV(CS51) | _BV(CS50);
} else if(prescaler == TIMER_PRESCALER_1_256) {
prescalerBits = _BV(CS52);
} else if(prescaler == TIMER_PRESCALER_1_1024) {
prescalerBits = _BV(CS52) | _BV(CS50);
} else {
// let it be no prescaler by default:
prescalerBits = _BV(CS50);
}
TCCR5A = 0; // normal counting mode
TCCR5B = 0; // timer mode:
TCCR5B |= _BV(WGM52); // CTC mode
TCCR5B |= prescalerBits;// set prescaler
OCR5A = adjustment; // compare match register
TCNT5 = 0; // clear the timer count
TIFR5 |= _BV(OCF5A); // clear any pending interrupts
TIMSK5 |= _BV(OCIE5A); // enable the output compare interrupt
}
#endif
}
/**
* Stop ISR (Interrupt service routine) for the timer.
*
* @param timer
* system timer id for started ISR
*/
void timer_stop_ISR(int timer) {
//disable use of the given timer
#if defined WIRING // Wiring
if(timer == _timer1) {
#if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
TIMSK1 &= ~_BV(OCIE1A); // disable timer 1 output compare interrupt
#else
TIMSK &= ~_BV(OCIE1A); // disable timer 1 output compare interrupt
#endif
timerDetach(TIMER1OUTCOMPAREA_INT);
} else if(timer == _timer3) {
#if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt
#else
ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt
#endif
timerDetach(TIMER3OUTCOMPAREA_INT);
}
#else
// Tested only on Arduino Leonardo
// post comments on other plaforms:
// https://github.com/sadr0b0t/arduino-timer-api/issues/6
#if defined (_useTimer1)
if(timer == _timer1) {
#if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
TIMSK &= ~_BV(OCIE1A); // disable output compare interrupt
#else
// here if not ATmega8 or ATmega128
TIMSK1 &= ~_BV(OCIE1A); // disable output compare interrupt
#endif
}
#endif
#if defined (_useTimer3)
if(timer == _timer3) {
#if defined(__AVR_ATmega128__)
ETIMSK &= ~_BV(OCIE3A); // disable output compare interrupt
#else
TIMSK3 &= ~_BV(OCIE3A); // disable output compare interrupt
#endif
}
#endif
#if defined (_useTimer4)
if(timer == _timer4) {
TIMSK4 &= ~_BV(OCIE4A); // disable output compare interrupt
}
#endif
#if defined (_useTimer5)
if(timer == _timer5) {
TIMSK5 &= ~_BV(OCIE5A); // disable output compare interrupt
}
#endif
#endif
}
#endif // ARDUINO_ARCH_AVR