examples/user/main.c
changeset 1255 38b7e05b20c1
parent 1249 d64ed42e0d11
child 1258 900f1124e8f8
equal deleted inserted replaced
1254:c19d273a9e76 1255:38b7e05b20c1
     1 /*****************************************************************************
     1 /*****************************************************************************
     2  *
     2  *
     3  * $Id$
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    10  *  and/or modify it under the terms of the GNU General Public License
       
    11  *  as published by the Free Software Foundation; either version 2 of the
       
    12  *  License, or (at your option) any later version.
       
    13  *
       
    14  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    15  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  *  GNU General Public License for more details.
       
    18  *
       
    19  *  You should have received a copy of the GNU General Public License
       
    20  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    22  *
       
    23  *  The right to use EtherCAT Technology is granted and comes free of
       
    24  *  charge under condition of compatibility of product made by
       
    25  *  Licensee. People intending to distribute/sell products based on the
       
    26  *  code, have to sign an agreement to guarantee that products using
       
    27  *  software based on IgH EtherCAT master stay compatible with the actual
       
    28  *  EtherCAT specification (which are released themselves as an open
       
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
       
    30  *  Technology, IP and trade marks.
     4  *
    31  *
     5  ****************************************************************************/
    32  ****************************************************************************/
     6 
    33 
     7 #include <errno.h>
    34 #include <errno.h>
     8 #include <signal.h>
    35 #include <signal.h>
    11 #include <sys/resource.h>
    38 #include <sys/resource.h>
    12 #include <sys/time.h>
    39 #include <sys/time.h>
    13 #include <sys/types.h>
    40 #include <sys/types.h>
    14 #include <unistd.h>
    41 #include <unistd.h>
    15 
    42 
       
    43 /****************************************************************************/
       
    44 
    16 #include "ecrt.h"
    45 #include "ecrt.h"
    17 
    46 
       
    47 /****************************************************************************/
       
    48 
       
    49 // Optional features
       
    50 #define CONFIGURE_PDOS  1
       
    51 #define EL3152_ALT_PDOS 0
       
    52 #define SDO_ACCESS      0
       
    53 #define VOE_ACCESS      0
       
    54 
    18 #define PRIORITY 1
    55 #define PRIORITY 1
       
    56 
       
    57 #define BusCouplerPos  0, 0
       
    58 #define AnaOutSlavePos 0, 1
       
    59 #define AnaInSlavePos  0, 2
       
    60 #define DigOutSlavePos 0, 3
       
    61 
       
    62 #define Beckhoff_EK1100 0x00000002, 0x044C2C52
       
    63 #define Beckhoff_EL2004 0x00000002, 0x07D43052
       
    64 #define Beckhoff_EL3152 0x00000002, 0x0c503052
       
    65 #define Beckhoff_EL4102 0x00000002, 0x10063052
    19 
    66 
    20 /****************************************************************************/
    67 /****************************************************************************/
    21 
    68 
    22 static unsigned int sig_alarms = 0;
    69 static unsigned int sig_alarms = 0;
    23 static unsigned int user_alarms = 0;
    70 static unsigned int user_alarms = 0;
       
    71 
       
    72 // offsets for Pdo entries
       
    73 static unsigned int off_ana_in;
       
    74 static unsigned int off_ana_out;
       
    75 static unsigned int off_dig_out;
       
    76 
       
    77 const static ec_pdo_entry_reg_t domain1_regs[] = {
       
    78 #if EL3152_ALT_PDOS
       
    79     {AnaInSlavePos,  Beckhoff_EL3152, 0x6401, 1, &off_ana_in},
       
    80 #else
       
    81     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 2, &off_ana_in},
       
    82 #endif
       
    83 	{AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
       
    84 	{DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
       
    85 	{}
       
    86 };
       
    87 
       
    88 static ec_slave_config_t *sc_ana_in = NULL;
       
    89 
       
    90 /*****************************************************************************/
       
    91 
       
    92 #if CONFIGURE_PDOS
       
    93 
       
    94 // Analog in --------------------------
       
    95 
       
    96 static ec_pdo_entry_info_t el3152_pdo_entries[] = {
       
    97     {0x3101, 1,  8}, // channel 1 status
       
    98     {0x3101, 2, 16}, // channel 1 value
       
    99     {0x3102, 1,  8}, // channel 2 status
       
   100     {0x3102, 2, 16}, // channel 2 value
       
   101     {0x6401, 1, 16}, // channel 1 value (alt.)
       
   102     {0x6401, 2, 16}  // channel 2 value (alt.)
       
   103 };
       
   104 
       
   105 #if EL3152_ALT_PDOS
       
   106 static ec_pdo_info_t el3152_pdos[] = {
       
   107     {0x1A10, 2, el3152_pdo_entries + 4},
       
   108 };
       
   109 
       
   110 static ec_sync_info_t el3152_syncs[] = {
       
   111     {2, EC_DIR_OUTPUT},
       
   112     {3, EC_DIR_INPUT, 1, el3152_pdos},
       
   113     {0xff}
       
   114 };
       
   115 #else
       
   116 static ec_pdo_info_t el3152_pdos[] = {
       
   117     {0x1A00, 2, el3152_pdo_entries},
       
   118     {0x1A01, 2, el3152_pdo_entries + 2}
       
   119 };
       
   120 
       
   121 static ec_sync_info_t el3152_syncs[] = {
       
   122     {2, EC_DIR_OUTPUT},
       
   123     {3, EC_DIR_INPUT, 2, el3152_pdos},
       
   124     {0xff}
       
   125 };
       
   126 #endif
       
   127 
       
   128 // Analog out -------------------------
       
   129 
       
   130 static ec_pdo_entry_info_t el4102_pdo_entries[] = {
       
   131     {0x3001, 1, 16}, // channel 1 value
       
   132     {0x3002, 1, 16}, // channel 2 value
       
   133 };
       
   134 
       
   135 static ec_pdo_info_t el4102_pdos[] = {
       
   136     {0x1600, 1, el4102_pdo_entries},
       
   137     {0x1601, 1, el4102_pdo_entries + 1}
       
   138 };
       
   139 
       
   140 static ec_sync_info_t el4102_syncs[] = {
       
   141     {2, EC_DIR_OUTPUT, 2, el4102_pdos},
       
   142     {3, EC_DIR_INPUT},
       
   143     {0xff}
       
   144 };
       
   145 
       
   146 // Digital out ------------------------
       
   147 
       
   148 static ec_pdo_entry_info_t el2004_channels[] = {
       
   149     {0x3001, 1, 1}, // Value 1
       
   150     {0x3001, 2, 1}, // Value 2
       
   151     {0x3001, 3, 1}, // Value 3
       
   152     {0x3001, 4, 1}  // Value 4
       
   153 };
       
   154 
       
   155 static ec_pdo_info_t el2004_pdos[] = {
       
   156     {0x1600, 1, &el2004_channels[0]},
       
   157     {0x1601, 1, &el2004_channels[1]},
       
   158     {0x1602, 1, &el2004_channels[2]},
       
   159     {0x1603, 1, &el2004_channels[3]}
       
   160 };
       
   161 
       
   162 static ec_sync_info_t el2004_syncs[] = {
       
   163     {0, EC_DIR_OUTPUT, 4, el2004_pdos},
       
   164     {1, EC_DIR_INPUT},
       
   165     {0xff}
       
   166 };
       
   167 #endif
    24 
   168 
    25 /****************************************************************************/
   169 /****************************************************************************/
    26 
   170 
    27 void signal_handler(int signum) {
   171 void signal_handler(int signum) {
    28     switch (signum) {
   172     switch (signum) {
    35 /****************************************************************************/
   179 /****************************************************************************/
    36 
   180 
    37 int main(int argc, char **argv)
   181 int main(int argc, char **argv)
    38 {
   182 {
    39 	ec_master_t *master;
   183 	ec_master_t *master;
    40 	ec_domain_t *domain;
   184 	ec_domain_t *domain1;
    41 	ec_slave_config_t *sc;
   185 	ec_slave_config_t *sc;
    42     struct sigaction sa;
   186     struct sigaction sa;
    43     struct itimerval tv;
   187     struct itimerval tv;
    44     
   188     
    45     master = ecrt_request_master(0);
   189     master = ecrt_request_master(0);
    46 	if (!master)
   190 	if (!master)
    47 		return -1;
   191 		return -1;
    48 
   192 
    49     domain = ecrt_master_create_domain(master);
   193     domain1 = ecrt_master_create_domain(master);
    50     if (!domain)
   194     if (!domain1)
    51         return -1;
   195         return -1;
    52 
   196 
    53     sc = ecrt_master_slave_config(master, 0, 0, 0x00000002, 0x044C2C52);
   197     if (!(sc_ana_in = ecrt_master_slave_config(
       
   198                     master, AnaInSlavePos, Beckhoff_EL3152))) {
       
   199         fprintf(stderr, "Failed to get slave configuration.\n");
       
   200         return -1;
       
   201     }
       
   202 
       
   203 #if CONFIGURE_PDOS
       
   204     printf("Configuring Pdos...\n");
       
   205     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3152_syncs)) {
       
   206         fprintf(stderr, "Failed to configure Pdos.\n");
       
   207         return -1;
       
   208     }
       
   209 
       
   210     if (!(sc = ecrt_master_slave_config(
       
   211                     master, AnaOutSlavePos, Beckhoff_EL4102))) {
       
   212         fprintf(stderr, "Failed to get slave configuration.\n");
       
   213         return -1;
       
   214     }
       
   215 
       
   216     if (ecrt_slave_config_pdos(sc, EC_END, el4102_syncs)) {
       
   217         fprintf(stderr, "Failed to configure Pdos.\n");
       
   218         return -1;
       
   219     }
       
   220 
       
   221     if (!(sc = ecrt_master_slave_config(
       
   222                     master, DigOutSlavePos, Beckhoff_EL2004))) {
       
   223         fprintf(stderr, "Failed to get slave configuration.\n");
       
   224         return -1;
       
   225     }
       
   226 
       
   227     if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) {
       
   228         fprintf(stderr, "Failed to configure Pdos.\n");
       
   229         return -1;
       
   230     }
       
   231 #endif
       
   232 
       
   233     // Create configuration for bus coupler
       
   234     sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
    54     if (!sc)
   235     if (!sc)
    55         return -1;
   236         return -1;
       
   237 
       
   238     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
       
   239         fprintf(stderr, "Pdo entry registration failed!\n");
       
   240 		return -1;
       
   241     }
    56 
   242 
    57     printf("Activating master...\n");
   243     printf("Activating master...\n");
    58     if (ecrt_master_activate(master))
   244     if (ecrt_master_activate(master))
    59         return -1;
   245         return -1;
    60 
   246 
    73         return -1;
   259         return -1;
    74     }
   260     }
    75 
   261 
    76     printf("Starting timer...\n");
   262     printf("Starting timer...\n");
    77     tv.it_interval.tv_sec = 0;
   263     tv.it_interval.tv_sec = 0;
    78     tv.it_interval.tv_usec = 100000;
   264     tv.it_interval.tv_usec = 10000;
    79     tv.it_value.tv_sec = 0;
   265     tv.it_value.tv_sec = 0;
    80     tv.it_value.tv_usec = 1000;
   266     tv.it_value.tv_usec = 1000;
    81     if (setitimer(ITIMER_REAL, &tv, NULL)) {
   267     if (setitimer(ITIMER_REAL, &tv, NULL)) {
    82         fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
   268         fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
    83         return 1;
   269         return 1;
    85 
   271 
    86     printf("Started.\n");
   272     printf("Started.\n");
    87 	while (1) {
   273 	while (1) {
    88         sleep(1);
   274         sleep(1);
    89 
   275 
    90 #if 1
   276 #if 0
    91         struct timeval t;
   277         struct timeval t;
    92         gettimeofday(&t, NULL);
   278         gettimeofday(&t, NULL);
    93         printf("%u.%06u\n", t.tv_sec, t.tv_usec);
   279         printf("%u.%06u\n", t.tv_sec, t.tv_usec);
    94 #endif
   280 #endif
    95 
   281