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@47: * fp@47: * Autor: Wilhelm Hagemeister, Florian Pose 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: fp@45: // Linux hm@28: #include hm@28: #include hm@28: fp@45: // RT_lib fp@45: #include fp@45: #include fp@45: #include fp@45: #include hm@28: #include fp@45: #include "msr_param.h" fp@45: #include "msr_jitter.h" fp@45: fp@45: // EtherCAT fp@54: #include "../include/EtherCAT_rt.h" fp@56: #include "../eclib/eclib.h" hm@28: fp@45: // Defines/Makros fp@45: #define TSC2US(T1, T2) ((T2 - T1) * 1000UL / cpu_khz) fp@47: #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ) hm@28: fp@45: /*****************************************************************************/ fp@45: /* Globale Variablen */ fp@45: fp@45: // RT_lib fp@42: extern struct timeval process_time; fp@42: struct timeval msr_time_increment; // Increment per Interrupt hm@28: fp@45: // Adeos hm@28: static struct ipipe_domain this_domain; hm@28: static struct ipipe_sysinfo sys_info; hm@28: fp@45: // EtherCAT fp@45: fp@54: ec_master_t *master = NULL; fp@47: static unsigned int ecat_bus_time = 0; fp@47: static unsigned int ecat_timeouts = 0; hm@28: fp@56: ec_slave_t *s_in1, *s_out1, *s_out2, *s_out3; fp@55: hm@28: double value; hm@28: int dig1; fp@55: fp@55: /*****************************************************************************/ fp@55: fp@55: static int register_slaves(void) fp@55: { fp@56: s_in1 = EtherCAT_rt_register_slave(master, 1, "Beckhoff", "EL3102", 0); fp@56: s_out1 = EtherCAT_rt_register_slave(master, 8, "Beckhoff", "EL2004", 0); fp@56: s_out2 = EtherCAT_rt_register_slave(master, 9, "Beckhoff", "EL2004", 0); fp@56: s_out3 = EtherCAT_rt_register_slave(master, 10, "Beckhoff", "EL2004", 0); fp@56: fp@56: return !s_in1 || !s_out1 || !s_out2 || !s_out3; fp@55: } 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: { fp@56: static unsigned int counter = 0; fp@56: hm@28: msr_jitter_run(MSR_ABTASTFREQUENZ); hm@28: fp@56: if (counter) { fp@56: counter--; fp@56: } fp@56: else { fp@56: // "Star Trek"-Effekte fp@56: *((unsigned char *) s_out1->process_data) = jiffies; fp@56: *((unsigned char *) s_out2->process_data) = jiffies >> 4; fp@56: *((unsigned char *) s_out3->process_data) = jiffies >> 8; fp@56: fp@56: counter = MSR_ABTASTFREQUENZ / 4; fp@56: } fp@56: fp@56: if (((char *) s_in1->process_data)[2] < 0) fp@56: ((unsigned char *) s_out3->process_data)[0] |= 8; fp@56: else fp@56: ((unsigned char *) s_out3->process_data)[0] &= ~8; fp@56: fp@54: fp@54: // Prozessdaten lesen und schreiben fp@56: EtherCAT_rt_domain_xio(master, 0, 40); 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@45: static int counter = 0; fp@45: fp@45: timeval_add(&process_time, &process_time, &msr_time_increment); fp@45: MSR_ADEOS_INTERRUPT_CODE(msr_controller_run(); msr_write_kanal_list();); hm@28: fp@40: ipipe_control_irq(irq,0,IPIPE_ENABLE_MASK); //Interrupt bestŽätigen fp@45: if (counter++ > HZREDUCTION) { fp@40: ipipe_propagate_irq(irq); //und weiterreichen hm@28: counter = 0; hm@28: } fp@42: } fp@42: fp@45: /*****************************************************************************/ fp@45: fp@45: 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: msr_reg_kanal("/value", "V", &value, TDBL); hm@28: msr_reg_kanal("/dig1", "", &dig1, TINT); fp@45: fp@45: msr_reg_kanal("/Taskinfo/EtherCAT/BusTime", "us", &ecat_bus_time, TUINT); fp@47: msr_reg_kanal("/Taskinfo/EtherCAT/Timeouts", "", &ecat_timeouts, TUINT); fp@45: fp@45: return 0; hm@28: } hm@28: fp@42: /****************************************************************************** hm@28: * the init/clean material fp@42: *****************************************************************************/ hm@28: fp@54: int __init init_rt_module(void) fp@54: { hm@28: struct ipipe_domain_attr attr; //ipipe hm@28: fp@36: // Als allererstes die RT-lib initialisieren 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: hm@28: msr_jitter_init(); fp@36: fp@36: printk(KERN_INFO "=== Starting EtherCAT environment... ===\n"); fp@36: fp@54: if ((master = EtherCAT_rt_request_master(0)) == NULL) { fp@55: printk(KERN_ERR "Error requesting master 0!\n"); fp@36: goto out_msr_cleanup; fp@36: } fp@36: fp@55: printk("Registering EtherCAT slaves.\n"); fp@55: if (register_slaves()) { 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@54: if (EtherCAT_rt_activate_slaves(master) < 0) { fp@54: printk(KERN_ERR "EtherCAT: Could not activate slaves!\n"); fp@54: goto out_release_master; 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@54: EtherCAT_rt_release_master(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: fp@54: void __exit cleanup_rt_module(void) fp@54: { hm@28: msr_print_info("msk_modul: unloading..."); hm@28: fp@45: ipipe_tune_timer(1000000000UL / HZ, 0); //alten Timertakt wieder herstellen hm@28: ipipe_unregister_domain(&this_domain); hm@28: fp@54: if (master) hm@28: { fp@43: printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n"); fp@43: fp@36: printk(KERN_INFO "Deactivating slaves.\n"); fp@42: fp@54: if (EtherCAT_rt_deactivate_slaves(master) < 0) { fp@54: printk(KERN_WARNING "Warning - Could not deactivate slaves!\n"); fp@42: } fp@42: fp@54: EtherCAT_rt_release_master(master); fp@43: fp@43: printk(KERN_INFO "=== EtherCAT environment stopped. ===\n"); fp@43: } hm@28: fp@36: msr_rtlib_cleanup(); 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: fp@54: module_init(init_rt_module); fp@54: module_exit(cleanup_rt_module); fp@54: fp@54: /*****************************************************************************/ fp@55: fp@55: /* Emacs-Konfiguration fp@55: ;;; Local Variables: *** fp@55: ;;; c-basic-offset:4 *** fp@55: ;;; End: *** fp@55: */