00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _GEL_TIMER_H
00023 #define _GEL_TIMER_H
00024
00025 #include <sys/param.h>
00026
00031
00032 #ifndef GEL_PAGE0_ATTRIBUTE
00033 # define GEL_PAGE0_ATTRIBUTE
00034 #endif
00035
00036 #define TIMER_TCNT_CLOCK_UNIT (2)
00037
00038 #ifndef TIMER_TCNT_DIV
00039 # define TIMER_TCNT_DIV (16)
00040 #endif
00041
00042 #if 0
00043 #define TIMER_TCNT_CLOCK_PERIOD \
00044 (((TIMER_TCNT_DIV * 1000000L) * TIMER_TCNT_CLOCK_UNIT) / M6811_CPU_E_CLOCK)
00045 #endif
00046
00047 #define TIMER_TCNT_PERIOD \
00048 ((TIMER_TCNT_CLOCK_PERIOD * 65536) / TIMER_TCNT_CLOCK_UNIT)
00049
00050 #define TIMER_TCNT_CLOCK_PERIOD 1
00051
00052 struct timeval
00053 {
00054 long tv_sec;
00055 long tv_usec;
00056 };
00057
00058 #define ULONG_MAX (0xffffffffUL)
00059 struct timer;
00060
00062 typedef void (* timer_overflow_handler) (void);
00063
00064 typedef void (* timer_handler) (struct timer *t);
00065
00066 #define TIMER_MASK 0x00
00067 #define TIMER_UNMASK 0x01
00068 #define TIMER_SLOW 0x02
00069 #define TIMER_OVERFLOW_INTERRUPT 0x04
00070 #define TIMER_HEAD 0x80
00071
00079 struct timer
00080 {
00081 struct timer *next;
00082 struct timer *prev;
00083 unsigned short flags;
00084 long timeout;
00085 timer_handler handler;
00086 void *data;
00087 };
00088
00089 #define TIMER_INIT_STATIC(FUNC,FLAGS) { 0, 0, FLAGS, 0, FUNC, 0 }
00090
00105 extern void timer_initialize (void);
00106
00145 extern void timer_create (struct timer *t,
00146 unsigned long timeout,
00147 timer_handler handler,
00148 unsigned char flags);
00149
00162 extern void timer_insert (struct timer *t);
00163
00173 extern void timer_remove (struct timer *t);
00174
00182 extern int timer_is_active (struct timer *t);
00183
00201 extern void timer_gettime (struct timeval *tv);
00202
00212 extern void timer_settime (struct timeval *tv);
00213
00222 extern long timer_adjtime (long adj_usec);
00223
00232 extern void timevalsub (struct timeval *to, struct timeval *val);
00233
00241 extern void timevaladd (struct timeval *to, struct timeval *val);
00242
00243 extern unsigned long timer_current_overflow (void);
00244 extern unsigned long usec_to_tcnt (unsigned long);
00245 extern unsigned long usec_to_overflow (unsigned long);
00246 extern unsigned long tcnt_to_usec (unsigned short);
00247 extern unsigned long tovf_to_usec (unsigned long);
00248 extern unsigned long tovf_to_sec (unsigned long);
00249
00253 extern void __attribute__((interrupt)) timer_overflow_interrupt (void);
00254
00262 extern void __attribute__((interrupt)) timer_interrupt (void);
00263
00265 static void set_timer_overflow_handler (timer_overflow_handler handler);
00266
00267 static inline void
00268 set_timer_overflow_handler (timer_overflow_handler handler)
00269 {
00270 extern timer_overflow_handler _overflow_handler;
00271
00272 _overflow_handler = handler;
00273 }
00274
00275 extern inline unsigned long
00276 timer_current_overflow (void)
00277 {
00278 extern unsigned long _timer_current_overflow;
00279
00280 return _timer_current_overflow;
00281 }
00282
00285 extern inline unsigned long
00286 usec_to_tcnt (unsigned long us)
00287 {
00288 us = us / TIMER_TCNT_CLOCK_PERIOD;
00289 return us * TIMER_TCNT_CLOCK_UNIT;
00290 }
00291
00292 extern inline unsigned long
00293 usec_to_overflow (unsigned long us)
00294 {
00295 return us / TIMER_TCNT_PERIOD;
00296 }
00297
00298 extern inline unsigned long
00299 tcnt_to_usec (unsigned short tcnt)
00300 {
00301 unsigned long usec;
00302
00303 usec = ((unsigned long) (tcnt) * TIMER_TCNT_CLOCK_PERIOD);
00304 usec = usec / TIMER_TCNT_CLOCK_UNIT;
00305 return usec;
00306 }
00307
00308 extern inline unsigned long
00309 tovf_to_usec (unsigned long overflow)
00310 {
00311 unsigned long usec;
00312
00313 usec = overflow * TIMER_TCNT_PERIOD;
00314 usec = usec % 1000000UL;
00315 return usec;
00316 }
00317
00318 extern inline unsigned long
00319 tovf_to_sec (unsigned long overflow)
00320 {
00321 unsigned long sec;
00322
00323 sec = overflow * TIMER_TCNT_PERIOD;
00324 return sec;
00325 }
00326
00327
00328 extern inline int
00329 timer_is_active (struct timer *t)
00330 {
00331 return t->next != 0;
00332 }
00333
00336 #endif