fp@42: /****************************************************************************** fp@42: * fp@42: * msr_module.c fp@42: * fp@42: * Kernelmodul für 2.6 Kernel zur Meßdatenerfassung, Steuerung und Regelung. fp@42: * Zeitgeber ist der Timerinterrupt (tq) fp@42: * fp@42: * Autor: Wilhelm Hagemeister fp@42: * fp@42: * (C) Copyright IgH 2002 fp@42: * Ingenieurgemeinschaft IgH fp@42: * Heinz-Bäcker Str. 34 fp@42: * D-45356 Essen fp@42: * Tel.: +49 201/61 99 31 fp@42: * Fax.: +49 201/61 98 36 fp@42: * E-mail: hm@igh-essen.com fp@42: * fp@42: * $Id$ fp@42: * fp@42: *****************************************************************************/ hm@28: hm@28: #ifndef __KERNEL__ hm@28: # define __KERNEL__ hm@28: #endif hm@28: #ifndef MODULE hm@28: # define MODULE hm@28: #endif hm@28: hm@28: #include hm@28: #include hm@28: hm@28: #include hm@28: #include fp@42: #include hm@28: #include /* everything... */ hm@28: #include hm@28: #include hm@28: #include hm@28: #include /* fuer get_cycles */ hm@28: #include /* error codes */ hm@28: #include /* maschine-specific registers */ hm@28: #include /* fuer HZ */ hm@28: #include hm@28: hm@28: #include "msr_param.h" //wird im Projektverzeichnis erwartet hm@28: hm@28: //#include hm@28: #include hm@28: #include hm@28: #include hm@28: #include hm@28: #include hm@28: #include hm@28: #include hm@28: #include hm@28: hm@28: hm@28: #include hm@28: hm@28: #include "../drivers/ec_master.h" hm@28: #include "../drivers/ec_device.h" hm@28: #include "../drivers/ec_types.h" hm@28: #include "../drivers/ec_module.h" hm@28: hm@28: #include "msr_jitter.h" hm@28: fp@42: #define TSC2US(T) ((unsigned long) (T) * 1000UL / cpu_khz) fp@42: fp@42: /*--external data------------------------------------------------------------*/ hm@28: hm@28: #define HZREDUCTION (MSR_ABTASTFREQUENZ/HZ) hm@28: hm@28: extern wait_queue_head_t msr_read_waitqueue; hm@28: hm@28: extern struct msr_char_buf *msr_kanal_puffer; hm@28: hm@28: extern int proc_abtastfrequenz; hm@28: fp@42: /*--local data---------------------------------------------------------------*/ fp@42: fp@42: extern struct timeval process_time; fp@42: struct timeval msr_time_increment; // Increment per Interrupt hm@28: hm@28: //adeos hm@28: hm@28: static struct ipipe_domain this_domain; hm@28: hm@28: static struct ipipe_sysinfo sys_info; hm@28: hm@28: static EtherCAT_master_t *ecat_master = NULL; hm@28: hm@28: static EtherCAT_slave_t ecat_slaves[] = hm@28: { hm@28: #if 1 hm@28: // Block 1 fp@42: ECAT_INIT_SLAVE(Beckhoff_EK1100, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4102, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3162, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3102, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 0), hm@28: hm@28: // Block 2 fp@42: ECAT_INIT_SLAVE(Beckhoff_EK1100, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL2004, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL1014, 1) fp@42: #endif fp@42: fp@42: #if 0 hm@28: // Block 3 fp@42: ,ECAT_INIT_SLAVE(Beckhoff_EK1100, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3162, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3162, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3162, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3162, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL3102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4102, 2), fp@42: ECAT_INIT_SLAVE(Beckhoff_EL4132, 2) hm@28: #endif hm@28: }; hm@28: hm@28: #define ECAT_SLAVES_COUNT (sizeof(ecat_slaves) / sizeof(EtherCAT_slave_t)) hm@28: hm@28: #define USE_MSR_LIB hm@28: hm@28: #ifdef USE_MSR_LIB hm@28: double value; hm@28: int dig1; hm@28: #endif hm@28: hm@28: /****************************************************************************** hm@28: * hm@28: * Function: next2004 hm@28: * hm@28: *****************************************************************************/ hm@28: hm@28: static int next2004(int *wrap) hm@28: { hm@28: static int i = 0; hm@28: unsigned int j = 0; hm@28: hm@28: *wrap = 0; hm@28: hm@28: for (j = 0; j < ECAT_SLAVES_COUNT; j++) hm@28: { hm@28: i++; hm@28: hm@28: i %= ECAT_SLAVES_COUNT; hm@28: hm@28: if (i == 0) *wrap = 1; hm@28: hm@28: if (ecat_slaves[i].desc == Beckhoff_EL2004) hm@28: { hm@28: return i; hm@28: } hm@28: } hm@28: hm@28: return -1; hm@28: } hm@28: hm@28: hm@28: /****************************************************************************** hm@28: * hm@28: * Function: msr_controller_run() hm@28: * hm@28: *****************************************************************************/ hm@28: hm@28: static void msr_controller_run(void) hm@28: { hm@28: static int ms = 0; hm@28: static int cnt = 0; hm@28: static unsigned long int k = 0; hm@28: static int firstrun = 1; hm@28: hm@28: static int klemme = 0; hm@28: static int kanal = 0; hm@28: static int up_down = 0; hm@28: int wrap = 0; hm@28: fp@42: static unsigned int debug_counter = 0; fp@42: unsigned long t1, t2, t3, t4, t5, t6, t7; fp@42: static unsigned long lt = 0; fp@42: unsigned int tr1, tr2; fp@42: fp@42: rdtscl(t1); hm@28: hm@28: // Prozessdaten lesen hm@28: msr_jitter_run(MSR_ABTASTFREQUENZ); hm@28: fp@42: if (firstrun) klemme = next2004(&wrap); hm@28: hm@28: ms++; hm@28: ms %= 1000; fp@40: if (cnt++ > 200) hm@28: { hm@28: cnt = 0; hm@28: hm@28: if (++kanal > 3) hm@28: { hm@28: kanal = 0; hm@28: klemme = next2004(&wrap); hm@28: hm@28: if (wrap == 1) hm@28: { hm@28: if (up_down == 1) up_down = 0; hm@28: else up_down = 1; hm@28: } hm@28: } hm@28: } hm@28: hm@28: if (klemme >= 0) { fp@42: EtherCAT_write_value(&ecat_slaves[klemme], kanal, up_down); hm@28: } hm@28: hm@28: #if 0 hm@28: EtherCAT_write_value(&ecat_master->slaves[13], 1, ms > 500 ? 0 : 1); hm@28: EtherCAT_write_value(&ecat_master->slaves[14], 2, ms > 500 ? 0 : 1); hm@28: EtherCAT_write_value(&ecat_master->slaves[15], 3, ms > 500 ? 1 : 0); hm@28: #endif hm@28: hm@28: // Prozessdaten schreiben hm@28: rdtscl(k); fp@42: rdtscl(t2); fp@42: fp@42: EtherCAT_process_data_cycle(ecat_master, 0); fp@42: fp@42: t3 = ecat_master->tx_time; fp@42: t4 = ecat_master->rx_time; fp@42: tr1 = ecat_master->rx_tries; fp@42: fp@42: EtherCAT_process_data_cycle(ecat_master, 1); fp@42: fp@42: t5 = ecat_master->tx_time; fp@42: t6 = ecat_master->rx_time; fp@42: tr2 = ecat_master->rx_tries; fp@42: fp@42: //EtherCAT_process_data_cycle(ecat_master, 2); fp@42: fp@42: // Daten lesen und skalieren fp@42: #ifdef USE_MSR_LIB fp@42: value = EtherCAT_read_value(&ecat_slaves[5], 0) / 3276.0; fp@42: dig1 = EtherCAT_read_value(&ecat_slaves[2], 0); fp@42: #endif fp@42: fp@42: rdtscl(t7); fp@42: fp@42: if (debug_counter == MSR_ABTASTFREQUENZ) { fp@42: printk(KERN_DEBUG "%lu: %luŽµs + %luŽµs + %luŽµs + %luŽµs + %luŽµs +" fp@42: " %luŽµs = %luŽµs (%u %u)\n", fp@42: TSC2US(t1 - lt), fp@42: TSC2US(t2 - t1), TSC2US(t3 - t2), TSC2US(t4 - t3), fp@42: TSC2US(t5 - t4), TSC2US(t6 - t5), TSC2US(t7 - t6), fp@42: TSC2US(t7 - t1), tr1, tr2); fp@42: debug_counter = 0; fp@42: } fp@42: fp@42: lt = t1; fp@42: hm@28: firstrun = 0; fp@42: debug_counter++; fp@42: } fp@42: fp@42: /****************************************************************************** fp@42: * fp@42: * Function: msr_run(_interrupt) fp@42: * fp@42: * Beschreibung: Routine wird zyklisch im Timerinterrupt ausgeführt fp@42: * (hier muß alles rein, was Echtzeit ist ...) fp@42: * fp@42: * Parameter: Zeiger auf msr_data fp@42: * fp@42: * Rückgabe: fp@42: * fp@42: * Status: exp fp@42: * fp@42: *****************************************************************************/ hm@28: hm@28: void msr_run(unsigned irq) hm@28: { fp@36: static int counter = 0; fp@36: #ifdef USE_MSR_LIB fp@36: fp@36: timeval_add(&process_time,&process_time,&msr_time_increment); hm@28: hm@28: MSR_ADEOS_INTERRUPT_CODE( hm@28: msr_controller_run(); hm@28: msr_write_kanal_list(); hm@28: ); hm@28: #else hm@28: msr_controller_run(); hm@28: #endif hm@28: /* und wieder in die Timerliste eintragen */ fp@36: /* und neu in die Taskqueue eintragen */ fp@42: //timer.expires += 1; fp@42: //add_timer(&timer); hm@28: fp@40: ipipe_control_irq(irq,0,IPIPE_ENABLE_MASK); //Interrupt bestŽätigen hm@28: if(counter++ > HZREDUCTION) { fp@40: ipipe_propagate_irq(irq); //und weiterreichen hm@28: counter = 0; hm@28: } fp@42: } fp@42: fp@42: void domain_entry (void) fp@42: { fp@42: printk("Domain %s started.\n", ipipe_current_domain->name); hm@28: hm@28: ipipe_get_sysinfo(&sys_info); hm@28: ipipe_virtualize_irq(ipipe_current_domain,sys_info.archdep.tmirq, hm@28: &msr_run, NULL, IPIPE_HANDLE_MASK); hm@28: fp@42: ipipe_tune_timer(1000000000UL/MSR_ABTASTFREQUENZ,0); fp@42: } fp@42: fp@42: /****************************************************************************** fp@42: * fp@42: * Function: msr_register_channels fp@42: * fp@42: * Beschreibung: KanŽäle registrieren fp@42: * fp@42: * Parameter: fp@42: * fp@42: * RŽückgabe: fp@42: * fp@42: * Status: exp fp@42: * fp@42: *****************************************************************************/ hm@28: hm@28: int msr_globals_register(void) hm@28: { hm@28: #ifdef USE_MSR_LIB hm@28: msr_reg_kanal("/value", "V", &value, TDBL); hm@28: msr_reg_kanal("/dig1", "", &dig1, TINT); hm@28: #endif fp@42: #if 0 fp@42: msr_reg_kanal("/Taskinfo/Ecat/TX-Delay","us",&ecat_tx_delay,TUINT); fp@42: msr_reg_kanal("/Taskinfo/Ecat/RX-Delay","us",&ecat_rx_delay,TUINT); fp@42: msr_reg_kanal("/Taskinfo/Ecat/TX-Cnt","",&tx_intr,TUINT); fp@42: msr_reg_kanal("/Taskinfo/Ecat/RX-Cnt","",&rx_intr,TUINT); fp@42: msr_reg_kanal("/Taskinfo/Ecat/Total-Cnt","",&total_intr,TUINT); fp@42: #endif hm@28: return 0; hm@28: } hm@28: fp@42: /****************************************************************************** hm@28: * the init/clean material fp@42: *****************************************************************************/ hm@28: hm@28: int __init init_module() hm@28: { fp@42: unsigned int i; hm@28: struct ipipe_domain_attr attr; //ipipe hm@28: fp@36: // Als allererstes die RT-lib initialisieren fp@36: #ifdef USE_MSR_LIB fp@36: if (msr_rtlib_init(1,MSR_ABTASTFREQUENZ,10,&msr_globals_register) < 0) { hm@28: msr_print_warn("msr_modul: can't initialize rtlib!"); fp@36: goto out_return; hm@28: } hm@28: #endif hm@28: hm@28: msr_jitter_init(); fp@36: fp@36: printk(KERN_INFO "=== Starting 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_msr_cleanup; 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@42: for (i = 0; i < ECAT_SLAVES_COUNT; i++) { fp@42: if (EtherCAT_activate_slave(ecat_master, ecat_slaves + i) < 0) { fp@42: goto out_release_master; fp@42: } fp@36: } fp@36: fp@36: do_gettimeofday(&process_time); fp@36: msr_time_increment.tv_sec=0; fp@36: msr_time_increment.tv_usec=(unsigned int)(1000000/MSR_ABTASTFREQUENZ); hm@28: hm@28: ipipe_init_attr (&attr); hm@28: attr.name = "IPIPE-MSR-MODULE"; hm@28: attr.priority = IPIPE_ROOT_PRIO + 1; hm@28: attr.entry = &domain_entry; hm@28: ipipe_register_domain(&this_domain,&attr); hm@28: fp@36: return 0; fp@36: fp@36: out_release_master: fp@36: EtherCAT_release(ecat_master); fp@36: fp@36: out_msr_cleanup: fp@36: msr_rtlib_cleanup(); fp@36: fp@36: out_return: fp@36: return -1; hm@28: } hm@28: fp@42: /*****************************************************************************/ fp@42: hm@28: void __exit cleanup_module() fp@42: { fp@42: unsigned int i; fp@42: hm@28: msr_print_info("msk_modul: unloading..."); hm@28: hm@28: ipipe_tune_timer(1000000000UL/HZ,0); //alten Timertakt wieder herstellen hm@28: ipipe_unregister_domain(&this_domain); hm@28: hm@28: printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n"); hm@28: hm@28: if (ecat_master) hm@28: { fp@36: printk(KERN_INFO "Deactivating slaves.\n"); fp@42: fp@42: for (i = 0; i < ECAT_SLAVES_COUNT; i++) { fp@42: if (EtherCAT_deactivate_slave(ecat_master, ecat_slaves + i) < 0) { fp@42: printk(KERN_WARNING "Warning - Could not deactivate slave!\n"); fp@42: } fp@42: } fp@42: fp@36: EtherCAT_release(ecat_master); hm@28: } hm@28: hm@28: printk(KERN_INFO "=== EtherCAT environment stopped. ===\n"); hm@28: fp@36: #ifdef USE_MSR_LIB fp@36: msr_rtlib_cleanup(); hm@28: #endif hm@28: } hm@28: fp@42: /*****************************************************************************/ fp@42: hm@28: MODULE_LICENSE("GPL"); hm@28: MODULE_AUTHOR ("Wilhelm Hagemeister "); hm@28: MODULE_DESCRIPTION ("EtherCAT test environment"); hm@28: hm@28: module_init(init_module); hm@28: module_exit(cleanup_module); fp@36: fp@42: /*****************************************************************************/