examples/user/main.c
author Florian Pose
Tue, 13 Feb 2018 17:16:01 +0100
branchstable-1.5
changeset 2698 9e65f782e8a1
parent 2697 4f5ae7e0fbaa
permissions -rw-r--r--
Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
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>
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    38
#include <time.h> /* clock_gettime() */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    39
#include <sys/mman.h> /* mlockall() */
2697
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
    40
#include <sched.h> /* sched_setscheduler() */
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    41
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
#include "ecrt.h"
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    45
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    46
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    47
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    48
/** Task period in ns. */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    49
#define PERIOD_NS   (1000000)
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    50
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    51
#define MAX_SAFE_STACK (8 * 1024) /* The maximum stack size which is
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    52
                                     guranteed safe to access without
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    53
                                     faulting */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    54
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    55
/****************************************************************************/
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    56
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    57
/* Constants */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    58
#define NSEC_PER_SEC (1000000000)
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
    59
#define FREQUENCY (NSEC_PER_SEC / PERIOD_NS)
2555
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
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    62
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    63
// EtherCAT
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    64
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
    65
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
    66
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    67
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
    68
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
    69
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    70
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
    71
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
    72
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    73
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    74
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    75
// process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    76
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
    77
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    78
#define BusCouplerPos  0, 0
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    79
#define DigOutSlavePos 0, 2
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    80
#define AnaInSlavePos  0, 3
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    81
#define AnaOutSlavePos 0, 4
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    82
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    83
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    84
#define Beckhoff_EL2004 0x00000002, 0x07d43052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    85
#define Beckhoff_EL2032 0x00000002, 0x07f03052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    86
#define Beckhoff_EL3152 0x00000002, 0x0c503052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    87
#define Beckhoff_EL3102 0x00000002, 0x0c1e3052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    88
#define Beckhoff_EL4102 0x00000002, 0x10063052
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    89
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
    90
