examples/mini/mini.c
changeset 1171 de880622f253
parent 1082 ff06c58e269c
child 1179 c0e6adec1aee
equal deleted inserted replaced
1170:af068174a630 1171:de880622f253
    41 /*****************************************************************************/
    41 /*****************************************************************************/
    42 
    42 
    43 // Module parameters
    43 // Module parameters
    44 #define FREQUENCY 100
    44 #define FREQUENCY 100
    45 
    45 
    46 // Optional features (comment to disable)
    46 // Optional features
    47 #define CONFIGURE_PDOS
    47 #define CONFIGURE_PDOS  1
    48 #define EXTERNAL_MEMORY
    48 #define EL3162_ALT_PDOS 0
    49 #define SDO_ACCESS
    49 #define EXTERNAL_MEMORY 1
       
    50 #define SDO_ACCESS      0
    50 
    51 
    51 #define PFX "ec_mini: "
    52 #define PFX "ec_mini: "
    52 
    53 
    53 /*****************************************************************************/
    54 /*****************************************************************************/
    54 
    55 
    79 
    80 
    80 static unsigned int off_ana_in; // offsets for Pdo entries
    81 static unsigned int off_ana_in; // offsets for Pdo entries
    81 static unsigned int off_dig_out;
    82 static unsigned int off_dig_out;
    82 
    83 
    83 const static ec_pdo_entry_reg_t domain1_regs[] = {
    84 const static ec_pdo_entry_reg_t domain1_regs[] = {
       
    85 #if EL3162_ALT_PDOS
       
    86     {AnaInSlavePos,  Beckhoff_EL3162, 0x6401, 2, &off_ana_in},
       
    87 #else
    84     {AnaInSlavePos,  Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
    88     {AnaInSlavePos,  Beckhoff_EL3162, 0x3101, 2, &off_ana_in},
       
    89 #endif
    85     {DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    90     {DigOutSlavePos, Beckhoff_EL2004, 0x3001, 1, &off_dig_out},
    86     {}
    91     {}
    87 };
    92 };
    88 
    93 
    89 static unsigned int counter = 0;
    94 static unsigned int counter = 0;
    90 static unsigned int blink = 0;
    95 static unsigned int blink = 0;
    91 
    96 
    92 /*****************************************************************************/
    97 /*****************************************************************************/
    93 
    98 
    94 #ifdef CONFIGURE_PDOS
    99 #if CONFIGURE_PDOS
    95 static ec_pdo_entry_info_t el3162_channel1[] = {
   100 
    96     {0x3101, 1,  8}, // status
   101 static ec_pdo_entry_info_t el3162_pdo_entries[] = {
    97     {0x3101, 2, 16}  // value
   102     {0x3101, 1,  8}, // channel 1 status
    98 };
   103     {0x3101, 2, 16}, // channel 1 value
    99 
   104     {0x3102, 1,  8}, // channel 2 status
   100 static ec_pdo_entry_info_t el3162_channel2[] = {
   105     {0x3102, 2, 16}, // channel 2 value
   101     {0x3102, 1,  8}, // status
   106     {0x6401, 1, 16}, // channel 1 value (alt.)
   102     {0x3102, 2, 16}  // value
   107     {0x6401, 2, 16}  // channel 2 value (alt.)
   103 };
   108 };
   104 
   109 
       
   110 #if EL3162_ALT_PDOS
   105 static ec_pdo_info_t el3162_pdos[] = {
   111 static ec_pdo_info_t el3162_pdos[] = {
   106     {0x1A00, 2, el3162_channel1},
   112     {0x1A10, 2, el3162_pdo_entries + 4},
   107     {0x1A01, 2, el3162_channel2}
   113 };
       
   114 
       
   115 static ec_sync_info_t el3162_syncs[] = {
       
   116     {2, EC_DIR_OUTPUT},
       
   117     {3, EC_DIR_INPUT, 1, el3162_pdos},
       
   118     {0xff}
       
   119 };
       
   120 #else
       
   121 static ec_pdo_info_t el3162_pdos[] = {
       
   122     {0x1A00, 2, el3162_pdo_entries},
       
   123     {0x1A01, 2, el3162_pdo_entries + 2}
   108 };
   124 };
   109 
   125 
   110 static ec_sync_info_t el3162_syncs[] = {
   126 static ec_sync_info_t el3162_syncs[] = {
   111     {2, EC_DIR_OUTPUT},
   127     {2, EC_DIR_OUTPUT},
   112     {3, EC_DIR_INPUT, 2, el3162_pdos},
   128     {3, EC_DIR_INPUT, 2, el3162_pdos},
   113     {0xff}
   129     {0xff}
   114 };
   130 };
       
   131 #endif
   115 
   132 
   116 static ec_pdo_entry_info_t el2004_channels[] = {
   133 static ec_pdo_entry_info_t el2004_channels[] = {
   117     {0x3001, 1, 1}, // Value 1
   134     {0x3001, 1, 1}, // Value 1
   118     {0x3001, 2, 1}, // Value 2
   135     {0x3001, 2, 1}, // Value 2
   119     {0x3001, 3, 1}, // Value 3
   136     {0x3001, 3, 1}, // Value 3
   134 };
   151 };
   135 #endif
   152 #endif
   136 
   153 
   137 /*****************************************************************************/
   154 /*****************************************************************************/
   138 
   155 
   139 #ifdef SDO_ACCESS
   156 #if SDO_ACCESS
   140 static ec_sdo_request_t *sdo;
   157 static ec_sdo_request_t *sdo;
   141 #endif
   158 #endif
   142 
   159 
   143 /*****************************************************************************/
   160 /*****************************************************************************/
   144 
   161 
   199     sc_ana_in_state = s;
   216     sc_ana_in_state = s;
   200 }
   217 }
   201 
   218 
   202 /*****************************************************************************/
   219 /*****************************************************************************/
   203 
   220 
   204 #ifdef SDO_ACCESS
   221 #if SDO_ACCESS
   205 void read_sdo(void)
   222 void read_sdo(void)
   206 {
   223 {
   207     switch (ecrt_sdo_request_state(sdo)) {
   224     switch (ecrt_sdo_request_state(sdo)) {
   208         case EC_SDO_REQUEST_UNUSED: // request was not used yet
   225         case EC_SDO_REQUEST_UNUSED: // request was not used yet
   209             ecrt_sdo_request_read(sdo); // trigger first read
   226             ecrt_sdo_request_read(sdo); // trigger first read
   249         check_master_state();
   266         check_master_state();
   250 
   267 
   251         // check for islave configuration state(s) (optional)
   268         // check for islave configuration state(s) (optional)
   252         check_slave_config_states();
   269         check_slave_config_states();
   253         
   270         
   254 #ifdef SDO_ACCESS
   271 #if SDO_ACCESS
   255         // read process data Sdo
   272         // read process data Sdo
   256         read_sdo();
   273         read_sdo();
   257 #endif
   274 #endif
   258     }
   275     }
   259 
   276 
   288 
   305 
   289 /*****************************************************************************/
   306 /*****************************************************************************/
   290 
   307 
   291 int __init init_mini_module(void)
   308 int __init init_mini_module(void)
   292 {
   309 {
   293 #ifdef CONFIGURE_PDOS
   310 #if CONFIGURE_PDOS
   294     ec_slave_config_t *sc;
   311     ec_slave_config_t *sc;
   295 #endif
   312 #endif
   296 #ifdef EXTERNAL_MEMORY
   313 #if EXTERNAL_MEMORY
   297     unsigned int size;
   314     unsigned int size;
   298 #endif
   315 #endif
   299     
   316     
   300     printk(KERN_INFO PFX "Starting...\n");
   317     printk(KERN_INFO PFX "Starting...\n");
   301 
   318 
   316                     master, AnaInSlavePos, Beckhoff_EL3162))) {
   333                     master, AnaInSlavePos, Beckhoff_EL3162))) {
   317         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   334         printk(KERN_ERR PFX "Failed to get slave configuration.\n");
   318         goto out_release_master;
   335         goto out_release_master;
   319     }
   336     }
   320 
   337 
   321 #ifdef CONFIGURE_PDOS
   338 #if CONFIGURE_PDOS
   322     printk(KERN_INFO PFX "Configuring Pdos...\n");
   339     printk(KERN_INFO PFX "Configuring Pdos...\n");
   323     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3162_syncs)) {
   340     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3162_syncs)) {
   324         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
   341         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
   325         goto out_release_master;
   342         goto out_release_master;
   326     }
   343     }
   335         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
   352         printk(KERN_ERR PFX "Failed to configure Pdos.\n");
   336         goto out_release_master;
   353         goto out_release_master;
   337     }
   354     }
   338 #endif
   355 #endif
   339 
   356 
   340 #ifdef SDO_ACCESS
   357 #if SDO_ACCESS
   341     printk(KERN_INFO PFX "Creating Sdo requests...\n");
   358     printk(KERN_INFO PFX "Creating Sdo requests...\n");
   342     if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) {
   359     if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) {
   343         printk(KERN_ERR PFX "Failed to create Sdo request.\n");
   360         printk(KERN_ERR PFX "Failed to create Sdo request.\n");
   344         goto out_release_master;
   361         goto out_release_master;
   345     }
   362     }
   350     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
   367     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
   351         printk(KERN_ERR PFX "Pdo entry registration failed!\n");
   368         printk(KERN_ERR PFX "Pdo entry registration failed!\n");
   352         goto out_release_master;
   369         goto out_release_master;
   353     }
   370     }
   354 
   371 
   355 #ifdef EXTERNAL_MEMORY
   372 #if EXTERNAL_MEMORY
   356     if ((size = ecrt_domain_size(domain1))) {
   373     if ((size = ecrt_domain_size(domain1))) {
   357         if (!(domain1_pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
   374         if (!(domain1_pd = (uint8_t *) kmalloc(size, GFP_KERNEL))) {
   358             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"
   375             printk(KERN_ERR PFX "Failed to allocate %u bytes of process data"
   359                     " memory!\n", size);
   376                     " memory!\n", size);
   360             goto out_release_master;
   377             goto out_release_master;
   364 #endif
   381 #endif
   365 
   382 
   366     printk(KERN_INFO PFX "Activating master...\n");
   383     printk(KERN_INFO PFX "Activating master...\n");
   367     if (ecrt_master_activate(master)) {
   384     if (ecrt_master_activate(master)) {
   368         printk(KERN_ERR PFX "Failed to activate master!\n");
   385         printk(KERN_ERR PFX "Failed to activate master!\n");
   369 #ifdef EXTERNAL_MEMORY
   386 #if EXTERNAL_MEMORY
   370         goto out_free_process_data;
   387         goto out_free_process_data;
   371 #else
   388 #else
   372         goto out_release_master;
   389         goto out_release_master;
   373 #endif
   390 #endif
   374     }
   391     }
   375 
   392 
   376 #ifndef EXTERNAL_MEMORY
   393 #if !EXTERNAL_MEMORY
   377     // Get internal process data for domain
   394     // Get internal process data for domain
   378     domain1_pd = ecrt_domain_data(domain1);
   395     domain1_pd = ecrt_domain_data(domain1);
   379 #endif
   396 #endif
   380 
   397 
   381     printk(KERN_INFO PFX "Starting cyclic sample thread.\n");
   398     printk(KERN_INFO PFX "Starting cyclic sample thread.\n");
   385     add_timer(&timer);
   402     add_timer(&timer);
   386 
   403 
   387     printk(KERN_INFO PFX "Started.\n");
   404     printk(KERN_INFO PFX "Started.\n");
   388     return 0;
   405     return 0;
   389 
   406 
   390 #ifdef EXTERNAL_MEMORY
   407 #if EXTERNAL_MEMORY
   391 out_free_process_data:
   408 out_free_process_data:
   392     kfree(domain1_pd);
   409     kfree(domain1_pd);
   393 #endif
   410 #endif
   394 out_release_master:
   411 out_release_master:
   395     printk(KERN_ERR PFX "Releasing master...\n");
   412     printk(KERN_ERR PFX "Releasing master...\n");
   405 {
   422 {
   406     printk(KERN_INFO PFX "Stopping...\n");
   423     printk(KERN_INFO PFX "Stopping...\n");
   407 
   424 
   408     del_timer_sync(&timer);
   425     del_timer_sync(&timer);
   409 
   426 
   410 #ifdef EXTERNAL_MEMORY
   427 #if EXTERNAL_MEMORY
   411     kfree(domain1_pd);
   428     kfree(domain1_pd);
   412 #endif
   429 #endif
   413 
   430 
   414     printk(KERN_INFO PFX "Releasing master...\n");
   431     printk(KERN_INFO PFX "Releasing master...\n");
   415     ecrt_release_master(master);
   432     ecrt_release_master(master);