fp@0: /****************************************************************************** fp@0: * fp@147: * m i n i . c fp@147: * fp@195: * Minimal module for EtherCAT. fp@147: * fp@147: * $Id$ fp@0: * fp@42: *****************************************************************************/ fp@0: fp@0: #include fp@24: #include fp@22: #include fp@0: fp@195: #include "../include/ecrt.h" // EtherCAT realtime interface fp@104: fp@106: #define ASYNC fp@195: #define FREQUENCY 100 fp@195: fp@195: /*****************************************************************************/ fp@22: fp@22: struct timer_list timer; fp@22: fp@73: // EtherCAT fp@73: ec_master_t *master = NULL; fp@73: ec_domain_t *domain1 = NULL; fp@73: fp@195: // data fields fp@130: //void *r_ssi_input, *r_ssi_status, *r_4102[3]; fp@73: fp@195: // channels fp@104: uint32_t k_pos; fp@111: uint8_t k_stat; fp@73: fp@73: ec_field_init_t domain1_fields[] = { fp@195: {NULL, "1", "Beckhoff", "EL5001", "InputValue", 0}, fp@147: {NULL, "2", "Beckhoff", "EL4132", "OutputValue", 0}, fp@73: {} fp@61: }; fp@0: fp@60: /*****************************************************************************/ fp@60: fp@60: void run(unsigned long data) fp@5: { fp@73: static unsigned int counter = 0; fp@68: fp@104: #ifdef ASYNC fp@195: // receive fp@104: ecrt_master_async_receive(master); fp@104: ecrt_domain_process(domain1); fp@104: #else fp@195: // send and receive fp@104: ecrt_domain_queue(domain1); fp@147: ecrt_master_run(master); fp@104: ecrt_master_sync_io(master); fp@104: ecrt_domain_process(domain1); fp@156: #endif fp@104: fp@195: // process data fp@156: //k_pos = EC_READ_U32(r_ssi); fp@156: fp@156: #ifdef ASYNC fp@195: // send fp@156: ecrt_domain_queue(domain1); fp@156: ecrt_master_run(master); fp@156: ecrt_master_async_send(master); fp@104: #endif fp@104: fp@68: if (counter) { fp@68: counter--; fp@68: } fp@68: else { fp@195: counter = FREQUENCY; fp@117: //printk(KERN_INFO "k_pos = %i\n", k_pos); fp@117: //printk(KERN_INFO "k_stat = 0x%02X\n", k_stat); fp@68: } fp@68: fp@195: // restart timer fp@195: timer.expires += HZ / FREQUENCY; fp@22: add_timer(&timer); fp@0: } fp@22: fp@60: /*****************************************************************************/ fp@22: fp@54: int __init init_mini_module(void) fp@5: { fp@36: printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); fp@36: fp@104: if ((master = ecrt_request_master(0)) == NULL) { fp@135: printk(KERN_ERR "Requesting master 0 failed!\n"); fp@36: goto out_return; fp@36: } fp@36: fp@73: printk(KERN_INFO "Registering domain...\n"); fp@104: if (!(domain1 = ecrt_master_create_domain(master))) fp@73: { fp@135: printk(KERN_ERR "Domain creation failed!\n"); fp@60: goto out_release_master; fp@60: } fp@36: fp@73: printk(KERN_INFO "Registering domain fields...\n"); fp@104: if (ecrt_domain_register_field_list(domain1, domain1_fields)) { fp@135: printk(KERN_ERR "Field registration failed!\n"); fp@104: goto out_release_master; fp@36: } fp@36: fp@73: printk(KERN_INFO "Activating master...\n"); fp@104: if (ecrt_master_activate(master)) { fp@135: printk(KERN_ERR "Failed to activate master!\n"); fp@68: goto out_release_master; fp@68: } fp@64: fp@140: #if 0 fp@135: if (ecrt_master_fetch_sdo_lists(master)) { fp@135: printk(KERN_ERR "Failed to fetch SDO lists!\n"); fp@135: goto out_deactivate; fp@135: } fp@140: ecrt_master_print(master, 2); fp@140: #else fp@140: ecrt_master_print(master, 0); fp@140: #endif fp@140: fp@111: fp@130: #if 0 fp@138: if (!(slave = ecrt_master_get_slave(master, "5"))) { fp@138: printk(KERN_ERR "Failed to get slave 5!\n"); fp@138: goto out_deactivate; fp@138: } fp@138: fp@138: if (ecrt_slave_sdo_write_exp8(slave, 0x4061, 1, 0) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4061, 2, 1) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4061, 3, 1) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4066, 0, 0) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4067, 0, 4) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4068, 0, 0) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || fp@138: ecrt_slave_sdo_write_exp8(slave, 0x406B, 0, 50)) { fp@135: printk(KERN_ERR "Failed to configure SSI slave!\n"); fp@111: goto out_deactivate; fp@111: } fp@130: #endif fp@111: fp@114: #if 0 fp@114: printk(KERN_INFO "Writing alias...\n"); fp@138: if (ecrt_slave_sdo_write_exp16(slave, 0xBEEF)) { fp@135: printk(KERN_ERR "Failed to write alias!\n"); fp@114: goto out_deactivate; fp@114: } fp@114: #endif fp@114: fp@104: #ifdef ASYNC fp@195: // send once and wait... fp@106: ecrt_master_prepare_async_io(master); fp@104: #endif fp@104: fp@36: printk("Starting cyclic sample thread.\n"); fp@36: init_timer(&timer); fp@36: timer.function = run; fp@195: timer.expires = jiffies + 10; fp@36: add_timer(&timer); fp@36: fp@36: printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); fp@36: return 0; fp@36: fp@147: #if 0 fp@111: out_deactivate: fp@111: ecrt_master_deactivate(master); fp@147: #endif fp@36: out_release_master: fp@111: ecrt_release_master(master); fp@36: out_return: fp@111: return -1; fp@0: } fp@0: fp@60: /*****************************************************************************/ fp@22: fp@54: void __exit cleanup_mini_module(void) fp@0: { fp@26: printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); fp@0: fp@135: if (master) { fp@36: del_timer_sync(&timer); fp@73: printk(KERN_INFO "Deactivating master...\n"); fp@104: ecrt_master_deactivate(master); fp@104: ecrt_release_master(master); fp@27: } fp@22: fp@26: printk(KERN_INFO "=== Minimal EtherCAT environment stopped. ===\n"); fp@0: } fp@0: fp@0: /*****************************************************************************/ fp@0: fp@0: MODULE_LICENSE("GPL"); fp@0: MODULE_AUTHOR ("Florian Pose "); fp@73: MODULE_DESCRIPTION ("EtherCAT minimal test environment"); fp@0: fp@54: module_init(init_mini_module); fp@54: module_exit(cleanup_mini_module); fp@0: fp@0: /*****************************************************************************/ fp@42: