author | Edouard Tisserant |
Mon, 30 Mar 2020 14:26:42 +0200 | |
branch | svghmi |
changeset 2906 | 3b4a1319da09 |
parent 2820 | d9b5303d43dc |
child 3294 | e3db472b0dfb |
permissions | -rw-r--r-- |
321 | 1 |
/** |
954 | 2 |
* Xenomai Linux specific code |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
3 |
**/ |
321 | 4 |
|
5 |
#include <stdio.h> |
|
615 | 6 |
#include <unistd.h> |
321 | 7 |
#include <string.h> |
8 |
#include <time.h> |
|
9 |
#include <signal.h> |
|
10 |
#include <stdlib.h> |
|
11 |
#include <sys/mman.h> |
|
342 | 12 |
#include <sys/fcntl.h> |
321 | 13 |
|
1974 | 14 |
#include <alchemy/task.h> |
15 |
#include <alchemy/timer.h> |
|
16 |
#include <alchemy/sem.h> |
|
17 |
#include <alchemy/pipe.h> |
|
321 | 18 |
|
19 |
unsigned int PLC_state = 0; |
|
615 | 20 |
#define PLC_STATE_TASK_CREATED 1 |
21 |
#define PLC_STATE_DEBUG_FILE_OPENED 2 |
|
22 |
#define PLC_STATE_DEBUG_PIPE_CREATED 4 |
|
23 |
#define PLC_STATE_PYTHON_FILE_OPENED 8 |
|
24 |
#define PLC_STATE_PYTHON_PIPE_CREATED 16 |
|
25 |
#define PLC_STATE_WAITDEBUG_FILE_OPENED 32 |
|
26 |
#define PLC_STATE_WAITDEBUG_PIPE_CREATED 64 |
|
27 |
#define PLC_STATE_WAITPYTHON_FILE_OPENED 128 |
|
28 |
#define PLC_STATE_WAITPYTHON_PIPE_CREATED 256 |
|
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
29 |
#define PLC_STATE_SVGHMI_FILE_OPENED 512 |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
30 |
#define PLC_STATE_SVGHMI_PIPE_CREATED 1024 |
615 | 31 |
|
32 |
#define WAITDEBUG_PIPE_DEVICE "/dev/rtp0" |
|
33 |
#define WAITDEBUG_PIPE_MINOR 0 |
|
34 |
#define DEBUG_PIPE_DEVICE "/dev/rtp1" |
|
35 |
#define DEBUG_PIPE_MINOR 1 |
|
36 |
#define WAITPYTHON_PIPE_DEVICE "/dev/rtp2" |
|
37 |
#define WAITPYTHON_PIPE_MINOR 2 |
|
38 |
#define PYTHON_PIPE_DEVICE "/dev/rtp3" |
|
39 |
#define PYTHON_PIPE_MINOR 3 |
|
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
40 |
#define SVGHMI_PIPE_DEVICE "/dev/rtp4" |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
41 |
#define SVGHMI_PIPE_MINOR 4 |
615 | 42 |
#define PIPE_SIZE 1 |
321 | 43 |
|
1990
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
44 |
// rt-pipes commands |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
45 |
|
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
46 |
#define PYTHON_PENDING_COMMAND 1 |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
47 |
#define PYTHON_FINISH 2 |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
48 |
|
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
49 |
#define DEBUG_FINISH 2 |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
50 |
|
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
51 |
#define DEBUG_PENDING_DATA 1 |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
52 |
#define DEBUG_UNLOCK 1 |
321 | 53 |
|
54 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
55 |
{ |
|
56 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
57 |
} |
|
954 | 58 |
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange) |
59 |
{ |
|
60 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
61 |
} |
|
321 | 62 |
|
63 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
64 |
{ |
|
65 |
RTIME current_time = rt_timer_read(); |
|
66 |
CURRENT_TIME->tv_sec = current_time / 1000000000; |
|
67 |
CURRENT_TIME->tv_nsec = current_time % 1000000000; |
|
68 |
} |
|
69 |
||
70 |
RT_TASK PLC_task; |
|
342 | 71 |
RT_PIPE WaitDebug_pipe; |
615 | 72 |
RT_PIPE WaitPython_pipe; |
73 |
RT_PIPE Debug_pipe; |
|
74 |
RT_PIPE Python_pipe; |
|
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
75 |
RT_PIPE svghmi_pipe; |
615 | 76 |
int WaitDebug_pipe_fd; |
77 |
int WaitPython_pipe_fd; |
|
78 |
int Debug_pipe_fd; |
|
79 |
int Python_pipe_fd; |
|
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
80 |
int svghmi_pipe_fd; |
615 | 81 |
|
321 | 82 |
int PLC_shutdown = 0; |
83 |
||
615 | 84 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
321 | 85 |
{ |
86 |
RTIME current_time = rt_timer_read(); |
|
87 |
rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period)); |
|
88 |
} |
|
89 |
||
90 |
void PLC_task_proc(void *arg) |
|
91 |
{ |
|
1428
e14003eb4d42
Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents:
985
diff
changeset
|
92 |
PLC_SetTimer(common_ticktime__, common_ticktime__); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
93 |
|
695 | 94 |
while (!PLC_shutdown) { |
321 | 95 |
PLC_GetTime(&__CURRENT_TIME); |
96 |
__run(); |
|
97 |
if (PLC_shutdown) break; |
|
98 |
rt_task_wait_period(NULL); |
|
99 |
} |
|
1990
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
100 |
/* since xenomai 3 it is not enough to close() |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
101 |
file descriptor to unblock read()... */ |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
102 |
{ |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
103 |
/* explicitely finish python thread */ |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
104 |
char msg = PYTHON_FINISH; |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
105 |
rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL); |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
106 |
} |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
107 |
{ |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
108 |
/* explicitely finish debug thread */ |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
109 |
char msg = DEBUG_FINISH; |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
110 |
rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL); |
2e0fbdd152de
Fixed Xenomai 3 PLC stop freeze. Now use explicit finish command with pipes. Closing both ends of pipes doesn't abort blocking read anymore.
Edouard Tisserant
parents:
1981
diff
changeset
|
111 |
} |
321 | 112 |
} |
113 |
||
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
345
diff
changeset
|
114 |
static unsigned long __debug_tick; |
321 | 115 |
|
116 |
void PLC_cleanup_all(void) |
|
117 |
{ |
|
118 |
if (PLC_state & PLC_STATE_TASK_CREATED) { |
|
119 |
rt_task_delete(&PLC_task); |
|
120 |
PLC_state &= ~PLC_STATE_TASK_CREATED; |
|
121 |
} |
|
122 |
||
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
123 |
if (PLC_state & PLC_STATE_SVGHMI_PIPE_CREATED) { |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
124 |
rt_pipe_delete(&svghmi_pipe); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
125 |
PLC_state &= ~PLC_STATE_SVGHMI_PIPE_CREATED; |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
126 |
} |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
127 |
|
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
128 |
if (PLC_state & PLC_STATE_SVGHMI_FILE_OPENED) { |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
129 |
close(svghmi_pipe_fd); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
130 |
PLC_state &= ~PLC_STATE_SVGHMI_FILE_OPENED; |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
131 |
} |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
132 |
|
615 | 133 |
if (PLC_state & PLC_STATE_WAITDEBUG_PIPE_CREATED) { |
134 |
rt_pipe_delete(&WaitDebug_pipe); |
|
135 |
PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
136 |
} |
|
137 |
||
138 |
if (PLC_state & PLC_STATE_WAITDEBUG_FILE_OPENED) { |
|
139 |
close(WaitDebug_pipe_fd); |
|
140 |
PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
141 |
} |
|
142 |
||
143 |
if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) { |
|
144 |
rt_pipe_delete(&WaitPython_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
145 |
PLC_state &= ~PLC_STATE_WAITPYTHON_PIPE_CREATED; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
146 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
147 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
148 |
if (PLC_state & PLC_STATE_WAITPYTHON_FILE_OPENED) { |
615 | 149 |
close(WaitPython_pipe_fd); |
150 |
PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
321 | 151 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
152 |
|
342 | 153 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) { |
615 | 154 |
rt_pipe_delete(&Debug_pipe); |
342 | 155 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
156 |
} |
|
157 |
||
158 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
|
615 | 159 |
close(Debug_pipe_fd); |
342 | 160 |
PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED; |
321 | 161 |
} |
162 |
||
615 | 163 |
if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) { |
164 |
rt_pipe_delete(&Python_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
165 |
PLC_state &= ~PLC_STATE_PYTHON_PIPE_CREATED; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
166 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
167 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
168 |
if (PLC_state & PLC_STATE_PYTHON_FILE_OPENED) { |
615 | 169 |
close(Python_pipe_fd); |
170 |
PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED; |
|
171 |
} |
|
172 |
||
321 | 173 |
} |
174 |
||
175 |
int stopPLC() |
|
176 |
{ |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
177 |
/* Stop the PLC */ |
321 | 178 |
PLC_shutdown = 1; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
179 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
180 |
/* Wait until PLC task stops */ |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
181 |
rt_task_join(&PLC_task); |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
182 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
183 |
PLC_cleanup_all(); |
342 | 184 |
__cleanup(); |
321 | 185 |
__debug_tick = -1; |
615 | 186 |
return 0; |
321 | 187 |
} |
188 |
||
189 |
// |
|
190 |
void catch_signal(int sig) |
|
191 |
{ |
|
192 |
stopPLC(); |
|
193 |
// signal(SIGTERM, catch_signal); |
|
194 |
signal(SIGINT, catch_signal); |
|
195 |
printf("Got Signal %d\n",sig); |
|
196 |
exit(0); |
|
197 |
} |
|
198 |
||
1981 | 199 |
#define _startPLCLog(text) \ |
200 |
{\ |
|
201 |
char mstr[] = text;\ |
|
202 |
LogMessage(LOG_CRITICAL, mstr, sizeof(mstr));\ |
|
203 |
goto error;\ |
|
204 |
} |
|
205 |
||
206 |
#define FO "Failed opening " |
|
207 |
||
321 | 208 |
#define max_val(a,b) ((a>b)?a:b) |
209 |
int startPLC(int argc,char **argv) |
|
210 |
{ |
|
211 |
signal(SIGINT, catch_signal); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
212 |
|
615 | 213 |
/* no memory swapping for that process */ |
321 | 214 |
mlockall(MCL_CURRENT | MCL_FUTURE); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
215 |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
216 |
PLC_shutdown = 0; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
217 |
|
615 | 218 |
/*** RT Pipes creation and opening ***/ |
219 |
/* create Debug_pipe */ |
|
1980 | 220 |
if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 221 |
_startPLCLog(FO "Debug_pipe real-time end"); |
615 | 222 |
PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED; |
223 |
||
224 |
/* open Debug_pipe*/ |
|
1981 | 225 |
if((Debug_pipe_fd = open(DEBUG_PIPE_DEVICE, O_RDWR)) == -1) |
226 |
_startPLCLog(FO DEBUG_PIPE_DEVICE); |
|
615 | 227 |
PLC_state |= PLC_STATE_DEBUG_FILE_OPENED; |
228 |
||
229 |
/* create Python_pipe */ |
|
1980 | 230 |
if(rt_pipe_create(&Python_pipe, "Python_pipe", PYTHON_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 231 |
_startPLCLog(FO "Python_pipe real-time end"); |
615 | 232 |
PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED; |
233 |
||
234 |
/* open Python_pipe*/ |
|
1981 | 235 |
if((Python_pipe_fd = open(PYTHON_PIPE_DEVICE, O_RDWR)) == -1) |
236 |
_startPLCLog(FO PYTHON_PIPE_DEVICE); |
|
615 | 237 |
PLC_state |= PLC_STATE_PYTHON_FILE_OPENED; |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
238 |
|
342 | 239 |
/* create WaitDebug_pipe */ |
1980 | 240 |
if(rt_pipe_create(&WaitDebug_pipe, "WaitDebug_pipe", WAITDEBUG_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 241 |
_startPLCLog(FO "WaitDebug_pipe real-time end"); |
615 | 242 |
PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED; |
342 | 243 |
|
244 |
/* open WaitDebug_pipe*/ |
|
1981 | 245 |
if((WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR)) == -1) |
246 |
_startPLCLog(FO WAITDEBUG_PIPE_DEVICE); |
|
615 | 247 |
PLC_state |= PLC_STATE_WAITDEBUG_FILE_OPENED; |
248 |
||
249 |
/* create WaitPython_pipe */ |
|
1980 | 250 |
if(rt_pipe_create(&WaitPython_pipe, "WaitPython_pipe", WAITPYTHON_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 251 |
_startPLCLog(FO "WaitPython_pipe real-time end"); |
615 | 252 |
PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED; |
253 |
||
254 |
/* open WaitPython_pipe*/ |
|
1981 | 255 |
if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) |
256 |
_startPLCLog(FO WAITPYTHON_PIPE_DEVICE); |
|
615 | 257 |
PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED; |
258 |
||
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
259 |
/* create svghmi_pipe */ |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
260 |
if(rt_pipe_create(&svghmi_pipe, "svghmi_pipe", SVGHMI_PIPE_MINOR, PIPE_SIZE) < 0) |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
261 |
_startPLCLog(FO "svghmi_pipe real-time end"); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
262 |
PLC_state |= PLC_STATE_SVGHMI_PIPE_CREATED; |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
263 |
|
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
264 |
/* open svghmi_pipe*/ |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
265 |
if((svghmi_pipe_fd = open(SVGHMI_PIPE_DEVICE, O_RDWR)) == -1) |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
266 |
_startPLCLog(FO SVGHMI_PIPE_DEVICE); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
267 |
PLC_state |= PLC_STATE_SVGHMI_FILE_OPENED; |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
268 |
|
615 | 269 |
/*** create PLC task ***/ |
1981 | 270 |
if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE)) |
271 |
_startPLCLog("Failed creating PLC task"); |
|
321 | 272 |
PLC_state |= PLC_STATE_TASK_CREATED; |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
273 |
|
615 | 274 |
if(__init(argc,argv)) goto error; |
275 |
||
276 |
/* start PLC task */ |
|
1981 | 277 |
if(rt_task_start(&PLC_task, &PLC_task_proc, NULL)) |
278 |
_startPLCLog("Failed starting PLC task"); |
|
321 | 279 |
|
280 |
return 0; |
|
281 |
||
282 |
error: |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
283 |
|
321 | 284 |
PLC_cleanup_all(); |
285 |
return 1; |
|
286 |
} |
|
287 |
||
615 | 288 |
#define DEBUG_FREE 0 |
289 |
#define DEBUG_BUSY 1 |
|
290 |
static long debug_state = DEBUG_FREE; |
|
291 |
||
321 | 292 |
int TryEnterDebugSection(void) |
293 |
{ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
294 |
if(AtomicCompareExchange( |
615 | 295 |
&debug_state, |
296 |
DEBUG_FREE, |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
297 |
DEBUG_BUSY) == DEBUG_FREE){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
298 |
if(__DEBUG){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
299 |
return 1; |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
300 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
301 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
302 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
303 |
return 0; |
615 | 304 |
} |
305 |
||
321 | 306 |
void LeaveDebugSection(void) |
307 |
{ |
|
615 | 308 |
if(AtomicCompareExchange( &debug_state, |
309 |
DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){ |
|
310 |
char msg = DEBUG_UNLOCK; |
|
311 |
/* signal to NRT for wakeup */ |
|
312 |
rt_pipe_write(&Debug_pipe, &msg, sizeof(msg), P_NORMAL); |
|
313 |
} |
|
321 | 314 |
} |
315 |
||
397
6a7ff66a811d
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
345
diff
changeset
|
316 |
extern unsigned long __tick; |
615 | 317 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
318 |
int WaitDebugData(unsigned long *tick) |
321 | 319 |
{ |
615 | 320 |
char cmd; |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
321 |
int res; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
322 |
if (PLC_shutdown) return -1; |
321 | 323 |
/* Wait signal from PLC thread */ |
615 | 324 |
res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd)); |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
325 |
if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA){ |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
326 |
*tick = __debug_tick; |
615 | 327 |
return 0; |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
328 |
} |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
329 |
return -1; |
321 | 330 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
331 |
|
321 | 332 |
/* Called by PLC thread when debug_publish finished |
333 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
334 |
void InitiateDebugTransfer() |
|
335 |
{ |
|
615 | 336 |
char msg = DEBUG_PENDING_DATA; |
321 | 337 |
/* remember tick */ |
338 |
__debug_tick = __tick; |
|
339 |
/* signal debugger thread it can read data */ |
|
615 | 340 |
rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL); |
341 |
} |
|
342 |
||
343 |
int suspendDebug(int disable) |
|
344 |
{ |
|
345 |
char cmd = DEBUG_UNLOCK; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
346 |
if (PLC_shutdown) return -1; |
615 | 347 |
while(AtomicCompareExchange( |
348 |
&debug_state, |
|
349 |
DEBUG_FREE, |
|
350 |
DEBUG_BUSY) != DEBUG_FREE && |
|
351 |
cmd == DEBUG_UNLOCK){ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
352 |
if(read(Debug_pipe_fd, &cmd, sizeof(cmd)) != sizeof(cmd)){ |
615 | 353 |
return -1; |
354 |
} |
|
355 |
} |
|
356 |
__DEBUG = !disable; |
|
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
357 |
if (disable) |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
358 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
615 | 359 |
return 0; |
321 | 360 |
} |
361 |
||
362 |
void resumeDebug(void) |
|
363 |
{ |
|
615 | 364 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
365 |
} |
|
366 |
||
367 |
#define PYTHON_FREE 0 |
|
368 |
#define PYTHON_BUSY 1 |
|
369 |
static long python_state = PYTHON_FREE; |
|
370 |
||
321 | 371 |
int WaitPythonCommands(void) |
615 | 372 |
{ |
373 |
char cmd; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
374 |
if (PLC_shutdown) return -1; |
321 | 375 |
/* Wait signal from PLC thread */ |
615 | 376 |
if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){ |
377 |
return 0; |
|
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
378 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
379 |
return -1; |
321 | 380 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
381 |
|
321 | 382 |
/* Called by PLC thread on each new python command*/ |
383 |
void UnBlockPythonCommands(void) |
|
384 |
{ |
|
615 | 385 |
char msg = PYTHON_PENDING_COMMAND; |
386 |
rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL); |
|
321 | 387 |
} |
388 |
||
389 |
int TryLockPython(void) |
|
390 |
{ |
|
615 | 391 |
return AtomicCompareExchange( |
392 |
&python_state, |
|
393 |
PYTHON_FREE, |
|
394 |
PYTHON_BUSY) == PYTHON_FREE; |
|
395 |
} |
|
396 |
||
397 |
#define UNLOCK_PYTHON 1 |
|
398 |
void LockPython(void) |
|
399 |
{ |
|
400 |
char cmd = UNLOCK_PYTHON; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
401 |
if (PLC_shutdown) return; |
615 | 402 |
while(AtomicCompareExchange( |
403 |
&python_state, |
|
404 |
PYTHON_FREE, |
|
405 |
PYTHON_BUSY) != PYTHON_FREE && |
|
406 |
cmd == UNLOCK_PYTHON){ |
|
407 |
read(Python_pipe_fd, &cmd, sizeof(cmd)); |
|
408 |
} |
|
321 | 409 |
} |
410 |
||
411 |
void UnLockPython(void) |
|
412 |
{ |
|
615 | 413 |
if(AtomicCompareExchange( |
414 |
&python_state, |
|
415 |
PYTHON_BUSY, |
|
416 |
PYTHON_FREE) == PYTHON_BUSY){ |
|
417 |
if(rt_task_self()){/*is that the real time task ?*/ |
|
418 |
char cmd = UNLOCK_PYTHON; |
|
419 |
rt_pipe_write(&Python_pipe, &cmd, sizeof(cmd), P_NORMAL); |
|
420 |
}/* otherwise, no signaling from non real time */ |
|
421 |
} /* as plc does not wait for lock. */ |
|
422 |
} |
|
423 |
||
2820
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
424 |
void SVGHMI_SuspendFromPythonThread(void) |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
425 |
{ |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
426 |
char cmd = 1; /*whatever*/ |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
427 |
read(svghmi_pipe_fd, &cmd, sizeof(cmd)); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
428 |
} |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
429 |
|
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
430 |
void SVGHMI_WakeupFromRTThread(void) |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
431 |
{ |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
432 |
char cmd; |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
433 |
rt_pipe_write(&svghmi_pipe, &cmd, sizeof(cmd), P_NORMAL); |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
434 |
} |
d9b5303d43dc
SVGHMI : had to move the problem of wkaing up python thread from plc thread to platform specific code.
Edouard Tisserant
parents:
1990
diff
changeset
|
435 |
|
1903
084256be3658
Main runtime template C code : Added HAVE_RETAIN preprocessor definition for customized build to signal it provides IEC-61131 Retain memory handling function. Removed targets/Xenomai/plc_Xenomai_noretain.c, now useless.
Edouard Tisserant
parents:
1717
diff
changeset
|
436 |
#ifndef HAVE_RETAIN |
1717
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
437 |
int CheckRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
438 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
439 |
return 1; |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
440 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
441 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
442 |
void ValidateRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
443 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
444 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
445 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
446 |
void InValidateRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
447 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
448 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
449 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
450 |
void Retain(unsigned int offset, unsigned int count, void *p) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
451 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
452 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
453 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
454 |
void Remind(unsigned int offset, unsigned int count, void *p) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
455 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
456 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
457 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
458 |
void CleanupRetain(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
459 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
460 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
461 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
462 |
void InitRetain(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
463 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
464 |
} |
1903
084256be3658
Main runtime template C code : Added HAVE_RETAIN preprocessor definition for customized build to signal it provides IEC-61131 Retain memory handling function. Removed targets/Xenomai/plc_Xenomai_noretain.c, now useless.
Edouard Tisserant
parents:
1717
diff
changeset
|
465 |
#endif // !HAVE_RETAIN |