fp@181: /****************************************************************************** fp@181: * fp@181: * m s r _ r t . c fp@181: * fp@181: * Kernelmodul für 2.6 Kernel zur Meßdatenerfassung, Steuerung und Regelung. fp@181: * fp@181: * $Id$ fp@181: * fp@197: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@197: * fp@197: * This file is part of the IgH EtherCAT Master. fp@197: * fp@197: * The IgH EtherCAT Master is free software; you can redistribute it fp@197: * and/or modify it under the terms of the GNU General Public License fp@197: * as published by the Free Software Foundation; version 2 of the License. fp@197: * fp@197: * The IgH EtherCAT Master is distributed in the hope that it will be fp@197: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@197: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@197: * GNU General Public License for more details. fp@197: * fp@197: * You should have received a copy of the GNU General Public License fp@197: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@197: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@197: * fp@181: *****************************************************************************/ fp@181: fp@181: // Linux fp@181: #include fp@181: #include fp@181: #include fp@181: #include fp@181: #include fp@181: fp@181: // RT_lib fp@181: #include fp@181: #include fp@181: #include fp@181: #include fp@181: #include fp@181: #include fp@181: #include "msr_param.h" fp@181: fp@181: // EtherCAT fp@181: #include "../include/ecrt.h" fp@181: fp@181: #define ASYNC fp@181: fp@181: // Defines/Makros fp@181: #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) fp@181: fp@181: /*****************************************************************************/ fp@181: /* Globale Variablen */ fp@181: fp@181: // Adeos fp@181: static struct ipipe_domain this_domain; fp@181: static struct ipipe_sysinfo sys_info; fp@181: fp@181: // EtherCAT fp@181: ec_master_t *master = NULL; fp@181: ec_domain_t *domain1 = NULL; fp@181: fp@181: // Prozessdaten fp@181: void *r_ssi; fp@181: void *r_ssi_st; fp@181: fp@181: // KanŽäle fp@181: uint32_t k_ssi; fp@181: uint32_t k_ssi_st; fp@181: fp@181: ec_field_init_t domain1_fields[] = { fp@188: {&r_ssi, "0:3", "Beckhoff", "EL5001", "InputValue", 0}, fp@188: {&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status", 0}, fp@181: {} fp@181: }; fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: static void msr_controller_run(void) fp@181: { fp@181: #ifdef ASYNC fp@181: // Empfangen fp@181: ecrt_master_async_receive(master); fp@181: ecrt_domain_process(domain1); fp@181: #else fp@181: // Senden und empfangen fp@181: ecrt_domain_queue(domain1); fp@181: ecrt_master_run(master); fp@181: ecrt_master_sync_io(master); fp@181: ecrt_domain_process(domain1); fp@181: #endif fp@181: fp@181: // Prozessdaten verarbeiten fp@181: k_ssi = EC_READ_U32(r_ssi); fp@181: k_ssi_st = EC_READ_U8 (r_ssi_st); fp@181: fp@181: #ifdef ASYNC fp@181: // Senden fp@181: ecrt_domain_queue(domain1); fp@181: ecrt_master_run(master); fp@181: ecrt_master_async_send(master); fp@181: #endif fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: int msr_globals_register(void) fp@181: { fp@181: msr_reg_kanal("/ssi_position", "", &k_ssi, TUINT); fp@181: msr_reg_kanal("/ssi_status", "", &k_ssi_st, TUINT); fp@181: return 0; fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: void msr_run(unsigned irq) fp@181: { fp@181: static int counter = 0; fp@181: fp@181: MSR_ADEOS_INTERRUPT_CODE(msr_controller_run(); msr_write_kanal_list();); fp@181: fp@181: ipipe_control_irq(irq, 0, IPIPE_ENABLE_MASK); // Interrupt bestŽätigen fp@181: if (++counter >= HZREDUCTION) { fp@181: ipipe_propagate_irq(irq); // und weiterreichen fp@181: counter = 0; fp@181: } fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: void domain_entry(void) fp@181: { fp@181: printk("Domain %s started.\n", ipipe_current_domain->name); fp@181: fp@181: ipipe_get_sysinfo(&sys_info); fp@181: ipipe_virtualize_irq(ipipe_current_domain,sys_info.archdep.tmirq, fp@181: &msr_run, NULL, IPIPE_HANDLE_MASK); fp@181: fp@181: ipipe_tune_timer(1000000000UL / MSR_ABTASTFREQUENZ, 0); fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: int __init init_rt_module(void) fp@181: { fp@181: struct ipipe_domain_attr attr; //ipipe fp@181: #if 1 fp@181: ec_slave_t *slave; fp@181: #endif fp@181: fp@181: // Als allererstes die RT-Lib initialisieren fp@181: if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_globals_register) < 0) { fp@181: printk(KERN_ERR "Failed to initialize rtlib!\n"); fp@181: goto out_return; fp@181: } fp@181: fp@181: if ((master = ecrt_request_master(0)) == NULL) { fp@181: printk(KERN_ERR "Failed to request master 0!\n"); fp@181: goto out_msr_cleanup; fp@181: } fp@181: fp@181: //ecrt_master_print(master, 2); fp@181: fp@181: printk(KERN_INFO "Creating domains...\n"); fp@181: if (!(domain1 = ecrt_master_create_domain(master))) { fp@181: printk(KERN_ERR "Failed to create domains!\n"); fp@181: goto out_release_master; fp@181: } fp@181: fp@181: printk(KERN_INFO "Registering domain fields...\n"); fp@181: if (ecrt_domain_register_field_list(domain1, domain1_fields)) { fp@181: printk(KERN_ERR "Failed to register domain fields.\n"); fp@181: goto out_release_master; fp@181: } fp@181: fp@181: printk(KERN_INFO "Activating master...\n"); fp@181: if (ecrt_master_activate(master)) { fp@181: printk(KERN_ERR "Could not activate master!\n"); fp@181: goto out_release_master; fp@181: } fp@181: fp@181: #if 0 fp@206: if (ecrt_master_start_eoe(master)) { fp@206: printk(KERN_ERR "Failed to start EoE processing!\n"); fp@206: goto out_deactivate; fp@206: } fp@206: #endif fp@206: fp@206: #if 0 fp@181: if (ecrt_master_fetch_sdo_lists(master)) { fp@181: printk(KERN_ERR "Failed to fetch SDO lists!\n"); fp@181: goto out_deactivate; fp@181: } fp@181: ecrt_master_print(master, 2); fp@181: #else fp@191: ecrt_master_print(master, 0); fp@181: #endif fp@181: fp@181: #if 1 fp@188: if (!(slave = ecrt_master_get_slave(master, "0:3"))) { fp@181: printk(KERN_ERR "Failed to get slave!\n"); fp@181: goto out_deactivate; fp@181: } fp@181: fp@181: if ( fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 1) || // disable frame error bit fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 0) || // power failure bit fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || // inhibit time fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4061, 4, 0) || // test mode fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 1) || // dualcode fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 5) || // 125kbaud fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || // single-turn fp@181: ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size fp@181: ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length fp@181: ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us fp@181: ) { fp@181: printk(KERN_ERR "Failed to configure SSI slave!\n"); fp@181: goto out_deactivate; fp@181: } fp@181: #endif fp@181: fp@188: #if 0 fp@188: if (!(slave = ecrt_master_get_slave(master, "1:0"))) { fp@188: printk(KERN_ERR "Failed to get slave!\n"); fp@188: goto out_deactivate; fp@188: } fp@188: if (ecrt_slave_write_alias(slave, 0x5678)) { fp@188: printk(KERN_ERR "Failed to write alias!\n"); fp@188: goto out_deactivate; fp@188: } fp@188: #endif fp@188: fp@181: #ifdef ASYNC fp@181: // Einmal senden und warten... fp@181: ecrt_master_prepare_async_io(master); fp@181: #endif fp@181: fp@181: ipipe_init_attr(&attr); fp@181: attr.name = "IPIPE-MSR-MODULE"; fp@181: attr.priority = IPIPE_ROOT_PRIO + 1; fp@181: attr.entry = &domain_entry; fp@181: ipipe_register_domain(&this_domain, &attr); fp@181: return 0; fp@181: fp@181: #if 1 fp@181: out_deactivate: fp@181: ecrt_master_deactivate(master); fp@181: #endif fp@181: out_release_master: fp@181: ecrt_release_master(master); fp@181: out_msr_cleanup: fp@181: msr_rtlib_cleanup(); fp@181: out_return: fp@181: return -1; fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: void __exit cleanup_rt_module(void) fp@181: { fp@181: printk(KERN_INFO "Cleanign up rt module...\n"); fp@181: fp@181: ipipe_tune_timer(1000000000UL / HZ, 0); // Alten Timertakt wiederherstellen fp@181: ipipe_unregister_domain(&this_domain); fp@181: fp@181: printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n"); fp@181: ecrt_master_deactivate(master); fp@181: ecrt_release_master(master); fp@181: printk(KERN_INFO "=== EtherCAT environment stopped. ===\n"); fp@181: fp@181: msr_rtlib_cleanup(); fp@181: } fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: #define EC_LIT(X) #X fp@181: #define EC_STR(X) EC_LIT(X) fp@181: #define COMPILE_INFO "Revision " EC_STR(SVNREV) \ fp@181: ", compiled by " EC_STR(USER) \ fp@181: " at " __DATE__ " " __TIME__ fp@181: fp@181: MODULE_LICENSE("GPL"); fp@181: MODULE_AUTHOR ("Florian Pose "); fp@181: MODULE_DESCRIPTION ("EtherCAT real-time test environment"); fp@181: MODULE_VERSION(COMPILE_INFO); fp@181: fp@181: module_init(init_rt_module); fp@181: module_exit(cleanup_rt_module); fp@181: fp@181: /*****************************************************************************/ fp@181: fp@181: /* Emacs-Konfiguration fp@181: ;;; Local Variables: *** fp@181: ;;; c-basic-offset:4 *** fp@181: ;;; End: *** fp@181: */