targets/Xenomai/plc_Xenomai_main.c
changeset 745 96dd6650854d
parent 695 f378fe028f6c
child 954 ab487d32ce9a
equal deleted inserted replaced
744:bdab45f96aa0 745:96dd6650854d
   102         PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED;
   102         PLC_state &= ~PLC_STATE_WAITDEBUG_FILE_OPENED;
   103     }
   103     }
   104 
   104 
   105     if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) {
   105     if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) {
   106         rt_pipe_delete(&WaitPython_pipe);
   106         rt_pipe_delete(&WaitPython_pipe);
   107         PLC_state &= ~PLC_STATE_WAITDEBUG_PIPE_CREATED;
   107         PLC_state &= ~PLC_STATE_WAITPYTHON_PIPE_CREATED;
   108     }
   108     }
   109 
   109 
   110     if (PLC_state & PLC_STATE_WAITPYTHON_PIPE_CREATED) {
   110     if (PLC_state & PLC_STATE_WAITPYTHON_FILE_OPENED) {
   111         close(WaitPython_pipe_fd);
   111         close(WaitPython_pipe_fd);
   112         PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED;
   112         PLC_state &= ~PLC_STATE_WAITPYTHON_FILE_OPENED;
   113     }
   113     }
   114 
   114 
   115     if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) {
   115     if (PLC_state & PLC_STATE_DEBUG_PIPE_CREATED) {
   122         PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED;
   122         PLC_state &= ~PLC_STATE_DEBUG_FILE_OPENED;
   123     }
   123     }
   124 
   124 
   125     if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) {
   125     if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) {
   126         rt_pipe_delete(&Python_pipe);
   126         rt_pipe_delete(&Python_pipe);
   127         PLC_state &= ~PLC_STATE_DEBUG_PIPE_CREATED;
   127         PLC_state &= ~PLC_STATE_PYTHON_PIPE_CREATED;
   128     }
   128     }
   129 
   129 
   130     if (PLC_state & PLC_STATE_PYTHON_PIPE_CREATED) {
   130     if (PLC_state & PLC_STATE_PYTHON_FILE_OPENED) {
   131         close(Python_pipe_fd);
   131         close(Python_pipe_fd);
   132         PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED;
   132         PLC_state &= ~PLC_STATE_PYTHON_FILE_OPENED;
   133     }
   133     }
   134 
   134 
   135 }
   135 }
   136 
   136 
   137 int stopPLC()
   137 int stopPLC()
   138 {
   138 {
       
   139     /* Stop the PLC */
   139     PLC_shutdown = 1;
   140     PLC_shutdown = 1;
   140     /* Stop the PLC */
   141 
   141     PLC_SetTimer(0, 0);
   142     /* Wait until PLC task stops */
       
   143     rt_task_join(&PLC_task);
       
   144 
       
   145     PLC_cleanup_all();
   142     __cleanup();
   146     __cleanup();
   143     PLC_cleanup_all();
       
   144     __debug_tick = -1;
   147     __debug_tick = -1;
   145     return 0;
   148     return 0;
   146 }
   149 }
   147 
   150 
   148 //
   151 //
   164     mlockall(MCL_CURRENT | MCL_FUTURE);
   167     mlockall(MCL_CURRENT | MCL_FUTURE);
   165 
   168 
   166     /* Define Ttick to 1ms if common_ticktime not defined */
   169     /* Define Ttick to 1ms if common_ticktime not defined */
   167     Ttick = common_ticktime__?common_ticktime__:1000000;
   170     Ttick = common_ticktime__?common_ticktime__:1000000;
   168 
   171 
       
   172     PLC_shutdown = 0;
       
   173 
   169     /*** RT Pipes creation and opening ***/
   174     /*** RT Pipes creation and opening ***/
   170     /* create Debug_pipe */
   175     /* create Debug_pipe */
   171     if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE)) 
   176     if(rt_pipe_create(&Debug_pipe, "Debug_pipe", DEBUG_PIPE_MINOR, PIPE_SIZE)) 
   172         goto error;
   177         goto error;
   173     PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED;
   178     PLC_state |= PLC_STATE_DEBUG_PIPE_CREATED;
   202     /* open WaitPython_pipe*/
   207     /* open WaitPython_pipe*/
   203     if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error;
   208     if((WaitPython_pipe_fd = open(WAITPYTHON_PIPE_DEVICE, O_RDWR)) == -1) goto error;
   204     PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED;
   209     PLC_state |= PLC_STATE_WAITPYTHON_FILE_OPENED;
   205 
   210 
   206     /*** create PLC task ***/
   211     /*** create PLC task ***/
   207     if(rt_task_create(&PLC_task, "PLC_task", 0, 50, 0)) goto error;
   212     if(rt_task_create(&PLC_task, "PLC_task", 0, 50, T_JOINABLE)) goto error;
   208     PLC_state |= PLC_STATE_TASK_CREATED;
   213     PLC_state |= PLC_STATE_TASK_CREATED;
   209 
   214 
   210     if(__init(argc,argv)) goto error;
   215     if(__init(argc,argv)) goto error;
   211 
   216 
   212     /* start PLC task */
   217     /* start PLC task */
   254 #define DEBUG_PENDING_DATA 1
   259 #define DEBUG_PENDING_DATA 1
   255 int WaitDebugData(unsigned long *tick)
   260 int WaitDebugData(unsigned long *tick)
   256 {
   261 {
   257     char cmd;
   262     char cmd;
   258     int res;
   263     int res;
       
   264     if (PLC_shutdown) return -1;
   259     /* Wait signal from PLC thread */
   265     /* Wait signal from PLC thread */
   260     res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd));
   266     res = read(WaitDebug_pipe_fd, &cmd, sizeof(cmd));
   261     if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA){
   267     if (res == sizeof(cmd) && cmd == DEBUG_PENDING_DATA){
   262         *tick = __debug_tick;
   268         *tick = __debug_tick;
   263         return 0;
   269         return 0;
   277 }
   283 }
   278 
   284 
   279 int suspendDebug(int disable)
   285 int suspendDebug(int disable)
   280 {
   286 {
   281     char cmd = DEBUG_UNLOCK;
   287     char cmd = DEBUG_UNLOCK;
       
   288     if (PLC_shutdown) return -1;
   282     while(AtomicCompareExchange(
   289     while(AtomicCompareExchange(
   283             &debug_state,
   290             &debug_state,
   284             DEBUG_FREE,
   291             DEBUG_FREE,
   285             DEBUG_BUSY) != DEBUG_FREE &&
   292             DEBUG_BUSY) != DEBUG_FREE &&
   286             cmd == DEBUG_UNLOCK){
   293             cmd == DEBUG_UNLOCK){
   306 static long python_state = PYTHON_FREE;
   313 static long python_state = PYTHON_FREE;
   307 
   314 
   308 int WaitPythonCommands(void)
   315 int WaitPythonCommands(void)
   309 { 
   316 { 
   310     char cmd;
   317     char cmd;
       
   318     if (PLC_shutdown) return -1;
   311     /* Wait signal from PLC thread */
   319     /* Wait signal from PLC thread */
   312     if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){
   320     if(read(WaitPython_pipe_fd, &cmd, sizeof(cmd))==sizeof(cmd) && cmd==PYTHON_PENDING_COMMAND){
   313         return 0;
   321         return 0;
   314     }
   322     }
   315     return -1;
   323     return -1;
   332 
   340 
   333 #define UNLOCK_PYTHON 1
   341 #define UNLOCK_PYTHON 1
   334 void LockPython(void)
   342 void LockPython(void)
   335 {
   343 {
   336     char cmd = UNLOCK_PYTHON;
   344     char cmd = UNLOCK_PYTHON;
       
   345     if (PLC_shutdown) return;
   337     while(AtomicCompareExchange(
   346     while(AtomicCompareExchange(
   338             &python_state,
   347             &python_state,
   339             PYTHON_FREE,
   348             PYTHON_FREE,
   340             PYTHON_BUSY) != PYTHON_FREE &&
   349             PYTHON_BUSY) != PYTHON_FREE &&
   341             cmd == UNLOCK_PYTHON){
   350             cmd == UNLOCK_PYTHON){
   374 }
   383 }
   375 
   384 
   376 void Remind(unsigned int offset, unsigned int count, void *p)
   385 void Remind(unsigned int offset, unsigned int count, void *p)
   377 {
   386 {
   378 }
   387 }
   379