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