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 |
{
|
|
79 |
EnterMutex();
|
|
80 |
// At first, TimeDispatch will call init_callback.
|
|
81 |
SetAlarm(NULL, 0, init_callback, 0, 0);
|
|
82 |
LeaveMutex();
|
|
83 |
|
571
|
84 |
while(!stop_timer)
|
556
|
85 |
{
|
571
|
86 |
WaitForSingleObject(timer, INFINITE);
|
|
87 |
if(stop_timer)
|
|
88 |
break;
|
556
|
89 |
_ftime(&timebuffer);
|
|
90 |
EnterMutex();
|
|
91 |
TimeDispatch();
|
|
92 |
LeaveMutex();
|
|
93 |
}
|
|
94 |
return 0;
|
|
95 |
}
|
|
96 |
|
|
97 |
void TimerInit(void)
|
|
98 |
{
|
|
99 |
LARGE_INTEGER liDueTime;
|
|
100 |
liDueTime.QuadPart = 0;
|
|
101 |
|
|
102 |
InitializeCriticalSection(&CanFestival_mutex);
|
|
103 |
|
571
|
104 |
timer = CreateWaitableTimer(NULL, FALSE, NULL);
|
556
|
105 |
if(NULL == timer)
|
|
106 |
{
|
|
107 |
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
|
|
108 |
}
|
|
109 |
|
|
110 |
// Take first absolute time ref in milliseconds.
|
|
111 |
_ftime(&timebuffer);
|
|
112 |
}
|
|
113 |
|
|
114 |
void TimerCleanup(void)
|
|
115 |
{
|
|
116 |
DeleteCriticalSection(&CanFestival_mutex);
|
|
117 |
}
|
|
118 |
|
|
119 |
void StopTimerLoop(TimerCallback_t exitfunction)
|
|
120 |
{
|
|
121 |
EnterMutex();
|
|
122 |
exitfunction(NULL,0);
|
|
123 |
LeaveMutex();
|
|
124 |
|
|
125 |
stop_timer = 1;
|
571
|
126 |
setTimer(0);
|
|
127 |
WaitForSingleObject(timer_thread, INFINITE);
|
556
|
128 |
CloseHandle(timer);
|
|
129 |
CloseHandle(timer_thread);
|
|
130 |
}
|
|
131 |
|
|
132 |
void StartTimerLoop(TimerCallback_t _init_callback)
|
|
133 |
{
|
|
134 |
unsigned long timer_thread_id;
|
|
135 |
stop_timer = 0;
|
|
136 |
init_callback = _init_callback;
|
|
137 |
timer_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimerThreadLoop, NULL, 0, &timer_thread_id);
|
|
138 |
}
|
|
139 |
|
|
140 |
/* Set the next alarm */
|
|
141 |
void setTimer(TIMEVAL value)
|
|
142 |
{
|
|
143 |
if(value == TIMEVAL_MAX)
|
|
144 |
CancelWaitableTimer(timer);
|
|
145 |
else
|
|
146 |
{
|
|
147 |
LARGE_INTEGER liDueTime;
|
|
148 |
|
|
149 |
/* arg 2 of SetWaitableTimer take 100 ns interval */
|
591
|
150 |
liDueTime.QuadPart = (-1 * value);
|
556
|
151 |
//printf("SetTimer(%llu)\n", value);
|
|
152 |
|
|
153 |
if (!SetWaitableTimer(timer, &liDueTime, 0, NULL, NULL, FALSE))
|
|
154 |
{
|
|
155 |
printf("SetWaitableTimer failed (%d)\n", GetLastError());
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
/* Get the elapsed time since the last occured alarm */
|
|
161 |
TIMEVAL getElapsedTime(void)
|
|
162 |
{
|
|
163 |
struct _timeb timetmp;
|
|
164 |
_ftime(&timetmp);
|
571
|
165 |
return (timetmp.time - timebuffer.time) * 10000000 + (timetmp.millitm - timebuffer.millitm) * 10000;
|
556
|
166 |
}
|
|
167 |
|