author | etisserant |
Wed, 04 Jun 2008 10:57:06 +0200 | |
changeset 469 | f2b07ea215b5 |
parent 454 | bc000083297a |
child 507 | c613e6cd34fa |
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 |
|
454 | 20 |
void TimerCleanup(void) |
21 |
{ |
|
22 |
/* only used in realtime apps */ |
|
23 |
} |
|
24 |
||
0 | 25 |
void EnterMutex(void) |
26 |
{ |
|
27 |
pthread_mutex_lock(&CanFestival_mutex); |
|
28 |
} |
|
29 |
||
30 |
void LeaveMutex(void) |
|
31 |
{ |
|
32 |
pthread_mutex_unlock(&CanFestival_mutex); |
|
33 |
} |
|
34 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
35 |
void timer_notify(sigval_t val) |
32 | 36 |
{ |
37 |
gettimeofday(&last_sig,NULL); |
|
38 |
EnterMutex(); |
|
39 |
TimeDispatch(); |
|
40 |
LeaveMutex(); |
|
41 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
42 |
} |
|
43 |
||
454 | 44 |
void TimerInit(void) |
32 | 45 |
{ |
46 |
struct sigevent sigev; |
|
47 |
||
48 |
// Take first absolute time ref. |
|
49 |
gettimeofday(&last_sig,NULL); |
|
50 |
||
51 |
memset (&sigev, 0, sizeof (struct sigevent)); |
|
52 |
sigev.sigev_value.sival_int = 0; |
|
53 |
sigev.sigev_notify = SIGEV_THREAD; |
|
54 |
sigev.sigev_notify_attributes = NULL; |
|
55 |
sigev.sigev_notify_function = timer_notify; |
|
56 |
||
57 |
timer_create (CLOCK_REALTIME, &sigev, &timer); |
|
58 |
} |
|
59 |
||
454 | 60 |
void StopTimerLoop(TimerCallback_t exitfunction) |
32 | 61 |
{ |
149 | 62 |
EnterMutex(); |
32 | 63 |
timer_delete (timer); |
454 | 64 |
exitfunction(NULL,0); |
149 | 65 |
LeaveMutex(); |
32 | 66 |
} |
67 |
||
68 |
void StartTimerLoop(TimerCallback_t init_callback) |
|
0 | 69 |
{ |
149 | 70 |
EnterMutex(); |
0 | 71 |
// At first, TimeDispatch will call init_callback. |
72 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
|
149 | 73 |
LeaveMutex(); |
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 CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
0 | 77 |
{ |
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
78 |
pthread_create(Thread, NULL, ReceiveLoopPtr, (void*)port); |
0 | 79 |
} |
80 |
||
401
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
81 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread) |
0 | 82 |
{ |
401
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
83 |
pthread_kill(*Thread, SIGTERM); |
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
84 |
pthread_join(*Thread, NULL); |
0 | 85 |
} |
86 |
||
48
adc6572caf5d
minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents:
38
diff
changeset
|
87 |
#define maxval(a,b) ((a>b)?a:b) |
0 | 88 |
void setTimer(TIMEVAL value) |
89 |
{ |
|
90 |
// printf("setTimer(TIMEVAL value=%d)\n", value); |
|
32 | 91 |
// TIMEVAL is us whereas setitimer wants ns... |
38 | 92 |
long tv_nsec = 1000 * (maxval(value,1)%1000000); |
32 | 93 |
time_t tv_sec = value/1000000; |
94 |
struct itimerspec timerValues; |
|
95 |
timerValues.it_value.tv_sec = tv_sec; |
|
96 |
timerValues.it_value.tv_nsec = tv_nsec; |
|
0 | 97 |
timerValues.it_interval.tv_sec = 0; |
32 | 98 |
timerValues.it_interval.tv_nsec = 0; |
99 |
||
100 |
timer_settime (timer, 0, &timerValues, NULL); |
|
0 | 101 |
} |
102 |
||
103 |
TIMEVAL getElapsedTime(void) |
|
104 |
{ |
|
105 |
struct timeval p; |
|
106 |
gettimeofday(&p,NULL); |
|
107 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
108 |
return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec; |
|
109 |
} |