fp@2433: /****************************************************************************** fp@2433: * fp@2433: * $Id$ fp@2433: * fp@2433: * Copyright (C) 2011 IgH Andreas Stewering-Bone fp@2433: * 2012 Florian Pose fp@2433: * fp@2433: * This file is part of the IgH EtherCAT master fp@2433: * fp@2433: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@2433: * modify it under the terms of the GNU General Public License version 2, as fp@2433: * published by the Free Software Foundation. fp@2433: * fp@2433: * The IgH EtherCAT master is distributed in the hope that it will be useful, fp@2433: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@2433: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@2433: * Public License for more details. fp@2433: * fp@2433: * You should have received a copy of the GNU General Public License along fp@2433: * with the IgH EtherCAT master. If not, see . fp@2433: * fp@2433: * --- fp@2433: * fp@2433: * The license mentioned above concerns the source code only. Using the fp@2433: * EtherCAT technology and brand is only permitted in compliance with the fp@2433: * industrial property and similar rights of Beckhoff Automation GmbH. fp@2433: * fp@2433: *****************************************************************************/ fp@2433: fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: #include fp@2433: fp@2433: #include "ecrt.h" fp@2433: fp@2433: #define NSEC_PER_SEC 1000000000 fp@2433: fp@2433: static unsigned int cycle_us = 1000; /* 1 ms */ fp@2433: fp@2433: static pthread_t cyclic_thread; fp@2433: fp@2433: static int run = 1; fp@2433: fp@2433: /****************************************************************************/ fp@2433: fp@2433: // EtherCAT fp@2433: static ec_master_t *master = NULL; fp@2433: static ec_master_state_t master_state = {}; fp@2433: fp@2433: static ec_domain_t *domain1 = NULL; fp@2433: static ec_domain_state_t domain1_state = {}; fp@2433: fp@2433: static uint8_t *domain1_pd = NULL; fp@2433: fp@2433: static ec_slave_config_t *sc_dig_out_01 = NULL; fp@2433: fp@2433: /****************************************************************************/ fp@2433: fp@2433: // process data fp@2433: fp@2433: #define BusCoupler01_Pos 0, 0 fp@2433: #define DigOutSlave01_Pos 0, 1 fp@2433: fp@2433: #define Beckhoff_EK1100 0x00000002, 0x044c2c52 edouard@2714: #define Beckhoff_EL2088 0x00000002, 0x08283052 fp@2433: fp@2433: // offsets for PDO entries fp@2433: static unsigned int off_dig_out0 = 0; fp@2433: fp@2433: // process data fp@2433: fp@2433: const static ec_pdo_entry_reg_t domain1_regs[] = { edouard@2714: {DigOutSlave01_Pos, Beckhoff_EL2088, 0x7000, 0x01, &off_dig_out0, NULL}, fp@2433: {} fp@2433: }; fp@2433: fp@2433: /****************************************************************************/ fp@2433: fp@2433: /* Slave 1, "EL2004" fp@2433: * Vendor ID: 0x00000002 fp@2433: * Product code: 0x07d43052 fp@2433: * Revision number: 0x00100000 fp@2433: */ fp@2433: fp@2433: ec_pdo_entry_info_t slave_1_pdo_entries[] = { fp@2433: {0x7000, 0x01, 1}, /* Output */ fp@2433: {0x7010, 0x01, 1}, /* Output */ fp@2433: {0x7020, 0x01, 1}, /* Output */ fp@2433: {0x7030, 0x01, 1}, /* Output */ fp@2433: }; fp@2433: fp@2433: ec_pdo_info_t slave_1_pdos[] = { fp@2433: {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */ fp@2433: {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */ fp@2433: {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */ fp@2433: {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */ fp@2433: }; fp@2433: fp@2433: ec_sync_info_t slave_1_syncs[] = { fp@2433: {0, EC_DIR_OUTPUT, 4, slave_1_pdos + 0, EC_WD_ENABLE}, fp@2433: {0xff} fp@2433: }; fp@2433: fp@2433: /***************************************************************************** fp@2433: * Realtime task fp@2433: ****************************************************************************/ fp@2433: fp@2433: void rt_check_domain_state(void) fp@2433: { fp@2433: ec_domain_state_t ds = {}; fp@2433: fp@2433: ecrt_domain_state(domain1, &ds); fp@2433: fp@2433: if (ds.working_counter != domain1_state.working_counter) { edouard@2705: printf("Domain1: WC %u.\n", ds.working_counter); fp@2433: } fp@2433: fp@2433: if (ds.wc_state != domain1_state.wc_state) { edouard@2705: printf("Domain1: State %u.\n", ds.wc_state); fp@2433: } fp@2433: fp@2433: domain1_state = ds; fp@2433: } fp@2433: fp@2433: /****************************************************************************/ fp@2433: fp@2433: void rt_check_master_state(void) fp@2433: { fp@2433: ec_master_state_t ms; fp@2433: fp@2433: ecrt_master_state(master, &ms); fp@2433: fp@2433: if (ms.slaves_responding != master_state.slaves_responding) { edouard@2705: printf("%u slave(s).\n", ms.slaves_responding); fp@2433: } fp@2433: fp@2433: if (ms.al_states != master_state.al_states) { edouard@2705: printf("AL states: 0x%02X.\n", ms.al_states); fp@2433: } fp@2433: fp@2433: if (ms.link_up != master_state.link_up) { edouard@2705: printf("Link is %s.\n", ms.link_up ? "up" : "down"); fp@2433: } fp@2433: fp@2433: master_state = ms; fp@2433: } fp@2433: fp@2433: /****************************************************************************/ fp@2433: fp@2433: void *my_thread(void *arg) fp@2433: { fp@2433: struct timespec next_period; fp@2433: int cycle_counter = 0; fp@2433: unsigned int blink = 0; edouard@2714: struct sched_param param = { .sched_priority = 80 }; edouard@2714: edouard@2714: pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); edouard@2714: edouard@2714: clock_gettime(CLOCK_MONOTONIC, &next_period); fp@2433: fp@2433: while (run) { fp@2433: next_period.tv_nsec += cycle_us * 1000; fp@2433: while (next_period.tv_nsec >= NSEC_PER_SEC) { fp@2433: next_period.tv_nsec -= NSEC_PER_SEC; fp@2433: next_period.tv_sec++; fp@2433: } fp@2433: edouard@2714: clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_period, NULL); fp@2433: fp@2433: cycle_counter++; fp@2433: fp@2433: // receive EtherCAT fp@2433: ecrt_master_receive(master); fp@2433: ecrt_domain_process(domain1); fp@2433: fp@2433: rt_check_domain_state(); fp@2433: fp@2433: if (!(cycle_counter % 1000)) { fp@2433: rt_check_master_state(); fp@2433: } fp@2433: fp@2433: if (!(cycle_counter % 200)) { fp@2433: blink = !blink; fp@2433: } fp@2433: fp@2433: EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0x0 : 0x0F); fp@2433: fp@2433: // send process data fp@2433: ecrt_domain_queue(domain1); fp@2433: ecrt_master_send(master); fp@2433: } fp@2433: fp@2433: return NULL; fp@2433: } fp@2433: fp@2433: /**************************************************************************** fp@2433: * Signal handler fp@2433: ***************************************************************************/ fp@2433: fp@2433: void signal_handler(int sig) fp@2433: { fp@2433: run = 0; fp@2433: } fp@2433: fp@2433: /**************************************************************************** fp@2433: * Main function fp@2433: ***************************************************************************/ fp@2433: fp@2433: int main(int argc, char *argv[]) fp@2433: { fp@2433: ec_slave_config_t *sc; fp@2433: int ret; fp@2433: fp@2433: signal(SIGTERM, signal_handler); fp@2433: signal(SIGINT, signal_handler); fp@2433: fp@2433: mlockall(MCL_CURRENT | MCL_FUTURE); fp@2433: fp@2433: printf("Requesting master...\n"); fp@2433: master = ecrt_request_master(0); fp@2433: if (!master) { fp@2433: return -1; fp@2433: } fp@2433: fp@2433: domain1 = ecrt_master_create_domain(master); fp@2433: if (!domain1) { fp@2433: return -1; fp@2433: } fp@2433: fp@2433: printf("Creating slave configurations...\n"); fp@2433: fp@2433: // Create configuration for bus coupler fp@2433: sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100); fp@2433: if (!sc) { fp@2433: return -1; fp@2433: } fp@2433: fp@2433: sc_dig_out_01 = edouard@2714: ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2088); fp@2433: if (!sc_dig_out_01) { fp@2433: fprintf(stderr, "Failed to get slave configuration.\n"); fp@2433: return -1; fp@2433: } fp@2433: fp@2433: if (ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_1_syncs)) { fp@2433: fprintf(stderr, "Failed to configure PDOs.\n"); fp@2433: return -1; fp@2433: } fp@2433: fp@2433: if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) { fp@2433: fprintf(stderr, "PDO entry registration failed!\n"); fp@2433: return -1; fp@2433: } fp@2433: fp@2433: printf("Activating master...\n"); fp@2433: if (ecrt_master_activate(master)) { fp@2433: return -1; fp@2433: } fp@2433: fp@2433: if (!(domain1_pd = ecrt_domain_data(domain1))) { fp@2433: fprintf(stderr, "Failed to get domain data pointer.\n"); fp@2433: return -1; fp@2433: } fp@2433: fp@2433: pthread_attr_t thattr; fp@2433: pthread_attr_init(&thattr); fp@2433: pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); fp@2433: fp@2433: ret = pthread_create(&cyclic_thread, &thattr, &my_thread, NULL); fp@2433: if (ret) { fp@2433: fprintf(stderr, "%s: pthread_create cyclic task failed\n", fp@2433: strerror(-ret)); fp@2433: return 1; fp@2433: } fp@2433: fp@2433: while (run) { fp@2433: sched_yield(); fp@2433: } fp@2433: fp@2433: pthread_join(cyclic_thread, NULL); fp@2433: fp@2433: printf("End of Program\n"); fp@2433: ecrt_release_master(master); fp@2433: fp@2433: return 0; fp@2433: } fp@2433: fp@2433: /****************************************************************************/