examples/rtai/rtai_sample.c
branchstable-1.0
changeset 1619 0d4119024f55
equal deleted inserted replaced
1618:5cff10efb927 1619:0d4119024f55
       
     1 /******************************************************************************
       
     2  *
       
     3  *  RTAI sample for the IgH EtherCAT master.
       
     4  *
       
     5  *  $Id$
       
     6  *
       
     7  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
     8  *
       
     9  *  This file is part of the IgH EtherCAT Master.
       
    10  *
       
    11  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    12  *  and/or modify it under the terms of the GNU General Public License
       
    13  *  as published by the Free Software Foundation; either version 2 of the
       
    14  *  License, or (at your option) any later version.
       
    15  *
       
    16  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    17  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19  *  GNU General Public License for more details.
       
    20  *
       
    21  *  You should have received a copy of the GNU General Public License
       
    22  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    24  *
       
    25  *  The right to use EtherCAT Technology is granted and comes free of
       
    26  *  charge under condition of compatibility of product made by
       
    27  *  Licensee. People intending to distribute/sell products based on the
       
    28  *  code, have to sign an agreement to guarantee that products using
       
    29  *  software based on IgH EtherCAT master stay compatible with the actual
       
    30  *  EtherCAT specification (which are released themselves as an open
       
    31  *  standard) as the (only) precondition to have the right to use EtherCAT
       
    32  *  Technology, IP and trade marks.
       
    33  *
       
    34  *****************************************************************************/
       
    35 
       
    36 // Linux
       
    37 #include <linux/module.h>
       
    38 
       
    39 // RTAI
       
    40 #include "rtai_sched.h"
       
    41 #include "rtai_sem.h"
       
    42 
       
    43 // EtherCAT
       
    44 #include "../../include/ecrt.h"
       
    45 
       
    46 /*****************************************************************************/
       
    47 
       
    48 MODULE_LICENSE("GPL");
       
    49 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
       
    50 MODULE_DESCRIPTION("EtherCAT RTAI sample module");
       
    51 
       
    52 /*****************************************************************************/
       
    53 
       
    54 // comment this for synchronous IO
       
    55 #define ASYNC
       
    56 
       
    57 // RTAI task frequency in Hz
       
    58 #define FREQUENCY 10000
       
    59 
       
    60 #define TIMERTICKS (1000000000 / FREQUENCY)
       
    61 
       
    62 /*****************************************************************************/
       
    63 
       
    64 // RTAI
       
    65 RT_TASK task;
       
    66 SEM master_sem;
       
    67 cycles_t t_last_start = 0;
       
    68 cycles_t t_critical;
       
    69 
       
    70 // EtherCAT
       
    71 ec_master_t *master = NULL;
       
    72 ec_domain_t *domain1 = NULL;
       
    73 
       
    74 // data fields
       
    75 void *r_ssi_input;
       
    76 
       
    77 // channels
       
    78 uint32_t k_pos;
       
    79 
       
    80 ec_field_init_t domain1_fields[] = {
       
    81     {&r_ssi_input, "3", "Beckhoff", "EL5001", "InputValue",   0},
       
    82     {NULL,         "2", "Beckhoff", "EL4132", "OutputValue",  0},
       
    83     {}
       
    84 };
       
    85 
       
    86 /*****************************************************************************/
       
    87 
       
    88 void run(long data)
       
    89 {
       
    90     while (1)
       
    91     {
       
    92         t_last_start = get_cycles();
       
    93         rt_sem_wait(&master_sem);
       
    94 
       
    95 #ifdef ASYNC
       
    96         // receive
       
    97         ecrt_master_async_receive(master);
       
    98         ecrt_domain_process(domain1);
       
    99 #else
       
   100         // send and receive
       
   101         ecrt_domain_queue(domain1);
       
   102         ecrt_master_run(master);
       
   103         ecrt_master_sync_io(master);
       
   104         ecrt_domain_process(domain1);
       
   105 #endif
       
   106 
       
   107         // process data
       
   108         k_pos = EC_READ_U32(r_ssi_input);
       
   109 
       
   110 #ifdef ASYNC
       
   111         // send
       
   112         ecrt_domain_queue(domain1);
       
   113         ecrt_master_run(master);
       
   114         ecrt_master_async_send(master);
       
   115 #endif
       
   116 
       
   117         rt_sem_signal(&master_sem);
       
   118         rt_task_wait_period();
       
   119     }
       
   120 }
       
   121 
       
   122 /*****************************************************************************/
       
   123 
       
   124 int request_lock(void *data)
       
   125 {
       
   126     // too close to the next RT cycle: deny access...
       
   127     if (get_cycles() - t_last_start > t_critical) return -1;
       
   128 
       
   129     // allow access
       
   130     rt_sem_wait(&master_sem);
       
   131     return 0;
       
   132 }
       
   133 
       
   134 /*****************************************************************************/
       
   135 
       
   136 void release_lock(void *data)
       
   137 {
       
   138     rt_sem_signal(&master_sem);
       
   139 }
       
   140 
       
   141 /*****************************************************************************/
       
   142 
       
   143 int __init init_mod(void)
       
   144 {
       
   145     RTIME tick_period, requested_ticks, now;
       
   146 
       
   147     printk(KERN_INFO "=== Starting EtherCAT RTAI sample module... ===\n");
       
   148 
       
   149     rt_sem_init(&master_sem, 1);
       
   150 
       
   151     t_critical = cpu_khz * 800 / FREQUENCY; // ticks for 80%
       
   152 
       
   153     if ((master = ecrt_request_master(0)) == NULL) {
       
   154         printk(KERN_ERR "Requesting master 0 failed!\n");
       
   155         goto out_return;
       
   156     }
       
   157 
       
   158     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
       
   159 
       
   160     printk(KERN_INFO "Registering domain...\n");
       
   161     if (!(domain1 = ecrt_master_create_domain(master)))
       
   162     {
       
   163         printk(KERN_ERR "Domain creation failed!\n");
       
   164         goto out_release_master;
       
   165     }
       
   166 
       
   167     printk(KERN_INFO "Registering domain fields...\n");
       
   168     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
       
   169         printk(KERN_ERR "Field registration failed!\n");
       
   170         goto out_release_master;
       
   171     }
       
   172 
       
   173     printk(KERN_INFO "Activating master...\n");
       
   174     if (ecrt_master_activate(master)) {
       
   175         printk(KERN_ERR "Failed to activate master!\n");
       
   176         goto out_release_master;
       
   177     }
       
   178 
       
   179 #if 0
       
   180     if (ecrt_master_fetch_sdo_lists(master)) {
       
   181         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   182         goto out_deactivate;
       
   183     }
       
   184     ecrt_master_print(master, 2);
       
   185 #else
       
   186     ecrt_master_print(master, 0);
       
   187 #endif
       
   188 
       
   189 #if 0
       
   190     if (!(slave = ecrt_master_get_slave(master, "5"))) {
       
   191         printk(KERN_ERR "Failed to get slave 5!\n");
       
   192         goto out_deactivate;
       
   193     }
       
   194 
       
   195     if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  0) ||
       
   196         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  1) ||
       
   197         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) ||
       
   198         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  0) ||
       
   199         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  4) ||
       
   200         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) ||
       
   201         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) ||
       
   202         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) ||
       
   203         ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) {
       
   204         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   205         goto out_deactivate;
       
   206     }
       
   207 #endif
       
   208 
       
   209 #if 0
       
   210     printk(KERN_INFO "Writing alias...\n");
       
   211     if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) {
       
   212         printk(KERN_ERR "Failed to write alias!\n");
       
   213         goto out_deactivate;
       
   214     }
       
   215 #endif
       
   216 
       
   217 #ifdef ASYNC
       
   218     // send once and wait...
       
   219     ecrt_master_prepare_async_io(master);
       
   220 #endif
       
   221 
       
   222     if (ecrt_master_start_eoe(master)) {
       
   223         printk(KERN_ERR "Failed to start EoE processing!\n");
       
   224         goto out_deactivate;
       
   225     }
       
   226 
       
   227     printk("Starting cyclic sample thread...\n");
       
   228     requested_ticks = nano2count(TIMERTICKS);
       
   229     tick_period = start_rt_timer(requested_ticks);
       
   230     printk(KERN_INFO "RT timer started with %i/%i ticks.\n",
       
   231            (int) tick_period, (int) requested_ticks);
       
   232 
       
   233     if (rt_task_init(&task, run, 0, 2000, 0, 1, NULL)) {
       
   234         printk(KERN_ERR "Failed to init RTAI task!\n");
       
   235         goto out_stop_timer;
       
   236     }
       
   237 
       
   238     now = rt_get_time();
       
   239     if (rt_task_make_periodic(&task, now + tick_period, tick_period)) {
       
   240         printk(KERN_ERR "Failed to run RTAI task!\n");
       
   241         goto out_stop_task;
       
   242     }
       
   243 
       
   244     printk(KERN_INFO "=== EtherCAT RTAI sample module started. ===\n");
       
   245     return 0;
       
   246 
       
   247  out_stop_task:
       
   248     rt_task_delete(&task);
       
   249  out_stop_timer:
       
   250     stop_rt_timer();
       
   251  out_deactivate:
       
   252     ecrt_master_deactivate(master);
       
   253  out_release_master:
       
   254     ecrt_release_master(master);
       
   255  out_return:
       
   256     rt_sem_delete(&master_sem);
       
   257     return -1;
       
   258 }
       
   259 
       
   260 /*****************************************************************************/
       
   261 
       
   262 void __exit cleanup_mod(void)
       
   263 {
       
   264     printk(KERN_INFO "=== Stopping EtherCAT RTAI sample module... ===\n");
       
   265 
       
   266     rt_task_delete(&task);
       
   267     stop_rt_timer();
       
   268     ecrt_master_deactivate(master);
       
   269     ecrt_release_master(master);
       
   270     rt_sem_delete(&master_sem);
       
   271 
       
   272     printk(KERN_INFO "=== EtherCAT RTAI sample module stopped. ===\n");
       
   273 }
       
   274 
       
   275 /*****************************************************************************/
       
   276 
       
   277 module_init(init_mod);
       
   278 module_exit(cleanup_mod);
       
   279 
       
   280 /*****************************************************************************/
       
   281