author | greg |
Thu, 08 Oct 2009 17:21:15 +0200 | |
changeset 605 | f91ee161b3a1 |
parent 591 | 513f5ec8b628 |
child 606 | 15a175f6305d |
permissions | -rw-r--r-- |
556 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
Copyright (C) Win32 Port Leonid Tochinski |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
||
24 |
#include <windows.h> |
|
25 |
#include <stdlib.h> |
|
26 |
#include <sys/timeb.h> |
|
27 |
||
591 | 28 |
#ifdef __cplusplus |
29 |
extern "C" { |
|
30 |
#endif |
|
31 |
||
556 | 32 |
#include "applicfg.h" |
33 |
#include "can_driver.h" |
|
34 |
#include "timer.h" |
|
35 |
#include "timers_driver.h" |
|
591 | 36 |
|
37 |
#ifdef __cplusplus |
|
577 | 38 |
}; |
591 | 39 |
#endif |
556 | 40 |
|
41 |
struct _timeb timebuffer; |
|
42 |
||
43 |
/* Synchronization Object Implementation */ |
|
44 |
CRITICAL_SECTION CanFestival_mutex; |
|
45 |
HANDLE timer_thread = NULL; |
|
46 |
HANDLE timer = NULL; |
|
47 |
||
575 | 48 |
int stop_timer=0; |
556 | 49 |
|
50 |
static TimerCallback_t init_callback; |
|
51 |
||
52 |
||
53 |
void EnterMutex(void) |
|
54 |
{ |
|
55 |
EnterCriticalSection(&CanFestival_mutex); |
|
56 |
} |
|
57 |
||
58 |
void LeaveMutex(void) |
|
59 |
{ |
|
60 |
LeaveCriticalSection(&CanFestival_mutex); |
|
61 |
} |
|
62 |
||
63 |
// --------------- CAN Receive Thread Implementation --------------- |
|
64 |
||
65 |
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread, void* ReceiveLoopPtr) |
|
66 |
{ |
|
67 |
unsigned long thread_id = 0; |
|
68 |
*Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveLoopPtr, fd0, 0, &thread_id); |
|
69 |
} |
|
70 |
||
71 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread) |
|
72 |
{ |
|
73 |
WaitForSingleObject(*Thread, INFINITE); |
|
74 |
CloseHandle(*Thread); |
|
75 |
} |
|
76 |
||
77 |
int TimerThreadLoop(void) |
|
78 |
{ |
|
605
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
79 |
|
556 | 80 |
|
571 | 81 |
while(!stop_timer) |
556 | 82 |
{ |
571 | 83 |
WaitForSingleObject(timer, INFINITE); |
84 |
if(stop_timer) |
|
85 |
break; |
|
556 | 86 |
_ftime(&timebuffer); |
87 |
EnterMutex(); |
|
88 |
TimeDispatch(); |
|
89 |
LeaveMutex(); |
|
90 |
} |
|
91 |
return 0; |
|
92 |
} |
|
93 |
||
94 |
void TimerInit(void) |
|
95 |
{ |
|
96 |
LARGE_INTEGER liDueTime; |
|
97 |
liDueTime.QuadPart = 0; |
|
98 |
||
99 |
InitializeCriticalSection(&CanFestival_mutex); |
|
100 |
||
571 | 101 |
timer = CreateWaitableTimer(NULL, FALSE, NULL); |
556 | 102 |
if(NULL == timer) |
103 |
{ |
|
104 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError()); |
|
105 |
} |
|
106 |
||
107 |
// Take first absolute time ref in milliseconds. |
|
108 |
_ftime(&timebuffer); |
|
109 |
} |
|
110 |
||
111 |
void TimerCleanup(void) |
|
112 |
{ |
|
113 |
DeleteCriticalSection(&CanFestival_mutex); |
|
114 |
} |
|
115 |
||
116 |
void StopTimerLoop(TimerCallback_t exitfunction) |
|
117 |
{ |
|
118 |
EnterMutex(); |
|
119 |
exitfunction(NULL,0); |
|
120 |
LeaveMutex(); |
|
121 |
||
122 |
stop_timer = 1; |
|
571 | 123 |
setTimer(0); |
124 |
WaitForSingleObject(timer_thread, INFINITE); |
|
556 | 125 |
CloseHandle(timer); |
126 |
CloseHandle(timer_thread); |
|
127 |
} |
|
128 |
||
129 |
void StartTimerLoop(TimerCallback_t _init_callback) |
|
130 |
{ |
|
131 |
unsigned long timer_thread_id; |
|
132 |
stop_timer = 0; |
|
133 |
init_callback = _init_callback; |
|
605
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
134 |
EnterMutex(); |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
135 |
// At first, TimeDispatch will call init_callback. |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
136 |
SetAlarm(NULL, 0, init_callback, 0, 0); |
f91ee161b3a1
-add LeaveMutex to avoid canopenshell deadlock when call NodeInit
greg
parents:
591
diff
changeset
|
137 |
LeaveMutex(); |
556 | 138 |
timer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimerThreadLoop, NULL, 0, &timer_thread_id); |
139 |
} |
|
140 |
||
141 |
/* Set the next alarm */ |
|
142 |
void setTimer(TIMEVAL value) |
|
143 |
{ |
|
144 |
if(value == TIMEVAL_MAX) |
|
145 |
CancelWaitableTimer(timer); |
|
146 |
else |
|
147 |
{ |
|
148 |
LARGE_INTEGER liDueTime; |
|
149 |
||
150 |
/* arg 2 of SetWaitableTimer take 100 ns interval */ |
|
591 | 151 |
liDueTime.QuadPart = (-1 * value); |
556 | 152 |
//printf("SetTimer(%llu)\n", value); |
153 |
||
154 |
if (!SetWaitableTimer(timer, &liDueTime, 0, NULL, NULL, FALSE)) |
|
155 |
{ |
|
156 |
printf("SetWaitableTimer failed (%d)\n", GetLastError()); |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/* Get the elapsed time since the last occured alarm */ |
|
162 |
TIMEVAL getElapsedTime(void) |
|
163 |
{ |
|
164 |
struct _timeb timetmp; |
|
165 |
_ftime(&timetmp); |
|
571 | 166 |
return (timetmp.time - timebuffer.time) * 10000000 + (timetmp.millitm - timebuffer.millitm) * 10000; |
556 | 167 |
} |
168 |