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 <linux/module.h>
hm@28: #include <linux/ipipe.h>
fp@73: #include <linux/slab.h>
fp@73: #include <linux/vmalloc.h>
fp@104: #include <linux/delay.h>
hm@28: 
fp@45: // RT_lib
fp@45: #include <msr_main.h>
fp@45: #include <msr_utils.h>
fp@45: #include <msr_messages.h>
fp@45: #include <msr_float.h>
hm@28: #include <msr_reg.h>
fp@73: #include <msr_time.h>
fp@45: #include "msr_param.h"
fp@45: 
fp@45: // EtherCAT
fp@104: #include "../include/ecrt.h"
fp@104: 
fp@106: #define ASYNC
hm@28: 
fp@45: // Defines/Makros
fp@47: #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
hm@28: 
fp@45: /*****************************************************************************/
fp@45: /* Globale Variablen */
fp@45: 
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@54: ec_master_t *master = NULL;
fp@73: ec_domain_t *domain1 = NULL;
fp@73: 
fp@73: // Prozessdaten
fp@156: void *r_ssi;
fp@170: void *r_ssi_st;
fp@156: 
fp@156: // KanŽäle
fp@170: uint32_t k_ssi;
fp@170: uint32_t k_ssi_st;
fp@156: 
fp@73: ec_field_init_t domain1_fields[] = {
fp@170:     {&r_ssi,    "5", "Beckhoff", "EL5001", "InputValue", 0},
fp@170:     {&r_ssi_st, "5", "Beckhoff", "EL5001", "Status",     0},
fp@73:     {}
fp@61: };
fp@98: 
fp@73: /*****************************************************************************/
hm@28: 
hm@28: static void msr_controller_run(void)
hm@28: {
fp@104: #ifdef ASYNC
fp@104:     // Empfangen
fp@104:     ecrt_master_async_receive(master);
fp@104:     ecrt_domain_process(domain1);
fp@136: #else
fp@104:     // Senden und empfangen
fp@104:     ecrt_domain_queue(domain1);
fp@144:     ecrt_master_run(master);
fp@104:     ecrt_master_sync_io(master);
fp@104:     ecrt_domain_process(domain1);
fp@156: #endif
fp@104: 
fp@104:     // Prozessdaten verarbeiten
fp@170:     k_ssi =    EC_READ_U32(r_ssi);
fp@170:     k_ssi_st = EC_READ_U8 (r_ssi_st);
fp@156: 
fp@156: #ifdef ASYNC
fp@156:     // Senden
fp@156:     ecrt_domain_queue(domain1);
fp@156:     ecrt_master_run(master);
fp@156:     ecrt_master_async_send(master);
fp@156: #endif
fp@82: }
fp@82: 
fp@82: /*****************************************************************************/
fp@82: 
fp@82: int msr_globals_register(void)
fp@82: {
fp@170:     msr_reg_kanal("/ssi_position", "", &k_ssi,    TUINT);
fp@170:     msr_reg_kanal("/ssi_status",   "", &k_ssi_st, TUINT);
fp@82:     return 0;
fp@73: }
fp@73: 
fp@73: /*****************************************************************************/
hm@28: 
hm@28: void msr_run(unsigned irq)
hm@28: {
fp@45:     static int counter = 0;
fp@45: 
fp@45:     MSR_ADEOS_INTERRUPT_CODE(msr_controller_run(); msr_write_kanal_list(););
hm@28: 
fp@73:     ipipe_control_irq(irq, 0, IPIPE_ENABLE_MASK); // Interrupt bestŽätigen
fp@73:     if (++counter >= HZREDUCTION) {
fp@73: 	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@68:     ipipe_tune_timer(1000000000UL / MSR_ABTASTFREQUENZ, 0);
fp@42: }
fp@42: 
fp@73: /*****************************************************************************/
hm@28: 
fp@54: int __init init_rt_module(void)
fp@54: {
hm@28:     struct ipipe_domain_attr attr; //ipipe
fp@170: #if 1
fp@138:     ec_slave_t *slave;
fp@144: #endif
fp@137: 
fp@137:     // Als allererstes die RT-Lib initialisieren
fp@73:     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: 
fp@104:     if ((master = ecrt_request_master(0)) == NULL) {
fp@158:         printk(KERN_ERR "Failed to request master 0!\n");
fp@36:         goto out_msr_cleanup;
fp@36:     }
fp@36: 
fp@156:     //ecrt_master_print(master, 2);
fp@156: 
fp@158:     printk(KERN_INFO "Creating domains...\n");
fp@104:     if (!(domain1 = ecrt_master_create_domain(master))) {
fp@158:         printk(KERN_ERR "Failed to create domains!\n");
fp@73:         goto out_release_master;
fp@73:     }
fp@73: 
fp@73:     printk(KERN_INFO "Registering domain fields...\n");
fp@104:     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
fp@98:         printk(KERN_ERR "Failed to register domain fields.\n");
fp@98:         goto out_release_master;
fp@98:     }
fp@98: 
fp@73:     printk(KERN_INFO "Activating master...\n");
fp@104:     if (ecrt_master_activate(master)) {
fp@80:         printk(KERN_ERR "Could not activate master!\n");
fp@73:         goto out_release_master;
fp@73:     }
fp@73: 
fp@158: #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@142:     ecrt_master_print(master, 1);
fp@140: #endif
fp@140: 
fp@170: #if 1
fp@170:     if (!(slave = ecrt_master_get_slave(master, "5"))) {
fp@170:         printk(KERN_ERR "Failed to get slave!\n");
fp@138:         goto out_deactivate;
fp@138:     }
fp@138: 
fp@170:     if (
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  1) || // disable frame error bit
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  0) || // power failure bit
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  1) || // dualcode
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
fp@170:         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
fp@170:         ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us
fp@170:         ) {
fp@133:         printk(KERN_ERR "Failed to configure SSI slave!\n");
fp@133:         goto out_deactivate;
fp@133:     }
fp@133: #endif
fp@133: 
fp@104: #ifdef ASYNC
fp@106:     // Einmal senden und warten...
fp@106:     ecrt_master_prepare_async_io(master);
fp@104: #endif
fp@104: 
fp@61:     ipipe_init_attr(&attr);
fp@61:     attr.name = "IPIPE-MSR-MODULE";
hm@28:     attr.priority = IPIPE_ROOT_PRIO + 1;
fp@61:     attr.entry = &domain_entry;
fp@61:     ipipe_register_domain(&this_domain, &attr);
fp@36:     return 0;
fp@36: 
fp@170: #if 1
fp@133:  out_deactivate:
fp@133:     ecrt_master_deactivate(master);
fp@144: #endif
fp@36:  out_release_master:
fp@104:     ecrt_release_master(master);
fp@36:  out_msr_cleanup:
fp@36:     msr_rtlib_cleanup();
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@156:     if (master) {
fp@43:         printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n");
fp@43: 
fp@73:         printk(KERN_INFO "Deactivating master...\n");
fp@104:         ecrt_master_deactivate(master);
fp@104:         ecrt_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: 
fp@98: #define EC_LIT(X) #X
fp@98: #define EC_STR(X) EC_LIT(X)
fp@98: #define COMPILE_INFO "Revision " EC_STR(SVNREV) \
fp@98:                      ", compiled by " EC_STR(USER) \
fp@98:                      " at " __DATE__ " " __TIME__
fp@98: 
hm@28: MODULE_LICENSE("GPL");
fp@73: MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
fp@73: MODULE_DESCRIPTION ("EtherCAT real-time test environment");
fp@98: MODULE_VERSION(COMPILE_INFO);
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: */