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@2649: #include /* clock_gettime() */ fp@2649: #include /* mlockall() */ fp@1247: fp@1255: /****************************************************************************/ fp@1255: fp@1242: #include "ecrt.h" fp@1242: fp@1255: /****************************************************************************/ fp@1255: fp@2649: /** Task period in ns. */ fp@2649: #define PERIOD_NS (1000000) fp@2649: fp@2649: #define MAX_SAFE_STACK (8 * 1024) /* The maximum stack size which is fp@2649: guranteed safe to access without fp@2649: faulting */ fp@2649: fp@2649: /****************************************************************************/ fp@2649: fp@2649: /* Constants */ fp@2649: #define NSEC_PER_SEC (1000000000) fp@2649: #define FREQUENCY (NSEC_PER_SEC / PERIOD_NS) 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: /****************************************************************************/ 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: // 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@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@2649: if (ds.working_counter != domain1_state.working_counter) { fp@1259: printf("Domain1: WC %u.\n", ds.working_counter); fp@2649: } fp@2649: if (ds.wc_state != domain1_state.wc_state) { fp@1259: printf("Domain1: State %u.\n", ds.wc_state); fp@2649: } 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@2649: if (ms.slaves_responding != master_state.slaves_responding) { fp@1259: printf("%u slave(s).\n", ms.slaves_responding); fp@2649: } fp@2649: if (ms.al_states != master_state.al_states) { fp@1259: printf("AL states: 0x%02X.\n", ms.al_states); fp@2649: } fp@2649: if (ms.link_up != master_state.link_up) { fp@1259: printf("Link is %s.\n", ms.link_up ? "up" : "down"); fp@2649: } 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@2649: if (s.al_state != sc_ana_in_state.al_state) { fp@1259: printf("AnaIn: State 0x%02X.\n", s.al_state); fp@2649: } fp@2649: if (s.online != sc_ana_in_state.online) { fp@1259: printf("AnaIn: %s.\n", s.online ? "online" : "offline"); fp@2649: } fp@2649: if (s.operational != sc_ana_in_state.operational) { fp@2649: printf("AnaIn: %soperational.\n", s.operational ? "" : "Not "); fp@2649: } fp@1259: fp@1259: sc_ana_in_state = s; fp@1259: } fp@1259: fp@1352: /*****************************************************************************/ fp@1352: 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@2649: // check process data state 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@2649: // check for slave configuration state(s) (optional) fp@1259: check_slave_config_states(); 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@2649: void stack_prefault(void) fp@2649: { fp@2649: unsigned char dummy[MAX_SAFE_STACK]; fp@2649: fp@2649: memset(dummy, 0, MAX_SAFE_STACK); 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@2649: struct timespec wakeup_time; fp@2649: int ret = 0; fp@2589: fp@1244: master = ecrt_request_master(0); fp@2649: if (!master) { fp@2649: return -1; fp@2649: } fp@1242: fp@1255: domain1 = ecrt_master_create_domain(master); fp@2649: if (!domain1) { fp@2649: return -1; fp@2649: } 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@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: fp@1255: // Create configuration for bus coupler fp@1255: sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100); fp@2649: if (!sc) { fp@2649: return -1; fp@2649: } 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@2649: if (ecrt_master_activate(master)) { fp@2649: return -1; fp@2649: } fp@1247: fp@1258: if (!(domain1_pd = ecrt_domain_data(domain1))) { fp@1258: return -1; fp@1258: } fp@1258: fp@2649: /* Set priority */ fp@2649: fp@1249: pid_t pid = getpid(); fp@2649: if (setpriority(PRIO_PROCESS, pid, -19)) { fp@1249: fprintf(stderr, "Warning: Failed to set priority: %s\n", fp@1249: strerror(errno)); fp@2649: } fp@2649: fp@2649: /* Lock memory */ fp@2649: fp@2649: if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { fp@2649: fprintf(stderr, "Warning: Failed to lock memory: %s\n", fp@2649: strerror(errno)); fp@2649: } fp@2649: fp@2649: stack_prefault(); fp@2649: fp@2649: printf("Starting RT task with dt=%u ns.\n", PERIOD_NS); fp@2649: fp@2649: clock_gettime(CLOCK_MONOTONIC, &wakeup_time); fp@2649: wakeup_time.tv_sec += 1; /* start in future */ fp@2649: wakeup_time.tv_nsec = 0; fp@2649: fp@1508: while (1) { fp@2649: ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, fp@2649: &wakeup_time, NULL); fp@2649: if (ret) { fp@2649: fprintf(stderr, "clock_nanosleep(): %s\n", strerror(ret)); fp@2649: break; fp@1247: } fp@2649: fp@2649: cyclic_task(); fp@2649: fp@2649: wakeup_time.tv_nsec += PERIOD_NS; fp@2649: while (wakeup_time.tv_nsec >= NSEC_PER_SEC) { fp@2649: wakeup_time.tv_nsec -= NSEC_PER_SEC; fp@2649: wakeup_time.tv_sec++; fp@2649: } fp@2649: } fp@2649: fp@2649: return ret; fp@2649: } fp@2649: fp@2649: /****************************************************************************/