-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
63 lines (53 loc) · 1.26 KB
/
timer.c
File metadata and controls
63 lines (53 loc) · 1.26 KB
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
#include "timer.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "vale_os.h"
#include "list.h"
#include "tcb.h"
#include "uart.h"
#ifdef F_CPU
#define AVR_CPU_HZ F_CPU
#else
#define AVR_CPU_HZ 16000000
#endif
extern list_t ready_list;
extern list_t sleep_list;
void timer_start(void)
{
/* Set timer 1 compare match value for configured system tick,
* with a prescaler of 256. We will get a compare match 1A
* interrupt on every system tick, in which we must call the
* OS's system tick handler. */
OCR1A = (AVR_CPU_HZ / 256 / SYSTEM_TICKS_PER_SEC);
/* Enable compare match 1A interrupt */
#ifdef TIMSK
TIMSK = _BV(OCIE5A);
#else
TIMSK1 = _BV(OCIE1A);
#endif
/* Set prescaler 256 */
TCCR1B = _BV(CS12) | _BV(WGM12);
}
int current_time = 0;
int get_current_time(void)
{
return current_time;
}
void increment_current_time(void)
{
current_time++;
}
void _remove_if_sleep_until_is_passed(list_node_t *node)
{
tcb_t *tcb = (tcb_t *)node->data;
if (tcb->sleep_until <= current_time)
{
tcb->status = THREAD_STATUS_READY;
list_remove(&sleep_list, node);
list_enqueue(&ready_list, node);
}
}
void awake_sleeping_threads(void)
{
list_foreach(&sleep_list, _remove_if_sleep_until_is_passed);
}