author | JaFojtik |
Fri, 03 Feb 2012 21:42:48 +0100 | |
changeset 695 | 0271f08ba819 |
parent 663 | 70fc3603e36f |
child 793 | 72e9e1064432 |
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> |
631 | 6 |
#include <time.h> |
0 | 7 |
|
8 |
#include "applicfg.h" |
|
9 |
#include "timer.h" |
|
10 |
||
631 | 11 |
static pthread_mutex_t CanFestival_mutex = PTHREAD_MUTEX_INITIALIZER; |
0 | 12 |
|
631 | 13 |
static struct timeval last_sig; |
32 | 14 |
|
631 | 15 |
static timer_t timer; |
0 | 16 |
|
454 | 17 |
void TimerCleanup(void) |
18 |
{ |
|
19 |
/* only used in realtime apps */ |
|
20 |
} |
|
21 |
||
0 | 22 |
void EnterMutex(void) |
23 |
{ |
|
631 | 24 |
if(pthread_mutex_lock(&CanFestival_mutex)) { |
25 |
fprintf(stderr, "pthread_mutex_lock() failed\n"); |
|
26 |
} |
|
0 | 27 |
} |
28 |
||
29 |
void LeaveMutex(void) |
|
30 |
{ |
|
631 | 31 |
if(pthread_mutex_unlock(&CanFestival_mutex)) { |
32 |
fprintf(stderr, "pthread_mutex_unlock() failed\n"); |
|
33 |
} |
|
0 | 34 |
} |
35 |
||
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
36 |
void timer_notify(sigval_t val) |
32 | 37 |
{ |
631 | 38 |
if(gettimeofday(&last_sig,NULL)) { |
39 |
perror("gettimeofday()"); |
|
40 |
} |
|
32 | 41 |
EnterMutex(); |
42 |
TimeDispatch(); |
|
43 |
LeaveMutex(); |
|
44 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
|
45 |
} |
|
46 |
||
454 | 47 |
void TimerInit(void) |
32 | 48 |
{ |
49 |
struct sigevent sigev; |
|
50 |
||
51 |
// Take first absolute time ref. |
|
631 | 52 |
if(gettimeofday(&last_sig,NULL)){ |
53 |
perror("gettimeofday()"); |
|
54 |
} |
|
32 | 55 |
|
607
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
56 |
#if defined(__UCLIBC__) |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
57 |
int ret; |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
58 |
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
|
59 |
signal(SIGALRM, timer_notify); |
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
60 |
#else |
32 | 61 |
memset (&sigev, 0, sizeof (struct sigevent)); |
62 |
sigev.sigev_value.sival_int = 0; |
|
63 |
sigev.sigev_notify = SIGEV_THREAD; |
|
64 |
sigev.sigev_notify_attributes = NULL; |
|
65 |
sigev.sigev_notify_function = timer_notify; |
|
66 |
||
631 | 67 |
if(timer_create (CLOCK_REALTIME, &sigev, &timer)) { |
68 |
perror("timer_create()"); |
|
69 |
} |
|
607
5fec528f66cf
create specific timer thread for UCLIBC support (with CLOCK_PROCESS_CPUTIME_ID parameter)
greg
parents:
540
diff
changeset
|
70 |
#endif |
32 | 71 |
} |
72 |
||
454 | 73 |
void StopTimerLoop(TimerCallback_t exitfunction) |
32 | 74 |
{ |
149 | 75 |
EnterMutex(); |
631 | 76 |
if(timer_delete (timer)) { |
77 |
perror("timer_delete()"); |
|
78 |
} |
|
454 | 79 |
exitfunction(NULL,0); |
149 | 80 |
LeaveMutex(); |
32 | 81 |
} |
82 |
||
83 |
void StartTimerLoop(TimerCallback_t init_callback) |
|
0 | 84 |
{ |
149 | 85 |
EnterMutex(); |
0 | 86 |
// At first, TimeDispatch will call init_callback. |
87 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
|
149 | 88 |
LeaveMutex(); |
0 | 89 |
} |
90 |
||
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
91 |
void canReceiveLoop_signal(int sig) |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
92 |
{ |
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 |
/* 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
|
95 |
static void (*unixtimer_ReceiveLoop_task_proc)(CAN_PORT) = NULL; |
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
96 |
|
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
97 |
/** |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
98 |
* Enter in realtime and start the CAN receiver loop |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
99 |
* @param port |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
100 |
*/ |
540 | 101 |
void* unixtimer_canReceiveLoop(void* port) |
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
102 |
{ |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
103 |
/*get signal*/ |
663 | 104 |
// if(signal(SIGTERM, canReceiveLoop_signal) == SIG_ERR) { |
105 |
// perror("signal()"); |
|
106 |
//} |
|
540 | 107 |
unixtimer_ReceiveLoop_task_proc((CAN_PORT)port); |
631 | 108 |
|
109 |
return NULL; |
|
507
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
110 |
} |
c613e6cd34fa
Added more correct signal handling to unix timers.
etisserant
parents:
454
diff
changeset
|
111 |
|
145
e747d2e26af0
Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents:
48
diff
changeset
|
112 |
void CreateReceiveTask(CAN_PORT port, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
0 | 113 |
{ |
508
08adb8d4b098
Added more correct signal handling to unix timers.
etisserant
parents:
507
diff
changeset
|
114 |
unixtimer_ReceiveLoop_task_proc = ReceiveLoopPtr; |
631 | 115 |
if(pthread_create(Thread, NULL, unixtimer_canReceiveLoop, (void*)port)) { |
116 |
perror("pthread_create()"); |
|
117 |
} |
|
0 | 118 |
} |
119 |
||
401
2c90876b9751
Fixed segfault on quit with Xenomai, due to bat parameter type in waitReceiveTaskEnd.
etisserant
parents:
149
diff
changeset
|
120 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread) |
0 | 121 |
{ |
631 | 122 |
if(pthread_kill(*Thread, SIGTERM)) { |
123 |
perror("pthread_kill()"); |
|
124 |
} |
|
125 |
if(pthread_join(*Thread, NULL)) { |
|
126 |
perror("pthread_join()"); |
|
127 |
} |
|
0 | 128 |
} |
129 |
||
48
adc6572caf5d
minval/maxval macro operators precedence fix. Thanks Luis Jim?nez.
etisserant
parents:
38
diff
changeset
|
130 |
#define maxval(a,b) ((a>b)?a:b) |
0 | 131 |
void setTimer(TIMEVAL value) |
132 |
{ |
|
133 |
// printf("setTimer(TIMEVAL value=%d)\n", value); |
|
32 | 134 |
// TIMEVAL is us whereas setitimer wants ns... |
38 | 135 |
long tv_nsec = 1000 * (maxval(value,1)%1000000); |
32 | 136 |
time_t tv_sec = value/1000000; |
137 |
struct itimerspec timerValues; |
|
138 |
timerValues.it_value.tv_sec = tv_sec; |
|
139 |
timerValues.it_value.tv_nsec = tv_nsec; |
|
0 | 140 |
timerValues.it_interval.tv_sec = 0; |
32 | 141 |
timerValues.it_interval.tv_nsec = 0; |
142 |
||
143 |
timer_settime (timer, 0, &timerValues, NULL); |
|
0 | 144 |
} |
145 |
||
146 |
TIMEVAL getElapsedTime(void) |
|
147 |
{ |
|
148 |
struct timeval p; |
|
631 | 149 |
if(gettimeofday(&p,NULL)) { |
150 |
perror("gettimeofday()"); |
|
151 |
} |
|
0 | 152 |
// printf("getCurrentTime() return=%u\n", p.tv_usec); |
153 |
return (p.tv_sec - last_sig.tv_sec)* 1000000 + p.tv_usec - last_sig.tv_usec; |
|
154 |
} |