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