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