fp@220: /****************************************************************************** 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@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. 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@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@220: *****************************************************************************/ fp@220: fp@220: #include fp@220: #include fp@220: #include fp@220: #include fp@220: fp@223: #include "../../include/ecrt.h" // EtherCAT realtime interface fp@1715: #include "../../include/ecdb.h" // EtherCAT slave database fp@220: fp@1744: #define PFX "ec_mini: " fp@1744: fp@220: #define FREQUENCY 100 fp@220: fp@1739: //#define KBUS fp@1732: fp@220: /*****************************************************************************/ fp@220: fp@1744: static struct timer_list timer; fp@220: fp@220: // EtherCAT fp@1744: static ec_master_t *master = NULL; fp@1744: static ec_domain_t *domain1 = NULL; fp@220: spinlock_t master_lock = SPIN_LOCK_UNLOCKED; fp@1744: static ec_master_status_t master_status, old_status = {}; fp@220: fp@220: // data fields fp@1732: #ifdef KBUS fp@1744: static void *r_inputs; fp@1744: static void *r_outputs; fp@1744: #endif fp@1744: fp@1744: static void *r_dig_out; fp@1744: static void *r_ana_out; fp@1744: static void *r_count; fp@1744: static void *r_freq; fp@1744: fp@1744: #if 1 fp@1744: const static ec_pdo_reg_t domain1_pdo_regs[] = { fp@1744: {"2", Beckhoff_EL2004_Outputs, &r_dig_out}, fp@1744: {"3", Beckhoff_EL4132_Output1, &r_ana_out}, fp@1744: {"#888:1", Beckhoff_EL5101_Value, &r_count}, fp@1744: {"4", Beckhoff_EL5101_Frequency, &r_freq}, fp@220: {} fp@220: }; fp@1732: #endif fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void run(unsigned long data) fp@220: { fp@220: static unsigned int counter = 0; fp@1739: static unsigned int blink = 0; fp@220: fp@220: // receive fp@1739: spin_lock(&master_lock); fp@1715: ecrt_master_receive(master); fp@220: ecrt_domain_process(domain1); fp@1739: spin_unlock(&master_lock); fp@220: fp@220: // process data fp@1739: // k_pos = EC_READ_U32(r_ssi); fp@1739: EC_WRITE_U8(r_dig_out, blink ? 0x0F : 0x00); fp@220: fp@220: if (counter) { fp@220: counter--; fp@220: } fp@220: else { fp@220: counter = FREQUENCY; fp@1739: blink = !blink; fp@1744: fp@1744: spin_lock(&master_lock); fp@1744: ecrt_master_get_status(master, &master_status); fp@1744: spin_unlock(&master_lock); fp@1744: fp@1744: if (master_status.bus_status != old_status.bus_status) { fp@1744: printk(KERN_INFO PFX "bus status changed to %i.\n", fp@1744: master_status.bus_status); fp@1744: } fp@1744: if (master_status.bus_tainted != old_status.bus_tainted) { fp@1744: printk(KERN_INFO PFX "tainted flag changed to %u.\n", fp@1744: master_status.bus_tainted); fp@1744: } fp@1744: if (master_status.slaves_responding != fp@1744: old_status.slaves_responding) { fp@1744: printk(KERN_INFO PFX "slaves_responding changed to %u.\n", fp@1744: master_status.slaves_responding); fp@1744: } fp@1744: fp@1744: old_status = master_status; fp@1739: } fp@1739: fp@1739: #ifdef KBUS fp@1739: EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00); fp@1739: #endif fp@1739: fp@1739: // send fp@1739: spin_lock(&master_lock); fp@1739: ecrt_domain_queue(domain1); fp@1739: spin_unlock(&master_lock); fp@1739: fp@1739: spin_lock(&master_lock); fp@1739: ecrt_master_send(master); fp@1739: spin_unlock(&master_lock); fp@220: fp@220: // restart timer fp@220: timer.expires += HZ / FREQUENCY; fp@220: add_timer(&timer); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: int request_lock(void *data) fp@220: { fp@1739: spin_lock(&master_lock); fp@226: return 0; // access allowed fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void release_lock(void *data) fp@220: { fp@1739: spin_unlock(&master_lock); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: int __init init_mini_module(void) fp@220: { fp@1744: #if 1 fp@1744: ec_slave_t *slave; fp@1744: #endif fp@1744: fp@1744: printk(KERN_INFO PFX "Starting...\n"); fp@1744: fp@1744: if (!(master = ecrt_request_master(0))) { fp@1744: printk(KERN_ERR PFX "Requesting master 0 failed!\n"); fp@1744: goto out_return; fp@1744: } fp@1744: fp@1744: ecrt_master_callbacks(master, request_lock, release_lock, NULL); fp@1744: fp@1744: printk(KERN_INFO PFX "Registering domain...\n"); fp@1744: if (!(domain1 = ecrt_master_create_domain(master))) { fp@1744: printk(KERN_ERR PFX "Domain creation failed!\n"); fp@1744: goto out_release_master; fp@1744: } fp@1744: fp@1744: #if 1 fp@1744: printk(KERN_INFO PFX "Configuring alternative PDO mapping...\n"); fp@1744: if (!(slave = ecrt_master_get_slave(master, "4", Beckhoff_EL5101))) fp@1744: goto out_release_master; fp@1744: fp@1744: if (ecrt_slave_pdo_mapping(slave, EC_DIR_INPUT, 2, 0x1A00, 0x1A02)) fp@1744: goto out_release_master; fp@1744: #endif fp@1744: fp@1744: printk(KERN_INFO PFX "Registering PDOs...\n"); fp@1744: #if 1 fp@1744: if (ecrt_domain_register_pdo_list(domain1, domain1_pdo_regs)) { fp@1744: printk(KERN_ERR PFX "PDO registration failed!\n"); fp@1744: goto out_release_master; fp@1744: } fp@1744: #endif fp@1744: fp@1744: #ifdef KBUS fp@1744: if (!(slave = ecrt_master_get_slave(master, "0", Beckhoff_BK1120))) fp@1744: goto out_release_master; fp@1744: fp@1744: if (!ecrt_domain_register_pdo_range( fp@1744: domain1, slave, EC_DIR_OUTPUT, 0, 4, &r_outputs)) { fp@1744: printk(KERN_ERR PFX "PDO registration failed!\n"); fp@1744: goto out_release_master; fp@1744: } fp@1744: fp@1744: if (!ecrt_domain_register_pdo_range( fp@1744: domain1, slave, EC_DIR_INPUT, 0, 4, &r_inputs)) { fp@1744: printk(KERN_ERR PFX "PDO registration failed!\n"); fp@1744: goto out_release_master; fp@1744: } fp@1744: #endif fp@1744: fp@1739: #if 0 fp@1744: if (!(slave = ecrt_master_get_slave(master, "4", Beckhoff_EL5001))) fp@1716: goto out_release_master; fp@1716: fp@1716: if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0)) fp@1716: goto out_release_master; fp@1732: #endif fp@1716: fp@1744: #if 1 fp@1744: #endif fp@1744: fp@1744: printk(KERN_INFO PFX "Activating master...\n"); fp@220: if (ecrt_master_activate(master)) { fp@1744: printk(KERN_ERR PFX "Failed to activate master!\n"); fp@1744: goto out_release_master; fp@1744: } fp@1744: fp@1744: printk(KERN_INFO PFX "Starting cyclic sample thread.\n"); fp@220: init_timer(&timer); fp@220: timer.function = run; fp@220: timer.expires = jiffies + 10; fp@220: add_timer(&timer); fp@220: fp@1744: printk(KERN_INFO PFX "Started.\n"); fp@220: return 0; fp@220: fp@220: out_release_master: fp@1744: printk(KERN_ERR PFX "Releasing master...\n"); fp@220: ecrt_release_master(master); fp@220: out_return: fp@1744: printk(KERN_ERR PFX "Failed to load. Aborting.\n"); fp@220: return -1; fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void __exit cleanup_mini_module(void) fp@220: { fp@1744: printk(KERN_INFO PFX "Stopping...\n"); fp@220: fp@1732: del_timer_sync(&timer); fp@1744: printk(KERN_INFO PFX "Releasing master...\n"); fp@1732: ecrt_release_master(master); fp@220: fp@1744: printk(KERN_INFO PFX "Unloading.\n"); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: MODULE_LICENSE("GPL"); fp@1739: MODULE_AUTHOR("Florian Pose "); fp@1739: MODULE_DESCRIPTION("EtherCAT minimal test environment"); fp@220: fp@220: module_init(init_mini_module); fp@220: module_exit(cleanup_mini_module); fp@220: fp@220: /*****************************************************************************/