fp@1619: /****************************************************************************** fp@1619: * fp@1619: * RTAI sample for the IgH EtherCAT master. fp@1619: * fp@1619: * $Id$ fp@1619: * fp@1619: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1619: * fp@1619: * This file is part of the IgH EtherCAT Master. fp@1619: * fp@1619: * The IgH EtherCAT Master is free software; you can redistribute it fp@1619: * and/or modify it under the terms of the GNU General Public License fp@1619: * as published by the Free Software Foundation; either version 2 of the fp@1619: * License, or (at your option) any later version. fp@1619: * fp@1619: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1619: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1619: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1619: * GNU General Public License for more details. fp@1619: * fp@1619: * You should have received a copy of the GNU General Public License fp@1619: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1619: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1619: * fp@1619: * The right to use EtherCAT Technology is granted and comes free of fp@1619: * charge under condition of compatibility of product made by fp@1619: * Licensee. People intending to distribute/sell products based on the fp@1619: * code, have to sign an agreement to guarantee that products using fp@1619: * software based on IgH EtherCAT master stay compatible with the actual fp@1619: * EtherCAT specification (which are released themselves as an open fp@1619: * standard) as the (only) precondition to have the right to use EtherCAT fp@1619: * Technology, IP and trade marks. fp@1619: * fp@1619: *****************************************************************************/ fp@1619: fp@1619: // Linux fp@1619: #include fp@1619: fp@1619: // RTAI fp@1619: #include "rtai_sched.h" fp@1619: #include "rtai_sem.h" fp@1619: fp@1619: // EtherCAT fp@1619: #include "../../include/ecrt.h" fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: MODULE_LICENSE("GPL"); fp@1619: MODULE_AUTHOR("Florian Pose "); fp@1619: MODULE_DESCRIPTION("EtherCAT RTAI sample module"); fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: // comment this for synchronous IO fp@1619: #define ASYNC fp@1619: fp@1619: // RTAI task frequency in Hz fp@1619: #define FREQUENCY 10000 fp@1619: fp@1619: #define TIMERTICKS (1000000000 / FREQUENCY) fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: // RTAI fp@1619: RT_TASK task; fp@1619: SEM master_sem; fp@1619: cycles_t t_last_start = 0; fp@1619: cycles_t t_critical; fp@1619: fp@1619: // EtherCAT fp@1619: ec_master_t *master = NULL; fp@1619: ec_domain_t *domain1 = NULL; fp@1619: fp@1619: // data fields fp@1619: void *r_ssi_input; fp@1619: fp@1619: // channels fp@1619: uint32_t k_pos; fp@1619: fp@1619: ec_field_init_t domain1_fields[] = { fp@1619: {&r_ssi_input, "3", "Beckhoff", "EL5001", "InputValue", 0}, fp@1619: {NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0}, fp@1619: {} fp@1619: }; fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: void run(long data) fp@1619: { fp@1619: while (1) fp@1619: { fp@1619: t_last_start = get_cycles(); fp@1619: rt_sem_wait(&master_sem); fp@1619: fp@1619: #ifdef ASYNC fp@1619: // receive fp@1619: ecrt_master_async_receive(master); fp@1619: ecrt_domain_process(domain1); fp@1619: #else fp@1619: // send and receive fp@1619: ecrt_domain_queue(domain1); fp@1619: ecrt_master_run(master); fp@1619: ecrt_master_sync_io(master); fp@1619: ecrt_domain_process(domain1); fp@1619: #endif fp@1619: fp@1619: // process data fp@1619: k_pos = EC_READ_U32(r_ssi_input); fp@1619: fp@1619: #ifdef ASYNC fp@1619: // send fp@1619: ecrt_domain_queue(domain1); fp@1619: ecrt_master_run(master); fp@1619: ecrt_master_async_send(master); fp@1619: #endif fp@1619: fp@1619: rt_sem_signal(&master_sem); fp@1619: rt_task_wait_period(); fp@1619: } fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: int request_lock(void *data) fp@1619: { fp@1619: // too close to the next RT cycle: deny access... fp@1619: if (get_cycles() - t_last_start > t_critical) return -1; fp@1619: fp@1619: // allow access fp@1619: rt_sem_wait(&master_sem); fp@1619: return 0; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: void release_lock(void *data) fp@1619: { fp@1619: rt_sem_signal(&master_sem); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: int __init init_mod(void) fp@1619: { fp@1619: RTIME tick_period, requested_ticks, now; fp@1619: fp@1619: printk(KERN_INFO "=== Starting EtherCAT RTAI sample module... ===\n"); fp@1619: fp@1619: rt_sem_init(&master_sem, 1); fp@1619: fp@1619: t_critical = cpu_khz * 800 / FREQUENCY; // ticks for 80% fp@1619: fp@1619: if ((master = ecrt_request_master(0)) == NULL) { fp@1619: printk(KERN_ERR "Requesting master 0 failed!\n"); fp@1619: goto out_return; fp@1619: } fp@1619: fp@1619: ecrt_master_callbacks(master, request_lock, release_lock, NULL); fp@1619: fp@1619: printk(KERN_INFO "Registering domain...\n"); fp@1619: if (!(domain1 = ecrt_master_create_domain(master))) fp@1619: { fp@1619: printk(KERN_ERR "Domain creation failed!\n"); fp@1619: goto out_release_master; fp@1619: } fp@1619: fp@1619: printk(KERN_INFO "Registering domain fields...\n"); fp@1619: if (ecrt_domain_register_field_list(domain1, domain1_fields)) { fp@1619: printk(KERN_ERR "Field registration failed!\n"); fp@1619: goto out_release_master; fp@1619: } fp@1619: fp@1619: printk(KERN_INFO "Activating master...\n"); fp@1619: if (ecrt_master_activate(master)) { fp@1619: printk(KERN_ERR "Failed to activate master!\n"); fp@1619: goto out_release_master; fp@1619: } fp@1619: fp@1619: #if 0 fp@1619: if (ecrt_master_fetch_sdo_lists(master)) { fp@1619: printk(KERN_ERR "Failed to fetch SDO lists!\n"); fp@1619: goto out_deactivate; fp@1619: } fp@1619: ecrt_master_print(master, 2); fp@1619: #else fp@1619: ecrt_master_print(master, 0); fp@1619: #endif fp@1619: fp@1619: #if 0 fp@1619: if (!(slave = ecrt_master_get_slave(master, "5"))) { fp@1619: printk(KERN_ERR "Failed to get slave 5!\n"); fp@1619: goto out_deactivate; fp@1619: } fp@1619: fp@1619: if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || fp@1619: ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { fp@1619: printk(KERN_ERR "Failed to configure SSI slave!\n"); fp@1619: goto out_deactivate; fp@1619: } fp@1619: #endif fp@1619: fp@1619: #if 0 fp@1619: printk(KERN_INFO "Writing alias...\n"); fp@1619: if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { fp@1619: printk(KERN_ERR "Failed to write alias!\n"); fp@1619: goto out_deactivate; fp@1619: } fp@1619: #endif fp@1619: fp@1619: #ifdef ASYNC fp@1619: // send once and wait... fp@1619: ecrt_master_prepare_async_io(master); fp@1619: #endif fp@1619: fp@1619: if (ecrt_master_start_eoe(master)) { fp@1619: printk(KERN_ERR "Failed to start EoE processing!\n"); fp@1619: goto out_deactivate; fp@1619: } fp@1619: fp@1619: printk("Starting cyclic sample thread...\n"); fp@1619: requested_ticks = nano2count(TIMERTICKS); fp@1619: tick_period = start_rt_timer(requested_ticks); fp@1619: printk(KERN_INFO "RT timer started with %i/%i ticks.\n", fp@1619: (int) tick_period, (int) requested_ticks); fp@1619: fp@1619: if (rt_task_init(&task, run, 0, 2000, 0, 1, NULL)) { fp@1619: printk(KERN_ERR "Failed to init RTAI task!\n"); fp@1619: goto out_stop_timer; fp@1619: } fp@1619: fp@1619: now = rt_get_time(); fp@1619: if (rt_task_make_periodic(&task, now + tick_period, tick_period)) { fp@1619: printk(KERN_ERR "Failed to run RTAI task!\n"); fp@1619: goto out_stop_task; fp@1619: } fp@1619: fp@1619: printk(KERN_INFO "=== EtherCAT RTAI sample module started. ===\n"); fp@1619: return 0; fp@1619: fp@1619: out_stop_task: fp@1619: rt_task_delete(&task); fp@1619: out_stop_timer: fp@1619: stop_rt_timer(); fp@1619: out_deactivate: fp@1619: ecrt_master_deactivate(master); fp@1619: out_release_master: fp@1619: ecrt_release_master(master); fp@1619: out_return: fp@1619: rt_sem_delete(&master_sem); fp@1619: return -1; fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: void __exit cleanup_mod(void) fp@1619: { fp@1619: printk(KERN_INFO "=== Stopping EtherCAT RTAI sample module... ===\n"); fp@1619: fp@1619: rt_task_delete(&task); fp@1619: stop_rt_timer(); fp@1619: ecrt_master_deactivate(master); fp@1619: ecrt_release_master(master); fp@1619: rt_sem_delete(&master_sem); fp@1619: fp@1619: printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n"); fp@1619: } fp@1619: fp@1619: /*****************************************************************************/ fp@1619: fp@1619: module_init(init_mod); fp@1619: module_exit(cleanup_mod); fp@1619: fp@1619: /*****************************************************************************/ fp@1619: