Menuconfig Implementation and auto-qemu refactoring (#44)
[lunaix-os.git] / lunaix-os / hal / timer / timer_device.c
1 #include <lunaix/spike.h>
2 #include <lunaix/time.h>
3
4 #include <hal/hwtimer.h>
5
6 const struct hwtimer* systimer;
7
8 ticks_t
9 hwtimer_base_frequency()
10 {
11     assert(systimer);
12     return systimer->base_freq;
13 }
14
15 ticks_t
16 hwtimer_current_systicks()
17 {
18     assert(systimer);
19     return systimer->systicks();
20 }
21
22 ticks_t
23 hwtimer_to_ticks(u32_t value, int unit)
24 {
25     assert(systimer);
26     // in case system frequency is less than 1000Hz
27     if (unit != TIME_MS) {
28         return systimer->running_freq * unit * value;
29     }
30
31     ticks_t freq_ms = systimer->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 = select_platform_timer();
55
56     hwt_ctx->init(hwt_ctx, hertz, tick_callback);
57     hwt_ctx->running_freq = hertz;
58
59     systimer = hwt_ctx;
60
61     struct device* timerdev = device_allocsys(NULL, hwt_ctx);
62
63     timerdev->ops.exec_cmd = __hwtimer_ioctl;
64
65     register_device(timerdev, &hwt_ctx->class, hwt_ctx->name);
66 }