runtime/plc_Win32_main.c
author etisserant
Wed, 20 Aug 2008 00:11:40 +0200
changeset 203 cb9901076a21
parent 196 93d06827e31b
permissions -rw-r--r--
Added concepts :
- connector class (for PLC control, debug and flashing abstraction).
- builder class (C toolchain abstraction)

Added features :
- Pyro based connector.
- ctypes loader, load PLC shared object and run it
- ctypes based debugger embryo (not tested)
- gcc builder

Broken:
- Win32 runtime
- Most tests
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     1
#include <stdio.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     2
#include <sys/timeb.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     3
#include <time.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     4
#include <windows.h>
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
     5
203
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
     6
long AtomicCompareExchange(long* atomicvar,long exchange, long compared)
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
     7
{
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
     8
    return InterlockedCompareExchange(atomicvar, exchange, compared);
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
     9
}
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
    10
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
    11
//long AtomicExchange(long* atomicvar,long exchange)
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
    12
//{
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
    13
//    return InterlockedExchange(atomicvar, exchange);    
cb9901076a21 Added concepts :
etisserant
parents: 196
diff changeset
    14
//}
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 79
diff changeset
    15
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    16
struct _timeb timetmp;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    17
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    18
{
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    19
	_ftime(&timetmp);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    20
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    21
	(*CURRENT_TIME).tv_sec = timetmp.time;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    22
	(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    23
}
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    24
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    25
void PLC_timer_notify()
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    26
{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    27
    PLC_GetTime(&__CURRENT_TIME);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    28
    __run();
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    29
}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    30
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    31
HANDLE PLC_timer = NULL;
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    32
void PLC_SetTimer(long long next, long long period)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    33
{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    34
	LARGE_INTEGER liDueTime;		
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    35
	/* arg 2 of SetWaitableTimer take 100 ns interval*/
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    36
	liDueTime.QuadPart =  next / (-100);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    37
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    38
	/*
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    39
	printf("SetTimer(%lld,%lld)\n",next, period);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    40
	*/
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    41
	
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    42
	if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    43
    {
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    44
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    45
    }
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    46
	if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    47
	{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    48
		printf("WaitForSingleObject failed (%d)\n", GetLastError());
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    49
	}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    50
	PLC_timer_notify();
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    51
}
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    52
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    53
int main(int argc,char **argv)
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    54
{
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    55
	/* Translate PLC's microseconds to Ttick nanoseconds */
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    56
	Ttick = 1000000 * maxval(common_ticktime__,1);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    57
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    58
	/* Create a waitable timer */
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    59
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    60
    if(NULL == PLC_timer)
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    61
    {
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    62
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    63
        return 1;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    64
    }
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    65
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    66
    if( __init(argc,argv) == 0 )
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    67
    {
75
9ad18a387a96 Windows related enhancements
etisserant
parents: 57
diff changeset
    68
    	printf("Tick Time : %d ms\n", common_ticktime__);
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    69
    	while(1)
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    70
    	{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    71
    		// Set a timer
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    72
    		PLC_SetTimer(Ttick,Ttick);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    73
    		if (kbhit())
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    74
    		{
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    75
    			printf("Finishing\n");
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    76
    		    break;
57
3b53f9a509d9 Basic CANOpen master node test compiles and run.
etisserant
parents: 49
diff changeset
    77
            }
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    78
    	}
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    79
    	PLC_SetTimer(0,0);
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    80
    }
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    81
    __cleanup();
196
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    82
    CloseHandle(PLC_timer);
93d06827e31b changes in plc_Win32_main to match with new api (alignment feature)
greg
parents: 110
diff changeset
    83
    		
49
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    84
    return 0;
45dc6a944ab6 On the long wat towards generated code comilation...
etisserant
parents:
diff changeset
    85
}