examples/dc_user/main.c
author Martin Troxler <ch1010277@ch10pc446>
Tue, 09 Nov 2010 11:12:35 +0100
changeset 1997 65bcd7e304cf
parent 1941 e12266a4f473
child 1970 00e18cef9fc8
permissions -rw-r--r--
hg ignored debuild.log
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
 *  $Id$
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
 *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
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
 *  This file is part of the IgH EtherCAT Master.
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
 *  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
    10
 *  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
    11
 *  published by the Free Software Foundation.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  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
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  Public License for more details.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  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
    19
 *  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
    20
 *  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
    21
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  ---
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
 *  The license mentioned above concerns the source code only. Using the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 ****************************************************************************/
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
#include <errno.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include <signal.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <stdio.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <string.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <sys/resource.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <sys/time.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <sys/types.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <unistd.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#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
// Application parameters
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
#define FREQUENCY 100
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
#define PRIORITY 1
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
// Optional features
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
#define CONFIGURE_PDOS  1
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
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
// EtherCAT
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
static ec_master_t *master = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
static ec_master_state_t master_state = {};
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
static ec_domain_t *domain1 = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
static ec_domain_state_t domain1_state = {};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
// Timer
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
static unsigned int sig_alarms = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
static unsigned int user_alarms = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
// process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
static uint8_t *domain1_pd = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
#define BusCouplerPos    0, 0
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
#define DigOutSlavePos   0, 1
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
#define CounterSlavePos  0, 2
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
#define Beckhoff_EL2008 0x00000002, 0x07d83052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
#define IDS_Counter     0x000012ad, 0x05de3052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
// offsets for PDO entries
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
static int off_dig_out;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
static int off_counter_in;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
static int off_counter_out;
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 unsigned int counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
static unsigned int blink_counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
static unsigned int blink = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
static unsigned int sync_ref_counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
struct timeval app_time;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
void check_domain1_state(void)
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
    ec_domain_state_t ds;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
    ecrt_domain_state(domain1, &ds);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
    if (ds.working_counter != domain1_state.working_counter)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
        printf("Domain1: WC %u.\n", ds.working_counter);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    if (ds.wc_state != domain1_state.wc_state)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
        printf("Domain1: State %u.\n", ds.wc_state);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    domain1_state = ds;
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
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
void check_master_state(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
    ec_master_state_t ms;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
    ecrt_master_state(master, &ms);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
    if (ms.slaves_responding != master_state.slaves_responding)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
        printf("%u slave(s).\n", ms.slaves_responding);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    if (ms.al_states != master_state.al_states)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
        printf("AL states: 0x%02X.\n", ms.al_states);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    if (ms.link_up != master_state.link_up)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
    master_state = ms;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
void cyclic_task()
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    int i;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    // receive process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
    ecrt_master_receive(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
    ecrt_domain_process(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    // check process data state (optional)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    check_domain1_state();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
    if (counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
        counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
    } else { // do this at 1 Hz
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
        counter = FREQUENCY;
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
        // calculate new process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
        blink = !blink;
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
        // check for master state (optional)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
        check_master_state();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    if (blink_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
        blink_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
    } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
        blink_counter = 9;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
        // calculate new process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
        blink = !blink;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
    }
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
    // write process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
    EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02);
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
    app_time.tv_usec += 1000000 / FREQUENCY;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
    if (app_time.tv_usec >= 1000000)  {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
        app_time.tv_usec -= 1000000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
        app_time.tv_sec++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
    }
1466
362147819ca1 EC_TIMEVAL2NANO() takes no pointer argument any more.
Florian Pose <fp@igh-essen.com>
parents: 1449
diff changeset
   166
    ecrt_master_application_time(master, EC_TIMEVAL2NANO(app_time));
1414
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
    if (sync_ref_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
        sync_ref_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
        sync_ref_counter = 9;
1434
4c6fe0ae37f1 Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents: 1417
diff changeset
   172
        ecrt_master_sync_reference_clock(master);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    ecrt_master_sync_slave_clocks(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
    // send process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
    ecrt_domain_queue(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
    ecrt_master_send(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
void signal_handler(int signum) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
    switch (signum) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
        case SIGALRM:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
            sig_alarms++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
            break;
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
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
int main(int argc, char **argv)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
{
1804
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   195
    ec_slave_config_t *sc;
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    struct sigaction sa;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    struct itimerval tv;
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
    master = ecrt_request_master(0);
1804
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   200
    if (!master)
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   201
        return -1;
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    domain1 = ecrt_master_create_domain(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    if (!domain1)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
    // Create configuration for bus coupler
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
    sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    if (!sc)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
    if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
                    DigOutSlavePos, Beckhoff_EL2008))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
    }
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
    off_dig_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
            0x7000, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    if (off_dig_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
	if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
					CounterSlavePos, IDS_Counter))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
	}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
	off_counter_in = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
			0x6020, 0x11, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
	if (off_counter_in < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
	off_counter_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
			0x7020, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
	if (off_counter_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
        return -1;
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
    // 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
   240
	ecrt_slave_config_dc(sc, 0x0700, 10000000, 4400000, 0, 0);
1414
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
    printf("Activating master...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
    if (ecrt_master_activate(master))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
    if (!(domain1_pd = ecrt_domain_data(domain1))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
#if PRIORITY
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    pid_t pid = getpid();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
    if (setpriority(PRIO_PROCESS, pid, -19))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
        fprintf(stderr, "Warning: Failed to set priority: %s\n",
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
                strerror(errno));
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
#endif
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
    sa.sa_handler = signal_handler;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
    sigemptyset(&sa.sa_mask);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
    sa.sa_flags = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
    if (sigaction(SIGALRM, &sa, 0)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
        fprintf(stderr, "Failed to install signal handler!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    printf("Starting timer...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
    tv.it_interval.tv_sec = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    tv.it_interval.tv_usec = 1000000 / FREQUENCY;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
    tv.it_value.tv_sec = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    tv.it_value.tv_usec = 1000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    if (setitimer(ITIMER_REAL, &tv, NULL)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
        return 1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
    gettimeofday(&app_time, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
    printf("Started.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
	while (1) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
        pause();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
#if 0
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
        struct timeval t;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
        gettimeofday(&t, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
        printf("%u.%06u\n", t.tv_sec, t.tv_usec);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
#endif
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
        while (sig_alarms != user_alarms) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
            cyclic_task();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
            user_alarms++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
        }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
	}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
	return 0;
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
/****************************************************************************/