timer.h

00001 /* timer.h
00002    Copyright 2000 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@worldnet.fr)
00004 
00005 This file is part of GEL.
00006 
00007 GEL is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2, or (at your option)
00010 any later version.
00011 
00012 GEL is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with GEL; see the file COPYING.  If not, write to
00019 the Free Software Foundation, 59 Temple Place - Suite 330,
00020 Boston, MA 02111-1307, USA.  */
00021 
00022 #ifndef _GEL_TIMER_H
00023 #define _GEL_TIMER_H
00024 
00029 
00030 #define TIMER_TCNT_CLOCK_UNIT (2)
00031 
00032 #ifndef TIMER_TCNT_DIV
00033 # define TIMER_TCNT_DIV (16)
00034 #endif
00035 
00036 #if 0
00037 #define TIMER_TCNT_CLOCK_PERIOD  \
00038 (((TIMER_TCNT_DIV * 1000000L) * TIMER_TCNT_CLOCK_UNIT) / M6811_CPU_E_CLOCK)
00039 #endif
00040 
00041 #define TIMER_TCNT_PERIOD \
00042 ((TIMER_TCNT_CLOCK_PERIOD * 65536) / TIMER_TCNT_CLOCK_UNIT)
00043 
00044 #define TIMER_TCNT_CLOCK_PERIOD 1
00045 
00046 struct timeval
00047 {
00048   long tv_sec;
00049   long tv_usec;
00050 };
00051 
00052 #define ULONG_MAX (0xffffffffUL)
00053 struct timer;
00054 
00056 typedef void (* timer_overflow_handler) (void);
00057 
00058 typedef void (* timer_handler) (struct timer *t);
00059 
00060 #define TIMER_MASK   0x00
00061 #define TIMER_UNMASK 0x01
00062 #define TIMER_SLOW   0x02
00063 #define TIMER_OVERFLOW_INTERRUPT 0x04
00064 #define TIMER_HEAD   0x80
00065 
00073 struct timer 
00074 {
00075   struct timer   *next;
00076   struct timer   *prev;
00077   unsigned short flags;
00078   long           timeout;
00079   timer_handler  handler;
00080   void           *data;
00081 };
00082 
00083 #define TIMER_INIT_STATIC(FUNC,FLAGS) { 0, 0, FLAGS, 0, FUNC, 0 }
00084 
00099 extern void timer_initialize (void);
00100 
00139 extern void timer_create (struct timer *t,
00140                           unsigned long timeout,
00141                           timer_handler handler,
00142                           unsigned char flags);
00143 
00156 extern void timer_insert (struct timer *t);
00157 
00167 extern void timer_remove (struct timer *t);
00168 
00176 extern int timer_is_active (struct timer *t);
00177 
00195 extern void timer_gettime (struct timeval *tv);
00196 
00206 extern void timer_settime (struct timeval *tv);
00207 
00216 extern long timer_adjtime (long adj_usec);
00217 
00226 extern void timevalsub (struct timeval *to, struct timeval *val);
00227 
00235 extern void timevaladd (struct timeval *to, struct timeval *val);
00236 
00237 extern unsigned long timer_current_overflow (void);
00238 extern unsigned long usec_to_tcnt (unsigned long);
00239 extern unsigned long usec_to_overflow (unsigned long);
00240 extern unsigned long tcnt_to_usec (unsigned short);
00241 extern unsigned long tovf_to_usec (unsigned long);
00242 extern unsigned long tovf_to_sec (unsigned long);
00243 
00247 extern void __attribute__((interrupt)) timer_overflow_interrupt (void);
00248 
00256 extern void __attribute__((interrupt)) timer_interrupt (void);
00257 
00259 static void set_timer_overflow_handler (timer_overflow_handler handler);
00260 
00261 static inline void
00262 set_timer_overflow_handler (timer_overflow_handler handler)
00263 {
00264   extern timer_overflow_handler _overflow_handler;
00265 
00266   _overflow_handler = handler;
00267 }
00268 
00269 extern inline unsigned long
00270 timer_current_overflow (void)
00271 {
00272   extern unsigned long _timer_current_overflow;
00273 
00274   return _timer_current_overflow;
00275 }
00276 
00279 extern inline unsigned long
00280 usec_to_tcnt (unsigned long us)
00281 {
00282   us = us / TIMER_TCNT_CLOCK_PERIOD;
00283   return us * TIMER_TCNT_CLOCK_UNIT;
00284 }
00285 
00286 extern inline unsigned long
00287 usec_to_overflow (unsigned long us)
00288 {
00289   return us / TIMER_TCNT_PERIOD;
00290 }
00291 
00292 extern inline unsigned long
00293 tcnt_to_usec (unsigned short tcnt)
00294 {
00295   unsigned long usec;
00296 
00297   usec = ((unsigned long) (tcnt) * TIMER_TCNT_CLOCK_PERIOD);
00298   usec = usec / TIMER_TCNT_CLOCK_UNIT;
00299   return usec;
00300 }
00301 
00302 extern inline unsigned long
00303 tovf_to_usec (unsigned long overflow)
00304 {
00305   unsigned long usec;
00306 
00307   usec = overflow * TIMER_TCNT_PERIOD;
00308   usec = usec % 1000000UL;
00309   return usec;
00310 }
00311 
00312 extern inline unsigned long
00313 tovf_to_sec (unsigned long overflow)
00314 {
00315   unsigned long sec;
00316 
00317   sec = overflow * TIMER_TCNT_PERIOD;
00318   return sec;
00319 }
00320 
00321 
00322 extern inline int
00323 timer_is_active (struct timer *t)
00324 {
00325   return t->next != 0;
00326 }
00327 
00330 #endif