fp@1242: /***************************************************************************** fp@1242: * fp@1255: * $Id$ fp@1255: * fp@1255: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1255: * fp@1255: * This file is part of the IgH EtherCAT Master. fp@1255: * fp@1255: * The IgH EtherCAT Master is free software; you can redistribute it fp@1255: * and/or modify it under the terms of the GNU General Public License fp@1255: * as published by the Free Software Foundation; either version 2 of the fp@1255: * License, or (at your option) any later version. fp@1255: * fp@1255: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1255: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1255: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1255: * GNU General Public License for more details. fp@1255: * fp@1255: * You should have received a copy of the GNU General Public License fp@1255: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1255: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1255: * fp@1255: * The right to use EtherCAT Technology is granted and comes free of fp@1255: * charge under condition of compatibility of product made by fp@1255: * Licensee. People intending to distribute/sell products based on the fp@1255: * code, have to sign an agreement to guarantee that products using fp@1255: * software based on IgH EtherCAT master stay compatible with the actual fp@1255: * EtherCAT specification (which are released themselves as an open fp@1255: * standard) as the (only) precondition to have the right to use EtherCAT fp@1255: * Technology, IP and trade marks. fp@1242: * fp@1242: ****************************************************************************/ fp@1242: fp@1249: #include fp@1249: #include fp@1249: #include fp@1249: #include fp@1249: #include fp@1249: #include fp@1249: #include fp@1247: #include fp@1247: fp@1255: /****************************************************************************/ fp@1255: fp@1242: #include "ecrt.h" fp@1242: fp@1255: /****************************************************************************/ fp@1255: fp@1255: // Optional features fp@1255: #define CONFIGURE_PDOS 1 fp@1255: #define EL3152_ALT_PDOS 0 fp@1255: #define SDO_ACCESS 0 fp@1255: #define VOE_ACCESS 0 fp@1255: fp@1249: #define PRIORITY 1 fp@1249: fp@1255: #define BusCouplerPos 0, 0 fp@1255: #define AnaOutSlavePos 0, 1 fp@1255: #define AnaInSlavePos 0, 2 fp@1255: #define DigOutSlavePos 0, 3 fp@1255: fp@1255: #define Beckhoff_EK1100 0x00000002, 0x044C2C52 fp@1255: #define Beckhoff_EL2004 0x00000002, 0x07D43052 fp@1255: #define Beckhoff_EL3152 0x00000002, 0x0c503052 fp@1255: #define Beckhoff_EL4102 0x00000002, 0x10063052 fp@1255: fp@1242: /****************************************************************************/ fp@1242: fp@1247: static unsigned int sig_alarms = 0; fp@1247: static unsigned int user_alarms = 0; fp@1247: fp@1255: // offsets for Pdo entries fp@1255: static unsigned int off_ana_in; fp@1255: static unsigned int off_ana_out; fp@1255: static unsigned int off_dig_out; fp@1255: fp@1255: const static ec_pdo_entry_reg_t domain1_regs[] = { fp@1255: #if EL3152_ALT_PDOS fp@1255: {AnaInSlavePos, Beckhoff_EL3152, 0x6401, 1, &off_ana_in}, fp@1255: #else fp@1255: {AnaInSlavePos, Beckhoff_EL3152, 0x3101, 2, &off_ana_in}, fp@1255: #endif fp@1255: {AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out}, fp@1255: {DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out}, fp@1255: {} fp@1255: }; fp@1255: fp@1255: static ec_slave_config_t *sc_ana_in = NULL; fp@1255: fp@1255: /*****************************************************************************/ fp@1255: fp@1255: #if CONFIGURE_PDOS fp@1255: fp@1255: // Analog in -------------------------- fp@1255: fp@1255: static ec_pdo_entry_info_t el3152_pdo_entries[] = { fp@1255: {0x3101, 1, 8}, // channel 1 status fp@1255: {0x3101, 2, 16}, // channel 1 value fp@1255: {0x3102, 1, 8}, // channel 2 status fp@1255: {0x3102, 2, 16}, // channel 2 value fp@1255: {0x6401, 1, 16}, // channel 1 value (alt.) fp@1255: {0x6401, 2, 16} // channel 2 value (alt.) fp@1255: }; fp@1255: fp@1255: #if EL3152_ALT_PDOS fp@1255: static ec_pdo_info_t el3152_pdos[] = { fp@1255: {0x1A10, 2, el3152_pdo_entries + 4}, fp@1255: }; fp@1255: fp@1255: static ec_sync_info_t el3152_syncs[] = { fp@1255: {2, EC_DIR_OUTPUT}, fp@1255: {3, EC_DIR_INPUT, 1, el3152_pdos}, fp@1255: {0xff} fp@1255: }; fp@1255: #else fp@1255: static ec_pdo_info_t el3152_pdos[] = { fp@1255: {0x1A00, 2, el3152_pdo_entries}, fp@1255: {0x1A01, 2, el3152_pdo_entries + 2} fp@1255: }; fp@1255: fp@1255: static ec_sync_info_t el3152_syncs[] = { fp@1255: {2, EC_DIR_OUTPUT}, fp@1255: {3, EC_DIR_INPUT, 2, el3152_pdos}, fp@1255: {0xff} fp@1255: }; fp@1255: #endif fp@1255: fp@1255: // Analog out ------------------------- fp@1255: fp@1255: static ec_pdo_entry_info_t el4102_pdo_entries[] = { fp@1255: {0x3001, 1, 16}, // channel 1 value fp@1255: {0x3002, 1, 16}, // channel 2 value fp@1255: }; fp@1255: fp@1255: static ec_pdo_info_t el4102_pdos[] = { fp@1255: {0x1600, 1, el4102_pdo_entries}, fp@1255: {0x1601, 1, el4102_pdo_entries + 1} fp@1255: }; fp@1255: fp@1255: static ec_sync_info_t el4102_syncs[] = { fp@1255: {2, EC_DIR_OUTPUT, 2, el4102_pdos}, fp@1255: {3, EC_DIR_INPUT}, fp@1255: {0xff} fp@1255: }; fp@1255: fp@1255: // Digital out ------------------------ fp@1255: fp@1255: static ec_pdo_entry_info_t el2004_channels[] = { fp@1255: {0x3001, 1, 1}, // Value 1 fp@1255: {0x3001, 2, 1}, // Value 2 fp@1255: {0x3001, 3, 1}, // Value 3 fp@1255: {0x3001, 4, 1} // Value 4 fp@1255: }; fp@1255: fp@1255: static ec_pdo_info_t el2004_pdos[] = { fp@1255: {0x1600, 1, &el2004_channels[0]}, fp@1255: {0x1601, 1, &el2004_channels[1]}, fp@1255: {0x1602, 1, &el2004_channels[2]}, fp@1255: {0x1603, 1, &el2004_channels[3]} fp@1255: }; fp@1255: fp@1255: static ec_sync_info_t el2004_syncs[] = { fp@1255: {0, EC_DIR_OUTPUT, 4, el2004_pdos}, fp@1255: {1, EC_DIR_INPUT}, fp@1255: {0xff} fp@1255: }; fp@1255: #endif fp@1255: fp@1247: /****************************************************************************/ fp@1247: fp@1247: void signal_handler(int signum) { fp@1247: switch (signum) { fp@1247: case SIGALRM: fp@1247: sig_alarms++; fp@1247: break; fp@1247: } fp@1247: } fp@1247: fp@1247: /****************************************************************************/ fp@1247: fp@1242: int main(int argc, char **argv) fp@1242: { fp@1244: ec_master_t *master; fp@1255: ec_domain_t *domain1; fp@1246: ec_slave_config_t *sc; fp@1247: struct sigaction sa; fp@1247: struct itimerval tv; fp@1244: fp@1244: master = ecrt_request_master(0); fp@1242: if (!master) fp@1242: return -1; fp@1242: fp@1255: domain1 = ecrt_master_create_domain(master); fp@1255: if (!domain1) fp@1255: return -1; fp@1255: fp@1255: if (!(sc_ana_in = ecrt_master_slave_config( fp@1255: master, AnaInSlavePos, Beckhoff_EL3152))) { fp@1255: fprintf(stderr, "Failed to get slave configuration.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: #if CONFIGURE_PDOS fp@1255: printf("Configuring Pdos...\n"); fp@1255: if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3152_syncs)) { fp@1255: fprintf(stderr, "Failed to configure Pdos.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: if (!(sc = ecrt_master_slave_config( fp@1255: master, AnaOutSlavePos, Beckhoff_EL4102))) { fp@1255: fprintf(stderr, "Failed to get slave configuration.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: if (ecrt_slave_config_pdos(sc, EC_END, el4102_syncs)) { fp@1255: fprintf(stderr, "Failed to configure Pdos.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: if (!(sc = ecrt_master_slave_config( fp@1255: master, DigOutSlavePos, Beckhoff_EL2004))) { fp@1255: fprintf(stderr, "Failed to get slave configuration.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) { fp@1255: fprintf(stderr, "Failed to configure Pdos.\n"); fp@1255: return -1; fp@1255: } fp@1255: #endif fp@1255: fp@1255: // Create configuration for bus coupler fp@1255: sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); fp@1246: if (!sc) fp@1246: return -1; fp@1246: fp@1255: if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) { fp@1255: fprintf(stderr, "Pdo entry registration failed!\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1249: printf("Activating master...\n"); fp@1247: if (ecrt_master_activate(master)) fp@1247: return -1; fp@1247: fp@1249: #if PRIORITY fp@1249: pid_t pid = getpid(); fp@1249: if (setpriority(PRIO_PROCESS, pid, -19)) fp@1249: fprintf(stderr, "Warning: Failed to set priority: %s\n", fp@1249: strerror(errno)); fp@1249: #endif fp@1249: fp@1247: sa.sa_handler = signal_handler; fp@1247: sigemptyset(&sa.sa_mask); fp@1247: sa.sa_flags = 0; fp@1247: if (sigaction(SIGALRM, &sa, 0)) { fp@1247: fprintf(stderr, "Failed to install signal handler!\n"); fp@1247: return -1; fp@1247: } fp@1247: fp@1249: printf("Starting timer...\n"); fp@1247: tv.it_interval.tv_sec = 0; fp@1255: tv.it_interval.tv_usec = 10000; fp@1247: tv.it_value.tv_sec = 0; fp@1249: tv.it_value.tv_usec = 1000; fp@1247: if (setitimer(ITIMER_REAL, &tv, NULL)) { fp@1247: fprintf(stderr, "Failed to start timer: %s\n", strerror(errno)); fp@1247: return 1; fp@1247: } fp@1247: fp@1249: printf("Started.\n"); fp@1242: while (1) { fp@1247: sleep(1); fp@1247: fp@1255: #if 0 fp@1249: struct timeval t; fp@1249: gettimeofday(&t, NULL); fp@1249: printf("%u.%06u\n", t.tv_sec, t.tv_usec); fp@1249: #endif fp@1249: fp@1247: while (sig_alarms != user_alarms) { fp@1247: ecrt_master_receive(master); fp@1247: ecrt_master_send(master); fp@1247: fp@1247: user_alarms++; fp@1247: } fp@1242: } fp@1242: fp@1242: return 0; fp@1242: } fp@1242: fp@1242: /****************************************************************************/