-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.c
262 lines (209 loc) · 6.83 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
/*
* Copyright 2013, 2014 Chris Rhodin <chris@notav8.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "project.h"
#include "timer.h"
#include <util/atomic.h>
#include <stdio.h>
/*
* system timebase
*/
static tbtick_t system_tick;
static struct timer_event * timer_event_list;
#if (TBTSIZE == 16) && (TBSIZE == 32)
tbtick_t timebase_update(void) __attribute__((__naked__));
tbtick_t timebase_update(void)
{
register tbtick_t tick __asm__("r22");
__asm__ __volatile__ (
" lds r22, %[tbtcntl] \n"
" lds r23, %[tbtcnth] \n"
" ldi r30, lo8(system_tick) \n"
" ldi r31, hi8(system_tick) \n"
" ldd r24, Z+2 \n"
" ldd r25, Z+3 \n"
" ld __tmp_reg__, Z \n"
" cp r22, __tmp_reg__ \n"
" ldd __tmp_reg__, Z+1 \n"
" cpc r23, __tmp_reg__ \n"
" adc r24, __zero_reg__ \n"
" adc r25, __zero_reg__ \n"
" std Z+0, r22 \n"
" std Z+1, r23 \n"
" std Z+2, r24 \n"
" std Z+3, r25 \n"
" \n"
" ret \n"
:
: /* i/o registers */
[tbtcntl] "i" _SFR_MEM_ADDR(TBTCNTL),
[tbtcnth] "i" _SFR_MEM_ADDR(TBTCNTH)
: "r0", "r22", "r23", "r24", "r25", "r30", "r31", "memory"
);
return tick;
}
#else
tbtick_t timebase_update(void)
{
return (system_tick += (tbtimer_t) (TBTCNT - system_tick));
}
#endif
static void link_timer_event(struct timer_event * this_timer_event)
{
struct timer_event ** tthis_timer_event;
for ( tthis_timer_event = &timer_event_list;
*tthis_timer_event != NULL;
tthis_timer_event = &((*tthis_timer_event)->next))
{
if ((tbtick_st)
(this_timer_event->tbtick - (*tthis_timer_event)->tbtick) < 0)
{
break;
}
}
this_timer_event->next = *tthis_timer_event;
*tthis_timer_event = this_timer_event;
}
static void unlink_timer_event(struct timer_event * this_timer_event)
{
struct timer_event ** tthis_timer_event;
for ( tthis_timer_event = &timer_event_list;
*tthis_timer_event != NULL;
tthis_timer_event = &((*tthis_timer_event)->next))
{
if (*tthis_timer_event == this_timer_event)
{
*tthis_timer_event = this_timer_event->next;
break;
}
}
this_timer_event->next = this_timer_event;
}
/*
* timebase interrupt handler
*/
static void tbtimer_handler(void)
{
for (;;)
{
tbtick_st delta;
tbtimer_t ocr;
if (timer_event_list == NULL)
{
/* no pending timer events */
delta = TBTIMER_MAX_DELAY;
}
else
{
/* process timer event */
delta = timer_event_list->tbtick - timebase_update();
if (delta < 0)
{
/* handle expired timer event */
struct timer_event * this_timer_event;
this_timer_event = timer_event_list;
timer_event_list = this_timer_event->next;
this_timer_event->next = this_timer_event;
if (this_timer_event->handler)
{
if (this_timer_event->handler(this_timer_event))
{
link_timer_event(this_timer_event);
}
}
continue;
}
if (delta > TBTIMER_MAX_DELAY)
{
/* limit delta to maximum supported by timer */
delta = TBTIMER_MAX_DELAY;
}
}
TBOCR = ocr = (tbtimer_t) timebase_update() + delta + 1;
if ((tbtimer_st) (TBTCNT - ocr) < 0)
{
break;
}
}
}
ISR(TBTIMER_COMP_vect)
{
tbtimer_handler();
}
tbtimer_t timebase_get(void)
{
tbtimer_t tbtcnt;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
tbtcnt = TBTCNT;
}
return tbtcnt;
}
void timebase_delay(tbtimer_st counts)
{
tbtimer_t terminal;
terminal = timebase_get() + counts;
while ((tbtimer_st) (terminal - timebase_get()) >= 0);
}
void schedule_timer_event(struct timer_event * this_timer_event,
struct timer_event * ref_timer_event)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
this_timer_event->tbtick += (ref_timer_event)
? (ref_timer_event->tbtick)
: (timebase_update());
link_timer_event(this_timer_event);
tbtimer_handler();
}
}
void cancel_timer_event(struct timer_event * this_timer_event)
{
if (!timer_is_expired(this_timer_event)) {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
unlink_timer_event(this_timer_event);
tbtimer_handler();
}
}
}
void timer_delay(tbtick_st ticks)
{
struct timer_event timer_delay_event;
init_timer_event(&timer_delay_event, ticks, NULL);
schedule_timer_event(&timer_delay_event, NULL);
while (!timer_is_expired(&timer_delay_event));
}
/*
* setup timer as a free running counter
*/
void timebase_init(void)
{
/*
* initialize timer
*/
/* setup the initial timer interrupt */
TBOCR = TBTIMER_MAX_DELAY;
/*
* initialize timebase
*/
system_tick = 0;
timer_event_list = NULL;
/* clear pending timer interrupts */
TBTIFR = _BV(TBOCF);
/* enable compare interrupt */
TBTIMSK |= _BV(TBOCIE);
}