Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
authorlaurent
Mon, 21 Sep 2009 12:00:52 +0200
changeset 397 6a7ff66a811d
parent 396 d1083f580ca1
child 398 31d08063b5d6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
targets/Linux/plc_Linux_main.c
targets/Win32/plc_Win32_main.c
targets/Xenomai/plc_Xenomai_main.c
targets/plc_common_main.c
targets/plc_debug.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;
--- 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 <windows.h>
 
 /* 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;
--- 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;
--- 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 <locale.h>
 #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 %%
 /*
--- 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 */