author | lbessard |
Mon, 24 Sep 2007 09:37:04 +0200 | |
changeset 280 | d9b232ec7057 |
parent 149 | fe50ada8020b |
child 401 | 2c90876b9751 |
permissions | -rw-r--r-- |
0 | 1 |
#include <stdlib.h> |
2 |
||
3 |
#include <sys/time.h> |
|
32 | 4 |
#include <pthread.h> |
0 | 5 |
#include <signal.h> |
6 |
||
7 |
#include "applicfg.h" |
|
8 |
#include "timer.h" |
|
9 |
||
10 |
pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
11 |
||
32 | 12 |
TASK_HANDLE TimerLoopThread; |
13 |
||
0 | 14 |
TIMEVAL last_time_set = TIMEVAL_MAX; |
15 |
||
16 |
struct timeval last_sig; |
|
17 |
||
32 | 18 |
timer_t timer; |
0 | 19 |
|
20 |
void EnterMutex(void) |
|
21 |
{ |
|
22 |
pthread_mutex_lock(&CanFestival_mutex); |
|
23 |
} |
|
24 |
||
25 |
void LeaveMutex(void) |
|
26 |
{ |
|
27 |
pthread_mutex_unlock(&CanFestival_mutex); |
|
28 |
} |
|
29 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
30 |
void timer_notify(sigval_t val) |
32 | 31 |
{ |
32 |
gettimeofday(&last_sig,NULL); |
|
33 |
EnterMutex(); |
|
34 |
TimeDispatch(); |
|
35 |
LeaveMutex(); |
|
36 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
37 |
} |
|
38 |
||
39 |
void initTimer(void) |
|
40 |
{ |
|
41 |
struct sigevent sigev; |
|
42 |
||
43 |
// Take first absolute time ref. |
|
44 |
gettimeofday(&last_sig,NULL); |
|
45 |
||
46 |
memset (&sigev, 0, sizeof (struct sigevent)); |
|
47 |
sigev.sigev_value.sival_int = 0; |
|
48 |
sigev.sigev_notify = SIGEV_THREAD; |
|
49 |
sigev.sigev_notify_attributes = NULL; |
|
50 |
sigev.sigev_notify_function = timer_notify; |
|
51 |
||
52 |
timer_create (CLOCK_REALTIME, &sigev, &timer); |
|
53 |
} |
|
54 |
||
55 |
void StopTimerLoop(void) |
|
56 |
{ |
|
149 | 57 |
EnterMutex(); |
32 | 58 |
timer_delete (timer); |
149 | 59 |
LeaveMutex(); |
32 | 60 |
} |
61 |
||
62 |
void StartTimerLoop(TimerCallback_t init_callback) |
|
0 | 63 |
{ |
64 |
initTimer(); |
|
149 | 65 |
EnterMutex(); |
0 | 66 |
// At first, TimeDispatch will call init_callback. |
67 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
|
149 | 68 |
LeaveMutex(); |
0 | 69 |
} |
70 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
71 |
void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
0 | 72 |
{ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
73 |
pthread_create(Thread, NULL, ReceiveLoopPtr, (void*)port); |
0 | 74 |
} |
75 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
76 |
void WaitReceiveTaskEnd(TASK_HANDLE Thread) |
0 | 77 |
{ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
78 |
pthread_kill(Thread, SIGTERM); |
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
79 |
pthread_join(Thread, NULL); |
0 | 80 |
} |
81 |
||
48
adc6572caf5d
minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents:
38
diff
changeset
|
82 |
#define maxval(a,b) ((a>b)?a:b) |
0 | 83 |
void setTimer(TIMEVAL value) |
84 |
{ |
|
85 |
// printf("setTimer(TIMEVAL value=%d)\n", value); |
|
32 | 86 |
// TIMEVAL is us whereas setitimer wants ns... |
38 | 87 |
long tv_nsec = 1000 * (maxval(value,1)%1000000); |
32 | 88 |
time_t tv_sec = value/1000000; |
89 |
struct itimerspec timerValues; |
|
90 |
timerValues.it_value.tv_sec = tv_sec; |
|
91 |
timerValues.it_value.tv_nsec = tv_nsec; |
|
0 | 92 |
timerValues.it_interval.tv_sec = 0; |
32 | 93 |
timerValues.it_interval.tv_nsec = 0; |
94 |
||
95 |
timer_settime (timer, 0, &timerValues, NULL); |
|
0 | 96 |
} |
97 |
||
98 |
TIMEVAL getElapsedTime(void) |
|
99 |
{ |
|
100 |
struct timeval p; |
|
101 |
gettimeofday(&p,NULL); |
|
102 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
103 |
return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec; |
|
104 |
} |