drivers/timers_unix/timers_unix.c
author leonid
Wed, 14 Feb 2007 22:19:53 +0100
changeset 95 e696976fcbe3
parent 48 adc6572caf5d
child 145 e747d2e26af0
permissions -rw-r--r--
file CAN-uVCCM.cpp was initially added on branch BRANCH_WIN32PORT.
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>
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
     4
#include <pthread.h> 
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
#include <signal.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
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    13
TASK_HANDLE TimerLoopThread;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    14
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
TIMEVAL last_time_set = TIMEVAL_MAX;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
struct timeval last_sig;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    19
timer_t timer;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
void EnterMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
	pthread_mutex_lock(&CanFestival_mutex); 
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 LeaveMutex(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
	pthread_mutex_unlock(&CanFestival_mutex);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    31
void timer_notify(int val)
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    32
{
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    33
	gettimeofday(&last_sig,NULL);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    34
	EnterMutex();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    35
	TimeDispatch();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    36
	LeaveMutex();
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    37
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    38
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    39
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    40
void initTimer(void)
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    41
{
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    42
	struct sigevent sigev;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    43
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    44
	// Take first absolute time ref.
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    45
	gettimeofday(&last_sig,NULL);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    46
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    47
	memset (&sigev, 0, sizeof (struct sigevent));
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    48
	sigev.sigev_value.sival_int = 0;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    49
	sigev.sigev_notify = SIGEV_THREAD;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    50
	sigev.sigev_notify_attributes = NULL;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    51
	sigev.sigev_notify_function = timer_notify;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    52
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    53
	timer_create (CLOCK_REALTIME, &sigev, &timer);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    54
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    55
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    56
void StopTimerLoop(void)
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    57
{
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    58
	timer_delete (timer);
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    59
}
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    60
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    61
void StartTimerLoop(TimerCallback_t init_callback)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
	initTimer();
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
	// At first, TimeDispatch will call init_callback.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
	SetAlarm(NULL, 0, init_callback, 0, 0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
void ReceiveLoop(void* arg)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
	canReceiveLoop((CAN_HANDLE)arg);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
	pthread_create(Thread, NULL, (void *)&ReceiveLoop, (void*)fd0);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 32
diff changeset
    78
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
{
35
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 32
diff changeset
    80
	pthread_kill(*Thread, SIGTERM);
88812de1d7cc Bug on receiveLoopTask termination corrected
lbessard
parents: 32
diff changeset
    81
	pthread_join(*Thread, NULL);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
48
adc6572caf5d minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents: 38
diff changeset
    84
#define maxval(a,b) ((a>b)?a:b)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
void setTimer(TIMEVAL value)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
//	printf("setTimer(TIMEVAL value=%d)\n", value);
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    88
	// TIMEVAL is us whereas setitimer wants ns...
38
9b5bb1dcb4f5 Cygwin port. Still untested. Compiles and link.
etisserant
parents: 35
diff changeset
    89
	long tv_nsec = 1000 * (maxval(value,1)%1000000);
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    90
	time_t tv_sec = value/1000000;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    91
	struct itimerspec timerValues;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    92
	timerValues.it_value.tv_sec = tv_sec;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    93
	timerValues.it_value.tv_nsec = tv_nsec;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
	timerValues.it_interval.tv_sec = 0;
32
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    95
	timerValues.it_interval.tv_nsec = 0;
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    96
8afa33692372 SDO callbacks.
etisserant
parents: 0
diff changeset
    97
 	timer_settime (timer, 0, &timerValues, NULL);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
TIMEVAL getElapsedTime(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
	struct timeval p;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
	gettimeofday(&p,NULL);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
//	printf("getCurrentTime() return=%u\n", p.tv_usec);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
	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
   106
}