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