Merge branch 'master' into isa/arm64
[lunaix-os.git] / lunaix-os / includes / lunaix / timer.h
1 #ifndef __LUNAIX_TIMER_H
2 #define __LUNAIX_TIMER_H
3
4 #include <lunaix/ds/llist.h>
5 #include <lunaix/time.h>
6 #include <lunaix/hart_state.h>
7
8 #define SYS_TIMER_FREQUENCY_HZ 1000
9
10 #define TIMER_MODE_PERIODIC 0x1
11
12 struct lx_timer_context
13 {
14     struct lx_timer* active_timers;
15     /**
16      * @brief timer hardware base frequency (ticks per seconds)
17      *
18      */
19     ticks_t base_frequency;
20     /**
21      * @brief Desired system running frequency
22      *
23      */
24     ticks_t running_frequency;
25     /**
26      * @brief Ticks per hertz
27      *
28      */
29     ticks_t tphz;
30 };
31
32 struct timer_init_param
33 {
34     struct lx_timer_context* context;
35     void* timer_update_isr;
36 };
37
38 struct lx_timer
39 {
40     struct llist_header link;
41     ticks_t deadline;
42     ticks_t counter;
43     void* payload;
44     void (*callback)(void*);
45     u8_t flags;
46 };
47
48 /**
49  * @brief Initialize the system timer that runs at specified frequency
50  *
51  * @param frequency The frequency that timer should run in Hz.
52  */
53 void
54 timer_init();
55
56 struct lx_timer*
57 timer_run_second(u32_t second,
58                  void (*callback)(void*),
59                  void* payload,
60                  u8_t flags);
61
62 struct lx_timer*
63 timer_run_ms(u32_t millisecond,
64              void (*callback)(void*),
65              void* payload,
66              u8_t flags);
67
68 struct lx_timer*
69 timer_run(ticks_t ticks, void (*callback)(void*), void* payload, u8_t flags);
70
71 struct lx_timer_context*
72 timer_context();
73
74 #endif /* __LUNAIX_TIMER_H */