205
|
1 |
#include <stdio.h>
|
|
2 |
#include <sys/timeb.h>
|
|
3 |
#include <time.h>
|
|
4 |
#include <windows.h>
|
|
5 |
|
244
|
6 |
long AtomicCompareExchange(long* atomicvar, long compared, long exchange)
|
205
|
7 |
{
|
|
8 |
return InterlockedCompareExchange(atomicvar, exchange, compared);
|
|
9 |
}
|
|
10 |
|
|
11 |
//long AtomicExchange(long* atomicvar,long exchange)
|
|
12 |
//{
|
|
13 |
// return InterlockedExchange(atomicvar, exchange);
|
|
14 |
//}
|
|
15 |
|
|
16 |
struct _timeb timetmp;
|
|
17 |
void PLC_GetTime(IEC_TIME *CURRENT_TIME)
|
|
18 |
{
|
|
19 |
_ftime(&timetmp);
|
|
20 |
|
|
21 |
(*CURRENT_TIME).tv_sec = timetmp.time;
|
|
22 |
(*CURRENT_TIME).tv_nsec = timetmp.millitm * 1000000;
|
|
23 |
}
|
|
24 |
|
|
25 |
void PLC_timer_notify()
|
|
26 |
{
|
|
27 |
PLC_GetTime(&__CURRENT_TIME);
|
|
28 |
__run();
|
|
29 |
}
|
|
30 |
|
|
31 |
HANDLE PLC_timer = NULL;
|
|
32 |
void PLC_SetTimer(long long next, long long period)
|
|
33 |
{
|
229
|
34 |
LARGE_INTEGER liDueTime;
|
205
|
35 |
/* arg 2 of SetWaitableTimer take 100 ns interval*/
|
|
36 |
liDueTime.QuadPart = next / (-100);
|
|
37 |
|
|
38 |
/*
|
|
39 |
printf("SetTimer(%lld,%lld)\n",next, period);
|
|
40 |
*/
|
|
41 |
|
|
42 |
if (!SetWaitableTimer(PLC_timer, &liDueTime, common_ticktime__, NULL, NULL, 0))
|
|
43 |
{
|
|
44 |
printf("SetWaitableTimer failed (%d)\n", GetLastError());
|
|
45 |
}
|
229
|
46 |
}
|
|
47 |
|
|
48 |
/* Variable used to stop plcloop thread */
|
|
49 |
int runplcloop;
|
|
50 |
void PlcLoop()
|
|
51 |
{
|
|
52 |
runplcloop = 1;
|
|
53 |
while(runplcloop)
|
|
54 |
{
|
|
55 |
// Set a timer
|
|
56 |
PLC_SetTimer(Ttick,Ttick);
|
205
|
57 |
if (WaitForSingleObject(PLC_timer, INFINITE) != WAIT_OBJECT_0)
|
|
58 |
{
|
|
59 |
printf("WaitForSingleObject failed (%d)\n", GetLastError());
|
|
60 |
}
|
|
61 |
PLC_timer_notify();
|
229
|
62 |
}
|
205
|
63 |
}
|
|
64 |
|
229
|
65 |
HANDLE PLC_thread;
|
244
|
66 |
HANDLE debug_sem;
|
|
67 |
HANDLE wait_sem;
|
|
68 |
#define MAX_SEM_COUNT 10
|
229
|
69 |
|
|
70 |
int startPLC(int argc,char **argv)
|
205
|
71 |
{
|
229
|
72 |
unsigned long thread_id = 0;
|
205
|
73 |
/* Translate PLC's microseconds to Ttick nanoseconds */
|
|
74 |
Ttick = 1000000 * maxval(common_ticktime__,1);
|
|
75 |
|
244
|
76 |
debug_sem = CreateSemaphore(
|
|
77 |
NULL, // default security attributes
|
|
78 |
1, // initial count
|
|
79 |
MAX_SEM_COUNT, // maximum count
|
|
80 |
NULL); // unnamed semaphore
|
|
81 |
if (debug_sem == NULL)
|
|
82 |
{
|
|
83 |
printf("CreateMutex error: %d\n", GetLastError());
|
|
84 |
return;
|
|
85 |
}
|
|
86 |
|
|
87 |
wait_sem = CreateSemaphore(
|
|
88 |
NULL, // default security attributes
|
|
89 |
0, // initial count
|
|
90 |
MAX_SEM_COUNT, // maximum count
|
|
91 |
NULL); // unnamed semaphore
|
229
|
92 |
|
244
|
93 |
if (wait_sem == NULL)
|
229
|
94 |
{
|
|
95 |
printf("CreateMutex error: %d\n", GetLastError());
|
|
96 |
return;
|
|
97 |
}
|
|
98 |
|
205
|
99 |
/* Create a waitable timer */
|
|
100 |
PLC_timer = CreateWaitableTimer(NULL, FALSE, "WaitableTimer");
|
|
101 |
if(NULL == PLC_timer)
|
|
102 |
{
|
|
103 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
|
|
104 |
return 1;
|
|
105 |
}
|
|
106 |
if( __init(argc,argv) == 0 )
|
|
107 |
{
|
|
108 |
printf("Tick Time : %d ms\n", common_ticktime__);
|
229
|
109 |
PLC_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PlcLoop, NULL, 0, &thread_id);
|
205
|
110 |
}
|
229
|
111 |
else{
|
|
112 |
return 1;
|
|
113 |
}
|
205
|
114 |
return 0;
|
|
115 |
}
|
244
|
116 |
static int __debug_tick;
|
|
117 |
|
|
118 |
int TryEnterDebugSection(void)
|
|
119 |
{
|
|
120 |
//printf("TryEnterDebugSection\n");
|
|
121 |
return WaitForSingleObject(debug_sem, 0) == WAIT_OBJECT_0;
|
|
122 |
}
|
|
123 |
|
|
124 |
void LeaveDebugSection(void)
|
|
125 |
{
|
|
126 |
ReleaseSemaphore(debug_sem, 1, NULL);
|
|
127 |
//printf("LeaveDebugSection\n");
|
|
128 |
}
|
229
|
129 |
|
|
130 |
int stopPLC()
|
|
131 |
{
|
|
132 |
runplcloop = 0;
|
|
133 |
WaitForSingleObject(PLC_thread, INFINITE);
|
244
|
134 |
__cleanup();
|
|
135 |
__debug_tick = -1;
|
|
136 |
ReleaseSemaphore(wait_sem, 1, NULL);
|
|
137 |
CloseHandle(debug_sem);
|
|
138 |
CloseHandle(wait_sem);
|
|
139 |
CloseHandle(PLC_timer);
|
229
|
140 |
CloseHandle(PLC_thread);
|
|
141 |
}
|
|
142 |
|
|
143 |
/* from plc_debugger.c */
|
244
|
144 |
int WaitDebugData()
|
229
|
145 |
{
|
244
|
146 |
WaitForSingleObject(wait_sem, INFINITE);
|
|
147 |
return __debug_tick;
|
229
|
148 |
}
|
|
149 |
|
|
150 |
/* Called by PLC thread when debug_publish finished
|
|
151 |
* This is supposed to unlock debugger thread in WaitDebugData*/
|
|
152 |
void InitiateDebugTransfer()
|
|
153 |
{
|
244
|
154 |
/* Leave debugger section */
|
|
155 |
ReleaseSemaphore(debug_sem, 1, NULL);
|
|
156 |
/* remember tick */
|
|
157 |
__debug_tick = __tick;
|
|
158 |
/* signal debugger thread it can read data */
|
|
159 |
ReleaseSemaphore(wait_sem, 1, NULL);
|
229
|
160 |
}
|
244
|
161 |
|
|
162 |
void suspendDebug()
|
|
163 |
{
|
|
164 |
/* Prevent PLC to enter debug code */
|
|
165 |
WaitForSingleObject(debug_sem, INFINITE);
|
|
166 |
}
|
|
167 |
|
|
168 |
void resumeDebug()
|
|
169 |
{
|
|
170 |
/* Let PLC enter debug code */
|
|
171 |
ReleaseSemaphore(debug_sem, 1, NULL);
|
|
172 |
}
|