fp@0: /****************************************************************************** fp@0: * fp@0: * ec_mini.c fp@0: * fp@0: * Minimalmodul für EtherCAT fp@0: * fp@0: * $Id$ fp@0: * fp@0: ******************************************************************************/ fp@0: fp@0: #include fp@0: #include fp@0: #include fp@0: #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@0: #include "../drivers/ec_dbg.h" fp@0: fp@0: extern EtherCAT_device_t rtl_ecat_dev; fp@0: fp@0: //static EtherCAT_master_t *ecat_master = NULL; fp@0: fp@0: #if 0 fp@0: static EtherCAT_slave_t ecat_slaves[] = fp@0: { fp@0: // Block 1 fp@0: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: fp@0: // Block 2 fp@0: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL4102), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3162), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL3102), fp@0: fp@0: // Block 3 fp@0: ECAT_INIT_SLAVE(Beckhoff_EK1100), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL2004), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014), fp@0: ECAT_INIT_SLAVE(Beckhoff_EL1014) fp@0: }; fp@0: fp@0: #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t)) fp@0: #endif fp@0: fp@0: double value; fp@0: int dig1; fp@0: fp@0: /****************************************************************************** fp@0: * fp@0: * Function: next2004 fp@0: * fp@0: *****************************************************************************/ fp@0: fp@0: #if 0 fp@0: static int next2004(int *wrap) fp@0: { fp@0: static int i = 0; fp@0: unsigned int j = 0; fp@0: fp@0: *wrap = 0; fp@0: fp@0: for (j = 0; j < ECAT_SLAVES_COUNT; j++) fp@0: { fp@0: i++; fp@0: fp@0: i %= ECAT_SLAVES_COUNT; fp@0: fp@0: if (i == 0) *wrap = 1; fp@0: fp@0: if (ecat_slaves[i].desc == Beckhoff_EL2004) fp@0: { fp@0: return i; fp@0: } fp@0: } fp@0: fp@0: return -1; fp@0: } fp@0: #endif fp@0: fp@0: /****************************************************************************** fp@0: * fp@0: * Function: msr_controller fp@0: * fp@0: * Beschreibung: Zyklischer Prozess fp@0: * fp@0: *****************************************************************************/ fp@0: fp@0: #if 0 fp@0: void msr_controller() fp@0: { fp@0: static int ms = 0; fp@0: static int cnt = 0; fp@0: static unsigned long int k = 0; fp@0: static int firstrun = 1; fp@0: fp@0: static int klemme = 12; fp@0: static int kanal = 0; fp@0: static int up_down = 0; fp@0: int wrap = 0; fp@0: fp@0: ms++; fp@0: ms %= 1000; fp@0: fp@0: #if 0 fp@0: ecat_tx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->tx_time-k)) fp@0: / (current_cpu_data.loops_per_jiffy / 10); fp@0: ecat_rx_delay = ((unsigned int) (100000 / HZ) * (ecat_master->dev->rx_time-k)) fp@0: / (current_cpu_data.loops_per_jiffy / 10); fp@0: fp@0: rx_intr = ecat_master->dev->rx_intr_cnt; fp@0: tx_intr = ecat_master->dev->tx_intr_cnt; fp@0: total_intr = ecat_master->dev->intr_cnt; fp@0: #endif fp@0: fp@0: // Prozessdaten lesen fp@0: if (!firstrun) fp@0: { fp@0: EtherCAT_read_process_data(ecat_master); fp@0: fp@0: // Daten lesen und skalieren fp@0: value = EtherCAT_read_value(&ecat_master->slaves[5], 0) / 3276.7; fp@0: dig1 = EtherCAT_read_value(&ecat_master->slaves[3], 0); fp@0: } fp@0: fp@0: // Daten schreiben fp@0: EtherCAT_write_value(&ecat_master->slaves[4], 0, ms > 500 ? 1 : 0); fp@0: EtherCAT_write_value(&ecat_master->slaves[4], 1, ms > 500 ? 0 : 1); fp@0: EtherCAT_write_value(&ecat_master->slaves[4], 2, ms > 500 ? 0 : 1); fp@0: EtherCAT_write_value(&ecat_master->slaves[4], 3, ms > 500 ? 1 : 0); fp@0: fp@0: if (cnt++ > 20) fp@0: { fp@0: cnt = 0; fp@0: fp@0: if (++kanal > 3) fp@0: { fp@0: kanal = 0; fp@0: klemme = next2004(&wrap); fp@0: fp@0: if (wrap == 1) fp@0: { fp@0: if (up_down == 1) up_down = 0; fp@0: else up_down = 1; fp@0: } fp@0: } fp@0: } fp@0: fp@0: if (klemme >= 0) fp@0: EtherCAT_write_value(&ecat_master->slaves[klemme], kanal,up_down); fp@0: fp@0: //EtherCAT_write_value(&ecat_master->slaves[13], 1, ms > 500 ? 0 : 1); fp@0: //EtherCAT_write_value(&ecat_master->slaves[14], 2, ms > 500 ? 0 : 1); fp@0: //EtherCAT_write_value(&ecat_master->slaves[15], 3, ms > 500 ? 1 : 0); fp@0: fp@0: // Prozessdaten schreiben fp@0: rdtscl(k); fp@0: EtherCAT_write_process_data(ecat_master); fp@0: firstrun = 0; fp@0: } fp@0: #endif fp@0: fp@0: /****************************************************************************** fp@0: * fp@0: * Function: init fp@0: * fp@0: ******************************************************************************/ fp@0: fp@0: //#define ECAT_OPEN fp@0: fp@0: int init() fp@0: { fp@0: #ifdef ECAT_OPEN fp@0: int rv = -1; fp@0: #endif fp@0: fp@0: EC_DBG(KERN_INFO "=== Starting Minimal EtherCAT environment... ===\n"); fp@0: fp@0: EtherCAT_device_debug(&rtl_ecat_dev); fp@0: //mdelay(5000); fp@0: fp@0: #ifdef ECAT_OPEN fp@0: EC_DBG("Opening EtherCAT device.\n"); fp@0: fp@0: // HIER PASSIERT DER FEHLER: fp@0: if (EtherCAT_device_open(&rtl_ecat_dev) < 0) fp@0: { fp@0: EC_DBG(KERN_ERR "msr_modul: Could not initialize EtherCAT NIC.\n"); fp@0: goto out_nothing; fp@0: } fp@0: fp@0: if (!rtl_ecat_dev.dev) // Es gibt kein EtherCAT-Device fp@0: { fp@0: EC_DBG(KERN_ERR "msr_modul: No EtherCAT device!\n"); fp@0: goto out_close; fp@0: } fp@0: #endif fp@0: fp@0: #if 0 fp@0: // EtherCAT-Master und Slaves initialisieren fp@0: EC_DBG("Initialising EtherCAT master\n"); fp@0: fp@0: if ((ecat_master = (EtherCAT_master_t *) kmalloc(sizeof(EtherCAT_master_t), GFP_KERNEL)) == 0) fp@0: { fp@0: EC_DBG(KERN_ERR "msr_modul: Could not alloc memory for EtherCAT master!\n"); fp@0: goto out_close; fp@0: } fp@0: fp@0: if (EtherCAT_master_init(ecat_master, &rtl_ecat_dev) < 0) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT could not init master!\n"); fp@0: goto out_master; fp@0: } fp@0: #endif fp@0: fp@0: #if 0 fp@0: EC_DBG("Checking EtherCAT slaves.\n"); fp@0: fp@0: if (EtherCAT_check_slaves(ecat_master, ecat_slaves, ECAT_SLAVES_COUNT) != 0) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not init slaves!\n"); fp@0: goto out_masterclear; fp@0: } fp@0: fp@0: EC_DBG("Activating all EtherCAT slaves.\n"); fp@0: fp@0: if (EtherCAT_activate_all_slaves(ecat_master) != 0) fp@0: { fp@0: EC_DBG(KERN_ERR "EtherCAT: Could not activate slaves!\n"); fp@0: goto out_masterclear; fp@0: } fp@0: fp@0: // Zyklischen Aufruf starten fp@0: fp@0: EC_DBG("Starting cyclic sample thread.\n"); fp@0: fp@0: EtherCAT_write_process_data(ecat_master); fp@0: fp@0: EC_DBG("Initialised sample thread.\n"); fp@0: #endif fp@0: fp@0: EC_DBG(KERN_INFO "=== Minimal EtherCAT environment started. ===\n"); fp@0: fp@0: return 0; fp@0: fp@0: #if 0 fp@0: out_masterclear: fp@0: EC_DBG(KERN_INFO "Clearing EtherCAT master.\n"); fp@0: EtherCAT_master_clear(ecat_master); fp@0: #endif fp@0: fp@0: #if 0 fp@0: out_master: fp@0: EC_DBG(KERN_INFO "Freeing EtherCAT master.\n"); fp@0: kfree(ecat_master); fp@0: #endif fp@0: fp@0: #ifdef ECAT_OPEN fp@0: out_close: fp@0: EC_DBG(KERN_INFO "Closing device.\n"); fp@0: EtherCAT_device_close(&rtl_ecat_dev); fp@0: fp@0: out_nothing: fp@0: return rv; fp@0: #endif fp@0: } fp@0: fp@0: /****************************************************************************** fp@0: * fp@0: * Function: cleanup fp@0: * fp@0: ******************************************************************************/ fp@0: fp@0: void cleanup() fp@0: { fp@0: EC_DBG(KERN_INFO "=== Stopping Minimal EtherCAT environment... ===\n"); fp@0: fp@0: // Noch einmal lesen fp@0: //EC_DBG(KERN_INFO "Reading process data.\n"); fp@0: //EtherCAT_read_process_data(ecat_master); fp@0: fp@0: #if 0 fp@0: if (ecat_master) fp@0: { fp@0: #if 0 fp@0: EC_DBG(KERN_INFO "Deactivating slaves.\n"); fp@0: EtherCAT_deactivate_all_slaves(ecat_master); fp@0: #endif fp@0: fp@0: EC_DBG(KERN_INFO "Clearing EtherCAT master.\n"); fp@0: EtherCAT_master_clear(ecat_master); fp@0: fp@0: EC_DBG(KERN_INFO "Freeing EtherCAT master.\n"); fp@0: kfree(ecat_master); fp@0: ecat_master = NULL; fp@0: } fp@0: #endif fp@0: fp@0: #ifdef ECAT_OPEN fp@0: EC_DBG(KERN_INFO "Closing device.\n"); fp@0: EtherCAT_device_close(&rtl_ecat_dev); fp@0: #endif fp@0: fp@0: EC_DBG(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@0: module_init(init); fp@0: module_exit(cleanup); fp@0: fp@0: /*****************************************************************************/