author | edouard |
Fri, 10 Sep 2010 13:44:56 +0200 | |
changeset 627 | 4939452b1bf1 |
parent 607 | 5fec528f66cf |
child 631 | 08b6b903f84a |
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 |
||
607
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
51 |
#if defined(__UCLIBC__) |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
52 |
int ret; |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
53 |
ret = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &timer); |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
54 |
signal(SIGALRM, timer_notify); |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
55 |
#else |
32 | 56 |
memset (&sigev, 0, sizeof (struct sigevent)); |
57 |
sigev.sigev_value.sival_int = 0; |
|
58 |
sigev.sigev_notify = SIGEV_THREAD; |
|
59 |
sigev.sigev_notify_attributes = NULL; |
|
60 |
sigev.sigev_notify_function = timer_notify; |
|
61 |
||
62 |
timer_create (CLOCK_REALTIME, &sigev, &timer); |
|
607
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
63 |
#endif |
32 | 64 |
} |
65 |
||
454 | 66 |
void StopTimerLoop(TimerCallback_t exitfunction) |
32 | 67 |
{ |
149 | 68 |
EnterMutex(); |
32 | 69 |
timer_delete (timer); |
454 | 70 |
exitfunction(NULL,0); |
149 | 71 |
LeaveMutex(); |
32 | 72 |
} |
73 |
||
74 |
void StartTimerLoop(TimerCallback_t init_callback) |
|
0 | 75 |
{ |
149 | 76 |
EnterMutex(); |
0 | 77 |
// At first, TimeDispatch will call init_callback. |
78 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
|
149 | 79 |
LeaveMutex(); |
0 | 80 |
} |
81 |
||
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
82 |
void canReceiveLoop_signal(int sig) |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
83 |
{ |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
84 |
} |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
85 |
/* We assume that ReceiveLoop_task_proc is always the same */ |
508
08adb8d4b098
Added more correct signal handling to unix timers.
etisserant
parents:
507
diff
changeset
|
86 |
static void (*unixtimer_ReceiveLoop_task_proc)(CAN_PORT) = NULL; |
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
87 |
|
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
88 |
/** |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
89 |
* Enter in realtime and start the CAN receiver loop |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
90 |
* @param port |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
91 |
*/ |
540 | 92 |
void* unixtimer_canReceiveLoop(void* port) |
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
93 |
{ |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
94 |
|
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
95 |
/*get signal*/ |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
96 |
signal(SIGTERM, canReceiveLoop_signal); |
540 | 97 |
unixtimer_ReceiveLoop_task_proc((CAN_PORT)port); |
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
98 |
} |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
99 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
100 |
void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
0 | 101 |
{ |
508
08adb8d4b098
Added more correct signal handling to unix timers.
etisserant
parents:
507
diff
changeset
|
102 |
unixtimer_ReceiveLoop_task_proc = ReceiveLoopPtr; |
08adb8d4b098
Added more correct signal handling to unix timers.
etisserant
parents:
507
diff
changeset
|
103 |
pthread_create(Thread, NULL, unixtimer_canReceiveLoop, (void*)port); |
0 | 104 |
} |
105 |
||
401
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
106 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread) |
0 | 107 |
{ |
401
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
108 |
pthread_kill(*Thread, SIGTERM); |
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
109 |
pthread_join(*Thread, NULL); |
0 | 110 |
} |
111 |
||
48
adc6572caf5d
minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents:
38
diff
changeset
|
112 |
#define maxval(a,b) ((a>b)?a:b) |
0 | 113 |
void setTimer(TIMEVAL value) |
114 |
{ |
|
115 |
// printf("setTimer(TIMEVAL value=%d)\n", value); |
|
32 | 116 |
// TIMEVAL is us whereas setitimer wants ns... |
38 | 117 |
long tv_nsec = 1000 * (maxval(value,1)%1000000); |
32 | 118 |
time_t tv_sec = value/1000000; |
119 |
struct itimerspec timerValues; |
|
120 |
timerValues.it_value.tv_sec = tv_sec; |
|
121 |
timerValues.it_value.tv_nsec = tv_nsec; |
|
0 | 122 |
timerValues.it_interval.tv_sec = 0; |
32 | 123 |
timerValues.it_interval.tv_nsec = 0; |
124 |
||
125 |
timer_settime (timer, 0, &timerValues, NULL); |
|
0 | 126 |
} |
127 |
||
128 |
TIMEVAL getElapsedTime(void) |
|
129 |
{ |
|
130 |
struct timeval p; |
|
131 |
gettimeofday(&p,NULL); |
|
132 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
133 |
return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec; |
|
134 |
} |