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 HZ CLOCK_PER_SECOND
25
27#define SEC_TO_TICKS(_time) SEC((_time) * HZ)
28#define MS_TO_TICKS(_time) MS_TO_SEC(SEC_TO_TICKS(_time))
29#define US_TO_TICKS(_time) US_TO_SEC(SEC_TO_TICKS(_time))
30#define NS_TO_TICKS(_time) NS_TO_SEC(SEC_TO_TICKS(_time))
34#define TICKS_TO_SEC(_ticks) SEC((_ticks) / HZ)
35#define TICKS_TO_MS(_ticks) MS(TICKS_TO_SEC(_ticks))
36#define TICKS_TO_US(_ticks) US(TICKS_TO_SEC(_ticks))
37#define TICKS_TO_NS(_ticks) NS(TICKS_TO_SEC(_ticks))
46extern volatile clock_t timer_ticks_counter;
47
51extern volatile u32 timer_kernel_frequency;
52
57void timer_start(u32 frequency);
58
60static inline clock_t timer_gettick(void)
61{
63}
64
69static inline bool timer_tick(void)
70{
71 clock_t old_ticks = timer_ticks_counter;
73 return old_ticks > timer_ticks_counter;
74}
75
77static inline time_t timer_get_ms(void)
78{
79 return TICKS_TO_MS(timer_gettick());
80}
81
83static inline time_t timer_get_us(void)
84{
85 return TICKS_TO_US(timer_gettick());
86}
87
89static inline time_t timer_get_ns(void)
90{
91 return TICKS_TO_NS(timer_gettick());
92}
93
99static inline void clock_get_time(struct timespec *time)
100{
101 time_t ns = timer_get_ns();
102
103 time->tv_sec = NS_TO_SEC(ns);
104 time->tv_nsec = ns % NS(1);
105}
106
111void timer_wait_ms(time_t);
112
113/*
114 * Wait for a certain amount of time.
115 * Can be used in a non-preemptible context.
116 *
117 * TODO: Make it safe to call in an un-interruptible
118 * context by computing the timing of a for loop
119 * and using this instead.
120 */
121void timer_delay_ms(time_t);
122
123#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:35
static time_t timer_get_ms(void)
Definition: timer.h:77
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:60
static time_t timer_get_ns(void)
Definition: timer.h:89
static time_t timer_get_us(void)
Definition: timer.h:83
#define TICKS_TO_NS(_ticks)
Convert a number of ticks into a regular time unit.
Definition: timer.h:37
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:36
static void clock_get_time(struct timespec *time)
Fill a timespec structure with the current time of the day.
Definition: timer.h:99
static bool timer_tick(void)
Increment the timekeeping timer's tick count.
Definition: timer.h:69
#define NS(_s)
Used for conversions from seconds to another time unit.
Definition: time.h:18
#define NS_TO_SEC(_s)
Used for conversions to seconds from another time unit.
Definition: time.h:25