<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 2.0cm 2.0cm 2.0cm;}
div.WordSection1
{page:WordSection1;}
--></style></head><body lang=IT><div class=WordSection1><p class=MsoNormal>Hi everyone,</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I have some problem when I’m trying to use a slave as reference clock. My application is based on rtai_rtdm_dc example and it is attached at the end of this mail. The encountered problem is that the execution returns continuously the following messages:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4809954, sysTime: 1467294001654551772)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4810955, sysTime: 1467294001655556313)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4811956, sysTime: 1467294001656548544)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4812957, sysTime: 1467294001657550067)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4813958, sysTime: 1467294001658543298)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4814959, sysTime: 1467294001659538712)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4815960, sysTime: 1467294001660554236)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4816961, sysTime: 1467294001661578083)</p><p class=MsoNormal>app_getTimeNS error: TimeBase greater than system time (timeBase: -4817962, sysTime: 1467294001662547853)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>where the timeBase value continuosly varies. The dmsg output is the following:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>[20002.274528] EtherCAT: Requesting master 0...</p><p class=MsoNormal>[20002.274535] EtherCAT: Successfully requested master 0.</p><p class=MsoNormal>[20002.274628] EtherCAT 0: Domain0: Logical address 0x00000000, 5 byte, expected working counter 2.</p><p class=MsoNormal>[20002.274631] EtherCAT 0: Datagram domain0-0-main: Logical offset 0x00000000, 5 byte, type LWR.</p><p class=MsoNormal>[20002.274673] EtherCAT 0: Master thread exited.</p><p class=MsoNormal>[20002.274677] EtherCAT 0: Starting EtherCAT-OP thread.</p><p class=MsoNormal>[20002.274889] EtherCAT WARNING 0: 1 datagram UNMATCHED!</p><p class=MsoNormal>[20002.356896] EtherCAT 0: Domain 0: Working counter changed to 1/2.</p><p class=MsoNormal>[20002.418739] EtherCAT 0: Slave states on main device: PREOP, OP.</p><p class=MsoNormal>[20003.357932] EtherCAT 0: Domain 0: 3 working counter changes - now 2/2.</p><p class=MsoNormal>[20004.274027] EtherCAT WARNING 0: 8 datagrams UNMATCHED!</p><p class=MsoNormal>[20004.277909] EtherCAT WARNING: Datagram ffff88003517c318 (domain0-0-main) was SKIPPED 4 times.</p><p class=MsoNormal>[20008.152346] EtherCAT 0: Releasing master...</p><p class=MsoNormal>[20008.152364] EtherCAT 0: Master thread exited.</p><p class=MsoNormal>[20008.152379] EtherCAT 0: Starting EtherCAT-IDLE thread.</p><p class=MsoNormal>[20008.152699] EtherCAT 0: Released.</p><p class=MsoNormal>[20008.176023] EtherCAT 0: Slave states on main device: PREOP.</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Anyone has any suggestion about this issue?</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thank you for your help.</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Tommaso<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#include <errno.h></p><p class=MsoNormal>#include <signal.h></p><p class=MsoNormal>#include <stdio.h></p><p class=MsoNormal>#include <string.h></p><p class=MsoNormal>#include <sys/resource.h></p><p class=MsoNormal>#include <sys/time.h></p><p class=MsoNormal>#include <sys/types.h></p><p class=MsoNormal>#include <unistd.h></p><p class=MsoNormal>#include <time.h></p><p class=MsoNormal>#include <sys/mman.h></p><p class=MsoNormal>#include <malloc.h></p><p class=MsoNormal>#include <math.h></p><p class=MsoNormal>#include <stdbool.h></p><p class=MsoNormal>#include <sched.h></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#include "ecrt.h"</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>// Application parameters</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define FREQUENCY 1000 // Frequency set</p><p class=MsoNormal>#define CLOCK_TO_USE CLOCK_REALTIME // Its time represents seconds and nanoseconds since the Epoch</p><p class=MsoNormal>#define MEASURE_TIMING 1 // 1 in order to measure all the time values</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define NSEC_PER_SEC (1000000000L)</p><p class=MsoNormal>#define PERIOD_NS (NSEC_PER_SEC / FREQUENCY)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define DIFF_NS(A, B) (((B).tv_sec - (A).tv_sec) * NSEC_PER_SEC + (B).tv_nsec - (A).tv_nsec)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define TIMESPEC2NS(T) ((uint64_t) (T).tv_sec * NSEC_PER_SEC + (T).tv_nsec)</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define sign(val) ({ typeof (val) _val = (val); ((_val > 0) - (_val < 0)); }) /* Return the sign of a number</p><p class=MsoNormal> *</p><p class=MsoNormal> * ie -1 for -ve value, 0 for 0, +1 for +ve value</p><p class=MsoNormal> *</p><p class=MsoNormal> * \retval the sign of the value</p><p class=MsoNormal> */</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>// EtherCAT</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static ec_master_t *master = NULL;</p><p class=MsoNormal>static ec_master_state_t master_state = {};</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static ec_domain_t *domain1 = NULL;</p><p class=MsoNormal>static ec_domain_state_t domain1_state = {};</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>// EtherCAT synchronization variables</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define DC_FILTER_CNT 1024</p><p class=MsoNormal>#define SYNC_MASTER_TO_REF 1</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static uint64_t dc_start_time_ns = 0LL;</p><p class=MsoNormal>static uint64_t dc_time_ns = 0;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#if SYNC_MASTER_TO_REF</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> static uint8_t dc_started = 0;</p><p class=MsoNormal> static int32_t dc_diff_ns = 0;</p><p class=MsoNormal> static int32_t prev_dc_diff_ns = 0;</p><p class=MsoNormal> static int64_t dc_diff_total_ns = 0LL;</p><p class=MsoNormal> static int64_t dc_delta_total_ns = 0LL;</p><p class=MsoNormal> static int dc_filter_idx = 0;</p><p class=MsoNormal> static int64_t dc_adjust_ns;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static int64_t system_time_base = 0LL;</p><p class=MsoNormal>static uint64_t wakeup_time = 0LL;</p><p class=MsoNormal>static uint64_t overruns = 0LL;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>// Process data</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define BusCouplerPos 0, 0</p><p class=MsoNormal>#define DigOutSlavePos 0, 1</p><p class=MsoNormal>#define AnaOutSlavePos 0, 6</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#define Beckhoff_EK1100 0x00000002, 0x044c2c52</p><p class=MsoNormal>#define Beckhoff_EL2004 0x00000002, 0x07d43052</p><p class=MsoNormal>#define Beckhoff_EL4002 0x00000002, 0x0fa23052</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>// offsets for PDO entries</p><p class=MsoNormal>static int off_dig_out;</p><p class=MsoNormal>static int off_ana_out;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static uint8_t *domain1_pd = NULL;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>static unsigned int counter = 0;</p><p class=MsoNormal>static unsigned int blink = 0;</p><p class=MsoNormal>const struct timespec cycletime = {0, PERIOD_NS};</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/*****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>struct timespec timespec_add(struct timespec time1, struct timespec time2) { //Sum of two timespecs</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> struct timespec result;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if ((time1.tv_nsec + time2.tv_nsec) >= NSEC_PER_SEC) {</p><p class=MsoNormal> result.tv_sec = time1.tv_sec + time2.tv_sec + 1;</p><p class=MsoNormal> result.tv_nsec = time1.tv_nsec + time2.tv_nsec - NSEC_PER_SEC;</p><p class=MsoNormal> } else {</p><p class=MsoNormal> result.tv_sec = time1.tv_sec + time2.tv_sec;</p><p class=MsoNormal> result.tv_nsec = time1.tv_nsec + time2.tv_nsec;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> return result;</p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/*****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>void check_domain1_state(FILE * f) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ec_domain_state_t ds;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ecrt_domain_state(domain1, &ds);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (ds.working_counter != domain1_state.working_counter)</p><p class=MsoNormal> fprintf(f,"Domain1: WC %u.\n", ds.working_counter);</p><p class=MsoNormal> if (ds.wc_state != domain1_state.wc_state)</p><p class=MsoNormal> fprintf(f,"Domain1: State %u.\n", ds.wc_state);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> fflush(f);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> domain1_state = ds;</p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/*****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>void check_master_state(FILE * f) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ec_master_state_t ms;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ecrt_master_state(master, &ms);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (ms.slaves_responding != master_state.slaves_responding)</p><p class=MsoNormal> fprintf(f,"%u slave(s).\n", ms.slaves_responding);</p><p class=MsoNormal> if (ms.al_states != master_state.al_states)</p><p class=MsoNormal> fprintf(f,"AL states: 0x%02X.\n", ms.al_states);</p><p class=MsoNormal> if (ms.link_up != master_state.link_up)</p><p class=MsoNormal> fprintf(f,"Link is %s.\n", ms.link_up ? "up" : "down");</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> fflush(f);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> master_state = ms;</p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>//Misc time functions:</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>uint64_t system_time_ns() { /* get the time in ns for the current cpu (adjusted for the app time base)</p><p class=MsoNormal> *</p><p class=MsoNormal> * \ret the time in ns</p><p class=MsoNormal> */</p><p class=MsoNormal> struct timespec auxTime;</p><p class=MsoNormal> uint64_t time;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> clock_gettime(CLOCK_TO_USE, &auxTime);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> time = TIMESPEC2NS(auxTime);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (system_time_base > time) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> printf("app_getTimeNS error: TimeBase greater than system time (timeBase: %lld, sysTime: %llu)\n", system_time_base, time);</p><p class=MsoNormal> return time;</p><p class=MsoNormal> }</p><p class=MsoNormal> else {</p><p class=MsoNormal> return time - system_time_base;</p><p class=MsoNormal> }</p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>void syncDistClock(void) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#if SYNC_MASTER_TO_REF</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> uint32_t ref_time = 0;</p><p class=MsoNormal> uint64_t prev_app_time = dc_time_ns;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> dc_time_ns = system_time_ns();</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // set master time in nano-seconds</p><p class=MsoNormal> ecrt_master_application_time(master, dc_time_ns);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#if SYNC_MASTER_TO_REF</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // get reference clock time to synchronize master cycle</p><p class=MsoNormal> ecrt_master_reference_clock_time(master, &ref_time);</p><p class=MsoNormal> dc_diff_ns = (uint32_t) prev_app_time - ref_time;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#else</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // sync reference clock to master</p><p class=MsoNormal> ecrt_master_sync_reference_clock(master);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // call to sync slaves to ref slave</p><p class=MsoNormal> ecrt_master_sync_slave_clocks(master);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>//Computation of the slave-to-master time drift and adjust the master clock and cycle period to match</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>//Called after the EtherCAT frame is sent to avoid time jitter in syncDistClock()</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>int32_t updateMasterClock(void) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#if SYNC_MASTER_TO_REF</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // calc drift (via un-normalised time diff)</p><p class=MsoNormal> int32_t delta = dc_diff_ns - prev_dc_diff_ns;</p><p class=MsoNormal> prev_dc_diff_ns = dc_diff_ns;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // normalise the time diff</p><p class=MsoNormal> dc_diff_ns =</p><p class=MsoNormal> ((dc_diff_ns + (PERIOD_NS / 2)) % PERIOD_NS) - (PERIOD_NS / 2);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // only update if primary master</p><p class=MsoNormal> if (dc_started) {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // add to totals</p><p class=MsoNormal> dc_diff_total_ns += dc_diff_ns;</p><p class=MsoNormal> dc_delta_total_ns += delta;</p><p class=MsoNormal> dc_filter_idx++;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (dc_filter_idx >= DC_FILTER_CNT) {</p><p class=MsoNormal> // add rounded delta average</p><p class=MsoNormal> dc_adjust_ns +=</p><p class=MsoNormal> ((dc_delta_total_ns + (DC_FILTER_CNT / 2)) / DC_FILTER_CNT);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // and add adjustment for general diff (to pull in drift)</p><p class=MsoNormal> dc_adjust_ns += sign(dc_diff_total_ns / DC_FILTER_CNT);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // limit crazy numbers (0.1% of std cycle time)</p><p class=MsoNormal> if (dc_adjust_ns < -1000) {</p><p class=MsoNormal> dc_adjust_ns = -1000;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (dc_adjust_ns > 1000) {</p><p class=MsoNormal> dc_adjust_ns = 1000;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // reset</p><p class=MsoNormal> dc_diff_total_ns = 0LL;</p><p class=MsoNormal> dc_delta_total_ns = 0LL;</p><p class=MsoNormal> dc_filter_idx = 0;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // add cycles adjustment to time base (including a spot adjustment)</p><p class=MsoNormal> system_time_base += dc_adjust_ns + sign(dc_diff_ns);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> } else {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> dc_started = (dc_diff_ns != 0);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (dc_started) {</p><p class=MsoNormal> // output first diff</p><p class=MsoNormal> printf("First master diff: %d.\n", dc_diff_ns);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // record the time of this initial cycle</p><p class=MsoNormal> dc_start_time_ns = dc_time_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/*******************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>void cyclic_task() {</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> FILE * f = fopen("/home/temis/Documenti/Compilazione_c/Dc_user/SlaveRef/data1KHzRTFIFOSlaveRef.txt", "w+");</p><p class=MsoNormal> fprintf(f,"Reference:period min/max execution min/max latency min/max\n");</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> struct timespec wakeupTime;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#ifdef MEASURE_TIMING</p><p class=MsoNormal> struct timespec startTime, endTime, lastStartTime = {};</p><p class=MsoNormal> uint32_t period_ns = 0, exec_ns = 0, latency_ns = 0,</p><p class=MsoNormal> latency_min_ns = 0, latency_max_ns = 0,</p><p class=MsoNormal> period_min_ns = 0, period_max_ns = 0,</p><p class=MsoNormal> exec_min_ns = 0, exec_max_ns = 0;</p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // get current time from the clock set in "CLOCK_TO_USE" and put it into wakeupTime</p><p class=MsoNormal> clock_gettime(CLOCK_TO_USE, &wakeupTime);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> while(1) {</p><p class=MsoNormal> wakeupTime = timespec_add(wakeupTime, cycletime); </p><p class=MsoNormal> clock_nanosleep(CLOCK_TO_USE, TIMER_ABSTIME, &wakeupTime, NULL);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#ifdef MEASURE_TIMING</p><p class=MsoNormal> clock_gettime(CLOCK_TO_USE, &startTime);</p><p class=MsoNormal> latency_ns = DIFF_NS(wakeupTime, startTime); </p><p class=MsoNormal> period_ns = DIFF_NS(lastStartTime, startTime); </p><p class=MsoNormal> exec_ns = DIFF_NS(lastStartTime, endTime); </p><p class=MsoNormal> lastStartTime = startTime;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (latency_ns > latency_max_ns) {</p><p class=MsoNormal> latency_max_ns = latency_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (latency_ns < latency_min_ns) {</p><p class=MsoNormal> latency_min_ns = latency_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (period_ns > period_max_ns) {</p><p class=MsoNormal> period_max_ns = period_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (period_ns < period_min_ns) {</p><p class=MsoNormal> period_min_ns = period_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (exec_ns > exec_max_ns) {</p><p class=MsoNormal> exec_max_ns = exec_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal> if (exec_ns < exec_min_ns) {</p><p class=MsoNormal> exec_min_ns = exec_ns;</p><p class=MsoNormal> }</p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // receive process data</p><p class=MsoNormal> ecrt_master_receive(master);</p><p class=MsoNormal> ecrt_domain_process(domain1);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // check process data state (optional)</p><p class=MsoNormal> check_domain1_state(f);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (counter) {</p><p class=MsoNormal> counter--;</p><p class=MsoNormal> } else { // do this at 1 Hz</p><p class=MsoNormal> counter = FREQUENCY;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // check for master state (optional)</p><p class=MsoNormal> check_master_state(f);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#ifdef MEASURE_TIMING</p><p class=MsoNormal> // output timing stats</p><p class=MsoNormal> fprintf(f,"%10u\t%10u\t%10u\t%10u\t%10u\t%10u\n", period_min_ns, period_max_ns, exec_min_ns, exec_max_ns,</p><p class=MsoNormal> latency_min_ns, latency_max_ns);</p><p class=MsoNormal> fflush(f);</p><p class=MsoNormal> period_max_ns = 0;</p><p class=MsoNormal> period_min_ns = 0xffffffff;</p><p class=MsoNormal> exec_max_ns = 0;</p><p class=MsoNormal> exec_min_ns = 0xffffffff;</p><p class=MsoNormal> latency_max_ns = 0;</p><p class=MsoNormal> latency_min_ns = 0xffffffff;</p><p class=MsoNormal>#endif</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // calculate new process data</p><p class=MsoNormal> blink = !blink;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // write process data</p><p class=MsoNormal> EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x66 : 0x99);</p><p class=MsoNormal> </p><p class=MsoNormal> // send process data</p><p class=MsoNormal> ecrt_domain_queue(domain1);</p><p class=MsoNormal> </p><p class=MsoNormal> syncDistClock();</p><p class=MsoNormal> </p><p class=MsoNormal> ecrt_master_send(master);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> updateMasterClock();</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>#ifdef MEASURE_TIMING</p><p class=MsoNormal> clock_gettime(CLOCK_TO_USE, &endTime);</p><p class=MsoNormal>#endif</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>}</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>/****************************************************************************/</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>int main(int argc, char **argv)</p><p class=MsoNormal>{</p><p class=MsoNormal> ec_slave_config_t *sc, *sc_ek1100;</p><p class=MsoNormal> int ret;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {</p><p class=MsoNormal> perror("mlockall failed");</p><p class=MsoNormal> return -1;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> master = ecrt_request_master(0);</p><p class=MsoNormal> if (!master)</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> domain1 = ecrt_master_create_domain(master);</p><p class=MsoNormal> if (!domain1)</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // Create configuration for bus coupler</p><p class=MsoNormal> sc_ek1100 = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);</p><p class=MsoNormal> if (!sc)</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> //Create configuration for digital output</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (!(sc = ecrt_master_slave_config(master, DigOutSlavePos, Beckhoff_EL2004))) {</p><p class=MsoNormal> fprintf(stderr, "Failed to get slave configuration.\n");</p><p class=MsoNormal> return -1;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> off_dig_out = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL);</p><p class=MsoNormal> if (off_dig_out < 0)</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> //Create configuration for analog output</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (!(sc = ecrt_master_slave_config(master, AnaOutSlavePos, Beckhoff_EL4002))) {</p><p class=MsoNormal> fprintf(stderr, "Failed to get slave configuration.\n");</p><p class=MsoNormal> return -1;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> off_ana_out = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL);</p><p class=MsoNormal> if (off_dig_out < 0)</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> // configure SYNC signals for this slave</p><p class=MsoNormal> ecrt_slave_config_dc(sc, 0x0700, PERIOD_NS, 4400000, 0, 0);</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> /* Set the initial master time and select a slave to use as the DC</p><p class=MsoNormal> * reference clock, otherwise pass NULL to auto select the first capable</p><p class=MsoNormal> * slave. Note: This can be used whether the master or the ref slave will</p><p class=MsoNormal> * be used as the systems master DC clock.</p><p class=MsoNormal> */</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> dc_start_time_ns = system_time_ns();</p><p class=MsoNormal> dc_time_ns = dc_start_time_ns;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> /* Attention: The initial application time is also used for phase</p><p class=MsoNormal> * calculation for the SYNC0/1 interrupts. Please be sure to call it at</p><p class=MsoNormal> * the correct phase to the realtime cycle.</p><p class=MsoNormal> */</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ecrt_master_application_time(master, dc_start_time_ns); // Set initial app time</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> ret = ecrt_master_select_reference_clock(master, sc_ek1100);</p><p class=MsoNormal> if (ret < 0) {</p><p class=MsoNormal> fprintf(stderr, "Failed to select reference clock: %s\n", strerror(-ret));</p><p class=MsoNormal> return ret;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> printf("Activating master...\n");</p><p class=MsoNormal> if (ecrt_master_activate(master))</p><p class=MsoNormal> return -1;</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> if (!(domain1_pd = ecrt_domain_data(domain1))) {</p><p class=MsoNormal> return -1;</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> struct sched_param param;</p><p class=MsoNormal> param.sched_priority = 99; /*99 = sched_get_priority_max(SCHED_FIFO)*/ // 99 is the maximal priority</p><p class=MsoNormal> if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {</p><p class=MsoNormal> perror("sched_setscheduler failed");</p><p class=MsoNormal> exit(-1);</p><p class=MsoNormal> }</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> printf("Starting cyclic function.\n");</p><p class=MsoNormal> cyclic_task();</p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> return 0;</p><p class=MsoNormal>}</p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Times New Roman",serif'><o:p> </o:p></span></p></div></body></html>