# HG changeset patch # User laurent # Date 1253527252 -7200 # Node ID 6a7ff66a811da38a8b87bb5b86a5e78ab79b017a # Parent d1083f580ca1f95ecdc4eb53ad049cd6304eeb99 Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized diff -r d1083f580ca1 -r 6a7ff66a811d targets/Linux/plc_Linux_main.c --- a/targets/Linux/plc_Linux_main.c Mon Sep 21 11:56:55 2009 +0200 +++ b/targets/Linux/plc_Linux_main.c Mon Sep 21 12:00:52 2009 +0200 @@ -64,7 +64,7 @@ } -static int __debug_tick; +static unsigned long __debug_tick; static pthread_mutex_t python_wait_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t python_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -75,8 +75,8 @@ int startPLC(int argc,char **argv) { struct sigevent sigev; - /* Translate PLC's microseconds to Ttick nanoseconds */ - Ttick = 1000000 * maxval(common_ticktime__,1); + /* Define Ttick to 1ms if common_ticktime not defined */ + Ttick = common_ticktime__?common_ticktime__:1000000; memset (&sigev, 0, sizeof (struct sigevent)); sigev.sigev_value.sival_int = 0; @@ -97,6 +97,8 @@ /* install signal handler for manual break */ // signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); + + pthread_mutex_trylock(&debug_mutex); }else{ return 1; } @@ -120,6 +122,7 @@ timer_delete (PLC_timer); __cleanup(); __debug_tick = -1; + pthread_mutex_unlock(&debug_mutex); pthread_mutex_unlock(&debug_wait_mutex); pthread_mutex_destroy(&debug_wait_mutex); pthread_mutex_unlock(&python_wait_mutex); @@ -127,9 +130,9 @@ return 0; } -extern int __tick; +extern unsigned long __tick; /* from plc_debugger.c */ -int WaitDebugData() +unsigned long WaitDebugData() { /* Wait signal from PLC thread */ if(pthread_mutex_lock(&debug_wait_mutex)) return -1; diff -r d1083f580ca1 -r 6a7ff66a811d targets/Win32/plc_Win32_main.c --- a/targets/Win32/plc_Win32_main.c Mon Sep 21 11:56:55 2009 +0200 +++ b/targets/Win32/plc_Win32_main.c Mon Sep 21 12:00:52 2009 +0200 @@ -8,7 +8,7 @@ #include /* provided by POUS.C */ -extern int common_ticktime__; +extern unsigned long common_ticktime__; long AtomicCompareExchange(long* atomicvar, long compared, long exchange) { @@ -74,8 +74,8 @@ int startPLC(int argc,char **argv) { unsigned long thread_id = 0; - /* Translate PLC's microseconds to Ttick nanoseconds */ - Ttick = 1000000 * maxval(common_ticktime__,1); + /* Define Ttick to 1ms if common_ticktime not defined */ + Ttick = common_ticktime__?common_ticktime__:1000000; debug_sem = CreateSemaphore( NULL, // default security attributes @@ -142,7 +142,7 @@ } return 0; } -static int __debug_tick; +static unsigned long __debug_tick; int TryEnterDebugSection(void) { @@ -170,7 +170,7 @@ } /* from plc_debugger.c */ -int WaitDebugData() +unsigned long WaitDebugData() { if(WaitForSingleObject(debug_wait_sem, INFINITE) != WAIT_OBJECT_0) return -1; return __debug_tick; diff -r d1083f580ca1 -r 6a7ff66a811d targets/Xenomai/plc_Xenomai_main.c --- a/targets/Xenomai/plc_Xenomai_main.c Mon Sep 21 11:56:55 2009 +0200 +++ b/targets/Xenomai/plc_Xenomai_main.c Mon Sep 21 12:00:52 2009 +0200 @@ -29,7 +29,7 @@ #define WAITDEBUG_PIPE_SIZE 500 /* provided by POUS.C */ -extern int common_ticktime__; +extern unsigned long common_ticktime__; long AtomicCompareExchange(long* atomicvar,long compared, long exchange) { @@ -72,7 +72,7 @@ } } -static int __debug_tick; +static unsigned long __debug_tick; RT_SEM python_wait_sem; RT_MUTEX python_mutex; @@ -142,8 +142,8 @@ /* ne-memory-swapping for this program */ mlockall(MCL_CURRENT | MCL_FUTURE); - /* Translate PLC's microseconds to Ttick nanoseconds */ - Ttick = 1000000 * max_val(common_ticktime__,1); + /* Define Ttick to 1ms if common_ticktime not defined */ + Ttick = common_ticktime__?common_ticktime__:1000000; /* create python_wait_sem */ ret = rt_sem_create(&python_wait_sem, "python_wait_sem", 0, S_FIFO); @@ -203,9 +203,9 @@ rt_mutex_release(&debug_mutex); } -extern int __tick; +extern unsigned long __tick; /* from plc_debugger.c */ -int WaitDebugData() +unsigned long WaitDebugData() { char message; int res; diff -r d1083f580ca1 -r 6a7ff66a811d targets/plc_common_main.c --- a/targets/plc_common_main.c Mon Sep 21 11:56:55 2009 +0200 +++ b/targets/plc_common_main.c Mon Sep 21 12:00:52 2009 +0200 @@ -5,13 +5,13 @@ #include #include "iec_types.h" /* - * Prototypes of functions provied by generated C softPLC + * Prototypes of functions provided by generated C softPLC **/ -void config_run__(int tick); +void config_run__(unsigned long tick); void config_init__(void); /* - * Prototypes of functions provied by generated target C code + * Prototypes of functions provided by generated target C code * */ void __init_debug(void); void __cleanup_debug(void); @@ -22,8 +22,13 @@ * Variables used by generated C softPLC and plugins **/ IEC_TIME __CURRENT_TIME; -IEC_BOOL __DEBUG; -int __tick = -1; +IEC_BOOL __DEBUG = 0; +unsigned long __tick = -1; + +/* + * Variable generated by C softPLC and plugins + **/ +extern unsigned long greatest_tick_count__; /* Help to quit cleanly when init fail at a certain level */ static int init_level = 0; @@ -39,6 +44,8 @@ void __run() { __tick++; + if (greatest_tick_count__) + __tick %%= greatest_tick_count__; %(retrieve_calls)s @@ -53,7 +60,7 @@ } /* - * Initialize variables according to PLC's defalut values, + * Initialize variables according to PLC's default values, * and then init plugins with that values **/ int __init(int argc,char **argv) @@ -85,7 +92,7 @@ static long long Tsync = 0; static long long FreqCorr = 0; static int Nticks = 0; -static int last_tick = 0; +static unsigned long last_tick = 0; static long long Ttick = 0; #define mod %% /* diff -r d1083f580ca1 -r 6a7ff66a811d targets/plc_debug.c --- a/targets/plc_debug.c Mon Sep 21 11:56:55 2009 +0200 +++ b/targets/plc_debug.c Mon Sep 21 12:00:52 2009 +0200 @@ -70,7 +70,7 @@ extern long AtomicCompareExchange(long*, long, long); extern void InitiateDebugTransfer(void); -extern int __tick; +extern unsigned long __tick; void __publish_debug() { /* Check there is no running debugger re-configuration */