author | Edouard Tisserant |
Mon, 28 Mar 2011 11:42:00 +0200 | |
changeset 587 | c6354f7fe26e |
parent 446 | 1edde533db19 |
child 615 | 72bc3e53a1fa |
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> |
|
6 |
#include <string.h> |
|
7 |
#include <time.h> |
|
8 |
#include <signal.h> |
|
9 |
#include <stdlib.h> |
|
10 |
#include <sys/mman.h> |
|
342 | 11 |
#include <sys/fcntl.h> |
321 | 12 |
|
13 |
#include <native/task.h> |
|
14 |
#include <native/timer.h> |
|
15 |
#include <native/mutex.h> |
|
16 |
#include <native/sem.h> |
|
342 | 17 |
#include <native/pipe.h> |
321 | 18 |
|
19 |
unsigned int PLC_state = 0; |
|
20 |
#define PLC_STATE_TASK_CREATED 1 |
|
21 |
#define PLC_STATE_PYTHON_MUTEX_CREATED 2 |
|
22 |
#define PLC_STATE_PYTHON_WAIT_SEM_CREATED 4 |
|
23 |
#define PLC_STATE_DEBUG_MUTEX_CREATED 8 |
|
342 | 24 |
#define PLC_STATE_DEBUG_FILE_OPENED 16 |
25 |
#define PLC_STATE_DEBUG_PIPE_CREATED 32 |
|
26 |
||
27 |
#define WAITDEBUG_PIPE_DEVICE "/dev/rtp0" |
|
28 |
#define WAITDEBUG_PIPE_MINOR 0 |
|
29 |
#define WAITDEBUG_PIPE_SIZE 500 |
|
321 | 30 |
|
31 |
/* 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
|
32 |
extern unsigned long common_ticktime__; |
321 | 33 |
|
34 |
long AtomicCompareExchange(long* atomicvar,long compared, long exchange) |
|
35 |
{ |
|
36 |
return __sync_val_compare_and_swap(atomicvar, compared, exchange); |
|
37 |
} |
|
38 |
||
39 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME) |
|
40 |
{ |
|
41 |
RTIME current_time = rt_timer_read(); |
|
42 |
CURRENT_TIME->tv_sec = current_time / 1000000000; |
|
43 |
CURRENT_TIME->tv_nsec = current_time % 1000000000; |
|
44 |
} |
|
45 |
||
46 |
RT_TASK PLC_task; |
|
342 | 47 |
RT_PIPE WaitDebug_pipe; |
48 |
RT_TASK SuspendDebug_task; |
|
49 |
RT_TASK ResumeDebug_task; |
|
321 | 50 |
RT_TASK WaitPythonCommand_task; |
51 |
RT_TASK UnLockPython_task; |
|
52 |
RT_TASK LockPython_task; |
|
53 |
int PLC_shutdown = 0; |
|
54 |
||
342 | 55 |
int WaitDebug_pipe_fd = -1; |
56 |
||
321 | 57 |
void PLC_SetTimer(long long next, long long period) |
58 |
{ |
|
59 |
RTIME current_time = rt_timer_read(); |
|
60 |
rt_task_set_periodic(&PLC_task, current_time + next, rt_timer_ns2ticks(period)); |
|
61 |
} |
|
62 |
||
63 |
void PLC_task_proc(void *arg) |
|
64 |
{ |
|
65 |
PLC_SetTimer(Ttick, Ttick); |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
66 |
|
321 | 67 |
while (1) { |
68 |
PLC_GetTime(&__CURRENT_TIME); |
|
69 |
__run(); |
|
70 |
if (PLC_shutdown) break; |
|
71 |
rt_task_wait_period(NULL); |
|
72 |
} |
|
73 |
} |
|
74 |
||
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
|
75 |
static unsigned long __debug_tick; |
321 | 76 |
|
77 |
RT_SEM python_wait_sem; |
|
78 |
RT_MUTEX python_mutex; |
|
79 |
RT_MUTEX debug_mutex; |
|
80 |
||
81 |
void PLC_cleanup_all(void) |
|
82 |
{ |
|
83 |
if (PLC_state & PLC_STATE_TASK_CREATED) { |
|
84 |
rt_task_delete(&PLC_task); |
|
85 |
PLC_state &= ~PLC_STATE_TASK_CREATED; |
|
86 |
} |
|
87 |
||
88 |
if (PLC_state & PLC_STATE_PYTHON_WAIT_SEM_CREATED) { |
|
342 | 89 |
rt_sem_v(&python_wait_sem); |
321 | 90 |
rt_sem_delete(&python_wait_sem); |
91 |
PLC_state &= ~ PLC_STATE_PYTHON_WAIT_SEM_CREATED; |
|
92 |
} |
|
93 |
||
94 |
if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) { |
|
95 |
rt_mutex_delete(&python_mutex); |
|
96 |
PLC_state &= ~ PLC_STATE_PYTHON_MUTEX_CREATED; |
|
97 |
} |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
98 |
|
342 | 99 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) { |
100 |
rt_pipe_delete(&WaitDebug_pipe); |
|
101 |
PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED; |
|
102 |
} |
|
103 |
||
104 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
|
105 |
close(WaitDebug_pipe_fd); |
|
106 |
PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED; |
|
321 | 107 |
} |
108 |
||
109 |
if (PLC_state & PLC_STATE_DEBUG_MUTEX_CREATED) { |
|
110 |
rt_mutex_delete(&debug_mutex); |
|
111 |
PLC_state &= ~ PLC_STATE_DEBUG_MUTEX_CREATED; |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
int stopPLC() |
|
116 |
{ |
|
117 |
PLC_shutdown = 1; |
|
118 |
/* Stop the PLC */ |
|
119 |
PLC_SetTimer(0, 0); |
|
342 | 120 |
__cleanup(); |
321 | 121 |
PLC_cleanup_all(); |
122 |
__debug_tick = -1; |
|
123 |
} |
|
124 |
||
125 |
// |
|
126 |
void catch_signal(int sig) |
|
127 |
{ |
|
128 |
stopPLC(); |
|
129 |
// signal(SIGTERM, catch_signal); |
|
130 |
signal(SIGINT, catch_signal); |
|
131 |
printf("Got Signal %d\n",sig); |
|
132 |
exit(0); |
|
133 |
} |
|
134 |
||
135 |
#define max_val(a,b) ((a>b)?a:b) |
|
136 |
int startPLC(int argc,char **argv) |
|
137 |
{ |
|
138 |
int ret = 0; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
139 |
|
321 | 140 |
signal(SIGINT, catch_signal); |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
141 |
|
321 | 142 |
/* ne-memory-swapping for this program */ |
143 |
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
|
144 |
|
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
|
145 |
/* 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
|
146 |
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
|
147 |
|
321 | 148 |
/* create python_wait_sem */ |
149 |
ret = rt_sem_create(&python_wait_sem, "python_wait_sem", 0, S_FIFO); |
|
150 |
if (ret) goto error; |
|
151 |
PLC_state |= PLC_STATE_PYTHON_WAIT_SEM_CREATED; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
152 |
|
321 | 153 |
/* create python_mutex */ |
154 |
ret = rt_mutex_create(&python_mutex, "python_mutex"); |
|
155 |
if (ret) goto error; |
|
156 |
PLC_state |= PLC_STATE_PYTHON_MUTEX_CREATED; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
157 |
|
342 | 158 |
/* create WaitDebug_pipe */ |
159 |
ret = rt_pipe_create(&WaitDebug_pipe, "WaitDebug_pipe", WAITDEBUG_PIPE_MINOR, |
|
160 |
WAITDEBUG_PIPE_SIZE * sizeof(char)); |
|
161 |
if (ret) goto error; |
|
162 |
PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED; |
|
163 |
||
164 |
/* open WaitDebug_pipe*/ |
|
165 |
WaitDebug_pipe_fd = open(WAITDEBUG_PIPE_DEVICE, O_RDWR); |
|
166 |
if (WaitDebug_pipe_fd == -1) { |
|
167 |
ret = -EBADF; |
|
168 |
goto error; |
|
169 |
} |
|
170 |
PLC_state |= PLC_STATE_DEBUG_FILE_OPENED; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
171 |
|
321 | 172 |
/* create debug_mutex */ |
173 |
ret = rt_mutex_create(&debug_mutex, "debug_mutex"); |
|
174 |
if (ret) goto error; |
|
175 |
PLC_state |= PLC_STATE_DEBUG_MUTEX_CREATED; |
|
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
176 |
|
321 | 177 |
/* create can_driver_task */ |
178 |
ret = rt_task_create(&PLC_task, "PLC_task", 0, 50, 0); |
|
179 |
if (ret) goto error; |
|
180 |
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
|
181 |
|
321 | 182 |
ret = __init(argc,argv); |
183 |
if (ret) goto error; |
|
184 |
||
185 |
/* start can_driver_task */ |
|
186 |
ret = rt_task_start(&PLC_task, &PLC_task_proc, NULL); |
|
187 |
if (ret) goto error; |
|
188 |
||
189 |
return 0; |
|
190 |
||
191 |
error: |
|
192 |
PLC_cleanup_all(); |
|
193 |
return 1; |
|
194 |
} |
|
195 |
||
196 |
int TryEnterDebugSection(void) |
|
197 |
{ |
|
198 |
return rt_mutex_acquire(&debug_mutex, TM_NONBLOCK) == 0; |
|
199 |
} |
|
200 |
||
201 |
void LeaveDebugSection(void) |
|
202 |
{ |
|
203 |
rt_mutex_release(&debug_mutex); |
|
204 |
} |
|
205 |
||
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
|
206 |
extern unsigned long __tick; |
321 | 207 |
/* from plc_debugger.c */ |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
208 |
int WaitDebugData(unsigned long *tick) |
321 | 209 |
{ |
342 | 210 |
char message; |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
211 |
int res; |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
212 |
*tick = __debug_tick; |
321 | 213 |
/* Wait signal from PLC thread */ |
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
214 |
if (PLC_state & PLC_STATE_DEBUG_FILE_OPENED) { |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
215 |
res = read(WaitDebug_pipe_fd, &message, sizeof(char)); |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
216 |
if (res == sizeof(char)) |
446
1edde533db19
Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents:
397
diff
changeset
|
217 |
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
|
218 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
219 |
return -1; |
321 | 220 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
221 |
|
321 | 222 |
/* Called by PLC thread when debug_publish finished |
223 |
* This is supposed to unlock debugger thread in WaitDebugData*/ |
|
224 |
void InitiateDebugTransfer() |
|
225 |
{ |
|
342 | 226 |
char message = 1; |
321 | 227 |
/* remember tick */ |
228 |
__debug_tick = __tick; |
|
229 |
/* signal debugger thread it can read data */ |
|
342 | 230 |
if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) |
231 |
rt_pipe_write(&WaitDebug_pipe, &message, sizeof(char), P_NORMAL); |
|
321 | 232 |
} |
233 |
||
234 |
void suspendDebug(void) |
|
235 |
{ |
|
236 |
__DEBUG = 0; |
|
342 | 237 |
if (PLC_state & PLC_STATE_DEBUG_MUTEX_CREATED) { |
238 |
rt_task_shadow(&SuspendDebug_task, "SuspendDebug_task", 0, 0); |
|
239 |
/* Prevent PLC to enter debug code */ |
|
240 |
rt_mutex_acquire(&debug_mutex, TM_INFINITE); |
|
241 |
} |
|
321 | 242 |
} |
243 |
||
244 |
void resumeDebug(void) |
|
245 |
{ |
|
246 |
__DEBUG = 1; |
|
342 | 247 |
if (PLC_state & PLC_STATE_DEBUG_MUTEX_CREATED) { |
248 |
rt_task_shadow(&ResumeDebug_task, "ResumeDebug_task", 0, 0); |
|
249 |
/* Let PLC enter debug code */ |
|
250 |
rt_mutex_release(&debug_mutex); |
|
251 |
} |
|
321 | 252 |
} |
253 |
||
254 |
/* from plc_python.c */ |
|
255 |
int WaitPythonCommands(void) |
|
256 |
{ |
|
257 |
/* Wait signal from PLC thread */ |
|
342 | 258 |
if (PLC_state & PLC_STATE_PYTHON_WAIT_SEM_CREATED) { |
259 |
rt_task_shadow(&WaitPythonCommand_task, "WaitPythonCommand_task", 0, 0); |
|
345
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
260 |
return rt_sem_p(&python_wait_sem, TM_INFINITE); |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
261 |
} |
a3520d75a722
get the WaitPythonCommands return (semaphore error code) to quit python_iterator loop when semaphore is destroyed
greg
parents:
342
diff
changeset
|
262 |
return -1; |
321 | 263 |
} |
336
ae3488c79283
Fixed bug : Segmentation fault or locks when stop PLC if no CAN network.
greg
parents:
321
diff
changeset
|
264 |
|
321 | 265 |
/* Called by PLC thread on each new python command*/ |
266 |
void UnBlockPythonCommands(void) |
|
267 |
{ |
|
268 |
/* signal debugger thread it can read data */ |
|
269 |
rt_sem_v(&python_wait_sem); |
|
270 |
} |
|
271 |
||
272 |
int TryLockPython(void) |
|
273 |
{ |
|
274 |
return rt_mutex_acquire(&python_mutex, TM_NONBLOCK) == 0; |
|
275 |
} |
|
276 |
||
277 |
void UnLockPython(void) |
|
278 |
{ |
|
342 | 279 |
if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) { |
280 |
rt_task_shadow(&UnLockPython_task, "UnLockPython_task", 0, 0); |
|
281 |
rt_mutex_release(&python_mutex); |
|
282 |
} |
|
321 | 283 |
} |
284 |
||
285 |
void LockPython(void) |
|
286 |
{ |
|
342 | 287 |
if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) { |
288 |
rt_task_shadow(&LockPython_task, "LockPython_task", 0, 0); |
|
289 |
rt_mutex_acquire(&python_mutex, TM_INFINITE); |
|
290 |
} |
|
291 |
} |