// offsets for PDO entries
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    91
static unsigned int off_ana_in_status;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    92
static unsigned int off_ana_in_value;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    93
static unsigned int off_ana_out;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    94
static unsigned int off_dig_out;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    95
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    96
const static ec_pdo_entry_reg_t domain1_regs[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    97
    {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 1, &off_ana_in_status},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    98
    {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 2, &off_ana_in_value},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
    99
    {AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   100
    {DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   101
    {}
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   102
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   103
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   104
static unsigned int counter = 0;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   105
static unsigned int blink = 0;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   106
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   107
/*****************************************************************************/
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   108
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   109
// Analog in --------------------------
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   110
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   111
static ec_pdo_entry_info_t el3102_pdo_entries[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   112
    {0x3101, 1,  8}, // channel 1 status
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   113
    {0x3101, 2, 16}, // channel 1 value
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   114
    {0x3102, 1,  8}, // channel 2 status
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   115
    {0x3102, 2, 16}, // channel 2 value
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   116
    {0x6401, 1, 16}, // channel 1 value (alt.)
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   117
    {0x6401, 2, 16}  // channel 2 value (alt.)
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   118
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   119
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   120
static ec_pdo_info_t el3102_pdos[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   121
    {0x1A00, 2, el3102_pdo_entries},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   122
    {0x1A01, 2, el3102_pdo_entries + 2}
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   123
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   124
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   125
static ec_sync_info_t el3102_syncs[] = {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   126
    {2, EC_DIR_OUTPUT},
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   127
    {3, EC_DIR_INPUT, 2, el3102_pdos},
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   128
    {0xff}
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
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   131
// Analog out -------------------------
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   132
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   133
static ec_pdo_entry_info_t el4102_pdo_entries[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   134
    {0x3001, 1, 16}, // channel 1 value
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   135
    {0x3002, 1, 16}, // channel 2 value
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   136
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   137
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   138
static ec_pdo_info_t el4102_pdos[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   139
    {0x1600, 1, el4102_pdo_entries},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   140
    {0x1601, 1, el4102_pdo_entries + 1}
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   141
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   142
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   143
static ec_sync_info_t el4102_syncs[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   144
    {2, EC_DIR_OUTPUT, 2, el4102_pdos},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   145
    {3, EC_DIR_INPUT},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   146
    {0xff}
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   147
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   148
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   149
// Digital out ------------------------
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   150
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   151
static ec_pdo_entry_info_t el2004_channels[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   152
    {0x3001, 1, 1}, // Value 1
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   153
    {0x3001, 2, 1}, // Value 2
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   154
    {0x3001, 3, 1}, // Value 3
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   155
    {0x3001, 4, 1}  // Value 4
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   156
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   157
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   158
static ec_pdo_info_t el2004_pdos[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   159
    {0x1600, 1, &el2004_channels[0]},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   160
    {0x1601, 1, &el2004_channels[1]},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   161
    {0x1602, 1, &el2004_channels[2]},
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   162
    {0x1603, 1, &el2004_channels[3]}
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   163
};
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   164
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   165
static ec_sync_info_t el2004_syncs[] = {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   166
    {0, EC_DIR_OUTPUT, 4, el2004_pdos},
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   167
    {1, EC_DIR_INPUT},
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   168
    {0xff}
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   169
};
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
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   172
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   173
void check_domain1_state(void)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   174
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   175
    ec_domain_state_t ds;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   176
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   177
    ecrt_domain_state(domain1, &ds);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   178
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   179
    if (ds.working_counter != domain1_state.working_counter) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   180
        printf("Domain1: WC %u.\n", ds.working_counter);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   181
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   182
    if (ds.wc_state != domain1_state.wc_state) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   183
        printf("Domain1: State %u.\n", ds.wc_state);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   184
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   185
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   186
    domain1_state = ds;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   187
}
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
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   190
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   191
void check_master_state(void)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   192
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   193
    ec_master_state_t ms;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   194
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   195
    ecrt_master_state(master, &ms);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   196
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   197
    if (ms.slaves_responding != master_state.slaves_responding) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   198
        printf("%u slave(s).\n", ms.slaves_responding);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   199
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   200
    if (ms.al_states != master_state.al_states) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   201
        printf("AL states: 0x%02X.\n", ms.al_states);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   202
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   203
    if (ms.link_up != master_state.link_up) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   204
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   205
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   206
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   207
    master_state = ms;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   208
}
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
/*****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   211
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   212
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
   213
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   214
    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
   215
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   216
    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
   217
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   218
    if (s.al_state != sc_ana_in_state.al_state) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   219
        printf("AnaIn: State 0x%02X.\n", s.al_state);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   220
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   221
    if (s.online != sc_ana_in_state.online) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   222
        printf("AnaIn: %s.\n", s.online ? "online" : "offline");
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   223
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   224
    if (s.operational != sc_ana_in_state.operational) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   225
        printf("AnaIn: %soperational.\n", s.operational ? "" : "Not ");
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   226
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   227
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   228
    sc_ana_in_state = s;
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
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   231
/*****************************************************************************/
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   232
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   233
void cyclic_task()
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   234
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   235
    // receive process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   236
    ecrt_master_receive(master);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   237
    ecrt_domain_process(domain1);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   238
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   239
    // check process data state
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   240
    check_domain1_state();
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   241
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   242
    if (counter) {
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   243
        counter--;
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   244
    } else { // do this at 1 Hz
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   245
        counter = FREQUENCY;
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   246
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   247
        // calculate new process data
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   248
        blink = !blink;
2555
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
        // check for master state (optional)
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   251
        check_master_state();
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   252
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   253
        // check for slave configuration state(s) (optional)
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   254
        check_slave_config_states();
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   255
    }
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   256
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   257
#if 0
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   258
    // read process data
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   259
    printf("AnaIn: state %u value %u\n",
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   260
            EC_READ_U8(domain1_pd + off_ana_in_status),
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   261
            EC_READ_U16(domain1_pd + off_ana_in_value));
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   262
#endif
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   263
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   264
#if 1
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   265
    // write process data
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   266
    EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09);
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   267
#endif
2555
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
    // send process data
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   270
    ecrt_domain_queue(domain1);
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   271
    ecrt_master_send(master);
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
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   274
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   275
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   276
void stack_prefault(void)
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   277
{
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   278
    unsigned char dummy[MAX_SAFE_STACK];
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   279
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   280
    memset(dummy, 0, MAX_SAFE_STACK);
2555
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
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   283
/****************************************************************************/
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   284
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   285
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
   286
{
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   287
    ec_slave_config_t *sc;
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   288
    struct timespec wakeup_time;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   289
    int ret = 0;
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   290
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   291
    master = ecrt_request_master(0);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   292
    if (!master) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   293
        return -1;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   294
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   295
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   296
    domain1 = ecrt_master_create_domain(master);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   297
    if (!domain1) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   298
        return -1;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   299
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   300
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   301
    if (!(sc_ana_in = ecrt_master_slave_config(
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   302
                    master, AnaInSlavePos, Beckhoff_EL3102))) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   303
        fprintf(stderr, "Failed to get slave configuration.\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   304
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   305
    }
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   306
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   307
    printf("Configuring PDOs...\n");
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   308
    if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3102_syncs)) {
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   309
        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
   310
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   311
    }
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   312
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   313
    if (!(sc = ecrt_master_slave_config(
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   314
                    master, AnaOutSlavePos, Beckhoff_EL4102))) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   315
        fprintf(stderr, "Failed to get slave configuration.\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   316
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   317
    }
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   318
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   319
    if (ecrt_slave_config_pdos(sc, EC_END, el4102_syncs)) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   320
        fprintf(stderr, "Failed to configure PDOs.\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   321
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   322
    }
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   323
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   324
    if (!(sc = ecrt_master_slave_config(
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   325
                    master, DigOutSlavePos, Beckhoff_EL2032))) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   326
        fprintf(stderr, "Failed to get slave configuration.\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   327
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   328
    }
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   329
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   330
    if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   331
        fprintf(stderr, "Failed to configure PDOs.\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   332
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   333
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   334
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   335
    // Create configuration for bus coupler
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   336
    sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   337
    if (!sc) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   338
        return -1;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   339
    }
2565
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   340
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   341
    if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   342
        fprintf(stderr, "PDO entry registration failed!\n");
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   343
        return -1;
f7b06b264646 - restored examples/user/
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2556
diff changeset
   344
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   345
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   346
    printf("Activating master...\n");
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   347
    if (ecrt_master_activate(master)) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   348
        return -1;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   349
    }
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   350
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   351
    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
   352
        return -1;
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   353
    }
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   354
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   355
    /* Set priority */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   356
2697
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   357
    struct sched_param param = {};
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   358
    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   359
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   360
    printf("Using priority %i.", param.sched_priority);
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   361
    if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
4f5ae7e0fbaa Use sched_setscheduler() to set priority in user example.
Florian Pose
parents: 2664
diff changeset
   362
        perror("sched_setscheduler failed");
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   363
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   364
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   365
    /* Lock memory */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   366
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   367
    if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   368
        fprintf(stderr, "Warning: Failed to lock memory: %s\n",
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   369
                strerror(errno));
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   370
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   371
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   372
    stack_prefault();
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   373
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   374
    printf("Starting RT task with dt=%u ns.\n", PERIOD_NS);
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   375
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   376
    clock_gettime(CLOCK_MONOTONIC, &wakeup_time);
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   377
    wakeup_time.tv_sec += 1; /* start in future */
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   378
    wakeup_time.tv_nsec = 0;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   379
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   380
    while (1) {
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   381
        ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   382
                &wakeup_time, NULL);
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   383
        if (ret) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   384
            fprintf(stderr, "clock_nanosleep(): %s\n", strerror(ret));
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   385
            break;
2555
18c226b66533 replace symlink in example with the real file
Patrick Bruenn <p.bruenn@beckhoff.com>
parents: 2549
diff changeset
   386
        }
2664
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   387
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   388
        cyclic_task();
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   389
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   390
        wakeup_time.tv_nsec += PERIOD_NS;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   391
        while (wakeup_time.tv_nsec >= NSEC_PER_SEC) {
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   392
            wakeup_time.tv_nsec -= NSEC_PER_SEC;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   393
            wakeup_time.tv_sec++;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   394
        }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   395
    }
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   396
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   397
    return ret;
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   398
}
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   399
14a18eae7e3b Migrated user-space example to use clock_nanosleep().
Florian Pose <fp@igh-essen.com>
parents: 2565
diff changeset
   400
/****************************************************************************/