targets/Xenomai/plc_Xenomai_main.c
branchsvghmi
changeset 3298 e6131894be1d
parent 3295 0375d801fff7
child 3315 5f9db9c6c69c
equal deleted inserted replaced
3297:7e59bd180bc6 3298:e6131894be1d
    16 #include <alchemy/sem.h>
    16 #include <alchemy/sem.h>
    17 #include <alchemy/pipe.h>
    17 #include <alchemy/pipe.h>
    18 
    18 
    19 unsigned int PLC_state = 0;
    19 unsigned int PLC_state = 0;
    20 #define PLC_STATE_TASK_CREATED                 1
    20 #define PLC_STATE_TASK_CREATED                 1
    21 #define PLC_STATE_DEBUG_PIPE_CREATED           2 
    21 #define PLC_STATE_DEBUG_PIPE_CREATED           2
    22 #define PLC_STATE_PYTHON_PIPE_CREATED          8   
    22 #define PLC_STATE_PYTHON_PIPE_CREATED          8
    23 #define PLC_STATE_WAITDEBUG_PIPE_CREATED       16
    23 #define PLC_STATE_WAITDEBUG_PIPE_CREATED       16
    24 #define PLC_STATE_WAITPYTHON_PIPE_CREATED      32
    24 #define PLC_STATE_WAITPYTHON_PIPE_CREATED      32
    25 
    25 
    26 #define PIPE_SIZE                    1 
    26 #define PIPE_SIZE                    1
    27 
    27 
    28 // rt-pipes commands
    28 // rt-pipes commands
    29 
    29 
    30 #define PYTHON_PENDING_COMMAND 1
    30 #define PYTHON_PENDING_COMMAND 1
    31 #define PYTHON_FINISH 2
    31 #define PYTHON_FINISH 2
    60 
    60 
    61 struct RT_to_nRT_signal_s {
    61 struct RT_to_nRT_signal_s {
    62     int used;
    62     int used;
    63     RT_PIPE pipe;
    63     RT_PIPE pipe;
    64     int pipe_fd;
    64     int pipe_fd;
    65     char *name; 
    65     char *name;
    66 };
    66 };
    67 typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
    67 typedef struct RT_to_nRT_signal_s RT_to_nRT_signal_t;
    68 
    68 
    69 #define max_RT_to_nRT_signals 16
    69 #define max_RT_to_nRT_signals 16
    70 
    70 
    99         PLC_GetTime(&__CURRENT_TIME);
    99         PLC_GetTime(&__CURRENT_TIME);
   100         __run();
   100         __run();
   101         if (PLC_shutdown) break;
   101         if (PLC_shutdown) break;
   102         rt_task_wait_period(NULL);
   102         rt_task_wait_period(NULL);
   103     }
   103     }
   104     /* since xenomai 3 it is not enough to close() 
   104     /* since xenomai 3 it is not enough to close()
   105        file descriptor to unblock read()... */
   105        file descriptor to unblock read()... */
   106     {
   106     {
   107         /* explicitely finish python thread */
   107         /* explicitely finish python thread */
   108         char msg = PYTHON_FINISH;
   108         char msg = PYTHON_FINISH;
   109         send_RT_to_nRT_signal(WaitPython_handle, msg);
   109         send_RT_to_nRT_signal(WaitPython_handle, msg);
   256     signal(SIGINT, catch_signal);
   256     signal(SIGINT, catch_signal);
   257 
   257 
   258     /* no memory swapping for that process */
   258     /* no memory swapping for that process */
   259     mlockall(MCL_CURRENT | MCL_FUTURE);
   259     mlockall(MCL_CURRENT | MCL_FUTURE);
   260 
   260 
   261    
       
   262     /* memory initialization */
   261     /* memory initialization */
   263     PLC_shutdown = 0;
   262     PLC_shutdown = 0;
   264     bzero(RT_to_nRT_signal_pool, sizeof(RT_to_nRT_signal_pool));
   263     bzero(RT_to_nRT_signal_pool, sizeof(RT_to_nRT_signal_pool));
   265 
   264 
   266     /*** RT Pipes ***/
   265     /*** RT Pipes ***/
   267     /* create Debug_pipe */
   266     /* create Debug_pipe */
   268     if(Debug_handle = create_RT_to_nRT_signal("Debug_pipe")) goto error;
   267     if(!(Debug_handle = create_RT_to_nRT_signal("Debug_pipe"))) goto error;
   269     PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED;
   268     PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED;
   270     
   269 
   271     /* create Python_pipe */
   270     /* create Python_pipe */
   272     if(Python_handle = create_RT_to_nRT_signal("Python_pipe")) goto error;
   271     if(!(Python_handle = create_RT_to_nRT_signal("Python_pipe"))) goto error;
   273     PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED;
   272     PLC_state |= PLC_STATE_PYTHON_PIPE_CREATED;
   274 
   273 
   275     /* create WaitDebug_pipe */
   274     /* create WaitDebug_pipe */
   276     if(WaitDebug_handle = create_RT_to_nRT_signal("WaitDebug_pipe")) goto error;
   275     if(!(WaitDebug_handle = create_RT_to_nRT_signal("WaitDebug_pipe"))) goto error;
   277     PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED;
   276     PLC_state |= PLC_STATE_WAITDEBUG_PIPE_CREATED;
   278 
   277 
   279     /* create WaitPython_pipe */
   278     /* create WaitPython_pipe */
   280     if(WaitPython_handle = create_RT_to_nRT_signal("WaitPython_pipe")) goto error;
   279     if(!(WaitPython_handle = create_RT_to_nRT_signal("WaitPython_pipe"))) goto error;
   281     PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED;
   280     PLC_state |= PLC_STATE_WAITPYTHON_PIPE_CREATED;
   282 
   281 
   283     /*** create PLC task ***/
   282     /*** create PLC task ***/
   284     if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE))
   283     if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE))
   285         _startPLCLog("Failed creating PLC task");
   284         _startPLCLog("Failed creating PLC task");
   317     return 0;
   316     return 0;
   318 }
   317 }
   319 
   318 
   320 void LeaveDebugSection(void)
   319 void LeaveDebugSection(void)
   321 {
   320 {
   322     if(AtomicCompareExchange( &debug_state, 
   321     if(AtomicCompareExchange( &debug_state,
   323         DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){
   322         DEBUG_BUSY, DEBUG_FREE) == DEBUG_BUSY){
   324         char msg = DEBUG_UNLOCK;
   323         char msg = DEBUG_UNLOCK;
   325         /* signal to NRT for wakeup */
   324         /* signal to NRT for wakeup */
   326         send_RT_to_nRT_signal(Debug_handle, msg);
   325         send_RT_to_nRT_signal(Debug_handle, msg);
   327     }
   326     }
   333 {
   332 {
   334     char cmd;
   333     char cmd;
   335     int res;
   334     int res;
   336     if (PLC_shutdown) return -1;
   335     if (PLC_shutdown) return -1;
   337     /* Wait signal from PLC thread */
   336     /* Wait signal from PLC thread */
   338     recv_RT_to_nRT_signal(WaitDebug_handle, &cmd);
   337     res = recv_RT_to_nRT_signal(WaitDebug_handle, &cmd);
   339     if (res == 1 && cmd == DEBUG_PENDING_DATA){
   338     if (res == 1 && cmd == DEBUG_PENDING_DATA){
   340         *tick = __debug_tick;
   339         *tick = __debug_tick;
   341         return 0;
   340         return 0;
   342     }
   341     }
   343     return -1;
   342     return -1;
   381 #define PYTHON_FREE 0
   380 #define PYTHON_FREE 0
   382 #define PYTHON_BUSY 1
   381 #define PYTHON_BUSY 1
   383 static long python_state = PYTHON_FREE;
   382 static long python_state = PYTHON_FREE;
   384 
   383 
   385 int WaitPythonCommands(void)
   384 int WaitPythonCommands(void)
   386 { 
   385 {
   387     char cmd;
   386     char cmd;
   388     if (PLC_shutdown) return -1;
   387     if (PLC_shutdown) return -1;
   389     /* Wait signal from PLC thread */
   388     /* Wait signal from PLC thread */
   390     if(recv_RT_to_nRT_signal(WaitPython_handle, &cmd) == 1 && cmd==PYTHON_PENDING_COMMAND){
   389     if(recv_RT_to_nRT_signal(WaitPython_handle, &cmd) == 1 && cmd==PYTHON_PENDING_COMMAND){
   391         return 0;
   390         return 0;