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>
|
577
|
29 |
extern "C"
|
|
30 |
{
|
556
|
31 |
#include "canfestival.h"
|
|
32 |
#include "timer.h"
|
|
33 |
#include "timers_driver.h"
|
577
|
34 |
};
|
556
|
35 |
// GetProcAddress doesn't have an UNICODE version for NT
|
|
36 |
#ifdef UNDER_CE
|
|
37 |
#define myTEXT(str) TEXT(str)
|
|
38 |
#else
|
|
39 |
#define myTEXT(str) str
|
|
40 |
#endif
|
|
41 |
|
|
42 |
#define MAX_NB_CAN_PORTS 16
|
|
43 |
|
|
44 |
typedef UNS8 (CALLBACK* CANRECEIVE_DRIVER_PROC)(void* inst, Message *m);
|
|
45 |
typedef UNS8 (CALLBACK* CANSEND_DRIVER_PROC)(void* inst, const Message *m);
|
|
46 |
typedef void* (CALLBACK* CANOPEN_DRIVER_PROC)(s_BOARD *board);
|
|
47 |
typedef int (CALLBACK* CANCLOSE_DRIVER_PROC)(void* inst);
|
|
48 |
typedef UNS8 (CALLBACK* CANCHANGEBAUDRATE_DRIVER_PROC)(void* fd, char* baud);
|
|
49 |
|
|
50 |
CANRECEIVE_DRIVER_PROC m_canReceive;
|
|
51 |
CANSEND_DRIVER_PROC m_canSend;
|
|
52 |
CANOPEN_DRIVER_PROC m_canOpen;
|
|
53 |
CANCLOSE_DRIVER_PROC m_canClose;
|
|
54 |
CANCHANGEBAUDRATE_DRIVER_PROC m_canChangeBaudRate;
|
|
55 |
|
|
56 |
/* CAN port structure */
|
|
57 |
typedef struct
|
|
58 |
{
|
|
59 |
char used; /**< flag indicating CAN port usage, will be used to abort Receiver task*/
|
|
60 |
CAN_HANDLE fd; /**< CAN port file descriptor*/
|
|
61 |
TASK_HANDLE receiveTask; /**< CAN Receiver task*/
|
|
62 |
CO_Data* d; /**< CAN object data*/
|
|
63 |
}CANPort;
|
|
64 |
|
|
65 |
CANPort canports[MAX_NB_CAN_PORTS] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,}};
|
|
66 |
|
|
67 |
|
|
68 |
/***************************************************************************/
|
|
69 |
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
|
|
70 |
{
|
|
71 |
if(handle != NULL)
|
|
72 |
{
|
|
73 |
FreeLibrary(handle);
|
|
74 |
handle=NULL;
|
|
75 |
return 0;
|
|
76 |
}
|
|
77 |
return -1;
|
|
78 |
}
|
|
79 |
|
|
80 |
/***************************************************************************/
|
|
81 |
/**
|
|
82 |
* Loads the dll and get funcs ptr
|
|
83 |
*
|
|
84 |
* @param driver_name String containing driver's dynamic library name
|
|
85 |
* @return Library handle
|
|
86 |
*/
|
|
87 |
LIB_HANDLE LoadCanDriver(LPCTSTR driver_name)
|
|
88 |
{
|
|
89 |
// driver module handle
|
|
90 |
LIB_HANDLE handle = NULL;
|
|
91 |
|
|
92 |
if(handle == NULL)
|
|
93 |
{
|
|
94 |
handle = LoadLibrary(driver_name);
|
|
95 |
}
|
577
|
96 |
|
|
97 |
if (!handle)
|
556
|
98 |
{
|
|
99 |
fprintf (stderr, "%s\n", GetLastError());
|
|
100 |
return NULL;
|
|
101 |
}
|
|
102 |
|
|
103 |
m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver"));
|
|
104 |
m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver"));
|
|
105 |
m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver"));
|
|
106 |
m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver"));
|
|
107 |
m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver"));
|
577
|
108 |
|
556
|
109 |
return handle;
|
|
110 |
}
|
|
111 |
|
|
112 |
/***************************************************************************/
|
|
113 |
UNS8 canSend(CAN_PORT port, Message *m)
|
|
114 |
{
|
|
115 |
UNS8 res;
|
|
116 |
if (port && (m_canSend != NULL))
|
|
117 |
{
|
|
118 |
res = m_canSend(((CANPort*)port)->fd, m);
|
|
119 |
if (res) return 1; // OK
|
|
120 |
}
|
|
121 |
return 0; // NOT OK
|
|
122 |
}
|
|
123 |
|
|
124 |
/***************************************************************************/
|
|
125 |
void canReceiveLoop(CAN_PORT port)
|
|
126 |
{
|
|
127 |
Message m;
|
|
128 |
while(((CANPort*)port)->used)
|
|
129 |
{
|
|
130 |
if(m_canReceive(((CANPort*)port)->fd, &m) != 0) break;
|
|
131 |
EnterMutex();
|
|
132 |
canDispatch(((CANPort*)port)->d, &m);
|
|
133 |
LeaveMutex();
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
/***************************************************************************/
|
|
138 |
CAN_HANDLE canOpen(s_BOARD *board, CO_Data * d)
|
|
139 |
{
|
|
140 |
int i;
|
|
141 |
for(i=0; i < MAX_NB_CAN_PORTS; i++)
|
|
142 |
{
|
|
143 |
if(!canports[i].used)
|
|
144 |
break;
|
|
145 |
}
|
|
146 |
|
577
|
147 |
#ifndef NOT_USE_DYNAMIC_LOADING
|
556
|
148 |
if (m_canOpen == NULL)
|
|
149 |
{
|
|
150 |
fprintf(stderr,"CanOpen : Can Driver dll not loaded\n");
|
|
151 |
return NULL;
|
|
152 |
}
|
|
153 |
#endif
|
577
|
154 |
|
556
|
155 |
CAN_HANDLE fd0 = m_canOpen(board);
|
|
156 |
if(fd0)
|
|
157 |
{
|
|
158 |
canports[i].used = 1;
|
|
159 |
canports[i].fd = fd0;
|
|
160 |
canports[i].d = d;
|
|
161 |
d->canHandle = (CAN_PORT)&canports[i];
|
577
|
162 |
CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop);
|
556
|
163 |
return (CAN_PORT)&canports[i];
|
|
164 |
}
|
|
165 |
else
|
|
166 |
{
|
|
167 |
MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate);
|
|
168 |
return NULL;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
/***************************************************************************/
|
|
173 |
int canClose(CO_Data * d)
|
|
174 |
{
|
|
175 |
UNS8 res;
|
577
|
176 |
|
556
|
177 |
((CANPort*)d->canHandle)->used = 0;
|
|
178 |
CANPort* tmp = (CANPort*)d->canHandle;
|
|
179 |
d->canHandle = NULL;
|
577
|
180 |
|
556
|
181 |
// close CAN port
|
|
182 |
res = m_canClose(tmp->fd);
|
|
183 |
|
|
184 |
// kill receiver task
|
|
185 |
WaitReceiveTaskEnd(&tmp->receiveTask);
|
|
186 |
return res;
|
|
187 |
}
|
|
188 |
|
|
189 |
UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
|
|
190 |
{
|
|
191 |
if(port){
|
|
192 |
UNS8 res;
|
|
193 |
//LeaveMutex();
|
|
194 |
res = m_canChangeBaudRate(((CANPort*)port)->fd, baud);
|
|
195 |
//EnterMutex();
|
|
196 |
return res; // OK
|
577
|
197 |
}
|
556
|
198 |
return 1; // NOT OK
|
|
199 |
}
|
|
200 |
|