[etherlab-users] Ethercat/ProfiNet - add correct slave configuration to program

Carlos Herkt ch at lessmueller.de
Wed Sep 10 14:15:24 CEST 2014


Hello Richard,

thank you for your quick repsonse and your advice.

We modified our program to fit your remarks, but the
ecrt_slave_config_complete_sdo(...) function fails with the error
"Failed to configure SDO: Invalid argument" and a return code smaller
than zero.
Also some errors appear in dmesq log:
[  +0,359596] EtherCAT ERROR 0-4: SDO upload 0x1600:00 aborted.
[  +0,000010] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This
object does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO
entries.
[  +0,000005] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO
0x1600.

After starting the program, the "Run" led of the EL6631 stays green, the
"Pn Run/Err" keeps blinking green and the "Pn Err" is off.

What am I missing?
Attached you find the whole C program source code, dmesg log, output of
the program and the XML definitions exported from TwinCAT.

Best regards,
Carlos Herkt

Am 10.09.2014 09:16, schrieb Richard Hacker:
> I don't see you using pn_config_master_0xf800 et al. anywhere in your
> proggie! Besides you're missing CoE 0x8002.
>
> Mit freundlichem Gruß
>
> Richard Hacker
>

-------------- next part --------------
/* Dmesg output after starting program */
[ +10,975693] EtherCAT: Requesting master 0...
[  +0,000008] EtherCAT: Successfully requested master 0.
[  +3,000440] EtherCAT 0: Domain0: Logical address 0x00000000, 98 byte, expected working counter 3.
[  +0,000006] EtherCAT 0:   Datagram domain0-0-main: Logical offset 0x00000000, 98 byte, type LRW.
[  +0,000129] EtherCAT 0: Master thread exited.
[  +0,000006] EtherCAT 0: Starting EtherCAT-OP thread.
[  +0,001573] EtherCAT WARNING 0: No app_time received up to now, but master already active.
[  +0,359596] EtherCAT ERROR 0-4: SDO upload 0x1600:00 aborted.
[  +0,000010] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This object does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO entries.
[  +0,000005] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO 0x1600.
[  +0,579982] EtherCAT ERROR 0-4: SDO upload 0x1A00:00 aborted.
[  +0,000012] EtherCAT ERROR 0-4: SDO abort message 0x06020000: "This object does not exist in the object directory".
[  +0,000005] EtherCAT ERROR 0-4: Failed to read number of mapped PDO entries.
[  +0,000004] EtherCAT WARNING 0-4: Failed to read PDO entries for PDO 0x1A00.
[  +0,679983] EtherCAT 0: Domain 0: Working counter changed to 3/3.
[  +0,050383] EtherCAT 0: Slave states on main device: PREOP, OP.
[  +3,776881] EtherCAT 0: Releasing master...
[  +0,000063] EtherCAT 0: Master thread exited.
[  +0,000021] EtherCAT 0: Starting EtherCAT-IDLE thread.
[  +0,000702] EtherCAT 0: Released.
[  +0,138335] EtherCAT 0: Slave states on main device: PREOP.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Klemme_6631f.xml
Type: text/xml
Size: 73716 bytes
Desc: not available
URL: <http://lists.etherlab.org/pipermail/etherlab-users/attachments/20140910/dac45b05/attachment-0004.xml>
-------------- next part --------------
/*****************************************************************************
 *
 *  $Id$
 *
 *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License version 2, as
 *  published by the Free Software Foundation.
 *
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 *  Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  ---
 *
 *  The license mentioned above concerns the source code only. Using the
 *  EtherCAT technology and brand is only permitted in compliance with the
 *  industrial property and similar rights of Beckhoff Automation GmbH.
 *
 ****************************************************************************/

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

/****************************************************************************/

#include "ecrt.h"

/****************************************************************************/

// Application parameters
#define FREQUENCY 100
#define PRIORITY 1

// Optional features
#define CONFIGURE_PDOS  1
#define SDO_ACCESS      0

/****************************************************************************/

// EtherCAT
static ec_master_t *master = NULL;
static ec_master_state_t master_state = {};

static ec_domain_t *domain1 = NULL;
static ec_domain_state_t domain1_state = {};

