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 <linux/module.h>
fp@220: #include <linux/timer.h>
fp@220: #include <linux/spinlock.h>
fp@220: #include <linux/interrupt.h>
fp@220: 
fp@223: #include "../../include/ecrt.h" // EtherCAT realtime interface
fp@220: 
fp@820: /*****************************************************************************/
fp@820: 
fp@858: // Module parameters
fp@220: #define FREQUENCY 100
fp@858: 
fp@858: // Optional features
fp@879: #define CONFIGURE_PDOS
fp@809: #define EXTERNAL_MEMORY
fp@858: #define SDO_ACCESS
fp@457: 
fp@820: #define PFX "ec_mini: "
fp@820: 
fp@1027: #define AnaInPos  0, 1
fp@1026: #define DigOutPos 0, 3
fp@1026: 
fp@820: /*****************************************************************************/
fp@220: 
fp@220: // EtherCAT
fp@612: static ec_master_t *master = NULL;
fp@818: static ec_master_state_t master_state = {};
fp@818: spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
fp@818: 
fp@612: static ec_domain_t *domain1 = NULL;
fp@818: static ec_domain_state_t domain1_state = {};
fp@818: 
fp@1022: static ec_slave_config_t *sc_ana_in = NULL;
fp@1022: static ec_slave_config_state_t sc_ana_in_state = {};
fp@1022: 
fp@820: static struct timer_list timer;
fp@820: static unsigned int counter = 0;
fp@820: 
fp@820: /*****************************************************************************/
fp@820: 
fp@820: // process data
fp@820: static uint8_t *domain1_pd; // process data memory
fp@820: 
fp@820: static unsigned int off_ana_in; // offsets for Pdo entries
fp@820: static unsigned int off_dig_out;
fp@820: 
fp@820: static unsigned int blink = 0;
fp@820: 
fp@909: #define Beckhoff_EL2004 0x00000002, 0x07D43052
fp@909: #define Beckhoff_EL3162 0x00000002, 0x0C5A3052
fp@909: 
fp@820: const static ec_pdo_entry_reg_t domain1_regs[] = {
fp@1026:     {AnaInPos,  Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
fp@1026:     {DigOutPos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
fp@820:     {}
fp@820: };
fp@820: 
fp@820: /*****************************************************************************/
fp@820: 
fp@879: #ifdef CONFIGURE_PDOS
fp@851: static ec_pdo_entry_info_t el3162_channel1[] = {
fp@793:     {0x3101, 1,  8}, // status
fp@793:     {0x3101, 2, 16}  // value
fp@793: };
fp@793: 
fp@851: static ec_pdo_entry_info_t el3162_channel2[] = {
fp@793:     {0x3102, 1,  8}, // status
fp@793:     {0x3102, 2, 16}  // value
fp@793: };
fp@793: 
fp@879: static ec_pdo_info_t el3162_pdos[] = {
fp@793:     {EC_DIR_INPUT, 0x1A00, 2, el3162_channel1},
fp@793:     {EC_DIR_INPUT, 0x1A01, 2, el3162_channel2},
fp@879:     {EC_END}
fp@792: };
fp@809: 
fp@851: static ec_pdo_entry_info_t el2004_channels[] = {
fp@809:     {0x3001, 1, 1}, // Value 1
fp@809:     {0x3001, 2, 1}, // Value 2
fp@809:     {0x3001, 3, 1}, // Value 3
fp@809:     {0x3001, 4, 1}  // Value 4
fp@809: };
fp@809: 
fp@879: static ec_pdo_info_t el2004_pdos[] = {
fp@809:     {EC_DIR_OUTPUT, 0x1600, 1, &el2004_channels[0]},
fp@809:     {EC_DIR_OUTPUT, 0x1601, 1, &el2004_channels[1]},
fp@809:     {EC_DIR_OUTPUT, 0x1602, 1, &el2004_channels[2]},
fp@809:     {EC_DIR_OUTPUT, 0x1603, 1, &el2004_channels[3]},
fp@1026:     {EC_END}
fp@809: };
fp@792: #endif
fp@792: 
fp@818: /*****************************************************************************/
fp@818: 
fp@858: #ifdef SDO_ACCESS
fp@858: static ec_sdo_request_t *sdo;
fp@858: #endif
fp@858: 
fp@858: /*****************************************************************************/
fp@858: 
fp@818: void check_domain1_state(void)
fp@818: {
fp@818:     ec_domain_state_t ds;
fp@818: 
fp@1022:     spin_lock(&master_lock);
fp@818:     ecrt_domain_state(domain1, &ds);
fp@1022:     spin_unlock(&master_lock);
fp@1022: 
fp@818:     if (ds.working_counter != domain1_state.working_counter)
fp@1022:         printk(KERN_INFO PFX "Domain1: WC %u.\n", ds.working_counter);
fp@818:     if (ds.wc_state != domain1_state.wc_state)
fp@1022:         printk(KERN_INFO PFX "Domain1: State %u.\n", ds.wc_state);
fp@818: 
fp@818:     domain1_state = ds;
fp@818: }
fp@818: 
fp@818: /*****************************************************************************/
fp@818: 
fp@818: void check_master_state(void)
fp@818: {
fp@818:     ec_master_state_t ms;
fp@818: 
fp@818:     spin_lock(&master_lock);
fp@818:     ecrt_master_state(master, &ms);
fp@818:     spin_unlock(&master_lock);
fp@818: 
fp@1022:     if (ms.slaves_responding != master_state.slaves_responding)
fp@1022:         printk(KERN_INFO PFX "%u slave(s).\n", ms.slaves_responding);
fp@1022:     if (ms.al_states != master_state.al_states)
fp@1022:         printk(KERN_INFO PFX "AL states: 0x%02X.\n", ms.al_states);
fp@1022:     if (ms.link_up != master_state.link_up)
fp@1022:         printk(KERN_INFO PFX "Link is %s.\n", ms.link_up ? "up" : "down");
fp@818: 
fp@818:     master_state = ms;
fp@818: }
fp@220: 
fp@220: /*****************************************************************************/
fp@220: 
fp@1022: void check_slave_config_states(void)
fp@1022: {
fp@1022:     ec_slave_config_state_t s;
fp@1022: 
fp@1022:     spin_lock(&master_lock);
fp@1022:     ecrt_slave_config_state(sc_ana_in, &s);
fp@1022:     spin_unlock(&master_lock);
fp@1022: 
fp@1022:     if (s.al_state != sc_ana_in_state.al_state)
fp@1022:         printk(KERN_INFO PFX "AnaIn: State 0x%02X.\n", s.al_state);
fp@1022:     if (s.online != sc_ana_in_state.online)
fp@1022:         printk(KERN_INFO PFX "AnaIn: %s.\n", s.online ? "online" : "offline");
fp@1022:     if (s.operational != sc_ana_in_state.operational)
fp@1022:         printk(KERN_INFO PFX "AnaIn: %soperational.\n",
fp@1022:                 s.operational ? "" : "Not ");
fp@1022: 
fp@1022:     sc_ana_in_state = s;
fp@1022: }
fp@1022: 
fp@1022: /*****************************************************************************/
fp@1022: 
fp@858: #ifdef SDO_ACCESS
fp@858: void read_sdo(void)
fp@858: {
fp@858:     switch (ecrt_sdo_request_state(sdo)) {
fp@861:         case EC_SDO_REQUEST_UNUSED: // request was not used yet
fp@867:             ecrt_sdo_request_read(sdo); // trigger first read
fp@858:             break;
fp@861:         case EC_SDO_REQUEST_BUSY:
fp@861:             printk(KERN_INFO PFX "Still busy...\n");
fp@861:             break;
fp@861:         case EC_SDO_REQUEST_SUCCESS:
fp@861:             printk(KERN_INFO PFX "Sdo value: 0x%04X\n",
fp@861:                     EC_READ_U16(ecrt_sdo_request_data(sdo)));
fp@867:             ecrt_sdo_request_read(sdo); // trigger next read
fp@861:             break;
fp@861:         case EC_SDO_REQUEST_ERROR:
fp@858:             printk(KERN_INFO PFX "Failed to read Sdo!\n");
fp@867:             ecrt_sdo_request_read(sdo); // retry reading
fp@858:             break;
fp@858:     }
fp@858: }
fp@858: #endif
fp@858: 
fp@858: /*****************************************************************************/
fp@858: 
fp@820: void cyclic_task(unsigned long data)
fp@820: {
fp@818:     // receive process data
fp@220:     spin_lock(&master_lock);
fp@325:     ecrt_master_receive(master);
fp@220:     ecrt_domain_process(domain1);
fp@512:     spin_unlock(&master_lock);
fp@220: 
fp@818:     // check process data state (optional)
fp@818:     check_domain1_state();
fp@512: 
fp@512:     if (counter) {
fp@512:         counter--;
fp@858:     } else { // do this at 1 Hz
fp@512:         counter = FREQUENCY;
fp@818: 
fp@818:         // calculate new process data
fp@512:         blink = !blink;
fp@612: 
fp@818:         // check for master state (optional)
fp@818:         check_master_state();
fp@1022: 
fp@1022:         // check for islave configuration state(s) (optional)
fp@1022:         check_slave_config_states();
fp@858:         
fp@858: #ifdef SDO_ACCESS
fp@858:         // read process data Sdo
fp@858:         read_sdo();
fp@858: #endif
fp@818:     }
fp@818: 
fp@818:     // write process data
fp@846:     EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09);
fp@818: 
fp@818:     // send process data
fp@512:     spin_lock(&master_lock);
fp@509:     ecrt_domain_queue(domain1);
fp@325:     ecrt_master_send(master);
fp@220:     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@516:     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@516:     spin_unlock(&master_lock);
fp@220: }
fp@220: 
fp@220: /*****************************************************************************/
fp@220: 
fp@220: int __init init_mini_module(void)
fp@220: {
fp@879: #ifdef CONFIGURE_PDOS
fp@792:     ec_slave_config_t *sc;
fp@792: #endif
fp@809: #ifdef EXTERNAL_MEMORY
fp@809:     unsigned int size;
fp@809: #endif
fp@792:     
fp@612:     printk(KERN_INFO PFX "Starting...\n");
fp@220: 
fp@509:     if (!(master = ecrt_request_master(0))) {
fp@612:         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@612:     printk(KERN_INFO PFX "Registering domain...\n");
fp@509:     if (!(domain1 = ecrt_master_create_domain(master))) {
fp@612:         printk(KERN_ERR PFX "Domain creation failed!\n");
fp@612:         goto out_release_master;
fp@612:     }
fp@612: 
fp@1022:     if (!(sc_ana_in = ecrt_master_slave_config(
fp@1026:                     master, AnaInPos, Beckhoff_EL3162))) {
fp@1022:         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
fp@1022:         goto out_release_master;
fp@1022:     }
fp@1022: 
fp@879: #ifdef CONFIGURE_PDOS
fp@879:     printk(KERN_INFO PFX "Configuring Pdos...\n");
fp@1022:     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3162_pdos)) {
fp@879:         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
fp@809:         goto out_release_master;
fp@809:     }
fp@809: 
fp@1026:     if (!(sc = ecrt_master_slave_config(master, DigOutPos, Beckhoff_EL2004))) {
fp@809:         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
fp@809:         goto out_release_master;
fp@809:     }
fp@809: 
fp@1026:     if (ecrt_slave_config_pdos(sc, EC_END, el2004_pdos)) {
fp@879:         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
fp@792:         goto out_release_master;
fp@792:     }
fp@792: #endif
fp@792: 
fp@858: #ifdef SDO_ACCESS
fp@858:     printk(KERN_INFO PFX "Creating Sdo requests...\n");
fp@1026:     if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) {
fp@858:         printk(KERN_ERR PFX "Failed to create Sdo request.\n");
fp@858:         goto out_release_master;
fp@858:     }
fp@1026:     ecrt_sdo_request_timeout(sdo, 500); // ms
fp@858: #endif
fp@858: 
fp@814:     printk(KERN_INFO PFX "Registering Pdo entries...\n");
fp@792:     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
fp@814:         printk(KERN_ERR PFX "Pdo entry registration failed!\n");
fp@792:         goto out_release_master;
fp@792:     }
fp@635: 
fp@809: #ifdef EXTERNAL_MEMORY
fp@809:     if ((size = ecrt_domain_size(domain1))) {
fp@820:         if (!(domain1_pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
fp@809:             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"
fp@809:                     " memory!\n", size);
fp@809:             goto out_release_master;
fp@809:         }
fp@820:         ecrt_domain_external_memory(domain1, domain1_pd);
fp@809:     }
fp@809: #endif
fp@809: 
fp@612:     printk(KERN_INFO PFX "Activating master...\n");
fp@220:     if (ecrt_master_activate(master)) {
fp@612:         printk(KERN_ERR PFX "Failed to activate master!\n");
fp@809: #ifdef EXTERNAL_MEMORY
fp@809:         goto out_free_process_data;
fp@809: #else
fp@809:         goto out_release_master;
fp@809: #endif
fp@809:     }
fp@809: 
fp@809: #ifndef EXTERNAL_MEMORY
fp@809:     // Get internal process data for domain
fp@820:     domain1_pd = ecrt_domain_data(domain1);
fp@809: #endif
fp@612: 
fp@612:     printk(KERN_INFO PFX "Starting cyclic sample thread.\n");
fp@220:     init_timer(&timer);
fp@820:     timer.function = cyclic_task;
fp@220:     timer.expires = jiffies + 10;
fp@220:     add_timer(&timer);
fp@220: 
fp@612:     printk(KERN_INFO PFX "Started.\n");
fp@220:     return 0;
fp@220: 
fp@809: #ifdef EXTERNAL_MEMORY
fp@809: out_free_process_data:
fp@820:     kfree(domain1_pd);
fp@809: #endif
fp@809: out_release_master:
fp@654:     printk(KERN_ERR PFX "Releasing master...\n");
fp@220:     ecrt_release_master(master);
fp@809: out_return:
fp@654:     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@612:     printk(KERN_INFO PFX "Stopping...\n");
fp@220: 
fp@449:     del_timer_sync(&timer);
fp@809: 
fp@809: #ifdef EXTERNAL_MEMORY
fp@820:     kfree(domain1_pd);
fp@809: #endif
fp@809: 
fp@612:     printk(KERN_INFO PFX "Releasing master...\n");
fp@449:     ecrt_release_master(master);
fp@220: 
fp@654:     printk(KERN_INFO PFX "Unloading.\n");
fp@220: }
fp@220: 
fp@220: /*****************************************************************************/
fp@220: 
fp@220: MODULE_LICENSE("GPL");
fp@512: MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
fp@512: 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: /*****************************************************************************/