blob: 967a2fa770ac7ea33ab53a38120c92fd23bfede6 (
plain)
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
|
#include <timer.h>
#ifdef CONFIG_IOE_TIMERS
void timer_init(uint8_t timer, enum timerDivider div) {
switch (timer) {
#ifdef COUNTER0_PWM
case COUNTER0_PWM:
// Null counting register
TCNT0 = 0;
// Set interrupt flag
TIMSK0 = _BV(TOIE0);
// Set division and start counting
switch (div) {
case TIMER_DIVIDER_1:
TCCR0B = _BV(CS00);
break;
case TIMER_DIVIDER_8:
TCCR0B = _BV(CS01);
break;
case TIMER_DIVIDER_64:
TCCR0B = _BV(CS01) | _BV(CS00);
break;
case TIMER_DIVIDER_256:
TCCR0B = _BV(CS02);
break;
case TIMER_DIVIDER_1024:
TCCR0B = _BV(CS02) | _BV(CS01);
break;
default:
return;
}
break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
case COUNTER1_16PWM:
break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
case COUNTER2_PWM_ASYNC:
break;
#endif /* COUNTER2_PWM_ASYNC */
}
}
void timer_disable(uint8_t timer) {
switch (timer) {
#ifdef COUNTER0_PWM
case COUNTER0_PWM:
TCCR0B = 0;
break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
case COUNTER1_16PWM:
break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
case COUNTER2_PWM_ASYNC:
break;
#endif /* COUNTER2_PWM_ASYNC */
}
}
void timer_reset(uint8_t timer) {
switch (timer) {
#ifdef COUNTER0_PWM
case COUNTER0_PWM:
TCNT0 = 0;
break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
case COUNTER1_16PWM:
break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
case COUNTER2_PWM_ASYNC:
break;
#endif /* COUNTER2_PWM_ASYNC */
}
}
uint16_t timer_value(uint8_t timer) {
switch (timer) {
#ifdef COUNTER0_PWM
case COUNTER0_PWM:
return TCNT0;
break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
case COUNTER1_16PWM:
break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
case COUNTER2_PWM_ASYNC:
break;
#endif /* COUNTER2_PWM_ASYNC */
default:
return 0;
}
}
#ifdef COUNTER0_PWM
static void *(*timer_0_pwm_overflow) (void);
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
static void (*timer_1_16pwm_overflow) (void);
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
static void (*timer_2_pwm_overflow) (void);
#endif /* COUNTER2_PWM_ASYNC */
void timer_sethook(uint8_t timer, void (*fnc) (void)) {
switch (timer) {
#ifdef COUNTER0_PWM
case COUNTER0_PWM:
timer_0_pwm_overflow = fnc;
break;
#endif /* COUNTER0_PWM */
#ifdef COUNTER1_16PWM
case COUNTER1_16PWM:
timer_1_16pwm_overflow = fnc;
break;
#endif /* COUNTER1_16PWM */
#ifdef COUNTER2_PWM_ASYNC
case COUNTER2_PWM_ASYNC:
timer_2_pwm_overflow = fnc;
break;
#endif /* COUNTER2_PWM_ASYNC */
}
}
#ifdef COUNTER0_PWM
ISR(TIMER0_OVF_vect, ISR_BLOCK) {
if (timer_0_pwm_overflow)
timer_0_pwm_overflow();
}
#endif /* COUNTER0_PWM */
#ifdef COUNTER0_PWM
ISR(TIMER1_OVF_vect, ISR_BLOCK) {
if (timer_1_16pwm_overflow)
timer_1_16pwm_overflow();
}
#endif /* COUNTER0_PWM */
#ifdef COUNTER0_PWM
ISR(TIMER2_OVF_vect, ISR_BLOCK) {
if (timer_2_pwm_overflow)
timer_2_pwm_overflow();
}
#endif /* COUNTER0_PWM */
#endif /* CONFIG_IOE_TIMERS */
|