author | edouard |
Sun, 28 Nov 2010 00:05:16 +0100 | |
changeset 634 | 15e8d1ec0e61 |
parent 629 | b9274b595650 |
child 638 | 00bc03cb5606 |
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 |
{ |
|
78 |
if(handle != NULL) |
|
79 |
{ |
|
80 |
FreeLibrary(handle); |
|
81 |
handle=NULL; |
|
82 |
return 0; |
|
83 |
} |
|
84 |
return -1; |
|
85 |
} |
|
86 |
||
87 |
/***************************************************************************/ |
|
88 |
/** |
|
89 |
* Loads the dll and get funcs ptr |
|
90 |
* |
|
91 |
* @param driver_name String containing driver's dynamic library name |
|
92 |
* @return Library handle |
|
93 |
*/ |
|
94 |
LIB_HANDLE LoadCanDriver(LPCTSTR driver_name) |
|
95 |
{ |
|
96 |
// driver module handle |
|
97 |
LIB_HANDLE handle = NULL; |
|
98 |
||
99 |
if(handle == NULL) |
|
100 |
{ |
|
101 |
handle = LoadLibrary(driver_name); |
|
102 |
} |
|
577 | 103 |
|
104 |
if (!handle) |
|
556 | 105 |
{ |
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
106 |
fprintf (stderr, "%d\n", GetLastError()); |
556 | 107 |
return NULL; |
108 |
} |
|
109 |
||
110 |
m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver")); |
|
111 |
m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver")); |
|
112 |
m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver")); |
|
113 |
m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver")); |
|
114 |
m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver")); |
|
577 | 115 |
|
556 | 116 |
return handle; |
117 |
} |
|
118 |
||
119 |
/***************************************************************************/ |
|
120 |
UNS8 canSend(CAN_PORT port, Message *m) |
|
121 |
{ |
|
122 |
UNS8 res; |
|
123 |
if (port && (m_canSend != NULL)) |
|
124 |
{ |
|
125 |
res = m_canSend(((CANPort*)port)->fd, m); |
|
126 |
if (res) return 1; // OK |
|
127 |
} |
|
128 |
return 0; // NOT OK |
|
129 |
} |
|
130 |
||
131 |
/***************************************************************************/ |
|
132 |
void canReceiveLoop(CAN_PORT port) |
|
133 |
{ |
|
134 |
Message m; |
|
135 |
while(((CANPort*)port)->used) |
|
136 |
{ |
|
137 |
if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break; |
|
138 |
EnterMutex(); |
|
139 |
canDispatch(((CANPort*)port)->d, &m); |
|
140 |
LeaveMutex(); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/***************************************************************************/ |
|
145 |
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d) |
|
146 |
{ |
|
147 |
int i; |
|
591 | 148 |
CAN_HANDLE fd0; |
149 |
||
556 | 150 |
for(i=0; i < MAX_NB_CAN_PORTS; i++) |
151 |
{ |
|
152 |
if(!canports[i].used) |
|
153 |
break; |
|
154 |
} |
|
155 |
||
577 | 156 |
#ifndef NOT_USE_DYNAMIC_LOADING |
556 | 157 |
if (m_canOpen == NULL) |
158 |
{ |
|
159 |
fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
|
160 |
return NULL; |
|
161 |
} |
|
162 |
#endif |
|
577 | 163 |
|
591 | 164 |
fd0 = m_canOpen(board); |
556 | 165 |
if(fd0) |
166 |
{ |
|
167 |
canports[i].used = 1; |
|
168 |
canports[i].fd = fd0; |
|
169 |
canports[i].d = d; |
|
170 |
d->canHandle = (CAN_PORT)&canports[i]; |
|
577 | 171 |
CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop); |
556 | 172 |
return (CAN_PORT)&canports[i]; |
173 |
} |
|
174 |
else |
|
175 |
{ |
|
176 |
MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate); |
|
177 |
return NULL; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
/***************************************************************************/ |
|
182 |
int canClose(CO_Data * d) |
|
183 |
{ |
|
600
7767029937aa
add timeout for waitreceivetaskend for the win32, fix GetLastError print
'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>'
parents:
591
diff
changeset
|
184 |
UNS8 res = 1; |
591 | 185 |
CANPort* tmp; |
186 |
||
187 |
if((CANPort*)d->canHandle) |
|
188 |
{ |
|
189 |
((CANPort*)d->canHandle)->used = 0; |
|
190 |
} |
|
191 |
||
192 |
tmp = (CANPort*)d->canHandle; |
|
577 | 193 |
|
591 | 194 |
if(tmp) |
195 |
{ |
|
629 | 196 |
// kill receiver task before port is closed and handle set to NULL |
197 |
WaitReceiveTaskEnd(&tmp->receiveTask); |
|
198 |
||
591 | 199 |
// close CAN port |
200 |
res = m_canClose(tmp->fd); |
|
629 | 201 |
} |
202 |
||
203 |
d->canHandle = NULL; |
|
204 |
||
556 | 205 |
return res; |
206 |
} |
|
207 |
||
208 |
UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
|
209 |
{ |
|
210 |
if(port){ |
|
211 |
UNS8 res; |
|
212 |
//LeaveMutex(); |
|
213 |
res = m_canChangeBaudRate(((CANPort*)port)->fd, baud); |
|
214 |
//EnterMutex(); |
|
215 |
return res; // OK |
|
577 | 216 |
} |
556 | 217 |
return 1; // NOT OK |
218 |
} |
|
219 |