targets/Win32/plc_Win32_main.c
changeset 483 bc26c42d2eec
parent 469 a6a9b59636d8
child 484 3d0c06e2648c
equal deleted inserted replaced
482:7c83eb6a55bd 483:bc26c42d2eec
    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 	if (!SetWaitableTimer(PLC_timer, &liDueTime, period/1000000, NULL, NULL, 0))
    41 	printf("SetTimer(%lld,%lld)\n",next, period);
       
    42 	*/
       
    43 
       
    44 	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
       
    45     {
    41     {
    46         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    42         printf("SetWaitableTimer failed (%d)\n", GetLastError());
    47     }
    43     }
    48 }
    44 }
    49 
    45 
    50 /* Variable used to stop plcloop thread */
    46 /* Variable used to stop plcloop thread */
    51 int runplcloop;
       
    52 void PlcLoop()
    47 void PlcLoop()
    53 {
    48 {
    54 	runplcloop = 1;
    49     while(WaitForSingleObject(PLC_timer, INFINITE) == WAIT_OBJECT_0)
    55 	while(runplcloop)
    50     {
    56 	{
    51         PLC_timer_notify();
    57 	// Set a timer
    52     }
    58 	PLC_SetTimer(Ttick,Ttick);
       
    59 	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
       
    60 	{
       
    61 		printf("WaitForSingleObject failed (%d)\n", GetLastError());
       
    62 	}
       
    63 	PLC_timer_notify();
       
    64 	}
       
    65 }
    53 }
    66 
    54 
    67 HANDLE PLC_thread;
    55 HANDLE PLC_thread;
    68 HANDLE debug_sem;
    56 HANDLE debug_sem;
    69 HANDLE debug_wait_sem;
    57 HANDLE debug_wait_sem;
    75 {
    63 {
    76 	unsigned long thread_id = 0;
    64 	unsigned long thread_id = 0;
    77 	/* Define Ttick to 1ms if common_ticktime not defined */
    65 	/* Define Ttick to 1ms if common_ticktime not defined */
    78     Ttick = common_ticktime__?common_ticktime__:1000000;
    66     Ttick = common_ticktime__?common_ticktime__:1000000;
    79 
    67 
    80 	debug_sem = CreateSemaphore(
    68     debug_sem = CreateSemaphore(
    81 							NULL,           // default security attributes
    69                             NULL,           // default security attributes
    82 					        1,  			// initial count
    70                             1,  			// initial count
    83 					        1,  			// maximum count
    71                             1,  			// maximum count
    84 					        NULL);          // unnamed semaphore
    72                             NULL);          // unnamed semaphore
    85     if (debug_sem == NULL)
    73     if (debug_sem == NULL)
    86     {
    74     {
    87         printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
    75         printf("startPLC CreateSemaphore debug_sem error: %d\n", GetLastError());
    88         return;
    76         return;
    89     }
    77     }
    90 
    78 
    91 	debug_wait_sem = CreateSemaphore(
    79     debug_wait_sem = CreateSemaphore(
    92 					        NULL,           // default security attributes
    80                             NULL,           // default security attributes
    93 					        0,  			// initial count
    81                             0,  			// initial count
    94 					        1,  			// maximum count
    82                             1,  			// maximum count
    95 					        NULL);          // unnamed semaphore
    83                             NULL);          // unnamed semaphore
    96 
    84 
    97     if (debug_wait_sem == NULL)
    85     if (debug_wait_sem == NULL)
    98     {
    86     {
    99         printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
    87         printf("startPLC CreateSemaphore debug_wait_sem error: %d\n", GetLastError());
   100         return;
    88         return;
   101     }
    89     }
   102 
    90 
   103 	python_sem = CreateSemaphore(
    91     python_sem = CreateSemaphore(
   104 					        NULL,           // default security attributes
    92                             NULL,           // default security attributes
   105 					        1,  			// initial count
    93                             1,  			// initial count
   106 					        1,  			// maximum count
    94                             1,  			// maximum count
   107 					        NULL);          // unnamed semaphore
    95                             NULL);          // unnamed semaphore
   108 
    96 
   109     if (python_sem == NULL)
    97     if (python_sem == NULL)
   110     {
    98     {
   111         printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
    99         printf("startPLC CreateSemaphore python_sem error: %d\n", GetLastError());
   112         return;
   100         return;
   113     }
   101     }
   114 	python_wait_sem = CreateSemaphore(
   102     python_wait_sem = CreateSemaphore(
   115 					        NULL,           // default security attributes
   103                             NULL,           // default security attributes
   116 					        0,  			// initial count
   104                             0,  			// initial count
   117 					        1,  			// maximum count
   105                             1,  			// maximum count
   118 					        NULL);          // unnamed semaphore
   106                             NULL);          // unnamed semaphore
   119 
   107 
   120 
   108 
   121     if (python_wait_sem == NULL)
   109     if (python_wait_sem == NULL)
   122     {
   110     {
   123         printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
   111         printf("startPLC CreateSemaphore python_wait_sem error: %d\n", GetLastError());
   124         return;
   112         return;
   125     }
   113     }
   126 
   114 
   127 
   115 
   128 	/* Create a waitable timer */
   116     /* Create a waitable timer */
   129     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
   117     PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
   130     if(NULL == PLC_timer)
   118     if(NULL == PLC_timer)
   131     {
   119     {
   132         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   120         printf("CreateWaitableTimer failed (%d)\n", GetLastError());
   133         return 1;
   121         return 1;
   134     }
   122     }
   135     if( __init(argc,argv) == 0 )
   123     if( __init(argc,argv) == 0 )
   136     {
   124     {
   137     	printf("Tick Time : %d ms\n", common_ticktime__);
   125         PLC_SetTimer(Ttick,Ttick);
   138     	PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
   126         PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
   139     }
   127     }
   140     else{
   128     else{
   141     	return 1;
   129         return 1;
   142     }
   130     }
   143     return 0;
   131     return 0;
   144 }
   132 }
   145 static unsigned long __debug_tick;
   133 static unsigned long __debug_tick;
   146 
   134 
   150     if(WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0){
   138     if(WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0){
   151         /* Only enter if debug active */
   139         /* Only enter if debug active */
   152         if(__DEBUG){
   140         if(__DEBUG){
   153             return 1;
   141             return 1;
   154         }
   142         }
   155     ReleaseSemaphore(debug_sem, 1, NULL);
   143         ReleaseSemaphore(debug_sem, 1, NULL);
       
   144     }
   156     return 0;
   145     return 0;
   157     }
       
   158 }
   146 }
   159 
   147 
   160 void LeaveDebugSection(void)
   148 void LeaveDebugSection(void)
   161 {
   149 {
   162 	ReleaseSemaphore(debug_sem, 1, NULL);
   150 	ReleaseSemaphore(debug_sem, 1, NULL);
   163     //printf("LeaveDebugSection\n");
   151     //printf("LeaveDebugSection\n");
   164 }
   152 }
   165 
   153 
   166 int stopPLC()
   154 int stopPLC()
   167 {
   155 {
   168 	runplcloop = 0;
   156     CloseHandle(PLC_timer);
   169 	WaitForSingleObject(PLC_thread, INFINITE);
   157     WaitForSingleObject(PLC_thread, INFINITE);
   170 	__cleanup();
   158     __cleanup();
   171 	__debug_tick = -1;
   159     CloseHandle(debug_wait_sem);
   172 	ReleaseSemaphore(debug_wait_sem, 1, NULL);
   160     CloseHandle(debug_sem);
   173 	CloseHandle(debug_sem);
   161     CloseHandle(python_wait_sem);
   174 	CloseHandle(debug_wait_sem);
   162     CloseHandle(python_sem);
   175 	CloseHandle(PLC_timer);
   163     CloseHandle(PLC_thread);
   176 	CloseHandle(PLC_thread);
       
   177 }
   164 }
   178 
   165 
   179 /* from plc_debugger.c */
   166 /* from plc_debugger.c */
   180 int WaitDebugData(unsigned long *tick)
   167 int WaitDebugData(unsigned long *tick)
   181 {
   168 {
   195 }
   182 }
   196 
   183 
   197 void suspendDebug(int disable)
   184 void suspendDebug(int disable)
   198 {
   185 {
   199     /* Prevent PLC to enter debug code */
   186     /* Prevent PLC to enter debug code */
   200 	WaitForSingleObject(debug_sem, INFINITE);
   187     WaitForSingleObject(debug_sem, INFINITE);
   201     /*__DEBUG is protected by this mutex */
   188     /*__DEBUG is protected by this mutex */
   202     __DEBUG = !disable;
   189     __DEBUG = !disable;
   203 }
   190 }
   204 
   191 
   205 void resumeDebug()
   192 void resumeDebug()
   236 void LockPython(void)
   223 void LockPython(void)
   237 {
   224 {
   238 	WaitForSingleObject(python_sem, INFINITE);
   225 	WaitForSingleObject(python_sem, INFINITE);
   239 }
   226 }
   240 
   227 
       
   228 void Retain(unsigned int offset, unsigned int count, void * p)
       
   229 {
       
   230     unsigned int position;
       
   231     for(position=0; position<count; position++ ){
       
   232         printf("%d : 0x%2.2x\n", offset+position, ((char*)p)[position]);
       
   233     }
       
   234 }
       
   235 
       
   236 void Remind(unsigned int offset, unsigned int count, void *p)
       
   237 {
       
   238 }
       
   239