13 #include <linux/timer.h> |
13 #include <linux/timer.h> |
14 |
14 |
15 #include "../drivers/ec_master.h" |
15 #include "../drivers/ec_master.h" |
16 #include "../drivers/ec_device.h" |
16 #include "../drivers/ec_device.h" |
17 #include "../drivers/ec_types.h" |
17 #include "../drivers/ec_types.h" |
|
18 #include "../drivers/ec_module.h" |
18 |
19 |
19 /******************************************************************************/ |
20 /******************************************************************************/ |
20 |
21 |
21 #define ECAT |
22 // Auskommentieren, wenn keine zyklischen Daten erwuenscht |
22 |
|
23 #define ECAT_OPEN |
|
24 #define ECAT_MASTER |
|
25 #define ECAT_SLAVES |
|
26 #define ECAT_CYCLIC_DATA |
23 #define ECAT_CYCLIC_DATA |
27 |
24 |
28 /******************************************************************************/ |
25 /******************************************************************************/ |
29 |
26 |
30 #ifdef ECAT |
|
31 |
|
32 extern EtherCAT_device_t rtl_ecat_dev; |
|
33 |
|
34 #ifdef ECAT_MASTER |
|
35 static EtherCAT_master_t *ecat_master = NULL; |
27 static EtherCAT_master_t *ecat_master = NULL; |
36 #endif |
28 |
37 |
|
38 #ifdef ECAT_SLAVES |
|
39 static EtherCAT_slave_t ecat_slaves[] = |
29 static EtherCAT_slave_t ecat_slaves[] = |
40 { |
30 { |
41 #if 0 |
31 #if 0 |
42 // Block 1 |
32 // Block 1 |
43 ECAT_INIT_SLAVE(Beckhoff_EK1100), |
33 ECAT_INIT_SLAVE(Beckhoff_EK1100), |
244 * |
226 * |
245 ******************************************************************************/ |
227 ******************************************************************************/ |
246 |
228 |
247 int __init init_module() |
229 int __init init_module() |
248 { |
230 { |
249 #ifdef ECAT |
231 printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
250 #ifdef ECAT_OPEN |
232 |
251 int rv = -1; |
233 if ((ecat_master = EtherCAT_master(0)) == NULL) |
252 #endif |
234 { |
253 #endif |
235 printk(KERN_ERR "No EtherCAT master available!\n"); |
254 |
236 return -1; |
255 printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); |
237 } |
256 |
238 |
257 #ifdef ECAT |
239 printk("Checking EtherCAT slaves.\n"); |
258 |
240 |
259 EtherCAT_device_debug(&rtl_ecat_dev); |
241 if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0) |
260 |
242 { |
261 #ifdef ECAT_OPEN |
243 printk(KERN_ERR "EtherCAT: Could not init slaves!\n"); |
262 printk("Opening EtherCAT device.\n"); |
244 return -1; |
263 |
245 } |
264 // HIER PASSIERT DER FEHLER: |
246 |
265 if (EtherCAT_device_open(&rtl_ecat_dev) < 0) |
247 printk("Activating all EtherCAT slaves.\n"); |
266 { |
248 |
267 printk(KERN_ERR "msr_modul: Could not initialize EtherCAT NIC.\n"); |
249 if (EtherCAT_activate_all_slaves(ecat_master) != 0) |
268 goto out_nothing; |
250 { |
269 } |
251 printk(KERN_ERR "EtherCAT: Could not activate slaves!\n"); |
270 |
252 return -1; |
271 if (!rtl_ecat_dev.dev) // Es gibt kein EtherCAT-Device |
253 } |
272 { |
254 |
273 printk(KERN_ERR "msr_modul: No EtherCAT device!\n"); |
255 #ifdef ECAT_CYCLIC_DATA |
274 goto out_close; |
256 printk("Starting cyclic sample thread.\n"); |
275 } |
257 |
276 #endif // ECAT_OPEN |
258 schedule(); |
277 |
259 mdelay(1000); |
278 #ifdef ECAT_MASTER |
260 schedule(); |
279 printk("Initialising EtherCAT master\n"); |
261 init_timer(&timer); |
280 |
262 |
281 if ((ecat_master = (EtherCAT_master_t *) kmalloc(sizeof(EtherCAT_master_t), GFP_KERNEL)) == 0) |
263 timer.function = run; |
282 { |
264 timer.data = 0; |
283 printk(KERN_ERR "msr_modul: Could not alloc memory for EtherCAT master!\n"); |
265 timer.expires = jiffies+10; // Das erste Mal sofort feuern |
284 goto out_close; |
266 last_start_jiffies = timer.expires; |
285 } |
267 add_timer(&timer); |
286 |
268 |
287 if (EtherCAT_master_init(ecat_master, &rtl_ecat_dev) < 0) |
269 printk("Initialised sample thread.\n"); |
288 { |
270 #endif |
289 printk(KERN_ERR "EtherCAT could not init master!\n"); |
271 |
290 goto out_master; |
272 printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
291 } |
273 |
292 |
274 return 0; |
293 //ecat_master->debug_level = 1; |
|
294 |
|
295 #endif // ECAT_MASTER |
|
296 |
|
297 #ifdef ECAT_SLAVES |
|
298 printk("Checking EtherCAT slaves.\n"); |
|
299 |
|
300 if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0) |
|
301 { |
|
302 printk(KERN_ERR "EtherCAT: Could not init slaves!\n"); |
|
303 goto out_masterclear; |
|
304 } |
|
305 |
|
306 printk("Activating all EtherCAT slaves.\n"); |
|
307 |
|
308 if (EtherCAT_activate_all_slaves(ecat_master) != 0) |
|
309 { |
|
310 printk(KERN_ERR "EtherCAT: Could not activate slaves!\n"); |
|
311 goto out_masterclear; |
|
312 } |
|
313 #endif |
|
314 |
|
315 #endif // ECAT |
|
316 |
|
317 #ifdef ECAT_CYCLIC_DATA |
|
318 printk("Starting cyclic sample thread.\n"); |
|
319 |
|
320 schedule(); |
|
321 mdelay(1000); |
|
322 schedule(); |
|
323 init_timer(&timer); |
|
324 |
|
325 timer.function = run; |
|
326 timer.data = 0; |
|
327 timer.expires = jiffies+10; // Das erste Mal sofort feuern |
|
328 last_start_jiffies = timer.expires; |
|
329 add_timer(&timer); |
|
330 |
|
331 printk("Initialised sample thread.\n"); |
|
332 #endif |
|
333 |
|
334 printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); |
|
335 |
|
336 return 0; |
|
337 |
|
338 #ifdef ECAT |
|
339 |
|
340 #ifdef ECAT_SLAVES |
|
341 out_masterclear: |
|
342 printk(KERN_INFO "Clearing EtherCAT master.\n"); |
|
343 EtherCAT_master_clear(ecat_master); |
|
344 #endif |
|
345 |
|
346 #ifdef ECAT_MASTER |
|
347 out_master: |
|
348 printk(KERN_INFO "Freeing EtherCAT master.\n"); |
|
349 kfree(ecat_master); |
|
350 #endif |
|
351 |
|
352 #ifdef ECAT_OPEN |
|
353 out_close: |
|
354 printk(KERN_INFO "Closing device.\n"); |
|
355 EtherCAT_device_close(&rtl_ecat_dev); |
|
356 |
|
357 out_nothing: |
|
358 return rv; |
|
359 #endif |
|
360 |
|
361 #endif // ECAT |
|
362 } |
275 } |
363 |
276 |
364 /****************************************************************************** |
277 /****************************************************************************** |
365 * |
278 * |
366 * Function: cleanup |
279 * Function: cleanup |
369 |
282 |
370 void __exit cleanup_module() |
283 void __exit cleanup_module() |
371 { |
284 { |
372 printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
285 printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); |
373 |
286 |
374 #ifdef ECAT_MASTER |
|
375 |
|
376 #ifdef ECAT |
|
377 if (ecat_master) |
287 if (ecat_master) |
378 { |
288 { |
379 #endif |
289 #ifdef ECAT_CYCLIC_DATA |
380 //ecat_master->debug_level = 1; |
290 del_timer_sync(&timer); |
381 |
291 EtherCAT_clear_process_data(ecat_master); |
382 #ifdef ECAT_CYCLIC_DATA |
|
383 |
|
384 del_timer_sync(&timer); |
|
385 |
|
386 #ifdef ECAT |
|
387 EtherCAT_clear_process_data(ecat_master); |
|
388 #endif |
|
389 |
|
390 #endif // ECAT_CYCLIC_DATA |
292 #endif // ECAT_CYCLIC_DATA |
391 |
293 |
392 #ifdef ECAT |
294 printk(KERN_INFO "Deactivating slaves.\n"); |
393 |
295 EtherCAT_deactivate_all_slaves(ecat_master); |
394 #ifdef ECAT_SLAVES |
296 } |
395 printk(KERN_INFO "Deactivating slaves.\n"); |
|
396 EtherCAT_deactivate_all_slaves(ecat_master); |
|
397 #endif |
|
398 |
|
399 printk(KERN_INFO "Clearing EtherCAT master.\n"); |
|
400 EtherCAT_master_clear(ecat_master); |
|
401 |
|
402 printk(KERN_INFO "Freeing EtherCAT master.\n"); |
|
403 kfree(ecat_master); |
|
404 ecat_master = NULL; |
|
405 } |
|
406 #endif // ECAT |
|
407 |
|
408 #endif // ECAT_MASTER |
|
409 |
|
410 #ifdef ECAT |
|
411 #ifdef ECAT_OPEN |
|
412 printk(KERN_INFO "Closing device.\n"); |
|
413 EtherCAT_device_close(&rtl_ecat_dev); |
|
414 #endif |
|
415 #endif |
|
416 |
297 |
417 printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
298 printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); |
418 } |
299 } |
419 |
300 |
420 /*****************************************************************************/ |
301 /*****************************************************************************/ |