author | Edouard Tisserant |
Fri, 03 Feb 2012 15:43:44 +0100 | |
changeset 693 | e21822be8189 |
parent 691 | 17d9c0736935 |
child 696 | 381af6bb6155 |
permissions | -rwxr-xr-x |
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 |
/* |
|
25 |
CAN driver interface. |
|
26 |
*/ |
|
27 |
||
28 |
#include <windows.h> |
|
591 | 29 |
|
30 |
#ifdef __cplusplus |
|
31 |
extern "C" { |
|
32 |
#endif |
|
33 |
||
556 | 34 |
#include "canfestival.h" |
35 |
#include "timer.h" |
|
36 |
#include "timers_driver.h" |
|
591 | 37 |
|
38 |
#ifdef __cplusplus |
|
577 | 39 |
}; |
591 | 40 |
#endif |
41 |
||
556 | 42 |
// GetProcAddress doesn't have an UNICODE version for NT |
43 |
#ifdef UNDER_CE |
|
44 |
#define myTEXT(str) TEXT(str) |
|
45 |
#else |
|
46 |
#define myTEXT(str) str |
|
47 |
#endif |
|
48 |
||
49 |
#define MAX_NB_CAN_PORTS 16 |
|
50 |
||
51 |
typedef UNS8 (CALLBACK* CANRECEIVE_DRIVER_PROC)(void* inst, Message *m); |
|
52 |
typedef UNS8 (CALLBACK* CANSEND_DRIVER_PROC)(void* inst, const Message *m); |
|
53 |
typedef void* (CALLBACK* CANOPEN_DRIVER_PROC)(s_BOARD *board); |
|
54 |
typedef int (CALLBACK* CANCLOSE_DRIVER_PROC)(void* inst); |
|
55 |
typedef UNS8 (CALLBACK* CANCHANGEBAUDRATE_DRIVER_PROC)(void* fd, char* baud); |
|
56 |
||
57 |
CANRECEIVE_DRIVER_PROC m_canReceive; |
|
58 |
CANSEND_DRIVER_PROC m_canSend; |
|
59 |
CANOPEN_DRIVER_PROC m_canOpen; |
|
60 |
CANCLOSE_DRIVER_PROC m_canClose; |
|
61 |
CANCHANGEBAUDRATE_DRIVER_PROC m_canChangeBaudRate; |
|
62 |
||
63 |
/* CAN port structure */ |
|
64 |
typedef struct |
|
65 |
{ |
|
66 |
char used; /**< flag indicating CAN port usage, will be used to abort Receiver task*/ |
|
67 |
CAN_HANDLE fd; /**< CAN port file descriptor*/ |
|
68 |
TASK_HANDLE receiveTask; /**< CAN Receiver task*/ |
|
69 |
CO_Data* d; /**< CAN object data*/ |
|
70 |
}CANPort; |
|
71 |
||
72 |
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}}; |
|
73 |
||
74 |
||
75 |
/***************************************************************************/ |
|
76 |
UNS8 UnLoadCanDriver(LIB_HANDLE handle) |
|
77 |
{ |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
78 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 79 |
if(handle != NULL) |
80 |
{ |
|
81 |
FreeLibrary(handle); |
|
82 |
handle=NULL; |
|
83 |
return 0; |
|
84 |
} |
|
85 |
return -1; |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
86 |
#else |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
87 |
handle = NULL; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
88 |
return 0; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
89 |
#endif //NOT_USE_DYNAMIC_LOADING |
556 | 90 |
} |
91 |
||
92 |
/***************************************************************************/ |
|
93 |
/** |
|
94 |
* Loads the dll and get funcs ptr |
|
95 |
* |
|
96 |
* @param driver_name String containing driver's dynamic library name |
|
97 |
* @return Library handle |
|
98 |
*/ |
|
99 |
LIB_HANDLE LoadCanDriver(LPCTSTR driver_name) |
|
100 |
{ |
|
101 |
// driver module handle |
|
102 |
LIB_HANDLE handle = NULL; |
|
103 |
||
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
104 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 105 |
if(handle == NULL) |
106 |
{ |
|
107 |
handle = LoadLibrary(driver_name); |
|
108 |
} |
|
577 | 109 |
|
110 |
if (!handle) |
|
556 | 111 |
{ |
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
112 |
fprintf (stderr, "%d\n", GetLastError()); |
556 | 113 |
return NULL; |
114 |
} |
|
115 |
||
116 |
m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver")); |
|
117 |
m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver")); |
|
118 |
m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver")); |
|
119 |
m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver")); |
|
120 |
m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver")); |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
121 |
#else |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
122 |
//compiled in... |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
123 |
handle = 1; //TODO: remove this hack |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
124 |
|
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
125 |
m_canReceive = canReceive_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
126 |
m_canSend = canSend_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
127 |
m_canOpen = canOpen_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
128 |
m_canClose = canClose_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
129 |
m_canChangeBaudRate = canChangeBaudRate_driver; |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
130 |
#endif |
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
131 |
|
577 | 132 |
|
556 | 133 |
return handle; |
134 |
} |
|
135 |
||
136 |
/***************************************************************************/ |
|
137 |
UNS8 canSend(CAN_PORT port, Message *m) |
|
138 |
{ |
|
139 |
if (port && (m_canSend != NULL)) |
|
140 |
{ |
|
691
17d9c0736935
[ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents:
687
diff
changeset
|
141 |
return m_canSend(((CANPort*)port)->fd, m); |
17d9c0736935
[ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents:
687
diff
changeset
|
142 |
} |
17d9c0736935
[ canfestival-Patches-3481330 ] Useless structure SHORT_CAN
Edouard Tisserant
parents:
687
diff
changeset
|
143 |
return 1; |
556 | 144 |
} |
145 |
||
146 |
/***************************************************************************/ |
|
147 |
void canReceiveLoop(CAN_PORT port) |
|
148 |
{ |
|
149 |
Message m; |
|
150 |
while(((CANPort*)port)->used) |
|
151 |
{ |
|
152 |
if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break; |
|
153 |
EnterMutex(); |
|
154 |
canDispatch(((CANPort*)port)->d, &m); |
|
155 |
LeaveMutex(); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
/***************************************************************************/ |
|
160 |
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d) |
|
161 |
{ |
|
162 |
int i; |
|
591 | 163 |
CAN_HANDLE fd0; |
164 |
||
556 | 165 |
for(i=0; i < MAX_NB_CAN_PORTS; i++) |
166 |
{ |
|
167 |
if(!canports[i].used) |
|
168 |
break; |
|
169 |
} |
|
170 |
||
577 | 171 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 172 |
if (m_canOpen == NULL) |
173 |
{ |
|
174 |
fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
|
175 |
return NULL; |
|
176 |
} |
|
177 |
#endif |
|
577 | 178 |
|
591 | 179 |
fd0 = m_canOpen(board); |
556 | 180 |
if(fd0) |
181 |
{ |
|
182 |
canports[i].used = 1; |
|
183 |
canports[i].fd = fd0; |
|
184 |
canports[i].d = d; |
|
185 |
d->canHandle = (CAN_PORT)&canports[i]; |
|
577 | 186 |
CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop); |
556 | 187 |
return (CAN_PORT)&canports[i]; |
188 |
} |
|
189 |
else |
|
190 |
{ |
|
687 | 191 |
MSG("CanOpen : Cannot open board {busname='%S',baudrate='%S'}\n",board->busname, board->baudrate); |
556 | 192 |
return NULL; |
193 |
} |
|
194 |
} |
|
195 |
||
196 |
/***************************************************************************/ |
|
197 |
int canClose(CO_Data * d) |
|
198 |
{ |
|
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
199 |
UNS8 res = 1; |
591 | 200 |
CANPort* tmp; |
201 |
||
202 |
if((CANPort*)d->canHandle) |
|
203 |
{ |
|
204 |
((CANPort*)d->canHandle)->used = 0; |
|
205 |
} |
|
206 |
||
207 |
tmp = (CANPort*)d->canHandle; |
|
577 | 208 |
|
591 | 209 |
if(tmp) |
210 |
{ |
|
629 | 211 |
// kill receiver task before port is closed and handle set to NULL |
212 |
WaitReceiveTaskEnd(&tmp->receiveTask); |
|
213 |
||
591 | 214 |
// close CAN port |
215 |
res = m_canClose(tmp->fd); |
|
629 | 216 |
} |
217 |
||
218 |
d->canHandle = NULL; |
|
219 |
||
556 | 220 |
return res; |
221 |
} |
|
222 |
||
223 |
UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
|
224 |
{ |
|
225 |
if(port){ |
|
226 |
UNS8 res; |
|
227 |
//LeaveMutex(); |
|
228 |
res = m_canChangeBaudRate(((CANPort*)port)->fd, baud); |
|
229 |
//EnterMutex(); |
|
230 |
return res; // OK |
|
577 | 231 |
} |
556 | 232 |
return 1; // NOT OK |
233 |
} |
|
234 |