static ec_slave_config_t *sc = NULL;
static ec_slave_config_state_t sc_state = {};

// Timer
static unsigned int sig_alarms = 0;
static unsigned int user_alarms = 0;

/****************************************************************************/

// process data
static uint8_t *domain1_pd = NULL;

#define BusCouplerPos  0, 0
#define ProfiNetPos    0, 4

#define Beckhoff_EK1100 0x00000002, 0x044c2c52
#define ProfiNetTerm 0x00000002, 0x19e73052

// Configuration from XML file

const char *pn_config_master_0xf800 = "0f000100656c363633312d706e636f6e74726f6c6c657200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002500200160ea48eec0a80101ffffff00c0a80101c0a8b2fd0501ffff000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
const char *pn_config_master_0xf820 = "0200c0a8b2fd01012203";
const char *pn_config_master_0x8000 = "3a00636966787265706e7300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003014c0200000810c0a8b2fd050120000100c0a80109ffffff00c0a8010100c001000000010001000100050008000200000000000000000000000000000000000000200004000100030000000000020000000000000000000000000000000000000000000000000000000000000000000000";
const char *pn_config_slave_0x8002 = "7800000000000000008001110000011100000000000000000000000002000503240980003200e803000000000000000000000000000000000000000000000000000000000180011100000211000000000000000000000100040054015c000000000000000000010001000000000000000000000000000000000000000280011100000311000000000000000000000200040054015c000000000000000000000000000000000000000000000000000000000000000100011100000011000000000000000000000300080000000000000000000000000000000000000000000000000000000000000000000000010001001100000010000000000000001400000004000800000000000000000000000000000000000000000000000000000000000000000000000200010011000000100000000000000014000000050008000000000000000000000000000000000000000000000000000000000000000000000005000100100000000f0000001400000000000000060008000000000000000000000000000000000000000000000000000000000000000000000006000100100000000f000000140000000000000007000800000000000000000000000000000000000000000000000000000000000000";

// offsets for PDO entries

static unsigned int off_in0;
static unsigned int off_out0;

const static ec_pdo_entry_reg_t domain1_regs[] = {
	{ProfiNetPos, ProfiNetTerm, 0x6001, 1, &off_in0},
	{ProfiNetPos, ProfiNetTerm, 0x7001, 1, &off_out0},
	{}
};

static unsigned int counter = 0;
static unsigned int blink = 0;

/*****************************************************************************/

#if CONFIGURE_PDOS

/* Master 0, Slave 4, "EL6631"
 * Vendor ID:       0x00000002
 * Product code:    0x19e73052
 * Revision number: 0x00120000
 */
ec_pdo_entry_info_t profinet_pdo_entries[] = {
	// TxPDO: cifxrepns_In
	{0x6000, 0x01, 16},		// cifxrepns_BoxState
	{0x6000, 0x02, 16},		// cifxrepns_BoxDiag
	{0x6001, 0x01, 160},	// Input Data Submodule 1
	{0x6001, 0x02, 160},	// Input Data Submodule 2

	// RxPDO: cifxrepns_Out
	{0x7000, 0x01, 16},		// cifxrepns_BoxCtrl
	{0x7001, 0x01, 160},	// Output Data Submodule 1
	{0x7001, 0x02, 160},	// Output Data Submodule 2

	// TxPDO: PnIoProtocolState
	{0xf100, 0x01, 16},		// DevState
	{0xf100, 0x02, 16},		// PnIoError
	{0xf100, 0x03, 16},		// PnIoDiag

	// TxPDO: ECatState
	{0xf101, 0x01, 16},		// ECatState

	// RxPdo: PnIoProtocolCtrl
	{0xf200, 0x01, 16},		// DevCtrl

	// RxPdo: ECatCtrl
	{0xf201, 0x01, 16},		// ECatCtrl
	{}
};

ec_pdo_info_t profinet_pdos[] = {
	{0x1a00, 4, profinet_pdo_entries + 0},	// TxPDO: cifxrepns_In
	{0x1bfe, 3, profinet_pdo_entries + 7}, 	// TxPDO: PnIoProtocolState
	{0x1bff, 1, profinet_pdo_entries + 10},	// TxPDO: ECatState
	{0x1600, 3, profinet_pdo_entries + 4},	// RxPDO: cifxrepns_Out
	{0x17fe, 1, profinet_pdo_entries + 11}, // RxPDO: PnIoProtocolCtrl
	{0x17ff, 1, profinet_pdo_entries + 12},	// RxPDO: ECatCtrl
};

