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 |
|
|
28 |
#include "applicfg.h"
|
|
29 |
#include "can_driver.h"
|
|
30 |
#include "timer.h"
|
|
31 |
#include "timers_driver.h"
|
|
32 |
|
|
33 |
|
|
34 |
struct _timeb timebuffer;
|
|
35 |
|
|
36 |
/* Synchronization Object Implementation */
|
|
37 |
CRITICAL_SECTION CanFestival_mutex;
|
|
38 |
HANDLE timer_thread = NULL;
|
|
39 |
HANDLE timer = NULL;
|
|
40 |
|
|
41 |
stop_timer=0;
|
|
42 |
|
|
43 |
static TimerCallback_t init_callback;
|
|
44 |
|
|
45 |
|
|
46 |
void EnterMutex(void)
|
|
47 |
{
|
|
48 |
EnterCriticalSection(&CanFestival_mutex);
|
|
49 |
}
|
|
50 |
|
|
51 |
void LeaveMutex(void)
|
|
52 |
{
|
|
53 |
LeaveCriticalSection(&CanFestival_mutex);
|
|
54 |
}
|
|
55 |
|
|
56 |
// --------------- CAN Receive Thread Implementation ---------------
|
|
57 |
|
|
58 |
void CreateReceiveTask(CAN_HANDLE fd0, TASK_HANDLE* Thread, void* ReceiveLoopPtr)
|
|
59 |
{
|
|
60 |
unsigned long thread_id = 0;
|
|
61 |
*Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveLoopPtr, fd0, 0, &thread_id);
|
|
62 |
}
|
|
63 |
|
|
64 |
void WaitReceiveTaskEnd(TASK_HANDLE *Thread)
|
|
65 |
{
|
|
66 |
WaitForSingleObject(*Thread, INFINITE);
|
|
67 |
CloseHandle(*Thread);
|
|
68 |
}
|
|
69 |
|
|
70 |
int TimerThreadLoop(void)
|
|
71 |
{
|
|
72 |
EnterMutex();
|
|
73 |
// At first, TimeDispatch will call init_callback.
|
|
74 |
SetAlarm(NULL, 0, init_callback, 0, 0);
|
|
75 |
LeaveMutex();
|
|
76 |
|
571
|
77 |
while(!stop_timer)
|
556
|
78 |
{
|
571
|
79 |
WaitForSingleObject(timer, INFINITE);
|
|
80 |
if(stop_timer)
|
|
81 |
break;
|
556
|
82 |
_ftime(&timebuffer);
|
|
83 |
EnterMutex();
|
|
84 |
TimeDispatch();
|
|
85 |
LeaveMutex();
|
|
86 |
}
|
|
87 |
return 0;
|
|
88 |
}
|
|
89 |
|
|
90 |
void TimerInit(void)
|
|
91 |
{
|
|
92 |
LARGE_INTEGER liDueTime;
|
|
93 |
liDueTime.QuadPart = 0;
|
|
94 |
|
|
95 |
InitializeCriticalSection(&CanFestival_mutex);
|
|
96 |
|
571
|
97 |
timer = CreateWaitableTimer(NULL, FALSE, NULL);
|
556
|
98 |
if(NULL == timer)
|
|
99 |
{
|
|
100 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
|
|
101 |
}
|
|
102 |
|
|
103 |
// Take first absolute time ref in milliseconds.
|
|
104 |
_ftime(&timebuffer);
|
|
105 |
}
|
|
106 |
|
|
107 |
void TimerCleanup(void)
|
|
108 |
{
|
|
109 |
DeleteCriticalSection(&CanFestival_mutex);
|
|
110 |
}
|
|
111 |
|
|
112 |
void StopTimerLoop(TimerCallback_t exitfunction)
|
|
113 |
{
|
|
114 |
EnterMutex();
|
|
115 |
exitfunction(NULL,0);
|
|
116 |
LeaveMutex();
|
|
117 |
|
|
118 |
stop_timer = 1;
|
571
|
119 |
setTimer(0);
|
|
120 |
WaitForSingleObject(timer_thread, INFINITE);
|
556
|
121 |
CloseHandle(timer);
|
|
122 |
CloseHandle(timer_thread);
|
|
123 |
}
|
|
124 |
|
|
125 |
void StartTimerLoop(TimerCallback_t _init_callback)
|
|
126 |
{
|
|
127 |
unsigned long timer_thread_id;
|
|
128 |
stop_timer = 0;
|
|
129 |
init_callback = _init_callback;
|
|
130 |
timer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimerThreadLoop, NULL, 0, &timer_thread_id);
|
|
131 |
}
|
|
132 |
|
|
133 |
/* Set the next alarm */
|
|
134 |
void setTimer(TIMEVAL value)
|
|
135 |
{
|
|
136 |
if(value == TIMEVAL_MAX)
|
|
137 |
CancelWaitableTimer(timer);
|
|
138 |
else
|
|
139 |
{
|
|
140 |
LARGE_INTEGER liDueTime;
|
|
141 |
|
|
142 |
/* arg 2 of SetWaitableTimer take 100 ns interval */
|
|
143 |
liDueTime.QuadPart = -value;
|
|
144 |
//printf("SetTimer(%llu)\n", value);
|
|
145 |
|
|
146 |
if (!SetWaitableTimer(timer, &liDueTime, 0, NULL, NULL, FALSE))
|
|
147 |
{
|
|
148 |
printf("SetWaitableTimer failed (%d)\n", GetLastError());
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
/* Get the elapsed time since the last occured alarm */
|
|
154 |
TIMEVAL getElapsedTime(void)
|
|
155 |
{
|
|
156 |
struct _timeb timetmp;
|
|
157 |
_ftime(&timetmp);
|
571
|
158 |
return (timetmp.time - timebuffer.time) * 10000000 + (timetmp.millitm - timebuffer.millitm) * 10000;
|
556
|
159 |
}
|
|
160 |
|