examples/msr/msr_sample.c
author Florian Pose <fp@igh-essen.com>
Tue, 17 Oct 2006 07:25:13 +0000
changeset 399 40375a50be4e
parent 325 7833cf70c4f2
child 449 3caf8ff4d8a2
child 1732 1cc865ba17c2
permissions -rw-r--r--
Removed varsized_fields
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  Sample module for use with IgH MSR library.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  $Id$
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  This file is part of the IgH EtherCAT Master.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  The IgH EtherCAT Master is free software; you can redistribute it
f6594a07cdda MSR example finished.
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: 240
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: 240
diff changeset
    14
 *  License, or (at your option) any later version.
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  The IgH EtherCAT Master is distributed in the hope that it will be
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  GNU General Public License for more details.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  You should have received a copy of the GNU General Public License
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
f6594a07cdda MSR example finished.
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: 240
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: 240
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: 240
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: 240
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: 240
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: 240
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: 240
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: 240
diff changeset
    32
 *  Technology, IP and trade marks.
0bf7c769de06 Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents: 240
diff changeset
    33
 *
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
 *****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
// Linux
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <linux/module.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
#include "rtai_sched.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "rtai_sem.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
// RT_lib
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include <msr_main.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
#include <msr_reg.h>
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    46
#include <msr_time.h>
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
#include "msr_param.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
#include "../../include/ecrt.h"
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    51
#include "../../include/ecdb.h"
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
RT_TASK task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
SEM master_sem;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
cycles_t t_start = 0, t_critical;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
ec_master_t *master = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
ec_domain_t *domain1 = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    67
// raw process data
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    68
void *r_ana_out;
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
// Channels
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    71
double k_ana_out;
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    72
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    73
ec_pdo_reg_t domain1_pdos[] = {
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    74
    {"3", Beckhoff_EL4132_Output1, &r_ana_out},
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
    {}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
};
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    80
void msr_controller_run(void)
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    81
{
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    82
    rt_sem_wait(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    83
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    84
    // receive
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    85
    ecrt_master_receive(master);
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    86
    ecrt_domain_process(domain1);
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    87
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    88
    // Process data
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    89
    EC_WRITE_S16(r_ana_out, k_ana_out / 10.0 * 0x7FFF);
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    90
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    91
    // Send
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    92
    ecrt_master_run(master);
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    93
    ecrt_master_send(master);
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    94
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    95
    rt_sem_signal(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    96
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    97
    msr_write_kanal_list();
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    98
}
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    99
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   100
/*****************************************************************************/
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   101
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
void msr_run(long data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
{
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   104
    while (1) {
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
        t_start = get_cycles();
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   106
        MSR_RTAITHREAD_CODE(msr_controller_run(););
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
        rt_task_wait_period();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
int msr_reg(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
{
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   115
    msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL);
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
int request_lock(void *data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    // too close to the next RT cycle: deny access...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    if (get_cycles() - t_start > t_critical) return -1;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    // allow access
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    rt_sem_wait(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
void release_lock(void *data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
    rt_sem_signal(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
int __init init_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
    RTIME ticks;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
    rt_sem_init(&master_sem, 1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
    if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
        printk(KERN_ERR "Failed to initialize rtlib!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
        goto out_return;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   154
    if (!(master = ecrt_request_master(0))) {
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
        printk(KERN_ERR "Failed to request master 0!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
        goto out_msr_cleanup;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
    printk(KERN_INFO "Creating domains...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
    if (!(domain1 = ecrt_master_create_domain(master))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
        printk(KERN_ERR "Failed to create domains!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   167
    printk(KERN_INFO "Registering PDOs...\n");
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   168
    if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) {
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   169
        printk(KERN_ERR "Failed to register PDOs.\n");
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
    printk(KERN_INFO "Activating master...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    if (ecrt_master_activate(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        printk(KERN_ERR "Could not activate master!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   179
    ecrt_master_prepare(master);
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
    printk("Starting cyclic sample thread...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    ticks = start_rt_timer(nano2count(TIMERTICKS));
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
    if (rt_task_init(&task, msr_run, 0, 2000, 0, 1, NULL)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        printk(KERN_ERR "Failed to init RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
        goto out_stop_timer;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
    if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
        printk(KERN_ERR "Failed to run RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
        goto out_stop_task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
 out_stop_task:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
 out_stop_timer:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
 out_release_master:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
 out_msr_cleanup:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
 out_return:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
    return -1;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
void __exit cleanup_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
    printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
MODULE_LICENSE("GPL");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
module_init(init_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
module_exit(cleanup_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
/*****************************************************************************/