examples/rt/msr_rt.c
changeset 220 342ad851ec78
child 223 daa5e5656b35
equal deleted inserted replaced
219:7572c5bf7bb3 220:342ad851ec78
       
     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_start_eoe(master)) {
       
   184         printk(KERN_ERR "Failed to start EoE processing!\n");
       
   185         goto out_deactivate;
       
   186     }
       
   187 #endif
       
   188 
       
   189 #if 0
       
   190     if (ecrt_master_fetch_sdo_lists(master)) {
       
   191         printk(KERN_ERR "Failed to fetch SDO lists!\n");
       
   192         goto out_deactivate;
       
   193     }
       
   194     ecrt_master_print(master, 2);
       
   195 #else
       
   196     ecrt_master_print(master, 0);
       
   197 #endif
       
   198 
       
   199 #if 1
       
   200     if (!(slave = ecrt_master_get_slave(master, "0:3"))) {
       
   201         printk(KERN_ERR "Failed to get slave!\n");
       
   202         goto out_deactivate;
       
   203     }
       
   204 
       
   205     if (
       
   206         ecrt_slave_sdo_write_exp8(slave, 0x4061, 1,  1) || // disable frame error bit
       
   207         ecrt_slave_sdo_write_exp8(slave, 0x4061, 2,  0) || // power failure bit
       
   208         ecrt_slave_sdo_write_exp8(slave, 0x4061, 3,  1) || // inhibit time
       
   209         ecrt_slave_sdo_write_exp8(slave, 0x4061, 4,  0) || // test mode
       
   210         ecrt_slave_sdo_write_exp8(slave, 0x4066, 0,  1) || // dualcode
       
   211         ecrt_slave_sdo_write_exp8(slave, 0x4067, 0,  5) || // 125kbaud
       
   212         ecrt_slave_sdo_write_exp8(slave, 0x4068, 0,  0) || // single-turn
       
   213         ecrt_slave_sdo_write_exp8(slave, 0x4069, 0, 25) || // frame size
       
   214         ecrt_slave_sdo_write_exp8(slave, 0x406A, 0, 25) || // data length
       
   215         ecrt_slave_sdo_write_exp16(slave, 0x406B, 0, 30000) // inhibit time in us
       
   216         ) {
       
   217         printk(KERN_ERR "Failed to configure SSI slave!\n");
       
   218         goto out_deactivate;
       
   219     }
       
   220 #endif
       
   221 
       
   222 #if 0
       
   223     if (!(slave = ecrt_master_get_slave(master, "1:0"))) {
       
   224         printk(KERN_ERR "Failed to get slave!\n");
       
   225         goto out_deactivate;
       
   226     }
       
   227     if (ecrt_slave_write_alias(slave, 0x5678)) {
       
   228         printk(KERN_ERR "Failed to write alias!\n");
       
   229         goto out_deactivate;
       
   230     }
       
   231 #endif
       
   232 
       
   233 #ifdef ASYNC
       
   234     // Einmal senden und warten...
       
   235     ecrt_master_prepare_async_io(master);
       
   236 #endif
       
   237 
       
   238     ipipe_init_attr(&attr);
       
   239     attr.name = "IPIPE-MSR-MODULE";
       
   240     attr.priority = IPIPE_ROOT_PRIO + 1;
       
   241     attr.entry = &domain_entry;
       
   242     ipipe_register_domain(&this_domain, &attr);
       
   243     return 0;
       
   244 
       
   245 #if 1
       
   246  out_deactivate:
       
   247     ecrt_master_deactivate(master);
       
   248 #endif
       
   249  out_release_master:
       
   250     ecrt_release_master(master);
       
   251  out_msr_cleanup:
       
   252     msr_rtlib_cleanup();
       
   253  out_return:
       
   254     return -1;
       
   255 }
       
   256 
       
   257 /*****************************************************************************/
       
   258 
       
   259 void __exit cleanup_rt_module(void)
       
   260 {
       
   261     printk(KERN_INFO "Cleanign up rt module...\n");
       
   262 
       
   263     ipipe_tune_timer(1000000000UL / HZ, 0); // Alten Timertakt wiederherstellen
       
   264     ipipe_unregister_domain(&this_domain);
       
   265 
       
   266     printk(KERN_INFO "=== Stopping EtherCAT environment... ===\n");
       
   267     ecrt_master_deactivate(master);
       
   268     ecrt_release_master(master);
       
   269     printk(KERN_INFO "=== EtherCAT environment stopped. ===\n");
       
   270 
       
   271     msr_rtlib_cleanup();
       
   272 }
       
   273 
       
   274 /*****************************************************************************/
       
   275 
       
   276 #define EC_LIT(X) #X
       
   277 #define EC_STR(X) EC_LIT(X)
       
   278 #define COMPILE_INFO "Revision " EC_STR(SVNREV) \
       
   279                      ", compiled by " EC_STR(USER) \
       
   280                      " at " __DATE__ " " __TIME__
       
   281 
       
   282 MODULE_LICENSE("GPL");
       
   283 MODULE_AUTHOR ("Florian Pose <fp@igh-essen.com>");
       
   284 MODULE_DESCRIPTION ("EtherCAT real-time test environment");
       
   285 MODULE_VERSION(COMPILE_INFO);
       
   286 
       
   287 module_init(init_rt_module);
       
   288 module_exit(cleanup_rt_module);
       
   289 
       
   290 /*****************************************************************************/
       
   291 
       
   292 /* Emacs-Konfiguration
       
   293 ;;; Local Variables: ***
       
   294 ;;; c-basic-offset:4 ***
       
   295 ;;; End: ***
       
   296 */