mini/ec_mini.c
changeset 42 a22a202d0f42
parent 36 6e9de6fbf74c
child 47 ad5f969f263b
equal deleted inserted replaced
41:42c66194c0c8 42:a22a202d0f42
     4  *
     4  *
     5  * Minimalmodul für EtherCAT
     5  * Minimalmodul für EtherCAT
     6  *
     6  *
     7  * $Id$
     7  * $Id$
     8  *
     8  *
     9  ******************************************************************************/
     9  *****************************************************************************/
    10 
    10 
    11 #include <linux/module.h>
    11 #include <linux/module.h>
    12 #include <linux/delay.h>
    12 #include <linux/delay.h>
    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 #include "../drivers/ec_module.h"
    19 
    19 
    20 /******************************************************************************/
    20 /*****************************************************************************/
    21 
    21 
    22 // Auskommentieren, wenn keine zyklischen Daten erwuenscht
    22 // Auskommentieren, wenn keine zyklischen Daten erwuenscht
    23 #define ECAT_CYCLIC_DATA
    23 #define ECAT_CYCLIC_DATA
    24 
    24 
    25 /******************************************************************************/
    25 /*****************************************************************************/
    26 
    26 
    27 static EtherCAT_master_t *ecat_master = NULL;
    27 static EtherCAT_master_t *ecat_master = NULL;
    28 
    28 
    29 static EtherCAT_slave_t ecat_slaves[] =
    29 static EtherCAT_slave_t ecat_slaves[] =
    30 {
    30 {
    31 #if 0
    31 #if 0
    32     // Block 1
    32     // Block 1
    33     ECAT_INIT_SLAVE(Beckhoff_EK1100),
    33     ECAT_INIT_SLAVE(Beckhoff_EK1100, 1),
    34     ECAT_INIT_SLAVE(Beckhoff_EL4102),
    34     ECAT_INIT_SLAVE(Beckhoff_EL4102, 1),
    35     ECAT_INIT_SLAVE(Beckhoff_EL3162),
    35     ECAT_INIT_SLAVE(Beckhoff_EL3162, 1),
    36     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    36     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    37 
    37 
    38     ECAT_INIT_SLAVE(Beckhoff_EL4102),
    38     ECAT_INIT_SLAVE(Beckhoff_EL4102, 1),
    39     ECAT_INIT_SLAVE(Beckhoff_EL4102),
    39     ECAT_INIT_SLAVE(Beckhoff_EL4102, 1),
    40     ECAT_INIT_SLAVE(Beckhoff_EL4102),
    40     ECAT_INIT_SLAVE(Beckhoff_EL4102, 1),
    41 
    41 
    42     ECAT_INIT_SLAVE(Beckhoff_EL3162),
    42     ECAT_INIT_SLAVE(Beckhoff_EL3162, 1),
    43     ECAT_INIT_SLAVE(Beckhoff_EL3162),
    43     ECAT_INIT_SLAVE(Beckhoff_EL3162, 1),
    44     ECAT_INIT_SLAVE(Beckhoff_EL3162),
    44     ECAT_INIT_SLAVE(Beckhoff_EL3162, 1),
    45     ECAT_INIT_SLAVE(Beckhoff_EL3102),
    45     ECAT_INIT_SLAVE(Beckhoff_EL3102, 1),
    46     ECAT_INIT_SLAVE(Beckhoff_EL3102),
    46     ECAT_INIT_SLAVE(Beckhoff_EL3102, 1),
    47     ECAT_INIT_SLAVE(Beckhoff_EL3102),
    47     ECAT_INIT_SLAVE(Beckhoff_EL3102, 1),
    48 
    48 
    49 #endif
    49 #endif
    50 
    50 
    51 #if 1
    51 #if 1
    52     // Block 2
    52     // Block 2
    53     ECAT_INIT_SLAVE(Beckhoff_EK1100),
    53     ECAT_INIT_SLAVE(Beckhoff_EK1100, 1),
    54     ECAT_INIT_SLAVE(Beckhoff_EL4102),
    54     ECAT_INIT_SLAVE(Beckhoff_EL4102, 1),
    55     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    55     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    56     ECAT_INIT_SLAVE(Beckhoff_EL3162),
    56     ECAT_INIT_SLAVE(Beckhoff_EL3162, 1),
    57     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    57     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    58     ECAT_INIT_SLAVE(Beckhoff_EL3102),
    58     ECAT_INIT_SLAVE(Beckhoff_EL3102, 1),
    59     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    59     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    60     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    60     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    61     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    61     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    62     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    62     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    63     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    63     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    64 
    64 
    65     // Block 3
    65     // Block 3
    66     ECAT_INIT_SLAVE(Beckhoff_EK1100),
    66     ECAT_INIT_SLAVE(Beckhoff_EK1100, 1),
    67     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    67     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    68     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    68     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    69     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    69     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    70     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    70     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    71     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    71     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    72     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    72     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    73     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    73     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    74     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    74     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    75     ECAT_INIT_SLAVE(Beckhoff_EL2004),
    75     ECAT_INIT_SLAVE(Beckhoff_EL2004, 1),
    76     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    76     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    77     ECAT_INIT_SLAVE(Beckhoff_EL1014),
    77     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1),
    78     ECAT_INIT_SLAVE(Beckhoff_EL1014)
    78     ECAT_INIT_SLAVE(Beckhoff_EL1014, 1)
    79 #endif
    79 #endif
    80 };
    80 };
    81 
    81 
    82 #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t))
    82 #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t))
    83 
    83 
   134 
   134 
   135 #ifdef ECAT_CYCLIC_DATA
   135 #ifdef ECAT_CYCLIC_DATA
   136 
   136 
   137 static void run(unsigned long data)
   137 static void run(unsigned long data)
   138 {
   138 {
   139     static int ms = 0;
   139   static int ms = 0;
   140     static int cnt = 0;
   140     static int cnt = 0;
   141     static unsigned long int k = 0;
   141     static unsigned long int k = 0;
   142     static int firstrun = 1;
   142     static int firstrun = 1;
   143 
   143 
   144     static int klemme = 0;
   144     static int klemme = 0;
   147     int wrap = 0;
   147     int wrap = 0;
   148 
   148 
   149     ms++;
   149     ms++;
   150     ms %= 1000;
   150     ms %= 1000;
   151 
   151 
   152 #if 0
   152     if (firstrun) klemme = next2004(&wrap);
   153     ecat_tx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->tx_time-k))
       
   154         / (current_cpu_data.loops_per_jiffy / 10);
       
   155     ecat_rx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->rx_time-k))
       
   156         / (current_cpu_data.loops_per_jiffy / 10);
       
   157 
       
   158     rx_intr = ecat_master->dev->rx_intr_cnt;
       
   159     tx_intr = ecat_master->dev->tx_intr_cnt;
       
   160     total_intr = ecat_master->dev->intr_cnt;
       
   161 #endif
       
   162 
       
   163     // Prozessdaten lesen
       
   164     if (!firstrun)
       
   165     {
       
   166         EtherCAT_read_process_data(ecat_master);
       
   167 
       
   168         // Daten lesen und skalieren
       
   169 //        value = EtherCAT_read_value(&ecat_master->slaves[5], 0) / 3276; //.7; FIXME kein FP im Kernel ohne Schutz !!
       
   170 //        dig1 = EtherCAT_read_value(&ecat_master->slaves[3], 0);
       
   171     }
       
   172     else
       
   173         klemme = next2004(&wrap);
       
   174 
       
   175 
       
   176 #if 0
       
   177     // Daten schreiben
       
   178     EtherCAT_write_value(&ecat_master->slaves[4], 0, ms > 500 ? 1 : 0);
       
   179     EtherCAT_write_value(&ecat_master->slaves[4], 1, ms > 500 ? 0 : 1);
       
   180     EtherCAT_write_value(&ecat_master->slaves[4], 2, ms > 500 ? 0 : 1);
       
   181     EtherCAT_write_value(&ecat_master->slaves[4], 3, ms > 500 ? 1 : 0);
       
   182 #endif
       
   183 
   153 
   184     if (cnt++ > 20)
   154     if (cnt++ > 20)
   185     {
   155     {
   186         cnt = 0;
   156         cnt = 0;
   187 
   157 
   196                 else up_down = 1;
   166                 else up_down = 1;
   197             }
   167             }
   198         }
   168         }
   199     }
   169     }
   200 
   170 
   201     if (klemme >= 0) {
   171     if (klemme >= 0)
   202         EtherCAT_write_value(&ecat_master->slaves[klemme], kanal,up_down);
   172         EtherCAT_write_value(&ecat_slaves[klemme], kanal, up_down);
   203 	//printk("ECAT write: Klemme: %d, Kanal: %d, Wert: %d\n",klemme,kanal,up_down); 
   173 
   204     }
   174     // Prozessdaten lesen und schreiben
   205 
       
   206 #if 0
       
   207     EtherCAT_write_value(&ecat_master->slaves[13], 1, ms > 500 ? 0 : 1);
       
   208     EtherCAT_write_value(&ecat_master->slaves[14], 2, ms > 500 ? 0 : 1);
       
   209     EtherCAT_write_value(&ecat_master->slaves[15], 3, ms > 500 ? 1 : 0);
       
   210 #endif
       
   211 
       
   212     // Prozessdaten schreiben
       
   213     rdtscl(k);
   175     rdtscl(k);
   214     EtherCAT_write_process_data(ecat_master);
   176     EtherCAT_process_data_cycle(ecat_master, 1);
   215     firstrun = 0;
   177     firstrun = 0;
   216 
   178 
   217     timer.expires += HZ / 1000;
   179     timer.expires += HZ / 1000;
   218     add_timer(&timer);
   180     add_timer(&timer);
   219 }
   181 }
   222 
   184 
   223 /******************************************************************************
   185 /******************************************************************************
   224  *
   186  *
   225  * Function: init
   187  * Function: init
   226  *
   188  *
   227  ******************************************************************************/
   189  *****************************************************************************/
   228 
   190 
   229 int __init init_module()
   191 int __init init_module()
   230 {
   192 {
       
   193     unsigned int i;
       
   194 
   231     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   195     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   232 
   196 
   233     if ((ecat_master = EtherCAT_request(0)) == NULL) {
   197     if ((ecat_master = EtherCAT_request(0)) == NULL) {
   234         printk(KERN_ERR "EtherCAT master 0 not available!\n");
   198         printk(KERN_ERR "EtherCAT master 0 not available!\n");
   235         goto out_return;
   199         goto out_return;
   236     }
   200     }
   237 
   201 
   238     printk("Checking EtherCAT slaves.\n");
   202     printk("Checking EtherCAT slaves.\n");
   239 
   203 
   240     if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0) {
   204     if (EtherCAT_check_slaves(ecat_master, ecat_slaves,
       
   205                               ECAT_SLAVES_COUNT) != 0) {
   241         printk(KERN_ERR "EtherCAT: Could not init slaves!\n");
   206         printk(KERN_ERR "EtherCAT: Could not init slaves!\n");
   242         goto out_release_master;
   207         goto out_release_master;
   243     }
   208     }
   244 
   209 
   245     printk("Activating all EtherCAT slaves.\n");
   210     printk("Activating all EtherCAT slaves.\n");
   246 
   211 
   247     if (EtherCAT_activate_all_slaves(ecat_master) != 0)
   212     for (i = 0; i < ECAT_SLAVES_COUNT; i++) {
   248     {
   213         if (EtherCAT_activate_slave(ecat_master, &ecat_slaves[i]) != 0) {
   249         printk(KERN_ERR "EtherCAT: Could not activate slaves!\n");
   214             printk(KERN_ERR "EtherCAT: Could not activate slave %i!\n", i);
   250         goto out_release_master;
   215             goto out_release_master;
       
   216         }
   251     }
   217     }
   252 
   218 
   253 #ifdef ECAT_CYCLIC_DATA
   219 #ifdef ECAT_CYCLIC_DATA
   254     printk("Starting cyclic sample thread.\n");
   220     printk("Starting cyclic sample thread.\n");
   255 
   221 
   277 
   243 
   278 /******************************************************************************
   244 /******************************************************************************
   279  *
   245  *
   280  * Function: cleanup
   246  * Function: cleanup
   281  *
   247  *
   282  ******************************************************************************/
   248  *****************************************************************************/
   283 
   249 
   284 void __exit cleanup_module()
   250 void __exit cleanup_module()
   285 {
   251 {
       
   252     unsigned int i;
       
   253 
   286     printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
   254     printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n");
   287 
   255 
   288     if (ecat_master)
   256     if (ecat_master)
   289     {
   257     {
   290 #ifdef ECAT_CYCLIC_DATA
   258 #ifdef ECAT_CYCLIC_DATA
   291         del_timer_sync(&timer);
   259         del_timer_sync(&timer);
   292         EtherCAT_clear_process_data(ecat_master);
       
   293 #endif // ECAT_CYCLIC_DATA
   260 #endif // ECAT_CYCLIC_DATA
   294 
   261 
   295         printk(KERN_INFO "Deactivating slaves.\n");
   262         printk(KERN_INFO "Deactivating slaves.\n");
   296         EtherCAT_deactivate_all_slaves(ecat_master);
   263 
       
   264         for (i = 0; i < ECAT_SLAVES_COUNT; i++) {
       
   265             EtherCAT_deactivate_slave(ecat_master, &ecat_slaves[i]);
       
   266         }
   297 
   267 
   298         EtherCAT_release(ecat_master);
   268         EtherCAT_release(ecat_master);
   299     }
   269     }
   300 
   270 
   301     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   271     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   309 
   279 
   310 module_init(init_module);
   280 module_init(init_module);
   311 module_exit(cleanup_module);
   281 module_exit(cleanup_module);
   312 
   282 
   313 /*****************************************************************************/
   283 /*****************************************************************************/
       
   284 
       
   285 /* Emacs-Konfiguration
       
   286 ;;; Local Variables: ***
       
   287 ;;; c-basic-offset:4 ***
       
   288 ;;; End: ***
       
   289 */