fp@0: /****************************************************************************** fp@0: * fp@54: * m i n i . c fp@0: * fp@0: * Minimalmodul für EtherCAT fp@13: * fp@0: * $Id$ fp@0: * fp@42: *****************************************************************************/ fp@0: fp@0: #include fp@24: #include fp@22: #include fp@0: fp@104: #include "../include/ecrt.h" // Echtzeitschnittstelle fp@104: fp@106: #define ASYNC fp@5: fp@42: /*****************************************************************************/ fp@5: fp@98: #define ABTASTFREQUENZ 100 fp@22: fp@22: struct timer_list timer; fp@22: fp@73: /*****************************************************************************/ fp@73: fp@73: // EtherCAT fp@73: ec_master_t *master = NULL; fp@73: ec_domain_t *domain1 = NULL; fp@73: fp@83: // Datenfelder fp@130: //void *r_ssi_input, *r_ssi_status, *r_4102[3]; fp@73: fp@83: // Kanäle fp@104: uint32_t k_pos; fp@111: uint8_t k_stat; fp@73: fp@73: ec_field_init_t domain1_fields[] = { fp@117: {NULL, "1", "Beckhoff", "EL1014", "InputValue", 0}, fp@130: {NULL, "2", "Beckhoff", "EL2004", "OutputValue", 0}, fp@130: //{NULL, "3", "Beckhoff", "EL3162", "InputValue", 0}, fp@130: {NULL, "4", "Beckhoff", "EL4132", "OutputValue", 0}, fp@130: {NULL, "6", "Beckhoff", "EL5001", "InputValue", 0}, fp@73: {} fp@61: }; fp@0: fp@60: /*****************************************************************************/ fp@60: fp@60: void run(unsigned long data) fp@5: { fp@73: static unsigned int counter = 0; fp@68: fp@104: #ifdef ASYNC fp@112: // Prozessdaten empfangen fp@104: ecrt_master_async_receive(master); fp@104: ecrt_domain_process(domain1); fp@73: fp@104: // Prozessdaten verarbeiten fp@117: //k_pos = EC_READ_U32(r_ssi_input); fp@117: //k_stat = EC_READ_U8(r_ssi_status); fp@22: fp@104: // Prozessdaten senden fp@104: ecrt_domain_queue(domain1); fp@104: ecrt_master_async_send(master); fp@104: #else fp@112: // Prozessdaten senden und empfangen fp@104: ecrt_domain_queue(domain1); fp@104: ecrt_master_sync_io(master); fp@104: ecrt_domain_process(domain1); fp@104: fp@104: // Prozessdaten verarbeiten fp@117: //k_pos = EC_READ_U32(r_ssi_input); fp@117: //k_stat = EC_READ_U8(r_ssi_status); fp@104: #endif fp@104: fp@68: if (counter) { fp@68: counter--; fp@68: } fp@68: else { fp@73: counter = ABTASTFREQUENZ; fp@117: //printk(KERN_INFO "k_pos = %i\n", k_pos); fp@117: //printk(KERN_INFO "k_stat = 0x%02X\n", k_stat); fp@68: } fp@68: fp@62: // Timer neu starten fp@73: timer.expires += HZ / ABTASTFREQUENZ; fp@22: add_timer(&timer); fp@0: } fp@22: fp@60: /*****************************************************************************/ fp@22: fp@54: int __init init_mini_module(void) fp@5: { fp@36: printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); fp@36: fp@104: if ((master = ecrt_request_master(0)) == NULL) { fp@73: printk(KERN_ERR "Error requesting master 0!\n"); fp@36: goto out_return; fp@36: } fp@36: fp@109: ecrt_master_print(master); fp@73: fp@73: printk(KERN_INFO "Registering domain...\n"); fp@73: fp@104: if (!(domain1 = ecrt_master_create_domain(master))) fp@73: { fp@73: printk(KERN_ERR "EtherCAT: Could not register domain!\n"); fp@60: goto out_release_master; fp@60: } fp@36: fp@73: printk(KERN_INFO "Registering domain fields...\n"); fp@36: fp@104: if (ecrt_domain_register_field_list(domain1, domain1_fields)) { fp@104: printk(KERN_ERR "EtherCAT: Could not register field!\n"); fp@104: goto out_release_master; fp@36: } fp@36: fp@73: printk(KERN_INFO "Activating master...\n"); fp@64: fp@104: if (ecrt_master_activate(master)) { fp@73: printk(KERN_ERR "EtherCAT: Could not activate master!\n"); fp@68: goto out_release_master; fp@68: } fp@64: fp@111: //ecrt_master_debug(master, 2); fp@111: fp@130: #if 0 fp@111: if (ecrt_master_sdo_write(master, "1", 0x4061, 1, 0, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4061, 2, 1, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4061, 3, 1, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4066, 0, 0, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4067, 0, 4, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4068, 0, 0, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x4069, 0, 25, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x406A, 0, 25, 1) || fp@111: ecrt_master_sdo_write(master, "1", 0x406B, 0, 50, 1)) { fp@111: printk(KERN_ERR "EtherCAT: Failed to configure SSI!\n"); fp@111: goto out_deactivate; fp@111: } fp@130: #endif fp@111: fp@111: //ecrt_master_debug(master, 0); fp@111: fp@114: #if 0 fp@114: printk(KERN_INFO "Writing alias...\n"); fp@114: if (ecrt_master_write_slave_alias(master, "0", 0xBEEF)) { fp@114: printk(KERN_ERR "EtherCAT: Failed to write alias!\n"); fp@114: goto out_deactivate; fp@114: } fp@114: #endif fp@114: fp@104: #ifdef ASYNC fp@106: // Einmal senden und warten... fp@106: ecrt_master_prepare_async_io(master); fp@104: #endif fp@104: fp@36: printk("Starting cyclic sample thread.\n"); fp@36: fp@36: init_timer(&timer); fp@36: timer.function = run; fp@60: timer.expires = jiffies + 10; // Das erste Mal sofort feuern fp@36: add_timer(&timer); fp@36: fp@36: printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); fp@36: fp@36: return 0; fp@36: fp@130: #if 0 fp@111: out_deactivate: fp@111: ecrt_master_deactivate(master); fp@130: #endif fp@111: fp@36: out_release_master: fp@111: ecrt_release_master(master); fp@36: fp@36: out_return: fp@111: return -1; fp@0: } fp@0: fp@60: /*****************************************************************************/ fp@22: fp@54: void __exit cleanup_mini_module(void) fp@0: { fp@26: printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); fp@0: fp@54: if (master) fp@22: { fp@36: del_timer_sync(&timer); fp@22: fp@73: printk(KERN_INFO "Deactivating master...\n"); fp@42: fp@104: ecrt_master_deactivate(master); fp@104: ecrt_release_master(master); fp@27: } fp@22: fp@26: printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); fp@0: } fp@0: fp@0: /*****************************************************************************/ fp@0: fp@0: MODULE_LICENSE("GPL"); fp@0: MODULE_AUTHOR ("Florian Pose "); fp@73: MODULE_DESCRIPTION ("EtherCAT minimal test environment"); fp@0: fp@54: module_init(init_mini_module); fp@54: module_exit(cleanup_mini_module); fp@0: fp@0: /*****************************************************************************/ fp@42: fp@42: /* Emacs-Konfiguration fp@42: ;;; Local Variables: *** fp@42: ;;; c-basic-offset:4 *** fp@42: ;;; End: *** fp@42: */