fp@220: /****************************************************************************** fp@220: * fp@220: * m i n i . c fp@220: * fp@220: * Minimal module for EtherCAT. 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: #include fp@220: fp@223: #include "../../include/ecrt.h" // EtherCAT realtime interface fp@220: fp@220: #define ASYNC fp@220: #define FREQUENCY 100 fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: struct timer_list timer; fp@220: fp@220: // EtherCAT fp@220: ec_master_t *master = NULL; fp@220: ec_domain_t *domain1 = NULL; fp@220: spinlock_t master_lock = SPIN_LOCK_UNLOCKED; fp@220: fp@220: // data fields fp@220: //void *r_ssi_input, *r_ssi_status, *r_4102[3]; fp@275: void *r_kbus_in; fp@220: fp@220: // channels fp@220: uint32_t k_pos; fp@220: uint8_t k_stat; fp@220: fp@220: ec_field_init_t domain1_fields[] = { fp@275: {&r_kbus_in, "0", "Beckhoff", "BK1120", "Inputs", 0}, fp@220: {} fp@220: }; fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void run(unsigned long data) fp@220: { fp@220: static unsigned int counter = 0; fp@220: fp@220: spin_lock(&master_lock); fp@220: fp@220: #ifdef ASYNC fp@220: // receive fp@220: ecrt_master_async_receive(master); fp@220: ecrt_domain_process(domain1); fp@220: #else fp@220: // send and receive 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: // process data fp@220: //k_pos = EC_READ_U32(r_ssi); fp@220: fp@220: #ifdef ASYNC fp@220: // send 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: spin_unlock(&master_lock); fp@220: fp@220: if (counter) { fp@220: counter--; fp@220: } fp@220: else { fp@220: counter = FREQUENCY; fp@220: //printk(KERN_INFO "k_pos = %i\n", k_pos); fp@220: //printk(KERN_INFO "k_stat = 0x%02X\n", k_stat); fp@220: } 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@226: spin_lock_bh(&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@226: spin_unlock_bh(&master_lock); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: int __init init_mini_module(void) fp@220: { fp@275: ec_slave_t *slave; fp@275: fp@220: printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); fp@220: fp@220: if ((master = ecrt_request_master(0)) == NULL) { fp@220: printk(KERN_ERR "Requesting master 0 failed!\n"); fp@220: goto out_return; fp@220: } fp@220: fp@220: ecrt_master_callbacks(master, request_lock, release_lock, NULL); fp@220: fp@220: printk(KERN_INFO "Registering domain...\n"); fp@220: if (!(domain1 = ecrt_master_create_domain(master))) fp@220: { fp@220: printk(KERN_ERR "Domain creation failed!\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 "Field registration failed!\n"); fp@220: goto out_release_master; fp@220: } fp@220: fp@275: #if 1 fp@275: printk(KERN_INFO "Setting variable data field sizes...\n"); fp@275: if (!(slave = ecrt_master_get_slave(master, "0"))) { fp@275: printk(KERN_ERR "Failed to get slave!\n"); fp@275: goto out_deactivate; fp@275: } fp@275: ecrt_slave_field_size(slave, "Inputs", 0, 1); fp@275: #endif fp@275: fp@220: printk(KERN_INFO "Activating master...\n"); fp@220: if (ecrt_master_activate(master)) { fp@220: printk(KERN_ERR "Failed to activate master!\n"); fp@220: goto out_release_master; fp@220: } 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 0 fp@220: if (!(slave = ecrt_master_get_slave(master, "5"))) { fp@220: printk(KERN_ERR "Failed to get slave 5!\n"); fp@220: goto out_deactivate; fp@220: } fp@220: fp@220: if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || fp@220: ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { 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: #ifdef ASYNC fp@220: // send once and wait... fp@220: ecrt_master_prepare_async_io(master); fp@220: #endif fp@220: fp@220: #if 1 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: printk("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@220: printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); 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_return: fp@220: return -1; fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void __exit cleanup_mini_module(void) fp@220: { fp@220: printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); fp@220: fp@220: if (master) { fp@220: del_timer_sync(&timer); fp@220: printk(KERN_INFO "Deactivating master...\n"); fp@220: ecrt_master_deactivate(master); fp@220: ecrt_release_master(master); fp@220: } fp@220: fp@220: printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: MODULE_LICENSE("GPL"); fp@220: MODULE_AUTHOR ("Florian Pose "); fp@220: 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: /*****************************************************************************/ fp@220: