author | Edouard Tisserant |
Thu, 22 Sep 2011 17:35:29 +0200 | |
changeset 615 | 72bc3e53a1fa |
parent 446 | 1edde533db19 |
child 616 | b9271faec96e |
permissions | -rw-r--r-- |
321 | 1 |
/** |
2 |
* 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 |
|
14 |
#include <native/task.h> |
|
15 |
#include <native/timer.h> |
|
16 |
#include <native/mutex.h> |
|
17 |
#include <native/sem.h> |
|
342 | 18 |
#include <native/pipe.h> |
321 | 19 |
|
20 |
unsigned int PLC_state = 0; |
|
615 | 21 |
#define PLC_STATE_TASK_CREATED 1 |
22 |
#define PLC_STATE_DEBUG_FILE_OPENED 2 |
|
23 |
#define PLC_STATE_DEBUG_PIPE_CREATED 4 |
|
24 |
#define PLC_STATE_PYTHON_FILE_OPENED 8 |
|
25 |
#define PLC_STATE_PYTHON_PIPE_CREATED 16 |
|
26 |
#define PLC_STATE_WAITDEBUG_FILE_OPENED 32 |
|
27 |
#define PLC_STATE_WAITDEBUG_PIPE_CREATED 64 |
|
28 |
#define PLC_STATE_WAITPYTHON_FILE_OPENED 128 |
|
29 |
#define PLC_STATE_WAITPYTHON_PIPE_CREATED 256 |
|
30 |
||
31 |
#define WAITDEBUG_PIPE_DEVICE "/dev/rtp0" |
|
32 |
#define WAITDEBUG_PIPE_MINOR 0 |
|
33 |
#define DEBUG_PIPE_DEVICE "/dev/rtp1" |
|
34 |
#define DEBUG_PIPE_MINOR 1 |
|
35 |
#define WAITPYTHON_PIPE_DEVICE "/dev/rtp2" |
|
36 |
#define WAITPYTHON_PIPE_MINOR 2 |
|
37 |
#define PYTHON_PIPE_DEVICE "/dev/rtp3" |
|
38 |
#define PYTHON_PIPE_MINOR 3 |
|
39 |
#define PIPE_SIZE 1 |
|
321 | 40 |
|
41 |
/* provided by POUS.C */ |
|
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
|
42 |
extern unsigned long common_ticktime__; |
321 | 43 |
|
44 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
45 |
{ |
|
46 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
47 |
} |
|
48 |
||
49 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
50 |
{ |
|
51 |
RTIME current_time = rt_timer_read(); |
|
52 |
CURRENT_TIME->tv_sec = current_time / 1000000000; |
|
53 |
CURRENT_TIME->tv_nsec = current_time % 1000000000; |
|
54 |
} |
|
55 |
||
56 |
RT_TASK PLC_task; |
|
342 | 57 |
RT_PIPE WaitDebug_pipe; |
615 | 58 |
RT_PIPE WaitPython_pipe; |
59 |
RT_PIPE Debug_pipe; |
|
60 |
RT_PIPE Python_pipe; |
|
61 |
int WaitDebug_pipe_fd; |
|
62 |
int WaitPython_pipe_fd; |
|
63 |
int Debug_pipe_fd; |
|
64 |
int Python_pipe_fd; |
|
65 |
||
321 | 66 |
int PLC_shutdown = 0; |
67 |
||
615 | 68 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
321 | 69 |
{ |
70 |
RTIME current_time = rt_timer_read(); |
|
71 |
rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period)); |
|
72 |
} |
|
73 |
||
74 |
void PLC_task_proc(void *arg) |
|
75 |
{ |
|
76 |
PLC_SetTimer(Ttick, Ttick); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
77 |
|
321 | 78 |
while (1) { |
79 |
PLC_GetTime(&__CURRENT_TIME); |
|
80 |
__run(); |
|
81 |
if (PLC_shutdown) break; |
|
82 |
rt_task_wait_period(NULL); |
|
83 |
} |
|
84 |
} |
|
85 |
||
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
|
86 |
static unsigned long __debug_tick; |
321 | 87 |
|
88 |
void PLC_cleanup_all(void) |
|
89 |
{ |
|
90 |
if (PLC_state & PLC_STATE_TASK_CREATED) { |
|
91 |
rt_task_delete(&PLC_task); |
|
92 |
PLC_state &= ~PLC_STATE_TASK_CREATED; |
|
93 |
} |
|
94 |
||
615 | 95 |
if (PLC_state & PLC_STATE_WAITDEBUG_PIPE_CREATED) { |
96 |
rt_pipe_delete(&WaitDebug_pipe); |
|
97 |
PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
98 |
} |
|
99 |
||
100 |
if (PLC_state & PLC_STATE_WAITDEBUG_FILE_OPENED) { |
|
101 |
close(WaitDebug_pipe_fd); |
|
102 |
PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
103 |
} |
|
104 |
||
105 |
if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) { |
|
106 |
rt_pipe_delete(&WaitPython_pipe); |
|
107 |
PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
108 |
} |
|
109 |
||
110 |
if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) { |
|
111 |
close(WaitPython_pipe_fd); |
|
112 |
PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
321 | 113 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
114 |
|
342 | 115 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) { |
615 | 116 |
rt_pipe_delete(&Debug_pipe); |
342 | 117 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
118 |
} |
|
119 |
||
120 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
|
615 | 121 |
close(Debug_pipe_fd); |
342 | 122 |
PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED; |
321 | 123 |
} |
124 |
||
615 | 125 |
if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) { |
126 |
rt_pipe_delete(&Python_pipe); |
|
127 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
|
128 |
} |
|
129 |
||
130 |
if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) { |
|
131 |
close(Python_pipe_fd); |
|
132 |
PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED; |
|
133 |
} |
|
134 |
||
321 | 135 |
} |
136 |
||
137 |
int stopPLC() |
|
138 |
{ |
|
139 |
PLC_shutdown = 1; |
|
140 |
/* Stop the PLC */ |
|
141 |
PLC_SetTimer(0, 0); |
|
342 | 142 |
__cleanup(); |
321 | 143 |
PLC_cleanup_all(); |
144 |
__debug_tick = -1; |
|
615 | 145 |
return 0; |
321 | 146 |
} |
147 |
||
148 |
// |
|
149 |
void catch_signal(int sig) |
|
150 |
{ |
|
151 |
stopPLC(); |
|
152 |
// signal(SIGTERM, catch_signal); |
|
153 |
signal(SIGINT, catch_signal); |
|
154 |
printf("Got Signal %d\n",sig); |
|
155 |
exit(0); |
|
156 |
} |
|
157 |
||
158 |
#define max_val(a,b) ((a>b)?a:b) |
|
159 |
int startPLC(int argc,char **argv) |
|
160 |
{ |
|
161 |
signal(SIGINT, catch_signal); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
162 |
|
615 | 163 |
/* no memory swapping for that process */ |
321 | 164 |
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
|
165 |
|
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
|
166 |
/* Define Ttick to 1ms if common_ticktime not defined */ |
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
|
167 |
Ttick = common_ticktime__?common_ticktime__:1000000; |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
168 |
|
615 | 169 |
/*** RT Pipes creation and opening ***/ |
170 |
/* create Debug_pipe */ |
|
171 |
if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE)) |
|
172 |
goto error; |
|
173 |
PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED; |
|
174 |
||
175 |
/* open Debug_pipe*/ |
|
176 |
if((Debug_pipe_fd = open(DEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
177 |
PLC_state |= PLC_STATE_DEBUG_FILE_OPENED; |
|
178 |
||
179 |
/* create Python_pipe */ |
|
180 |
if(rt_pipe_create(&Python_pipe, "Python_pipe", PYTHON_PIPE_MINOR, PIPE_SIZE)) |
|
181 |
goto error; |
|
182 |
PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED; |
|
183 |
||
184 |
/* open Python_pipe*/ |
|
185 |
if((Python_pipe_fd = open(PYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
186 |
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
|
187 |
|
342 | 188 |
/* create WaitDebug_pipe */ |
615 | 189 |
if(rt_pipe_create(&WaitDebug_pipe, "Debug_pipe", WAITDEBUG_PIPE_MINOR, PIPE_SIZE)) |
190 |
goto error; |
|
191 |
PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
342 | 192 |
|
193 |
/* open WaitDebug_pipe*/ |
|
615 | 194 |
if((WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
195 |
PLC_state |= PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
196 |
||
197 |
/* create WaitPython_pipe */ |
|
198 |
if(rt_pipe_create(&WaitPython_pipe, "Python_pipe", WAITPYTHON_PIPE_MINOR, PIPE_SIZE)) |
|
342 | 199 |
goto error; |
615 | 200 |
PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED; |
201 |
||
202 |
/* open WaitPython_pipe*/ |
|
203 |
if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error; |
|
204 |
PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
205 |
||
206 |
/*** create PLC task ***/ |
|
207 |
if(rt_task_create(&PLC_task, "PLC_task", 0, 50, 0)) goto error; |
|
321 | 208 |
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
|
209 |
|
615 | 210 |
if(__init(argc,argv)) goto error; |
211 |
||
212 |
/* start PLC task */ |
|
213 |
if(rt_task_start(&PLC_task, &PLC_task_proc, NULL)) goto error; |
|
321 | 214 |
|
215 |
return 0; |
|
216 |
||
217 |
error: |
|
218 |
PLC_cleanup_all(); |
|
219 |
return 1; |
|
220 |
} |
|
221 |
||
615 | 222 |
#define DEBUG_FREE 0 |
223 |
#define DEBUG_BUSY 1 |
|
224 |
static long debug_state = DEBUG_FREE; |
|
225 |
||
321 | 226 |
int TryEnterDebugSection(void) |
227 |
{ |
|
615 | 228 |
long old_debug_state = AtomicCompareExchange( |
229 |
&debug_state, |
|
230 |
DEBUG_FREE, |
|
231 |
DEBUG_BUSY); |
|
232 |
return old_debug_state == DEBUG_FREE; |
|
233 |
} |
|
234 |
||
235 |
#define DEBUG_UNLOCK 1 |
|
321 | 236 |
void LeaveDebugSection(void) |
237 |
{ |
|
615 | 238 |
if(AtomicCompareExchange( &debug_state, |
239 |
DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){ |
|
240 |
char msg = DEBUG_UNLOCK; |
|
241 |
/* signal to NRT for wakeup */ |
|
242 |
rt_pipe_write(&Debug_pipe, &msg, sizeof(msg), P_NORMAL); |
|
243 |
} |
|
321 | 244 |
} |
245 |
||
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
|
246 |
extern unsigned long __tick; |
615 | 247 |
|
248 |
#define DEBUG_PENDING_DATA 1 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
249 |
int WaitDebugData(unsigned long *tick) |
321 | 250 |
{ |
615 | 251 |
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
|
252 |
int res; |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
253 |
*tick = __debug_tick; |
321 | 254 |
/* Wait signal from PLC thread */ |
615 | 255 |
res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd)); |
256 |
if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA) |
|
257 |
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
|
258 |
return -1; |
321 | 259 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
260 |
|
321 | 261 |
/* Called by PLC thread when debug_publish finished |
262 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
263 |
void InitiateDebugTransfer() |
|
264 |
{ |
|
615 | 265 |
char msg = DEBUG_PENDING_DATA; |
321 | 266 |
/* remember tick */ |
267 |
__debug_tick = __tick; |
|
268 |
/* signal debugger thread it can read data */ |
|
615 | 269 |
rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL); |
270 |
} |
|
271 |
||
272 |
int suspendDebug(int disable) |
|
273 |
{ |
|
274 |
char cmd = DEBUG_UNLOCK; |
|
275 |
while(AtomicCompareExchange( |
|
276 |
&debug_state, |
|
277 |
DEBUG_FREE, |
|
278 |
DEBUG_BUSY) != DEBUG_FREE && |
|
279 |
cmd == DEBUG_UNLOCK){ |
|
280 |
if(read(Debug_pipe_fd, &cmd, sizeof(cmd)) == sizeof(cmd)){ |
|
281 |
return -1; |
|
282 |
} |
|
283 |
} |
|
284 |
__DEBUG = !disable; |
|
285 |
return 0; |
|
321 | 286 |
} |
287 |
||
288 |
void resumeDebug(void) |
|
289 |
{ |
|
615 | 290 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
291 |
} |
|
292 |
||
293 |
#define PYTHON_PENDING_COMMAND 1 |
|
294 |
||
295 |
#define PYTHON_FREE 0 |
|
296 |
#define PYTHON_BUSY 1 |
|
297 |
static long python_state = PYTHON_FREE; |
|
298 |
||
321 | 299 |
int WaitPythonCommands(void) |
615 | 300 |
{ |
301 |
char cmd; |
|
321 | 302 |
/* Wait signal from PLC thread */ |
615 | 303 |
if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){ |
304 |
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
|
305 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
306 |
return -1; |
321 | 307 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
308 |
|
321 | 309 |
/* Called by PLC thread on each new python command*/ |
310 |
void UnBlockPythonCommands(void) |
|
311 |
{ |
|
615 | 312 |
char msg = PYTHON_PENDING_COMMAND; |
313 |
rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL); |
|
321 | 314 |
} |
315 |
||
316 |
int TryLockPython(void) |
|
317 |
{ |
|
615 | 318 |
return AtomicCompareExchange( |
319 |
&python_state, |
|
320 |
PYTHON_FREE, |
|
321 |
PYTHON_BUSY) == PYTHON_FREE; |
|
322 |
} |
|
323 |
||
324 |
#define UNLOCK_PYTHON 1 |
|
325 |
void LockPython(void) |
|
326 |
{ |
|
327 |
char cmd = UNLOCK_PYTHON; |
|
328 |
while(AtomicCompareExchange( |
|
329 |
&python_state, |
|
330 |
PYTHON_FREE, |
|
331 |
PYTHON_BUSY) != PYTHON_FREE && |
|
332 |
cmd == UNLOCK_PYTHON){ |
|
333 |
read(Python_pipe_fd, &cmd, sizeof(cmd)); |
|
334 |
} |
|
321 | 335 |
} |
336 |
||
337 |
void UnLockPython(void) |
|
338 |
{ |
|
615 | 339 |
if(AtomicCompareExchange( |
340 |
&python_state, |
|
341 |
PYTHON_BUSY, |
|
342 |
PYTHON_FREE) == PYTHON_BUSY){ |
|
343 |
if(rt_task_self()){/*is that the real time task ?*/ |
|
344 |
char cmd = UNLOCK_PYTHON; |
|
345 |
rt_pipe_write(&Python_pipe, &cmd, sizeof(cmd), P_NORMAL); |
|
346 |
}/* otherwise, no signaling from non real time */ |
|
347 |
} /* as plc does not wait for lock. */ |
|
348 |
} |
|
349 |