4da156895fb3de77384edb9e0ac5dc5a75f058e5
[lunaix-os.git] / lunaix-os / hal / timer / hwtimer.c
1 #include <hal/hwtimer.h>
2 #include <lunaix/spike.h>
3
4 #include <usr/lunaix/ioctl_defs.h>
5
6 struct hwtimer* current_timer;
7
8 ticks_t
9 hwtimer_base_frequency()
10 {
11     assert(current_timer);
12     return current_timer->base_freq;
13 }
14
15 ticks_t
16 hwtimer_current_systicks()
17 {
18     assert(current_timer);
19     return current_timer->systicks();
20 }
21
22 ticks_t
23 hwtimer_to_ticks(u32_t value, int unit)
24 {
25     assert(current_timer);
26     // in case system frequency is less than 1000Hz
27     if (unit != TIME_MS) {
28         return current_timer->running_freq * unit * value;
29     }
30
31     ticks_t freq_ms = current_timer->running_freq / 1000;
32
33     return freq_ms * value;
34 }
35
36 static int
37 __hwtimer_ioctl(struct device* dev, u32_t req, va_list args)
38 {
39     struct hwtimer* hwt = (struct hwtimer*)dev->underlay;
40     switch (req) {
41         case TIMERIO_GETINFO:
42             // TODO
43             break;
44
45         default:
46             break;
47     }
48     return 0;
49 }
50
51 void
52 hwtimer_init(u32_t hertz, void* tick_callback)
53 {
54     struct hwtimer* hwt_ctx = hwtimer_choose();
55
56     hwt_ctx->init(hwt_ctx, hertz, tick_callback);
57     hwt_ctx->running_freq = hertz;
58
59     current_timer = hwt_ctx;
60
61     struct device* timerdev =
62       device_addsys(NULL, &hwt_ctx->class, hwt_ctx, hwt_ctx->name);
63
64     timerdev->ops.exec_cmd = __hwtimer_ioctl;
65 }