targets/Win32/plc_Win32_main.c
changeset 329 22e65b8e20f4
parent 290 3bd617ae7a05
child 397 6a7ff66a811d
equal deleted inserted replaced
328:c23daa6996c2 329:22e65b8e20f4
     1 /**
     1 /**
     2  * Win32 specific code
     2  * Win32 specific code
     3  **/ 
     3  **/
     4 
     4 
     5 #include <stdio.h>
     5 #include <stdio.h>
     6 #include <sys/timeb.h>
     6 #include <sys/timeb.h>
     7 #include <time.h>
     7 #include <time.h>
     8 #include <windows.h>
     8 #include <windows.h>
    17 
    17 
    18 struct _timeb timetmp;
    18 struct _timeb timetmp;
    19 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
    19 void PLC_GetTime(IEC_TIME *CURRENT_TIME)
    20 {
    20 {
    21 	_ftime(&timetmp);
    21 	_ftime(&timetmp);
    22 	
    22 
    23 	(*CURRENT_TIME).tv_sec = timetmp.time;
    23 	(*CURRENT_TIME).tv_sec = timetmp.time;
    24 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
    24 	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
    25 }
    25 }
    26 
    26 
    27 void PLC_timer_notify()
    27 void PLC_timer_notify()
    34 void PLC_SetTimer(long long next, long long period)
    34 void PLC_SetTimer(long long next, long long period)
    35 {
    35 {
    36 	LARGE_INTEGER liDueTime;
    36 	LARGE_INTEGER liDueTime;
    37 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    37 	/* arg 2 of SetWaitableTimer take 100 ns interval*/
    38 	liDueTime.QuadPart =  next / (-100);
    38 	liDueTime.QuadPart =  next / (-100);
    39 	
    39 
    40 	/*
    40 	/*
    41 	printf("SetTimer(%lld,%lld)\n",next, period);
    41 	printf("SetTimer(%lld,%lld)\n",next, period);
    42 	*/
    42 	*/
    43 	
    43 
    44 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
    44 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
    45     {
    45     {
    46         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    46         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    47     }
    47     }
    48 }
    48 }
    64 	}
    64 	}
    65 }
    65 }
    66 
    66 
    67 HANDLE PLC_thread;
    67 HANDLE PLC_thread;
    68 HANDLE debug_sem;
    68 HANDLE debug_sem;
    69 HANDLE debug_wait_sem; 
    69 HANDLE debug_wait_sem;
    70 HANDLE python_sem;
    70 HANDLE python_sem;
    71 HANDLE python_wait_sem; 
    71 HANDLE python_wait_sem;
    72 
    72 
    73 #define maxval(a,b) ((a>b)?a:b)
    73 #define maxval(a,b) ((a>b)?a:b)
    74 int startPLC(int argc,char **argv)
    74 int startPLC(int argc,char **argv)
    75 {
    75 {
    76 	unsigned long thread_id = 0;
    76 	unsigned long thread_id = 0;
    77 	/* Translate PLC's microseconds to Ttick nanoseconds */
    77 	/* Translate PLC's microseconds to Ttick nanoseconds */
    78 	Ttick = 1000000 * maxval(common_ticktime__,1);
    78 	Ttick = 1000000 * maxval(common_ticktime__,1);
    79 
    79 
    80 	debug_sem = CreateSemaphore( 
    80 	debug_sem = CreateSemaphore(
    81 							NULL,           // default security attributes
    81 							NULL,           // default security attributes
    82 					        1,  			// initial count
    82 					        1,  			// initial count
    83 					        1,  			// maximum count
    83 					        1,  			// maximum count
    84 					        NULL);          // unnamed semaphore
    84 					        NULL);          // unnamed semaphore
    85     if (debug_sem == NULL) 
    85     if (debug_sem == NULL)
    86     {
    86     {
    87         printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
    87         printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
    88         return;
    88         return;
    89     }
    89     }
    90     
    90 
    91 	debug_wait_sem = CreateSemaphore( 
    91 	debug_wait_sem = CreateSemaphore(
    92 					        NULL,           // default security attributes
    92 					        NULL,           // default security attributes
    93 					        0,  			// initial count
    93 					        0,  			// initial count
    94 					        1,  			// maximum count
    94 					        1,  			// maximum count
    95 					        NULL);          // unnamed semaphore
    95 					        NULL);          // unnamed semaphore
    96 
    96 
    97     if (debug_wait_sem == NULL) 
    97     if (debug_wait_sem == NULL)
    98     {
    98     {
    99         printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
    99         printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
   100         return;
   100         return;
   101     }
   101     }
   102 
   102 
   103 	python_sem = CreateSemaphore( 
   103 	python_sem = CreateSemaphore(
   104 					        NULL,           // default security attributes
   104 					        NULL,           // default security attributes
   105 					        1,  			// initial count
   105 					        1,  			// initial count
   106 					        1,  			// maximum count
   106 					        1,  			// maximum count
   107 					        NULL);          // unnamed semaphore
   107 					        NULL);          // unnamed semaphore
   108 
   108 
   109     if (python_sem == NULL) 
   109     if (python_sem == NULL)
   110     {
   110     {
   111         printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
   111         printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
   112         return;
   112         return;
   113     }
   113     }
   114 	python_wait_sem = CreateSemaphore( 
   114 	python_wait_sem = CreateSemaphore(
   115 					        NULL,           // default security attributes
   115 					        NULL,           // default security attributes
   116 					        0,  			// initial count
   116 					        0,  			// initial count
   117 					        1,  			// maximum count
   117 					        1,  			// maximum count
   118 					        NULL);          // unnamed semaphore
   118 					        NULL);          // unnamed semaphore
   119 
   119 
   120 
   120 
   121     if (python_wait_sem == NULL) 
   121     if (python_wait_sem == NULL)
   122     {
   122     {
   123         printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
   123         printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
   124         return;
   124         return;
   125     }
   125     }
   126 
   126 
   127 	
   127 
   128 	/* Create a waitable timer */
   128 	/* Create a waitable timer */
   129     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
   129     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
   130     if(NULL == PLC_timer)
   130     if(NULL == PLC_timer)
   131     {
   131     {
   132         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   132         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   170 }
   170 }
   171 
   171 
   172 /* from plc_debugger.c */
   172 /* from plc_debugger.c */
   173 int WaitDebugData()
   173 int WaitDebugData()
   174 {
   174 {
   175 	WaitForSingleObject(debug_wait_sem, INFINITE);
   175 	if(WaitForSingleObject(debug_wait_sem, INFINITE) != WAIT_OBJECT_0) return -1;
   176 	return __debug_tick;
   176 	return __debug_tick;
   177 }
   177 }
   178  
   178 
   179 /* Called by PLC thread when debug_publish finished
   179 /* Called by PLC thread when debug_publish finished
   180  * This is supposed to unlock debugger thread in WaitDebugData*/
   180  * This is supposed to unlock debugger thread in WaitDebugData*/
   181 void InitiateDebugTransfer()
   181 void InitiateDebugTransfer()
   182 {
   182 {
   183     /* remember tick */
   183     /* remember tick */
   188 
   188 
   189 void suspendDebug()
   189 void suspendDebug()
   190 {
   190 {
   191 	__DEBUG = 0;
   191 	__DEBUG = 0;
   192     /* Prevent PLC to enter debug code */
   192     /* Prevent PLC to enter debug code */
   193 	WaitForSingleObject(debug_sem, INFINITE);  
   193 	WaitForSingleObject(debug_sem, INFINITE);
   194 }
   194 }
   195 
   195 
   196 void resumeDebug()
   196 void resumeDebug()
   197 {
   197 {
   198 	__DEBUG = 1;
   198 	__DEBUG = 1;
   202 
   202 
   203 /* from plc_python.c */
   203 /* from plc_python.c */
   204 int WaitPythonCommands(void)
   204 int WaitPythonCommands(void)
   205 {
   205 {
   206     /* Wait signal from PLC thread */
   206     /* Wait signal from PLC thread */
   207 	WaitForSingleObject(python_wait_sem, INFINITE);
   207 	return WaitForSingleObject(python_wait_sem, INFINITE);
   208 }
   208 }
   209  
   209 
   210 /* Called by PLC thread on each new python command*/
   210 /* Called by PLC thread on each new python command*/
   211 void UnBlockPythonCommands(void)
   211 void UnBlockPythonCommands(void)
   212 {
   212 {
   213     /* signal debugger thread it can read data */
   213     /* signal debugger thread it can read data */
   214 	ReleaseSemaphore(python_wait_sem, 1, NULL);
   214 	ReleaseSemaphore(python_wait_sem, 1, NULL);