timer_gettick(): Rename to timer_tick() and document better.
[bertos.git] / drv / timer.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Hardware independent timer driver (interface)
14  */
15
16 /*
17  * $Log$
18  * Revision 1.8  2004/07/18 21:57:32  bernie
19  * timer_gettick(): Rename to timer_tick() and document better.
20  *
21  * Revision 1.7  2004/06/27 15:26:17  aleph
22  * Declaration fix for build with GCC 3.4
23  *
24  * Revision 1.6  2004/06/07 18:10:06  aleph
25  * Remove free pool of timers; use user-provided Timer structure instead
26  *
27  * Revision 1.5  2004/06/07 15:57:12  aleph
28  * Add function prototypes
29  *
30  * Revision 1.4  2004/06/06 18:25:44  bernie
31  * Rename event macros to look like regular functions.
32  *
33  * Revision 1.3  2004/06/06 16:57:18  bernie
34  * Mark some functions INLINE instead of 'extern inline'.
35  *
36  * Revision 1.2  2004/06/03 11:27:09  bernie
37  * Add dual-license information.
38  *
39  * Revision 1.1  2004/05/23 18:23:30  bernie
40  * Import drv/timer module.
41  *
42  */
43 #ifndef DRV_TIMER_H
44 #define DRV_TIMER_H
45
46 #include "cpu.h"
47 #include "compiler.h"
48 #include <mware/list.h>
49
50 #ifdef CONFIG_KERNEL
51         #include <kern/event.h>
52 #else
53         #include <mware/event.h>
54 #endif
55
56 /*! Number of timer ticks per second. */
57 #define TICKS_PER_SEC       1000
58
59 typedef struct Timer
60 {
61         Node   link;      /*!< Link into timers queue */
62         time_t delay;     /*!< Timer delay in ms */
63         time_t tick;      /*!< Timer will expire at this tick */
64         Event  expire;    /*!< Event to execute when the timer expires */
65 } Timer;
66
67 /* Function protos */
68 extern void timer_init(void);
69 extern void timer_add(Timer *timer);
70 extern Timer *timer_abort(Timer *timer);
71 extern void timer_delay(time_t time);
72 extern void timer_udelay(utime_t utime);
73 INLINE time_t timer_tick(void);
74 INLINE time_t timer_tick_unlocked(void);
75 INLINE void timer_set_event_softint(Timer* timer, Hook func, void* user_data);
76 INLINE void timer_set_delay(Timer* timer, time_t delay);
77
78 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
79
80 /*! Set the timer so that it sends a signal when it expires */
81 INLINE void timer_set_event_signal(Timer* timer, struct Process* proc, sigset_t sigs);
82 INLINE void timer_set_event_signal(Timer* timer, struct Process* proc, sigset_t sigs)
83 {
84         event_initSignal(&timer->expire, proc, sigs);
85 }
86
87 #endif /* CONFIG_KERN_SIGNALS */
88
89 /*! Set the timer so that it calls an user hook when it expires */
90 INLINE void timer_set_event_softint(Timer* timer, Hook func, void* user_data)
91 {
92         event_initSoftInt(&timer->expire, func, user_data);
93 }
94
95 /*! Set the timer delay (the time before the event will be triggered) */
96 INLINE void timer_set_delay(Timer* timer, time_t delay)
97 {
98         timer->delay = delay;
99 }
100
101
102 extern volatile time_t _clock;
103
104 /*!
105  * \brief Return the system tick counter (expressed in ms)
106  *
107  * The result is guaranteed to increment monotonically,
108  * but client code must be tolerant with respect to overflows.
109  *
110  * The following code is safe:
111  *
112  * \example
113  *   time_t tea_start_time = get_tick();
114  *
115  *   boil_water();
116  *
117  *   if (get_tick() - tea_start_time > TEAPOT_DELAY)
118  *       printf("Your tea, Sir.\n");
119  * \endexample
120  *
121  * When the tick counter increments every millisecond and time_t
122  * is 32bit wide, the tick count will overflow every 49.7 days.
123  *
124  * \note This function must disable interrupts on 8/16bit CPUs because the
125  * clock variable is larger than the processor word size and can't
126  * be copied atomically.
127  */
128 INLINE time_t timer_tick(void)
129 {
130         time_t result;
131         cpuflags_t flags;
132
133         DISABLE_IRQSAVE(flags);
134         result = _clock;
135         ENABLE_IRQRESTORE(flags);
136
137         return result;
138 }
139
140 /* OBSOLETE */
141 #define timer_gettick timer_tick
142
143
144 /*!
145  * Like \c timer_tick, faster version to be called
146  * from interrupt context only.
147  *
148  * \sa timer_tick
149  */
150 INLINE time_t timer_tick_unlocked(void)
151 {
152         return _clock;
153 }
154
155 /* OBSOLETE */
156 #define timer_gettick_irq timer_tick_unlocked
157
158 #endif /* DRV_TIMER_H */
159