examples/mini/mini.c
changeset 325 7833cf70c4f2
parent 280 35c99cdda518
child 329 d004349777fc
equal deleted inserted replaced
324:9aa51cbdbfae 325:7833cf70c4f2
    40 #include <linux/timer.h>
    40 #include <linux/timer.h>
    41 #include <linux/spinlock.h>
    41 #include <linux/spinlock.h>
    42 #include <linux/interrupt.h>
    42 #include <linux/interrupt.h>
    43 
    43 
    44 #include "../../include/ecrt.h" // EtherCAT realtime interface
    44 #include "../../include/ecrt.h" // EtherCAT realtime interface
       
    45 #include "../../include/ecdb.h" // EtherCAT slave database
    45 
    46 
    46 #define ASYNC
       
    47 #define FREQUENCY 100
    47 #define FREQUENCY 100
    48 
    48 
    49 /*****************************************************************************/
    49 /*****************************************************************************/
    50 
    50 
    51 struct timer_list timer;
    51 struct timer_list timer;
    54 ec_master_t *master = NULL;
    54 ec_master_t *master = NULL;
    55 ec_domain_t *domain1 = NULL;
    55 ec_domain_t *domain1 = NULL;
    56 spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
    56 spinlock_t master_lock = SPIN_LOCK_UNLOCKED;
    57 
    57 
    58 // data fields
    58 // data fields
    59 //void *r_ssi_input, *r_ssi_status, *r_4102[3];
    59 void *r_ana_out;
    60 void *r_kbus_in, *r_kbus_out;
       
    61 
    60 
    62 // channels
    61 // channels
    63 uint32_t k_pos;
    62 uint32_t k_pos;
    64 uint8_t k_stat;
    63 uint8_t k_stat;
    65 
    64 
    66 ec_field_init_t domain1_fields[] = {
    65 ec_pdo_reg_t domain1_pdos[] = {
    67     //{&r_kbus_out, "0", "Beckhoff", "BK1120", "Outputs", 0},
    66     {"1", Beckhoff_EL4132_Output1, &r_ana_out},
    68     //{&r_kbus_in,  "0", "Beckhoff", "BK1120", "Inputs",  0},
       
    69     {}
    67     {}
    70 };
    68 };
    71 
    69 
    72 /*****************************************************************************/
    70 /*****************************************************************************/
    73 
    71 
    74 void run(unsigned long data)
    72 void run(unsigned long data)
    75 {
    73 {
    76     static unsigned int counter = 0;
    74     static unsigned int counter = 0;
    77     unsigned int i;
       
    78 
    75 
    79     spin_lock(&master_lock);
    76     spin_lock(&master_lock);
    80 
    77 
    81 #ifdef ASYNC
       
    82     // receive
    78     // receive
    83     ecrt_master_async_receive(master);
    79     ecrt_master_receive(master);
    84     ecrt_domain_process(domain1);
    80     ecrt_domain_process(domain1);
    85 #else
       
    86     // send and receive
       
    87     ecrt_domain_queue(domain1);
       
    88     ecrt_master_run(master);
       
    89     ecrt_master_sync_io(master);
       
    90     ecrt_domain_process(domain1);
       
    91 #endif
       
    92 
    81 
    93     // process data
    82     // process data
    94     //k_pos = EC_READ_U32(r_ssi);
    83     //k_pos = EC_READ_U32(r_ssi);
    95 
    84 
    96 #ifdef ASYNC
       
    97     // send
    85     // send
    98     ecrt_domain_queue(domain1);
       
    99     ecrt_master_run(master);
    86     ecrt_master_run(master);
   100     ecrt_master_async_send(master);
    87     ecrt_master_send(master);
   101 #endif
       
   102 
    88 
   103     spin_unlock(&master_lock);
    89     spin_unlock(&master_lock);
   104 
    90 
   105     if (counter) {
    91     if (counter) {
   106         counter--;
    92         counter--;
   107     }
    93     }
   108     else {
    94     else {
   109         counter = FREQUENCY;
    95         counter = FREQUENCY;
   110         printk(KERN_INFO "input = ");
    96         //printk(KERN_INFO "input = ");
   111         for (i = 0; i < 22; i++) printk("%02X ", *((uint8_t *) r_kbus_in + i));
    97         //for (i = 0; i < 22; i++)
   112         printk("\n");
    98         //    printk("%02X ", *((uint8_t *) r_kbus_in + i));
       
    99         //printk("\n");
   113     }
   100     }
   114 
   101 
   115     // restart timer
   102     // restart timer
   116     timer.expires += HZ / FREQUENCY;
   103     timer.expires += HZ / FREQUENCY;
   117     add_timer(&timer);
   104     add_timer(&timer);
   134 
   121 
   135 /*****************************************************************************/
   122 /*****************************************************************************/
   136 
   123 
   137 int __init init_mini_module(void)
   124 int __init init_mini_module(void)
   138 {
   125 {
   139     ec_slave_t *slave;
       
   140 
       
   141     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   126     printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n");
   142 
   127 
   143     if ((master = ecrt_request_master(0)) == NULL) {
   128     if ((master = ecrt_request_master(0)) == NULL) {
   144         printk(KERN_ERR "Requesting master 0 failed!\n");
   129         printk(KERN_ERR "Requesting master 0 failed!\n");
   145         goto out_return;
   130         goto out_return;
   152     {
   137     {
   153         printk(KERN_ERR "Domain creation failed!\n");
   138         printk(KERN_ERR "Domain creation failed!\n");
   154         goto out_release_master;
   139         goto out_release_master;
   155     }
   140     }
   156 
   141 
   157     printk(KERN_INFO "Registering domain fields...\n");
   142     printk(KERN_INFO "Registering PDOs...\n");
   158     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
   143     if (ecrt_domain_register_pdo_list(domain1, domain1_pdos)) {
   159         printk(KERN_ERR "Field registration failed!\n");
   144         printk(KERN_ERR "PDO registration failed!\n");
   160         goto out_release_master;
   145         goto out_release_master;
   161     }
   146     }
   162 
       
   163 #if 1
       
   164     printk(KERN_INFO "Setting variable data field sizes...\n");
       
   165     if (!(slave = ecrt_master_get_slave(master, "0"))) {
       
   166         printk(KERN_ERR "Failed to get slave!\n");
       
   167         goto out_deactivate;
       
   168     }
       
   169     ecrt_slave_field_size(slave, "Outputs", 0, 0x16);
       
   170     ecrt_slave_field_size(slave, "Inputs", 0, 0x16);
       
   171 #endif
       
   172 
   147 
   173     printk(KERN_INFO "Activating master...\n");
   148     printk(KERN_INFO "Activating master...\n");
   174     if (ecrt_master_activate(master)) {
   149     if (ecrt_master_activate(master)) {
   175         printk(KERN_ERR "Failed to activate master!\n");
   150         printk(KERN_ERR "Failed to activate master!\n");
   176         goto out_release_master;
   151         goto out_release_master;
   177     }
   152     }
   178 
   153 
   179 #if 1
   154     ecrt_master_prepare(master);
   180     if (ecrt_master_fetch_sdo_lists(master)) {
       
   181         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   182         goto out_deactivate;
       
   183     }
       
   184     ecrt_master_print(master, 2);
       
   185 #else
       
   186     ecrt_master_print(master, 0);
       
   187 #endif
       
   188 
       
   189 #if 0
       
   190     if (!(slave = ecrt_master_get_slave(master, "5"))) {
       
   191         printk(KERN_ERR "Failed to get slave 5!\n");
       
   192         goto out_deactivate;
       
   193     }
       
   194 
       
   195     if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  0) ||
       
   196         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  1) ||
       
   197         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) ||
       
   198         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  0) ||
       
   199         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  4) ||
       
   200         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) ||
       
   201         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) ||
       
   202         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) ||
       
   203         ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) {
       
   204         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   205         goto out_deactivate;
       
   206     }
       
   207 #endif
       
   208 
       
   209 #ifdef ASYNC
       
   210     // send once and wait...
       
   211     ecrt_master_prepare_async_io(master);
       
   212 #endif
       
   213 
       
   214 #if 1
       
   215     if (ecrt_master_start_eoe(master)) {
       
   216         printk(KERN_ERR "Failed to start EoE processing!\n");
       
   217         goto out_deactivate;
       
   218     }
       
   219 #endif
       
   220 
   155 
   221     printk("Starting cyclic sample thread.\n");
   156     printk("Starting cyclic sample thread.\n");
   222     init_timer(&timer);
   157     init_timer(&timer);
   223     timer.function = run;
   158     timer.function = run;
   224     timer.expires = jiffies + 10;
   159     timer.expires = jiffies + 10;
   225     add_timer(&timer);
   160     add_timer(&timer);
   226 
   161 
   227     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
   162     printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n");
   228     return 0;
   163     return 0;
   229 
   164 
   230 #if 1
       
   231  out_deactivate:
       
   232     ecrt_master_deactivate(master);
       
   233 #endif
       
   234  out_release_master:
   165  out_release_master:
   235     ecrt_release_master(master);
   166     ecrt_release_master(master);
   236  out_return:
   167  out_return:
   237     return -1;
   168     return -1;
   238 }
   169 }