mini/ec_mini.c
changeset 0 05c992bf5847
child 5 6f2508af550c
equal deleted inserted replaced
-1:000000000000 0:05c992bf5847
       
     1 /******************************************************************************
       
     2  *
       
     3  * ec_mini.c
       
     4  *
       
     5  * Minimalmodul für EtherCAT
       
     6  *           
       
     7  * $Id$
       
     8  *
       
     9  ******************************************************************************/
       
    10 
       
    11 #include <linux/module.h>
       
    12 #include <linux/tqueue.h>
       
    13 #include <linux/slab.h>
       
    14 #include <linux/delay.h>
       
    15 
       
    16 #include "../drivers/ec_master.h"
       
    17 #include "../drivers/ec_device.h"
       
    18 #include "../drivers/ec_types.h"
       
    19 #include "../drivers/ec_dbg.h"
       
    20 
       
    21 extern EtherCAT_device_t rtl_ecat_dev;
       
    22 
       
    23 //static EtherCAT_master_t *ecat_master = NULL;
       
    24 
       
    25 #if 0
       
    26 static EtherCAT_slave_t ecat_slaves[] =
       
    27 {
       
    28   // Block 1
       
    29   ECAT_INIT_SLAVE(Beckhoff_EK1100),
       
    30   ECAT_INIT_SLAVE(Beckhoff_EL4102),
       
    31   ECAT_INIT_SLAVE(Beckhoff_EL3162),
       
    32   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    33   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    34   ECAT_INIT_SLAVE(Beckhoff_EL3102),
       
    35   ECAT_INIT_SLAVE(Beckhoff_EL4102),
       
    36   ECAT_INIT_SLAVE(Beckhoff_EL4102),
       
    37   ECAT_INIT_SLAVE(Beckhoff_EL4102),
       
    38   ECAT_INIT_SLAVE(Beckhoff_EL3162),
       
    39   ECAT_INIT_SLAVE(Beckhoff_EL3162),
       
    40   ECAT_INIT_SLAVE(Beckhoff_EL3162),
       
    41   ECAT_INIT_SLAVE(Beckhoff_EL3102),
       
    42   ECAT_INIT_SLAVE(Beckhoff_EL3102),
       
    43   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    44   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    45   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    46   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    47 
       
    48   // Block 2
       
    49   ECAT_INIT_SLAVE(Beckhoff_EK1100),
       
    50   ECAT_INIT_SLAVE(Beckhoff_EL4102),
       
    51   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    52   ECAT_INIT_SLAVE(Beckhoff_EL3162),
       
    53   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    54   ECAT_INIT_SLAVE(Beckhoff_EL3102),
       
    55 
       
    56   // Block 3
       
    57   ECAT_INIT_SLAVE(Beckhoff_EK1100),
       
    58   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    59   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    60   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    61   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    62   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    63   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    64   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    65   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    66   ECAT_INIT_SLAVE(Beckhoff_EL2004),
       
    67   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    68   ECAT_INIT_SLAVE(Beckhoff_EL1014),
       
    69   ECAT_INIT_SLAVE(Beckhoff_EL1014)
       
    70 };
       
    71 
       
    72 #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t))
       
    73 #endif
       
    74 
       
    75 double value;
       
    76 int dig1;
       
    77 
       
    78 /******************************************************************************
       
    79  *
       
    80  * Function: next2004
       
    81  *
       
    82  *****************************************************************************/
       
    83 
       
    84 #if 0
       
    85 static int next2004(int *wrap)
       
    86 {
       
    87   static int i = 0;
       
    88   unsigned int j = 0;
       
    89 
       
    90   *wrap = 0;
       
    91 
       
    92   for (j = 0; j < ECAT_SLAVES_COUNT; j++)
       
    93   {
       
    94     i++;
       
    95 
       
    96     i %= ECAT_SLAVES_COUNT;
       
    97 
       
    98     if (i == 0) *wrap = 1;
       
    99 
       
   100     if (ecat_slaves[i].desc == Beckhoff_EL2004)
       
   101     {
       
   102       return i;
       
   103     }
       
   104   }
       
   105 
       
   106   return -1;
       
   107 }
       
   108 #endif
       
   109 
       
   110 /******************************************************************************
       
   111  *
       
   112  * Function: msr_controller
       
   113  *
       
   114  * Beschreibung: Zyklischer Prozess
       
   115  *
       
   116  *****************************************************************************/
       
   117 
       
   118 #if 0
       
   119 void msr_controller()
       
   120 {
       
   121   static int ms = 0;
       
   122   static int cnt = 0;
       
   123   static unsigned long int k = 0;
       
   124   static int firstrun = 1;
       
   125   
       
   126   static int klemme = 12;
       
   127   static int kanal = 0;
       
   128   static int up_down = 0;
       
   129   int wrap = 0;
       
   130 
       
   131   ms++;
       
   132   ms %= 1000;
       
   133 
       
   134 #if 0
       
   135   ecat_tx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->tx_time-k))
       
   136     / (current_cpu_data.loops_per_jiffy / 10);  
       
   137   ecat_rx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->rx_time-k))
       
   138     / (current_cpu_data.loops_per_jiffy / 10);  
       
   139   
       
   140   rx_intr = ecat_master->dev->rx_intr_cnt;
       
   141   tx_intr = ecat_master->dev->tx_intr_cnt;
       
   142   total_intr = ecat_master->dev->intr_cnt;
       
   143 #endif
       
   144 
       
   145   // Prozessdaten lesen
       
   146   if (!firstrun)
       
   147   {
       
   148     EtherCAT_read_process_data(ecat_master);
       
   149 
       
   150     // Daten lesen und skalieren
       
   151     value = EtherCAT_read_value(&ecat_master->slaves[5], 0) / 3276.7;
       
   152     dig1 = EtherCAT_read_value(&ecat_master->slaves[3], 0);
       
   153   }
       
   154 
       
   155   // Daten schreiben
       
   156   EtherCAT_write_value(&ecat_master->slaves[4], 0, ms > 500 ? 1 : 0);
       
   157   EtherCAT_write_value(&ecat_master->slaves[4], 1, ms > 500 ? 0 : 1);
       
   158   EtherCAT_write_value(&ecat_master->slaves[4], 2, ms > 500 ? 0 : 1);
       
   159   EtherCAT_write_value(&ecat_master->slaves[4], 3, ms > 500 ? 1 : 0);
       
   160 
       
   161   if (cnt++ > 20)
       
   162   {
       
   163     cnt = 0;
       
   164 
       
   165     if (++kanal > 3)
       
   166     {
       
   167       kanal = 0;
       
   168       klemme = next2004(&wrap);
       
   169 
       
   170       if (wrap == 1)
       
   171       { 
       
   172         if (up_down == 1) up_down = 0;
       
   173         else up_down = 1;
       
   174       }
       
   175     }
       
   176   }
       
   177 
       
   178   if (klemme >= 0)
       
   179     EtherCAT_write_value(&ecat_master->slaves[klemme], kanal,up_down);
       
   180   
       
   181   //EtherCAT_write_value(&ecat_master->slaves[13], 1, ms > 500 ? 0 : 1);
       
   182   //EtherCAT_write_value(&ecat_master->slaves[14], 2, ms > 500 ? 0 : 1);
       
   183   //EtherCAT_write_value(&ecat_master->slaves[15], 3, ms > 500 ? 1 : 0);
       
   184   
       
   185   // Prozessdaten schreiben
       
   186   rdtscl(k);
       
   187   EtherCAT_write_process_data(ecat_master);
       
   188   firstrun = 0;
       
   189 }
       
   190 #endif
       
   191 
       
   192 /******************************************************************************
       
   193 *
       
   194 * Function: init
       
   195 *
       
   196 ******************************************************************************/
       
   197 
       
   198 //#define ECAT_OPEN
       
   199 
       
   200 int init()
       
   201 {   
       
   202 #ifdef ECAT_OPEN
       
   203   int rv = -1;
       
   204 #endif
       
   205 
       
   206   EC_DBG(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
       
   207 
       
   208   EtherCAT_device_debug(&rtl_ecat_dev);
       
   209   //mdelay(5000);
       
   210 
       
   211 #ifdef ECAT_OPEN
       
   212   EC_DBG("Opening EtherCAT device.\n");
       
   213 
       
   214   // HIER PASSIERT DER FEHLER:
       
   215   if (EtherCAT_device_open(&rtl_ecat_dev) < 0)
       
   216   {
       
   217     EC_DBG(KERN_ERR "msr_modul: Could not initialize EtherCAT NIC.\n");
       
   218     goto out_nothing;
       
   219   }
       
   220 
       
   221   if (!rtl_ecat_dev.dev) // Es gibt kein EtherCAT-Device
       
   222   {  
       
   223     EC_DBG(KERN_ERR "msr_modul: No EtherCAT device!\n");
       
   224     goto out_close;
       
   225   }
       
   226 #endif
       
   227 
       
   228 #if 0
       
   229   // EtherCAT-Master und Slaves initialisieren
       
   230   EC_DBG("Initialising EtherCAT master\n");
       
   231 
       
   232   if ((ecat_master = (EtherCAT_master_t *) kmalloc(sizeof(EtherCAT_master_t), GFP_KERNEL)) == 0)
       
   233   {
       
   234     EC_DBG(KERN_ERR "msr_modul: Could not alloc memory for EtherCAT master!\n");
       
   235     goto out_close;
       
   236   }
       
   237 
       
   238   if (EtherCAT_master_init(ecat_master, &rtl_ecat_dev) < 0)
       
   239   {
       
   240     EC_DBG(KERN_ERR "EtherCAT could not init master!\n");
       
   241     goto out_master;
       
   242   }
       
   243 #endif
       
   244 
       
   245 #if 0
       
   246   EC_DBG("Checking EtherCAT slaves.\n");
       
   247 
       
   248   if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0)
       
   249   {
       
   250     EC_DBG(KERN_ERR "EtherCAT: Could not init slaves!\n");
       
   251     goto out_masterclear;
       
   252   }
       
   253 
       
   254   EC_DBG("Activating all EtherCAT slaves.\n");
       
   255 
       
   256   if (EtherCAT_activate_all_slaves(ecat_master) != 0)
       
   257   {
       
   258     EC_DBG(KERN_ERR "EtherCAT: Could not activate slaves!\n");
       
   259     goto out_masterclear;
       
   260   }
       
   261 
       
   262   // Zyklischen Aufruf starten
       
   263 
       
   264   EC_DBG("Starting cyclic sample thread.\n");
       
   265 
       
   266   EtherCAT_write_process_data(ecat_master);
       
   267 
       
   268   EC_DBG("Initialised sample thread.\n");
       
   269 #endif
       
   270 
       
   271   EC_DBG(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
       
   272 
       
   273   return 0;
       
   274 
       
   275 #if 0
       
   276  out_masterclear:
       
   277   EC_DBG(KERN_INFO "Clearing EtherCAT master.\n");
       
   278   EtherCAT_master_clear(ecat_master);
       
   279 #endif
       
   280 
       
   281 #if 0
       
   282  out_master:
       
   283   EC_DBG(KERN_INFO "Freeing EtherCAT master.\n");
       
   284   kfree(ecat_master);
       
   285 #endif
       
   286 
       
   287 #ifdef ECAT_OPEN
       
   288  out_close:
       
   289   EC_DBG(KERN_INFO "Closing device.\n");
       
   290   EtherCAT_device_close(&rtl_ecat_dev);
       
   291 
       
   292  out_nothing:
       
   293   return rv;
       
   294 #endif
       
   295 }
       
   296 
       
   297 /******************************************************************************
       
   298 *
       
   299 * Function: cleanup
       
   300 *
       
   301 ******************************************************************************/
       
   302 
       
   303 void cleanup()
       
   304 {
       
   305   EC_DBG(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
       
   306 
       
   307   // Noch einmal lesen
       
   308   //EC_DBG(KERN_INFO "Reading process data.\n");
       
   309   //EtherCAT_read_process_data(ecat_master);
       
   310 
       
   311 #if 0
       
   312   if (ecat_master)
       
   313   {
       
   314 #if 0
       
   315     EC_DBG(KERN_INFO "Deactivating slaves.\n");
       
   316     EtherCAT_deactivate_all_slaves(ecat_master);
       
   317 #endif
       
   318 
       
   319     EC_DBG(KERN_INFO "Clearing EtherCAT master.\n");
       
   320     EtherCAT_master_clear(ecat_master);
       
   321 
       
   322     EC_DBG(KERN_INFO "Freeing EtherCAT master.\n");
       
   323     kfree(ecat_master);
       
   324     ecat_master = NULL;
       
   325   }
       
   326 #endif
       
   327 
       
   328 #ifdef ECAT_OPEN
       
   329   EC_DBG(KERN_INFO "Closing device.\n");
       
   330   EtherCAT_device_close(&rtl_ecat_dev);
       
   331 #endif
       
   332 
       
   333   EC_DBG(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
       
   334 }
       
   335 
       
   336 /*****************************************************************************/
       
   337 
       
   338 MODULE_LICENSE("GPL");
       
   339 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
       
   340 MODULE_DESCRIPTION ("Minimal EtherCAT environment");
       
   341 
       
   342 module_init(init);
       
   343 module_exit(cleanup);
       
   344 
       
   345 /*****************************************************************************/