X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/8c06c883e7b13c115d5ff207f79d4b68fccd5ad6..2236410f4582ab45ae8c384dd6eeeef5d10aab15:/lunaix-os/hal/timer/timer_device.c diff --git a/lunaix-os/hal/timer/timer_device.c b/lunaix-os/hal/timer/timer_device.c new file mode 100644 index 0000000..c5341be --- /dev/null +++ b/lunaix-os/hal/timer/timer_device.c @@ -0,0 +1,66 @@ +#include +#include + +#include + +const struct hwtimer* systimer; + +ticks_t +hwtimer_base_frequency() +{ + assert(systimer); + return systimer->base_freq; +} + +ticks_t +hwtimer_current_systicks() +{ + assert(systimer); + return systimer->systicks(); +} + +ticks_t +hwtimer_to_ticks(u32_t value, int unit) +{ + assert(systimer); + // in case system frequency is less than 1000Hz + if (unit != TIME_MS) { + return systimer->running_freq * unit * value; + } + + ticks_t freq_ms = systimer->running_freq / 1000; + + return freq_ms * value; +} + +static int +__hwtimer_ioctl(struct device* dev, u32_t req, va_list args) +{ + struct hwtimer* hwt = (struct hwtimer*)dev->underlay; + switch (req) { + case TIMERIO_GETINFO: + // TODO + break; + + default: + break; + } + return 0; +} + +void +hwtimer_init(u32_t hertz, void* tick_callback) +{ + struct hwtimer* hwt_ctx = hwtimer_choose(); + + hwt_ctx->init(hwt_ctx, hertz, tick_callback); + hwt_ctx->running_freq = hertz; + + systimer = hwt_ctx; + + struct device* timerdev = device_allocsys(NULL, hwt_ctx); + + timerdev->ops.exec_cmd = __hwtimer_ioctl; + + device_register(timerdev, &hwt_ctx->class, hwt_ctx->name); +} \ No newline at end of file