ab@2054: /******************************************************************************
ab@2054: *
ab@2054: * $Id$
ab@2054: *
ab@2056: * main.c Copyright (C) 2009-2010 Moehwald GmbH B.Benner
ab@2054: * 2011 IgH Andreas Stewering-Bone
ab@2054: *
ab@2054: * This file is part of ethercatrtdm interface to IgH EtherCAT master
ab@2054: *
ab@2054: * The Moehwald ethercatrtdm interface is free software; you can
ab@2054: * redistribute it and/or modify it under the terms of the GNU Lesser General
ab@2054: * Public License as published by the Free Software Foundation; version 2.1
ab@2054: * of the License.
ab@2054: *
ab@2054: * The IgH EtherCAT Master is free software; you can redistribute it and/or
ab@2054: * modify it under the terms of the GNU General Public License version 2, as
ab@2054: * published by the Free Software Foundation.
ab@2054: *
ab@2054: * The IgH EtherCAT master userspace library is distributed in the hope that
ab@2054: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
ab@2054: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab@2054: * GNU Lesser General Public License for more details.
ab@2054: *
ab@2054: * You should have received a copy of the GNU Lesser General Public License
ab@2054: * along with the IgH EtherCAT master userspace library. If not, see
ab@2054: * .
ab@2054: *
ab@2054: * The license mentioned above concerns the source code only. Using the
ab@2054: * EtherCAT technology and brand is only permitted in compliance with the
ab@2054: * industrial property and similar rights of Beckhoff Automation GmbH.
ab@2054: *
ab@2054: *****************************************************************************/
ab@2054:
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2054: #include
ab@2055: #include
ab@2054: #include
ab@2054:
ab@2054: /****************************************************************************/
ab@2054:
ab@2054: #include "../../include/ecrt.h"
ab@2054: #include "../../include/ec_rtdm.h"
ab@2054:
ab@2054:
ab@2054: RT_TASK my_task;
ab@2054:
ab@2054: int rt_fd = -1;
ab@2054: int run=0;
ab@2054:
ab@2054: unsigned int sync_ref_counter = 0;
ab@2054:
ab@2054: CstructMstrAttach MstrAttach;
ab@2054:
ab@2054: /****************************************************************************/
ab@2054:
ab@2056:
ab@2054:
ab@2054: // Optional features
ab@2054: #define CONFIGURE_PDOS 1
ab@2054: //#define SDO_ACCESS 1
ab@2054:
ab@2054: /****************************************************************************/
ab@2054:
ab@2054: // EtherCAT
ab@2054: static ec_master_t *master = NULL;
ab@2054: static ec_master_state_t master_state = {};
ab@2054:
ab@2054: static ec_domain_t *domain1 = NULL;
ab@2054: static ec_domain_state_t domain1_state = {};
ab@2054:
ab@2054: //static ec_slave_config_t *sc_ana_in = NULL;
ab@2054: //static ec_slave_config_state_t sc_ana_in_state = {};
ab@2054:
ab@2054: // Timer
ab@2054: static unsigned int sig_alarms = 0;
ab@2054: //static unsigned int user_alarms = 0;
ab@2054:
ab@2054: /****************************************************************************/
ab@2054: static uint8_t *domain1_pd = NULL;
ab@2054:
ab@2054: // process data
ab@2054:
ab@2054: #define BusCoupler01_Pos 0, 0
ab@2054: #define DigOutSlave01_Pos 0, 1
ab@2054: #define DigOutSlave02_Pos 0, 2
ab@2054: #define DigInSlave01_Pos 0, 3
ab@2054: #define AnaOutSlave01_Pos 0, 4
ab@2054: #define AnaInSlave01_Pos 0, 5
ab@2054: #define BusCoupler02_Pos 0, 6
ab@2054: #define AnaInSlave02_Pos 0, 7
ab@2055: #define DPSlave01_Pos 0, 8
ab@2054:
ab@2054:
ab@2054: #define Beckhoff_EK1100 0x00000002, 0x044c2c52
ab@2054: #define Beckhoff_EL1014 0x00000002, 0x03f63052
ab@2054: #define Beckhoff_EL2004 0x00000002, 0x07d43052
ab@2054: #define Beckhoff_EL4132 0x00000002, 0x10243052
ab@2054: #define Beckhoff_EL3102 0x00000002, 0x0c1e3052
ab@2054: #define Beckhoff_EL4102 0x00000002, 0x10063052
ab@2054: #define Beckhoff_EL6731 0x00000002, 0x1a4b3052
ab@2054: #define Beckhoff_EL6600 0x00000002, 0x19c93052
ab@2054: #define Beckhoff_EL3602 0x00000002, 0x0e123052
ab@2054: #define Beckhoff_EL5151 0x00000002, 0x141f3052
ab@2054:
ab@2054:
ab@2054: // offsets for PDO entries
ab@2054: static unsigned int off_dig_out0 = 0;
ab@2054: static unsigned int off_dig_out1 = 0;
ab@2054: static unsigned int off_dig_out2 = 0;
ab@2054: static unsigned int off_dig_out3 = 0;
ab@2054: static unsigned int off_dig_out4 = 0;
ab@2054: static unsigned int off_dig_out5 = 0;
ab@2054: static unsigned int off_dig_out6 = 0;
ab@2054: static unsigned int off_dig_out7 = 0;
ab@2054: static unsigned int off_dig_in0 = 0;
ab@2054: static unsigned int off_dig_in1 = 0;
ab@2054: static unsigned int off_dig_in2 = 0;
ab@2054: static unsigned int off_dig_in3 = 0;
ab@2054: static unsigned int off_ana_out0 = 0;
ab@2054: static unsigned int off_ana_out1 = 0;
ab@2054: static unsigned int off_ana_in0_status = 0;
ab@2054: static unsigned int off_ana_in0_value = 0;
ab@2054: static unsigned int off_ana_in1_status = 0;
ab@2054: static unsigned int off_ana_in1_value = 0;
ab@2054: static unsigned int off_ana_in2_status = 0;
ab@2054: static unsigned int off_ana_in2_value = 0;
ab@2054: static unsigned int off_ana_in3_status = 0;
ab@2054: static unsigned int off_ana_in3_value = 0;
ab@2054:
ab@2054: //static unsigned int off_dp_slave;
ab@2054:
ab@2054: // process data
ab@2054: unsigned int bit_position0=0; /* Pointer to a variable to store a bit */
ab@2054: unsigned int bit_position1=0; /* Pointer to a variable to store a bit */
ab@2054: unsigned int bit_position2=0; /* Pointer to a variable to store a bit */
ab@2054: unsigned int bit_position3=0; /* Pointer to a variable to store a bit */
ab@2054:
ab@2054: const static ec_pdo_entry_reg_t domain1_regs[] = {
ab@2054: {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7000, 0x01, &off_dig_out0, &bit_position0},
ab@2054: {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7010, 0x01, &off_dig_out1, &bit_position1},
ab@2054: {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7020, 0x01, &off_dig_out2, &bit_position2},
ab@2054: {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7030, 0x01, &off_dig_out3, &bit_position3},
ab@2054: {DigInSlave01_Pos, Beckhoff_EL1014, 0x6000, 0x01, &off_dig_in0},
ab@2054: {AnaOutSlave01_Pos, Beckhoff_EL4132, 0x3001, 0x01, &off_ana_out0},
ab@2054: {AnaOutSlave01_Pos, Beckhoff_EL4132, 0x3002, 0x01, &off_ana_out1},
ab@2054: {AnaInSlave01_Pos, Beckhoff_EL3102, 0x3101, 0x01, &off_ana_in0_status},
ab@2054: {AnaInSlave01_Pos, Beckhoff_EL3102, 0x3101, 0x02, &off_ana_in0_value},
ab@2054: {AnaInSlave01_Pos, Beckhoff_EL3102, 0x3102, 0x01, &off_ana_in1_status},
ab@2054: {AnaInSlave01_Pos, Beckhoff_EL3102, 0x3102, 0x02, &off_ana_in1_value},
ab@2054: {}
ab@2054: };
ab@2054:
ab@2054: char rt_dev_file[64];
ab@2054: static unsigned int counter = 0;
ab@2054: static unsigned int blink = 0;
ab@2054:
ab@2054: static ec_slave_config_t *sc_dig_out_01 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_dig_out_02 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_dig_in_01 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_ana_out_01 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_ana_in_01 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_dpslv_01 = NULL;
ab@2054:
ab@2054: static ec_slave_config_t *sc_ana_in_02 = NULL;
ab@2054:
ab@2054: /*****************************************************************************/
ab@2054:
ab@2054: /* Slave 1, "EL2004"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x07d43052
ab@2054: * Revision number: 0x00100000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_1_pdo_entries[] = {
ab@2054: {0x7000, 0x01, 1}, /* Output */
ab@2054: {0x7010, 0x01, 1}, /* Output */
ab@2054: {0x7020, 0x01, 1}, /* Output */
ab@2054: {0x7030, 0x01, 1}, /* Output */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_1_pdos[] = {
ab@2054: {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
ab@2054: {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
ab@2054: {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
ab@2054: {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_1_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 4, slave_1_pdos + 0, EC_WD_ENABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Slave 2, "EL2004"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x07d43052
ab@2054: * Revision number: 0x00100000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_2_pdo_entries[] = {
ab@2054: {0x7000, 0x01, 1}, /* Output */
ab@2054: {0x7010, 0x01, 1}, /* Output */
ab@2054: {0x7020, 0x01, 1}, /* Output */
ab@2054: {0x7030, 0x01, 1}, /* Output */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_2_pdos[] = {
ab@2054: {0x1600, 1, slave_2_pdo_entries + 0}, /* Channel 1 */
ab@2054: {0x1601, 1, slave_2_pdo_entries + 1}, /* Channel 2 */
ab@2054: {0x1602, 1, slave_2_pdo_entries + 2}, /* Channel 3 */
ab@2054: {0x1603, 1, slave_2_pdo_entries + 3}, /* Channel 4 */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_2_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 4, slave_2_pdos + 0, EC_WD_ENABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Slave 3, "EL1014"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x03f63052
ab@2054: * Revision number: 0x00100000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_3_pdo_entries[] = {
ab@2054: {0x6000, 0x01, 1}, /* Input */
ab@2054: {0x6010, 0x01, 1}, /* Input */
ab@2054: {0x6020, 0x01, 1}, /* Input */
ab@2054: {0x6030, 0x01, 1}, /* Input */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_3_pdos[] = {
ab@2054: {0x1a00, 1, slave_3_pdo_entries + 0}, /* Channel 1 */
ab@2054: {0x1a01, 1, slave_3_pdo_entries + 1}, /* Channel 2 */
ab@2054: {0x1a02, 1, slave_3_pdo_entries + 2}, /* Channel 3 */
ab@2054: {0x1a03, 1, slave_3_pdo_entries + 3}, /* Channel 4 */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_3_syncs[] = {
ab@2054: {0, EC_DIR_INPUT, 4, slave_3_pdos + 0, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Slave 4, "EL4132"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x10243052
ab@2054: * Revision number: 0x03f90000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_4_pdo_entries[] = {
ab@2054: {0x3001, 0x01, 16}, /* Output */
ab@2054: {0x3002, 0x01, 16}, /* Output */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_4_pdos[] = {
ab@2054: {0x1600, 1, slave_4_pdo_entries + 0}, /* RxPDO 01 mapping */
ab@2054: {0x1601, 1, slave_4_pdo_entries + 1}, /* RxPDO 02 mapping */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_4_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 2, slave_4_pdos + 0, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Slave 5, "EL3102"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x0c1e3052
ab@2054: * Revision number: 0x00000000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_5_pdo_entries[] = {
ab@2054: {0x3101, 0x01, 8}, /* Status */
ab@2054: {0x3101, 0x02, 16}, /* Value */
ab@2054: {0x3102, 0x01, 8}, /* Status */
ab@2054: {0x3102, 0x02, 16}, /* Value */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_5_pdos[] = {
ab@2054: {0x1a00, 2, slave_5_pdo_entries + 0}, /* TxPDO 001 mapping */
ab@2054: {0x1a01, 2, slave_5_pdo_entries + 2}, /* TxPDO 002 mapping */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_5_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 2, slave_5_pdos + 0, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Slave 6, "EL6731-0010"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x1a4b3052
ab@2054: * Revision number: 0x0011000a
ab@2054: */
ab@2054:
ab@2054: ec_sync_info_t slave_6_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: };
ab@2054:
ab@2054:
ab@2054: /* Slave 7, "EL6601"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x19c93052
ab@2054: * Revision number: 0x00110000
ab@2054: */
ab@2054: /*
ab@2054: ec_sync_info_t slave_7_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054: */
ab@2054:
ab@2054: /* Master 0, Slave 7, "EL3602"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x0e123052
ab@2054: * Revision number: 0x00100000
ab@2054: */
ab@2054: ec_pdo_entry_info_t slave_7_pdo_entries[] = {
ab@2054: {0x6000, 0x01, 1}, /* Underrange */
ab@2054: {0x6000, 0x02, 1}, /* Overrange */
ab@2054: {0x6000, 0x03, 2}, /* Limit 1 */
ab@2054: {0x6000, 0x05, 2}, /* Limit 2 */
ab@2054: {0x6000, 0x07, 1}, /* Error */
ab@2054: {0x0000, 0x00, 7}, /* Gap */
ab@2054: {0x1800, 0x07, 1},
ab@2054: {0x1800, 0x09, 1},
ab@2054: {0x6000, 0x11, 32}, /* Value */
ab@2054: {0x6010, 0x01, 1}, /* Underrange */
ab@2054: {0x6010, 0x02, 1}, /* Overrange */
ab@2054: {0x6010, 0x03, 2}, /* Limit 1 */
ab@2054: {0x6010, 0x05, 2}, /* Limit 2 */
ab@2054: {0x6010, 0x07, 1}, /* Error */
ab@2054: {0x0000, 0x00, 7}, /* Gap */
ab@2054: {0x1801, 0x07, 1},
ab@2054: {0x1801, 0x09, 1},
ab@2054: {0x6010, 0x11, 32}, /* Value */
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_7_pdos[] = {
ab@2054: {0x1a00, 9, slave_7_pdo_entries + 0}, /* AI TxPDO-Map Inputs Ch.1 */
ab@2054: {0x1a01, 9, slave_7_pdo_entries + 9}, /* AI TxPDO-Map Inputs Ch.2 */
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_7_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 2, slave_7_pdos + 0, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054: /* Master 0, Slave 8, "EL5151"
ab@2054: * Vendor ID: 0x00000002
ab@2054: * Product code: 0x141f3052
ab@2054: * Revision number: 0x00130000
ab@2054: */
ab@2054:
ab@2054: ec_pdo_entry_info_t slave_8_pdo_entries[] = {
ab@2054: {0x6000, 0x01, 1},
ab@2054: {0x6000, 0x02, 1},
ab@2054: {0x6000, 0x03, 1},
ab@2054: {0x0000, 0x00, 4}, /* Gap */
ab@2054: {0x6000, 0x08, 1},
ab@2054: {0x6000, 0x09, 1},
ab@2054: {0x6000, 0x0a, 1},
ab@2054: {0x6000, 0x0b, 1},
ab@2054: {0x0000, 0x00, 1}, /* Gap */
ab@2054: {0x6000, 0x0d, 1},
ab@2054: {0x1c32, 0x20, 1},
ab@2054: {0x0000, 0x00, 1}, /* Gap */
ab@2054: {0x1800, 0x09, 1},
ab@2054: {0x6000, 0x11, 32},
ab@2054: {0x6000, 0x12, 32},
ab@2054: {0x6000, 0x14, 32},
ab@2054: };
ab@2054:
ab@2054: ec_pdo_info_t slave_8_pdos[] = {
ab@2054: {0x0000, 0, NULL},
ab@2054: {0x1a00, 15, slave_8_pdo_entries + 0},
ab@2054: {0x1a02, 1, slave_8_pdo_entries + 15},
ab@2054: };
ab@2054:
ab@2054: ec_sync_info_t slave_8_syncs[] = {
ab@2054: {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
ab@2054: {2, EC_DIR_OUTPUT, 1, slave_8_pdos + 0, EC_WD_DISABLE},
ab@2054: {3, EC_DIR_INPUT, 2, slave_8_pdos + 1, EC_WD_DISABLE},
ab@2054: {0xff}
ab@2054: };
ab@2054:
ab@2054:
ab@2054: /*****************************************************************************/
ab@2054:
ab@2054: #if SDO_ACCESS
ab@2054: static ec_sdo_request_t *sdo;
ab@2054: uint8_t *sdo_adr = NULL;
ab@2054: #endif
ab@2054:
ab@2054:
ab@2054:
ab@2054: void rt_check_domain_state(void)
ab@2054: {
ab@2054: ec_domain_state_t ds;
ab@2054:
ab@2054: if (rt_fd>=0)
ab@2054: {
ab@2054: ecrt_rtdm_domain_state(rt_fd,&ds);
ab@2054: }
ab@2054:
ab@2055: if (ds.working_counter != domain1_state.working_counter)
ab@2054: {
ab@2055: rt_printf("Domain1: WC %u.\n", ds.working_counter);
ab@2054: }
ab@2054: if (ds.wc_state != domain1_state.wc_state)
ab@2054: {
ab@2055: rt_printf("Domain1: State %u.\n", ds.wc_state);
ab@2054: }
ab@2055:
ab@2054: domain1_state = ds;
ab@2054: }
ab@2054:
ab@2054: void rt_check_master_state(void)
ab@2054: {
ab@2054: ec_master_state_t ms;
ab@2054:
ab@2054: if (rt_fd>=0)
ab@2054: {
ab@2054: ecrt_rtdm_master_state(rt_fd,&ms);
ab@2054: }
ab@2054:
ab@2054: if (ms.slaves_responding != master_state.slaves_responding)
ab@2054: {
ab@2055: rt_printf("%u slave(s).\n", ms.slaves_responding);
ab@2054: }
ab@2054: if (ms.al_states != master_state.al_states)
ab@2054: {
ab@2055: rt_printf("AL states: 0x%02X.\n", ms.al_states);
ab@2054: }
ab@2054: if (ms.link_up != master_state.link_up)
ab@2054: {
ab@2055: rt_printf("Link is %s.\n", ms.link_up ? "up" : "down");
ab@2055: }
ab@2054: master_state = ms;
ab@2054: }
ab@2054:
ab@2055:
ab@2055:
ab@2054:
ab@2054: void rt_sync()
ab@2054: {
ab@2054: RTIME now;
ab@2054: now = rt_timer_read();
ab@2055:
ab@2054:
ab@2054: if (rt_fd>=0)
ab@2054: {
ab@2054: ecrt_rtdm_master_application_time(rt_fd, &now);
ab@2054: }
ab@2054:
ab@2054: if (sync_ref_counter) {
ab@2054: sync_ref_counter--;
ab@2054: } else {
ab@2054: sync_ref_counter = 9;
ab@2054: if (rt_fd>=0)
ab@2054: {
ab@2054: ecrt_rtdm_master_sync_reference_clock(rt_fd);
ab@2054: }
ab@2054: }
ab@2054: if (rt_fd>=0)
ab@2054: {
ab@2054: ecrt_rtdm_master_sync_slave_clocks(rt_fd) ;
ab@2054: }
ab@2054: }
ab@2054:
ab@2054: /*****************************************************************************/
ab@2054:
ab@2054: #if SDO_ACCESS
ab@2054: void read_sdo(void)
ab@2054: {
ab@2055: switch (ecrt_sdo_request_state(sdo))
ab@2055: {
ab@2055: case EC_REQUEST_UNUSED: // request was not used yet
ab@2055: ecrt_sdo_request_read(sdo); // trigger first read
ab@2055: break;
ab@2055: case EC_REQUEST_BUSY:
ab@2055: fprintf(stderr, "Still busy...\n");
ab@2055: break;
ab@2055: case EC_REQUEST_SUCCESS:
ab@2055: fprintf(stderr, "SDO value: 0x%04X\n",
ab@2055: EC_READ_U16(ecrt_sdo_request_data(sdo)));
ab@2055: ecrt_sdo_request_read(sdo); // trigger next read
ab@2055: break;
ab@2055: case EC_REQUEST_ERROR:
ab@2055: fprintf(stderr, "Failed to read SDO!\n");
ab@2055: ecrt_sdo_request_read(sdo); // retry reading
ab@2055: break;
ab@2055: }
ab@2054: }
ab@2054:
ab@2054: void PrintSDOState(void)
ab@2054: {
ab@2055: switch (ecrt_sdo_request_state(sdo))
ab@2055: {
ab@2055: case EC_REQUEST_UNUSED: // request was not used yet
ab@2055: fprintf(stderr, "SDO State: EC_REQUEST_UNUSED\n"); // trigger first read
ab@2055: break;
ab@2055: case EC_REQUEST_BUSY:
ab@2055: fprintf(stderr, "SDO State: EC_REQUEST_BUSY\n");
ab@2055: break;
ab@2055: case EC_REQUEST_SUCCESS:
ab@2055: fprintf(stderr, "SDO State: EC_REQUEST_SUCCESS\n");
ab@2055: break;
ab@2055: case EC_REQUEST_ERROR:
ab@2055: fprintf(stderr, "SDO State: EC_REQUEST_ERROR\n");
ab@2055: break;
ab@2055: default:
ab@2055: fprintf(stderr, "SDO State: undefined\n");
ab@2055: break;
ab@2054: }
ab@2054: }
ab@2054: #endif
ab@2054:
ab@2054:
ab@2054: static int cyccount=0;
ab@2054:
ab@2054: /****************************************************************************/
ab@2054:
ab@2054: void signal_handler(int signum) {
ab@2054: switch (signum) {
ab@2054: case SIGALRM:
ab@2054: sig_alarms++;
ab@2054: break;
ab@2054: }
ab@2054: }
ab@2054:
ab@2054:
ab@2054: /**********************************************************/
ab@2054: /* REAL TIME TASK */
ab@2054: /**********************************************************/
ab@2054: void my_task_proc(void *arg)
ab@2054: {
ab@2054: int counter = 0;
ab@2054: int divcounter = 0;
ab@2054: int divider = 10;
ab@2054: int ret;
ab@2054:
ab@2054: RTIME periodns;
ab@2054: float period;
ab@2054:
ab@2054:
ab@2056: period=1E-3; //1kHz
ab@2054:
ab@2054:
ab@2054: periodns=(RTIME)(((double)period * 1E9) + 0.4999999);
ab@2054: rt_task_set_periodic(NULL, TM_NOW, periodns);
ab@2054:
ab@2054: run=1;
ab@2054:
ab@2054: ret = rt_task_set_mode(0, T_PRIMARY, NULL);
ab@2054: if (ret) {
ab@2055: rt_printf("error while rt_task_set_mode, code %d\n",ret);
ab@2054: return;
ab@2054: }
ab@2054:
ab@2054:
ab@2054: while (run) {
ab@2054: rt_task_wait_period(NULL);
ab@2054:
ab@2054: counter++;
ab@2054: if (counter>600000) {
ab@2054: run=0;
ab@2054: return;
ab@2054: }
ab@2054:
ab@2054: // receive ethercat
ab@2055: ecrt_rtdm_master_recieve(rt_fd);
ab@2055: ecrt_rtdm_domain_process(rt_fd);
ab@2055:
ab@2054: rt_check_domain_state();
ab@2054:
ab@2054: if (divcounter ==0)
ab@2054: {
ab@2054: divcounter=divider;
ab@2054: rt_check_master_state();
ab@2054: }
ab@2054: divcounter--;
ab@2054: if ((counter % 200)==0)
ab@2054: {
ab@2054: blink = !blink;
ab@2054:
ab@2054: }
ab@2054:
ab@2054:
ab@2054: EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0x0 : 0x0F);
ab@2054: EC_WRITE_U16(domain1_pd + off_ana_out0, blink ? 0x0: 0xfff);
ab@2054:
ab@2054: //sync DC
ab@2054: rt_sync();
ab@2054:
ab@2054: // send process data
ab@2055: ecrt_rtdm_domain_queque(rt_fd);
ab@2055: ecrt_rtdm_master_send(rt_fd);
ab@2054: }
ab@2054:
ab@2054: }
ab@2054:
ab@2054:
ab@2054: /**********************************************************/
ab@2054: /* CLEANING UP */
ab@2054: /**********************************************************/
ab@2054: void cleanup_all(void)
ab@2054: {
ab@2054: printf("delete my_task\n");
ab@2054: rt_task_delete(&my_task);
ab@2054:
ab@2054: if (rt_fd >= 0) {
ab@2054: printf("closing rt device %s\n", &rt_dev_file[0]);
ab@2054: rt_dev_close(rt_fd);
ab@2054:
ab@2054: }
ab@2054: }
ab@2054: /****************************************************************************/
ab@2054:
ab@2054: void catch_signal(int sig) {
ab@2054: cleanup_all();
ab@2054: printf("exit\n");
ab@2054: exit(0);
ab@2054: return;
ab@2054: }
ab@2054:
ab@2054:
ab@2054: /****************************************************************************/
ab@2054:
ab@2054: int main(int argc, char **argv)
ab@2054: {
ab@2054: ec_slave_config_t *sc;
ab@2054:
ab@2054: int rtstatus;
ab@2054:
ab@2054: mlockall(MCL_CURRENT | MCL_FUTURE);
ab@2054:
ab@2055: /* Perform auto-init of rt_print buffers if the task doesn't do so */
ab@2055: rt_print_auto_init(1);
ab@2055:
ab@2054: signal(SIGTERM, catch_signal);
ab@2054: signal(SIGINT, catch_signal);
ab@2054:
ab@2054: MstrAttach.masterindex = 0;
ab@2054:
ab@2054: printf("request master\n");
ab@2054: master = ecrt_request_master(MstrAttach.masterindex);
ab@2054: if (!master)
ab@2054: return -1;
ab@2054:
ab@2054:
ab@2054: domain1 = ecrt_master_create_domain(master);
ab@2054: if (!domain1)
ab@2054: return -1;
ab@2054:
ab@2054:
ab@2054: #ifdef CONFIGURE_PDOS
ab@2054:
ab@2054: printf("Configuring PDOs...\n");
ab@2054:
ab@2054: printf("Get Configuring el2004...\n");
ab@2054: sc_dig_out_01 = ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2004);
ab@2054: if (!sc_dig_out_01) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Configuring EL2004...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_1_syncs))
ab@2054: {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Get Configuring el2004...\n");
ab@2054: sc_dig_out_02 = ecrt_master_slave_config(master, DigOutSlave02_Pos, Beckhoff_EL2004);
ab@2054: if (!sc_dig_out_02) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Configuring EL2004...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_dig_out_02, EC_END, slave_2_syncs)) {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Get Configuring el1014...\n");
ab@2054: sc_dig_in_01 = ecrt_master_slave_config(master, DigInSlave01_Pos, Beckhoff_EL1014);
ab@2054: if (!sc_dig_in_01) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Configuring EL1014...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_dig_in_01, EC_END, slave_3_syncs)) {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Get Configuring EL4132...\n");
ab@2054: sc_ana_out_01 = ecrt_master_slave_config(master, AnaOutSlave01_Pos, Beckhoff_EL4132);
ab@2054: if (!sc_ana_out_01) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Configuring EL4132...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_ana_out_01, EC_END, slave_4_syncs)) {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Get Configuring EL3102...\n");
ab@2054: sc_ana_in_01 = ecrt_master_slave_config(master, AnaInSlave01_Pos, Beckhoff_EL3102);
ab@2054: if (!sc_ana_in_01) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Configuring EL3102...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_ana_in_01, EC_END, slave_5_syncs)) {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Get Configuring EL3602...\n");
ab@2054: sc_ana_in_02 = ecrt_master_slave_config(master, AnaInSlave02_Pos, Beckhoff_EL3602);
ab@2054: if (!sc_ana_in_02) {
ab@2054: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: // DP Slave Parameter Set
ab@2054:
ab@2054: /* printf( "Creating SDO requests...\n");
ab@2054: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in_02, 0x8000, 0x06, 1))) {
ab@2054: fprintf(stderr, "Failed to create SDO request.\n");
ab@2054: return -1;
ab@2054: }
ab@2054: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2054:
ab@2054: EC_WRITE_U8(ecrt_sdo_request_data(sdo), 00);
ab@2054: PrintSDOState();
ab@2054: ecrt_sdo_request_write(sdo);
ab@2054: PrintSDOState();
ab@2054:
ab@2054: */
ab@2054: printf("Configuring EL3602...\n");
ab@2054: if (ecrt_slave_config_pdos(sc_ana_in_02, EC_END, slave_7_syncs)) {
ab@2054: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: #endif
ab@2054:
ab@2054: // Create configuration for bus coupler
ab@2054: sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
ab@2054: if (!sc)
ab@2054: return -1;
ab@2054:
ab@2054: #ifdef CONFIGURE_PDOS
ab@2054: if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
ab@2054: fprintf(stderr, "PDO entry registration failed!\n");
ab@2054: return -1;
ab@2054: }
ab@2054: #endif
ab@2055:
ab@2055:
ab@2055: printf("Get Configuring EL6731...\n");
ab@2055: sc_dpslv_01 = ecrt_master_slave_config(master, DPSlave01_Pos, Beckhoff_EL6731);
ab@2055: if (!sc_dpslv_01) {
ab@2055: fprintf(stderr, "Failed to get slave configuration.\n");
ab@2055: return -1;
ab@2055: }
ab@2055:
ab@2055: printf("Configuring EL6731...\n");
ab@2055: if (ecrt_slave_config_pdos(sc_dpslv_01, EC_END, slave_7_syncs))
ab@2055: {
ab@2055: fprintf(stderr, "Failed to configure PDOs.\n");
ab@2055: return -1;
ab@2055: }
ab@2055:
ab@2055: #if SDO_ACCESS
ab@2055:
ab@2055:
ab@2055: // DP Slave Parameter Set
ab@2055: fprintf(stderr, "Creating SDO requests...\n");
ab@2055: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_dpslv_01, 0x8000, 0, 1))) {
ab@2055: fprintf(stderr, "Failed to create SDO request.\n");
ab@2055: return -1;
ab@2055: }
ab@2055: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2055: EC_WRITE_U8(ecrt_sdo_request_data(sdo), 0);
ab@2055: PrintSDOState();
ab@2055: ecrt_sdo_request_write(sdo);
ab@2055: PrintSDOState();
ab@2055:
ab@2055: // Station Address
ab@2055: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_dpslv_01, 0x8000, 1, 2))) {
ab@2055: fprintf(stderr, "Failed to create SDO request.\n");
ab@2055: return -1;
ab@2055: }
ab@2055: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2055: EC_WRITE_U16(ecrt_sdo_request_data(sdo), 5);
ab@2055: //EC_WRITE_U8(ecrt_sdo_request_data(sdo), 00);
ab@2055: //EC_WRITE_U8(ecrt_sdo_request_data(sdo)+1, 10);
ab@2055: PrintSDOState();
ab@2055: ecrt_sdo_request_write(sdo);
ab@2055: PrintSDOState();
ab@2055:
ab@2055: // Device Type (DP Ident Number)
ab@2055: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_dpslv_01, 0x8000, 4, 4))) {
ab@2055: fprintf(stderr, "Failed to create SDO request.\n");
ab@2055: return -1;
ab@2055: }
ab@2055: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2055: sdo_adr = ecrt_sdo_request_data(sdo);
ab@2055: EC_WRITE_U32(sdo_adr, 0x095F);
ab@2055: //EC_WRITE_U8(sdo_ad, 0x00); // Device Type
ab@2055: //EC_WRITE_U8(sdo_adr+1, 0x00);
ab@2055: //EC_WRITE_U8(sdo_adr+2, 0x09);
ab@2055: //EC_WRITE_U8(sdo_adr+3, 0x5F);
ab@2055: PrintSDOState();
ab@2055: ecrt_sdo_request_write(sdo);
ab@2055: PrintSDOState();
ab@2055:
ab@2055: // DP CfgData Slave
ab@2055: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_dpslv_01, 0x8002, 0, 244))) {
ab@2055: fprintf(stderr, "Failed to create SDO request.\n");
ab@2055: return -1;
ab@2055: }
ab@2055: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2055: sdo_adr = ecrt_sdo_request_data(sdo);
ab@2055: EC_WRITE_U8(sdo_adr, 0x10); // Device Type
ab@2055: EC_WRITE_U8(sdo_adr+1, 0x20);
ab@2055: PrintSDOState();
ab@2055: ecrt_sdo_request_write(sdo);
ab@2055: PrintSDOState();
ab@2055:
ab@2055: // DP Slave Parameter Set
ab@2055: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_dpslv_01, 0x8000, 0, 1))) {
ab@2055: fprintf(stderr, "Failed to create SDO request.\n");
ab@2055: return -1;
ab@2055: }
ab@2055:
ab@2055: ecrt_sdo_request_timeout(sdo, 500); // ms
ab@2055:
ab@2055: EC_WRITE_U8(ecrt_sdo_request_data(sdo), 0x33); // DP Slave Parameter Set
ab@2055: PrintSDOState();
ab@2055: ecrt_sdo_request_write(sdo);
ab@2055: PrintSDOState();
ab@2055: #endif
ab@2054:
ab@2054:
ab@2054:
ab@2054: sprintf(&rt_dev_file[0],"%s%u",EC_RTDM_DEV_FILE_NAME,0);
ab@2054:
ab@2054:
ab@2054: rt_fd = rt_dev_open( &rt_dev_file[0], 0);
ab@2054: if (rt_fd < 0) {
ab@2054: printf("can't open %s\n", &rt_dev_file[0]);
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: MstrAttach.domainindex = ecrt_domain_index(domain1);
ab@2054:
ab@2054: // attach the master over rtdm driver
ab@2054: rtstatus=ecrt_rtdm_master_attach(rt_fd, &MstrAttach);
ab@2054: if (rtstatus < 0)
ab@2054: {
ab@2054: printf("cannot attach to master over rtdm\n");
ab@2054: return -1;
ab@2054: }
ab@2054:
ab@2054: printf("Activating master...\n");
ab@2054: if (ecrt_master_activate(master))
ab@2054: return -1;
ab@2054:
ab@2054: if (!(domain1_pd = ecrt_domain_data(domain1))) {
ab@2054: return -1;
ab@2054: }
ab@2054: fprintf(stderr, "domain1_pd: 0x%.6x\n", (unsigned int)domain1_pd);
ab@2054:
ab@2054:
ab@2054:
ab@2054: int ret;
ab@2054: run=1;
ab@2054:
ab@2054: ret = rt_task_create(&my_task,"my_task",0,80,T_FPU);
ab@2054:
ab@2054: printf("starting my_task\n");
ab@2054: ret = rt_task_start(&my_task,&my_task_proc,NULL);
ab@2054:
ab@2054:
ab@2054: while (run)
ab@2054: {
ab@2054: sched_yield();
ab@2054: }
ab@2054:
ab@2054: rt_task_delete(&my_task);
ab@2054:
ab@2054:
ab@2054: if (rt_fd >= 0)
ab@2054: {
ab@2054: printf("closing rt device %s\n", &rt_dev_file[0]);
ab@2054:
ab@2054: rt_dev_close(rt_fd);
ab@2054:
ab@2054: }
ab@2054:
ab@2054: printf("End of Program\n");
ab@2054: ecrt_release_master(master);
ab@2054:
ab@2054: return 0;
ab@2054: }
ab@2054:
ab@2054: /****************************************************************************/