author | greg |
Fri, 17 Oct 2008 13:03:19 +0200 | |
changeset 257 | a4b01f57069a |
parent 245 | 60a221d72152 |
child 280 | f2ef79f3dba0 |
permissions | -rw-r--r-- |
205 | 1 |
#include <stdio.h> |
2 |
#include <string.h> |
|
3 |
#include <time.h> |
|
4 |
#include <signal.h> |
|
5 |
#include <stdlib.h> |
|
6 |
#include <pthread.h> |
|
7 |
||
236 | 8 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
205 | 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 |
//} |
|
17 |
||
18 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
19 |
{ |
|
20 |
clock_gettime(CLOCK_REALTIME, CURRENT_TIME); |
|
21 |
} |
|
22 |
||
23 |
void PLC_timer_notify(sigval_t val) |
|
24 |
{ |
|
25 |
PLC_GetTime(&__CURRENT_TIME); |
|
26 |
__run(); |
|
27 |
} |
|
28 |
||
29 |
timer_t PLC_timer; |
|
30 |
||
31 |
void PLC_SetTimer(long long next, long long period) |
|
32 |
{ |
|
33 |
struct itimerspec timerValues; |
|
34 |
/* |
|
35 |
printf("SetTimer(%lld,%lld)\n",next, period); |
|
36 |
*/ |
|
37 |
memset (&timerValues, 0, sizeof (struct itimerspec)); |
|
38 |
{ |
|
39 |
#ifdef __lldiv_t_defined |
|
40 |
lldiv_t nxt_div = lldiv(next, 1000000000); |
|
41 |
lldiv_t period_div = lldiv(period, 1000000000); |
|
42 |
timerValues.it_value.tv_sec = nxt_div.quot; |
|
43 |
timerValues.it_value.tv_nsec = nxt_div.rem; |
|
44 |
timerValues.it_interval.tv_sec = period_div.quot; |
|
45 |
timerValues.it_interval.tv_nsec = period_div.rem; |
|
46 |
#else |
|
47 |
timerValues.it_value.tv_sec = next / 1000000000; |
|
48 |
timerValues.it_value.tv_nsec = next % 1000000000; |
|
49 |
timerValues.it_interval.tv_sec = period / 1000000000; |
|
50 |
timerValues.it_interval.tv_nsec = period % 1000000000; |
|
51 |
#endif |
|
52 |
} |
|
53 |
timer_settime (PLC_timer, 0, &timerValues, NULL); |
|
54 |
} |
|
55 |
// |
|
56 |
void catch_signal(int sig) |
|
57 |
{ |
|
58 |
// signal(SIGTERM, catch_signal); |
|
59 |
signal(SIGINT, catch_signal); |
|
60 |
printf("Got Signal %d\n",sig); |
|
61 |
exit(0); |
|
62 |
} |
|
63 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
64 |
|
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
65 |
static int __debug_tick; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
66 |
|
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
67 |
static pthread_mutex_t wait_mutex = PTHREAD_MUTEX_INITIALIZER; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
68 |
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
69 |
|
205 | 70 |
int startPLC(int argc,char **argv) |
71 |
{ |
|
72 |
struct sigevent sigev; |
|
73 |
/* Translate PLC's microseconds to Ttick nanoseconds */ |
|
74 |
Ttick = 1000000 * maxval(common_ticktime__,1); |
|
75 |
||
76 |
memset (&sigev, 0, sizeof (struct sigevent)); |
|
77 |
sigev.sigev_value.sival_int = 0; |
|
78 |
sigev.sigev_notify = SIGEV_THREAD; |
|
79 |
sigev.sigev_notify_attributes = NULL; |
|
80 |
sigev.sigev_notify_function = PLC_timer_notify; |
|
81 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
82 |
pthread_mutex_lock(&wait_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
83 |
|
205 | 84 |
timer_create (CLOCK_REALTIME, &sigev, &PLC_timer); |
85 |
if( __init(argc,argv) == 0 ){ |
|
86 |
PLC_SetTimer(Ttick,Ttick); |
|
87 |
||
88 |
/* install signal handler for manual break */ |
|
89 |
// signal(SIGTERM, catch_signal); |
|
90 |
signal(SIGINT, catch_signal); |
|
91 |
}else{ |
|
92 |
return 1; |
|
93 |
} |
|
94 |
return 0; |
|
95 |
} |
|
96 |
||
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
97 |
int TryEnterDebugSection(void) |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
98 |
{ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
99 |
return pthread_mutex_trylock(&debug_mutex) == 0; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
100 |
} |
235 | 101 |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
102 |
void LeaveDebugSection(void) |
235 | 103 |
{ |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
104 |
pthread_mutex_unlock(&debug_mutex); |
235 | 105 |
} |
106 |
||
205 | 107 |
int stopPLC() |
108 |
{ |
|
109 |
/* Stop the PLC */ |
|
110 |
PLC_SetTimer(0,0); |
|
111 |
timer_delete (PLC_timer); |
|
112 |
__cleanup(); |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
113 |
__debug_tick = -1; |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
114 |
pthread_mutex_unlock(&wait_mutex); |
205 | 115 |
} |
116 |
||
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
117 |
extern int __tick; |
205 | 118 |
/* from plc_debugger.c */ |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
119 |
int WaitDebugData() |
205 | 120 |
{ |
121 |
/* Wait signal from PLC thread */ |
|
235 | 122 |
pthread_mutex_lock(&wait_mutex); |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
123 |
return __debug_tick; |
205 | 124 |
} |
125 |
||
126 |
/* Called by PLC thread when debug_publish finished |
|
127 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
128 |
void InitiateDebugTransfer() |
|
129 |
{ |
|
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
130 |
/* Leave debugger section */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
131 |
pthread_mutex_unlock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
132 |
/* remember tick */ |
227
48c13b84505c
- Some improovements in debug data feedback data
etisserant
parents:
205
diff
changeset
|
133 |
__debug_tick = __tick; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
134 |
/* signal debugger thread it can read data */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
135 |
pthread_mutex_unlock(&wait_mutex); |
205 | 136 |
} |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
137 |
|
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
138 |
void suspendDebug() |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
139 |
{ |
245
60a221d72152
Added __DEBUG global var to eventually change PLC code execution gehavior
etisserant
parents:
239
diff
changeset
|
140 |
__DEBUG = 0; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
141 |
/* Prevent PLC to enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
142 |
pthread_mutex_lock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
143 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
144 |
|
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
145 |
void resumeDebug() |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
146 |
{ |
245
60a221d72152
Added __DEBUG global var to eventually change PLC code execution gehavior
etisserant
parents:
239
diff
changeset
|
147 |
__DEBUG = 1; |
239
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
148 |
/* Let PLC enter debug code */ |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
149 |
pthread_mutex_unlock(&debug_mutex); |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
150 |
} |
112b4bc523b3
Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents:
236
diff
changeset
|
151 |