My Kernel v0.1.0
timer.h
Go to the documentation of this file.
1
17#ifndef KERNEL_DEVICES_TIMER_H
18#define KERNEL_DEVICES_TIMER_H
19
20#include <kernel/time.h>
21#include <kernel/types.h>
22
24#define SEC_TO_TICKS(_time) SEC((_time) * TICKS_PER_SECOND)
25#define MS_TO_TICKS(_time) MS_TO_SEC(SEC_TO_TICKS(_time))
26#define US_TO_TICKS(_time) US_TO_SEC(SEC_TO_TICKS(_time))
27#define NS_TO_TICKS(_time) NS_TO_SEC(SEC_TO_TICKS(_time))
31#define TICKS_TO_SEC(_ticks) SEC((_ticks) / TICKS_PER_SECOND)
32#define TICKS_TO_MS(_ticks) MS(TICKS_TO_SEC(_ticks))
33#define TICKS_TO_US(_ticks) US(TICKS_TO_SEC(_ticks))
34#define TICKS_TO_NS(_ticks) NS(TICKS_TO_SEC(_ticks))
43extern volatile clock_t timer_ticks_counter;
44
48extern volatile u32 timer_kernel_frequency;
49
54void timer_start(u32 frequency);
55
57static inline clock_t timer_gettick(void)
58{
60}
61
66static inline bool timer_tick(void)
67{
68 clock_t old_ticks = timer_ticks_counter;
70 return old_ticks > timer_ticks_counter;
71}
72
74static inline time_t timer_get_ms(void)
75{
76 return TICKS_TO_MS(timer_gettick());
77}
78
80static inline time_t timer_get_us(void)
81{
82 return TICKS_TO_US(timer_gettick());
83}
84
86static inline time_t timer_get_ns(void)
87{
88 return TICKS_TO_NS(timer_gettick());
89}
90
96static inline void clock_get_time(struct timespec *time)
97{
98 time_t ns = timer_get_ns();
99
100 time->tv_sec = NS_TO_SEC(ns);
101 time->tv_nsec = ns % NS(1);
102}
103
108void timer_wait_ms(time_t);
109
110/*
111 * Wait for a certain amount of time.
112 * Can be used in a non-preemptible context.
113 *
114 * TODO: Make it safe to call in an un-interruptible
115 * context by computing the timing of a for loop
116 * and using this instead.
117 */
118void timer_delay_ms(time_t);
119
120#endif /* KERNEL_DEVICES_TIMER_H */
void timer_wait_ms(time_t)
Wait a certain amount of miliseconds.
Definition: timer.c:12
#define TICKS_TO_MS(_ticks)
Convert a number of ticks into a regular time unit.
Definition: timer.h:32
static time_t timer_get_ms(void)
Definition: timer.h:74
volatile clock_t timer_ticks_counter
This is where we keep track of the number of intervals reported by the timer.
Definition: timer.c:27
void timer_start(u32 frequency)
Start the global timekeeping timer.
Definition: timer.c:39
static clock_t timer_gettick(void)
Definition: timer.h:57
static time_t timer_get_ns(void)
Definition: timer.h:86
static time_t timer_get_us(void)
Definition: timer.h:80
#define TICKS_TO_NS(_ticks)
Convert a number of ticks into a regular time unit.
Definition: timer.h:34
volatile u32 timer_kernel_frequency
Frequency of the global timekeeping timer.
Definition: timer.c:28
#define TICKS_TO_US(_ticks)
Convert a number of ticks into a regular time unit.
Definition: timer.h:33
static void clock_get_time(struct timespec *time)
Fill a timespec structure with the current time of the day.
Definition: timer.h:96
static bool timer_tick(void)
Increment the timekeeping timer's tick count.
Definition: timer.h:66
#define NS(_s)
Used for conversions from seconds to another time unit.
Definition: time.h:24
#define NS_TO_SEC(_s)
Used for conversions to seconds from another time unit.
Definition: time.h:31