1 #include <stdio.h> |
1 #include <stdio.h> |
2 #include <string.h> |
2 #include <string.h> |
3 #include <time.h> |
3 #include <time.h> |
4 #include <signal.h> |
4 #include <signal.h> |
|
5 #include <stdlib.h> |
5 |
6 |
|
7 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
8 { |
|
9 clock_gettime(CLOCK_REALTIME, CURRENT_TIME); |
|
10 } |
6 |
11 |
7 void PLC_timer_notify(sigval_t val) |
12 void PLC_timer_notify(sigval_t val) |
8 { |
13 { |
9 clock_gettime(CLOCK_REALTIME, &__CURRENT_TIME); |
14 PLC_GetTime(&__CURRENT_TIME); |
10 __run(); |
15 __run(); |
|
16 } |
|
17 |
|
18 timer_t PLC_timer; |
|
19 |
|
20 void PLC_SetTimer(long long next, long long period) |
|
21 { |
|
22 struct itimerspec timerValues; |
|
23 /* |
|
24 printf("SetTimer(%lld,%lld)\n",next, period); |
|
25 */ |
|
26 memset (&timerValues, 0, sizeof (struct itimerspec)); |
|
27 { |
|
28 #ifdef __lldiv_t_defined |
|
29 lldiv_t nxt_div = lldiv(next, 1000000000); |
|
30 lldiv_t period_div = lldiv(period, 1000000000); |
|
31 timerValues.it_value.tv_sec = nxt_div.quot; |
|
32 timerValues.it_value.tv_nsec = nxt_div.rem; |
|
33 timerValues.it_interval.tv_sec = period_div.quot; |
|
34 timerValues.it_interval.tv_nsec = period_div.rem; |
|
35 #else |
|
36 timerValues.it_value.tv_sec = next / 1000000000; |
|
37 timerValues.it_value.tv_nsec = next % 1000000000; |
|
38 timerValues.it_interval.tv_sec = period / 1000000000; |
|
39 timerValues.it_interval.tv_nsec = period % 1000000000; |
|
40 #endif |
|
41 } |
|
42 timer_settime (PLC_timer, 0, &timerValues, NULL); |
11 } |
43 } |
12 |
44 |
13 void catch_signal(int sig) |
45 void catch_signal(int sig) |
14 { |
46 { |
15 signal(SIGTERM, catch_signal); |
47 signal(SIGTERM, catch_signal); |
17 printf("Got Signal %d\n",sig); |
49 printf("Got Signal %d\n",sig); |
18 } |
50 } |
19 |
51 |
20 int main(int argc,char **argv) |
52 int main(int argc,char **argv) |
21 { |
53 { |
22 timer_t timer; |
|
23 struct sigevent sigev; |
54 struct sigevent sigev; |
24 long tv_nsec = 1000000 * (maxval(common_ticktime__,1)%1000); |
55 /* Translate PLC's microseconds to Ttick nanoseconds */ |
25 time_t tv_sec = common_ticktime__/1000; |
56 Ttick = 1000000 * maxval(common_ticktime__,1); |
26 struct itimerspec timerValues; |
|
27 |
57 |
28 memset (&sigev, 0, sizeof (struct sigevent)); |
58 memset (&sigev, 0, sizeof (struct sigevent)); |
29 memset (&timerValues, 0, sizeof (struct itimerspec)); |
|
30 sigev.sigev_value.sival_int = 0; |
59 sigev.sigev_value.sival_int = 0; |
31 sigev.sigev_notify = SIGEV_THREAD; |
60 sigev.sigev_notify = SIGEV_THREAD; |
32 sigev.sigev_notify_attributes = NULL; |
61 sigev.sigev_notify_attributes = NULL; |
33 sigev.sigev_notify_function = PLC_timer_notify; |
62 sigev.sigev_notify_function = PLC_timer_notify; |
34 timerValues.it_value.tv_sec = tv_sec; |
|
35 timerValues.it_value.tv_nsec = tv_nsec; |
|
36 timerValues.it_interval.tv_sec = tv_sec; |
|
37 timerValues.it_interval.tv_nsec = tv_nsec; |
|
38 |
63 |
|
64 timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
39 if( __init(argc,argv) == 0 ){ |
65 if( __init(argc,argv) == 0 ){ |
40 timer_create (CLOCK_REALTIME, &sigev, &timer); |
66 PLC_SetTimer(Ttick,Ttick); |
41 timer_settime (timer, 0, &timerValues, NULL); |
|
42 |
67 |
43 /* install signal handler for manual break */ |
68 /* install signal handler for manual break */ |
44 signal(SIGTERM, catch_signal); |
69 signal(SIGTERM, catch_signal); |
45 signal(SIGINT, catch_signal); |
70 signal(SIGINT, catch_signal); |
46 |
71 /* Wait some signal */ |
47 pause(); |
72 pause(); |
48 |
73 /* Stop the PLC */ |
49 timer_delete (timer); |
74 PLC_SetTimer(0,0); |
50 } |
75 } |
51 __cleanup(); |
76 __cleanup(); |
|
77 timer_delete (PLC_timer); |
52 |
78 |
53 return 0; |
79 return 0; |
54 } |
80 } |