24 /* |
24 /* |
25 CAN driver interface. |
25 CAN driver interface. |
26 */ |
26 */ |
27 |
27 |
28 #include <windows.h> |
28 #include <windows.h> |
29 |
29 extern "C" |
|
30 { |
30 #include "canfestival.h" |
31 #include "canfestival.h" |
31 #include "timer.h" |
32 #include "timer.h" |
32 #include "timers_driver.h" |
33 #include "timers_driver.h" |
33 |
34 }; |
34 // GetProcAddress doesn't have an UNICODE version for NT |
35 // GetProcAddress doesn't have an UNICODE version for NT |
35 #ifdef UNDER_CE |
36 #ifdef UNDER_CE |
36 #define myTEXT(str) TEXT(str) |
37 #define myTEXT(str) TEXT(str) |
37 #else |
38 #else |
38 #define myTEXT(str) str |
39 #define myTEXT(str) str |
90 |
91 |
91 if(handle == NULL) |
92 if(handle == NULL) |
92 { |
93 { |
93 handle = LoadLibrary(driver_name); |
94 handle = LoadLibrary(driver_name); |
94 } |
95 } |
95 |
96 |
96 if (!handle) |
97 if (!handle) |
97 { |
98 { |
98 fprintf (stderr, "%s\n", GetLastError()); |
99 fprintf (stderr, "%s\n", GetLastError()); |
99 return NULL; |
100 return NULL; |
100 } |
101 } |
101 |
102 |
102 m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver")); |
103 m_canReceive = (CANRECEIVE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canReceive_driver")); |
103 m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver")); |
104 m_canSend = (CANSEND_DRIVER_PROC)GetProcAddress(handle, myTEXT("canSend_driver")); |
104 m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver")); |
105 m_canOpen = (CANOPEN_DRIVER_PROC)GetProcAddress(handle, myTEXT("canOpen_driver")); |
105 m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver")); |
106 m_canClose = (CANCLOSE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canClose_driver")); |
106 m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver")); |
107 m_canChangeBaudRate = (CANCHANGEBAUDRATE_DRIVER_PROC)GetProcAddress(handle, myTEXT("canChangeBaudRate_driver")); |
107 |
108 |
108 return handle; |
109 return handle; |
109 } |
110 } |
110 |
111 |
111 /***************************************************************************/ |
112 /***************************************************************************/ |
112 UNS8 canSend(CAN_PORT port, Message *m) |
113 UNS8 canSend(CAN_PORT port, Message *m) |
141 { |
142 { |
142 if(!canports[i].used) |
143 if(!canports[i].used) |
143 break; |
144 break; |
144 } |
145 } |
145 |
146 |
146 #ifndef NOT_USE_DYNAMIC_LOADING |
147 #ifndef NOT_USE_DYNAMIC_LOADING |
147 if (m_canOpen == NULL) |
148 if (m_canOpen == NULL) |
148 { |
149 { |
149 fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
150 fprintf(stderr,"CanOpen : Can Driver dll not loaded\n"); |
150 return NULL; |
151 return NULL; |
151 } |
152 } |
152 #endif |
153 #endif |
153 |
154 |
154 CAN_HANDLE fd0 = m_canOpen(board); |
155 CAN_HANDLE fd0 = m_canOpen(board); |
155 if(fd0) |
156 if(fd0) |
156 { |
157 { |
157 canports[i].used = 1; |
158 canports[i].used = 1; |
158 canports[i].fd = fd0; |
159 canports[i].fd = fd0; |
159 canports[i].d = d; |
160 canports[i].d = d; |
160 d->canHandle = (CAN_PORT)&canports[i]; |
161 d->canHandle = (CAN_PORT)&canports[i]; |
161 CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, &canReceiveLoop); |
162 CreateReceiveTask(&(canports[i]), &canports[i].receiveTask, (void *)canReceiveLoop); |
162 return (CAN_PORT)&canports[i]; |
163 return (CAN_PORT)&canports[i]; |
163 } |
164 } |
164 else |
165 else |
165 { |
166 { |
166 MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate); |
167 MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate); |
170 |
171 |
171 /***************************************************************************/ |
172 /***************************************************************************/ |
172 int canClose(CO_Data * d) |
173 int canClose(CO_Data * d) |
173 { |
174 { |
174 UNS8 res; |
175 UNS8 res; |
175 |
176 |
176 ((CANPort*)d->canHandle)->used = 0; |
177 ((CANPort*)d->canHandle)->used = 0; |
177 CANPort* tmp = (CANPort*)d->canHandle; |
178 CANPort* tmp = (CANPort*)d->canHandle; |
178 d->canHandle = NULL; |
179 d->canHandle = NULL; |
179 |
180 |
180 // close CAN port |
181 // close CAN port |
181 res = m_canClose(tmp->fd); |
182 res = m_canClose(tmp->fd); |
182 |
183 |
183 // kill receiver task |
184 // kill receiver task |
184 WaitReceiveTaskEnd(&tmp->receiveTask); |
185 WaitReceiveTaskEnd(&tmp->receiveTask); |
185 |
|
186 return res; |
186 return res; |
187 } |
187 } |
188 |
188 |
189 UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
189 UNS8 canChangeBaudRate(CAN_PORT port, char* baud) |
190 { |
190 { |