Xenomai API  2.5.6.1
include/nucleus/timebase.h
Go to the documentation of this file.
00001 
00023 #ifndef _XENO_NUCLEUS_TIMEBASE_H
00024 #define _XENO_NUCLEUS_TIMEBASE_H
00025 
00029 #include <nucleus/queue.h>
00030 
00031 #if defined(__KERNEL__) || defined(__XENO_SIM__)
00032 
00033 struct xntimer;
00034 
00035 typedef struct xntbops {
00036 
00037         int (*start_timer)(struct xntimer *timer,
00038                            xnticks_t value,
00039                            xnticks_t interval,
00040                            xntmode_t mode);
00041         void (*stop_timer)(struct xntimer *timer);
00042         xnticks_t (*get_timer_date)(struct xntimer *timer);
00043         xnticks_t (*get_timer_timeout)(struct xntimer *timer);
00044         xnticks_t (*get_timer_interval)(struct xntimer *timer);
00045         xnticks_t (*get_timer_raw_expiry)(struct xntimer *timer);
00046         void (*move_timer)(struct xntimer *timer);
00047 
00048 } xntbops_t;
00049 
00050 #define XNTBRUN  0x00000001     /* Time base is running. */
00051 #define XNTBSET  0x00000002     /* Time set in time base. */
00052 #define XNTBLCK  0x00000004     /* Time base is locked. */
00053 #define XNTBISO  0x00000008     /* Time base uses private wallclock offset */
00054 
00055 typedef struct xntbase {
00056 
00057         struct xntbops *ops;    
00059         xnticks_t jiffies;      
00061         void (*hook)(void);     
00063         xnticks_t wallclock_offset; 
00065         u_long tickvalue;       
00067         u_long ticks2sec;       
00069         u_long status;          
00071         const char *name;       /* !< Name of time base. */
00072 
00073         xnholder_t link;
00074 
00075 #define link2tbase(ln)          container_of(ln, xntbase_t, link)
00076 
00077 #ifdef CONFIG_XENO_OPT_STATS
00078         xnqueue_t timerq;       /* !< Timer holder in timebase. */
00079 
00080         int timerq_rev;         /* !< Revision (for non-atomic list walks). */
00081 #endif /* CONFIG_XENO_OPT_STATS */
00082 
00083 } xntbase_t;
00084 
00085 #ifdef __cplusplus
00086 extern "C" {
00087 #endif
00088 
00089 extern xntbase_t nktbase;
00090 
00091 extern xnqueue_t nktimebaseq;
00092 
00093 static inline u_long xntbase_get_ticks2sec(xntbase_t *base)
00094 {
00095         return base->ticks2sec;
00096 }
00097 
00098 static inline u_long xntbase_get_tickval(xntbase_t *base)
00099 {
00100         /* Returns the duration of a tick in nanoseconds */
00101         return base->tickvalue;
00102 }
00103 
00104 static inline xnticks_t xntbase_get_wallclock_offset(xntbase_t *base)
00105 {
00106         return base->wallclock_offset;
00107 }
00108 
00109 static inline void xntbase_set_hook(xntbase_t *base, void (*hook)(void))
00110 {
00111         base->hook = hook;
00112 }
00113 
00114 static inline int xntbase_timeset_p(xntbase_t *base)
00115 {
00116         return !!testbits(base->status, XNTBSET);
00117 }
00118 
00119 static inline int xntbase_enabled_p(xntbase_t *base)
00120 {
00121         return !!testbits(base->status, XNTBRUN);
00122 }
00123 
00124 static inline int xntbase_isolated_p(xntbase_t *base)
00125 {
00126         return !!testbits(base->status, XNTBISO);
00127 }
00128 
00129 static inline const char *xntbase_name(xntbase_t *base)
00130 {
00131         return base->name;
00132 }
00133 
00134 #ifdef CONFIG_XENO_OPT_TIMING_PERIODIC
00135 
00136 static inline xntime_t xntbase_ticks2ns(xntbase_t *base, xnticks_t ticks)
00137 {
00138         /* Convert a count of ticks in nanoseconds */
00139         return ticks * xntbase_get_tickval(base);
00140 }
00141 
00142 static inline xnticks_t xntbase_ns2ticks(xntbase_t *base, xntime_t t)
00143 {
00144         return xnarch_ulldiv(t, xntbase_get_tickval(base), NULL);
00145 }
00146 
00147 static inline int xntbase_master_p(xntbase_t *base)
00148 {
00149         return base == &nktbase;
00150 }
00151 
00152 static inline int xntbase_periodic_p(xntbase_t *base)
00153 {
00154         return !xntbase_master_p(base);
00155 }
00156 
00157 static inline xnticks_t xntbase_get_jiffies(xntbase_t *base)
00158 {
00159         return xntbase_periodic_p(base) ? base->jiffies : xnarch_get_cpu_time();
00160 }
00161 
00162 static inline xnticks_t xntbase_get_rawclock(xntbase_t *base)
00163 {
00164         return xntbase_periodic_p(base) ? base->jiffies : xnarch_get_cpu_tsc();
00165 }
00166 
00167 int xntbase_alloc(const char *name,
00168                   u_long period,
00169                   u_long flags,
00170                   xntbase_t **basep);
00171 
00172 void xntbase_free(xntbase_t *base);
00173 
00174 int xntbase_update(xntbase_t *base,
00175                    u_long period);
00176 
00177 int xntbase_switch(const char *name,
00178                    u_long period,
00179                    xntbase_t **basep);
00180 
00181 void xntbase_start(xntbase_t *base);
00182 
00183 void xntbase_stop(xntbase_t *base);
00184 
00185 void xntbase_tick(xntbase_t *base);
00186 
00187 xnticks_t xntbase_ns2ticks_ceil(xntbase_t *base, xntime_t t);
00188 
00189 xnticks_t xntbase_convert(xntbase_t *srcbase,
00190                           xnticks_t ticks,
00191                           xntbase_t *dstbase);
00192 
00193 #else /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
00194 
00195 void xntimer_tick_aperiodic(void);
00196 
00197 static inline xntime_t xntbase_ticks2ns(xntbase_t *base, xnticks_t ticks)
00198 {
00199         return ticks;
00200 }
00201 
00202 static inline xnticks_t xntbase_ns2ticks(xntbase_t *base, xntime_t t)
00203 {
00204         return t;
00205 }
00206 
00207 static inline xnticks_t xntbase_ns2ticks_ceil(xntbase_t *base, xntime_t t)
00208 {
00209         return t;
00210 }
00211 
00212 static inline int xntbase_master_p(xntbase_t *base)
00213 {
00214         return 1;
00215 }
00216 
00217 static inline xnticks_t xntbase_convert(xntbase_t *srcbase, xnticks_t ticks, xntbase_t *dstbase)
00218 {
00219         return ticks;
00220 }
00221 
00222 static inline int xntbase_periodic_p(xntbase_t *base)
00223 {
00224         return 0;
00225 }
00226 
00227 static inline xnticks_t xntbase_get_jiffies(xntbase_t *base)
00228 {
00229         return xnarch_get_cpu_time();
00230 }
00231 
00232 static inline xnticks_t xntbase_get_rawclock(xntbase_t *base)
00233 {
00234         return xnarch_get_cpu_tsc();
00235 }
00236 
00237 static inline int xntbase_alloc(const char *name, u_long period, u_long flags, xntbase_t **basep)
00238 {
00239         *basep = &nktbase;
00240         return 0;
00241 }
00242 
00243 static inline void xntbase_free(xntbase_t *base)
00244 {
00245 }
00246 
00247 static inline int xntbase_update(xntbase_t *base, u_long period)
00248 {
00249         return 0;
00250 }
00251 
00252 static inline int xntbase_switch(const char *name, u_long period, xntbase_t **basep)
00253 {
00254         return period == XN_APERIODIC_TICK ? 0 : -ENODEV;
00255 }
00256 
00257 static inline void xntbase_start(xntbase_t *base)
00258 {
00259 }
00260 
00261 static inline void xntbase_stop(xntbase_t *base)
00262 {
00263 }
00264 
00265 static inline void xntbase_tick(xntbase_t *base)
00266 {
00267         xntimer_tick_aperiodic();
00268 }
00269 
00270 #endif /* !CONFIG_XENO_OPT_TIMING_PERIODIC */
00271 
00299 static inline xnticks_t xntbase_get_time(xntbase_t *base)
00300 {
00301         /* Return an adjusted value of the monotonic time with the
00302            translated system wallclock offset. */
00303         return xntbase_get_jiffies(base) + base->wallclock_offset;
00304 }
00305 
00306 void xntbase_adjust_time(xntbase_t *base, xnsticks_t delta);
00307 
00308 #ifdef __cplusplus
00309 }
00310 #endif
00311 
00312 #define xntbase_mount() \
00313 do {                                            \
00314         inith(&nktbase.link);                   \
00315         appendq(&nktimebaseq, &nktbase.link);   \
00316         xntbase_declare_proc(&nktbase); \
00317 } while (0)
00318 
00319 #define xntbase_umount()                        \
00320 do {                                            \
00321         xntbase_remove_proc(&nktbase);          \
00322         removeq(&nktimebaseq, &nktbase.link);   \
00323 } while (0)
00324 
00325 #endif /* __KERNEL__ || __XENO_SIM__ */
00326 
00327 void xntbase_init_proc(void);
00328 
00329 void xntbase_cleanup_proc(void);
00330 
00331 #ifdef CONFIG_XENO_OPT_STATS
00332 void xntbase_declare_proc(xntbase_t *base);
00333 void xntbase_remove_proc(xntbase_t *base);
00334 #else /* !CONFIG_XENO_OPT_STATS */
00335 static inline void xntbase_declare_proc(xntbase_t *base) { }
00336 static inline void xntbase_remove_proc(xntbase_t *base) { }
00337 #endif /* !CONFIG_XENO_OPT_STATS */
00338 
00341 #endif /* !_XENO_NUCLEUS_TIMEBASE_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines