examples/dc_rtai/dc_rtai_sample.c
author Florian Pose <fp@igh-essen.com>
Mon, 15 Jun 2009 15:46:04 +0000
changeset 1471 2ec173081b7f
parent 1466 362147819ca1
child 1500 ed1a733efbc5
permissions -rw-r--r--
Fixed mailbox usage for slaves that do not provide sync manager
configurations.
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  Distributed clocks sample for the IgH EtherCAT master.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  $Id$
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  This file is part of the IgH EtherCAT Master.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  modify it under the terms of the GNU General Public License version 2, as
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  published by the Free Software Foundation.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  Public License for more details.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  You should have received a copy of the GNU General Public License along
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  with the IgH EtherCAT Master; if not, write to the Free Software
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  ---
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  The license mentioned above concerns the source code only. Using the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  EtherCAT technology and brand is only permitted in compliance with the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  industrial property and similar rights of Beckhoff Automation GmbH.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
// Linux
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <linux/module.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <linux/err.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
// RTAI
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <rtai_sched.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include <rtai_sem.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
// EtherCAT
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "../../include/ecrt.h"
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
// Module parameters
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
#define FREQUENCY 1000 // task frequency in Hz
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
#define INHIBIT_TIME 20
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
#define TIMERTICKS (1000000000 / FREQUENCY)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
#define NUM_DIG_OUT 1
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
1434
4c6fe0ae37f1 Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents: 1417
diff changeset
    54
