40 ,getenv("PCANIO_Port") ? strtol(getenv("PCANIO_Port"),NULL,0):0\ |
40 ,getenv("PCANIO_Port") ? strtol(getenv("PCANIO_Port"),NULL,0):0\ |
41 ,getenv("PCANInterupt") ? strtol(getenv("PCANInterupt"),NULL,0):0 |
41 ,getenv("PCANInterupt") ? strtol(getenv("PCANInterupt"),NULL,0):0 |
42 #endif |
42 #endif |
43 |
43 |
44 static s_BOARD *first_board = NULL; |
44 static s_BOARD *first_board = NULL; |
45 #ifdef PCAN2_HEADER_ |
45 |
46 static s_BOARD *second_board = NULL; |
46 //Create the Event for the first board |
47 #endif |
47 HANDLE hEvent1 = NULL; |
48 |
48 |
49 //pthread_mutex_t PeakCan_mutex = PTHREAD_MUTEX_INITIALIZER; |
49 |
|
50 #ifdef PCAN2_HEADER_ |
|
51 static s_BOARD *second_board = NULL; |
|
52 HANDLE hEvent2 = NULL; |
|
53 #endif |
50 |
54 |
51 // Define for rtr CAN message |
55 // Define for rtr CAN message |
52 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR |
56 #define CAN_INIT_TYPE_ST_RTR MSGTYPE_STANDARD | MSGTYPE_RTR |
53 |
57 |
54 /***************************************************************************/ |
58 /***************************************************************************/ |
64 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; |
68 if(!strcmp( optarg, "5K")) return CAN_BAUD_5K; |
65 if(!strcmp( optarg, "none")) return 0; |
69 if(!strcmp( optarg, "none")) return 0; |
66 return 0x0000; |
70 return 0x0000; |
67 } |
71 } |
68 |
72 |
69 void |
73 void canInit (s_BOARD *board) |
70 canInit (s_BOARD *board) |
|
71 { |
74 { |
72 int baudrate; |
75 int baudrate; |
73 |
76 |
74 #ifdef PCAN2_HEADER_ |
77 #ifdef PCAN2_HEADER_ |
75 // if not the first handler |
78 // if not the first handler |
76 if(second_board == (s_BOARD *)board) |
79 if(second_board == (s_BOARD *)board) { |
77 if(baudrate = TranslateBaudeRate(board->baudrate)) |
80 if(baudrate = TranslateBaudeRate(board->baudrate)) |
78 CAN2_Init (baudrate, |
81 CAN2_Init (baudrate, |
79 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
82 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
80 #endif |
83 |
81 if(first_board == (s_BOARD *)board) |
84 //Create the Event for the first board |
|
85 hEvent2 = CreateEvent(NULL, // lpEventAttributes |
|
86 FALSE, // bManualReset |
|
87 FALSE, // bInitialState |
|
88 ""); // lpName |
|
89 |
|
90 //Set Event Handle for CANReadExt |
|
91 CAN2_SetRcvEvent(hEvent2); |
|
92 } |
|
93 else |
|
94 #endif |
|
95 if(first_board == (s_BOARD *)board) { |
82 if(baudrate = TranslateBaudeRate(board->baudrate)) |
96 if(baudrate = TranslateBaudeRate(board->baudrate)) |
83 CAN_Init (baudrate, |
97 CAN_Init (baudrate, |
84 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
98 CAN_INIT_TYPE_ST extra_PCAN_init_params); |
85 } |
99 |
86 |
100 //Create the Event for the first board |
87 /*********functions which permit to communicate with the board****************/ |
101 hEvent1 = CreateEvent(NULL, // lpEventAttributes |
88 UNS8 |
102 FALSE, // bManualReset |
89 canReceive_driver (CAN_HANDLE fd0, Message * m) |
103 FALSE, // bInitialState |
90 { |
104 ""); // lpName |
|
105 //Set Event Handle for CANReadExt |
|
106 CAN_SetRcvEvent(hEvent1); |
|
107 } |
|
108 } |
|
109 |
|
110 /********* functions which permit to communicate with the board ****************/ |
|
111 UNS8 canReceive_driver (CAN_HANDLE fd0, Message * m) |
|
112 { |
|
113 int ret=0; |
91 UNS8 data; |
114 UNS8 data; |
92 TPCANMsg peakMsg; |
115 TPCANMsg peakMsg; |
|
116 TPCANTimestamp peakRcvTime; |
93 DWORD Res; |
117 DWORD Res; |
94 do{ |
118 DWORD result; |
95 // We read the queue looking for messages. |
119 |
96 // |
120 #ifdef PCAN2_HEADER_ |
97 //pthread_mutex_lock (&PeakCan_mutex); |
121 // if not the first handler |
98 #ifdef PCAN2_HEADER_ |
122 if(second_board == (s_BOARD *)fd0) { |
99 // if not the first handler |
123 //wait for CAN msg... |
100 if(second_board == (s_BOARD *)fd0) |
124 result = WaitForSingleObject(hEvent2, INFINITE); |
101 Res = CAN2_Read (&peakMsg); |
125 if (result == WAIT_OBJECT_0) |
|
126 Res = CAN2_ReadEx(&peakMsg, &peakRcvTime); |
|
127 } |
|
128 else |
|
129 #endif |
|
130 |
|
131 // We read the queue looking for messages. |
|
132 if(first_board == (s_BOARD *)fd0) { |
|
133 result = WaitForSingleObject(hEvent1, INFINITE); |
|
134 if (result == WAIT_OBJECT_0) |
|
135 Res = CAN_ReadEx(&peakMsg, &peakRcvTime); |
|
136 } |
|
137 else |
|
138 Res = CAN_ERR_BUSOFF; |
|
139 |
|
140 // A message was received : we process the message(s) |
|
141 if (Res == CAN_ERR_OK) |
|
142 { |
|
143 // if something different that 11bit or rtr... problem |
|
144 if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) |
|
145 { |
|
146 if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) |
|
147 { |
|
148 printf ("!!! Peak board read : re-init\n"); |
|
149 canInit((s_BOARD*) fd0); |
|
150 usleep (10000); |
|
151 } |
|
152 |
|
153 // If status, return status if 29bit, return overrun |
|
154 return peakMsg.MSGTYPE == |
|
155 MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; |
|
156 } |
|
157 m->cob_id = peakMsg.ID; |
|
158 |
|
159 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ |
|
160 m->rtr = 0; |
102 else |
161 else |
103 #endif |
162 m->rtr = 1; |
104 if(first_board == (s_BOARD *)fd0) |
163 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
105 Res = CAN_Read (&peakMsg); |
164 for (data = 0; data < peakMsg.LEN; data++) |
106 else |
165 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
107 Res = CAN_ERR_BUSOFF; |
|
108 // A message was received |
|
109 // We process the message(s) |
|
110 // |
|
111 if (Res == CAN_ERR_OK) |
|
112 { |
|
113 // if something different that 11bit or rtr... problem |
|
114 if (peakMsg.MSGTYPE & ~(MSGTYPE_STANDARD | MSGTYPE_RTR)) |
|
115 { |
|
116 if (peakMsg.MSGTYPE == CAN_ERR_BUSOFF) |
|
117 { |
|
118 printf ("!!! Peak board read : re-init\n"); |
|
119 canInit((s_BOARD*) fd0); |
|
120 usleep (10000); |
|
121 } |
|
122 |
|
123 // If status, return status if 29bit, return overrun |
|
124 //pthread_mutex_unlock (&PeakCan_mutex); |
|
125 return peakMsg.MSGTYPE == |
|
126 MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN; |
|
127 } |
|
128 m->cob_id = peakMsg.ID; |
|
129 |
|
130 if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST) /* bits of MSGTYPE_ */ |
|
131 m->rtr = 0; |
|
132 else |
|
133 m->rtr = 1; |
|
134 m->len = peakMsg.LEN; /* count of data bytes (0..8) */ |
|
135 for (data = 0; data < peakMsg.LEN; data++) |
|
136 m->data[data] = peakMsg.DATA[data]; /* data bytes, up to 8 */ |
|
137 #if defined DEBUG_MSG_CONSOLE_ON |
166 #if defined DEBUG_MSG_CONSOLE_ON |
138 MSG("in : "); |
167 MSG("in : "); |
139 print_message(m); |
168 print_message(m); |
140 #endif |
169 #endif |
141 }else{ |
170 } |
142 //pthread_mutex_unlock (&PeakCan_mutex); |
171 else |
143 //if (Res != CAN_ERR_OK) |
172 { |
144 //{ |
173 if (!(Res & CAN_ERR_QRCVEMPTY |
145 if (! |
174 || Res & CAN_ERR_BUSLIGHT |
146 (Res & CAN_ERR_QRCVEMPTY || Res & CAN_ERR_BUSLIGHT |
175 || Res & CAN_ERR_BUSHEAVY)) |
147 || Res & CAN_ERR_BUSHEAVY)) |
176 { |
148 { |
177 printf ("canReceive returned error (%d)\n", Res); |
149 printf ("canReceive returned error (%d)\n", Res); |
178 return 1; |
150 return 1; |
179 } |
151 } |
180 } |
152 usleep (1000); |
181 return 0; |
153 } |
182 } |
154 }while(Res != CAN_ERR_OK); |
183 |
155 return 0; |
184 /***************************************************************************/ |
156 } |
185 UNS8 canSend_driver (CAN_HANDLE fd0, Message * m) |
157 |
|
158 /***************************************************************************/ |
|
159 UNS8 |
|
160 canSend_driver (CAN_HANDLE fd0, Message * m) |
|
161 { |
186 { |
162 UNS8 data; |
187 UNS8 data; |
163 TPCANMsg peakMsg; |
188 TPCANMsg peakMsg; |
164 peakMsg.ID = m->cob_id; /* 11/29 bit code */ |
189 peakMsg.ID = m->cob_id; /* 11/29 bit code */ |
165 if (m->rtr == 0) |
190 if (m->rtr == 0) |
216 printf("canChangeBaudRate not yet supported by this driver\n"); |
241 printf("canChangeBaudRate not yet supported by this driver\n"); |
217 return 0; |
242 return 0; |
218 } |
243 } |
219 |
244 |
220 /***************************************************************************/ |
245 /***************************************************************************/ |
221 CAN_HANDLE |
246 CAN_HANDLE canOpen_driver (s_BOARD * board) |
222 canOpen_driver (s_BOARD * board) |
|
223 { |
247 { |
224 char busname[64]; |
248 char busname[64]; |
225 char* pEnd; |
249 char* pEnd; |
226 |
250 |
227 //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0)); |
251 //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0)); |
228 if (strtol(board->busname, &pEnd,0) == 0) |
252 if (strtol(board->busname, &pEnd,0) == 0) |
229 { |
253 { |
230 first_board = board; |
254 first_board = board; |
231 printf ("First Board selected\n"); |
255 printf ("First Board selected\n"); |
232 canInit(board); |
256 canInit(board); |
233 return (CAN_HANDLE)board; |
257 return (CAN_HANDLE)board; |
234 } |
258 } |
235 #ifdef PCAN2_HEADER_ |
259 #ifdef PCAN2_HEADER_ |
236 if (strtol(board->busname, &pEnd,0) == 1) |
260 if (strtol(board->busname, &pEnd,0) == 1) |
237 { |
261 { |
238 second_board = board; |
262 second_board = board; |
239 printf ("Second Board selected\n"); |
263 printf ("Second Board selected\n"); |
240 canInit(board); |
264 canInit(board); |
241 return (CAN_HANDLE)board; |
265 return (CAN_HANDLE)board; |
242 } |
266 } |
243 #endif |
267 #endif |
244 return NULL; |
268 return NULL; |
245 } |
269 } |
246 |
270 |
247 /***************************************************************************/ |
271 /***************************************************************************/ |
248 int |
272 int canClose_driver (CAN_HANDLE fd0) |
249 canClose_driver (CAN_HANDLE fd0) |
|
250 { |
273 { |
251 #ifdef PCAN2_HEADER_ |
274 #ifdef PCAN2_HEADER_ |
252 // if not the first handler |
275 // if not the first handler |
253 if(second_board == (s_BOARD *)fd0) |
276 if(second_board == (s_BOARD *)fd0) |
254 { |
277 { |
|
278 ResetEvent(hEvent2); |
255 CAN2_Close (); |
279 CAN2_Close (); |
256 second_board = (s_BOARD *)NULL; |
280 second_board = (s_BOARD *)NULL; |
257 }else |
281 }else |
258 #endif |
282 #endif |
259 if(first_board == (s_BOARD *)fd0) |
283 if(first_board == (s_BOARD *)fd0) |
260 { |
284 { |
|
285 ResetEvent(hEvent1); |
261 CAN_Close (); |
286 CAN_Close (); |
262 first_board = (s_BOARD *)NULL; |
287 first_board = (s_BOARD *)NULL; |
263 } |
288 } |
264 return 0; |
289 return 0; |
265 } |
290 } |