author | Christian Taedcke |
Wed, 10 Nov 2010 14:19:31 +0100 | |
changeset 650 | 58b07a7e695b |
parent 638 | 00bc03cb5606 |
child 652 | 3b5789c34499 |
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 |
UNS8 res; |
|
140 |
if (port && (m_canSend != NULL)) |
|
141 |
{ |
|
142 |
res = m_canSend(((CANPort*)port)->fd, m); |
|
143 |
if (res) return 1; // OK |
|
144 |
} |
|
145 |
return 0; // NOT OK |
|
146 |
} |
|
147 |
||
148 |
/***************************************************************************/ |
|
149 |
void canReceiveLoop(CAN_PORT port) |
|
150 |
{ |
|
151 |
Message m; |
|
152 |
while(((CANPort*)port)->used) |
|
153 |
{ |
|
154 |
if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break; |
|
155 |
EnterMutex(); |
|
156 |
canDispatch(((CANPort*)port)->d, &m); |
|
157 |
LeaveMutex(); |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/***************************************************************************/ |
|
162 |
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d) |
|
163 |
{ |
|
164 |
int i; |
|
591 | 165 |
CAN_HANDLE fd0; |
166 |
||
556 | 167 |
for(i=0; i < MAX_NB_CAN_PORTS; i++) |
168 |
{ |
|
169 |
if(!canports[i].used) |
|
170 |
break; |
|
171 |
} |
|
172 |
||
577 | 173 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 174 |
if (m_canOpen == NULL) |
175 |
{ |
|
176 |
fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
|
177 |
return NULL; |
|
178 |
} |
|
179 |
#endif |
|
577 | 180 |
|
591 | 181 |
fd0 = m_canOpen(board); |
556 | 182 |
if(fd0) |
183 |
{ |
|
184 |
canports[i].used = 1; |
|
185 |
canports[i].fd = fd0; |
|
186 |
canports[i].d = d; |
|
187 |
d->canHandle = (CAN_PORT)&canports[i]; |
|
577 | 188 |
CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop); |
556 | 189 |
return (CAN_PORT)&canports[i]; |
190 |
} |
|
191 |
else |
|
192 |
{ |
|
638
00bc03cb5606
CHANGED: - option to integrate a can driver statically (hack)
Christian Taedcke
parents:
629
diff
changeset
|
193 |
MSG(("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate)); |
556 | 194 |
return NULL; |
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/***************************************************************************/ |
|
199 |
int canClose(CO_Data * d) |
|
200 |
{ |
|
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
201 |
UNS8 res = 1; |
591 | 202 |
CANPort* tmp; |
203 |
||
204 |
if((CANPort*)d->canHandle) |
|
205 |
{ |
|
206 |
((CANPort*)d->canHandle)->used = 0; |
|
207 |
} |
|
208 |
||
209 |
tmp = (CANPort*)d->canHandle; |
|
577 | 210 |
|
591 | 211 |
if(tmp) |
212 |
{ |
|
629 | 213 |
// kill receiver task before port is closed and handle set to NULL |
214 |
WaitReceiveTaskEnd(&tmp->receiveTask); |
|
215 |
||
591 | 216 |
// close CAN port |
217 |
res = m_canClose(tmp->fd); |
|
629 | 218 |
} |
219 |
||
220 |
d->canHandle = NULL; |
|
221 |
||
556 | 222 |
return res; |
223 |
} |
|
224 |
||
225 |
UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
|
226 |
{ |
|
227 |
if(port){ |
|
228 |
UNS8 res; |
|
229 |
//LeaveMutex(); |
|
230 |
res = m_canChangeBaudRate(((CANPort*)port)->fd, baud); |
|
231 |
//EnterMutex(); |
|
232 |
return res; // OK |
|
577 | 233 |
} |
556 | 234 |
return 1; // NOT OK |
235 |
} |
|
236 |