mini/mini.c
branchstable-1.0
changeset 1619 0d4119024f55
parent 1618 5cff10efb927
child 1620 9d7453c16ade
equal deleted inserted replaced
1618:5cff10efb927 1619:0d4119024f55
     1 /******************************************************************************
       
     2  *
       
     3  *  m i n i . c
       
     4  *
       
     5  *  Minimal module for EtherCAT.
       
     6  *
       
     7  *  $Id$
       
     8  *
       
     9  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    10  *
       
    11  *  This file is part of the IgH EtherCAT Master.
       
    12  *
       
    13  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    14  *  and/or modify it under the terms of the GNU General Public License
       
    15  *  as published by the Free Software Foundation; version 2 of the License.
       
    16  *
       
    17  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    18  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    20  *  GNU General Public License for more details.
       
    21  *
       
    22  *  You should have received a copy of the GNU General Public License
       
    23  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    24  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    25  *
       
    26  *****************************************************************************/
       
    27 
       
    28 #include <linux/module.h>
       
    29 #include <linux/delay.h>
       
    30 #include <linux/timer.h>
       
    31 
       
    32 #include "../include/ecrt.h" // EtherCAT realtime interface
       
    33 
       
    34 #define ASYNC
       
    35 #define FREQUENCY 100
       
    36 
       
    37 /*****************************************************************************/
       
    38 
       
    39 struct timer_list timer;
       
    40 
       
    41 // EtherCAT
       
    42 ec_master_t *master = NULL;
       
    43 ec_domain_t *domain1 = NULL;
       
    44 
       
    45 // data fields
       
    46 //void *r_ssi_input, *r_ssi_status, *r_4102[3];
       
    47 
       
    48 // channels
       
    49 uint32_t k_pos;
       
    50 uint8_t k_stat;
       
    51 
       
    52 ec_field_init_t domain1_fields[] = {
       
    53     {NULL, "1", "Beckhoff", "EL5001", "InputValue",   0},
       
    54     {NULL, "2", "Beckhoff", "EL4132", "OutputValue",  0},
       
    55     {}
       
    56 };
       
    57 
       
    58 /*****************************************************************************/
       
    59 
       
    60 void run(unsigned long data)
       
    61 {
       
    62     static unsigned int counter = 0;
       
    63 
       
    64 #ifdef ASYNC
       
    65     // receive
       
    66     ecrt_master_async_receive(master);
       
    67     ecrt_domain_process(domain1);
       
    68 #else
       
    69     // send and receive
       
    70     ecrt_domain_queue(domain1);
       
    71     ecrt_master_run(master);
       
    72     ecrt_master_sync_io(master);
       
    73     ecrt_domain_process(domain1);
       
    74 #endif
       
    75 
       
    76     // process data
       
    77     //k_pos = EC_READ_U32(r_ssi);
       
    78 
       
    79 #ifdef ASYNC
       
    80     // send
       
    81     ecrt_domain_queue(domain1);
       
    82     ecrt_master_run(master);
       
    83     ecrt_master_async_send(master);
       
    84 #endif
       
    85 
       
    86     if (counter) {
       
    87         counter--;
       
    88     }
       
    89     else {
       
    90         counter = FREQUENCY;
       
    91         //printk(KERN_INFO "k_pos    = %i\n", k_pos);
       
    92         //printk(KERN_INFO "k_stat   = 0x%02X\n", k_stat);
       
    93     }
       
    94 
       
    95     // restart timer
       
    96     timer.expires += HZ / FREQUENCY;
       
    97     add_timer(&timer);
       
    98 }
       
    99 
       
   100 /*****************************************************************************/
       
   101 
       
   102 int __init init_mini_module(void)
       
   103 {
       
   104     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
       
   105 
       
   106     if ((master = ecrt_request_master(0)) == NULL) {
       
   107         printk(KERN_ERR "Requesting master 0 failed!\n");
       
   108         goto out_return;
       
   109     }
       
   110 
       
   111     printk(KERN_INFO "Registering domain...\n");
       
   112     if (!(domain1 = ecrt_master_create_domain(master)))
       
   113     {
       
   114         printk(KERN_ERR "Domain creation failed!\n");
       
   115         goto out_release_master;
       
   116     }
       
   117 
       
   118     printk(KERN_INFO "Registering domain fields...\n");
       
   119     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
       
   120         printk(KERN_ERR "Field registration failed!\n");
       
   121         goto out_release_master;
       
   122     }
       
   123 
       
   124     printk(KERN_INFO "Activating master...\n");
       
   125     if (ecrt_master_activate(master)) {
       
   126         printk(KERN_ERR "Failed to activate master!\n");
       
   127         goto out_release_master;
       
   128     }
       
   129 
       
   130 #if 0
       
   131     if (ecrt_master_fetch_sdo_lists(master)) {
       
   132         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   133         goto out_deactivate;
       
   134     }
       
   135     ecrt_master_print(master, 2);
       
   136 #else
       
   137     ecrt_master_print(master, 0);
       
   138 #endif
       
   139 
       
   140 
       
   141 #if 0
       
   142     if (!(slave = ecrt_master_get_slave(master, "5"))) {
       
   143         printk(KERN_ERR "Failed to get slave 5!\n");
       
   144         goto out_deactivate;
       
   145     }
       
   146 
       
   147     if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  0) ||
       
   148         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  1) ||
       
   149         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) ||
       
   150         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  0) ||
       
   151         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  4) ||
       
   152         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) ||
       
   153         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) ||
       
   154         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) ||
       
   155         ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) {
       
   156         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   157         goto out_deactivate;
       
   158     }
       
   159 #endif
       
   160 
       
   161 #if 0
       
   162     printk(KERN_INFO "Writing alias...\n");
       
   163     if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) {
       
   164         printk(KERN_ERR "Failed to write alias!\n");
       
   165         goto out_deactivate;
       
   166     }
       
   167 #endif
       
   168 
       
   169 #ifdef ASYNC
       
   170     // send once and wait...
       
   171     ecrt_master_prepare_async_io(master);
       
   172 #endif
       
   173 
       
   174     printk("Starting cyclic sample thread.\n");
       
   175     init_timer(&timer);
       
   176     timer.function = run;
       
   177     timer.expires = jiffies + 10;
       
   178     add_timer(&timer);
       
   179 
       
   180     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
       
   181     return 0;
       
   182 
       
   183 #if 0
       
   184  out_deactivate:
       
   185     ecrt_master_deactivate(master);
       
   186 #endif
       
   187  out_release_master:
       
   188     ecrt_release_master(master);
       
   189  out_return:
       
   190     return -1;
       
   191 }
       
   192 
       
   193 /*****************************************************************************/
       
   194 
       
   195 void __exit cleanup_mini_module(void)
       
   196 {
       
   197     printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
       
   198 
       
   199     if (master) {
       
   200         del_timer_sync(&timer);
       
   201         printk(KERN_INFO "Deactivating master...\n");
       
   202         ecrt_master_deactivate(master);
       
   203         ecrt_release_master(master);
       
   204     }
       
   205 
       
   206     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
       
   207 }
       
   208 
       
   209 /*****************************************************************************/
       
   210 
       
   211 MODULE_LICENSE("GPL");
       
   212 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
       
   213 MODULE_DESCRIPTION ("EtherCAT minimal test environment");
       
   214 
       
   215 module_init(init_mini_module);
       
   216 module_exit(cleanup_mini_module);
       
   217 
       
   218 /*****************************************************************************/
       
   219