fp@220: /****************************************************************************** fp@220: * fp@225: * RTAI sample for the IgH EtherCAT master. 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@233: // Linux fp@220: #include fp@220: fp@230: // RTAI fp@225: #include "rtai_sched.h" fp@225: #include "rtai_sem.h" fp@225: fp@233: // EtherCAT fp@230: #include "../../include/ecrt.h" fp@325: #include "../../include/ecdb.h" fp@230: fp@230: /*****************************************************************************/ fp@230: fp@230: // RTAI task frequency in Hz fp@643: #define FREQUENCY 2000 fp@342: #define INHIBIT_TIME 20 fp@342: fp@230: #define TIMERTICKS (1000000000 / FREQUENCY) fp@225: fp@643: #define PFX "ec_rtai_sample: " fp@643: fp@225: /*****************************************************************************/ fp@225: fp@225: // RTAI fp@643: static RT_TASK task; fp@643: static SEM master_sem; fp@643: static cycles_t t_last_cycle = 0, t_critical; fp@220: fp@220: // EtherCAT fp@643: static ec_master_t *master = NULL; fp@643: static ec_domain_t *domain1 = NULL; fp@643: static ec_master_status_t master_status, old_status = {}; fp@220: fp@220: // data fields fp@643: static void *r_dig_out; fp@643: static void *r_ana_out; fp@643: static void *r_count; fp@643: //static void *r_freq; fp@643: fp@643: const static ec_pdo_reg_t domain1_pdo_regs[] = { fp@643: {"2", Beckhoff_EL2004_Outputs, &r_dig_out}, fp@643: {"3", Beckhoff_EL4132_Output1, &r_ana_out}, fp@643: {"#888:1", Beckhoff_EL5101_Value, &r_count}, fp@643: //{"4", Beckhoff_EL5101_Frequency, &r_freq}, fp@220: {} fp@220: }; fp@220: fp@220: /*****************************************************************************/ fp@220: fp@225: void run(long data) fp@225: { fp@516: static unsigned int blink = 0; fp@516: static unsigned int counter = 0; fp@516: fp@512: while (1) { fp@342: t_last_cycle = get_cycles(); fp@512: fp@225: rt_sem_wait(&master_sem); fp@325: ecrt_master_receive(master); fp@225: ecrt_domain_process(domain1); fp@512: rt_sem_signal(&master_sem); fp@325: fp@325: // process data fp@516: EC_WRITE_U8(r_dig_out, blink ? 0x0F : 0x00); fp@325: fp@512: rt_sem_wait(&master_sem); fp@509: ecrt_domain_queue(domain1); fp@325: ecrt_master_send(master); fp@225: rt_sem_signal(&master_sem); fp@516: fp@516: if (counter) { fp@516: counter--; fp@516: } fp@516: else { fp@516: counter = FREQUENCY; fp@516: blink = !blink; fp@643: fp@643: rt_sem_wait(&master_sem); fp@643: ecrt_master_get_status(master, &master_status); fp@643: rt_sem_signal(&master_sem); fp@643: fp@643: if (master_status.bus_status != old_status.bus_status) { fp@643: printk(KERN_INFO PFX "bus status changed to %i.\n", fp@643: master_status.bus_status); fp@643: } fp@643: if (master_status.bus_tainted != old_status.bus_tainted) { fp@643: printk(KERN_INFO PFX "tainted flag changed to %u.\n", fp@643: master_status.bus_tainted); fp@643: } fp@643: if (master_status.slaves_responding != fp@643: old_status.slaves_responding) { fp@643: printk(KERN_INFO PFX "slaves_responding changed to %u.\n", fp@643: master_status.slaves_responding); fp@643: } fp@643: fp@643: old_status = master_status; fp@516: } fp@512: fp@225: rt_task_wait_period(); fp@225: } fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: int request_lock(void *data) fp@220: { fp@512: // too close to the next real time cycle: deny access... fp@342: if (get_cycles() - t_last_cycle > t_critical) return -1; fp@230: fp@230: // allow access fp@225: rt_sem_wait(&master_sem); fp@230: return 0; fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@220: void release_lock(void *data) fp@220: { fp@225: rt_sem_signal(&master_sem); fp@225: } fp@225: fp@225: /*****************************************************************************/ fp@225: fp@225: int __init init_mod(void) fp@225: { fp@225: RTIME tick_period, requested_ticks, now; fp@225: fp@643: printk(KERN_INFO PFX "Starting...\n"); fp@225: fp@225: rt_sem_init(&master_sem, 1); fp@220: fp@342: t_critical = cpu_khz * 1000 / FREQUENCY - cpu_khz * INHIBIT_TIME / 1000; fp@342: fp@342: if (!(master = ecrt_request_master(0))) { fp@643: printk(KERN_ERR PFX "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@643: printk(KERN_INFO PFX "Creating domain...\n"); fp@325: if (!(domain1 = ecrt_master_create_domain(master))) { fp@643: printk(KERN_ERR PFX "Domain creation failed!\n"); fp@220: goto out_release_master; fp@220: } fp@220: fp@643: printk(KERN_INFO PFX "Registering PDOs...\n"); fp@643: if (ecrt_domain_register_pdo_list(domain1, domain1_pdo_regs)) { fp@643: printk(KERN_ERR PFX "PDO registration failed!\n"); fp@220: goto out_release_master; fp@220: } fp@220: fp@643: printk(KERN_INFO PFX "Activating master...\n"); fp@220: if (ecrt_master_activate(master)) { fp@643: printk(KERN_ERR PFX "Failed to activate master!\n"); fp@220: goto out_release_master; fp@220: } fp@220: fp@643: printk(KERN_INFO PFX "Starting cyclic sample thread...\n"); fp@225: requested_ticks = nano2count(TIMERTICKS); fp@225: tick_period = start_rt_timer(requested_ticks); fp@643: printk(KERN_INFO PFX "RT timer started with %i/%i ticks.\n", fp@225: (int) tick_period, (int) requested_ticks); fp@225: fp@225: if (rt_task_init(&task, run, 0, 2000, 0, 1, NULL)) { fp@643: printk(KERN_ERR PFX "Failed to init RTAI task!\n"); fp@225: goto out_stop_timer; fp@225: } fp@225: fp@225: now = rt_get_time(); fp@225: if (rt_task_make_periodic(&task, now + tick_period, tick_period)) { fp@643: printk(KERN_ERR PFX "Failed to run RTAI task!\n"); fp@225: goto out_stop_task; fp@225: } fp@225: fp@643: printk(KERN_INFO PFX "Initialized.\n"); fp@220: return 0; fp@220: fp@225: out_stop_task: fp@225: rt_task_delete(&task); fp@225: out_stop_timer: fp@225: stop_rt_timer(); fp@220: out_release_master: fp@655: printk(KERN_ERR PFX "Releasing master...\n"); fp@220: ecrt_release_master(master); fp@220: out_return: fp@225: rt_sem_delete(&master_sem); fp@655: printk(KERN_ERR PFX "Failed to load. Aborting.\n"); fp@220: return -1; fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@225: void __exit cleanup_mod(void) fp@225: { fp@655: printk(KERN_INFO PFX "Stopping...\n"); fp@225: fp@225: rt_task_delete(&task); fp@225: stop_rt_timer(); fp@225: ecrt_release_master(master); fp@225: rt_sem_delete(&master_sem); fp@225: fp@655: printk(KERN_INFO PFX "Unloading.\n"); fp@220: } fp@220: fp@220: /*****************************************************************************/ fp@220: fp@512: MODULE_LICENSE("GPL"); fp@512: MODULE_AUTHOR("Florian Pose "); fp@512: MODULE_DESCRIPTION("EtherCAT RTAI sample module"); fp@512: fp@225: module_init(init_mod); fp@225: module_exit(cleanup_mod); fp@225: fp@225: /*****************************************************************************/