nico@215: nico@215: nico@215: CanFestival: drivers/timers_unix/timers_unix.c Source File nico@215: nico@215: nico@215: nico@215: nico@215:
nico@215:
nico@215:
nico@215:
nico@215: nico@215:

timers_unix.c

Go to the documentation of this file.
00001 #include <stdlib.h>
nico@215: 00002 
nico@215: 00003 #include <sys/time.h>
nico@215: 00004 #include <pthread.h> 
nico@215: 00005 #include <signal.h>
nico@215: 00006 
nico@215: 00007 #include "applicfg.h"
nico@215: 00008 #include "timer.h"
nico@215: 00009 
nico@215: 00010 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
nico@215: 00011 
nico@215: 00012 TASK_HANDLE TimerLoopThread;
nico@215: 00013 
nico@215: 00014 TIMEVAL last_time_set = TIMEVAL_MAX;
nico@215: 00015 
nico@215: 00016 struct timeval last_sig;
nico@215: 00017 
nico@215: 00018 timer_t timer;
nico@215: 00019 
nico@215: 00020 void EnterMutex(void)
nico@215: 00021 {
nico@215: 00022         pthread_mutex_lock(&CanFestival_mutex); 
nico@215: 00023 }
nico@215: 00024 
nico@215: 00025 void LeaveMutex(void)
nico@215: 00026 {
nico@215: 00027         pthread_mutex_unlock(&CanFestival_mutex);
nico@215: 00028 }
nico@215: 00029 
nico@215: 00030 void timer_notify(sigval_t val)
nico@215: 00031 {
nico@215: 00032         gettimeofday(&last_sig,NULL);
nico@215: 00033         EnterMutex();
nico@215: 00034         TimeDispatch();
nico@215: 00035         LeaveMutex();
nico@215: 00036 //      printf("getCurrentTime() return=%u\n", p.tv_usec);
nico@215: 00037 }
nico@215: 00038 
nico@215: 00039 void initTimer(void)
nico@215: 00040 {
nico@215: 00041         struct sigevent sigev;
nico@215: 00042 
nico@215: 00043         // Take first absolute time ref.
nico@215: 00044         gettimeofday(&last_sig,NULL);
nico@215: 00045 
nico@215: 00046         memset (&sigev, 0, sizeof (struct sigevent));
nico@215: 00047         sigev.sigev_value.sival_int = 0;
nico@215: 00048         sigev.sigev_notify = SIGEV_THREAD;
nico@215: 00049         sigev.sigev_notify_attributes = NULL;
nico@215: 00050         sigev.sigev_notify_function = timer_notify;
nico@215: 00051 
nico@215: 00052         timer_create (CLOCK_REALTIME, &sigev, &timer);
nico@215: 00053 }
nico@215: 00054 
nico@215: 00055 void StopTimerLoop(void)
nico@215: 00056 {
nico@215: 00057         EnterMutex();
nico@215: 00058         timer_delete (timer);
nico@215: 00059         LeaveMutex();
nico@215: 00060 }
nico@215: 00061 
nico@215: 00062 void StartTimerLoop(TimerCallback_t init_callback)
nico@215: 00063 {
nico@215: 00064         initTimer();
nico@215: 00065         EnterMutex();
nico@215: 00066         // At first, TimeDispatch will call init_callback.
nico@215: 00067         SetAlarm(NULL, 0, init_callback, 0, 0);
nico@215: 00068         LeaveMutex();
nico@215: 00069 }
nico@215: 00070 
nico@215: 00071 void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
nico@215: 00072 {
nico@215: 00073         pthread_create(Thread, NULL, ReceiveLoopPtr, (void*)port);
nico@215: 00074 }
nico@215: 00075 
nico@215: 00076 void WaitReceiveTaskEnd(TASK_HANDLE Thread)
nico@215: 00077 {
nico@215: 00078         pthread_kill(Thread, SIGTERM);
nico@215: 00079         pthread_join(Thread, NULL);
nico@215: 00080 }
nico@215: 00081 
nico@215: 00082 #define maxval(a,b) ((a>b)?a:b)
nico@215: 00083 void setTimer(TIMEVAL value)
nico@215: 00084 {
nico@215: 00085 //      printf("setTimer(TIMEVAL value=%d)\n", value);
nico@215: 00086         // TIMEVAL is us whereas setitimer wants ns...
nico@215: 00087         long tv_nsec = 1000 * (maxval(value,1)%1000000);
nico@215: 00088         time_t tv_sec = value/1000000;
nico@215: 00089         struct itimerspec timerValues;
nico@215: 00090         timerValues.it_value.tv_sec = tv_sec;
nico@215: 00091         timerValues.it_value.tv_nsec = tv_nsec;
nico@215: 00092         timerValues.it_interval.tv_sec = 0;
nico@215: 00093         timerValues.it_interval.tv_nsec = 0;
nico@215: 00094 
nico@215: 00095         timer_settime (timer, 0, &timerValues, NULL);
nico@215: 00096 }
nico@215: 00097 
nico@215: 00098 TIMEVAL getElapsedTime(void)
nico@215: 00099 {
nico@215: 00100         struct timeval p;
nico@215: 00101         gettimeofday(&p,NULL);
nico@215: 00102 //      printf("getCurrentTime() return=%u\n", p.tv_usec);
nico@215: 00103         return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;
nico@215: 00104 }
nico@215: 

Generated on Fri Jun 8 08:51:38 2007 for CanFestival by  nico@215: nico@215: doxygen 1.5.1
nico@215: nico@215: