3a79ca1a7ec6d773521e8f8b7ff321b04526d406
[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     return current_timer->base_freq;
12 }
13
14 ticks_t
15 hwtimer_current_systicks()
16 {
17     return current_timer->systicks();
18 }
19
20 ticks_t
21 hwtimer_to_ticks(u32_t value, int unit)
22 {
23     // in case system frequency is less than 1000Hz
24     if (unit != TIME_MS) {
25         return current_timer->running_freq * unit * value;
26     }
27
28     ticks_t freq_ms = current_timer->running_freq / 1000;
29
30     return freq_ms * value;
31 }
32
33 static int
34 __hwtimer_ioctl(struct device* dev, u32_t req, va_list args)
35 {
36     struct hwtimer* hwt = (struct hwtimer*)dev->underlay;
37     switch (req) {
38         case TIMERIO_GETINFO:
39             // TODO
40             break;
41
42         default:
43             break;
44     }
45     return 0;
46 }
47
48 void
49 hwtimer_init(u32_t hertz, void* tick_callback)
50 {
51     struct hwtimer* hwt_ctx = hwtimer_choose();
52
53     hwt_ctx->init(hwt_ctx, hertz, tick_callback);
54     hwt_ctx->running_freq = hertz;
55
56     current_timer = hwt_ctx;
57
58     struct device* timerdev = device_addsys(NULL, hwt_ctx, hwt_ctx->name);
59
60     timerdev->ops.exec_cmd = __hwtimer_ioctl;
61 }