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 #include <stdlib.h> |
|
6 #include <pthread.h> |
|
7 |
|
8 long AtomicCompareExchange(long* atomicvar,long exchange, long compared) |
|
9 { |
|
10 return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
11 } |
|
12 |
|
13 //long AtomicExchange(long* atomicvar,long exchange) |
|
14 //{ |
|
15 // return __sync_lock_test_and_set(atomicvar, exchange); |
|
16 //} |
6 |
17 |
7 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
18 void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
8 { |
19 { |
9 clock_gettime(CLOCK_REALTIME, CURRENT_TIME); |
20 clock_gettime(CLOCK_REALTIME, CURRENT_TIME); |
10 } |
21 } |
39 timerValues.it_interval.tv_nsec = period % 1000000000; |
50 timerValues.it_interval.tv_nsec = period % 1000000000; |
40 #endif |
51 #endif |
41 } |
52 } |
42 timer_settime (PLC_timer, 0, &timerValues, NULL); |
53 timer_settime (PLC_timer, 0, &timerValues, NULL); |
43 } |
54 } |
44 |
55 // |
45 void catch_signal(int sig) |
56 void catch_signal(int sig) |
46 { |
57 { |
47 signal(SIGTERM, catch_signal); |
58 // signal(SIGTERM, catch_signal); |
48 signal(SIGINT, catch_signal); |
59 signal(SIGINT, catch_signal); |
49 printf("Got Signal %d\n",sig); |
60 printf("Got Signal %d\n",sig); |
|
61 exit(0); |
50 } |
62 } |
51 |
63 |
52 int main(int argc,char **argv) |
64 int startPLC(int argc,char **argv) |
53 { |
65 { |
54 struct sigevent sigev; |
66 struct sigevent sigev; |
55 /* Translate PLC's microseconds to Ttick nanoseconds */ |
67 /* Translate PLC's microseconds to Ttick nanoseconds */ |
56 Ttick = 1000000 * maxval(common_ticktime__,1); |
68 Ttick = 1000000 * maxval(common_ticktime__,1); |
57 |
69 |
64 timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
76 timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
65 if( __init(argc,argv) == 0 ){ |
77 if( __init(argc,argv) == 0 ){ |
66 PLC_SetTimer(Ttick,Ttick); |
78 PLC_SetTimer(Ttick,Ttick); |
67 |
79 |
68 /* install signal handler for manual break */ |
80 /* install signal handler for manual break */ |
69 signal(SIGTERM, catch_signal); |
81 // signal(SIGTERM, catch_signal); |
70 signal(SIGINT, catch_signal); |
82 signal(SIGINT, catch_signal); |
71 /* Wait some signal */ |
83 }else{ |
72 pause(); |
84 return 1; |
73 /* Stop the PLC */ |
|
74 PLC_SetTimer(0,0); |
|
75 } |
85 } |
76 __cleanup(); |
|
77 timer_delete (PLC_timer); |
|
78 |
|
79 return 0; |
86 return 0; |
80 } |
87 } |
|
88 |
|
89 int stopPLC() |
|
90 { |
|
91 /* Stop the PLC */ |
|
92 PLC_SetTimer(0,0); |
|
93 timer_delete (PLC_timer); |
|
94 __cleanup(); |
|
95 } |
|
96 |
|
97 pthread_mutex_t DebugLock = PTHREAD_MUTEX_INITIALIZER; |
|
98 |
|
99 /* from plc_debugger.c */ |
|
100 void WaitDebugData() |
|
101 { |
|
102 /* Wait signal from PLC thread */ |
|
103 pthread_mutex_lock(&DebugLock); |
|
104 } |
|
105 |
|
106 /* Called by PLC thread when debug_publish finished |
|
107 * This is supposed to unlock debugger thread in WaitDebugData*/ |
|
108 void InitiateDebugTransfer() |
|
109 { |
|
110 /* signal debugger thread to continue*/ |
|
111 pthread_mutex_unlock(&DebugLock); |
|
112 } |