-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.c
393 lines (311 loc) · 8.26 KB
/
timer.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
/*
* ex: set syntax=c tabstop=8 noexpandtab shiftwidth=8:
*
* cw-kbd 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 3 of the License, or (at
* your option) any later version.
*
* cw-kbd 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 cw-kbd. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright © 2009-2010, Vernon Mauery (N7OH)
*/
#include <avr/interrupt.h>
#include "timer.h"
#define TIMER0_ENABLED
//#define TIMER1_ENABLED
#define TIMER3_ENABLED
#ifdef TIMER0_ENABLED
/*
* Timer 0
*/
static timer_callback timer0_overflow_callback_ = NULL;
static timer_callback timer0_compa_callback_ = NULL;
static timer_callback timer0_compb_callback_ = NULL;
/* Timer 0 Overflow */
ISR( TIMER0_OVF_vect)
{
if( timer0_overflow_callback_) timer0_overflow_callback_();
}
/* Timer 0 Comp A */
ISR( TIMER0_COMPA_vect)
{
if( timer0_compa_callback_) timer0_compa_callback_();
}
/* Timer 0 Overflow */
ISR( TIMER0_COMPB_vect)
{
if( timer0_compb_callback_) timer0_compb_callback_();
}
bool timer0_init(clock_select8_t clock_select, t8_mode_t mode, t8_interrupt_t interrupts_mask)
{
/*check that the clock = 0 (TCCR0A & 0x7) */
if( TCCR0B & 0x7 ){
/* timer already in use */
return false;
}
TCNT0 = 0;
OCR0A = 0xff;
TIMSK0 = interrupts_mask; /* don't forget to set the "i" bit in the Status Register */
TCCR0A = (/*wgm11:10*/mode & 0x03);
TCCR0B = ((/*wgm13:12*/mode & 0x0C)<<1) | clock_select;
return true;
}
void timer0_set_scale(clock_select8_t scale) {
TCCR0B = scale & 0x7;
}
uint8_t timer0_read()
{
return TCNT0;
}
void timer0_write(uint8_t value)
{
TCNT0 = value;
}
void timer0_set_interrupts(t8_interrupt_t mask)
{
TIMSK0 = mask; /* don't forget to set the "i" bit in the Status Register */
}
void timer0_set_overflow_callback(timer_callback callback)
{
timer0_overflow_callback_= callback;
}
void timer0_set_compa_callback(timer_callback callback)
{
timer0_compa_callback_ = callback;
}
void timer0_set_compb_callback(timer_callback callback)
{
timer0_compb_callback_ = callback;
}
void timer0_stop(){
TCCR0B = 0;
}
void timer0_set_top(uint8_t top)
{
/* NOTE: the top value is NOT used in normal mode
* NOTE: only those modes specified in t8_mode_t are implemented
*/
OCR0A = top;
}
uint8_t timer0_get_top()
{
/* NOTE: the top value is NOT used in normal mode
*/
return OCR0A;
}
#endif
/*
* Timer 1
*/
#ifdef TIMER1_ENABLED
static t16_mode_t timer1_mode_value = 0;
static timer_callback timer1_capture_callback_ = NULL;
static timer_callback timer1_compare_a_callback_ = NULL;
static timer_callback timer1_compare_b_callback_ = NULL;
static timer_callback timer1_overflow_callback_ = NULL;
/* Timer 1 Capture event */
ISR( TIMER1_CAPT_vect )
{
if( timer1_capture_callback_) timer1_capture_callback_();
}
/* Timer 1 Compare match A */
ISR( TIMER1_COMPA_vect )
{
if( timer1_compare_a_callback_) timer1_compare_a_callback_();
}
/* Timer 1 Compare match B */
ISR( TIMER1_COMPB_vect )
{
if( timer1_compare_b_callback_) timer1_compare_b_callback_();
}
/* Timer 1 Overflow */
ISR( TIMER1_OVF_vect)
{
if( timer1_overflow_callback_) timer1_overflow_callback_();
}
bool timer1_init(clock_select16_t clock_select, t16_mode_t mode, t16_interrupt_t interrupts_mask)
{
timer1_mode_value = mode;
set_reg_16(ICR1, 65535);
timer1_set_compare(65535);
TCCR1A = (/*wgm11:10*/mode & 0x03);
TCCR1B = ((/*wgm13:12*/mode & 0x0C)<<1) | clock_select;
TIMSK1 = interrupts_mask & 0x2f; /* don't forget to set the "i" bit in the Status Register */
timer1_write( 0 );
return true;
}
void timer1_set_scale(clock_select16_t scale) {
TCCR1B = (TCCR1B & 0xf8) | scale;
}
uint16_t timer1_read()
{
return get_reg_16(TCNT1);
}
void timer1_write(uint16_t value)
{
set_reg_16(TCNT1, value);
}
void timer1_set_interrupts(t16_interrupt_t interrupts_mask)
{
TIMSK1 = (TIMSK1 & ~0x2f) | (interrupts_mask & 0x2f); /* don't forget to set the "i" bit in the Status Register */
}
void timer1_set_compare(uint16_t compare)
{
/* NOTE: the compare value is NOT used in normal or CTC modes */
set_reg_16(OCR1A, compare);
}
uint16_t timer1_get_compare()
{
/* NOTE: the compare value is NOT used in normal or CTC modes */
return get_reg_16(OCR1A);
}
void timer1_set_top(uint16_t top)
{
/* NOTE: the top value is NOT used in normal mode
* NOTE: only those modes specified in t16_mode_t are implemented
*/
set_reg_16(OCR1A, top);
}
uint16_t timer1_get_top()
{
/* NOTE: the top value is NOT used in normal mode
*/
return get_reg_16(OCR1A);
}
void timer1_set_compare_output_mode(compare_output_mode_t compare_output_mode)
{
TCCR1A = (TCCR1A & 0x3f) | ( compare_output_mode << 6 );
}
void timer1_set_capture_callback(timer_callback callback)
{
timer1_capture_callback_ = callback;
}
void timer1_set_compare_a_callback(timer_callback callback)
{
timer1_compare_a_callback_ = callback;
}
void timer1_set_compare_b_callback(timer_callback callback)
{
timer1_compare_b_callback_ = callback;
}
void timer1_set_overflow_callback(timer_callback callback){
timer1_overflow_callback_ = callback;
}
void timer1_stop(){
TCCR1B &= 0xf8;
}
#endif
/*
* Timer 3
*/
#ifdef TIMER3_ENABLED
static t16_mode_t timer3_mode_value = 0;
static timer_callback timer3_capture_callback_ = NULL;
static timer_callback timer3_compare_a_callback_ = NULL;
static timer_callback timer3_compare_b_callback_ = NULL;
static timer_callback timer3_compare_c_callback_ = NULL;
static timer_callback timer3_overflow_callback_ = NULL;
/* Timer 3 Capture event */
ISR( TIMER3_CAPT_vect )
{
if( timer3_capture_callback_) timer3_capture_callback_();
}
/* Timer 3 Compare match A */
ISR( TIMER3_COMPA_vect )
{
if( timer3_compare_a_callback_) timer3_compare_a_callback_();
}
/* Timer 3 Compare match B */
ISR( TIMER3_COMPB_vect )
{
if( timer3_compare_b_callback_) timer3_compare_b_callback_();
}
/* Timer 3 Compare match B */
ISR( TIMER3_COMPC_vect )
{
if( timer3_compare_c_callback_) timer3_compare_c_callback_();
}
/* Timer 3 Overflow */
ISR( TIMER3_OVF_vect)
{
if( timer3_overflow_callback_) timer3_overflow_callback_();
}
bool timer3_init(clock_select16_t clock_select, t16_mode_t mode, t16_interrupt_t interrupts_mask)
{
timer3_mode_value = mode;
set_reg_16(ICR3, 65535);
timer3_set_compare(65535);
TCCR3A = (/*wgm11:10*/mode & 0x03);
TCCR3B = ((/*wgm13:12*/mode & 0x0C)<<1) | clock_select;
TIMSK3 = interrupts_mask & 0x2f; /* don't forget to set the "i" bit in the Status Register */
timer3_write( 0 );
return true;
}
void timer3_set_scale(clock_select16_t scale) {
TCCR3B = (TCCR3B & 0xf8) | scale;
}
uint16_t timer3_read()
{
return get_reg_16(TCNT3);
}
void timer3_write(uint16_t value)
{
set_reg_16(TCNT3, value);
}
void timer3_set_interrupts(t16_interrupt_t interrupts_mask)
{
TIMSK3 = (TIMSK3 & ~0x2f) | (interrupts_mask & 0x2f); /* don't forget to set the "i" bit in the Status Register */
}
void timer3_set_compare(uint16_t compare)
{
/* NOTE: the compare value is NOT used in normal or CTC modes */
set_reg_16(OCR3A, compare);
}
uint16_t timer3_get_compare()
{
/* NOTE: the compare value is NOT used in normal or CTC modes */
return get_reg_16(OCR3A);
}
void timer3_set_top(uint16_t top)
{
/* NOTE: the top value is NOT used in normal mode
* NOTE: only those modes specified in t16_mode_t are implemented
*/
set_reg_16(OCR3A, top);
}
uint16_t timer3_get_top()
{
/* NOTE: the top value is NOT used in normal mode
*/
return get_reg_16(OCR3A);
}
void timer3_set_compare_output_mode(compare_output_mode_t compare_output_mode)
{
TCCR3A = (TCCR3A & 0x3f) | ( compare_output_mode << 6 );
}
void timer3_set_capture_callback(timer_callback callback)
{
timer3_capture_callback_ = callback;
}
void timer3_set_compare_a_callback(timer_callback callback)
{
timer3_compare_a_callback_ = callback;
}
void timer3_set_compare_b_callback(timer_callback callback)
{
timer3_compare_b_callback_ = callback;
}
void timer3_set_overflow_callback(timer_callback callback){
timer3_overflow_callback_ = callback;
}
void timer3_stop(){
TCCR3B &= 0xf8;
}
#endif