Declaration fix for build with GCC 3.4
[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.7  2004/06/27 15:26:17  aleph
19  * Declaration fix for build with GCC 3.4
20  *
21  * Revision 1.6  2004/06/07 18:10:06  aleph
22  * Remove free pool of timers; use user-provided Timer structure instead
23  *
24  * Revision 1.5  2004/06/07 15:57:12  aleph
25  * Add function prototypes
26  *
27  * Revision 1.4  2004/06/06 18:25:44  bernie
28  * Rename event macros to look like regular functions.
29  *
30  * Revision 1.3  2004/06/06 16:57:18  bernie
31  * Mark some functions INLINE instead of 'extern inline'.
32  *
33  * Revision 1.2  2004/06/03 11:27:09  bernie
34  * Add dual-license information.
35  *
36  * Revision 1.1  2004/05/23 18:23:30  bernie
37  * Import drv/timer module.
38  *
39  */
40 #ifndef DRV_TIMER_H
41 #define DRV_TIMER_H
42
43 #include "cpu.h"
44 #include "compiler.h"
45 #include <mware/list.h>
46 #include <kern/event.h>
47
48 /*! Number of timer ticks per second. */
49 #define TICKS_PER_SEC       1000
50
51 typedef struct Timer
52 {
53         Node   link;      /*!< Link into timers queue */
54         time_t delay;     /*!< Timer delay in ms */
55         time_t tick;      /*!< Timer will expire at this tick */
56         Event  expire;    /*!< Event to execute when the timer expires */
57 } Timer;
58
59 /* Function protos */
60 extern void timer_init(void);
61 extern void timer_add(Timer *timer);
62 extern Timer *timer_abort(Timer *timer);
63 extern void timer_delay(time_t time);
64 extern void timer_udelay(utime_t utime);
65 INLINE time_t timer_gettick(void);
66 INLINE time_t timer_gettick_irq(void);
67 INLINE void timer_set_event_softint(Timer* timer, Hook func, void* user_data);
68 INLINE void timer_set_delay(Timer* timer, time_t delay);
69
70 #if defined(CONFIG_KERN_SIGNALS) && CONFIG_KERN_SIGNALS
71
72 /*! Set the timer so that it sends a signal when it expires */
73 INLINE void timer_set_event_signal(Timer* timer, struct Process* proc, sigset_t sigs);
74 INLINE void timer_set_event_signal(Timer* timer, struct Process* proc, sigset_t sigs)
75 {
76         event_initSignal(&timer->expire, proc, sigs);
77 }
78
79 #endif /* CONFIG_KERN_SIGNALS */
80
81 /*! Set the timer so that it calls an user hook when it expires */
82 INLINE void timer_set_event_softint(Timer* timer, Hook func, void* user_data)
83 {
84         event_initSoftInt(&timer->expire, func, user_data);
85 }
86
87 /*! Set the timer delay (the time before the event will be triggered) */
88 INLINE void timer_set_delay(Timer* timer, time_t delay)
89 {
90         timer->delay = delay;
91 }
92
93
94 extern volatile time_t _clock;
95
96 /*!
97  * Return the system tick counter (expressed in ms)
98  * This function must disable interrupts on 8/16bit CPUs because the
99  * clock variable is larger than the processor word size and can't
100  * be copied atomically.
101  */
102 INLINE time_t timer_gettick(void)
103 {
104         time_t result;
105         cpuflags_t flags;
106
107         DISABLE_IRQSAVE(flags);
108         result = _clock;
109         ENABLE_IRQRESTORE(flags);
110
111         return result;
112 }
113
114
115 /*!
116  * Like \c timer_gettick, faster version to be called
117  * from interrupt context only.
118  */
119 INLINE time_t timer_gettick_irq(void)
120 {
121         return _clock;
122 }
123
124 #endif /* DRV_TIMER_H */
125