author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 21 Feb 2019 10:58:37 +0300 | |
changeset 2513 | 1ee326651a74 |
parent 1990 | 2e0fbdd152de |
child 2820 | d9b5303d43dc |
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 |
|
29 |
||
30 |
#define WAITDEBUG_PIPE_DEVICE "/dev/rtp0" |
|
31 |
#define WAITDEBUG_PIPE_MINOR 0 |
|
32 |
#define DEBUG_PIPE_DEVICE "/dev/rtp1" |
|
33 |
#define DEBUG_PIPE_MINOR 1 |
|
34 |
#define WAITPYTHON_PIPE_DEVICE "/dev/rtp2" |
|
35 |
#define WAITPYTHON_PIPE_MINOR 2 |
|
36 |
#define PYTHON_PIPE_DEVICE "/dev/rtp3" |
|
37 |
#define PYTHON_PIPE_MINOR 3 |
|
38 |
#define PIPE_SIZE 1 |
|
321 | 39 |
|
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
|
40 |
// 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
|
41 |
|
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
|
42 |
#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
|
43 |
#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
|
44 |
|
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 |
#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
|
46 |
|
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 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
|
48 |
#define DEBUG_UNLOCK 1 |
321 | 49 |
|
50 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
51 |
{ |
|
52 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
53 |
} |
|
954 | 54 |
long long AtomicCompareExchange64(long long* atomicvar, long long compared, long long exchange) |
55 |
{ |
|
56 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
57 |
} |
|
321 | 58 |
|
59 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
60 |
{ |
|
61 |
RTIME current_time = rt_timer_read(); |
|
62 |
CURRENT_TIME->tv_sec = current_time / 1000000000; |
|
63 |
CURRENT_TIME->tv_nsec = current_time % 1000000000; |
|
64 |
} |
|
65 |
||
66 |
RT_TASK PLC_task; |
|
342 | 67 |
RT_PIPE WaitDebug_pipe; |
615 | 68 |
RT_PIPE WaitPython_pipe; |
69 |
RT_PIPE Debug_pipe; |
|
70 |
RT_PIPE Python_pipe; |
|
71 |
int WaitDebug_pipe_fd; |
|
72 |
int WaitPython_pipe_fd; |
|
73 |
int Debug_pipe_fd; |
|
74 |
int Python_pipe_fd; |
|
75 |
||
321 | 76 |
int PLC_shutdown = 0; |
77 |
||
615 | 78 |
void PLC_SetTimer(unsigned long long next, unsigned long long period) |
321 | 79 |
{ |
80 |
RTIME current_time = rt_timer_read(); |
|
81 |
rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period)); |
|
82 |
} |
|
83 |
||
84 |
void PLC_task_proc(void *arg) |
|
85 |
{ |
|
1428
e14003eb4d42
Simplified use of runtime's global variable __common_ticktime accross extensions.
Edouard Tisserant
parents:
985
diff
changeset
|
86 |
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
|
87 |
|
695 | 88 |
while (!PLC_shutdown) { |
321 | 89 |
PLC_GetTime(&__CURRENT_TIME); |
90 |
__run(); |
|
91 |
if (PLC_shutdown) break; |
|
92 |
rt_task_wait_period(NULL); |
|
93 |
} |
|
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
|
94 |
/* 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
|
95 |
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
|
96 |
{ |
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
|
97 |
/* 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
|
98 |
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
|
99 |
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
|
100 |
} |
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 |
{ |
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 |
/* 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
|
103 |
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
|
104 |
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
|
105 |
} |
321 | 106 |
} |
107 |
||
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
|
108 |
static unsigned long __debug_tick; |
321 | 109 |
|
110 |
void PLC_cleanup_all(void) |
|
111 |
{ |
|
112 |
if (PLC_state & PLC_STATE_TASK_CREATED) { |
|
113 |
rt_task_delete(&PLC_task); |
|
114 |
PLC_state &= ~PLC_STATE_TASK_CREATED; |
|
115 |
} |
|
116 |
||
615 | 117 |
if (PLC_state & PLC_STATE_WAITDEBUG_PIPE_CREATED) { |
118 |
rt_pipe_delete(&WaitDebug_pipe); |
|
119 |
PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED; |
|
120 |
} |
|
121 |
||
122 |
if (PLC_state & PLC_STATE_WAITDEBUG_FILE_OPENED) { |
|
123 |
close(WaitDebug_pipe_fd); |
|
124 |
PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED; |
|
125 |
} |
|
126 |
||
127 |
if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) { |
|
128 |
rt_pipe_delete(&WaitPython_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
129 |
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
|
130 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
131 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
132 |
if (PLC_state & PLC_STATE_WAITPYTHON_FILE_OPENED) { |
615 | 133 |
close(WaitPython_pipe_fd); |
134 |
PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED; |
|
321 | 135 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
136 |
|
342 | 137 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) { |
615 | 138 |
rt_pipe_delete(&Debug_pipe); |
342 | 139 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
140 |
} |
|
141 |
||
142 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
|
615 | 143 |
close(Debug_pipe_fd); |
342 | 144 |
PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED; |
321 | 145 |
} |
146 |
||
615 | 147 |
if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) { |
148 |
rt_pipe_delete(&Python_pipe); |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
149 |
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
|
150 |
} |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
151 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
152 |
if (PLC_state & PLC_STATE_PYTHON_FILE_OPENED) { |
615 | 153 |
close(Python_pipe_fd); |
154 |
PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED; |
|
155 |
} |
|
156 |
||
321 | 157 |
} |
158 |
||
159 |
int stopPLC() |
|
160 |
{ |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
161 |
/* Stop the PLC */ |
321 | 162 |
PLC_shutdown = 1; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
163 |
|
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
164 |
/* Wait until PLC task stops */ |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
165 |
rt_task_join(&PLC_task); |
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 |
PLC_cleanup_all(); |
342 | 168 |
__cleanup(); |
321 | 169 |
__debug_tick = -1; |
615 | 170 |
return 0; |
321 | 171 |
} |
172 |
||
173 |
// |
|
174 |
void catch_signal(int sig) |
|
175 |
{ |
|
176 |
stopPLC(); |
|
177 |
// signal(SIGTERM, catch_signal); |
|
178 |
signal(SIGINT, catch_signal); |
|
179 |
printf("Got Signal %d\n",sig); |
|
180 |
exit(0); |
|
181 |
} |
|
182 |
||
1981 | 183 |
#define _startPLCLog(text) \ |
184 |
{\ |
|
185 |
char mstr[] = text;\ |
|
186 |
LogMessage(LOG_CRITICAL, mstr, sizeof(mstr));\ |
|
187 |
goto error;\ |
|
188 |
} |
|
189 |
||
190 |
#define FO "Failed opening " |
|
191 |
||
321 | 192 |
#define max_val(a,b) ((a>b)?a:b) |
193 |
int startPLC(int argc,char **argv) |
|
194 |
{ |
|
195 |
signal(SIGINT, catch_signal); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
196 |
|
615 | 197 |
/* no memory swapping for that process */ |
321 | 198 |
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
|
199 |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
200 |
PLC_shutdown = 0; |
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
201 |
|
615 | 202 |
/*** RT Pipes creation and opening ***/ |
203 |
/* create Debug_pipe */ |
|
1980 | 204 |
if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 205 |
_startPLCLog(FO "Debug_pipe real-time end"); |
615 | 206 |
PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED; |
207 |
||
208 |
/* open Debug_pipe*/ |
|
1981 | 209 |
if((Debug_pipe_fd = open(DEBUG_PIPE_DEVICE, O_RDWR)) == -1) |
210 |
_startPLCLog(FO DEBUG_PIPE_DEVICE); |
|
615 | 211 |
PLC_state |= PLC_STATE_DEBUG_FILE_OPENED; |
212 |
||
213 |
/* create Python_pipe */ |
|
1980 | 214 |
if(rt_pipe_create(&Python_pipe, "Python_pipe", PYTHON_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 215 |
_startPLCLog(FO "Python_pipe real-time end"); |
615 | 216 |
PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED; |
217 |
||
218 |
/* open Python_pipe*/ |
|
1981 | 219 |
if((Python_pipe_fd = open(PYTHON_PIPE_DEVICE, O_RDWR)) == -1) |
220 |
_startPLCLog(FO PYTHON_PIPE_DEVICE); |
|
615 | 221 |
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
|
222 |
|
342 | 223 |
/* create WaitDebug_pipe */ |
1980 | 224 |
if(rt_pipe_create(&WaitDebug_pipe, "WaitDebug_pipe", WAITDEBUG_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 225 |
_startPLCLog(FO "WaitDebug_pipe real-time end"); |
615 | 226 |
PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED; |
342 | 227 |
|
228 |
/* open WaitDebug_pipe*/ |
|
1981 | 229 |
if((WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR)) == -1) |
230 |
_startPLCLog(FO WAITDEBUG_PIPE_DEVICE); |
|
615 | 231 |
PLC_state |= PLC_STATE_WAITDEBUG_FILE_OPENED; |
232 |
||
233 |
/* create WaitPython_pipe */ |
|
1980 | 234 |
if(rt_pipe_create(&WaitPython_pipe, "WaitPython_pipe", WAITPYTHON_PIPE_MINOR, PIPE_SIZE) < 0) |
1981 | 235 |
_startPLCLog(FO "WaitPython_pipe real-time end"); |
615 | 236 |
PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED; |
237 |
||
238 |
/* open WaitPython_pipe*/ |
|
1981 | 239 |
if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) |
240 |
_startPLCLog(FO WAITPYTHON_PIPE_DEVICE); |
|
615 | 241 |
PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED; |
242 |
||
243 |
/*** create PLC task ***/ |
|
1981 | 244 |
if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE)) |
245 |
_startPLCLog("Failed creating PLC task"); |
|
321 | 246 |
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
|
247 |
|
615 | 248 |
if(__init(argc,argv)) goto error; |
249 |
||
250 |
/* start PLC task */ |
|
1981 | 251 |
if(rt_task_start(&PLC_task, &PLC_task_proc, NULL)) |
252 |
_startPLCLog("Failed starting PLC task"); |
|
321 | 253 |
|
254 |
return 0; |
|
255 |
||
256 |
error: |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
257 |
|
321 | 258 |
PLC_cleanup_all(); |
259 |
return 1; |
|
260 |
} |
|
261 |
||
615 | 262 |
#define DEBUG_FREE 0 |
263 |
#define DEBUG_BUSY 1 |
|
264 |
static long debug_state = DEBUG_FREE; |
|
265 |
||
321 | 266 |
int TryEnterDebugSection(void) |
267 |
{ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
268 |
if(AtomicCompareExchange( |
615 | 269 |
&debug_state, |
270 |
DEBUG_FREE, |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
271 |
DEBUG_BUSY) == DEBUG_FREE){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
272 |
if(__DEBUG){ |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
273 |
return 1; |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
274 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
275 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
276 |
} |
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
277 |
return 0; |
615 | 278 |
} |
279 |
||
321 | 280 |
void LeaveDebugSection(void) |
281 |
{ |
|
615 | 282 |
if(AtomicCompareExchange( &debug_state, |
283 |
DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){ |
|
284 |
char msg = DEBUG_UNLOCK; |
|
285 |
/* signal to NRT for wakeup */ |
|
286 |
rt_pipe_write(&Debug_pipe, &msg, sizeof(msg), P_NORMAL); |
|
287 |
} |
|
321 | 288 |
} |
289 |
||
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
|
290 |
extern unsigned long __tick; |
615 | 291 |
|
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
292 |
int WaitDebugData(unsigned long *tick) |
321 | 293 |
{ |
615 | 294 |
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
|
295 |
int res; |
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
296 |
if (PLC_shutdown) return -1; |
321 | 297 |
/* Wait signal from PLC thread */ |
615 | 298 |
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
|
299 |
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
|
300 |
*tick = __debug_tick; |
615 | 301 |
return 0; |
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
302 |
} |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
303 |
return -1; |
321 | 304 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
305 |
|
321 | 306 |
/* Called by PLC thread when debug_publish finished |
307 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
308 |
void InitiateDebugTransfer() |
|
309 |
{ |
|
615 | 310 |
char msg = DEBUG_PENDING_DATA; |
321 | 311 |
/* remember tick */ |
312 |
__debug_tick = __tick; |
|
313 |
/* signal debugger thread it can read data */ |
|
615 | 314 |
rt_pipe_write(&WaitDebug_pipe, &msg, sizeof(msg), P_NORMAL); |
315 |
} |
|
316 |
||
317 |
int suspendDebug(int disable) |
|
318 |
{ |
|
319 |
char cmd = DEBUG_UNLOCK; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
320 |
if (PLC_shutdown) return -1; |
615 | 321 |
while(AtomicCompareExchange( |
322 |
&debug_state, |
|
323 |
DEBUG_FREE, |
|
324 |
DEBUG_BUSY) != DEBUG_FREE && |
|
325 |
cmd == DEBUG_UNLOCK){ |
|
616
b9271faec96e
Xenomai Fix : runs, but still fail in debug
Edouard Tisserant
parents:
615
diff
changeset
|
326 |
if(read(Debug_pipe_fd, &cmd, sizeof(cmd)) != sizeof(cmd)){ |
615 | 327 |
return -1; |
328 |
} |
|
329 |
} |
|
330 |
__DEBUG = !disable; |
|
617
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
331 |
if (disable) |
7c23fac40a2a
fixed debug with xenomai, fixed unprotected access to ___debug_tick
Edouard Tisserant
parents:
616
diff
changeset
|
332 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
615 | 333 |
return 0; |
321 | 334 |
} |
335 |
||
336 |
void resumeDebug(void) |
|
337 |
{ |
|
615 | 338 |
AtomicCompareExchange( &debug_state, DEBUG_BUSY, DEBUG_FREE); |
339 |
} |
|
340 |
||
341 |
#define PYTHON_FREE 0 |
|
342 |
#define PYTHON_BUSY 1 |
|
343 |
static long python_state = PYTHON_FREE; |
|
344 |
||
321 | 345 |
int WaitPythonCommands(void) |
615 | 346 |
{ |
347 |
char cmd; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
348 |
if (PLC_shutdown) return -1; |
321 | 349 |
/* Wait signal from PLC thread */ |
615 | 350 |
if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){ |
351 |
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
|
352 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
353 |
return -1; |
321 | 354 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
355 |
|
321 | 356 |
/* Called by PLC thread on each new python command*/ |
357 |
void UnBlockPythonCommands(void) |
|
358 |
{ |
|
615 | 359 |
char msg = PYTHON_PENDING_COMMAND; |
360 |
rt_pipe_write(&WaitPython_pipe, &msg, sizeof(msg), P_NORMAL); |
|
321 | 361 |
} |
362 |
||
363 |
int TryLockPython(void) |
|
364 |
{ |
|
615 | 365 |
return AtomicCompareExchange( |
366 |
&python_state, |
|
367 |
PYTHON_FREE, |
|
368 |
PYTHON_BUSY) == PYTHON_FREE; |
|
369 |
} |
|
370 |
||
371 |
#define UNLOCK_PYTHON 1 |
|
372 |
void LockPython(void) |
|
373 |
{ |
|
374 |
char cmd = UNLOCK_PYTHON; |
|
745
96dd6650854d
Fixing Xenomai runtime specific parts to remove segmentation fault when stopping PLC
laurent
parents:
695
diff
changeset
|
375 |
if (PLC_shutdown) return; |
615 | 376 |
while(AtomicCompareExchange( |
377 |
&python_state, |
|
378 |
PYTHON_FREE, |
|
379 |
PYTHON_BUSY) != PYTHON_FREE && |
|
380 |
cmd == UNLOCK_PYTHON){ |
|
381 |
read(Python_pipe_fd, &cmd, sizeof(cmd)); |
|
382 |
} |
|
321 | 383 |
} |
384 |
||
385 |
void UnLockPython(void) |
|
386 |
{ |
|
615 | 387 |
if(AtomicCompareExchange( |
388 |
&python_state, |
|
389 |
PYTHON_BUSY, |
|
390 |
PYTHON_FREE) == PYTHON_BUSY){ |
|
391 |
if(rt_task_self()){/*is that the real time task ?*/ |
|
392 |
char cmd = UNLOCK_PYTHON; |
|
393 |
rt_pipe_write(&Python_pipe, &cmd, sizeof(cmd), P_NORMAL); |
|
394 |
}/* otherwise, no signaling from non real time */ |
|
395 |
} /* as plc does not wait for lock. */ |
|
396 |
} |
|
397 |
||
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
|
398 |
#ifndef HAVE_RETAIN |
1717
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
399 |
int CheckRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
400 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
401 |
return 1; |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
402 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
403 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
404 |
void ValidateRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
405 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
406 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
407 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
408 |
void InValidateRetainBuffer(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
409 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
410 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
411 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
412 |
void Retain(unsigned int offset, unsigned int count, void *p) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
413 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
414 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
415 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
416 |
void Remind(unsigned int offset, unsigned int count, void *p) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
417 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
418 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
419 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
420 |
void CleanupRetain(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
421 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
422 |
} |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
423 |
|
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
424 |
void InitRetain(void) |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
425 |
{ |
a86deec92e08
fix target Xenomai missing functions
wuyangtang <wuyangtang@live.com>
parents:
1456
diff
changeset
|
426 |
} |
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
|
427 |
#endif // !HAVE_RETAIN |