targets/Win32/plc_Win32_main.c
author greg
Mon, 24 Nov 2008 08:57:43 +0100
changeset 271 ea7928fd07da
parent 247 655d5fef0204
child 276 0d7426f26c6f
permissions -rw-r--r--
add the possibility to enable or disable wxTaskbarIcon for Beremiz_service
Launch beremiz without taskbarIcon by default (incompatible with svgui plugin)
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
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
     6
long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
205
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 PLC_thread;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    66
HANDLE debug_sem;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    67
HANDLE wait_sem; 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    68
#define MAX_SEM_COUNT 10
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    69
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    70
int startPLC(int argc,char **argv)
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    71
{
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    72
	unsigned long thread_id = 0;
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    73
	/* Translate PLC's microseconds to Ttick nanoseconds */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    74
	Ttick = 1000000 * maxval(common_ticktime__,1);
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    75
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    76
	debug_sem = CreateSemaphore( 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    77
							NULL,           // default security attributes
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    78
					        1,  			// initial count
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    79
					        MAX_SEM_COUNT,  // maximum count
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    80
					        NULL);          // unnamed semaphore
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    81
    if (debug_sem == NULL) 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    82
    {
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    83
        printf("CreateMutex error: %d\n", GetLastError());
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    84
        return;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    85
    }
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    86
    
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    87
	wait_sem = CreateSemaphore( 
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    88
					        NULL,           // default security attributes
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    89
					        0,  			// initial count
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    90
					        MAX_SEM_COUNT,  // maximum count
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    91
					        NULL);          // unnamed semaphore
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    92
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
    93
    if (wait_sem == NULL) 
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    94
    {
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    95
        printf("CreateMutex error: %d\n", GetLastError());
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    96
        return;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    97
    }
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
    98
	
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
    99
	/* Create a waitable timer */
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   100
    PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   101
    if(NULL == PLC_timer)
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   102
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   103
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   104
        return 1;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   105
    }
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   106
    if( __init(argc,argv) == 0 )
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   107
    {
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   108
    	printf("Tick Time : %d ms\n", common_ticktime__);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   109
    	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
   110
    }
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   111
    else{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   112
    	return 1;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   113
    }
205
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   114
    return 0;
ee8d1f4528ef move specific target runtimes to their targets directory
greg
parents:
diff changeset
   115
}
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   116
static int __debug_tick;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   117
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   118
int TryEnterDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   119
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   120
	//printf("TryEnterDebugSection\n");
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   121
	return WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   122
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   123
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   124
void LeaveDebugSection(void)
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   125
{
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   126
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   127
    //printf("LeaveDebugSection\n");
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   128
}
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   129
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   130
int stopPLC()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   131
{
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   132
	runplcloop = 0;
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   133
	WaitForSingleObject(PLC_thread, INFINITE);
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   134
	__cleanup();
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   135
	__debug_tick = -1;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   136
	ReleaseSemaphore(wait_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   137
	CloseHandle(debug_sem);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   138
	CloseHandle(wait_sem);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   139
	CloseHandle(PLC_timer);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   140
	CloseHandle(PLC_thread);
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   141
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   142
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   143
/* from plc_debugger.c */
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   144
int WaitDebugData()
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   145
{
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   146
	WaitForSingleObject(wait_sem, INFINITE);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   147
	return __debug_tick;
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   148
}
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   149
 
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   150
/* Called by PLC thread when debug_publish finished
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   151
 * This is supposed to unlock debugger thread in WaitDebugData*/
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   152
void InitiateDebugTransfer()
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   153
{
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   154
	/* Leave debugger section */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   155
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   156
    /* remember tick */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   157
    __debug_tick = __tick;
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   158
    /* signal debugger thread it can read data */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   159
    ReleaseSemaphore(wait_sem, 1, NULL);
229
8bc65076e290 add tests for win32
greg
parents: 205
diff changeset
   160
}
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   161
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   162
void suspendDebug()
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   163
{
247
655d5fef0204 add __DEBUG variable
greg
parents: 244
diff changeset
   164
	__DEBUG = 0;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   165
    /* Prevent PLC to enter debug code */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   166
	WaitForSingleObject(debug_sem, INFINITE);  
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   167
}
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   168
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   169
void resumeDebug()
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   170
{
247
655d5fef0204 add __DEBUG variable
greg
parents: 244
diff changeset
   171
	__DEBUG = 1;
244
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   172
    /* Let PLC enter debug code */
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   173
	ReleaseSemaphore(debug_sem, 1, NULL);
85e92d9e34a8 fixed : bug in debugthreadproc (plugger.py)
greg
parents: 229
diff changeset
   174
}