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