examples/msr/msr_sample.c
author Florian Pose <fp@igh-essen.com>
Wed, 19 Jul 2006 13:15:02 +0000
changeset 306 45886de3db87
parent 246 0bf7c769de06
child 325 7833cf70c4f2
permissions -rw-r--r--
Renamed FREERUN mode to IDLE mode.
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"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
#define ASYNC
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
RT_TASK task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
SEM master_sem;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
cycles_t t_start = 0, t_critical;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
ec_master_t *master = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
ec_domain_t *domain1 = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    68
// raw process data
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
void *r_ssi;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void *r_ssi_st;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
// Channels
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
uint32_t k_ssi;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
uint32_t k_ssi_st;
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
ec_field_init_t domain1_fields[] = {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    {&r_ssi,    "0:3", "Beckhoff", "EL5001", "InputValue", 0},
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
    {&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status",     0},
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
    {}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
};
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    84
void msr_controller_run(void)
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    85
{
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    86
    rt_sem_wait(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    87
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    88
#ifdef ASYNC
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    89
    // Empfangen
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    90
    ecrt_master_async_receive(master);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    91
    ecrt_domain_process(domain1);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    92
#else
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    93
    // Senden und empfangen
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    94
    ecrt_domain_queue(domain1);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    95
    ecrt_master_run(master);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    96
    ecrt_master_sync_io(master);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    97
    ecrt_domain_process(domain1);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
    98
#endif
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
    // Prozessdaten verarbeiten
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   101
    k_ssi    = EC_READ_U32(r_ssi);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   102
    k_ssi_st = EC_READ_U8 (r_ssi_st);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   103
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   104
#ifdef ASYNC
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   105
    // Senden
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   106
    ecrt_domain_queue(domain1);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   107
    ecrt_master_run(master);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   108
    ecrt_master_async_send(master);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   109
#endif
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   110
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   111
    rt_sem_signal(&master_sem);
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   112
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   113
    msr_write_kanal_list();
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   114
}
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   115
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   116
/*****************************************************************************/
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   117
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
void msr_run(long data)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
{
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   120
    while (1) {
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
        t_start = get_cycles();
234
fd04cfde1c4b Corrected MSR example.
Florian Pose <fp@igh-essen.com>
parents: 232
diff changeset
   122
        MSR_RTAITHREAD_CODE(msr_controller_run(););
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
        rt_task_wait_period();
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
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
/*****************************************************************************/
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
int msr_reg(void)
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
    msr_reg_kanal("/ssi_position", "", &k_ssi,    TUINT);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    msr_reg_kanal("/ssi_status",   "", &k_ssi_st, TUINT);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    return 0;
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
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
int request_lock(void *data)
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
    // too close to the next RT cycle: deny access...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
    if (get_cycles() - t_start > t_critical) return -1;
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
    // allow access
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    rt_sem_wait(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
}
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
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
void release_lock(void *data)
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
    rt_sem_signal(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
/*****************************************************************************/
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
int __init init_mod(void)
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
    RTIME ticks;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
    ec_slave_t *slave;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
    printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
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
    rt_sem_init(&master_sem, 1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
    t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
        printk(KERN_ERR "Failed to initialize rtlib!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
        goto out_return;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    if ((master = ecrt_request_master(0)) == NULL) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        printk(KERN_ERR "Failed to request master 0!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        goto out_msr_cleanup;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);
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(KERN_INFO "Creating domains...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
    if (!(domain1 = ecrt_master_create_domain(master))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        printk(KERN_ERR "Failed to create domains!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        goto out_release_master;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
    printk(KERN_INFO "Registering domain fields...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
        printk(KERN_ERR "Failed to register domain fields.\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
        goto out_release_master;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    printk(KERN_INFO "Activating master...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
    if (ecrt_master_activate(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
        printk(KERN_ERR "Could not activate master!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    if (ecrt_master_fetch_sdo_lists(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
        printk(KERN_ERR "Failed to fetch SDO lists!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    ecrt_master_print(master, 2);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
#else
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
    ecrt_master_print(master, 0);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
#endif
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
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
        printk(KERN_ERR "Failed to get slave!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
        goto out_deactivate;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
    if (
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  1) || // disable frame error bit
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  0) || // power failure bit
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
240
e55b0572933c SSI sensor changes in MSR example
Florian Pose <fp@igh-essen.com>
parents: 234
diff changeset
   220
        ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  0) || // graycode
232
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
        ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
        ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
        ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
        ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
240
e55b0572933c SSI sensor changes in MSR example
Florian Pose <fp@igh-essen.com>
parents: 234
diff changeset
   225
        ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 50) // inhibit time in us
232
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
        printk(KERN_ERR "Failed to configure SSI slave!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
#endif
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
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
    if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
        printk(KERN_ERR "Failed to get slave!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
    if (ecrt_slave_write_alias(slave, 0x5678)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
        printk(KERN_ERR "Failed to write alias!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
#ifdef ASYNC
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
    // Einmal senden und warten...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
    ecrt_master_prepare_async_io(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    if (ecrt_master_start_eoe(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
        printk(KERN_ERR "Failed to start EoE processing!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
    printk("Starting cyclic sample thread...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
    ticks = start_rt_timer(nano2count(TIMERTICKS));
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    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
   256
        printk(KERN_ERR "Failed to init RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
        goto out_stop_timer;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
    if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
        printk(KERN_ERR "Failed to run RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
        goto out_stop_task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
 out_stop_task:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
 out_stop_timer:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
 out_deactivate:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
 out_release_master:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
 out_msr_cleanup:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
 out_return:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
    return -1;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
void __exit cleanup_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
    printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
}
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
MODULE_LICENSE("GPL");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
module_init(init_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
module_exit(cleanup_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
/*****************************************************************************/