rt/msr_rt.c
branchstable-1.0
changeset 1619 0d4119024f55
parent 1618 5cff10efb927
child 1620 9d7453c16ade
equal deleted inserted replaced
1618:5cff10efb927 1619:0d4119024f55
     1 /******************************************************************************
       
     2  *
       
     3  *  m s r _ r t . c
       
     4  *
       
     5  *  Kernelmodul für 2.6 Kernel zur Meßdatenerfassung, Steuerung und Regelung.
       
     6  *
       
     7  *  $Id$
       
     8  *
       
     9  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    10  *
       
    11  *  This file is part of the IgH EtherCAT Master.
       
    12  *
       
    13  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    14  *  and/or modify it under the terms of the GNU General Public License
       
    15  *  as published by the Free Software Foundation; version 2 of the License.
       
    16  *
       
    17  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    18  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    20  *  GNU General Public License for more details.
       
    21  *
       
    22  *  You should have received a copy of the GNU General Public License
       
    23  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    24  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    25  *
       
    26  *****************************************************************************/
       
    27 
       
    28 // Linux
       
    29 #include <linux/module.h>
       
    30 #include <linux/ipipe.h>
       
    31 #include <linux/slab.h>
       
    32 #include <linux/vmalloc.h>
       
    33 #include <linux/delay.h>
       
    34 
       
    35 // RT_lib
       
    36 #include <msr_main.h>
       
    37 #include <msr_utils.h>
       
    38 #include <msr_messages.h>
       
    39 #include <msr_float.h>
       
    40 #include <msr_reg.h>
       
    41 #include <msr_time.h>
       
    42 #include "msr_param.h"
       
    43 
       
    44 // EtherCAT
       
    45 #include "../include/ecrt.h"
       
    46 
       
    47 #define ASYNC
       
    48 
       
    49 // Defines/Makros
       
    50 #define HZREDUCTION (MSR_ABTASTFREQUENZ / HZ)
       
    51 
       
    52 /*****************************************************************************/
       
    53 /* Globale Variablen */
       
    54 
       
    55 // Adeos
       
    56 static struct ipipe_domain this_domain;
       
    57 static struct ipipe_sysinfo sys_info;
       
    58 
       
    59 // EtherCAT
       
    60 ec_master_t *master = NULL;
       
    61 ec_domain_t *domain1 = NULL;
       
    62 
       
    63 // Prozessdaten
       
    64 void *r_ssi;
       
    65 void *r_ssi_st;
       
    66 
       
    67 // KanŽäle
       
    68 uint32_t k_ssi;
       
    69 uint32_t k_ssi_st;
       
    70 
       
    71 ec_field_init_t domain1_fields[] = {
       
    72     {&r_ssi,    "0:3", "Beckhoff", "EL5001", "InputValue", 0},
       
    73     {&r_ssi_st, "0:3", "Beckhoff", "EL5001", "Status",     0},
       
    74     {}
       
    75 };
       
    76 
       
    77 /*****************************************************************************/
       
    78 
       
    79 static void msr_controller_run(void)
       
    80 {
       
    81 #ifdef ASYNC
       
    82     // Empfangen
       
    83     ecrt_master_async_receive(master);
       
    84     ecrt_domain_process(domain1);
       
    85 #else
       
    86     // Senden und empfangen
       
    87     ecrt_domain_queue(domain1);
       
    88     ecrt_master_run(master);
       
    89     ecrt_master_sync_io(master);
       
    90     ecrt_domain_process(domain1);
       
    91 #endif
       
    92 
       
    93     // Prozessdaten verarbeiten
       
    94     k_ssi =    EC_READ_U32(r_ssi);
       
    95     k_ssi_st = EC_READ_U8 (r_ssi_st);
       
    96 
       
    97 #ifdef ASYNC
       
    98     // Senden
       
    99     ecrt_domain_queue(domain1);
       
   100     ecrt_master_run(master);
       
   101     ecrt_master_async_send(master);
       
   102 #endif
       
   103 }
       
   104 
       
   105 /*****************************************************************************/
       
   106 
       
   107 int msr_globals_register(void)
       
   108 {
       
   109     msr_reg_kanal("/ssi_position", "", &k_ssi,    TUINT);
       
   110     msr_reg_kanal("/ssi_status",   "", &k_ssi_st, TUINT);
       
   111     return 0;
       
   112 }
       
   113 
       
   114 /*****************************************************************************/
       
   115 
       
   116 void msr_run(unsigned irq)
       
   117 {
       
   118     static int counter = 0;
       
   119 
       
   120     MSR_ADEOS_INTERRUPT_CODE(msr_controller_run(); msr_write_kanal_list(););
       
   121 
       
   122     ipipe_control_irq(irq, 0, IPIPE_ENABLE_MASK); // Interrupt bestŽätigen
       
   123     if (++counter >= HZREDUCTION) {
       
   124 	ipipe_propagate_irq(irq);  // und weiterreichen
       
   125 	counter = 0;
       
   126     }
       
   127 }
       
   128 
       
   129 /*****************************************************************************/
       
   130 
       
   131 void domain_entry(void)
       
   132 {
       
   133     printk("Domain %s started.\n", ipipe_current_domain->name);
       
   134 
       
   135     ipipe_get_sysinfo(&sys_info);
       
   136     ipipe_virtualize_irq(ipipe_current_domain,sys_info.archdep.tmirq,
       
   137 			 &msr_run, NULL, IPIPE_HANDLE_MASK);
       
   138 
       
   139     ipipe_tune_timer(1000000000UL / MSR_ABTASTFREQUENZ, 0);
       
   140 }
       
   141 
       
   142 /*****************************************************************************/
       
   143 
       
   144 int __init init_rt_module(void)
       
   145 {
       
   146     struct ipipe_domain_attr attr; //ipipe
       
   147 #if 1
       
   148     ec_slave_t *slave;
       
   149 #endif
       
   150 
       
   151     // Als allererstes die RT-Lib initialisieren
       
   152     if (msr_rtlib_init(1, MSR_ABTASTFREQUENZ, 10, &msr_globals_register) < 0) {
       
   153         printk(KERN_ERR "Failed to initialize rtlib!\n");
       
   154         goto out_return;
       
   155     }
       
   156 
       
   157     if ((master = ecrt_request_master(0)) == NULL) {
       
   158         printk(KERN_ERR "Failed to request master 0!\n");
       
   159         goto out_msr_cleanup;
       
   160     }
       
   161 
       
   162     //ecrt_master_print(master, 2);
       
   163 
       
   164     printk(KERN_INFO "Creating domains...\n");
       
   165     if (!(domain1 = ecrt_master_create_domain(master))) {
       
   166         printk(KERN_ERR "Failed to create domains!\n");
       
   167         goto out_release_master;
       
   168     }
       
   169 
       
   170     printk(KERN_INFO "Registering domain fields...\n");
       
   171     if (ecrt_domain_register_field_list(domain1, domain1_fields)) {
       
   172         printk(KERN_ERR "Failed to register domain fields.\n");
       
   173         goto out_release_master;
       
   174     }
       
   175 
       
   176     printk(KERN_INFO "Activating master...\n");
       
   177     if (ecrt_master_activate(master)) {
       
   178         printk(KERN_ERR "Could not activate master!\n");
       
   179         goto out_release_master;
       
   180     }
       
   181 
       
   182 #if 0
       
   183     if (ecrt_master_fetch_sdo_lists(master)) {
       
   184         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   185         goto out_deactivate;
       
   186     }
       
   187     ecrt_master_print(master, 2);
       
   188 #else
       
   189     ecrt_master_print(master, 0);
       
   190 #endif
       
   191 
       
   192 #if 1
       
   193     if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
       
   194         printk(KERN_ERR "Failed to get slave!\n");
       
   195         goto out_deactivate;
       
   196     }
       
   197 
       
   198     if (
       
   199         ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  1) || // disable frame error bit
       
   200         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  0) || // power failure bit
       
   201         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
       
   202         ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
       
   203         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  1) || // dualcode
       
   204         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
       
   205         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
       
   206         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
       
   207         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
       
   208         ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us
       
   209         ) {
       
   210         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   211         goto out_deactivate;
       
   212     }
       
   213 #endif
       
   214 
       
   215 #if 0
       
   216     if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
       
   217         printk(KERN_ERR "Failed to get slave!\n");
       
   218         goto out_deactivate;
       
   219     }
       
   220     if (ecrt_slave_write_alias(slave, 0x5678)) {
       
   221         printk(KERN_ERR "Failed to write alias!\n");
       
   222         goto out_deactivate;
       
   223     }
       
   224 #endif
       
   225 
       
   226 #ifdef ASYNC
       
   227     // Einmal senden und warten...
       
   228     ecrt_master_prepare_async_io(master);
       
   229 #endif
       
   230 
       
   231     ipipe_init_attr(&attr);
       
   232     attr.name = "IPIPE-MSR-MODULE";
       
   233     attr.priority = IPIPE_ROOT_PRIO + 1;
       
   234     attr.entry = &domain_entry;
       
   235     ipipe_register_domain(&this_domain, &attr);
       
   236     return 0;
       
   237 
       
   238 #if 1
       
   239  out_deactivate:
       
   240     ecrt_master_deactivate(master);
       
   241 #endif
       
   242  out_release_master:
       
   243     ecrt_release_master(master);
       
   244  out_msr_cleanup:
       
   245     msr_rtlib_cleanup();
       
   246  out_return:
       
   247     return -1;
       
   248 }
       
   249 
       
   250 /*****************************************************************************/
       
   251 
       
   252 void __exit cleanup_rt_module(void)
       
   253 {
       
   254     printk(KERN_INFO "Cleanign up rt module...\n");
       
   255 
       
   256     ipipe_tune_timer(1000000000UL / HZ, 0); // Alten Timertakt wiederherstellen
       
   257     ipipe_unregister_domain(&this_domain);
       
   258 
       
   259     printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n");
       
   260     ecrt_master_deactivate(master);
       
   261     ecrt_release_master(master);
       
   262     printk(KERN_INFO "=== EtherCAT environment stopped. ===\n");
       
   263 
       
   264     msr_rtlib_cleanup();
       
   265 }
       
   266 
       
   267 /*****************************************************************************/
       
   268 
       
   269 #define EC_LIT(X) #X
       
   270 #define EC_STR(X) EC_LIT(X)
       
   271 #define COMPILE_INFO "Revision " EC_STR(SVNREV) \
       
   272                      ", compiled by " EC_STR(USER) \
       
   273                      " at " __DATE__ " " __TIME__
       
   274 
       
   275 MODULE_LICENSE("GPL");
       
   276 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
       
   277 MODULE_DESCRIPTION ("EtherCAT real-time test environment");
       
   278 MODULE_VERSION(COMPILE_INFO);
       
   279 
       
   280 module_init(init_rt_module);
       
   281 module_exit(cleanup_rt_module);
       
   282 
       
   283 /*****************************************************************************/
       
   284 
       
   285 /* Emacs-Konfiguration
       
   286 ;;; Local Variables: ***
       
   287 ;;; c-basic-offset:4 ***
       
   288 ;;; End: ***
       
   289 */