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