examples/user/main.c
author Florian Pose <fp@igh-essen.com>
Mon, 27 Oct 2014 14:15:25 +0100
branchstable-1.5
changeset 2563 25da153a74b9
parent 2556 674fcdccc0f3
child 2565 f7b06b264646
permissions -rw-r--r--
Output CCAT configuration.
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     1
/*****************************************************************************
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     2
 *
2556
Florian Pose <fp@igh-essen.com>
parents: 2555
diff changeset
     3
 *  $Id$
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     4
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     5
 *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     6
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     8
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    11
 *  published by the Free Software Foundation.
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    12
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    16
 *  Public License for more details.
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    17
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    21
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    22
 *  ---
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    23
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    27
 *
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    28
 ****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    29
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    30
#include <errno.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    31
#include <signal.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    32
#include <stdio.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    33
#include <string.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    34
#include <sys/resource.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    35
#include <sys/time.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    36
#include <sys/types.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    37
#include <unistd.h>
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    38
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    39
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    40
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    41
#include "ecrt.h"
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    42
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    43
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    44
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    45
// Application parameters
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    46
#define FREQUENCY 100
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    47
#define PRIORITY 0
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    48
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    49
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    50
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    51
// EtherCAT
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    52
static ec_master_t *master = NULL;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    53
static ec_master_state_t master_state = {};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    54
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    55
static ec_domain_t *domain1 = NULL;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    56
static ec_domain_state_t domain1_state = {};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    57
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    58
static ec_slave_config_t *sc_ana_in = NULL;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    59
static ec_slave_config_state_t sc_ana_in_state = {};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    60
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    61
// Timer
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    62
static unsigned int sig_alarms = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    63
static unsigned int user_alarms = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    64
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    65
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    66
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    67
// process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    68
static uint8_t *domain1_pd = NULL;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    69
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    70
#define BusCouplerPos  0, 3
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    71
#define DigOutSlavePos 0, 0
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    72
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    73
//#define Beckhoff_EK1100 0x00000002, 0x044c2c52
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    74
#define Beckhoff_EK1100 0x00000002, 0x04562c52
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    75
#define Beckhoff_EL1008 0x00000002, 0x03f03052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    76
#define Beckhoff_EL2004 0x00000002, 0x07d43052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    77
#define Beckhoff_EL2008 0x00000002, 0x07d83052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    78
#define Beckhoff_EL2032 0x00000002, 0x07f03052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    79
#define Beckhoff_EL3152 0x00000002, 0x0c503052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    80
#define Beckhoff_EL3102 0x00000002, 0x0c1e3052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    81
#define Beckhoff_EL4102 0x00000002, 0x10063052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    82
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    83
// offsets for PDO entries
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    84
static unsigned int off_dig_in[1];
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    85
static unsigned int off_dig_out[2];
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    86
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    87
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    88
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    89
// Digital in ------------------------
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    90
static ec_pdo_entry_info_t el1008_channels[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    91
    {0x6000, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    92
    {0x6010, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    93
    {0x6020, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    94
    {0x6030, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    95
    {0x6040, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    96
    {0x6050, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    97
    {0x6060, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    98
    {0x6070, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    99
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   100
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   101
static ec_pdo_info_t el1008_pdos[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   102
    {0x1a00, 1, &el1008_channels[0]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   103
    {0x1a01, 1, &el1008_channels[1]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   104
    {0x1a02, 1, &el1008_channels[2]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   105
    {0x1a03, 1, &el1008_channels[3]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   106
    {0x1a04, 1, &el1008_channels[4]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   107
    {0x1a05, 1, &el1008_channels[5]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   108
    {0x1a06, 1, &el1008_channels[6]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   109
    {0x1a07, 1, &el1008_channels[7]}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   110
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   111
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   112
static ec_sync_info_t el1008_syncs[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   113
    {2, EC_DIR_OUTPUT},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   114
    {3, EC_DIR_INPUT, 8, el1008_pdos},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   115
    {0xff}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   116
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   117
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   118
// Digital out ------------------------
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   119
static ec_pdo_entry_info_t el2008_channels[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   120
    {0x7000, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   121
    {0x7010, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   122
    {0x7020, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   123
    {0x7030, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   124
    {0x7040, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   125
    {0x7050, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   126
    {0x7060, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   127
    {0x7070, 1, 1},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   128
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   129
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   130
static ec_pdo_info_t el2008_pdos[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   131
    {0x1600, 1, &el2008_channels[0]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   132
    {0x1601, 1, &el2008_channels[1]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   133
    {0x1602, 1, &el2008_channels[2]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   134
    {0x1603, 1, &el2008_channels[3]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   135
    {0x1604, 1, &el2008_channels[4]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   136
    {0x1605, 1, &el2008_channels[5]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   137
    {0x1606, 1, &el2008_channels[6]},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   138
    {0x1607, 1, &el2008_channels[7]}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   139
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   140
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   141
static ec_sync_info_t el2008_syncs[] = {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   142
    {0, EC_DIR_OUTPUT, 8, el2008_pdos},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   143
    {1, EC_DIR_INPUT},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   144
    {0xff}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   145
};
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   146
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   147
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   148
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   149
void check_domain1_state(void)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   150
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   151
    ec_domain_state_t ds;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   152
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   153
    ecrt_domain_state(domain1, &ds);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   154
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   155
    if (ds.working_counter != domain1_state.working_counter)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   156
        printf("Domain1: WC %u.\n", ds.working_counter);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   157
    if (ds.wc_state != domain1_state.wc_state)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   158
        printf("Domain1: State %u.\n", ds.wc_state);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   159
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   160
    domain1_state = ds;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   161
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   162
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   163
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   164
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   165
void check_master_state(void)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   166
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   167
    ec_master_state_t ms;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   168
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   169
    ecrt_master_state(master, &ms);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   170
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   171
    if (ms.slaves_responding != master_state.slaves_responding)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   172
        printf("%u slave(s).\n", ms.slaves_responding);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   173
    if (ms.al_states != master_state.al_states)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   174
        printf("AL states: 0x%02X.\n", ms.al_states);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   175
    if (ms.link_up != master_state.link_up)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   176
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   177
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   178
    master_state = ms;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   179
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   180
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   181
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   182
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   183
void check_slave_config_states(void)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   184
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   185
    ec_slave_config_state_t s;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   186
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   187
    ecrt_slave_config_state(sc_ana_in, &s);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   188
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   189
    if (s.al_state != sc_ana_in_state.al_state)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   190
        printf("AnaIn: State 0x%02X.\n", s.al_state);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   191
    if (s.online != sc_ana_in_state.online)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   192
        printf("AnaIn: %s.\n", s.online ? "online" : "offline");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   193
    if (s.operational != sc_ana_in_state.operational)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   194
        printf("AnaIn: %soperational.\n",
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   195
                s.operational ? "" : "Not ");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   196
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   197
    sc_ana_in_state = s;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   198
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   199
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   200
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   201
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   202
void cyclic_task()
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   203
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   204
    static unsigned int counter = 10;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   205
    static uint8_t outputValue = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   206
    static int numAsyncCycles = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   207
    uint8_t inputValue = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   208
    static uint8_t error = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   209
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   210
    // receive process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   211
    ecrt_master_receive(master);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   212
    ecrt_domain_process(domain1);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   213
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   214
    // check process data state (optional)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   215
    check_domain1_state();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   216
    
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   217
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   218
	inputValue = EC_READ_U8(domain1_pd + off_dig_in[0]);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   219
	
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   220
	if(inputValue != outputValue) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   221
		numAsyncCycles++;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   222
	} else {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   223
		numAsyncCycles = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   224
	}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   225
	
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   226
	if(numAsyncCycles > 2) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   227
		if(error != 0xff) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   228
			error++;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   229
		}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   230
	}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   231
	
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   232
	
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   233
    if (counter) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   234
        counter--;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   235
    } else {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   236
        counter = 5; //update delay
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   237
        
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   238
        
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   239
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   240
        // calculate new process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   241
        outputValue++;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   242
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   243
        // check for master state (optional)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   244
        check_master_state();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   245
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   246
        // check for islave configuration state(s) (optional)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   247
        check_slave_config_states();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   248
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   249
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   250
    // write process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   251
    EC_WRITE_U8(domain1_pd + off_dig_out[1], outputValue);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   252
    EC_WRITE_U8(domain1_pd + off_dig_out[0], error);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   253
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   254
    // send process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   255
    ecrt_domain_queue(domain1);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   256
    ecrt_master_send(master);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   257
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   258
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   259
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   260
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   261
void signal_handler(int signum) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   262
    switch (signum) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   263
        case SIGALRM:
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   264
            sig_alarms++;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   265
            break;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   266
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   267
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   268
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   269
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   270
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   271
int Init_EL2008(uint16_t position)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   272
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   273
    ec_slave_config_t *sc;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   274
    if (!(sc = ecrt_master_slave_config(master, 0, position, Beckhoff_EL2008))) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   275
        fprintf(stderr, "Failed to get EL2008 configuration #%u.\n", position);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   276
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   277
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   278
    if (ecrt_slave_config_pdos(sc, EC_END, el2008_syncs)) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   279
        fprintf(stderr, "Failed to configure PDOs #%u.\n", position);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   280
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   281
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   282
    if (0 > (off_dig_out[position] = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL))) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   283
		fprintf(stderr, "Failed to configure reg PDOs #%u.\n", position);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   284
		return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   285
	}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   286
    fprintf(stderr, "EL2008 #%u configured offset: %d.\n", position, off_dig_out[position]);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   287
    return 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   288
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   289
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   290
int main(int argc, char **argv)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   291
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   292
    ec_slave_config_t *sc;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   293
    struct sigaction sa;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   294
    struct itimerval tv;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   295
    uint16_t i;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   296
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   297
    master = ecrt_request_master(0);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   298
    if (!master)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   299
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   300
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   301
    domain1 = ecrt_master_create_domain(master);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   302
    if (!domain1)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   303
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   304
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   305
    printf("Configuring PDOs...\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   306
    if (!(sc_ana_in = ecrt_master_slave_config(master, 0, 2, Beckhoff_EL1008))) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   307
        fprintf(stderr, "Failed to get digital in configuration.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   308
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   309
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   310
    if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el1008_syncs)) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   311
        fprintf(stderr, "Failed to configure PDOs.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   312
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   313
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   314
    if (0 > (off_dig_in[0] = ecrt_slave_config_reg_pdo_entry(sc_ana_in, 0x6000, 1, domain1, NULL))) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   315
		fprintf(stderr, "Failed to configure reg PDOs.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   316
		return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   317
	}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   318
    printf("EL1008 configured.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   319
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   320
    for(i = 0; i < 2; ++i) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   321
        if(Init_EL2008(i)) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   322
            fprintf(stderr, "Failed to initialize EL2008 #%u.\n", i);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   323
            return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   324
        }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   325
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   326
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   327
    // Create configuration for bus coupler
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   328
    sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   329
    if (!sc)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   330
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   331
    fprintf(stderr, "EK1100 configured.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   332
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   333
    printf("Activating master...\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   334
    if (ecrt_master_activate(master))
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   335
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   336
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   337
    if (!(domain1_pd = ecrt_domain_data(domain1))) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   338
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   339
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   340
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   341
#if PRIORITY
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   342
    pid_t pid = getpid();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   343
    if (setpriority(PRIO_PROCESS, pid, -19))
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   344
        fprintf(stderr, "Warning: Failed to set priority: %s\n",
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   345
                strerror(errno));
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   346
#endif
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   347
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   348
    sa.sa_handler = signal_handler;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   349
    sigemptyset(&sa.sa_mask);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   350
    sa.sa_flags = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   351
    if (sigaction(SIGALRM, &sa, 0)) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   352
        fprintf(stderr, "Failed to install signal handler!\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   353
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   354
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   355
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   356
    printf("Starting timer...\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   357
    tv.it_interval.tv_sec = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   358
    tv.it_interval.tv_usec = 1000000 / FREQUENCY;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   359
    tv.it_value.tv_sec = 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   360
    tv.it_value.tv_usec = 1000;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   361
    if (setitimer(ITIMER_REAL, &tv, NULL)) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   362
        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   363
        return 1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   364
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   365
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   366
    printf("Started.\n");
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   367
    while (1) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   368
        pause();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   369
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   370
#if 0
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   371
        struct timeval t;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   372
        gettimeofday(&t, NULL);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   373
        printf("%u.%06u\n", t.tv_sec, t.tv_usec);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   374
#endif
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   375
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   376
        while (sig_alarms != user_alarms) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   377
            cyclic_task();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   378
            user_alarms++;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   379
        }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   380
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   381
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   382
    return 0;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   383
}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   384
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   385
/****************************************************************************/