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