mini/ec_mini.c
branchkernel2.6
changeset 27 d75ef6b46e33
parent 26 60435f959e5c
child 28 801dc7eabf51
equal deleted inserted replaced
26:60435f959e5c 27:d75ef6b46e33
    13 #include <linux/timer.h>
    13 #include <linux/timer.h>
    14 
    14 
    15 #include "../drivers/ec_master.h"
    15 #include "../drivers/ec_master.h"
    16 #include "../drivers/ec_device.h"
    16 #include "../drivers/ec_device.h"
    17 #include "../drivers/ec_types.h"
    17 #include "../drivers/ec_types.h"
       
    18 #include "../drivers/ec_module.h"
    18 
    19 
    19 /******************************************************************************/
    20 /******************************************************************************/
    20 
    21 
    21 #define ECAT
    22 // Auskommentieren, wenn keine zyklischen Daten erwuenscht
    22 
       
    23 #define ECAT_OPEN
       
    24 #define ECAT_MASTER
       
    25 #define ECAT_SLAVES
       
    26 #define ECAT_CYCLIC_DATA
    23 #define ECAT_CYCLIC_DATA
    27 
    24 
    28 /******************************************************************************/
    25 /******************************************************************************/
    29 
    26 
    30 #ifdef ECAT
       
    31 
       
    32 extern EtherCAT_device_t rtl_ecat_dev;
       
    33 
       
    34 #ifdef ECAT_MASTER
       
    35 static EtherCAT_master_t *ecat_master = NULL;
    27 static EtherCAT_master_t *ecat_master = NULL;
    36 #endif
    28 
    37 
       
    38 #ifdef ECAT_SLAVES
       
    39 static EtherCAT_slave_t ecat_slaves[] =
    29 static EtherCAT_slave_t ecat_slaves[] =
    40 {
    30 {
    41 #if 0
    31 #if 0
    42     // Block 1
    32     // Block 1
    43     ECAT_INIT_SLAVE(Beckhoff_EK1100),
    33     ECAT_INIT_SLAVE(Beckhoff_EK1100),
    89 #endif
    79 #endif
    90 };
    80 };
    91 
    81 
    92 #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t))
    82 #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t))
    93 
    83 
    94 #endif // ECAT_SLAVES
       
    95 
       
    96 #endif // ECAT
       
    97 
       
    98 #ifdef ECAT_CYCLIC_DATA
    84 #ifdef ECAT_CYCLIC_DATA
    99 
    85 
   100 int value;
    86 int value;
   101 int dig1;
    87 int dig1;
   102 
    88 
   109  *
    95  *
   110  * Function: next2004
    96  * Function: next2004
   111  *
    97  *
   112  *****************************************************************************/
    98  *****************************************************************************/
   113 
    99 
   114 #ifdef ECAT
       
   115 #ifdef ECAT_CYCLIC_DATA
   100 #ifdef ECAT_CYCLIC_DATA
   116 
   101 
   117 static int next2004(int *wrap)
   102 static int next2004(int *wrap)
   118 {
   103 {
   119     static int i = 0;
   104     static int i = 0;
   136     }
   121     }
   137 
   122 
   138     return -1;
   123     return -1;
   139 }
   124 }
   140 #endif
   125 #endif
   141 #endif
       
   142 
   126 
   143 /******************************************************************************
   127 /******************************************************************************
   144  *
   128  *
   145  * Function: run
   129  * Function: run
   146  *
   130  *
   150 
   134 
   151 #ifdef ECAT_CYCLIC_DATA
   135 #ifdef ECAT_CYCLIC_DATA
   152 
   136 
   153 static void run(unsigned long data)
   137 static void run(unsigned long data)
   154 {
   138 {
   155 #ifdef ECAT
       
   156     static int ms = 0;
   139     static int ms = 0;
   157     static int cnt = 0;
   140     static int cnt = 0;
   158     static unsigned long int k = 0;
   141     static unsigned long int k = 0;
   159     static int firstrun = 1;
   142     static int firstrun = 1;
   160 
   143 
   228 
   211 
   229     // Prozessdaten schreiben
   212     // Prozessdaten schreiben
   230     rdtscl(k);
   213     rdtscl(k);
   231     EtherCAT_write_process_data(ecat_master);
   214     EtherCAT_write_process_data(ecat_master);
   232     firstrun = 0;
   215     firstrun = 0;
   233 #endif
       
   234 
   216 
   235     timer.expires += HZ / 1000;
   217     timer.expires += HZ / 1000;
   236     add_timer(&timer);
   218     add_timer(&timer);
   237 }
   219 }
   238 
   220 
   244  *
   226  *
   245  ******************************************************************************/
   227  ******************************************************************************/
   246 
   228 
   247 int __init init_module()
   229 int __init init_module()
   248 {
   230 {
   249 #ifdef ECAT
   231   printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   250 #ifdef ECAT_OPEN
   232 
   251     int rv = -1;
   233   if ((ecat_master = EtherCAT_master(0)) == NULL)
   252 #endif
   234   {
   253 #endif
   235     printk(KERN_ERR "No EtherCAT master available!\n");
   254 
   236     return -1;
   255     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   237   }
   256 
   238 
   257 #ifdef ECAT
   239   printk("Checking EtherCAT slaves.\n");
   258 
   240 
   259     EtherCAT_device_debug(&rtl_ecat_dev);
   241   if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0)
   260 
   242   {
   261 #ifdef ECAT_OPEN
   243     printk(KERN_ERR "EtherCAT: Could not init slaves!\n");
   262     printk("Opening EtherCAT device.\n");
   244     return -1;
   263 
   245   }
   264     // HIER PASSIERT DER FEHLER:
   246 
   265     if (EtherCAT_device_open(&rtl_ecat_dev) < 0)
   247   printk("Activating all EtherCAT slaves.\n");
   266     {
   248 
   267         printk(KERN_ERR "msr_modul: Could not initialize EtherCAT NIC.\n");
   249   if (EtherCAT_activate_all_slaves(ecat_master) != 0)
   268         goto out_nothing;
   250   {
   269     }
   251     printk(KERN_ERR "EtherCAT: Could not activate slaves!\n");
   270 
   252     return -1;
   271     if (!rtl_ecat_dev.dev) // Es gibt kein EtherCAT-Device
   253   }
   272     {
   254 
   273         printk(KERN_ERR "msr_modul: No EtherCAT device!\n");
   255 #ifdef ECAT_CYCLIC_DATA
   274         goto out_close;
   256   printk("Starting cyclic sample thread.\n");
   275     }
   257 
   276 #endif // ECAT_OPEN
   258   schedule();
   277 
   259   mdelay(1000);
   278 #ifdef ECAT_MASTER
   260   schedule();
   279     printk("Initialising EtherCAT master\n");
   261   init_timer(&timer);
   280 
   262 
   281     if ((ecat_master = (EtherCAT_master_t *) kmalloc(sizeof(EtherCAT_master_t), GFP_KERNEL)) == 0)
   263   timer.function = run;
   282     {
   264   timer.data = 0;
   283         printk(KERN_ERR "msr_modul: Could not alloc memory for EtherCAT master!\n");
   265   timer.expires = jiffies+10; // Das erste Mal sofort feuern
   284         goto out_close;
   266   last_start_jiffies = timer.expires;
   285     }
   267   add_timer(&timer);
   286 
   268 
   287     if (EtherCAT_master_init(ecat_master, &rtl_ecat_dev) < 0)
   269   printk("Initialised sample thread.\n");
   288     {
   270 #endif
   289         printk(KERN_ERR "EtherCAT could not init master!\n");
   271 
   290         goto out_master;
   272   printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
   291     }
   273 
   292 
   274   return 0;
   293     //ecat_master->debug_level = 1;
       
   294 
       
   295 #endif // ECAT_MASTER
       
   296 
       
   297 #ifdef ECAT_SLAVES
       
   298     printk("Checking EtherCAT slaves.\n");
       
   299 
       
   300     if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0)
       
   301     {
       
   302         printk(KERN_ERR "EtherCAT: Could not init slaves!\n");
       
   303         goto out_masterclear;
       
   304     }
       
   305 
       
   306     printk("Activating all EtherCAT slaves.\n");
       
   307 
       
   308     if (EtherCAT_activate_all_slaves(ecat_master) != 0)
       
   309     {
       
   310         printk(KERN_ERR "EtherCAT: Could not activate slaves!\n");
       
   311         goto out_masterclear;
       
   312     }
       
   313 #endif
       
   314 
       
   315 #endif // ECAT
       
   316 
       
   317 #ifdef ECAT_CYCLIC_DATA
       
   318     printk("Starting cyclic sample thread.\n");
       
   319 
       
   320     schedule();
       
   321     mdelay(1000);
       
   322     schedule();
       
   323     init_timer(&timer);
       
   324 
       
   325     timer.function = run;
       
   326     timer.data = 0;
       
   327     timer.expires = jiffies+10; // Das erste Mal sofort feuern
       
   328     last_start_jiffies = timer.expires;
       
   329     add_timer(&timer);
       
   330 
       
   331     printk("Initialised sample thread.\n");
       
   332 #endif
       
   333 
       
   334     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
       
   335 
       
   336     return 0;
       
   337 
       
   338 #ifdef ECAT
       
   339 
       
   340 #ifdef ECAT_SLAVES
       
   341  out_masterclear:
       
   342     printk(KERN_INFO "Clearing EtherCAT master.\n");
       
   343     EtherCAT_master_clear(ecat_master);
       
   344 #endif
       
   345 
       
   346 #ifdef ECAT_MASTER
       
   347  out_master:
       
   348     printk(KERN_INFO "Freeing EtherCAT master.\n");
       
   349     kfree(ecat_master);
       
   350 #endif
       
   351 
       
   352 #ifdef ECAT_OPEN
       
   353  out_close:
       
   354     printk(KERN_INFO "Closing device.\n");
       
   355     EtherCAT_device_close(&rtl_ecat_dev);
       
   356 
       
   357  out_nothing:
       
   358     return rv;
       
   359 #endif
       
   360 
       
   361 #endif // ECAT
       
   362 }
   275 }
   363 
   276 
   364 /******************************************************************************
   277 /******************************************************************************
   365  *
   278  *
   366  * Function: cleanup
   279  * Function: cleanup
   369 
   282 
   370 void __exit cleanup_module()
   283 void __exit cleanup_module()
   371 {
   284 {
   372     printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
   285     printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
   373 
   286 
   374 #ifdef ECAT_MASTER
       
   375 
       
   376 #ifdef ECAT
       
   377     if (ecat_master)
   287     if (ecat_master)
   378     {
   288     {
   379 #endif
   289 #ifdef ECAT_CYCLIC_DATA
   380         //ecat_master->debug_level = 1;
   290       del_timer_sync(&timer);
   381 
   291       EtherCAT_clear_process_data(ecat_master);
   382 #ifdef ECAT_CYCLIC_DATA
       
   383 
       
   384         del_timer_sync(&timer);
       
   385 
       
   386 #ifdef ECAT
       
   387         EtherCAT_clear_process_data(ecat_master);
       
   388 #endif
       
   389 
       
   390 #endif // ECAT_CYCLIC_DATA
   292 #endif // ECAT_CYCLIC_DATA
   391 
   293 
   392 #ifdef ECAT
   294       printk(KERN_INFO "Deactivating slaves.\n");
   393 
   295       EtherCAT_deactivate_all_slaves(ecat_master);
   394 #ifdef ECAT_SLAVES
   296     }
   395         printk(KERN_INFO "Deactivating slaves.\n");
       
   396         EtherCAT_deactivate_all_slaves(ecat_master);
       
   397 #endif
       
   398 
       
   399         printk(KERN_INFO "Clearing EtherCAT master.\n");
       
   400         EtherCAT_master_clear(ecat_master);
       
   401 
       
   402         printk(KERN_INFO "Freeing EtherCAT master.\n");
       
   403         kfree(ecat_master);
       
   404         ecat_master = NULL;
       
   405     }
       
   406 #endif // ECAT
       
   407 
       
   408 #endif // ECAT_MASTER
       
   409 
       
   410 #ifdef ECAT
       
   411 #ifdef ECAT_OPEN
       
   412     printk(KERN_INFO "Closing device.\n");
       
   413     EtherCAT_device_close(&rtl_ecat_dev);
       
   414 #endif
       
   415 #endif
       
   416 
   297 
   417     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   298     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   418 }
   299 }
   419 
   300 
   420 /*****************************************************************************/
   301 /*****************************************************************************/