-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
64 lines (57 loc) · 1.37 KB
/
timer.c
File metadata and controls
64 lines (57 loc) · 1.37 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
64
#include "timer.h"
#include "uart.h"
static timer_t sw_timer[MAX_NUM_TIMER];
void init_timer(void)
{
for (int i = 0; i < MAX_NUM_TIMER; i++) {
sw_timer[i].handler = NULL;
sw_timer[i].args = NULL;
sw_timer[i].timeout_tick = 0;
}
}
timer_t *timer_create(
void (*handler)(void *args),
void *args,
uint32_t timeout_tick)
{
uint32_t _tick = get_timer_tick();
timer_t *timer = &(sw_timer[0]);
/* allocate if available */
int i;
for (i = 0; i < MAX_NUM_TIMER; i++) {
if (timer->handler == NULL) {
break;
}
timer++;
}
if (i >= MAX_NUM_TIMER) {
uart_puts("Failed to create timer.\n");
return NULL;
}
/* timer setting */
timer->handler = handler;
timer->args = args;
timer->timeout_tick = _tick + timeout_tick;
return timer;
}
void timer_delete(timer_t *timer)
{
if (!timer) {
uart_puts("timer_delete: NULL ptr\n");
return ;
}
for (int i = 0; i < MAX_NUM_TIMER; i++) {
if (&(sw_timer[i]) == timer) {
printf("deleted %d-th software timer\n", i);
/* DO NOT write to pointer without checking */
timer->handler = NULL;
timer->args = NULL;
timer->timeout_tick = 0;
break;
}
}
}
timer_t* get_timer_list()
{
return &(sw_timer[0]);
}