examples/msr/msr_sample.c
author Florian Pose <fp@igh-essen.com>
Fri, 12 May 2006 10:13:01 +0000
changeset 233 31d327ce38d3
parent 232 f6594a07cdda
child 234 fd04cfde1c4b
permissions -rw-r--r--
Smaller fixes on RTAI example.
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  as published by the Free Software Foundation; version 2 of the License.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  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
    16
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  GNU General Public License for more details.
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  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
    21
 *  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
    22
 *  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
    23
 *
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
// Linux
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
#include <linux/module.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#include "rtai_sched.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include "rtai_sem.h"
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
// RT_lib
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <msr_main.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <msr_reg.h>
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include "msr_param.h"
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
#include "../../include/ecrt.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
#define ASYNC
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
#define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#define TIMERTICKS (1000000000 / MSR_ABTASTFREQUENZ)
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
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
// RTAI
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
RT_TASK task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
SEM master_sem;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
cycles_t t_start = 0, t_critical;
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
// MSR
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
extern unsigned long msr_controller_execution_time;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
extern unsigned long msr_controller_call_time;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
extern wait_queue_head_t msr_read_waitqueue;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
int count_wakeup = 0;
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
// EtherCAT
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
ec_master_t *master = NULL;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
ec_domain_t *domain1 = NULL;
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
// Prozessdaten
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
void *r_ssi;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
void *r_ssi_st;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
// Channels
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
uint32_t k_ssi;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
uint32_t k_ssi_st;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
ec_field_init_t domain1_fields[] = {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
    {&r_ssi,    "0:3", "Beckhoff", "EL5001", "InputValue", 0},
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
    {&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status",     0},
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
void msr_run(long data)
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
    cycles_t t_last_start;
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
    while (1)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
        t_last_start = t_start;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
        t_start = get_cycles();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
        rt_sem_wait(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
#ifdef ASYNC
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
        // Empfangen
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
        ecrt_master_async_receive(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
        ecrt_domain_process(domain1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
#else
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
        // Senden und empfangen
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
        ecrt_domain_queue(domain1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
        ecrt_master_run(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
        ecrt_master_sync_io(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
        ecrt_domain_process(domain1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
        // Prozessdaten verarbeiten
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        k_ssi =    EC_READ_U32(r_ssi);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
        k_ssi_st = EC_READ_U8 (r_ssi_st);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
#ifdef ASYNC
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
        // Senden
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
        ecrt_domain_queue(domain1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        ecrt_master_run(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
        ecrt_master_async_send(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
#endif
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
        rt_sem_signal(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
        /* write data to MSR ring buffers */
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
        msr_write_kanal_list();
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
        /* wake up MSR read queue */
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
        if(++count_wakeup >= MSR_ABTASTFREQUENZ / 10) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
            wake_up_interruptible(&msr_read_waitqueue);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
            count_wakeup = 0;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
        /* calculate timing */
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
        msr_controller_execution_time =
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
            (unsigned long) (get_cycles() - t_start) * 1000 / cpu_khz;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
        msr_controller_call_time =
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
            (unsigned long) (t_start - t_last_start) * 1000 / cpu_khz;
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
        rt_task_wait_period();
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
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
int msr_reg(void)
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
    msr_reg_kanal("/ssi_position", "", &k_ssi,    TUINT);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    msr_reg_kanal("/ssi_status",   "", &k_ssi_st, TUINT);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
    return 0;
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
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
int request_lock(void *data)
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
    // too close to the next RT cycle: deny access...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    if (get_cycles() - t_start > t_critical) return -1;
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
    // allow access
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
    rt_sem_wait(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
    return 0;
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
void release_lock(void *data)
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
    rt_sem_signal(&master_sem);
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
/*****************************************************************************/
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
int __init init_mod(void)
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
    RTIME ticks;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
    ec_slave_t *slave;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
#endif
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
    printk(KERN_INFO "=== Starting EtherCAT RTAI MSR sample module... ===\n");
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
    rt_sem_init(&master_sem, 1);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    t_critical = cpu_khz * 800 / MSR_ABTASTFREQUENZ; // ticks for 80%
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
    if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_reg) < 0) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
        printk(KERN_ERR "Failed to initialize rtlib!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
        goto out_return;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
    if ((master = ecrt_request_master(0)) == NULL) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
        printk(KERN_ERR "Failed to request master 0!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        goto out_msr_cleanup;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
    }
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
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    printk(KERN_INFO "Creating domains...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
    if (!(domain1 = ecrt_master_create_domain(master))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
        printk(KERN_ERR "Failed to create domains!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
        goto out_release_master;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
    printk(KERN_INFO "Registering domain fields...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
    if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
        printk(KERN_ERR "Failed to register domain fields.\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
        goto out_release_master;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    printk(KERN_INFO "Activating master...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    if (ecrt_master_activate(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        printk(KERN_ERR "Could not activate master!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
        goto out_release_master;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
    if (ecrt_master_fetch_sdo_lists(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
        printk(KERN_ERR "Failed to fetch SDO lists!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
        goto out_deactivate;
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
    ecrt_master_print(master, 2);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
#else
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
    ecrt_master_print(master, 0);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
        printk(KERN_ERR "Failed to get slave!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    }
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
    if (
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
        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
   224
        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
   225
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
        ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
        ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  1) || // dualcode
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
        ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
        ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
        ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
        ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us
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
        printk(KERN_ERR "Failed to configure SSI 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
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
#if 0
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
    if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
        printk(KERN_ERR "Failed to get slave!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
        goto out_deactivate;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
    if (ecrt_slave_write_alias(slave, 0x5678)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
        printk(KERN_ERR "Failed to write alias!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
        goto out_deactivate;
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
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
#ifdef ASYNC
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    // Einmal senden und warten...
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
    ecrt_master_prepare_async_io(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
#endif
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    if (ecrt_master_start_eoe(master)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
        printk(KERN_ERR "Failed to start EoE processing!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
        goto out_deactivate;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
    printk("Starting cyclic sample thread...\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
    ticks = start_rt_timer(nano2count(TIMERTICKS));
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
    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
   263
        printk(KERN_ERR "Failed to init RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
        goto out_stop_timer;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
    if (rt_task_make_periodic(&task, rt_get_time() + ticks, ticks)) {
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
        printk(KERN_ERR "Failed to run RTAI task!\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
        goto out_stop_task;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    }
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module started. ===\n");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
    return 0;
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
 out_stop_task:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
 out_stop_timer:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
 out_deactivate:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
 out_release_master:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
 out_msr_cleanup:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
 out_return:
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
    return -1;
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
/*****************************************************************************/
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
void __exit cleanup_mod(void)
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
{
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
    printk(KERN_INFO "=== Unloading EtherCAT RTAI MSR sample module... ===\n");
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
    rt_task_delete(&task);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
    stop_rt_timer();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
    ecrt_master_deactivate(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
    ecrt_release_master(master);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
    rt_sem_delete(&master_sem);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
    msr_rtlib_cleanup();
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
    printk(KERN_INFO "=== EtherCAT RTAI MSR sample module unloaded. ===\n");
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
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
/*****************************************************************************/
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
MODULE_LICENSE("GPL");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
MODULE_DESCRIPTION ("EtherCAT RTAI MSR sample module");
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
module_init(init_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
module_exit(cleanup_mod);
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
f6594a07cdda MSR example finished.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
/*****************************************************************************/