9c864c7bdd3f29eb03d6926d3ed387ec93921125
[lunaix-os.git] / lunaix-os / hal / timer / hwtimer.c
1 #include <hal/hwtimer.h>
2 #include <lunaix/spike.h>
3
4 struct hwtimer_context* sys_hwtctx;
5
6 void
7 hwtimer_init(u32_t hertz, void* tick_callback)
8 {
9     struct hwtimer_context* hwt_ctx = hwtimer_choose();
10
11     hwt_ctx->init(hwt_ctx, hertz, tick_callback);
12     hwt_ctx->running_freq = hertz;
13
14     sys_hwtctx = hwt_ctx;
15 }
16
17 ticks_t
18 hwtimer_base_frequency()
19 {
20     return sys_hwtctx->base_freq;
21 }
22
23 ticks_t
24 hwtimer_current_systicks()
25 {
26     return sys_hwtctx->systicks();
27 }
28
29 ticks_t
30 hwtimer_to_ticks(u32_t value, int unit)
31 {
32     // in case system frequency is less than 1000Hz
33     if (unit != TIME_MS) {
34         return sys_hwtctx->running_freq * unit * value;
35     }
36
37     ticks_t freq_ms = sys_hwtctx->running_freq / 1000;
38
39     return freq_ms * value;
40 }