targets/Win32/plc_Win32_main.c
author etisserant
Sat, 30 Aug 2008 22:14:06 +0200
changeset 231 f1db3ce8f40a
parent 229 8bc65076e290
child 244 85e92d9e34a8
permissions -rw-r--r--
Re-organized pyro connector proxy members mascarading
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     1
#include <stdio.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     2
#include <sys/timeb.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     3
#include <time.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     4
#include <windows.h>
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     5
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     6
long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     7
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     8
    return InterlockedCompareExchange(atomicvar, exchange, compared);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
     9
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    10
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    11
//long AtomicExchange(long* atomicvar,long exchange)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    12
//{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    13
//    return InterlockedExchange(atomicvar, exchange);    
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    14
//}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    15
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    16
struct _timeb timetmp;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    17
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    18
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    19
	_ftime(&timetmp);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    20
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    21
	(*CURRENT_TIME).tv_sec = timetmp.time;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    22
	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    23
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    24
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    25
void PLC_timer_notify()
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    26
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    27
    PLC_GetTime(&__CURRENT_TIME);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    28
    __run();
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    29
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    30
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    31
HANDLE PLC_timer = NULL;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    32
void PLC_SetTimer(long long next, long long period)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    33
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    34
	LARGE_INTEGER liDueTime;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    35
	/* arg 2 of SetWaitableTimer take 100 ns interval*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    36
	liDueTime.QuadPart =  next / (-100);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    37
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    38
	/*
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    39
	printf("SetTimer(%lld,%lld)\n",next, period);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    40
	*/
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    41
	
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    42
	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    43
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    44
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    45
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    46
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    47
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    48
/* Variable used to stop plcloop thread */
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    49
int runplcloop;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    50
void PlcLoop()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    51
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    52
	runplcloop = 1;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    53
	while(runplcloop)
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    54
	{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    55
	// Set a timer
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    56
	PLC_SetTimer(Ttick,Ttick);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    57
	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    58
	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    59
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    60
	}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    61
	PLC_timer_notify();
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    62
	}
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    63
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    64
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    65
HANDLE DebugLock;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    66
HANDLE PLC_thread;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    67
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    68
int startPLC(int argc,char **argv)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    69
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    70
	unsigned long thread_id = 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
	/* Translate PLC's microseconds to Ttick nanoseconds */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
	Ttick = 1000000 * maxval(common_ticktime__,1);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    74
	DebugLock = CreateMutex( 
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    75
	        	NULL,              // default security attributes
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    76
	        	FALSE,             // initially not owned
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    77
	        	NULL);             // unnamed mutex
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    78
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    79
    if (DebugLock == NULL) 
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    80
    {
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    81
        printf("CreateMutex error: %d\n", GetLastError());
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    82
        return;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    83
    }
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    84
	
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    85
	/* Create a waitable timer */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    86
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    87
    if(NULL == PLC_timer)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    88
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    89
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    90
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    91
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    92
    if( __init(argc,argv) == 0 )
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    93
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    94
    	printf("Tick Time : %d ms\n", common_ticktime__);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    95
    	PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    96
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    97
    else{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    98
    	return 1;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    99
    }
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   100
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   101
}
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   102
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   103
int stopPLC()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   104
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   105
	runplcloop = 0;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   106
	WaitForSingleObject(PLC_thread, INFINITE);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   107
	CloseHandle(PLC_thread);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   108
	CloseHandle(DebugLock);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   109
    CloseHandle(PLC_timer);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   110
    __cleanup();
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   111
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   112
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   113
/* from plc_debugger.c */
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   114
void WaitDebugData()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   115
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   116
	DWORD dwWaitResult;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   117
	dwWaitResult = WaitForSingleObject( 
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   118
					DebugLock,  // handle to mutex
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   119
					INFINITE);  // no time-out interval
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   120
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   121
 
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   122
/* Called by PLC thread when debug_publish finished
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   123
 * This is supposed to unlock debugger thread in WaitDebugData*/
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   124
void InitiateDebugTransfer()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   125
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   126
    /* signal debugger thread to continue*/
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   127
	ReleaseMutex(DebugLock);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   128
}