1 #include <lunaix/spike.h>
2 #include <lunaix/time.h>
4 #include <hal/hwtimer.h>
6 const struct hwtimer_pot* systimer = NULL;
8 static struct device_alias* timer_alias;
9 static DEFINE_LLIST(timer_devices);
12 hwtimer_current_systicks()
15 return systimer->systick_raw;
19 hwtimer_to_ticks(u32_t value, int unit)
23 // in case system frequency is less than 1000Hz
24 if (unit != TIME_MS) {
25 return systimer->running_freq * unit * value;
28 ticks_t freq_ms = systimer->running_freq / 1000;
30 return freq_ms * value;
33 static struct hwtimer_pot*
36 struct hwtimer_pot *pos, *n, *sel = NULL;
38 llist_for_each(pos, n, &timer_devices, timers)
45 if (sel->precedence < pos->precedence) {
54 hwtimer_init(u32_t hertz, void* tick_callback)
56 struct hwtimer_pot* selected;
57 struct device* time_dev;
59 selected = __select_timer();
64 selected->callback = tick_callback;
65 selected->running_freq = hertz;
67 selected->ops->calibrate(selected, hertz);
69 time_dev = potens_dev(selected);
70 timer_alias = device_addalias(NULL, dev_meta(time_dev), "timer");
75 hwtimer_attach_potens(struct device* dev, int precedence,
76 struct hwtimer_pot_ops* ops)
78 struct hwtimer_pot* hwpot;
79 struct potens_meta* pot;
81 if (!potens_check_unique(dev, potens(HWTIMER)))
86 hwpot = new_potens(potens(HWTIMER), struct hwtimer_pot);
88 hwpot->precedence = precedence;
90 device_grant_potens(dev, potens_meta(hwpot));
92 llist_append(&timer_devices, &hwpot->timers);