68 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; |
68 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; |
69 if(!strcmp( optarg, "none")) return 0; |
69 if(!strcmp( optarg, "none")) return 0; |
70 return 0x0000; |
70 return 0x0000; |
71 } |
71 } |
72 |
72 |
73 void canInit (s_BOARD *board) |
73 UNS8 canInit (s_BOARD *board) |
74 { |
74 { |
75 int baudrate; |
75 int baudrate; |
|
76 int ret = 0; |
76 |
77 |
77 #ifdef PCAN2_HEADER_ |
78 #ifdef PCAN2_HEADER_ |
78 // if not the first handler |
79 // if not the first handler |
79 if(second_board == (s_BOARD *)board) { |
80 if(second_board == (s_BOARD *)board) { |
80 if(baudrate = TranslateBaudeRate(board->baudrate)) |
81 if(baudrate = TranslateBaudeRate(board->baudrate)) |
81 CAN2_Init (baudrate, |
82 { |
82 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
83 ret = CAN2_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); |
|
84 if(ret != CAN_ERR_OK) |
|
85 return 0; |
|
86 } |
83 |
87 |
84 //Create the Event for the first board |
88 //Create the Event for the first board |
85 hEvent2 = CreateEvent(NULL, // lpEventAttributes |
89 hEvent2 = CreateEvent(NULL, // lpEventAttributes |
86 FALSE, // bManualReset |
90 FALSE, // bManualReset |
87 FALSE, // bInitialState |
91 FALSE, // bInitialState |
92 } |
96 } |
93 else |
97 else |
94 #endif |
98 #endif |
95 if(first_board == (s_BOARD *)board) { |
99 if(first_board == (s_BOARD *)board) { |
96 if(baudrate = TranslateBaudeRate(board->baudrate)) |
100 if(baudrate = TranslateBaudeRate(board->baudrate)) |
97 CAN_Init (baudrate, |
101 { |
98 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
102 ret = CAN_Init(baudrate, CAN_INIT_TYPE_ST extra_PCAN_init_params); |
99 |
103 if(ret != CAN_ERR_OK) |
100 //Create the Event for the first board |
104 return 0; |
|
105 } |
|
106 //Create the Event for the first board |
101 hEvent1 = CreateEvent(NULL, // lpEventAttributes |
107 hEvent1 = CreateEvent(NULL, // lpEventAttributes |
102 FALSE, // bManualReset |
108 FALSE, // bManualReset |
103 FALSE, // bInitialState |
109 FALSE, // bInitialState |
104 ""); // lpName |
110 ""); // lpName |
105 //Set Event Handle for CANReadExt |
111 //Set Event Handle for CANReadExt |
106 CAN_SetRcvEvent(hEvent1); |
112 CAN_SetRcvEvent(hEvent1); |
107 } |
113 } |
|
114 return 1; |
108 } |
115 } |
109 |
116 |
110 /********* functions which permit to communicate with the board ****************/ |
117 /********* functions which permit to communicate with the board ****************/ |
111 UNS8 canReceive_driver (CAN_HANDLE fd0, Message * m) |
118 UNS8 canReceive_driver (CAN_HANDLE fd0, Message * m) |
112 { |
119 { |
122 if(second_board == (s_BOARD *)fd0) { |
129 if(second_board == (s_BOARD *)fd0) { |
123 //wait for CAN msg... |
130 //wait for CAN msg... |
124 result = WaitForSingleObject(hEvent2, INFINITE); |
131 result = WaitForSingleObject(hEvent2, INFINITE); |
125 if (result == WAIT_OBJECT_0) |
132 if (result == WAIT_OBJECT_0) |
126 Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
133 Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
|
134 if(Res == (CAN_ERR_QRCVEMPTY | CAN_ERRMASK_ILLHANDLE)) |
|
135 { |
|
136 ResetEvent(hEvent2); |
|
137 return 1; |
|
138 } |
127 } |
139 } |
128 else |
140 else |
129 #endif |
141 #endif |
130 |
142 |
131 // We read the queue looking for messages. |
143 // We read the queue looking for messages. |
132 if(first_board == (s_BOARD *)fd0) { |
144 if(first_board == (s_BOARD *)fd0) { |
133 result = WaitForSingleObject(hEvent1, INFINITE); |
145 result = WaitForSingleObject(hEvent1, INFINITE); |
134 if (result == WAIT_OBJECT_0) |
146 if (result == WAIT_OBJECT_0) |
|
147 { |
135 Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
148 Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
|
149 if(Res == (CAN_ERR_QRCVEMPTY | CAN_ERRMASK_ILLHANDLE)) |
|
150 { |
|
151 ResetEvent(hEvent1); |
|
152 return 1; |
|
153 } |
|
154 } |
136 } |
155 } |
137 else |
156 else |
138 Res = CAN_ERR_BUSOFF; |
157 Res = CAN_ERR_BUSOFF; |
139 |
158 |
140 // A message was received : we process the message(s) |
159 // A message was received : we process the message(s) |
245 /***************************************************************************/ |
264 /***************************************************************************/ |
246 CAN_HANDLE canOpen_driver (s_BOARD * board) |
265 CAN_HANDLE canOpen_driver (s_BOARD * board) |
247 { |
266 { |
248 char busname[64]; |
267 char busname[64]; |
249 char* pEnd; |
268 char* pEnd; |
|
269 int ret; |
250 |
270 |
251 //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0)); |
271 //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0)); |
252 if (strtol(board->busname, &pEnd,0) == 0) |
272 if (strtol(board->busname, &pEnd,0) == 0) |
253 { |
273 { |
254 first_board = board; |
274 first_board = board; |
255 printf ("First Board selected\n"); |
275 //printf ("First Board selected\n"); |
256 canInit(board); |
276 ret = canInit(board); |
257 return (CAN_HANDLE)board; |
277 if(ret) |
|
278 return (CAN_HANDLE)board; |
258 } |
279 } |
259 #ifdef PCAN2_HEADER_ |
280 #ifdef PCAN2_HEADER_ |
260 if (strtol(board->busname, &pEnd,0) == 1) |
281 if (strtol(board->busname, &pEnd,0) == 1) |
261 { |
282 { |
262 second_board = board; |
283 second_board = board; |
263 printf ("Second Board selected\n"); |
284 //printf ("Second Board selected\n"); |
264 canInit(board); |
285 ret = canInit(board); |
265 return (CAN_HANDLE)board; |
286 if(ret) |
|
287 return (CAN_HANDLE)board; |
266 } |
288 } |
267 #endif |
289 #endif |
268 return NULL; |
290 return NULL; |
269 } |
291 } |
270 |
292 |