drivers/timers_unix/timers_unix.c
changeset 32 8afa33692372
parent 0 4472ee7c6c3e
child 35 88812de1d7cc
equal deleted inserted replaced
31:a82b70738e5c 32:8afa33692372
     1 #include <stdlib.h>
     1 #include <stdlib.h>
     2 
     2 
     3 #include <sys/time.h>
     3 #include <sys/time.h>
       
     4 #include <pthread.h> 
     4 #include <signal.h>
     5 #include <signal.h>
     5 #include <pthread.h> 
       
     6 
     6 
     7 #include "applicfg.h"
     7 #include "applicfg.h"
     8 #include "can_driver.h"
     8 #include "can_driver.h"
     9 #include "timer.h"
     9 #include "timer.h"
    10 
    10 
    11 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
    11 pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
    12 
    12 
       
    13 TASK_HANDLE TimerLoopThread;
       
    14 
    13 TIMEVAL last_time_set = TIMEVAL_MAX;
    15 TIMEVAL last_time_set = TIMEVAL_MAX;
    14 
    16 
    15 struct timeval last_sig;
    17 struct timeval last_sig;
    16 
    18 
    17 char stop_timer=0;
    19 timer_t timer;
    18 
       
    19 void sig(int val)
       
    20 {
       
    21 	signal( SIGALRM, sig);
       
    22 	gettimeofday(&last_sig,NULL);
       
    23 //	printf("getCurrentTime() return=%u\n", p.tv_usec);
       
    24 }
       
    25 
       
    26 void initTimer(void)
       
    27 {
       
    28 	gettimeofday(&last_sig,NULL);
       
    29 	signal( SIGALRM, sig);
       
    30 	stop_timer = 0;
       
    31 }
       
    32 
       
    33 void stopTimer(void)
       
    34 {
       
    35 	stop_timer = 1;
       
    36 	kill(0, SIGALRM);	
       
    37 }
       
    38 
    20 
    39 void EnterMutex(void)
    21 void EnterMutex(void)
    40 {
    22 {
    41 	pthread_mutex_lock(&CanFestival_mutex); 
    23 	pthread_mutex_lock(&CanFestival_mutex); 
    42 }
    24 }
    44 void LeaveMutex(void)
    26 void LeaveMutex(void)
    45 {
    27 {
    46 	pthread_mutex_unlock(&CanFestival_mutex);
    28 	pthread_mutex_unlock(&CanFestival_mutex);
    47 }
    29 }
    48 
    30 
    49 void TimerLoop(TimerCallback_t init_callback)
    31 void timer_notify(int val)
       
    32 {
       
    33 	gettimeofday(&last_sig,NULL);
       
    34 	EnterMutex();
       
    35 	TimeDispatch();
       
    36 	LeaveMutex();
       
    37 //	printf("getCurrentTime() return=%u\n", p.tv_usec);
       
    38 }
       
    39 
       
    40 void initTimer(void)
       
    41 {
       
    42 	struct sigevent sigev;
       
    43 
       
    44 	// Take first absolute time ref.
       
    45 	gettimeofday(&last_sig,NULL);
       
    46 
       
    47 	memset (&sigev, 0, sizeof (struct sigevent));
       
    48 	sigev.sigev_value.sival_int = 0;
       
    49 	sigev.sigev_notify = SIGEV_THREAD;
       
    50 	sigev.sigev_notify_attributes = NULL;
       
    51 	sigev.sigev_notify_function = timer_notify;
       
    52 
       
    53 	timer_create (CLOCK_REALTIME, &sigev, &timer);
       
    54 }
       
    55 
       
    56 void StopTimerLoop(void)
       
    57 {
       
    58 	timer_delete (timer);
       
    59 }
       
    60 
       
    61 void StartTimerLoop(TimerCallback_t init_callback)
    50 {
    62 {
    51 	initTimer();
    63 	initTimer();
    52 	// At first, TimeDispatch will call init_callback.
    64 	// At first, TimeDispatch will call init_callback.
    53 	SetAlarm(NULL, 0, init_callback, 0, 0);
    65 	SetAlarm(NULL, 0, init_callback, 0, 0);
    54 	while (!stop_timer) {
       
    55 		EnterMutex();
       
    56 		TimeDispatch();
       
    57 		LeaveMutex();
       
    58 		pause();
       
    59 	}
       
    60 }
    66 }
    61 
    67 
    62 void ReceiveLoop(void* arg)
    68 void ReceiveLoop(void* arg)
    63 {
    69 {
    64 	canReceiveLoop((CAN_HANDLE)arg);
    70 	canReceiveLoop((CAN_HANDLE)arg);
    76 
    82 
    77 #define max(a,b) a>b?a:b
    83 #define max(a,b) a>b?a:b
    78 void setTimer(TIMEVAL value)
    84 void setTimer(TIMEVAL value)
    79 {
    85 {
    80 //	printf("setTimer(TIMEVAL value=%d)\n", value);
    86 //	printf("setTimer(TIMEVAL value=%d)\n", value);
    81 	struct itimerval timerValues;
    87 	// TIMEVAL is us whereas setitimer wants ns...
    82 	struct itimerval timerV = {{0,0},{0,0}};
    88 	long tv_nsec = 1000 * (max(value,1)%1000000);
    83 	timerValues.it_value.tv_sec = 0;
    89 	time_t tv_sec = value/1000000;
    84 	timerValues.it_value.tv_usec = max(value,1);
    90 	struct itimerspec timerValues;
       
    91 	timerValues.it_value.tv_sec = tv_sec;
       
    92 	timerValues.it_value.tv_nsec = tv_nsec;
    85 	timerValues.it_interval.tv_sec = 0;
    93 	timerValues.it_interval.tv_sec = 0;
    86 	timerValues.it_interval.tv_usec = 0;
    94 	timerValues.it_interval.tv_nsec = 0;
    87 	setitimer(ITIMER_REAL, &timerValues, &timerV);
    95 
       
    96  	timer_settime (timer, 0, &timerValues, NULL);
    88 }
    97 }
    89 
    98 
    90 TIMEVAL getElapsedTime(void)
    99 TIMEVAL getElapsedTime(void)
    91 {
   100 {
    92 	struct timeval p;
   101 	struct timeval p;