examples/msr/msr_sample.c
author Florian Pose <fp@igh-essen.com>
Thu, 02 Nov 2006 14:20:24 +0000
changeset 472 bd2e0f220289
parent 452 d8ae13a5a910
child 509 87baea61d0c7
permissions -rw-r--r--
Mini example with KBUS again.
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
 *  $Id$
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
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
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
 *  This file is part of the IgH EtherCAT Master.
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
 *  The IgH EtherCAT Master is free software; you can redistribute it
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  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
    11
 *  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
    12
 *  License, or (at your option) any later version.
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  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
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  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
    20
 *  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
    21
 *  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
    22
 *
246
0bf7c769de06 Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents: 240
diff changeset
    23
 *  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
    24
 *  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
    25
 *  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
    26
 *  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
    27
 *  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
    28
 *  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
    29
 *  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
    30
 *  Technology, IP and trade marks.
0bf7c769de06 Changed license headers and added EtherCAT license notice.
Florian Pose <fp@igh-essen.com>
parents: 240
diff changeset
    31
 *
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
// Linux
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <linux/module.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include "rtai_sched.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
#include "rtai_sem.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
// RT_lib
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include <msr_main.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include <msr_reg.h>
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    44
#include <msr_time.h>
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
#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
    48
#include "../../include/ecdb.h"
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
451
cd6b819c2351 MSR example: Replaced msr_load and msr_unload with init script, removed msr_param.h
Florian Pose <fp@igh-essen.com>
parents: 449
diff changeset
    50
#define MSR_ABTASTFREQUENZ 1000
cd6b819c2351 MSR example: Replaced msr_load and msr_unload with init script, removed msr_param.h
Florian Pose <fp@igh-essen.com>
parents: 449
diff changeset
    51
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
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
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
RT_TASK task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
SEM master_sem;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
cycles_t t_start = 0, t_critical;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
ec_master_t *master = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
ec_domain_t *domain1 = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    66
// raw process data
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    67
void *r_ana_out;
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
// Channels
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    70
double k_ana_out;
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    71
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    72
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
    73
    {"3", Beckhoff_EL4132_Output1, &r_ana_out},
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
    {}
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
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    79
void msr_controller_run(void)
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    80
{
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    81
    rt_sem_wait(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    82
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    83
    // receive
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    84
    ecrt_master_receive(master);
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    85
    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
    86
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    87
    // Process data
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    88
    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
    89
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
    90
    // Send
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    91
    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
    92
    ecrt_master_send(master);
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    93
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    94
    rt_sem_signal(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    95
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    96
    msr_write_kanal_list();
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    97
}
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
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
void msr_run(long data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
{
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   103
    while (1) {
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
        t_start = get_cycles();
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   105
        MSR_RTAITHREAD_CODE(msr_controller_run(););
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        rt_task_wait_period();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    }
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
int msr_reg(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
{
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   114
    msr_reg_kanal("/ana_out", "", &k_ana_out, TDBL);
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
}
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
int request_lock(void *data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
    // too close to the next RT cycle: deny access...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    if (get_cycles() - t_start > t_critical) return -1;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
    // allow access
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    rt_sem_wait(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
}
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
void release_lock(void *data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    rt_sem_signal(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
}
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
int __init init_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
    RTIME ticks;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
    printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
    rt_sem_init(&master_sem, 1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
    t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
        printk(KERN_ERR "Failed to initialize rtlib!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
        goto out_return;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   153
    if (!(master = ecrt_request_master(0))) {
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
        printk(KERN_ERR "Failed to request master 0!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
        goto out_msr_cleanup;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
    }
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
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
    printk(KERN_INFO "Creating domains...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
    if (!(domain1 = ecrt_master_create_domain(master))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
        printk(KERN_ERR "Failed to create domains!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   166
    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
   167
    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
   168
        printk(KERN_ERR "Failed to register PDOs.\n");
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    }
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
    printk(KERN_INFO "Activating master...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
    if (ecrt_master_activate(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
        printk(KERN_ERR "Could not activate master!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
325
7833cf70c4f2 VERSION 1.1: New realtime interface, only state machines.
Florian Pose <fp@igh-essen.com>
parents: 246
diff changeset
   178
    ecrt_master_prepare(master);
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
    printk("Starting cyclic sample thread...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
    ticks = start_rt_timer(nano2count(TIMERTICKS));
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    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
   183
        printk(KERN_ERR "Failed to init RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        goto out_stop_timer;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
    if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
        printk(KERN_ERR "Failed to run RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
        goto out_stop_task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
    }
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
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
 out_stop_task:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
 out_stop_timer:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
 out_release_master:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
 out_msr_cleanup:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
 out_return:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    return -1;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
}
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
void __exit cleanup_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
    printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n");
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
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
MODULE_LICENSE("GPL");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
module_init(init_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
module_exit(cleanup_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
/*****************************************************************************/