-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep.c
47 lines (38 loc) · 667 Bytes
/
sleep.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
#include <avr/interrupt.h>
#include "fiber.h"
struct timer {
struct fiber *fiber;
unsigned int ticks;
};
volatile struct timer timers[8] = { 0 };
ISR(TIMER0_OVF_vect)
{
// sei();
for (int i = 0; i < 8; i++) {
if (!timers[i].fiber)
continue;
if (--timers[i].ticks == 0) {
wake(timers[i].fiber);
timers[i].fiber = 0;
}
}
}
__attribute__((constructor))
static void _setup_timer0()
{
TIMSK = (1<<TOIE0);
TCNT0 = 0x00;
TCCR0 = (1<<CS01) | (1<<CS00);
sei();
}
void sleep(unsigned int ticks)
{
for (int i = 0; i < 8; i++) {
if (timers[i].fiber)
continue;
timers[i].fiber = current();
timers[i].ticks = ticks;
break;
}
yield();
}