source: pacpussensors/trunk/CanGateway/driver/VectorCanDriver.cpp@ 134

Last change on this file since 134 was 2, checked in by DHERBOMEZ Gérald, 11 years ago

correction of minor bugs (include and link). Build on Windows OK.

File size: 13.1 KB
RevLine 
[1]1/********************************************************************
2// created: 2006/08/07 - 14:14
3// filename: VectorCanDriver.cpp
4//
5// author: Gerald Dherbomez
6//
7// version: $Id: VectorCanDriver.cpp 1203 2012-08-02 11:58:15Z morasjul $
8//
9// purpose: Implementation of the VectorCanDriver class
10// This file is based on the work 2 UTC students (Maricot
11// Benoit & Pham Eric) in the framework of their SY27
12// project.
13//
14*********************************************************************/
15
16
17#include "VectorCanDriver.h"
18#include <stdio.h>
19#include <string>
[2]20#include "../CanFrame.h"
[1]21
22#define WAIT_RECEIVING_FRAME_TIMEOUT 1000
23
24
25
26/**
27* Constructor which enables to initialize the different attributes of the class with default values.
28* @see ~VectorCanDriver (void)
29* @see VectorCanDriver (unsigned int gHwTypeT, Vaccess gChannelMaskT, unsigned int gCanIdT, unsigned int gBitRateT,int gHwChannelT)
30*/
31VectorCanDriver::VectorCanDriver (int channel, unsigned int bitRate)
32{
33 printf("Notice : VECTOR CAN Driver used\n");
34 gPortHandle = INVALID_PORTHANDLE;
35 gChannelMask = 0x3;
36 gPermissionMask = 0;
37 gCanId = 0x2A9;
38 gBitRate = bitRate;
39 gHwType = HWTYPE_CANAC2PCI;
40 gHwChannel = channel;
41 gEventHandle = 0;
42}
43
44
45/**
46* Constructor which enables to initialize the different attributes of the class with default values.
47* @see ~VectorCanDriver (void)
48* @see VectorCanDriver (unsigned int gHwTypeT, Vaccess gChannelMaskT, unsigned int gCanIdT, unsigned int gBitRateT,int gHwChannelT)
49*/
50VectorCanDriver::VectorCanDriver (void)
51{
52 printf("Notice : VECTOR CAN Driver used\n");
53 gPortHandle = INVALID_PORTHANDLE;
54 gChannelMask = 0x3;
55 gPermissionMask = 0;
56 gCanId = 0x2A9;
57 gBitRate = 500000;
58 gHwType = HWTYPE_CANAC2PCI;
59 gHwChannel = 0;
60 gEventHandle = 0;
61}
62
63
64/**
65* Constructor which enables to initialize the different attributes of the class with values given in parameters.
66* @see ~VectorCanDriver (void)
67* @see VectorCanDriver (void)
68*/
69VectorCanDriver::VectorCanDriver (unsigned int gHwTypeT, Vaccess gChannelMaskT, unsigned int gCanIdT, unsigned int gBitRateT,int gHwChannelT)
70{
71 printf("Notice : VECTOR CAN Driver used\n");
72 gPortHandle = INVALID_PORTHANDLE;
73 gChannelMask = gChannelMaskT;
74 gPermissionMask = 0;
75 gCanId = gCanIdT;
76 gBitRate = gBitRateT;
77 gHwType = gHwTypeT;
78 gHwChannel = gHwChannelT;
79 gEventHandle = 0;
80}
81
82
83/**
84* Constructor which enables to select the channel and to initialize the different attributes of the class with default values..
85* @see ~VectorCanDriver (void)
86* @see VectorCanDriver (void)
87*/
88VectorCanDriver::VectorCanDriver(int channel)
89{
90 printf("Notice : VECTOR CAN Driver used\n");
91 gPortHandle = INVALID_PORTHANDLE;
92 gChannelMask = 0x3;
93 gPermissionMask = 0;
94 gCanId = 0x2A9;
95 gBitRate = 1000000;
96 gHwType = HWTYPE_CANAC2PCI;
97 gHwChannel = channel;
98 gEventHandle = 0;
99}
100
101
102
103/**
104* Destructor which clean up the different attributs of the class.
105* @see VectorCanDriver (void)
106* @see VectorCanDriver (unsigned int gHwTypeT, Vaccess gChannelMaskT, unsigned int gCanIdT, unsigned int gBitRateT,int gHwChannelT)
107*/
108VectorCanDriver::~VectorCanDriver (void)
109{
110 gEventHandle = NULL;
111 gPortHandle = INVALID_PORTHANDLE;
112}
113
114
115/**
116* Member used to initialise the configuration of the CAN Card.
117* @see cleanUpPort (void)
118* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
119*/
120Vstatus VectorCanDriver::initPort (void)
121{
122 Vstatus vErr;
123 VsetAcceptance acc;
124
125 /* Open the driver */
126 vErr = ncdOpenDriver ();
127 if (vErr)
128 {
129 printf ("ERROR:initport %s!\n", ncdGetErrorString (vErr));
130 return vErr;
131 }
132
133 /* Select the channel */
134 gChannelMask = ncdGetChannelMask (gHwType,0,gHwChannel);
135 if (!gChannelMask)
136 {
137 printf ("ERROR: (initport) Channel not present!\n");
138 printf ("ERROR:(initport) %s!\n", ncdGetErrorString (VERROR));
139 return vErr;
140 }
141
142 /* Select the index */
143 int gChannelIndex = ncdGetChannelIndex (gHwType,0,gHwChannel);
144 if ( gChannelIndex == -1 )
145 {
146 printf ("ERROR: (initport) Channel not present!\n");
147 printf ("ERROR:(initport) %s!\n", ncdGetErrorString (VERROR));
148 return vErr;
149 }
150
151 /* Open a port */
152 vErr = ncdOpenPort (&gPortHandle,"PACPUS_CANDriver",gChannelMask,gChannelMask,&gPermissionMask,1024);
153 if (vErr != VSUCCESS)
154 {
155 printf ("ERROR:(init port) %s!\n", ncdGetErrorString (vErr));
156 return vErr;
157 }
158
159 /* Only if permission to initialize the channel */
160 if (gPermissionMask)
161 {
162 /* Set the bus timing */
163 vErr = ncdSetChannelBitrate (gPortHandle,gPermissionMask,gBitRate);
164 if (vErr)
165 {
166 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
167 if (gPortHandle!=INVALID_PORTHANDLE)
168 {
169 ncdClosePort (gPortHandle);
170 gPortHandle = INVALID_PORTHANDLE;
171 }
172 return vErr;
173 }
174 }
175 else if (gBitRate)
176 {
177 printf ("WARNING: No init access! Bitrate ignored.\n");
178 }
179
180 /* Enable the TX and disable the TXRQ notifications */
181 vErr = ncdSetChannelMode (gPortHandle,gChannelMask,1,0);
182 if (vErr)
183 {
184 printf ("ERROR:(init port) %s!\n", ncdGetErrorString (vErr));
185 if (gPortHandle!=INVALID_PORTHANDLE)
186 {
187 ncdClosePort (gPortHandle);
188 gPortHandle = INVALID_PORTHANDLE;
189 }
190 return vErr;
191 }
192
193 /* create a synchronisation object */
194 gEventHandle = CreateEvent (NULL, FALSE, FALSE, NULL);
195 vErr = ncdSetNotification (gPortHandle, (unsigned long*)&gEventHandle, 1);
196 if (vErr)
197 {
198 printf ("ERROR(init port): %s!\n", ncdGetErrorString (vErr));
199 ncdDeactivateChannel (gPortHandle, gChannelMask);
200 cleanUpPort ();
201 return vErr;
202 }
203
204 /* Open the acceptance filter */
205 acc.mask = 0x000; // relevant=1
206 acc.code = 0x000;
207
208 vErr = ncdSetChannelAcceptance (gPortHandle,gChannelMask,&acc);
209 if (vErr)
210 {
211 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
212 if (gPortHandle!=INVALID_PORTHANDLE)
213 {
214 ncdClosePort (gPortHandle);
215 gPortHandle = INVALID_PORTHANDLE;
216 }
217 return vErr;
218 }
219
220 /* Go on bus */
221 vErr = ncdActivateChannel (gPortHandle,gChannelMask);
222 if (vErr)
223 {
224 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
225 ncdDeactivateChannel (gPortHandle, gChannelMask);
226 cleanUpPort ();
227 return vErr;
228 }
229
230 // Reset the clocks of all channels connected to the port
231 vErr = ncdResetClock(gPortHandle);
232 if (vErr)
233 {
234 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
235 ncdDeactivateChannel (gPortHandle, gChannelMask);
236 cleanUpPort ();
237 return vErr;
238 }
239
240 return VSUCCESS;
241}
242
243
244/**
245* Member used to clean up the configuration of the CAN Card.
246* @see initPort (void)
247* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
248*/
249Vstatus VectorCanDriver::cleanUpPort (void)
250{
251 Vstatus vErr;
252 vErr = ncdDeactivateChannel(gPortHandle, gChannelMask);
253 if (vErr)
254 {
255 printf ("ERROR: (cleanUpPort) Channel not correctly deactivated!\n Driver not correctly closed!\n");
256 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
257 return vErr;
258 }
259 vErr = ncdClosePort (gPortHandle);
260 if (vErr)
261 {
262 printf ("ERROR: (cleanUpPort) Port not correctly closed!\n Driver not correctly closed!\n");
263 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
264 return vErr;
265 }
266 gPortHandle = INVALID_PORTHANDLE;
267 vErr = ncdCloseDriver ();
268 if (vErr)
269 {
270 printf ("ERROR: (cleanUpPort) Driver not correctly closed!\n");
271 printf ("ERROR: %s!\n", ncdGetErrorString (vErr));
272 return vErr;
273 }
274
275 return VSUCCESS;
276}
277
278
279/**
280* Member which permit to send a frame on the CAN bus and test if the frame is well acknowledged.
281* @param flags a character which contain the flags of the sent frame.
282* @param dlc a character which defined the number of characters of the sent frame.
283* @param data a table of characters with the data of the sent frame.
284* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
285* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
286*/
287//Vstatus VectorCanDriver::sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data, int identifiant)
288Vstatus VectorCanDriver::sendFrame (struct CanFrame frame)
289{
290 Vstatus vErr;
291 Vevent event;
292 //Vevent * pEvent = NULL;
293
294 /* Create frame to send */
295 event.tag = V_TRANSMIT_MSG;
296 event.tagData.msg.id = frame.id;
297 event.tagData.msg.flags = 0;
298 event.tagData.msg.dlc = frame.dlc;
299
300 for (int i = 0; i < frame.dlc; i++)
301 event.tagData.msg.data[i] = frame.data[i];
302
303 /* Send the frame */
304 vErr = ncdTransmit (gPortHandle, gChannelMask, &event);
305
306 //printf("sendFrame ncdTransmit -- vErr=%d, event=0x%x\n", vErr, event);
307
308 if (vErr==VERR_QUEUE_IS_FULL)
309 {
310 printf("!");
311 }
312
313 else if (vErr)
314 {
315 printf ("ERROR:(send frame) %s!\n", ncdGetErrorString (vErr));
316 // ncdDeactivateChannel (gPortHandle, gChannelMask);
317 // cleanUpPort ();
318 return vErr;
319 }
320
321 return VSUCCESS;
322
323 ///* Wait the acknowledgement frame */
324 //waitReceivingFrame();
325
326 ///* Read the acknowledgement frame in the buffer */
327 //vErr = ncdReceive1 (gPortHandle,&pEvent);
328 ////printf("sendFrame ncdReceive1 -- vErr=%d, pEvent=0x%x\n", vErr, pEvent);
329
330 //if ( (pEvent==NULL) && (vErr==VERR_QUEUE_IS_EMPTY) )
331 //{
332 // printf ("%s\n",ncdGetErrorString (vErr));
333 // printf ("%s!\n",ncdGetErrorString (vErr));
334 // // ncdDeactivateChannel (gPortHandle, gChannelMask);
335 // // cleanUpPort ();
336 // return vErr;
337 //}
338
339 ///* If frame is well acknoledged */
340 //if (pEvent->tagData.msg.flags & 0x40)
341 // return VSUCCESS;
342 //else
343 //{/*
344 // printf("%x ",pEvent->tagData.msg.flags);
345 // printf (" no acknolegement of frame!\n");
346 // */
347 // return VERROR;
348 //}
349}
350
351
352/**
353* Member which permit to receive of a frame on the CAN bus.
354* @param flags a character pointer which contain the flags of the received frame.
355* @param dlc a character pointer which defined the number of characters of the received frame.
356* @param data a pointer of table of characters with the data of the received frame.
357* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
358* @return a Vstatus variable which contain the error code of the function. On success, it return VSUCCESS. On failure, it return Vstatus error code which is defined in VCanD.h
359*/
360//Vstatus VectorCanDriver::receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char * data, int * identifiant)
361Vstatus VectorCanDriver::receiveFrame (struct CanFrame &frame)
362{
363 int L ;
364 Vstatus vErr;
365 Vevent * pEvent = NULL;
366
367 do {
368 /* Read frame in the buffer */
369 ncdGetReceiveQueueLevel( gPortHandle , &L );
370 if( L <= 0 )
371 {
372 if (WaitForSingleObject (gEventHandle,WAIT_RECEIVING_FRAME_TIMEOUT) == WAIT_TIMEOUT)
373 {
374 //printf("no frame receiving in the %d last seconds\n", WAIT_RECEIVING_FRAME_TIMEOUT/1000);
375 return VERR_QUEUE_IS_EMPTY;
376 }
377 }
378
379 vErr = ncdReceive1 (gPortHandle,&pEvent);
380 if( vErr == VERROR )
381 {
382 printf ("receiveframe ERROR - ERROR: %s\n", ncdGetErrorString (vErr));
383 // ncdDeactivateChannel (gPortHandle, gChannelMask);
384 // cleanUpPort ();
385
386 return vErr;
387 }
388
389 } while((( pEvent==NULL ) && ( vErr==VERR_QUEUE_IS_EMPTY )) || ( vErr == VERROR ));
390
391 /* Return the flags field, dlc field and data field of the sent frame */
392 //frame.flags = pEvent->tagData.msg.flags;
393 frame.dlc = pEvent->tagData.msg.dlc;
394 frame.id = pEvent->tagData.msg.id;
395 if( ( frame.dlc > 8 ) || ( frame.dlc < 0 ))
396 frame.dlc = 8;
397
398 memcpy(frame.data,pEvent->tagData.msg.data, frame.dlc);
399
400 return VSUCCESS;
401}
402
403
404/**
405* Member which wait the reception of a frame on the CAN bus.
406* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
407* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
408*/
409void VectorCanDriver::waitReceivingFrame(void)
410{
411 if (WaitForSingleObject (gEventHandle,WAIT_RECEIVING_FRAME_TIMEOUT) == WAIT_TIMEOUT)
412 {
413 printf("waitReceivingFrame - Error : no frame receiving in the %d last seconds\n", WAIT_RECEIVING_FRAME_TIMEOUT/1000);
414 }
415}
416
417
418Vstatus VectorCanDriver::AcceptId(int id)
419{
420 if(ncdAddAcceptanceRange(gPortHandle,gChannelMask,id,id)!=VSUCCESS)
421 {
422 printf("erreur lors de la configuration du filtre");
423 return VERROR;
424 }
425 else
426 return VSUCCESS;
427}
428
429
430Vstatus VectorCanDriver::AcceptIdRange(int deb,int fin)
431{
432 if(ncdAddAcceptanceRange(gPortHandle,gChannelMask,deb,fin)!=VSUCCESS)
433 {
434 printf("erreur lors de la configuration du filtre");
435 return VERROR;
436 }
437 else
438 return VSUCCESS;
439}
Note: See TracBrowser for help on using the repository browser.