targets/Linux/plc_Linux_main.c
changeset 329 22e65b8e20f4
parent 290 3bd617ae7a05
child 333 e90aebdd2af1
equal deleted inserted replaced
328:c23daa6996c2 329:22e65b8e20f4
     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>
     9 #include <stdlib.h>
     9 #include <stdlib.h>
    10 #include <pthread.h> 
    10 #include <pthread.h>
    11 
    11 
    12 /* provided by POUS.C */
    12 /* provided by POUS.C */
    13 extern int common_ticktime__;
    13 extern int common_ticktime__;
    14 
    14 
    15 long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
    15 long AtomicCompareExchange(long* atomicvar,long compared, long exchange)
    49 	    timerValues.it_value.tv_sec = next / 1000000000;
    49 	    timerValues.it_value.tv_sec = next / 1000000000;
    50 	    timerValues.it_value.tv_nsec = next % 1000000000;
    50 	    timerValues.it_value.tv_nsec = next % 1000000000;
    51 	    timerValues.it_interval.tv_sec = period / 1000000000;
    51 	    timerValues.it_interval.tv_sec = period / 1000000000;
    52 	    timerValues.it_interval.tv_nsec = period % 1000000000;
    52 	    timerValues.it_interval.tv_nsec = period % 1000000000;
    53 #endif
    53 #endif
    54 	}	
    54 	}
    55     timer_settime (PLC_timer, 0, &timerValues, NULL);
    55     timer_settime (PLC_timer, 0, &timerValues, NULL);
    56 }
    56 }
    57 //
    57 //
    58 void catch_signal(int sig)
    58 void catch_signal(int sig)
    59 {
    59 {
    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     /* Translate PLC's microseconds to Ttick nanoseconds */
    79     Ttick = 1000000 * maxval(common_ticktime__,1);
    79     Ttick = 1000000 * maxval(common_ticktime__,1);
    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;
    85     sigev.sigev_notify_function = PLC_timer_notify;
    85     sigev.sigev_notify_function = PLC_timer_notify;
       
    86 
       
    87     pthread_mutex_init(&debug_wait_mutex);
       
    88     pthread_mutex_init(&python_wait_mutex);
    86 
    89 
    87     pthread_mutex_lock(&debug_wait_mutex);
    90     pthread_mutex_lock(&debug_wait_mutex);
    88     pthread_mutex_lock(&python_wait_mutex);
    91     pthread_mutex_lock(&python_wait_mutex);
    89 
    92 
    90     timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
    93     timer_create (CLOCK_REALTIME, &sigev, &PLC_timer);
    91     if(  __init(argc,argv) == 0 ){
    94     if(  __init(argc,argv) == 0 ){
    92         PLC_SetTimer(Ttick,Ttick);
    95         PLC_SetTimer(Ttick,Ttick);
    93         
    96 
    94         /* install signal handler for manual break */
    97         /* install signal handler for manual break */
    95 //        signal(SIGTERM, catch_signal);
    98 //        signal(SIGTERM, catch_signal);
    96         signal(SIGINT, catch_signal);
    99         signal(SIGINT, catch_signal);
    97     }else{
   100     }else{
    98         return 1;
   101         return 1;
   116     PLC_SetTimer(0,0);
   119     PLC_SetTimer(0,0);
   117     timer_delete (PLC_timer);
   120     timer_delete (PLC_timer);
   118     __cleanup();
   121     __cleanup();
   119     __debug_tick = -1;
   122     __debug_tick = -1;
   120     pthread_mutex_unlock(&debug_wait_mutex);
   123     pthread_mutex_unlock(&debug_wait_mutex);
       
   124     pthread_mutex_destroy(&debug_wait_mutex);
       
   125     pthread_mutex_unlock(&python_wait_mutex);
       
   126     pthread_mutex_destroy(&python_wait_mutex);
   121 }
   127 }
   122 
   128 
   123 extern int __tick;
   129 extern int __tick;
   124 /* from plc_debugger.c */
   130 /* from plc_debugger.c */
   125 int WaitDebugData()
   131 int WaitDebugData()
   126 {
   132 {
   127     /* Wait signal from PLC thread */
   133     /* Wait signal from PLC thread */
   128     pthread_mutex_lock(&debug_wait_mutex);
   134     if(pthread_mutex_lock(&debug_wait_mutex)) return -1;
   129     return __debug_tick;
   135     return __debug_tick;
   130 }
   136 }
   131  
   137 
   132 /* Called by PLC thread when debug_publish finished
   138 /* Called by PLC thread when debug_publish finished
   133  * This is supposed to unlock debugger thread in WaitDebugData*/
   139  * This is supposed to unlock debugger thread in WaitDebugData*/
   134 void InitiateDebugTransfer()
   140 void InitiateDebugTransfer()
   135 {
   141 {
   136     /* remember tick */
   142     /* remember tick */
   155 
   161 
   156 /* from plc_python.c */
   162 /* from plc_python.c */
   157 int WaitPythonCommands(void)
   163 int WaitPythonCommands(void)
   158 {
   164 {
   159     /* Wait signal from PLC thread */
   165     /* Wait signal from PLC thread */
   160     pthread_mutex_lock(&python_wait_mutex);
   166     return pthread_mutex_lock(&python_wait_mutex);
   161 }
   167 }
   162  
   168 
   163 /* Called by PLC thread on each new python command*/
   169 /* Called by PLC thread on each new python command*/
   164 void UnBlockPythonCommands(void)
   170 void UnBlockPythonCommands(void)
   165 {
   171 {
   166     /* signal debugger thread it can read data */
   172     /* signal debugger thread it can read data */
   167     pthread_mutex_unlock(&python_wait_mutex);
   173     pthread_mutex_unlock(&python_wait_mutex);