examples/mini/mini.c
branchstable-1.2
changeset 1739 5fcbd29151d2
parent 1732 1cc865ba17c2
child 1744 7bc131b92039
equal deleted inserted replaced
1738:bc89e3fba1a5 1739:5fcbd29151d2
    30  *  Technology, IP and trade marks.
    30  *  Technology, IP and trade marks.
    31  *
    31  *
    32  *****************************************************************************/
    32  *****************************************************************************/
    33 
    33 
    34 #include <linux/module.h>
    34 #include <linux/module.h>
    35 #include <linux/delay.h>
       
    36 #include <linux/timer.h>
    35 #include <linux/timer.h>
    37 #include <linux/spinlock.h>
    36 #include <linux/spinlock.h>
    38 #include <linux/interrupt.h>
    37 #include <linux/interrupt.h>
    39 
    38 
    40 #include "../../include/ecrt.h" // EtherCAT realtime interface
    39 #include "../../include/ecrt.h" // EtherCAT realtime interface
    41 #include "../../include/ecdb.h" // EtherCAT slave database
    40 #include "../../include/ecdb.h" // EtherCAT slave database
    42 
    41 
    43 #define FREQUENCY 100
    42 #define FREQUENCY 100
    44 
    43 
    45 #define KBUS
    44 //#define KBUS
    46 
    45 
    47 /*****************************************************************************/
    46 /*****************************************************************************/
    48 
    47 
    49 struct timer_list timer;
    48 struct timer_list timer;
    50 
    49 
    57 #ifdef KBUS
    56 #ifdef KBUS
    58 void *r_inputs;
    57 void *r_inputs;
    59 void *r_outputs;
    58 void *r_outputs;
    60 #endif
    59 #endif
    61 
    60 
    62 void *r_ana_in;
    61 void *r_dig_out;
    63 
    62 
    64 #if 1
    63 #if 1
    65 ec_pdo_reg_t domain1_pdos[] = {
    64 ec_pdo_reg_t domain1_pdos[] = {
    66     {"2", Beckhoff_EL3102_Input1, &r_ana_in},
    65     {"4", Beckhoff_EL2004_Outputs, &r_dig_out},
    67     {}
    66     {}
    68 };
    67 };
    69 #endif
    68 #endif
    70 
    69 
    71 /*****************************************************************************/
    70 /*****************************************************************************/
    72 
    71 
    73 void run(unsigned long data)
    72 void run(unsigned long data)
    74 {
    73 {
    75     static unsigned int counter = 0;
    74     static unsigned int counter = 0;
    76     static unsigned int einaus = 0;
    75     static unsigned int blink = 0;
    77 
       
    78     spin_lock(&master_lock);
       
    79 
    76 
    80     // receive
    77     // receive
       
    78     spin_lock(&master_lock);
    81     ecrt_master_receive(master);
    79     ecrt_master_receive(master);
    82     ecrt_domain_process(domain1);
    80     ecrt_domain_process(domain1);
       
    81     spin_unlock(&master_lock);
    83 
    82 
    84     // process data
    83     // process data
    85     //k_pos = EC_READ_U32(r_ssi);
    84     // k_pos = EC_READ_U32(r_ssi);
    86 #ifdef KBUS
    85     EC_WRITE_U8(r_dig_out, blink ? 0x0F : 0x00);
    87     EC_WRITE_U8(r_outputs + 2, einaus ? 0xFF : 0x00);
       
    88 #endif
       
    89 
       
    90     // send
       
    91     ecrt_master_run(master);
       
    92     ecrt_master_send(master);
       
    93 
       
    94     spin_unlock(&master_lock);
       
    95 
    86 
    96     if (counter) {
    87     if (counter) {
    97         counter--;
    88         counter--;
    98     }
    89     }
    99     else {
    90     else {
   100         counter = FREQUENCY;
    91         counter = FREQUENCY;
   101         einaus = !einaus;
    92         blink = !blink;
   102     }
    93     }
       
    94 
       
    95 #ifdef KBUS
       
    96     EC_WRITE_U8(r_outputs + 2, blink ? 0xFF : 0x00);
       
    97 #endif
       
    98 
       
    99     // send
       
   100     spin_lock(&master_lock);
       
   101     ecrt_domain_queue(domain1);
       
   102     spin_unlock(&master_lock);
       
   103 
       
   104     ecrt_master_run(master);
       
   105 
       
   106     spin_lock(&master_lock);
       
   107     ecrt_master_send(master);
       
   108     spin_unlock(&master_lock);
   103 
   109 
   104     // restart timer
   110     // restart timer
   105     timer.expires += HZ / FREQUENCY;
   111     timer.expires += HZ / FREQUENCY;
   106     add_timer(&timer);
   112     add_timer(&timer);
   107 }
   113 }
   108 
   114 
   109 /*****************************************************************************/
   115 /*****************************************************************************/
   110 
   116 
   111 int request_lock(void *data)
   117 int request_lock(void *data)
   112 {
   118 {
   113     spin_lock_bh(&master_lock);
   119     spin_lock(&master_lock);
   114     return 0; // access allowed
   120     return 0; // access allowed
   115 }
   121 }
   116 
   122 
   117 /*****************************************************************************/
   123 /*****************************************************************************/
   118 
   124 
   119 void release_lock(void *data)
   125 void release_lock(void *data)
   120 {
   126 {
   121     spin_unlock_bh(&master_lock);
   127     spin_unlock(&master_lock);
   122 }
   128 }
   123 
   129 
   124 /*****************************************************************************/
   130 /*****************************************************************************/
   125 
   131 
   126 int __init init_mini_module(void)
   132 int __init init_mini_module(void)
   127 {
   133 {
   128 #if 1
   134 #if 0
   129     ec_slave_t *slave;
   135     ec_slave_t *slave;
   130 #endif
   136 #endif
   131 
   137 
   132     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   138     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   133 
   139 
   134     if ((master = ecrt_request_master(0)) == NULL) {
   140     if (!(master = ecrt_request_master(0))) {
   135         printk(KERN_ERR "Requesting master 0 failed!\n");
   141         printk(KERN_ERR "Requesting master 0 failed!\n");
   136         goto out_return;
   142         goto out_return;
   137     }
   143     }
   138 
   144 
   139     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   145     ecrt_master_callbacks(master, request_lock, release_lock, NULL);
   140 
   146 
   141     printk(KERN_INFO "Registering domain...\n");
   147     printk(KERN_INFO "Registering domain...\n");
   142     if (!(domain1 = ecrt_master_create_domain(master)))
   148     if (!(domain1 = ecrt_master_create_domain(master))) {
   143     {
       
   144         printk(KERN_ERR "Domain creation failed!\n");
   149         printk(KERN_ERR "Domain creation failed!\n");
   145         goto out_release_master;
   150         goto out_release_master;
   146     }
   151     }
   147 
   152 
   148     printk(KERN_INFO "Registering PDOs...\n");
   153     printk(KERN_INFO "Registering PDOs...\n");
   164         printk(KERN_ERR "PDO registration failed!\n");
   169         printk(KERN_ERR "PDO registration failed!\n");
   165         goto out_release_master;
   170         goto out_release_master;
   166     }
   171     }
   167 #endif
   172 #endif
   168 
   173 
   169 #if 1
   174 #if 0
   170     if (!(slave = ecrt_master_get_slave(master, "2")))
   175     if (!(slave = ecrt_master_get_slave(master, "2")))
   171         goto out_release_master;
   176         goto out_release_master;
   172 
   177 
   173     if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0))
   178     if (ecrt_slave_conf_sdo8(slave, 0x4061, 1, 0))
   174         goto out_release_master;
   179         goto out_release_master;
   177     printk(KERN_INFO "Activating master...\n");
   182     printk(KERN_INFO "Activating master...\n");
   178     if (ecrt_master_activate(master)) {
   183     if (ecrt_master_activate(master)) {
   179         printk(KERN_ERR "Failed to activate master!\n");
   184         printk(KERN_ERR "Failed to activate master!\n");
   180         goto out_release_master;
   185         goto out_release_master;
   181     }
   186     }
   182 
       
   183     ecrt_master_prepare(master);
       
   184 
   187 
   185     printk("Starting cyclic sample thread.\n");
   188     printk("Starting cyclic sample thread.\n");
   186     init_timer(&timer);
   189     init_timer(&timer);
   187     timer.function = run;
   190     timer.function = run;
   188     timer.expires = jiffies + 10;
   191     timer.expires = jiffies + 10;
   211 }
   214 }
   212 
   215 
   213 /*****************************************************************************/
   216 /*****************************************************************************/
   214 
   217 
   215 MODULE_LICENSE("GPL");
   218 MODULE_LICENSE("GPL");
   216 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
   219 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
   217 MODULE_DESCRIPTION ("EtherCAT minimal test environment");
   220 MODULE_DESCRIPTION("EtherCAT minimal test environment");
   218 
   221 
   219 module_init(init_mini_module);
   222 module_init(init_mini_module);
   220 module_exit(cleanup_mini_module);
   223 module_exit(cleanup_mini_module);
   221 
   224 
   222 /*****************************************************************************/
   225 /*****************************************************************************/
   223