targets/Xenomai/plc_Xenomai_main.c
changeset 336 ae3488c79283
parent 321 5a4e6278a18b
child 342 80e5876bc53b
equal deleted inserted replaced
335:c5f3f71e7260 336:ae3488c79283
     1 /**
     1 /**
     2  * Linux specific code
     2  * Linux specific code
     3  **/ 
     3  **/
     4 
     4 
     5 #include <stdio.h>
     5 #include <stdio.h>
     6 #include <string.h>
     6 #include <string.h>
     7 #include <time.h>
     7 #include <time.h>
     8 #include <signal.h>
     8 #include <signal.h>
    50 }
    50 }
    51 
    51 
    52 void PLC_task_proc(void *arg)
    52 void PLC_task_proc(void *arg)
    53 {
    53 {
    54     PLC_SetTimer(Ttick, Ttick);
    54     PLC_SetTimer(Ttick, Ttick);
    55   
    55 
    56     while (1) {
    56     while (1) {
    57         PLC_GetTime(&__CURRENT_TIME);
    57         PLC_GetTime(&__CURRENT_TIME);
    58         __run();
    58         __run();
    59         if (PLC_shutdown) break;
    59         if (PLC_shutdown) break;
    60         rt_task_wait_period(NULL);
    60         rt_task_wait_period(NULL);
    82 
    82 
    83     if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) {
    83     if (PLC_state & PLC_STATE_PYTHON_MUTEX_CREATED) {
    84         rt_mutex_delete(&python_mutex);
    84         rt_mutex_delete(&python_mutex);
    85         PLC_state &= ~ PLC_STATE_PYTHON_MUTEX_CREATED;
    85         PLC_state &= ~ PLC_STATE_PYTHON_MUTEX_CREATED;
    86     }
    86     }
    87     
    87 
    88     if (PLC_state & PLC_STATE_DEBUG_WAIT_SEM_CREATED) {
    88     if (PLC_state & PLC_STATE_DEBUG_WAIT_SEM_CREATED) {
    89         rt_sem_delete(&debug_wait_sem);
    89         rt_sem_delete(&debug_wait_sem);
    90         PLC_state &= ~ PLC_STATE_DEBUG_WAIT_SEM_CREATED;
    90         PLC_state &= ~ PLC_STATE_DEBUG_WAIT_SEM_CREATED;
    91     }
    91     }
    92 
    92 
   120 
   120 
   121 #define max_val(a,b) ((a>b)?a:b)
   121 #define max_val(a,b) ((a>b)?a:b)
   122 int startPLC(int argc,char **argv)
   122 int startPLC(int argc,char **argv)
   123 {
   123 {
   124     int ret = 0;
   124     int ret = 0;
   125     
   125 
   126     signal(SIGINT, catch_signal);
   126     signal(SIGINT, catch_signal);
   127     
   127 
   128     /* ne-memory-swapping for this program */
   128     /* ne-memory-swapping for this program */
   129     mlockall(MCL_CURRENT | MCL_FUTURE);
   129     mlockall(MCL_CURRENT | MCL_FUTURE);
   130     
   130 
   131     /* Translate PLC's microseconds to Ttick nanoseconds */
   131     /* Translate PLC's microseconds to Ttick nanoseconds */
   132     Ttick = 1000000 * max_val(common_ticktime__,1);
   132     Ttick = 1000000 * max_val(common_ticktime__,1);
   133     
   133 
   134     /* create python_wait_sem */
   134     /* create python_wait_sem */
   135     ret = rt_sem_create(&python_wait_sem, "python_wait_sem", 0, S_FIFO);
   135     ret = rt_sem_create(&python_wait_sem, "python_wait_sem", 0, S_FIFO);
   136     if (ret) goto error;
   136     if (ret) goto error;
   137     PLC_state |= PLC_STATE_PYTHON_WAIT_SEM_CREATED;
   137     PLC_state |= PLC_STATE_PYTHON_WAIT_SEM_CREATED;
   138     
   138 
   139     /* create python_mutex */
   139     /* create python_mutex */
   140     ret = rt_mutex_create(&python_mutex, "python_mutex");
   140     ret = rt_mutex_create(&python_mutex, "python_mutex");
   141     if (ret) goto error;
   141     if (ret) goto error;
   142     PLC_state |= PLC_STATE_PYTHON_MUTEX_CREATED;
   142     PLC_state |= PLC_STATE_PYTHON_MUTEX_CREATED;
   143     
   143 
   144     /* create debug_wait_sem */
   144     /* create debug_wait_sem */
   145     ret = rt_sem_create(&debug_wait_sem, "debug_wait_sem", 0, S_FIFO);
   145     ret = rt_sem_create(&debug_wait_sem, "debug_wait_sem", 0, S_FIFO);
   146     if (ret) goto error;
   146     if (ret) goto error;
   147     PLC_state |= PLC_STATE_DEBUG_WAIT_SEM_CREATED;
   147     PLC_state |= PLC_STATE_DEBUG_WAIT_SEM_CREATED;
   148     
   148 
   149     /* create debug_mutex */
   149     /* create debug_mutex */
   150     ret = rt_mutex_create(&debug_mutex, "debug_mutex");
   150     ret = rt_mutex_create(&debug_mutex, "debug_mutex");
   151     if (ret) goto error;
   151     if (ret) goto error;
   152     PLC_state |= PLC_STATE_DEBUG_MUTEX_CREATED;
   152     PLC_state |= PLC_STATE_DEBUG_MUTEX_CREATED;
   153     
   153 
   154     /* create can_driver_task */
   154     /* create can_driver_task */
   155     ret = rt_task_create(&PLC_task, "PLC_task", 0, 50, 0);
   155     ret = rt_task_create(&PLC_task, "PLC_task", 0, 50, 0);
   156     if (ret) goto error;
   156     if (ret) goto error;
   157     PLC_state |= PLC_STATE_TASK_CREATED;
   157     PLC_state |= PLC_STATE_TASK_CREATED;
   158     
   158 
   159     ret = __init(argc,argv);
   159     ret = __init(argc,argv);
   160     if (ret) goto error;
   160     if (ret) goto error;
   161 
   161 
   162     /* start can_driver_task */
   162     /* start can_driver_task */
   163     ret = rt_task_start(&PLC_task, &PLC_task_proc, NULL);
   163     ret = rt_task_start(&PLC_task, &PLC_task_proc, NULL);
   187     rt_task_shadow(&WaitDebug_task, "WaitDebug_task", 0, 0);
   187     rt_task_shadow(&WaitDebug_task, "WaitDebug_task", 0, 0);
   188     /* Wait signal from PLC thread */
   188     /* Wait signal from PLC thread */
   189     rt_sem_p(&debug_wait_sem, TM_INFINITE);
   189     rt_sem_p(&debug_wait_sem, TM_INFINITE);
   190     return __debug_tick;
   190     return __debug_tick;
   191 }
   191 }
   192  
   192 
   193 /* Called by PLC thread when debug_publish finished
   193 /* Called by PLC thread when debug_publish finished
   194  * This is supposed to unlock debugger thread in WaitDebugData*/
   194  * This is supposed to unlock debugger thread in WaitDebugData*/
   195 void InitiateDebugTransfer()
   195 void InitiateDebugTransfer()
   196 {
   196 {
   197     /* remember tick */
   197     /* remember tick */
   219 {
   219 {
   220     rt_task_shadow(&WaitPythonCommand_task, "WaitPythonCommand_task", 0, 0);
   220     rt_task_shadow(&WaitPythonCommand_task, "WaitPythonCommand_task", 0, 0);
   221     /* Wait signal from PLC thread */
   221     /* Wait signal from PLC thread */
   222     rt_sem_p(&python_wait_sem, TM_INFINITE);
   222     rt_sem_p(&python_wait_sem, TM_INFINITE);
   223 }
   223 }
   224  
   224 
   225 /* Called by PLC thread on each new python command*/
   225 /* Called by PLC thread on each new python command*/
   226 void UnBlockPythonCommands(void)
   226 void UnBlockPythonCommands(void)
   227 {
   227 {
   228     /* signal debugger thread it can read data */
   228     /* signal debugger thread it can read data */
   229     rt_sem_v(&python_wait_sem);
   229     rt_sem_v(&python_wait_sem);