examples/dc_user/main.c
author Florian Pose <fp@igh-essen.com>
Tue, 05 May 2009 08:15:02 +0000
changeset 1441 27a8d6f97a95
parent 1438 1d30b96bb04f
child 1449 6d1ef8afeaab
permissions -rw-r--r--
Added ecrt_slave_sdo_upload() and ecrt_slave_sdo_download(). Thanks to
S. Weiser.
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
    if (blink_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
        blink_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
    } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
        blink_counter = 9;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
        // calculate new process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
        blink = !blink;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
    // write process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
    EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
    EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02);
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
    app_time.tv_usec += 1000000 / FREQUENCY;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
    if (app_time.tv_usec >= 1000000)  {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
        app_time.tv_usec -= 1000000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
        app_time.tv_sec++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
    }
1434
4c6fe0ae37f1 Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents: 1417
diff changeset
   167
    ecrt_master_application_time(master, EC_TIMEVAL2NANO(&app_time));
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    if (sync_ref_counter) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
        sync_ref_counter--;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
    } else {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
        sync_ref_counter = 9;
1434
4c6fe0ae37f1 Separated application time from synchronizing reference clock.
Florian Pose <fp@igh-essen.com>
parents: 1417
diff changeset
   173
        ecrt_master_sync_reference_clock(master);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
    ecrt_master_sync_slave_clocks(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
    // send process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
    ecrt_domain_queue(domain1);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
    ecrt_master_send(master);
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
void signal_handler(int signum) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    switch (signum) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
        case SIGALRM:
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
            sig_alarms++;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
            break;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
int main(int argc, char **argv)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
	ec_slave_config_t *sc;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    struct sigaction sa;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
    struct itimerval tv;
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
    master = ecrt_request_master(0);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
	if (!master)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
		return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    domain1 = ecrt_master_create_domain(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    if (!domain1)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
        return -1;
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
    // Create configuration for bus coupler
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    if (!sc)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
        return -1;
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
    if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
                    DigOutSlavePos, Beckhoff_EL2008))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
        return -1;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
    off_dig_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
            0x7000, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
    if (off_dig_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
	if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
					CounterSlavePos, IDS_Counter))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
        return -1;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
	off_counter_in = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
			0x6020, 0x11, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
	if (off_counter_in < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
	off_counter_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
			0x7020, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
	if (off_counter_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
        return -1;
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
#if 1
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
    // configure SYNC signals for this slave
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
	ecrt_slave_config_dc_assign_activate(sc, 0x0700);
1438
1d30b96bb04f Replaced ecrt_slave_config_dc_cycle_times() and ecrt_slave_config_dc_shift_times() by ecrt_slave_config_dc_sync_signals().
Florian Pose <fp@igh-essen.com>
parents: 1434
diff changeset
   243
	ecrt_slave_config_dc_sync_signals(sc, 10000000, 4400000, 0, 0);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
#endif
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
    printf("Activating master...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    if (ecrt_master_activate(master))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
        return -1;
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 (!(domain1_pd = ecrt_domain_data(domain1))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
        return -1;
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
#if PRIORITY
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    pid_t pid = getpid();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
    if (setpriority(PRIO_PROCESS, pid, -19))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
        fprintf(stderr, "Warning: Failed to set priority: %s\n",
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
                strerror(errno));
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
#endif
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
    sa.sa_handler = signal_handler;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
    sigemptyset(&sa.sa_mask);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    sa.sa_flags = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
    if (sigaction(SIGALRM, &sa, 0)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
        fprintf(stderr, "Failed to install signal handler!\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    }
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
    printf("Starting timer...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    tv.it_interval.tv_sec = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    tv.it_interval.tv_usec = 1000000 / FREQUENCY;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
    tv.it_value.tv_sec = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
    tv.it_value.tv_usec = 1000;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    if (setitimer(ITIMER_REAL, &tv, NULL)) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
        return 1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
    }
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
    gettimeofday(&app_time, NULL);
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
    printf("Started.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
	while (1) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
        pause();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
#if 0
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
        struct timeval t;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
        gettimeofday(&t, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
        printf("%u.%06u\n", t.tv_sec, t.tv_usec);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
#endif
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
        while (sig_alarms != user_alarms) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
            cyclic_task();
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
            user_alarms++;
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
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
	return 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
/****************************************************************************/