mini/mini.c
changeset 73 9f4ea66d89a3
parent 68 a452700db994
child 83 e8b76a509bc9
equal deleted inserted replaced
72:7c986b717411 73:9f4ea66d89a3
    15 #include "../include/EtherCAT_rt.h" // Echtzeitschnittstelle
    15 #include "../include/EtherCAT_rt.h" // Echtzeitschnittstelle
    16 #include "../include/EtherCAT_si.h" // Slave-Interface-Makros
    16 #include "../include/EtherCAT_si.h" // Slave-Interface-Makros
    17 
    17 
    18 /*****************************************************************************/
    18 /*****************************************************************************/
    19 
    19 
    20 ec_master_t *master = NULL;
    20 #define ABTASTFREQUENZ 1000
    21 ec_slave_t *s_in, *s_out, *s_ssi;
       
    22 
    21 
    23 struct timer_list timer;
    22 struct timer_list timer;
    24 
    23 
    25 ec_slave_init_t slaves[] = {
    24 /*****************************************************************************/
    26     // Zeiger, Index, Herstellername, Produktname, Domäne
    25 
    27     {  &s_in,  "1",   "Beckhoff",     "EL3102",    1      },
    26 // EtherCAT
    28     {  &s_out, "2",   "Beckhoff",     "EL2004",    1      },
    27 ec_master_t *master = NULL;
    29     {  &s_ssi, "3",   "Beckhoff",     "EL5001",    1      }
    28 ec_domain_t *domain1 = NULL;
       
    29 
       
    30 // Prozessdaten
       
    31 uint8_t *dig_out1;
       
    32 uint16_t *ssi_value;
       
    33 uint16_t *inc_value;
       
    34 
       
    35 uint32_t angle0;
       
    36 
       
    37 ec_field_init_t domain1_fields[] = {
       
    38     {(void **) &dig_out1,    "2", "Beckhoff", "EL2004", ec_opvalue, 0, 1},
       
    39     {(void **) &ssi_value,   "3", "Beckhoff", "EL5001", ec_ipvalue, 0, 1},
       
    40     {(void **) &inc_value, "0:4", "Beckhoff", "EL5101", ec_ipvalue, 0, 1},
       
    41     {}
    30 };
    42 };
    31 
       
    32 #define SLAVE_COUNT (sizeof(slaves) / sizeof(ec_slave_init_t))
       
    33 
    43 
    34 /*****************************************************************************/
    44 /*****************************************************************************/
    35 
    45 
    36 void run(unsigned long data)
    46 void run(unsigned long data)
    37 {
    47 {
    38     static unsigned int counter;
    48     static unsigned int counter = 0;
    39 
       
    40     // Klemmen-IO
       
    41     EC_WRITE_EL20XX(s_out, 3, EC_READ_EL31XX(s_in, 0) < 0);
       
    42 
       
    43     if (!counter) {
       
    44         EtherCAT_rt_debug_level(master, 2);
       
    45     }
       
    46 
    49 
    47     // Prozessdaten lesen und schreiben
    50     // Prozessdaten lesen und schreiben
    48     EtherCAT_rt_domain_xio(master, 1, 100);
    51     EtherCAT_rt_domain_xio(domain1);
       
    52 
       
    53     angle0 = (uint32_t) *inc_value;
    49 
    54 
    50     if (counter) {
    55     if (counter) {
    51         counter--;
    56         counter--;
    52     }
    57     }
    53     else {
    58     else {
    54         EtherCAT_rt_debug_level(master, 0);
    59         counter = ABTASTFREQUENZ;
    55         printk("SSI status=%X value=%u\n",
    60         printk(KERN_INFO "angle0 = %i\n", angle0);
    56                EC_READ_EL5001_STATE(s_ssi), EC_READ_EL5001_VALUE(s_ssi));
       
    57         counter = 1000;
       
    58     }
    61     }
    59 
    62 
    60     // Timer neu starten
    63     // Timer neu starten
    61     timer.expires += HZ / 1000;
    64     timer.expires += HZ / ABTASTFREQUENZ;
    62     add_timer(&timer);
    65     add_timer(&timer);
    63 }
    66 }
    64 
    67 
    65 /*****************************************************************************/
    68 /*****************************************************************************/
    66 
    69 
    67 int __init init_mini_module(void)
    70 int __init init_mini_module(void)
    68 {
    71 {
       
    72     const ec_field_init_t *field;
       
    73 
    69     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
    74     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
    70 
    75 
    71     if ((master = EtherCAT_rt_request_master(0)) == NULL) {
    76     if ((master = EtherCAT_rt_request_master(0)) == NULL) {
    72         printk(KERN_ERR "EtherCAT master 0 not available!\n");
    77         printk(KERN_ERR "Error requesting master 0!\n");
    73         goto out_return;
    78         goto out_return;
    74     }
    79     }
    75 
    80 
    76     if (EtherCAT_rt_register_slave_list(master, slaves, SLAVE_COUNT)) {
    81     EtherCAT_rt_master_print(master);
    77         printk(KERN_ERR "Could not register slaves!\n");
    82 
       
    83     printk(KERN_INFO "Registering domain...\n");
       
    84 
       
    85     if (!(domain1 = EtherCAT_rt_master_register_domain(master, ec_sync, 100)))
       
    86     {
       
    87         printk(KERN_ERR "EtherCAT: Could not register domain!\n");
    78         goto out_release_master;
    88         goto out_release_master;
    79     }
    89     }
    80 
    90 
    81     printk("Activating all EtherCAT slaves.\n");
    91     printk(KERN_INFO "Registering domain fields...\n");
    82 
    92 
    83     if (EtherCAT_rt_activate_slaves(master)) {
    93     for (field = domain1_fields; field->data; field++)
    84         printk(KERN_ERR "EtherCAT: Could not activate slaves!\n");
    94     {
    85         goto out_release_master;
    95         if (!EtherCAT_rt_register_slave_field(domain1,
       
    96                                               field->address,
       
    97                                               field->vendor,
       
    98                                               field->product,
       
    99                                               field->data,
       
   100                                               field->field_type,
       
   101                                               field->field_index,
       
   102                                               field->field_count)) {
       
   103             printk(KERN_ERR "EtherCAT: Could not register field!\n");
       
   104             goto out_release_master;
       
   105         }
    86     }
   106     }
    87 
   107 
    88     printk("Configuring EtherCAT slaves.\n");
   108     printk(KERN_INFO "Activating master...\n");
    89 
   109 
    90     if (EtherCAT_rt_canopen_sdo_write(master, s_ssi, 0x4067, 0, 2, 2)) {
   110     if (EtherCAT_rt_master_activate(master)) {
    91         printk(KERN_ERR "EtherCAT: Could not set SSI baud rate!\n");
   111         printk(KERN_ERR "EtherCAT: Could not activate master!\n");
    92         goto out_release_master;
       
    93     }
       
    94 
       
    95     if (EtherCAT_rt_canopen_sdo_write(master, s_ssi, 0x4061, 4, 1, 1)) {
       
    96         printk(KERN_ERR "EtherCAT: Could not set SSI feature bit!\n");
       
    97         goto out_release_master;
   112         goto out_release_master;
    98     }
   113     }
    99 
   114 
   100     printk("Starting cyclic sample thread.\n");
   115     printk("Starting cyclic sample thread.\n");
   101 
   116 
   102     init_timer(&timer);
   117     init_timer(&timer);
   103 
       
   104     timer.function = run;
   118     timer.function = run;
   105     timer.expires = jiffies + 10; // Das erste Mal sofort feuern
   119     timer.expires = jiffies + 10; // Das erste Mal sofort feuern
   106     add_timer(&timer);
   120     add_timer(&timer);
   107 
   121 
   108     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
   122     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
   124 
   138 
   125     if (master)
   139     if (master)
   126     {
   140     {
   127         del_timer_sync(&timer);
   141         del_timer_sync(&timer);
   128 
   142 
   129         printk(KERN_INFO "Deactivating slaves.\n");
   143         printk(KERN_INFO "Deactivating master...\n");
   130 
   144 
   131         EtherCAT_rt_deactivate_slaves(master);
   145         EtherCAT_rt_master_deactivate(master);
   132         EtherCAT_rt_release_master(master);
   146         EtherCAT_rt_release_master(master);
   133     }
   147     }
   134 
   148 
   135     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   149     printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n");
   136 }
   150 }
   137 
   151 
   138 /*****************************************************************************/
   152 /*****************************************************************************/
   139 
   153 
   140 MODULE_LICENSE("GPL");
   154 MODULE_LICENSE("GPL");
   141 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
   155 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
   142 MODULE_DESCRIPTION ("Minimal EtherCAT environment");
   156 MODULE_DESCRIPTION ("EtherCAT minimal test environment");
   143 
   157 
   144 module_init(init_mini_module);
   158 module_init(init_mini_module);
   145 module_exit(cleanup_mini_module);
   159 module_exit(cleanup_mini_module);
   146 
   160 
   147 /*****************************************************************************/
   161 /*****************************************************************************/