targets/Linux/plc_Linux_main.c
changeset 397 6a7ff66a811d
parent 386 2932b0dd437c
child 446 1edde533db19
equal deleted inserted replaced
396:d1083f580ca1 397:6a7ff66a811d
    62   printf("Got Signal %d\n",sig);
    62   printf("Got Signal %d\n",sig);
    63   exit(0);
    63   exit(0);
    64 }
    64 }
    65 
    65 
    66 
    66 
    67 static int __debug_tick;
    67 static unsigned long __debug_tick;
    68 
    68 
    69 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    69 static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    70 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
    70 static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER;
    71 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    71 static pthread_mutex_t debug_wait_mutex = PTHREAD_MUTEX_INITIALIZER;
    72 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
    72 static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
    73 
    73 
    74 #define maxval(a,b) ((a>b)?a:b)
    74 #define maxval(a,b) ((a>b)?a:b)
    75 int startPLC(int argc,char **argv)
    75 int startPLC(int argc,char **argv)
    76 {
    76 {
    77     struct sigevent sigev;
    77     struct sigevent sigev;
    78     /* Translate PLC's microseconds to Ttick nanoseconds */
    78     /* Define Ttick to 1ms if common_ticktime not defined */
    79     Ttick = 1000000 * maxval(common_ticktime__,1);
    79     Ttick = common_ticktime__?common_ticktime__:1000000;
    80 
    80 
    81     memset (&sigev, 0, sizeof (struct sigevent));
    81     memset (&sigev, 0, sizeof (struct sigevent));
    82     sigev.sigev_value.sival_int = 0;
    82     sigev.sigev_value.sival_int = 0;
    83     sigev.sigev_notify = SIGEV_THREAD;
    83     sigev.sigev_notify = SIGEV_THREAD;
    84     sigev.sigev_notify_attributes = NULL;
    84     sigev.sigev_notify_attributes = NULL;
    95         PLC_SetTimer(Ttick,Ttick);
    95         PLC_SetTimer(Ttick,Ttick);
    96 
    96 
    97         /* install signal handler for manual break */
    97         /* install signal handler for manual break */
    98 //        signal(SIGTERM, catch_signal);
    98 //        signal(SIGTERM, catch_signal);
    99         signal(SIGINT, catch_signal);
    99         signal(SIGINT, catch_signal);
       
   100 
       
   101         pthread_mutex_trylock(&debug_mutex);
   100     }else{
   102     }else{
   101         return 1;
   103         return 1;
   102     }
   104     }
   103     return 0;
   105     return 0;
   104 }
   106 }
   118     /* Stop the PLC */
   120     /* Stop the PLC */
   119     PLC_SetTimer(0,0);
   121     PLC_SetTimer(0,0);
   120     timer_delete (PLC_timer);
   122     timer_delete (PLC_timer);
   121     __cleanup();
   123     __cleanup();
   122     __debug_tick = -1;
   124     __debug_tick = -1;
       
   125     pthread_mutex_unlock(&debug_mutex);
   123     pthread_mutex_unlock(&debug_wait_mutex);
   126     pthread_mutex_unlock(&debug_wait_mutex);
   124     pthread_mutex_destroy(&debug_wait_mutex);
   127     pthread_mutex_destroy(&debug_wait_mutex);
   125     pthread_mutex_unlock(&python_wait_mutex);
   128     pthread_mutex_unlock(&python_wait_mutex);
   126     pthread_mutex_destroy(&python_wait_mutex);
   129     pthread_mutex_destroy(&python_wait_mutex);
   127     return 0;
   130     return 0;
   128 }
   131 }
   129 
   132 
   130 extern int __tick;
   133 extern unsigned long __tick;
   131 /* from plc_debugger.c */
   134 /* from plc_debugger.c */
   132 int WaitDebugData()
   135 unsigned long WaitDebugData()
   133 {
   136 {
   134     /* Wait signal from PLC thread */
   137     /* Wait signal from PLC thread */
   135     if(pthread_mutex_lock(&debug_wait_mutex)) return -1;
   138     if(pthread_mutex_lock(&debug_wait_mutex)) return -1;
   136     return __debug_tick;
   139     return __debug_tick;
   137 }
   140 }