# HG changeset patch # User Florian Pose # Date 1147247077 0 # Node ID 9864f39319a543150580a1e9bf9244eacb078a71 # Parent 342ad851ec786dd4102802141aad832815c30d54 Added rtai example. diff -r 342ad851ec78 -r 9864f39319a5 examples/rtai/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rtai/Makefile Wed May 10 07:44:37 2006 +0000 @@ -0,0 +1,60 @@ +#---------------------------------------------------------------- +# +# Makefile +# +# Minimal EtherCAT module. +# +# $Id$ +# +# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH +# +# This file is part of the IgH EtherCAT Master. +# +# The IgH EtherCAT Master is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 of the License. +# +# The IgH EtherCAT Master is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with the IgH EtherCAT Master; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +#---------------------------------------------------------------- + +ifneq ($(KERNELRELEASE),) + +#---------------------------------------------------------------- +# kbuild section +#---------------------------------------------------------------- + +obj-m := ec_mini.o + +ec_mini-objs := mini.o + +#---------------------------------------------------------------- + +else + +#---------------------------------------------------------------- +# default section +#---------------------------------------------------------------- + +ifneq ($(wildcard ethercat.conf),) +include ethercat.conf +else +KERNELDIR = /usr/src/linux +endif + +modules: + $(MAKE) -C $(KERNELDIR) M=`pwd` + +clean: + $(MAKE) -C $(KERNELDIR) M=`pwd` clean + +#---------------------------------------------------------------- + +endif diff -r 342ad851ec78 -r 9864f39319a5 examples/rtai/ethercat.conf.tmpl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rtai/ethercat.conf.tmpl Wed May 10 07:44:37 2006 +0000 @@ -0,0 +1,13 @@ +#------------------------------------------------------------------------------ +# +# EtherCAT Konfigurationsdatei Kernel 2.6 +# +# $Id$ +# +# This file is a versioned template configuration. Copy it to "ethercat.conf" +# (which is ignored by Subversion) and adjust it to your needs. +# +#------------------------------------------------------------------------------ + +KERNELDIR = /usr/src/linux + diff -r 342ad851ec78 -r 9864f39319a5 examples/rtai/mini.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/rtai/mini.c Wed May 10 07:44:37 2006 +0000 @@ -0,0 +1,255 @@ +/****************************************************************************** + * + * m i n i . c + * + * Minimal module for EtherCAT. + * + * $Id$ + * + * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH + * + * This file is part of the IgH EtherCAT Master. + * + * The IgH EtherCAT Master is free software; you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; version 2 of the License. + * + * The IgH EtherCAT Master is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with the IgH EtherCAT Master; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + +#include +#include +#include +#include +#include + +#include "../include/ecrt.h" // EtherCAT realtime interface + +#define ASYNC +#define FREQUENCY 100 + +/*****************************************************************************/ + +struct timer_list timer; + +// EtherCAT +ec_master_t *master = NULL; +ec_domain_t *domain1 = NULL; +spinlock_t master_lock = SPIN_LOCK_UNLOCKED; + +// data fields +//void *r_ssi_input, *r_ssi_status, *r_4102[3]; + +// channels +uint32_t k_pos; +uint8_t k_stat; + +ec_field_init_t domain1_fields[] = { + {NULL, "3", "Beckhoff", "EL5001", "InputValue", 0}, + {NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0}, + {} +}; + +/*****************************************************************************/ + +void run(unsigned long data) +{ + static unsigned int counter = 0; + + spin_lock(&master_lock); + +#ifdef ASYNC + // receive + ecrt_master_async_receive(master); + ecrt_domain_process(domain1); +#else + // send and receive + ecrt_domain_queue(domain1); + ecrt_master_run(master); + ecrt_master_sync_io(master); + ecrt_domain_process(domain1); +#endif + + // process data + //k_pos = EC_READ_U32(r_ssi); + +#ifdef ASYNC + // send + ecrt_domain_queue(domain1); + ecrt_master_run(master); + ecrt_master_async_send(master); +#endif + + spin_unlock(&master_lock); + + if (counter) { + counter--; + } + else { + counter = FREQUENCY; + //printk(KERN_INFO "k_pos = %i\n", k_pos); + //printk(KERN_INFO "k_stat = 0x%02X\n", k_stat); + } + + // restart timer + timer.expires += HZ / FREQUENCY; + add_timer(&timer); +} + +/*****************************************************************************/ + +int request_lock(void *data) +{ + unsigned int tries = 0; + while (1) { + if (spin_trylock(&master_lock)) { + if (tries) printk(KERN_INFO "lock: %i tries needed.\n", tries); + return 1; + } + tries++; + } +} + +/*****************************************************************************/ + +void release_lock(void *data) +{ + spin_unlock(&master_lock); +} + +/*****************************************************************************/ + +int __init init_mini_module(void) +{ + printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); + + if ((master = ecrt_request_master(0)) == NULL) { + printk(KERN_ERR "Requesting master 0 failed!\n"); + goto out_return; + } + + ecrt_master_callbacks(master, request_lock, release_lock, NULL); + + printk(KERN_INFO "Registering domain...\n"); + if (!(domain1 = ecrt_master_create_domain(master))) + { + printk(KERN_ERR "Domain creation failed!\n"); + goto out_release_master; + } + + printk(KERN_INFO "Registering domain fields...\n"); + if (ecrt_domain_register_field_list(domain1, domain1_fields)) { + printk(KERN_ERR "Field registration failed!\n"); + goto out_release_master; + } + + printk(KERN_INFO "Activating master...\n"); + if (ecrt_master_activate(master)) { + printk(KERN_ERR "Failed to activate master!\n"); + goto out_release_master; + } + +#if 0 + if (ecrt_master_fetch_sdo_lists(master)) { + printk(KERN_ERR "Failed to fetch SDO lists!\n"); + goto out_deactivate; + } + ecrt_master_print(master, 2); +#else + ecrt_master_print(master, 0); +#endif + +#if 0 + if (!(slave = ecrt_master_get_slave(master, "5"))) { + printk(KERN_ERR "Failed to get slave 5!\n"); + goto out_deactivate; + } + + if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || + ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || + ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || + ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || + ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || + ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || + ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || + ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || + ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { + printk(KERN_ERR "Failed to configure SSI slave!\n"); + goto out_deactivate; + } +#endif + +#if 0 + printk(KERN_INFO "Writing alias...\n"); + if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { + printk(KERN_ERR "Failed to write alias!\n"); + goto out_deactivate; + } +#endif + +#ifdef ASYNC + // send once and wait... + ecrt_master_prepare_async_io(master); +#endif + +#if 1 + if (ecrt_master_start_eoe(master)) { + printk(KERN_ERR "Failed to start EoE processing!\n"); + goto out_deactivate; + } +#endif + + printk("Starting cyclic sample thread.\n"); + init_timer(&timer); + timer.function = run; + timer.expires = jiffies + 10; + add_timer(&timer); + + printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); + return 0; + +#if 1 + out_deactivate: + ecrt_master_deactivate(master); +#endif + out_release_master: + ecrt_release_master(master); + out_return: + return -1; +} + +/*****************************************************************************/ + +void __exit cleanup_mini_module(void) +{ + printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); + + if (master) { + del_timer_sync(&timer); + printk(KERN_INFO "Deactivating master...\n"); + ecrt_master_deactivate(master); + ecrt_release_master(master); + } + + printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); +} + +/*****************************************************************************/ + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR ("Florian Pose "); +MODULE_DESCRIPTION ("EtherCAT minimal test environment"); + +module_init(init_mini_module); +module_exit(cleanup_mini_module); + +/*****************************************************************************/ +