examples/dc_user/main.c
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 05 Oct 2018 23:47:07 +0200
branchstable-1.5
changeset 2722 5e5bec5ee9c1
parent 2698 9e65f782e8a1
permissions -rw-r--r--
rtdmnet.c : Fixed rt/nrt invertion for both sendmsg and recvmsg... m(
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  published by the Free Software Foundation.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  Public License for more details.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  ---
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 ****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#include <errno.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#include <signal.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <stdio.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <string.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <sys/resource.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <sys/time.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <sys/types.h>
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <unistd.h>
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    38
#include <time.h>
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    39
#include <sys/mman.h>
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    40
#include <malloc.h>
2698
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
    41
#include <sched.h> /* sched_setscheduler() */
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
#include "ecrt.h"
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
// Application parameters
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    50
#define FREQUENCY 1000
2698
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
    51
#define CLOCK_TO_USE CLOCK_MONOTONIC
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    52
#define MEASURE_TIMING
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    53
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    54
/****************************************************************************/
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    55
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    56
#define NSEC_PER_SEC (1000000000L)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    57
#define PERIOD_NS (NSEC_PER_SEC / FREQUENCY)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    58
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    59
#define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + \
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    60
	(B).tv_nsec - (A).tv_nsec)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    61
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    62
#define TIMESPEC2NS(T) ((uint64_t) (T).tv_sec * NSEC_PER_SEC + (T).tv_nsec)
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
    63
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
// EtherCAT
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
static ec_master_t *master = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
static ec_master_state_t master_state = {};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
static ec_domain_t *domain1 = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
static ec_domain_state_t domain1_state = {};
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
// process data
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
static uint8_t *domain1_pd = NULL;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
#define BusCouplerPos    0, 0
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
#define DigOutSlavePos   0, 1
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
#define CounterSlavePos  0, 2
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
#define Beckhoff_EL2008 0x00000002, 0x07d83052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
#define IDS_Counter     0x000012ad, 0x05de3052
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
// offsets for PDO entries
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
static int off_dig_out;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
static int off_counter_in;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
static int off_counter_out;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
static unsigned int counter = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
static unsigned int blink = 0;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
static unsigned int sync_ref_counter = 0;
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    94
const struct timespec cycletime = {0, PERIOD_NS};
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    95
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    96
/*****************************************************************************/
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    97
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    98
struct timespec timespec_add(struct timespec time1, struct timespec time2)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
    99
{
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   100
	struct timespec result;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   101
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   102
	if ((time1.tv_nsec + time2.tv_nsec) >= NSEC_PER_SEC) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   103
		result.tv_sec = time1.tv_sec + time2.tv_sec + 1;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   104
		result.tv_nsec = time1.tv_nsec + time2.tv_nsec - NSEC_PER_SEC;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   105
	} else {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   106
		result.tv_sec = time1.tv_sec + time2.tv_sec;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   107
		result.tv_nsec = time1.tv_nsec + time2.tv_nsec;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   108
	}
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   109
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   110
	return result;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   111
}
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
void check_domain1_state(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    ec_domain_state_t ds;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
    ecrt_domain_state(domain1, &ds);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
    if (ds.working_counter != domain1_state.working_counter)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
        printf("Domain1: WC %u.\n", ds.working_counter);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
    if (ds.wc_state != domain1_state.wc_state)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
        printf("Domain1: State %u.\n", ds.wc_state);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    domain1_state = ds;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
/*****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
void check_master_state(void)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
{
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
    ec_master_state_t ms;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
    ecrt_master_state(master, &ms);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   137
	if (ms.slaves_responding != master_state.slaves_responding)
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
        printf("%u slave(s).\n", ms.slaves_responding);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
    if (ms.al_states != master_state.al_states)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
        printf("AL states: 0x%02X.\n", ms.al_states);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
    if (ms.link_up != master_state.link_up)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    master_state = ms;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
void cyclic_task()
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
{
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   151
    struct timespec wakeupTime, time;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   152
#ifdef MEASURE_TIMING
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   153
    struct timespec startTime, endTime, lastStartTime = {};
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   154
    uint32_t period_ns = 0, exec_ns = 0, latency_ns = 0,
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   155
             latency_min_ns = 0, latency_max_ns = 0,
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   156
             period_min_ns = 0, period_max_ns = 0,
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   157
             exec_min_ns = 0, exec_max_ns = 0;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   158
#endif
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   159
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   160
    // get current time
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   161
    clock_gettime(CLOCK_TO_USE, &wakeupTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   162
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   163
	while(1) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   164
		wakeupTime = timespec_add(wakeupTime, cycletime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   165
        clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   166
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   167
#ifdef MEASURE_TIMING
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   168
        clock_gettime(CLOCK_TO_USE, &startTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   169
        latency_ns = DIFF_NS(wakeupTime, startTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   170
        period_ns = DIFF_NS(lastStartTime, startTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   171
        exec_ns = DIFF_NS(lastStartTime, endTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   172
        lastStartTime = startTime;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   173
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   174
        if (latency_ns > latency_max_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   175
            latency_max_ns = latency_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   176
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   177
        if (latency_ns < latency_min_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   178
            latency_min_ns = latency_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   179
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   180
        if (period_ns > period_max_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   181
            period_max_ns = period_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   182
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   183
        if (period_ns < period_min_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   184
            period_min_ns = period_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   185
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   186
        if (exec_ns > exec_max_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   187
            exec_max_ns = exec_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   188
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   189
        if (exec_ns < exec_min_ns) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   190
            exec_min_ns = exec_ns;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   191
        }
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   192
#endif
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   193
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   194
		// receive process data
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   195
		ecrt_master_receive(master);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   196
		ecrt_domain_process(domain1);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   197
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   198
		// check process data state (optional)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   199
		check_domain1_state();
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   200
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   201
		if (counter) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   202
			counter--;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   203
		} else { // do this at 1 Hz
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   204
			counter = FREQUENCY;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   205
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   206
			// check for master state (optional)
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   207
			check_master_state();
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   208
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   209
#ifdef MEASURE_TIMING
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   210
            // output timing stats
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   211
            printf("period     %10u ... %10u\n",
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
   212
                    period_min_ns, period_max_ns);
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   213
            printf("exec       %10u ... %10u\n",
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
   214
                    exec_min_ns, exec_max_ns);
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   215
            printf("latency    %10u ... %10u\n",
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
   216
                    latency_min_ns, latency_max_ns);
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   217
            period_max_ns = 0;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   218
            period_min_ns = 0xffffffff;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   219
            exec_max_ns = 0;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   220
            exec_min_ns = 0xffffffff;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   221
            latency_max_ns = 0;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   222
            latency_min_ns = 0xffffffff;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   223
#endif
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   224
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   225
			// calculate new process data
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   226
			blink = !blink;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   227
		}
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   228
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   229
		// write process data
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   230
		EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   231
		EC_WRITE_U8(domain1_pd + off_counter_out, blink ? 0x00 : 0x02);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   232
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   233
		// write application time to master
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   234
		clock_gettime(CLOCK_TO_USE, &time);
1971
ba8a75cb1c98 Fixed TIMESPEC2NS() macro use.
Florian Pose <fp@igh-essen.com>
parents: 1970
diff changeset
   235
		ecrt_master_application_time(master, TIMESPEC2NS(time));
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   236
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   237
		if (sync_ref_counter) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   238
			sync_ref_counter--;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   239
		} else {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   240
			sync_ref_counter = 1; // sync every cycle
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   241
			ecrt_master_sync_reference_clock(master);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   242
		}
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   243
		ecrt_master_sync_slave_clocks(master);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   244
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   245
		// send process data
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   246
		ecrt_domain_queue(domain1);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   247
		ecrt_master_send(master);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   248
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   249
#ifdef MEASURE_TIMING
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   250
        clock_gettime(CLOCK_TO_USE, &endTime);
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   251
#endif
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   252
	}
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
/****************************************************************************/
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
int main(int argc, char **argv)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
{
1804
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   259
    ec_slave_config_t *sc;
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   260
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   261
	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   262
		perror("mlockall failed");
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   263
		return -1;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   264
	}
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
   265
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
    master = ecrt_request_master(0);
1804
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   267
    if (!master)
742607c464c4 Removed tabulators.
Florian Pose <fp@igh-essen.com>
parents: 1466
diff changeset
   268
        return -1;
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    domain1 = ecrt_master_create_domain(master);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    if (!domain1)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
    // Create configuration for bus coupler
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
    sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
    if (!sc)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
    if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
                    DigOutSlavePos, Beckhoff_EL2008))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
    off_dig_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
            0x7000, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
    if (off_dig_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
	if (!(sc = ecrt_master_slave_config(master,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
					CounterSlavePos, IDS_Counter))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
        fprintf(stderr, "Failed to get slave configuration.\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
	}
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
	off_counter_in = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
			0x6020, 0x11, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
	if (off_counter_in < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
	off_counter_out = ecrt_slave_config_reg_pdo_entry(sc,
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
			0x7020, 1, domain1, NULL);
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
	if (off_counter_out < 0)
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
    // configure SYNC signals for this slave
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   307
	ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0);
1414
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
    printf("Activating master...\n");
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
    if (ecrt_master_activate(master))
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   313
    if (!(domain1_pd = ecrt_domain_data(domain1))) {
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   314
        return -1;
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   315
    }
0037a63d3cc5 DC example applications.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   316
2698
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   317
    /* Set priority */
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   318
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   319
    struct sched_param param = {};
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   320
    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   321
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   322
    printf("Using priority %i.", param.sched_priority);
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   323
    if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   324
        perror("sched_setscheduler failed");
9e65f782e8a1 Fixed scheduler settings in dc_user example; use CLOCK_MONOTONIC.
Florian Pose
parents: 2421
diff changeset
   325
    }
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   326
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   327
	printf("Starting cyclic function.\n");
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   328
    cyclic_task();
2421
bc2d4bf9cbe5 Removed trailing spaces.
Florian Pose <fp@igh-essen.com>
parents: 1971
diff changeset
   329
1970
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   330
    return 0;
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   331
}
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   332
00e18cef9fc8 Improved dc_user example.
Florian Pose <fp@igh-essen.com>
parents: 1941
diff changeset
   333
/****************************************************************************/