p@2555: /***************************************************************************** p@2555: * fp@2556: * $Id$ p@2555: * p@2555: * Copyright (C) 2007-2009 Florian Pose, Ingenieurgemeinschaft IgH p@2555: * p@2555: * This file is part of the IgH EtherCAT Master. p@2555: * p@2555: * The IgH EtherCAT Master is free software; you can redistribute it and/or p@2555: * modify it under the terms of the GNU General Public License version 2, as p@2555: * published by the Free Software Foundation. p@2555: * p@2555: * The IgH EtherCAT Master is distributed in the hope that it will be useful, p@2555: * but WITHOUT ANY WARRANTY; without even the implied warranty of p@2555: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General p@2555: * Public License for more details. p@2555: * p@2555: * You should have received a copy of the GNU General Public License along p@2555: * with the IgH EtherCAT Master; if not, write to the Free Software p@2555: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA p@2555: * p@2555: * --- p@2555: * p@2555: * The license mentioned above concerns the source code only. Using the p@2555: * EtherCAT technology and brand is only permitted in compliance with the p@2555: * industrial property and similar rights of Beckhoff Automation GmbH. p@2555: * p@2555: ****************************************************************************/ p@2555: p@2555: #include p@2555: #include p@2555: #include p@2555: #include p@2555: #include p@2555: #include p@2555: #include p@2555: #include p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: #include "ecrt.h" p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: // Application parameters p@2555: #define FREQUENCY 100 p@2555: #define PRIORITY 0 p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: // EtherCAT p@2555: static ec_master_t *master = NULL; p@2555: static ec_master_state_t master_state = {}; p@2555: p@2555: static ec_domain_t *domain1 = NULL; p@2555: static ec_domain_state_t domain1_state = {}; p@2555: p@2555: static ec_slave_config_t *sc_ana_in = NULL; p@2555: static ec_slave_config_state_t sc_ana_in_state = {}; p@2555: p@2555: // Timer p@2555: static unsigned int sig_alarms = 0; p@2555: static unsigned int user_alarms = 0; p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: // process data p@2555: static uint8_t *domain1_pd = NULL; p@2555: p@2555: #define BusCouplerPos 0, 3 p@2555: #define DigOutSlavePos 0, 0 p@2555: p@2555: //#define Beckhoff_EK1100 0x00000002, 0x044c2c52 p@2555: #define Beckhoff_EK1100 0x00000002, 0x04562c52 p@2555: #define Beckhoff_EL1008 0x00000002, 0x03f03052 p@2555: #define Beckhoff_EL2004 0x00000002, 0x07d43052 p@2555: #define Beckhoff_EL2008 0x00000002, 0x07d83052 p@2555: #define Beckhoff_EL2032 0x00000002, 0x07f03052 p@2555: #define Beckhoff_EL3152 0x00000002, 0x0c503052 p@2555: #define Beckhoff_EL3102 0x00000002, 0x0c1e3052 p@2555: #define Beckhoff_EL4102 0x00000002, 0x10063052 p@2555: p@2555: // offsets for PDO entries p@2555: static unsigned int off_dig_in[1]; p@2555: static unsigned int off_dig_out[2]; p@2555: p@2555: p@2555: /*****************************************************************************/ p@2555: // Digital in ------------------------ p@2555: static ec_pdo_entry_info_t el1008_channels[] = { p@2555: {0x6000, 1, 1}, p@2555: {0x6010, 1, 1}, p@2555: {0x6020, 1, 1}, p@2555: {0x6030, 1, 1}, p@2555: {0x6040, 1, 1}, p@2555: {0x6050, 1, 1}, p@2555: {0x6060, 1, 1}, p@2555: {0x6070, 1, 1}, p@2555: }; p@2555: p@2555: static ec_pdo_info_t el1008_pdos[] = { p@2555: {0x1a00, 1, &el1008_channels[0]}, p@2555: {0x1a01, 1, &el1008_channels[1]}, p@2555: {0x1a02, 1, &el1008_channels[2]}, p@2555: {0x1a03, 1, &el1008_channels[3]}, p@2555: {0x1a04, 1, &el1008_channels[4]}, p@2555: {0x1a05, 1, &el1008_channels[5]}, p@2555: {0x1a06, 1, &el1008_channels[6]}, p@2555: {0x1a07, 1, &el1008_channels[7]} p@2555: }; p@2555: p@2555: static ec_sync_info_t el1008_syncs[] = { p@2555: {2, EC_DIR_OUTPUT}, p@2555: {3, EC_DIR_INPUT, 8, el1008_pdos}, p@2555: {0xff} p@2555: }; p@2555: p@2555: // Digital out ------------------------ p@2555: static ec_pdo_entry_info_t el2008_channels[] = { p@2555: {0x7000, 1, 1}, p@2555: {0x7010, 1, 1}, p@2555: {0x7020, 1, 1}, p@2555: {0x7030, 1, 1}, p@2555: {0x7040, 1, 1}, p@2555: {0x7050, 1, 1}, p@2555: {0x7060, 1, 1}, p@2555: {0x7070, 1, 1}, p@2555: }; p@2555: p@2555: static ec_pdo_info_t el2008_pdos[] = { p@2555: {0x1600, 1, &el2008_channels[0]}, p@2555: {0x1601, 1, &el2008_channels[1]}, p@2555: {0x1602, 1, &el2008_channels[2]}, p@2555: {0x1603, 1, &el2008_channels[3]}, p@2555: {0x1604, 1, &el2008_channels[4]}, p@2555: {0x1605, 1, &el2008_channels[5]}, p@2555: {0x1606, 1, &el2008_channels[6]}, p@2555: {0x1607, 1, &el2008_channels[7]} p@2555: }; p@2555: p@2555: static ec_sync_info_t el2008_syncs[] = { p@2555: {0, EC_DIR_OUTPUT, 8, el2008_pdos}, p@2555: {1, EC_DIR_INPUT}, p@2555: {0xff} p@2555: }; p@2555: p@2555: /*****************************************************************************/ p@2555: p@2555: void check_domain1_state(void) p@2555: { p@2555: ec_domain_state_t ds; p@2555: p@2555: ecrt_domain_state(domain1, &ds); p@2555: p@2555: if (ds.working_counter != domain1_state.working_counter) p@2555: printf("Domain1: WC %u.\n", ds.working_counter); p@2555: if (ds.wc_state != domain1_state.wc_state) p@2555: printf("Domain1: State %u.\n", ds.wc_state); p@2555: p@2555: domain1_state = ds; p@2555: } p@2555: p@2555: /*****************************************************************************/ p@2555: p@2555: void check_master_state(void) p@2555: { p@2555: ec_master_state_t ms; p@2555: p@2555: ecrt_master_state(master, &ms); p@2555: p@2555: if (ms.slaves_responding != master_state.slaves_responding) p@2555: printf("%u slave(s).\n", ms.slaves_responding); p@2555: if (ms.al_states != master_state.al_states) p@2555: printf("AL states: 0x%02X.\n", ms.al_states); p@2555: if (ms.link_up != master_state.link_up) p@2555: printf("Link is %s.\n", ms.link_up ? "up" : "down"); p@2555: p@2555: master_state = ms; p@2555: } p@2555: p@2555: /*****************************************************************************/ p@2555: p@2555: void check_slave_config_states(void) p@2555: { p@2555: ec_slave_config_state_t s; p@2555: p@2555: ecrt_slave_config_state(sc_ana_in, &s); p@2555: p@2555: if (s.al_state != sc_ana_in_state.al_state) p@2555: printf("AnaIn: State 0x%02X.\n", s.al_state); p@2555: if (s.online != sc_ana_in_state.online) p@2555: printf("AnaIn: %s.\n", s.online ? "online" : "offline"); p@2555: if (s.operational != sc_ana_in_state.operational) p@2555: printf("AnaIn: %soperational.\n", p@2555: s.operational ? "" : "Not "); p@2555: p@2555: sc_ana_in_state = s; p@2555: } p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: void cyclic_task() p@2555: { p@2555: static unsigned int counter = 10; p@2555: static uint8_t outputValue = 0; p@2555: static int numAsyncCycles = 0; p@2555: uint8_t inputValue = 0; p@2555: static uint8_t error = 0; p@2555: p@2555: // receive process data p@2555: ecrt_master_receive(master); p@2555: ecrt_domain_process(domain1); p@2555: p@2555: // check process data state (optional) p@2555: check_domain1_state(); p@2555: p@2555: p@2555: inputValue = EC_READ_U8(domain1_pd + off_dig_in[0]); p@2555: p@2555: if(inputValue != outputValue) { p@2555: numAsyncCycles++; p@2555: } else { p@2555: numAsyncCycles = 0; p@2555: } p@2555: p@2555: if(numAsyncCycles > 2) { p@2555: if(error != 0xff) { p@2555: error++; p@2555: } p@2555: } p@2555: p@2555: p@2555: if (counter) { p@2555: counter--; p@2555: } else { p@2555: counter = 5; //update delay p@2555: p@2555: p@2555: p@2555: // calculate new process data p@2555: outputValue++; p@2555: p@2555: // check for master state (optional) p@2555: check_master_state(); p@2555: p@2555: // check for islave configuration state(s) (optional) p@2555: check_slave_config_states(); p@2555: } p@2555: p@2555: // write process data p@2555: EC_WRITE_U8(domain1_pd + off_dig_out[1], outputValue); p@2555: EC_WRITE_U8(domain1_pd + off_dig_out[0], error); p@2555: p@2555: // send process data p@2555: ecrt_domain_queue(domain1); p@2555: ecrt_master_send(master); p@2555: } p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: void signal_handler(int signum) { p@2555: switch (signum) { p@2555: case SIGALRM: p@2555: sig_alarms++; p@2555: break; p@2555: } p@2555: } p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: int Init_EL2008(uint16_t position) p@2555: { p@2555: ec_slave_config_t *sc; p@2555: if (!(sc = ecrt_master_slave_config(master, 0, position, Beckhoff_EL2008))) { p@2555: fprintf(stderr, "Failed to get EL2008 configuration #%u.\n", position); p@2555: return -1; p@2555: } p@2555: if (ecrt_slave_config_pdos(sc, EC_END, el2008_syncs)) { p@2555: fprintf(stderr, "Failed to configure PDOs #%u.\n", position); p@2555: return -1; p@2555: } p@2555: if (0 > (off_dig_out[position] = ecrt_slave_config_reg_pdo_entry(sc, 0x7000, 1, domain1, NULL))) { p@2555: fprintf(stderr, "Failed to configure reg PDOs #%u.\n", position); p@2555: return -1; p@2555: } p@2555: fprintf(stderr, "EL2008 #%u configured offset: %d.\n", position, off_dig_out[position]); p@2555: return 0; p@2555: } p@2555: p@2555: int main(int argc, char **argv) p@2555: { p@2555: ec_slave_config_t *sc; p@2555: struct sigaction sa; p@2555: struct itimerval tv; p@2555: uint16_t i; p@2555: p@2555: master = ecrt_request_master(0); p@2555: if (!master) p@2555: return -1; p@2555: p@2555: domain1 = ecrt_master_create_domain(master); p@2555: if (!domain1) p@2555: return -1; p@2555: p@2555: printf("Configuring PDOs...\n"); p@2555: if (!(sc_ana_in = ecrt_master_slave_config(master, 0, 2, Beckhoff_EL1008))) { p@2555: fprintf(stderr, "Failed to get digital in configuration.\n"); p@2555: return -1; p@2555: } p@2555: if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el1008_syncs)) { p@2555: fprintf(stderr, "Failed to configure PDOs.\n"); p@2555: return -1; p@2555: } p@2555: if (0 > (off_dig_in[0] = ecrt_slave_config_reg_pdo_entry(sc_ana_in, 0x6000, 1, domain1, NULL))) { p@2555: fprintf(stderr, "Failed to configure reg PDOs.\n"); p@2555: return -1; p@2555: } p@2555: printf("EL1008 configured.\n"); p@2555: p@2555: for(i = 0; i < 2; ++i) { p@2555: if(Init_EL2008(i)) { p@2555: fprintf(stderr, "Failed to initialize EL2008 #%u.\n", i); p@2555: return -1; p@2555: } p@2555: } p@2555: p@2555: // Create configuration for bus coupler p@2555: sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); p@2555: if (!sc) p@2555: return -1; p@2555: fprintf(stderr, "EK1100 configured.\n"); p@2555: p@2555: printf("Activating master...\n"); p@2555: if (ecrt_master_activate(master)) p@2555: return -1; p@2555: p@2555: if (!(domain1_pd = ecrt_domain_data(domain1))) { p@2555: return -1; p@2555: } p@2555: p@2555: #if PRIORITY p@2555: pid_t pid = getpid(); p@2555: if (setpriority(PRIO_PROCESS, pid, -19)) p@2555: fprintf(stderr, "Warning: Failed to set priority: %s\n", p@2555: strerror(errno)); p@2555: #endif p@2555: p@2555: sa.sa_handler = signal_handler; p@2555: sigemptyset(&sa.sa_mask); p@2555: sa.sa_flags = 0; p@2555: if (sigaction(SIGALRM, &sa, 0)) { p@2555: fprintf(stderr, "Failed to install signal handler!\n"); p@2555: return -1; p@2555: } p@2555: p@2555: printf("Starting timer...\n"); p@2555: tv.it_interval.tv_sec = 0; p@2555: tv.it_interval.tv_usec = 1000000 / FREQUENCY; p@2555: tv.it_value.tv_sec = 0; p@2555: tv.it_value.tv_usec = 1000; p@2555: if (setitimer(ITIMER_REAL, &tv, NULL)) { p@2555: fprintf(stderr, "Failed to start timer: %s\n", strerror(errno)); p@2555: return 1; p@2555: } p@2555: p@2555: printf("Started.\n"); p@2555: while (1) { p@2555: pause(); p@2555: p@2555: #if 0 p@2555: struct timeval t; p@2555: gettimeofday(&t, NULL); p@2555: printf("%u.%06u\n", t.tv_sec, t.tv_usec); p@2555: #endif p@2555: p@2555: while (sig_alarms != user_alarms) { p@2555: cyclic_task(); p@2555: user_alarms++; p@2555: } p@2555: } p@2555: p@2555: return 0; p@2555: } p@2555: p@2555: /****************************************************************************/