Xenomai API  2.6.4
timebase.h
Go to the documentation of this file.
1 
23 #ifndef _XENO_NUCLEUS_TIMEBASE_H
24 #define _XENO_NUCLEUS_TIMEBASE_H
25 
29 #include <nucleus/queue.h>
30 
31 #if defined(__KERNEL__) || defined(__XENO_SIM__)
32 
33 #include <nucleus/vfile.h>
34 
35 struct xntimer;
36 
37 typedef struct xntbops {
38 
39  int (*start_timer)(struct xntimer *timer,
40  xnticks_t value,
41  xnticks_t interval,
42  xntmode_t mode);
43  void (*stop_timer)(struct xntimer *timer);
44  xnticks_t (*get_timer_date)(struct xntimer *timer);
45  xnticks_t (*get_timer_timeout)(struct xntimer *timer);
46  xnticks_t (*get_timer_interval)(struct xntimer *timer);
47  xnticks_t (*get_timer_raw_expiry)(struct xntimer *timer);
48  void (*move_timer)(struct xntimer *timer);
49 
50 } xntbops_t;
51 
52 #define XNTBRUN 0x00000001 /* Time base is running. */
53 #define XNTBSET 0x00000002 /* Time set in time base. */
54 #define XNTBLCK 0x00000004 /* Time base is locked. */
55 #define XNTBISO 0x00000008 /* Time base uses private wallclock offset */
56 
57 typedef struct xntbase {
58 
59  struct xntbops *ops;
61  xnticks_t jiffies;
63  void (*hook)(void);
65  xnticks_t wallclock_offset;
67  u_long tickvalue;
69  u_long ticks2sec;
71  u_long status;
73  const char *name; /* !< Name of time base. */
74 
75  xnholder_t link;
76 
77 #define link2tbase(ln) container_of(ln, xntbase_t, link)
78 
79 #ifdef CONFIG_XENO_OPT_STATS
80  struct xnvfile_snapshot vfile; /* !< Virtual file for access. */
81  struct xnvfile_rev_tag revtag; /* !< Revision (for non-atomic list walks). */
82  struct xnqueue timerq; /* !< Timer holder in timebase. */
83 #endif /* CONFIG_XENO_OPT_STATS */
84 
85 } xntbase_t;
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 extern xntbase_t nktbase;
92 
93 extern xnqueue_t nktimebaseq;
94 
95 static inline u_long xntbase_get_ticks2sec(xntbase_t *base)
96 {
97  return base->ticks2sec;
98 }
99 
100 static inline u_long xntbase_get_tickval(xntbase_t *base)
101 {
102  /* Returns the duration of a tick in nanoseconds */
103  return base->tickvalue;
104 }
105 
106 static inline xnticks_t xntbase_get_wallclock_offset(xntbase_t *base)
107 {
108  return base->wallclock_offset;
109 }
110 
111 static inline void xntbase_set_hook(xntbase_t *base, void (*hook)(void))
112 {
113  base->hook = hook;
114 }
115 
116 static inline int xntbase_timeset_p(xntbase_t *base)
117 {
118  return !!testbits(base->status, XNTBSET);
119 }
120 
121 static inline int xntbase_enabled_p(xntbase_t *base)
122 {
123  return !!testbits(base->status, XNTBRUN);
124 }
125 
126 static inline int xntbase_isolated_p(xntbase_t *base)
127 {
128  return !!testbits(base->status, XNTBISO);
129 }
130 
131 static inline const char *xntbase_name(xntbase_t *base)
132 {
133  return base->name;
134 }
135 
136 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
137 
138 static inline xntime_t xntbase_ticks2ns(xntbase_t *base, xnticks_t ticks)
139 {
140  /* Convert a count of ticks in nanoseconds */
141  return ticks * xntbase_get_tickval(base);
142 }
143 
144 static inline xnticks_t xntbase_ns2ticks(xntbase_t *base, xntime_t t)
145 {
146  return xnarch_ulldiv(t, xntbase_get_tickval(base), NULL);
147 }
148 
149 static inline int xntbase_master_p(xntbase_t *base)
150 {
151  return base == &nktbase;
152 }
153 
154 static inline int xntbase_periodic_p(xntbase_t *base)
155 {
156  return !xntbase_master_p(base);
157 }
158 
159 static inline xnticks_t xntbase_get_jiffies(xntbase_t *base)
160 {
161  return xntbase_periodic_p(base) ? base->jiffies : xnarch_get_cpu_time();
162 }
163 
164 static inline xnticks_t xntbase_get_rawclock(xntbase_t *base)
165 {
166  return xntbase_periodic_p(base) ? base->jiffies : xnarch_get_cpu_tsc();
167 }
168 
169 int xntbase_alloc(const char *name,
170  u_long period,
171  u_long flags,
172  xntbase_t **basep);
173 
174 void xntbase_free(xntbase_t *base);
175 
176 int xntbase_update(xntbase_t *base,
177  u_long period);
178 
179 int xntbase_switch(const char *name,
180  u_long period,
181  xntbase_t **basep);
182 
183 void xntbase_start(xntbase_t *base);
184 
185 void xntbase_stop(xntbase_t *base);
186 
187 void xntbase_tick(xntbase_t *base);
188 
189 xnticks_t xntbase_ns2ticks_ceil(xntbase_t *base, xntime_t t);
190 
191 xnticks_t xntbase_convert(xntbase_t *srcbase,
192  xnticks_t ticks,
193  xntbase_t *dstbase);
194 
195 #else /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
196 
197 void xntimer_tick_aperiodic(void);
198 
199 static inline xntime_t xntbase_ticks2ns(xntbase_t *base, xnticks_t ticks)
200 {
201  return ticks;
202 }
203 
204 static inline xnticks_t xntbase_ns2ticks(xntbase_t *base, xntime_t t)
205 {
206  return t;
207 }
208 
209 static inline xnticks_t xntbase_ns2ticks_ceil(xntbase_t *base, xntime_t t)
210 {
211  return t;
212 }
213 
214 static inline int xntbase_master_p(xntbase_t *base)
215 {
216  return 1;
217 }
218 
219 static inline xnticks_t xntbase_convert(xntbase_t *srcbase, xnticks_t ticks, xntbase_t *dstbase)
220 {
221  return ticks;
222 }
223 
224 static inline int xntbase_periodic_p(xntbase_t *base)
225 {
226  return 0;
227 }
228 
229 static inline xnticks_t xntbase_get_jiffies(xntbase_t *base)
230 {
231  return xnarch_get_cpu_time();
232 }
233 
234 static inline xnticks_t xntbase_get_rawclock(xntbase_t *base)
235 {
236  return xnarch_get_cpu_tsc();
237 }
238 
239 static inline int xntbase_alloc(const char *name, u_long period, u_long flags, xntbase_t **basep)
240 {
241  *basep = &nktbase;
242  return 0;
243 }
244 
245 static inline void xntbase_free(xntbase_t *base)
246 {
247 }
248 
249 static inline int xntbase_update(xntbase_t *base, u_long period)
250 {
251  return 0;
252 }
253 
254 static inline int xntbase_switch(const char *name, u_long period, xntbase_t **basep)
255 {
256  return period == XN_APERIODIC_TICK ? 0 : -ENODEV;
257 }
258 
259 static inline void xntbase_start(xntbase_t *base)
260 {
261 }
262 
263 static inline void xntbase_stop(xntbase_t *base)
264 {
265 }
266 
267 static inline void xntbase_tick(xntbase_t *base)
268 {
270 }
271 
272 #endif /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
273 
301 static inline xnticks_t xntbase_get_time(xntbase_t *base)
302 {
303  /* Return an adjusted value of the monotonic time with the
304  translated system wallclock offset. */
305  return xntbase_get_jiffies(base) + base->wallclock_offset;
306 }
307 
308 void xntbase_adjust_time(xntbase_t *base, xnsticks_t delta);
309 
310 #ifdef __cplusplus
311 }
312 #endif
313 
314 #define xntbase_mount() \
315 do { \
316  inith(&nktbase.link); \
317  appendq(&nktimebaseq, &nktbase.link); \
318  xntbase_declare_proc(&nktbase); \
319 } while (0)
320 
321 #define xntbase_umount() \
322 do { \
323  xntbase_remove_proc(&nktbase); \
324  removeq(&nktimebaseq, &nktbase.link); \
325 } while (0)
326 
327 void xntbase_init_proc(void);
328 
329 void xntbase_cleanup_proc(void);
330 
331 #ifdef CONFIG_XENO_OPT_STATS
332 void xntbase_declare_proc(xntbase_t *base);
333 void xntbase_remove_proc(xntbase_t *base);
334 #else /* !CONFIG_XENO_OPT_STATS */
335 static inline void xntbase_declare_proc(xntbase_t *base) { }
336 static inline void xntbase_remove_proc(xntbase_t *base) { }
337 #endif /* !CONFIG_XENO_OPT_STATS */
338 
339 extern struct xnvfile_rev_tag tbaselist_tag;
340 
341 #endif /* __KERNEL__ || __XENO_SIM__ */
342 
345 #endif /* !_XENO_NUCLEUS_TIMEBASE_H */