fp@0: /****************************************************************************** fp@0: * fp@0: * ec_mini.c fp@0: * fp@0: * Minimalmodul für EtherCAT fp@13: * fp@0: * $Id$ fp@0: * fp@0: ******************************************************************************/ fp@0: fp@0: #include fp@24: #include fp@22: #include fp@0: fp@0: #include "../drivers/ec_master.h" fp@0: #include "../drivers/ec_device.h" fp@0: #include "../drivers/ec_types.h" fp@27: #include "../drivers/ec_module.h" fp@0: fp@5: /******************************************************************************/ fp@5: fp@27: // Auskommentieren, wenn keine zyklischen Daten erwuenscht fp@5: #define ECAT_CYCLIC_DATA fp@5: fp@5: /******************************************************************************/ fp@5: fp@5: static EtherCAT_master_t *ecat_master = NULL; fp@27: fp@5: static EtherCAT_slave_t ecat_slaves[] = fp@5: { fp@0: #if 0 fp@22: // Block 1 fp@22: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@24: fp@22: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@24: fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@24: fp@24: #endif fp@24: fp@24: #if 1 fp@22: // Block 2 fp@22: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@24: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: fp@22: // Block 3 fp@22: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@22: ECAT_INIT_SLAVE(Beckhoff_EL1014) fp@24: #endif fp@0: }; fp@0: fp@0: #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t)) fp@22: fp@5: #ifdef ECAT_CYCLIC_DATA fp@22: fp@24: int value; fp@0: int dig1; fp@22: fp@22: struct timer_list timer; fp@22: unsigned long last_start_jiffies; fp@22: fp@22: #endif // ECAT_CYCLIC_DATA fp@0: fp@0: /****************************************************************************** fp@0: * fp@0: * Function: next2004 fp@0: * fp@0: *****************************************************************************/ fp@0: fp@5: #ifdef ECAT_CYCLIC_DATA fp@22: fp@0: static int next2004(int *wrap) fp@0: { fp@22: static int i = 0; fp@22: unsigned int j = 0; fp@22: fp@22: *wrap = 0; fp@22: fp@22: for (j = 0; j < ECAT_SLAVES_COUNT; j++) fp@22: { fp@22: i++; fp@22: fp@22: i %= ECAT_SLAVES_COUNT; fp@22: fp@22: if (i == 0) *wrap = 1; fp@22: fp@22: if (ecat_slaves[i].desc == Beckhoff_EL2004) fp@22: { fp@22: return i; fp@22: } fp@22: } fp@22: fp@22: return -1; fp@0: } fp@0: #endif fp@0: fp@0: /****************************************************************************** fp@0: * fp@5: * Function: run fp@0: * fp@0: * Beschreibung: Zyklischer Prozess fp@0: * fp@0: *****************************************************************************/ fp@0: fp@5: #ifdef ECAT_CYCLIC_DATA fp@22: fp@22: static void run(unsigned long data) fp@5: { fp@22: static int ms = 0; fp@22: static int cnt = 0; fp@22: static unsigned long int k = 0; fp@22: static int firstrun = 1; fp@22: fp@24: static int klemme = 0; fp@22: static int kanal = 0; fp@22: static int up_down = 0; fp@22: int wrap = 0; fp@22: fp@22: ms++; fp@22: ms %= 1000; fp@0: fp@0: #if 0 fp@22: ecat_tx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->tx_time-k)) fp@22: / (current_cpu_data.loops_per_jiffy / 10); fp@22: ecat_rx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->rx_time-k)) fp@22: / (current_cpu_data.loops_per_jiffy / 10); fp@22: fp@22: rx_intr = ecat_master->dev->rx_intr_cnt; fp@22: tx_intr = ecat_master->dev->tx_intr_cnt; fp@22: total_intr = ecat_master->dev->intr_cnt; fp@22: #endif fp@22: fp@22: // Prozessdaten lesen fp@22: if (!firstrun) fp@22: { fp@24: EtherCAT_read_process_data(ecat_master); fp@24: fp@24: // Daten lesen und skalieren fp@24: // value = EtherCAT_read_value(&ecat_master->slaves[5], 0) / 3276; //.7; FIXME kein FP im Kernel ohne Schutz !! fp@24: // dig1 = EtherCAT_read_value(&ecat_master->slaves[3], 0); fp@24: } fp@24: else fp@22: klemme = next2004(&wrap); fp@22: fp@0: fp@5: #if 0 fp@22: // Daten schreiben fp@22: EtherCAT_write_value(&ecat_master->slaves[4], 0, ms > 500 ? 1 : 0); fp@22: EtherCAT_write_value(&ecat_master->slaves[4], 1, ms > 500 ? 0 : 1); fp@22: EtherCAT_write_value(&ecat_master->slaves[4], 2, ms > 500 ? 0 : 1); fp@22: EtherCAT_write_value(&ecat_master->slaves[4], 3, ms > 500 ? 1 : 0); fp@22: #endif fp@22: fp@22: if (cnt++ > 20) fp@22: { fp@22: cnt = 0; fp@22: fp@22: if (++kanal > 3) fp@22: { fp@22: kanal = 0; fp@22: klemme = next2004(&wrap); fp@22: fp@22: if (wrap == 1) fp@22: { fp@22: if (up_down == 1) up_down = 0; fp@22: else up_down = 1; fp@22: } fp@22: } fp@22: } fp@22: fp@24: if (klemme >= 0) { fp@22: EtherCAT_write_value(&ecat_master->slaves[klemme], kanal,up_down); fp@24: //printk("ECAT write: Klemme: %d, Kanal: %d, Wert: %d\n",klemme,kanal,up_down); fp@24: } fp@5: fp@5: #if 0 fp@22: EtherCAT_write_value(&ecat_master->slaves[13], 1, ms > 500 ? 0 : 1); fp@22: EtherCAT_write_value(&ecat_master->slaves[14], 2, ms > 500 ? 0 : 1); fp@22: EtherCAT_write_value(&ecat_master->slaves[15], 3, ms > 500 ? 1 : 0); fp@22: #endif fp@22: fp@22: // Prozessdaten schreiben fp@22: rdtscl(k); fp@22: EtherCAT_write_process_data(ecat_master); fp@22: firstrun = 0; fp@22: fp@24: timer.expires += HZ / 1000; fp@22: add_timer(&timer); fp@0: } fp@22: fp@22: #endif // ECAT_CYCLIC_DATA fp@0: fp@0: /****************************************************************************** fp@22: * fp@22: * Function: init fp@22: * fp@22: ******************************************************************************/ fp@22: fp@22: int __init init_module() fp@5: { fp@36: printk(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); fp@36: fp@36: if ((ecat_master = EtherCAT_request(0)) == NULL) { fp@36: printk(KERN_ERR "EtherCAT master 0 not available!\n"); fp@36: goto out_return; fp@36: } fp@36: fp@36: printk("Checking EtherCAT slaves.\n"); fp@36: fp@36: if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0) { fp@36: printk(KERN_ERR "EtherCAT: Could not init slaves!\n"); fp@36: goto out_release_master; fp@36: } fp@36: fp@36: printk("Activating all EtherCAT slaves.\n"); fp@36: fp@36: if (EtherCAT_activate_all_slaves(ecat_master) != 0) fp@36: { fp@36: printk(KERN_ERR "EtherCAT: Could not activate slaves!\n"); fp@36: goto out_release_master; fp@36: } fp@36: fp@36: #ifdef ECAT_CYCLIC_DATA fp@36: printk("Starting cyclic sample thread.\n"); fp@36: fp@36: init_timer(&timer); fp@36: fp@36: timer.function = run; fp@36: timer.data = 0; fp@36: timer.expires = jiffies+10; // Das erste Mal sofort feuern fp@36: last_start_jiffies = timer.expires; fp@36: add_timer(&timer); fp@36: fp@36: printk("Initialised sample thread.\n"); fp@36: #endif fp@36: fp@36: printk(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); fp@36: fp@36: return 0; fp@36: fp@36: out_release_master: fp@36: EtherCAT_release(ecat_master); fp@36: fp@36: out_return: fp@36: return -1; fp@0: } fp@0: fp@0: /****************************************************************************** fp@22: * fp@22: * Function: cleanup fp@22: * fp@22: ******************************************************************************/ fp@22: fp@22: void __exit cleanup_module() fp@0: { fp@26: printk(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); fp@0: fp@22: if (ecat_master) fp@22: { fp@27: #ifdef ECAT_CYCLIC_DATA fp@36: del_timer_sync(&timer); fp@36: EtherCAT_clear_process_data(ecat_master); fp@22: #endif // ECAT_CYCLIC_DATA fp@22: fp@36: printk(KERN_INFO "Deactivating slaves.\n"); fp@36: EtherCAT_deactivate_all_slaves(ecat_master); fp@36: fp@36: EtherCAT_release(ecat_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@0: MODULE_DESCRIPTION ("Minimal EtherCAT environment"); fp@0: fp@22: module_init(init_module); fp@22: module_exit(cleanup_module); fp@0: fp@0: /*****************************************************************************/