drivers/timers_unix/timers_unix.c
author dejoigny
Tue, 16 May 2006 17:21:14 +0200
changeset 7 126e37bcca0f
parent 0 4472ee7c6c3e
child 32 8afa33692372
permissions -rw-r--r--
ajout filtrage msgs
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
#include <sys/time.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
#include <signal.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
#include <pthread.h> 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
#include "applicfg.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
#include "can_driver.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
#include "timer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
TIMEVAL last_time_set = TIMEVAL_MAX;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
struct timeval last_sig;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
char stop_timer=0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
void sig(int val)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
	signal( SIGALRM, sig);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
	gettimeofday(&last_sig,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
void initTimer(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
	gettimeofday(&last_sig,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
	signal( SIGALRM, sig);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
	stop_timer = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
void stopTimer(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
	stop_timer = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
	kill(0, SIGALRM);	
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
void EnterMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
	pthread_mutex_lock(&CanFestival_mutex); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
void LeaveMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
	pthread_mutex_unlock(&CanFestival_mutex);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
void TimerLoop(TimerCallback_t init_callback)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
	initTimer();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
	// At first, TimeDispatch will call init_callback.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
	SetAlarm(NULL, 0, init_callback, 0, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
	while (!stop_timer) {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
		EnterMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
		TimeDispatch();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
		LeaveMutex();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
		pause();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
void ReceiveLoop(void* arg)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
	canReceiveLoop((CAN_HANDLE)arg);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
	pthread_create(Thread, NULL, (void *)&ReceiveLoop, (void*)fd0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
void WaitReceiveTaskEnd(TASK_HANDLE Thread)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
	pthread_join(Thread, NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
#define max(a,b) a>b?a:b
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
//	printf("setTimer(TIMEVAL value=%d)\n", value);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
	struct itimerval timerValues;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
	struct itimerval timerV = {{0,0},{0,0}};
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
	timerValues.it_value.tv_sec = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
	timerValues.it_value.tv_usec = max(value,1);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
	timerValues.it_interval.tv_sec = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
	timerValues.it_interval.tv_usec = 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
	setitimer(ITIMER_REAL, &timerValues, &timerV);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
	struct timeval p;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
	gettimeofday(&p,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
	return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
}