examples/user/main.c
changeset 1352 275d2fdeab48
parent 1327 4d179b06dd3c
child 1363 11c0b2caa253
equal deleted inserted replaced
1351:cf8f08631c8e 1352:275d2fdeab48
    43 #define FREQUENCY 100
    43 #define FREQUENCY 100
    44 #define PRIORITY 1
    44 #define PRIORITY 1
    45 
    45 
    46 // Optional features
    46 // Optional features
    47 #define CONFIGURE_PDOS  1
    47 #define CONFIGURE_PDOS  1
       
    48 #define SDO_ACCESS      0
    48 
    49 
    49 /****************************************************************************/
    50 /****************************************************************************/
    50 
    51 
    51 // EtherCAT
    52 // EtherCAT
    52 static ec_master_t *master = NULL;
    53 static ec_master_t *master = NULL;
    66 
    67 
    67 // process data
    68 // process data
    68 static uint8_t *domain1_pd = NULL;
    69 static uint8_t *domain1_pd = NULL;
    69 
    70 
    70 #define BusCouplerPos  0, 0
    71 #define BusCouplerPos  0, 0
    71 #define AnaOutSlavePos 0, 1
    72 #define DigOutSlavePos 0, 2
    72 #define AnaInSlavePos  0, 2
    73 #define AnaInSlavePos  0, 3
    73 #define DigOutSlavePos 0, 3
    74 #define AnaOutSlavePos 0, 4
    74 
    75 
    75 #define Beckhoff_EK1100 0x00000002, 0x044c2c52
    76 #define Beckhoff_EK1100 0x00000002, 0x044c2c52
    76 #define Beckhoff_EL2004 0x00000002, 0x07d43052
    77 #define Beckhoff_EL2004 0x00000002, 0x07d43052
    77 #define Beckhoff_EL2032 0x00000002, 0x07f03052
    78 #define Beckhoff_EL2032 0x00000002, 0x07f03052
    78 #define Beckhoff_EL3152 0x00000002, 0x0c503052
    79 #define Beckhoff_EL3152 0x00000002, 0x0c503052
       
    80 #define Beckhoff_EL3102 0x00000002, 0x0c1e3052
    79 #define Beckhoff_EL4102 0x00000002, 0x10063052
    81 #define Beckhoff_EL4102 0x00000002, 0x10063052
    80 
    82 
    81 // offsets for PDO entries
    83 // offsets for PDO entries
    82 static unsigned int off_ana_in_status;
    84 static unsigned int off_ana_in_status;
    83 static unsigned int off_ana_in_value;
    85 static unsigned int off_ana_in_value;
    84 static unsigned int off_ana_out;
    86 static unsigned int off_ana_out;
    85 static unsigned int off_dig_out;
    87 static unsigned int off_dig_out;
    86 
    88 
    87 const static ec_pdo_entry_reg_t domain1_regs[] = {
    89 const static ec_pdo_entry_reg_t domain1_regs[] = {
    88     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 1, &off_ana_in_status},
    90     {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 1, &off_ana_in_status},
    89     {AnaInSlavePos,  Beckhoff_EL3152, 0x3101, 2, &off_ana_in_value},
    91     {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 2, &off_ana_in_value},
    90 	{AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
    92 	{AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
    91 	{DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out},
    93 	{DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out},
    92 	{}
    94 	{}
    93 };
    95 };
    94 
    96 
    99 
   101 
   100 #if CONFIGURE_PDOS
   102 #if CONFIGURE_PDOS
   101 
   103 
   102 // Analog in --------------------------
   104 // Analog in --------------------------
   103 
   105 
   104 static ec_pdo_entry_info_t el3152_pdo_entries[] = {
   106 static ec_pdo_entry_info_t el3102_pdo_entries[] = {
   105     {0x3101, 1,  8}, // channel 1 status
   107     {0x3101, 1,  8}, // channel 1 status
   106     {0x3101, 2, 16}, // channel 1 value
   108     {0x3101, 2, 16}, // channel 1 value
   107     {0x3102, 1,  8}, // channel 2 status
   109     {0x3102, 1,  8}, // channel 2 status
   108     {0x3102, 2, 16}, // channel 2 value
   110     {0x3102, 2, 16}, // channel 2 value
   109     {0x6401, 1, 16}, // channel 1 value (alt.)
   111     {0x6401, 1, 16}, // channel 1 value (alt.)
   110     {0x6401, 2, 16}  // channel 2 value (alt.)
   112     {0x6401, 2, 16}  // channel 2 value (alt.)
   111 };
   113 };
   112 
   114 
   113 static ec_pdo_info_t el3152_pdos[] = {
   115 static ec_pdo_info_t el3102_pdos[] = {
   114     {0x1A00, 2, el3152_pdo_entries},
   116     {0x1A00, 2, el3102_pdo_entries},
   115     {0x1A01, 2, el3152_pdo_entries + 2}
   117     {0x1A01, 2, el3102_pdo_entries + 2}
   116 };
   118 };
   117 
   119 
   118 static ec_sync_info_t el3152_syncs[] = {
   120 static ec_sync_info_t el3102_syncs[] = {
   119     {2, EC_DIR_OUTPUT},
   121     {2, EC_DIR_OUTPUT},
   120     {3, EC_DIR_INPUT, 2, el3152_pdos},
   122     {3, EC_DIR_INPUT, 2, el3102_pdos},
   121     {0xff}
   123     {0xff}
   122 };
   124 };
   123 
   125 
   124 // Analog out -------------------------
   126 // Analog out -------------------------
   125 
   127 
   158 static ec_sync_info_t el2004_syncs[] = {
   160 static ec_sync_info_t el2004_syncs[] = {
   159     {0, EC_DIR_OUTPUT, 4, el2004_pdos},
   161     {0, EC_DIR_OUTPUT, 4, el2004_pdos},
   160     {1, EC_DIR_INPUT},
   162     {1, EC_DIR_INPUT},
   161     {0xff}
   163     {0xff}
   162 };
   164 };
       
   165 #endif
       
   166 
       
   167 /*****************************************************************************/
       
   168 
       
   169 #if SDO_ACCESS
       
   170 static ec_sdo_request_t *sdo;
   163 #endif
   171 #endif
   164 
   172 
   165 /*****************************************************************************/
   173 /*****************************************************************************/
   166 
   174 
   167 void check_domain1_state(void)
   175 void check_domain1_state(void)
   213                 s.operational ? "" : "Not ");
   221                 s.operational ? "" : "Not ");
   214 
   222 
   215     sc_ana_in_state = s;
   223     sc_ana_in_state = s;
   216 }
   224 }
   217 
   225 
       
   226 /*****************************************************************************/
       
   227 
       
   228 #if SDO_ACCESS
       
   229 void read_sdo(void)
       
   230 {
       
   231     switch (ecrt_sdo_request_state(sdo)) {
       
   232         case EC_REQUEST_UNUSED: // request was not used yet
       
   233             ecrt_sdo_request_read(sdo); // trigger first read
       
   234             break;
       
   235         case EC_REQUEST_BUSY:
       
   236             fprintf(stderr, "Still busy...\n");
       
   237             break;
       
   238         case EC_REQUEST_SUCCESS:
       
   239             fprintf(stderr, "SDO value: 0x%04X\n",
       
   240                     EC_READ_U16(ecrt_sdo_request_data(sdo)));
       
   241             ecrt_sdo_request_read(sdo); // trigger next read
       
   242             break;
       
   243         case EC_REQUEST_ERROR:
       
   244             fprintf(stderr, "Failed to read SDO!\n");
       
   245             ecrt_sdo_request_read(sdo); // retry reading
       
   246             break;
       
   247     }
       
   248 }
       
   249 #endif
       
   250 
   218 /****************************************************************************/
   251 /****************************************************************************/
   219 
   252 
   220 void cyclic_task()
   253 void cyclic_task()
   221 {
   254 {
   222     int i;
   255     int i;
   239         // check for master state (optional)
   272         // check for master state (optional)
   240         check_master_state();
   273         check_master_state();
   241 
   274 
   242         // check for islave configuration state(s) (optional)
   275         // check for islave configuration state(s) (optional)
   243         check_slave_config_states();
   276         check_slave_config_states();
       
   277 
       
   278 #if SDO_ACCESS
       
   279         // read process data SDO
       
   280         read_sdo();
       
   281 #endif
       
   282 
   244     }
   283     }
   245 
   284 
   246 #if 0
   285 #if 0
   247     // read process data
   286     // read process data
   248     printf("AnaIn: state %u value %u\n",
   287     printf("AnaIn: state %u value %u\n",
   285     domain1 = ecrt_master_create_domain(master);
   324     domain1 = ecrt_master_create_domain(master);
   286     if (!domain1)
   325     if (!domain1)
   287         return -1;
   326         return -1;
   288 
   327 
   289     if (!(sc_ana_in = ecrt_master_slave_config(
   328     if (!(sc_ana_in = ecrt_master_slave_config(
   290                     master, AnaInSlavePos, Beckhoff_EL3152))) {
   329                     master, AnaInSlavePos, Beckhoff_EL3102))) {
   291         fprintf(stderr, "Failed to get slave configuration.\n");
   330         fprintf(stderr, "Failed to get slave configuration.\n");
   292         return -1;
   331         return -1;
   293     }
   332     }
       
   333 
       
   334 #if SDO_ACCESS
       
   335     fprintf(stderr, "Creating SDO requests...\n");
       
   336     if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) {
       
   337         fprintf(stderr, "Failed to create SDO request.\n");
       
   338         return -1;
       
   339     }
       
   340     ecrt_sdo_request_timeout(sdo, 500); // ms
       
   341 #endif
   294 
   342 
   295 #if CONFIGURE_PDOS
   343 #if CONFIGURE_PDOS
   296     printf("Configuring PDOs...\n");
   344     printf("Configuring PDOs...\n");
   297     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3152_syncs)) {
   345     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3102_syncs)) {
   298         fprintf(stderr, "Failed to configure PDOs.\n");
   346         fprintf(stderr, "Failed to configure PDOs.\n");
   299         return -1;
   347         return -1;
   300     }
   348     }
   301 
   349 
   302     if (!(sc = ecrt_master_slave_config(
   350     if (!(sc = ecrt_master_slave_config(