fp@1242: /***************************************************************************** fp@1242: * fp@1255: * $Id$ fp@1255: * fp@1326: * Copyright (C) 2007-2009 Florian Pose, Ingenieurgemeinschaft IgH fp@1255: * fp@1255: * This file is part of the IgH EtherCAT Master. fp@1255: * fp@1326: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1326: * modify it under the terms of the GNU General Public License version 2, as fp@1326: * published by the Free Software Foundation. fp@1326: * fp@1326: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1326: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1326: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1326: * Public License for more details. fp@1326: * fp@1326: * You should have received a copy of the GNU General Public License along fp@1326: * 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@1363: * --- fp@1363: * fp@1363: * The license mentioned above concerns the source code only. Using the fp@1363: * EtherCAT technology and brand is only permitted in compliance with the fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH. 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@1258: // Application parameters fp@1259: #define FREQUENCY 100 fp@1258: #define PRIORITY 1 fp@1258: fp@1255: // Optional features fp@1255: #define CONFIGURE_PDOS 1 fp@1352: #define SDO_ACCESS 0 fp@1258: fp@1258: /****************************************************************************/ fp@1258: fp@1258: // EtherCAT fp@1258: static ec_master_t *master = NULL; fp@1259: static ec_master_state_t master_state = {}; fp@1258: fp@1258: static ec_domain_t *domain1 = NULL; fp@1259: static ec_domain_state_t domain1_state = {}; fp@1258: fp@1258: static ec_slave_config_t *sc_ana_in = NULL; fp@1259: static ec_slave_config_state_t sc_ana_in_state = {}; fp@1258: fp@1258: // Timer fp@1258: static unsigned int sig_alarms = 0; fp@1258: static unsigned int user_alarms = 0; fp@1258: fp@1258: /****************************************************************************/ fp@1258: fp@1258: // process data fp@1258: static uint8_t *domain1_pd = NULL; fp@1249: fp@1255: #define BusCouplerPos 0, 0 fp@1352: #define DigOutSlavePos 0, 2 fp@1352: #define AnaInSlavePos 0, 3 fp@1352: #define AnaOutSlavePos 0, 4 fp@1255: fp@1259: #define Beckhoff_EK1100 0x00000002, 0x044c2c52 fp@1259: #define Beckhoff_EL2004 0x00000002, 0x07d43052 fp@1259: #define Beckhoff_EL2032 0x00000002, 0x07f03052 fp@1255: #define Beckhoff_EL3152 0x00000002, 0x0c503052 fp@1352: #define Beckhoff_EL3102 0x00000002, 0x0c1e3052 fp@1255: #define Beckhoff_EL4102 0x00000002, 0x10063052 fp@1255: fp@1327: // offsets for PDO entries fp@1258: static unsigned int off_ana_in_status; fp@1258: static unsigned int off_ana_in_value; 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@1352: {AnaInSlavePos, Beckhoff_EL3102, 0x3101, 1, &off_ana_in_status}, fp@1352: {AnaInSlavePos, Beckhoff_EL3102, 0x3101, 2, &off_ana_in_value}, fp@1508: {AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out}, fp@1508: {DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out}, fp@1508: {} fp@1255: }; fp@1255: fp@1259: static unsigned int counter = 0; fp@1259: static unsigned int blink = 0; fp@1259: fp@1255: /*****************************************************************************/ fp@1255: fp@1255: #if CONFIGURE_PDOS fp@1255: fp@1255: // Analog in -------------------------- fp@1255: fp@1352: static ec_pdo_entry_info_t el3102_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@1352: static ec_pdo_info_t el3102_pdos[] = { fp@1352: {0x1A00, 2, el3102_pdo_entries}, fp@1352: {0x1A01, 2, el3102_pdo_entries + 2} fp@1352: }; fp@1352: fp@1352: static ec_sync_info_t el3102_syncs[] = { fp@1255: {2, EC_DIR_OUTPUT}, fp@1352: {3, EC_DIR_INPUT, 2, el3102_pdos}, fp@1255: {0xff} fp@1255: }; 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@1259: /*****************************************************************************/ fp@1259: fp@1352: #if SDO_ACCESS fp@1352: static ec_sdo_request_t *sdo; fp@1352: #endif fp@1352: fp@1352: /*****************************************************************************/ fp@1352: fp@1259: void check_domain1_state(void) fp@1259: { fp@1259: ec_domain_state_t ds; fp@1259: fp@1259: ecrt_domain_state(domain1, &ds); fp@1259: fp@1259: if (ds.working_counter != domain1_state.working_counter) fp@1259: printf("Domain1: WC %u.\n", ds.working_counter); fp@1259: if (ds.wc_state != domain1_state.wc_state) fp@1259: printf("Domain1: State %u.\n", ds.wc_state); fp@1259: fp@1259: domain1_state = ds; fp@1259: } fp@1259: fp@1259: /*****************************************************************************/ fp@1259: fp@1259: void check_master_state(void) fp@1259: { fp@1259: ec_master_state_t ms; fp@1259: fp@1259: ecrt_master_state(master, &ms); fp@1259: fp@1259: if (ms.slaves_responding != master_state.slaves_responding) fp@1259: printf("%u slave(s).\n", ms.slaves_responding); fp@1259: if (ms.al_states != master_state.al_states) fp@1259: printf("AL states: 0x%02X.\n", ms.al_states); fp@1259: if (ms.link_up != master_state.link_up) fp@1259: printf("Link is %s.\n", ms.link_up ? "up" : "down"); fp@1259: fp@1259: master_state = ms; fp@1259: } fp@1259: fp@1259: /*****************************************************************************/ fp@1259: fp@1259: void check_slave_config_states(void) fp@1259: { fp@1259: ec_slave_config_state_t s; fp@1259: fp@1259: ecrt_slave_config_state(sc_ana_in, &s); fp@1259: fp@1259: if (s.al_state != sc_ana_in_state.al_state) fp@1259: printf("AnaIn: State 0x%02X.\n", s.al_state); fp@1259: if (s.online != sc_ana_in_state.online) fp@1259: printf("AnaIn: %s.\n", s.online ? "online" : "offline"); fp@1259: if (s.operational != sc_ana_in_state.operational) fp@1259: printf("AnaIn: %soperational.\n", fp@1259: s.operational ? "" : "Not "); fp@1259: fp@1259: sc_ana_in_state = s; fp@1259: } fp@1259: fp@1352: /*****************************************************************************/ fp@1352: fp@1352: #if SDO_ACCESS fp@1352: void read_sdo(void) fp@1352: { fp@1352: switch (ecrt_sdo_request_state(sdo)) { fp@1352: case EC_REQUEST_UNUSED: // request was not used yet fp@1352: ecrt_sdo_request_read(sdo); // trigger first read fp@1352: break; fp@1352: case EC_REQUEST_BUSY: fp@1352: fprintf(stderr, "Still busy...\n"); fp@1352: break; fp@1352: case EC_REQUEST_SUCCESS: fp@1352: fprintf(stderr, "SDO value: 0x%04X\n", fp@1352: EC_READ_U16(ecrt_sdo_request_data(sdo))); fp@1352: ecrt_sdo_request_read(sdo); // trigger next read fp@1352: break; fp@1352: case EC_REQUEST_ERROR: fp@1352: fprintf(stderr, "Failed to read SDO!\n"); fp@1352: ecrt_sdo_request_read(sdo); // retry reading fp@1352: break; fp@1352: } fp@1352: } fp@1352: #endif fp@1352: fp@1247: /****************************************************************************/ fp@1247: fp@1258: void cyclic_task() fp@1258: { fp@1259: // receive process data fp@1258: ecrt_master_receive(master); fp@1258: ecrt_domain_process(domain1); fp@1258: fp@1259: // check process data state (optional) fp@1259: check_domain1_state(); fp@1259: fp@1259: if (counter) { fp@1259: counter--; fp@1259: } else { // do this at 1 Hz fp@1259: counter = FREQUENCY; fp@1259: fp@1259: // calculate new process data fp@1259: blink = !blink; fp@1259: fp@1259: // check for master state (optional) fp@1259: check_master_state(); fp@1259: fp@1259: // check for islave configuration state(s) (optional) fp@1259: check_slave_config_states(); fp@1352: fp@1352: #if SDO_ACCESS fp@1352: // read process data SDO fp@1352: read_sdo(); fp@1352: #endif fp@1352: fp@1259: } fp@1259: fp@1258: #if 0 fp@1259: // read process data fp@1258: printf("AnaIn: state %u value %u\n", fp@1258: EC_READ_U8(domain1_pd + off_ana_in_status), fp@1258: EC_READ_U16(domain1_pd + off_ana_in_value)); fp@1258: #endif fp@1259: fp@1259: #if 1 fp@1259: // write process data fp@1259: EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09); fp@1259: #endif fp@1259: fp@1259: // send process data fp@1258: ecrt_domain_queue(domain1); fp@1258: ecrt_master_send(master); fp@1258: } fp@1258: fp@1258: /****************************************************************************/ fp@1258: 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@1508: ec_slave_config_t *sc; fp@1247: struct sigaction sa; fp@1247: struct itimerval tv; fp@2421: fp@1244: master = ecrt_request_master(0); fp@1508: if (!master) fp@1508: 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@1352: master, AnaInSlavePos, Beckhoff_EL3102))) { fp@1255: fprintf(stderr, "Failed to get slave configuration.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1352: #if SDO_ACCESS fp@1352: fprintf(stderr, "Creating SDO requests...\n"); fp@1352: if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) { fp@1352: fprintf(stderr, "Failed to create SDO request.\n"); fp@1352: return -1; fp@1352: } fp@1352: ecrt_sdo_request_timeout(sdo, 500); // ms fp@1352: #endif fp@1352: fp@1255: #if CONFIGURE_PDOS fp@1327: printf("Configuring PDOs...\n"); fp@1352: if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3102_syncs)) { fp@1327: 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@1327: fprintf(stderr, "Failed to configure PDOs.\n"); fp@1255: return -1; fp@1255: } fp@1255: fp@1255: if (!(sc = ecrt_master_slave_config( fp@1259: master, DigOutSlavePos, Beckhoff_EL2032))) { 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@1327: 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@1327: fprintf(stderr, "PDO entry registration failed!\n"); fp@1508: 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@1258: if (!(domain1_pd = ecrt_domain_data(domain1))) { fp@1258: return -1; fp@1258: } fp@1258: 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@1259: tv.it_interval.tv_usec = 1000000 / FREQUENCY; 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@1508: while (1) { fp@1261: pause(); 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@1258: cyclic_task(); fp@1247: user_alarms++; fp@1247: } fp@1508: } fp@1508: fp@1508: return 0; fp@1508: } fp@1508: fp@1508: /****************************************************************************/