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 fp@2664: #include /* clock_gettime() */ fp@2664: #include /* mlockall() */ Florian@2697: #include /* sched_setscheduler() */ p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: #include "ecrt.h" p@2555: p@2555: /****************************************************************************/ p@2555: fp@2664: /** Task period in ns. */ fp@2664: #define PERIOD_NS (1000000) fp@2664: fp@2664: #define MAX_SAFE_STACK (8 * 1024) /* The maximum stack size which is fp@2664: guranteed safe to access without fp@2664: faulting */ fp@2664: fp@2664: /****************************************************************************/ fp@2664: fp@2664: /* Constants */ fp@2664: #define NSEC_PER_SEC (1000000000) fp@2664: #define FREQUENCY (NSEC_PER_SEC / PERIOD_NS) 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: /****************************************************************************/ p@2555: p@2555: // process data p@2555: static uint8_t *domain1_pd = NULL; p@2555: p@2565: #define BusCouplerPos 0, 0 p@2565: #define DigOutSlavePos 0, 2 p@2565: #define AnaInSlavePos 0, 3 p@2565: #define AnaOutSlavePos 0, 4 p@2565: p@2565: #define Beckhoff_EK1100 0x00000002, 0x044c2c52 p@2555: #define Beckhoff_EL2004 0x00000002, 0x07d43052 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@2565: static unsigned int off_ana_in_status; p@2565: static unsigned int off_ana_in_value; p@2565: static unsigned int off_ana_out; p@2565: static unsigned int off_dig_out; p@2565: p@2565: const static ec_pdo_entry_reg_t domain1_regs[] = { p@2565: {AnaInSlavePos, Beckhoff_EL3102, 0x3101, 1, &off_ana_in_status}, p@2565: {AnaInSlavePos, Beckhoff_EL3102, 0x3101, 2, &off_ana_in_value}, p@2565: {AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out}, p@2565: {DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out}, p@2565: {} p@2565: }; p@2565: p@2565: static unsigned int counter = 0; p@2565: static unsigned int blink = 0; p@2565: p@2565: /*****************************************************************************/ p@2565: p@2565: // Analog in -------------------------- p@2565: p@2565: static ec_pdo_entry_info_t el3102_pdo_entries[] = { p@2565: {0x3101, 1, 8}, // channel 1 status p@2565: {0x3101, 2, 16}, // channel 1 value p@2565: {0x3102, 1, 8}, // channel 2 status p@2565: {0x3102, 2, 16}, // channel 2 value p@2565: {0x6401, 1, 16}, // channel 1 value (alt.) p@2565: {0x6401, 2, 16} // channel 2 value (alt.) p@2565: }; p@2565: p@2565: static ec_pdo_info_t el3102_pdos[] = { p@2565: {0x1A00, 2, el3102_pdo_entries}, p@2565: {0x1A01, 2, el3102_pdo_entries + 2} p@2565: }; p@2565: p@2565: static ec_sync_info_t el3102_syncs[] = { p@2555: {2, EC_DIR_OUTPUT}, p@2565: {3, EC_DIR_INPUT, 2, el3102_pdos}, p@2555: {0xff} p@2555: }; p@2555: p@2565: // Analog out ------------------------- p@2565: p@2565: static ec_pdo_entry_info_t el4102_pdo_entries[] = { p@2565: {0x3001, 1, 16}, // channel 1 value p@2565: {0x3002, 1, 16}, // channel 2 value p@2565: }; p@2565: p@2565: static ec_pdo_info_t el4102_pdos[] = { p@2565: {0x1600, 1, el4102_pdo_entries}, p@2565: {0x1601, 1, el4102_pdo_entries + 1} p@2565: }; p@2565: p@2565: static ec_sync_info_t el4102_syncs[] = { p@2565: {2, EC_DIR_OUTPUT, 2, el4102_pdos}, p@2565: {3, EC_DIR_INPUT}, p@2565: {0xff} p@2565: }; p@2565: p@2555: // Digital out ------------------------ p@2565: p@2565: static ec_pdo_entry_info_t el2004_channels[] = { p@2565: {0x3001, 1, 1}, // Value 1 p@2565: {0x3001, 2, 1}, // Value 2 p@2565: {0x3001, 3, 1}, // Value 3 p@2565: {0x3001, 4, 1} // Value 4 p@2565: }; p@2565: p@2565: static ec_pdo_info_t el2004_pdos[] = { p@2565: {0x1600, 1, &el2004_channels[0]}, p@2565: {0x1601, 1, &el2004_channels[1]}, p@2565: {0x1602, 1, &el2004_channels[2]}, p@2565: {0x1603, 1, &el2004_channels[3]} p@2565: }; p@2565: p@2565: static ec_sync_info_t el2004_syncs[] = { p@2565: {0, EC_DIR_OUTPUT, 4, el2004_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: fp@2664: if (ds.working_counter != domain1_state.working_counter) { p@2555: printf("Domain1: WC %u.\n", ds.working_counter); fp@2664: } fp@2664: if (ds.wc_state != domain1_state.wc_state) { p@2555: printf("Domain1: State %u.\n", ds.wc_state); fp@2664: } 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: fp@2664: if (ms.slaves_responding != master_state.slaves_responding) { p@2555: printf("%u slave(s).\n", ms.slaves_responding); fp@2664: } fp@2664: if (ms.al_states != master_state.al_states) { p@2555: printf("AL states: 0x%02X.\n", ms.al_states); fp@2664: } fp@2664: if (ms.link_up != master_state.link_up) { p@2555: printf("Link is %s.\n", ms.link_up ? "up" : "down"); fp@2664: } 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: fp@2664: if (s.al_state != sc_ana_in_state.al_state) { p@2555: printf("AnaIn: State 0x%02X.\n", s.al_state); fp@2664: } fp@2664: if (s.online != sc_ana_in_state.online) { p@2555: printf("AnaIn: %s.\n", s.online ? "online" : "offline"); fp@2664: } fp@2664: if (s.operational != sc_ana_in_state.operational) { fp@2664: printf("AnaIn: %soperational.\n", s.operational ? "" : "Not "); fp@2664: } p@2555: p@2555: sc_ana_in_state = s; p@2555: } p@2555: p@2565: /*****************************************************************************/ p@2565: p@2555: void cyclic_task() p@2555: { p@2555: // receive process data p@2555: ecrt_master_receive(master); p@2555: ecrt_domain_process(domain1); p@2555: fp@2664: // check process data state p@2555: check_domain1_state(); p@2565: p@2555: if (counter) { p@2555: counter--; p@2565: } else { // do this at 1 Hz p@2565: counter = FREQUENCY; p@2555: p@2555: // calculate new process data p@2565: blink = !blink; p@2555: p@2555: // check for master state (optional) p@2555: check_master_state(); p@2555: fp@2664: // check for slave configuration state(s) (optional) p@2555: check_slave_config_states(); p@2565: } p@2565: p@2565: #if 0 p@2565: // read process data p@2565: printf("AnaIn: state %u value %u\n", p@2565: EC_READ_U8(domain1_pd + off_ana_in_status), p@2565: EC_READ_U16(domain1_pd + off_ana_in_value)); p@2565: #endif p@2565: p@2565: #if 1 p@2555: // write process data p@2565: EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09); p@2565: #endif 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: fp@2664: void stack_prefault(void) fp@2664: { fp@2664: unsigned char dummy[MAX_SAFE_STACK]; fp@2664: fp@2664: memset(dummy, 0, MAX_SAFE_STACK); p@2555: } p@2555: p@2555: /****************************************************************************/ p@2555: p@2555: int main(int argc, char **argv) p@2555: { p@2555: ec_slave_config_t *sc; fp@2664: struct timespec wakeup_time; fp@2664: int ret = 0; p@2555: p@2555: master = ecrt_request_master(0); fp@2664: if (!master) { fp@2664: return -1; fp@2664: } p@2555: p@2555: domain1 = ecrt_master_create_domain(master); fp@2664: if (!domain1) { fp@2664: return -1; fp@2664: } p@2555: p@2565: if (!(sc_ana_in = ecrt_master_slave_config( p@2565: master, AnaInSlavePos, Beckhoff_EL3102))) { p@2565: fprintf(stderr, "Failed to get slave configuration.\n"); p@2565: return -1; p@2565: } p@2565: p@2555: printf("Configuring PDOs...\n"); p@2565: if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3102_syncs)) { p@2555: fprintf(stderr, "Failed to configure PDOs.\n"); p@2555: return -1; p@2555: } p@2565: p@2565: if (!(sc = ecrt_master_slave_config( p@2565: master, AnaOutSlavePos, Beckhoff_EL4102))) { p@2565: fprintf(stderr, "Failed to get slave configuration.\n"); p@2565: return -1; p@2565: } p@2565: p@2565: if (ecrt_slave_config_pdos(sc, EC_END, el4102_syncs)) { p@2565: fprintf(stderr, "Failed to configure PDOs.\n"); p@2565: return -1; p@2565: } p@2565: p@2565: if (!(sc = ecrt_master_slave_config( p@2565: master, DigOutSlavePos, Beckhoff_EL2032))) { p@2565: fprintf(stderr, "Failed to get slave configuration.\n"); p@2565: return -1; p@2565: } p@2565: p@2565: if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) { p@2565: fprintf(stderr, "Failed to configure PDOs.\n"); p@2565: return -1; p@2565: } p@2555: p@2555: // Create configuration for bus coupler p@2555: sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); fp@2664: if (!sc) { fp@2664: return -1; fp@2664: } p@2565: p@2565: if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) { p@2565: fprintf(stderr, "PDO entry registration failed!\n"); p@2565: return -1; p@2565: } p@2555: p@2555: printf("Activating master...\n"); fp@2664: if (ecrt_master_activate(master)) { fp@2664: return -1; fp@2664: } p@2555: p@2555: if (!(domain1_pd = ecrt_domain_data(domain1))) { p@2555: return -1; p@2555: } p@2555: fp@2664: /* Set priority */ fp@2664: Florian@2697: struct sched_param param = {}; Florian@2697: param.sched_priority = sched_get_priority_max(SCHED_FIFO); Florian@2697: Florian@2697: printf("Using priority %i.", param.sched_priority); Florian@2697: if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { Florian@2697: perror("sched_setscheduler failed"); fp@2664: } fp@2664: fp@2664: /* Lock memory */ fp@2664: fp@2664: if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { fp@2664: fprintf(stderr, "Warning: Failed to lock memory: %s\n", fp@2664: strerror(errno)); fp@2664: } fp@2664: fp@2664: stack_prefault(); fp@2664: fp@2664: printf("Starting RT task with dt=%u ns.\n", PERIOD_NS); fp@2664: fp@2664: clock_gettime(CLOCK_MONOTONIC, &wakeup_time); fp@2664: wakeup_time.tv_sec += 1; /* start in future */ fp@2664: wakeup_time.tv_nsec = 0; fp@2664: p@2555: while (1) { fp@2664: ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, fp@2664: &wakeup_time, NULL); fp@2664: if (ret) { fp@2664: fprintf(stderr, "clock_nanosleep(): %s\n", strerror(ret)); fp@2664: break; p@2555: } fp@2664: fp@2664: cyclic_task(); fp@2664: fp@2664: wakeup_time.tv_nsec += PERIOD_NS; fp@2664: while (wakeup_time.tv_nsec >= NSEC_PER_SEC) { fp@2664: wakeup_time.tv_nsec -= NSEC_PER_SEC; fp@2664: wakeup_time.tv_sec++; fp@2664: } fp@2664: } fp@2664: fp@2664: return ret; fp@2664: } fp@2664: fp@2664: /****************************************************************************/