targets/Win32/plc_Win32_main.c
author greg
Wed, 20 Aug 2008 10:10:34 +0200
changeset 205 ee8d1f4528ef
child 229 8bc65076e290
permissions -rw-r--r--
move specific target runtimes to their targets directory
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
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    34
	LARGE_INTEGER liDueTime;		
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
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    46
	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    47
	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    48
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    49
	}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    50
	PLC_timer_notify();
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    51
}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    52
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    53
int main(int argc,char **argv)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    54
{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    55
	/* Translate PLC's microseconds to Ttick nanoseconds */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    56
	Ttick = 1000000 * maxval(common_ticktime__,1);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    57
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    58
	/* Create a waitable timer */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    59
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    60
    if(NULL == PLC_timer)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    61
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    62
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    63
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    64
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    65
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    66
    if( __init(argc,argv) == 0 )
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    67
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    68
    	printf("Tick Time : %d ms\n", common_ticktime__);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    69
    	while(1)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    70
    	{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
    		// Set a timer
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    72
    		PLC_SetTimer(Ttick,Ttick);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
    		if (kbhit())
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    74
    		{
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    75
    			printf("Finishing\n");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    76
    		    break;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    77
            }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    78
    	}
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    79
    	PLC_SetTimer(0,0);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    80
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    81
    __cleanup();
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    82
    CloseHandle(PLC_timer);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    83
    		
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    84
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    85
}