ec_sync_info_t profinet_syncs[] = {
	{0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
	{1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
	{2, EC_DIR_OUTPUT, 3, profinet_pdos + 3, EC_WD_ENABLE},
	{3, EC_DIR_INPUT, 3, profinet_pdos + 0, EC_WD_DISABLE},
	{0xff}
};

#endif

/*****************************************************************************/

#if SDO_ACCESS
static ec_sdo_request_t *sdo;
#endif

/*****************************************************************************/

/* Helper funktion, buffer has be allocated and big enough */
unsigned int strToCharArray(const char *str,unsigned char *buffer)
{
	unsigned int i;
	unsigned char byteVal;
	//        const char *str = pb_config;
	//        unsigned char cf_data[256];
	size_t size = strlen(str);

	for (i = 0; i<  size; i++) {
		sscanf(str, "%2X",&byteVal);
		str += 2;
		buffer[i] = (uint8_t) byteVal;
	}
	return (size / 2);
}

void check_domain1_state(void)
{
	ec_domain_state_t ds;

	ecrt_domain_state(domain1, &ds);

	if (ds.working_counter != domain1_state.working_counter)
		printf("Domain1: WC %u.\n", ds.working_counter);
	if (ds.wc_state != domain1_state.wc_state)
		printf("Domain1: State %u.\n", ds.wc_state);

	domain1_state = ds;
}

/*****************************************************************************/

void check_master_state(void)
{
	ec_master_state_t ms;

	ecrt_master_state(master, &ms);

	if (ms.slaves_responding != master_state.slaves_responding)
		printf("%u slave(s).\n", ms.slaves_responding);
	if (ms.al_states != master_state.al_states)
		printf("AL states: 0x%02X.\n", ms.al_states);
	if (ms.link_up != master_state.link_up)
		printf("Link is %s.\n", ms.link_up ? "up" : "down");

	master_state = ms;
}

/*****************************************************************************/

void check_slave_config_states(void)
{
	ec_slave_config_state_t s;

	ecrt_slave_config_state(sc, &s);

	if (s.al_state != sc_state.al_state)
		printf("DigIn: State 0x%02X.\n", s.al_state);
	if (s.online != sc_state.online)
		printf("DigIn: %s.\n", s.online ? "online" : "offline");
	if (s.operational != sc_state.operational)
		printf("DigIn: %soperational.\n",
				s.operational ? "" : "Not ");

	sc_state = s;
}

/*****************************************************************************/

#if SDO_ACCESS
void read_sdo(void)
{
	switch (ecrt_sdo_request_state(sdo)) {
		case EC_REQUEST_UNUSED: // request was not used yet
			ecrt_sdo_request_read(sdo); // trigger first read
			break;
		case EC_REQUEST_BUSY:
			fprintf(stderr, "Still busy...\n");
			break;
		case EC_REQUEST_SUCCESS:
			fprintf(stderr, "SDO value: 0x%04X\n",
					EC_READ_U16(ecrt_sdo_request_data(sdo)));
			ecrt_sdo_request_read(sdo); // trigger next read
			break;
		case EC_REQUEST_ERROR:
			fprintf(stderr, "Failed to read SDO!\n");
			ecrt_sdo_request_read(sdo); // retry reading
			break;
	}
}
#endif

/****************************************************************************/

void cyclic_task()
{
	// receive process data
	ecrt_master_receive(master);
	ecrt_domain_process(domain1);

	// check process data state (optional)
	check_domain1_state();

	if (counter) {
		counter--;
	} else { // do this at 1 Hz
		counter = FREQUENCY;

		// check for master state (optional)
		check_master_state();

		// check for islave configuration state(s) (optional)
		check_slave_config_states();

#if SDO_ACCESS
		// read process data SDO
		read_sdo();
#endif

	}

	printf("0:%x\n1:%x\n", EC_READ_U8(domain1_pd + off_in0), EC_READ_U8(domain1_pd + off_in0 + 1));
	EC_WRITE_U8(domain1_pd + off_out0, 0x06);


	// send process data
	ecrt_domain_queue(domain1);
	ecrt_master_send(master);
}

/****************************************************************************/

void signal_handler(int signum) {
	switch (signum) {
		case SIGALRM:
			sig_alarms++;
			break;
	}
}

/****************************************************************************/

int main(int argc, char **argv) {

	int cnt; 
	unsigned char pb_config_buf[1024];
	struct sigaction sa;
	struct itimerval tv;

	master = ecrt_request_master(0);
	if (!master)
		return -1;

	domain1 = ecrt_master_create_domain(master);
	if (!domain1)
		return -1;


	/* ELK1100 BusCoupler */
	sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
	if (!sc) {
		fprintf(stderr, "Failed to get EK1100 configuration of coupler.\n");
		return -1;
	}


	/* EL6631 Master */
	if (!(sc = ecrt_master_slave_config(master, ProfiNetPos, ProfiNetTerm))) {
		fprintf(stderr, "Failed to get slave configuration.\n");
		return -1;
	}

	cnt = strToCharArray(pn_config_master_0xf800, pb_config_buf);
	ecrt_slave_config_complete_sdo(sc, 0xf800, pb_config_buf, cnt);

	cnt = strToCharArray(pn_config_master_0xf820, pb_config_buf);
	ecrt_slave_config_complete_sdo(sc, 0xf820, pb_config_buf, cnt);

	cnt = strToCharArray(pn_config_master_0x8000 ,pb_config_buf);
	ecrt_slave_config_complete_sdo(sc, 0x8000, pb_config_buf, cnt); 

	cnt = strToCharArray(pn_config_slave_0x8002, pb_config_buf);
	ecrt_slave_config_complete_sdo(sc, 0x8002, pb_config_buf, cnt);

	sleep(3); // Workaround from mailinglist: permalink.gmane.org/gmane.network.etherlab.devel/64


	printf("Configuring PDOs...\n");
	if (ecrt_slave_config_pdos(sc, EC_END, profinet_syncs)) {
		fprintf(stderr, "Failed to configure PDOs.\n");
		return -1;
	}

	if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
		fprintf(stderr, "PDO entry registration failed!\n");
		return -1;
	}


	/* Active Master */
	printf("Activating master... ");
	if (ecrt_master_activate(master)){
		fprintf(stderr, "activation failed.\n");
		return -1;
	}
	printf("ok!\n");

	if (!(domain1_pd = ecrt_domain_data(domain1))) {
		fprintf(stderr, "Domain data initialization failed.\n");
		return -1;
	}
	printf("Domain data registered ok.\n");

#if PRIORITY
	pid_t pid = getpid();
	if (setpriority(PRIO_PROCESS, pid, -19))
		fprintf(stderr, "Warning: Failed to set priority: %s\n",
				strerror(errno));
#endif

	sa.sa_handler = signal_handler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction(SIGALRM, &sa, 0)) {
		fprintf(stderr, "Failed to install signal handler!\n");
		return -1;
	}

	printf("Starting timer...\n");
	tv.it_interval.tv_sec = 0;
	tv.it_interval.tv_usec = 1000000 / FREQUENCY;
	tv.it_value.tv_sec = 0;
	tv.it_value.tv_usec = 1000;
	if (setitimer(ITIMER_REAL, &tv, NULL)) {
		fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
		return 1;
	}

	printf("Started.\n");
	while (1) {
		pause();

#if 0
		struct timeval t;
		gettimeofday(&t, NULL);
		printf("%u.%06u\n", t.tv_sec, t.tv_usec);
#endif

		while (sig_alarms != user_alarms) {
			cyclic_task();
			user_alarms++;
		}
	}

	return 0;
}

/****************************************************************************/
-------------- next part --------------
/* Output of program */
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Failed to configure SDO: Invalid argument
Configuring PDOs...
Activating master... ok!
Domain data registered ok.
Starting timer...
Started.
6 slave(s).
AL states: 0x02.
Link is up.
DigIn: State 0x02.
DigIn: online.


More information about the Etherlab-users mailing list