#define PFX "ec_dc_rtai_sample: "
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
// EtherCAT
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
static ec_master_t *master = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
static ec_master_state_t master_state = {};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
static ec_domain_t *domain1 = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
static ec_domain_state_t domain1_state = {};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
// RTAI
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
static RT_TASK task;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
static SEM master_sem;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
static cycles_t t_last_cycle = 0, t_critical;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
// process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
static uint8_t *domain1_pd; // process data memory
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
#define DigOutSlavePos(X) 0, (1 + (X))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
#define CounterSlavePos   0, 2
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
#define Beckhoff_EL2008 0x00000002, 0x07d83052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
#define IDS_Counter     0x000012ad, 0x05de3052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
static int off_dig_out[NUM_DIG_OUT];
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
static int off_counter_in;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
static int off_counter_out;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
static unsigned int counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
static unsigned int blink_counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
static unsigned int blink = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
static u32 counter_value = 0U;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
static ec_pdo_entry_info_t el2008_channels[] = {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    {0x7000, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
    {0x7010, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
    {0x7020, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    {0x7030, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    {0x7040, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    {0x7050, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
    {0x7060, 1, 1},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    {0x7070, 1, 1}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
static ec_pdo_info_t el2008_pdos[] = {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
    {0x1600, 1, &el2008_channels[0]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
    {0x1601, 1, &el2008_channels[1]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    {0x1602, 1, &el2008_channels[2]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    {0x1603, 1, &el2008_channels[3]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
    {0x1604, 1, &el2008_channels[4]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    {0x1605, 1, &el2008_channels[5]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
    {0x1606, 1, &el2008_channels[6]},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    {0x1607, 1, &el2008_channels[7]}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
static ec_sync_info_t el2008_syncs[] = {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    {0, EC_DIR_OUTPUT, 8, el2008_pdos},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
    {1, EC_DIR_INPUT},
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    {0xff}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
void check_domain1_state(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    ec_domain_state_t ds;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    spin_lock(&master_lock);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    ecrt_domain_state(domain1, &ds);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    spin_unlock(&master_lock);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    if (ds.working_counter != domain1_state.working_counter)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
        printk(KERN_INFO PFX "Domain1: WC %u.\n", ds.working_counter);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    if (ds.wc_state != domain1_state.wc_state)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
        printk(KERN_INFO PFX "Domain1: State %u.\n", ds.wc_state);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
    domain1_state = ds;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
void check_master_state(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    ec_master_state_t ms;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
    spin_lock(&master_lock);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    ecrt_master_state(master, &ms);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    spin_unlock(&master_lock);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
    if (ms.slaves_responding != master_state.slaves_responding)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
        printk(KERN_INFO PFX "%u slave(s).\n", ms.slaves_responding);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
    if (ms.al_states != master_state.al_states)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
        printk(KERN_INFO PFX "AL states: 0x%02X.\n", ms.al_states);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
    if (ms.link_up != master_state.link_up)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
        printk(KERN_INFO PFX "Link is %s.\n", ms.link_up ? "up" : "down");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
    master_state = ms;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
void run(long data)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
    int i;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
	struct timeval tv;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
    unsigned int sync_ref_counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
    count2timeval(nano2count(rt_get_real_time_ns()), &tv);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    while (1) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
        t_last_cycle = get_cycles();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
        // receive process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
        rt_sem_wait(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        ecrt_master_receive(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        ecrt_domain_process(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
        rt_sem_signal(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
        // check process data state (optional)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
        check_domain1_state();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
        if (counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
            counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
        } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
			u32 c;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
			
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
            counter = FREQUENCY;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
            // check for master state (optional)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
            check_master_state();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
			c = EC_READ_U32(domain1_pd + off_counter_in);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
			if (counter_value != c) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
				counter_value = c;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
				printk(KERN_INFO PFX "counter=%u\n", counter_value);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
			}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
        if (blink_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
            blink_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
            blink_counter = 9;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
            // calculate new process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
            blink = !blink;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
        // write process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
        for (i = 0; i < NUM_DIG_OUT; i++) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
            EC_WRITE_U8(domain1_pd + off_dig_out[i], blink ? 0x66 : 0x99);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
		EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
        rt_sem_wait(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
		
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
        tv.tv_usec += 1000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
        if (tv.tv_usec >= 1000000)  {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
            tv.tv_usec -= 1000000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
            tv.tv_sec++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
        }
1466
362147819ca1 EC_TIMEVAL2NANO() takes no pointer argument any more.
Florian Pose <fp@igh-essen.com>
parents: 1449
diff changeset
   223
        ecrt_master_application_time(master, EC_TIMEVAL2NANO(tv));
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
            
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
        if (sync_ref_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
            sync_ref_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
        } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
            sync_ref_counter = 9;
1434
4c6fe0ae37f1 Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents: 1417
diff changeset
   229
            ecrt_master_sync_reference_clock(master);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
		ecrt_master_sync_slave_clocks(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        ecrt_domain_queue(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
        ecrt_master_send(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
        rt_sem_signal(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
		
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
        rt_task_wait_period();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
int request_lock(void *data)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
    // too close to the next real time cycle: deny access...
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
    if (get_cycles() - t_last_cycle > t_critical) return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    // allow access
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    rt_sem_wait(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
    return 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
void release_lock(void *data)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
    rt_sem_signal(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
int __init init_mod(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    int ret = -1, i;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
    RTIME tick_period, requested_ticks, now;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    ec_slave_config_t *sc;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    printk(KERN_INFO PFX "Starting...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    rt_sem_init(&master_sem, 1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    t_critical = cpu_khz * 1000 / FREQUENCY - cpu_khz * INHIBIT_TIME / 1000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
    master = ecrt_request_master(0);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    if (!master) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
        ret = -EBUSY; 
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
        printk(KERN_ERR PFX "Requesting master 0 failed!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
        goto out_return;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
    ecrt_master_callbacks(master, request_lock, release_lock, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
    printk(KERN_INFO PFX "Registering domain...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
    if (!(domain1 = ecrt_master_create_domain(master))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
        printk(KERN_ERR PFX "Domain creation failed!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
        goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
    printk(KERN_INFO PFX "Configuring PDOs...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
    // create configuration for reference clock FIXME
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
	if (!(sc = ecrt_master_slave_config(master, 0, 0, Beckhoff_EK1100))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
		printk(KERN_ERR PFX "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
		goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
	}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
    for (i = 0; i < NUM_DIG_OUT; i++) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
        if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
                        DigOutSlavePos(i), Beckhoff_EL2008))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
            printk(KERN_ERR PFX "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
            goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
        if (ecrt_slave_config_pdos(sc, EC_END, el2008_syncs)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
            printk(KERN_ERR PFX "Failed to configure PDOs.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
            goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
        off_dig_out[i] = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
                0x7000, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
        if (off_dig_out[i] < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
            goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
	if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   316
					CounterSlavePos, IDS_Counter))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   317
		printk(KERN_ERR PFX "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   318
		goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   319
	}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   320
	off_counter_in = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   321
			0x6020, 0x11, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   322
	if (off_counter_in < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   323
		goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   324
	off_counter_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   325
			0x7020, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   326
	if (off_counter_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   327
		goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   328
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   329
    // configure SYNC signals for this slave
1449
6d1ef8afeaab Ported examples to new DC configuration function.
Florian Pose <fp@igh-essen.com>
parents: 1438
diff changeset
   330
	ecrt_slave_config_dc(sc, 0x0700, 1000000, 440000, 0, 0);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   331
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   332
    printk(KERN_INFO PFX "Activating master...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   333
    if (ecrt_master_activate(master)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   334
        printk(KERN_ERR PFX "Failed to activate master!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   335
        goto out_release_master;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   336
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   337
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   338
    // Get internal process data for domain
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   339
    domain1_pd = ecrt_domain_data(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   340
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   341
    printk(KERN_INFO PFX "Starting cyclic sample thread...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   342
    requested_ticks = nano2count(TIMERTICKS);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   343
    tick_period = start_rt_timer(requested_ticks);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   344
    printk(KERN_INFO PFX "RT timer started with %i/%i ticks.\n",
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   345
           (int) tick_period, (int) requested_ticks);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   346
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   347
    if (rt_task_init(&task, run, 0, 2000, 0, 1, NULL)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   348
        printk(KERN_ERR PFX "Failed to init RTAI task!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   349
        goto out_stop_timer;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   350
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   351
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   352
    now = rt_get_time();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   353
    if (rt_task_make_periodic(&task, now + tick_period, tick_period)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   354
        printk(KERN_ERR PFX "Failed to run RTAI task!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   355
        goto out_stop_task;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   356
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   357
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   358
    printk(KERN_INFO PFX "Initialized.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   359
    return 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   360
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   361
 out_stop_task:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   362
    rt_task_delete(&task);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   363
 out_stop_timer:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   364
    stop_rt_timer();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   365
 out_release_master:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   366
    printk(KERN_ERR PFX "Releasing master...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   367
    ecrt_release_master(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   368
 out_return:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   369
    rt_sem_delete(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   370
    printk(KERN_ERR PFX "Failed to load. Aborting.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   371
    return ret;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   372
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   373
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   374
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   375
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   376
void __exit cleanup_mod(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   377
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   378
    printk(KERN_INFO PFX "Stopping...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   379
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   380
    rt_task_delete(&task);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   381
    stop_rt_timer();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   382
    ecrt_release_master(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   383
    rt_sem_delete(&master_sem);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   384
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   385
    printk(KERN_INFO PFX "Unloading.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   386
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   387
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   388
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   389
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   390
MODULE_LICENSE("GPL");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   391
MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   392
MODULE_DESCRIPTION("EtherCAT distributed clocks sample module");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   393
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   394
module_init(init_mod);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   395
module_exit(cleanup_mod);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   396
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   397
/*****************************************************************************/