Working RTAI RTDM module
authorAndreas Stewering-Bone <ab@igh-essen.com>
Tue, 22 Mar 2011 09:15:19 +0100
changeset 2058 543c4ce9e86e
parent 2057 23fca60b7ca2
child 2059 ab0b96ac18bb
Working RTAI RTDM module
LXRT exmaple application to be tested
configure.ac
examples/Makefile.am
examples/rtai_rtdm/Makefile.am
examples/rtai_rtdm/main.c
rtdm/Kbuild.in
rtdm/module.c
--- a/configure.ac	Mon Mar 21 21:07:38 2011 +0100
+++ b/configure.ac	Tue Mar 22 09:15:19 2011 +0100
@@ -369,9 +369,11 @@
     ),
     [
         rtaidir=[$withval]
+        rtai=1
     ],
     [
         rtaidir=""
+        rtai=0
     ]
 )
 
@@ -387,7 +389,8 @@
 fi
 
 AC_SUBST(RTAI_DIR,[$rtaidir])
-
+AM_CONDITIONAL(ENABLE_RTAI, test "x$rtai" = "x1")
+AC_SUBST(ENABLE_RTAI,[$rtai])
 
 #------------------------------------------------------------------------------
 # Xenomai path (optional)
@@ -396,13 +399,15 @@
 AC_ARG_WITH([xenomai-dir],
     AC_HELP_STRING(
         [--with-xenomai-dir=<DIR>],
-        [Xenomai path (only for RTDM Interface or Xenomai examples)]
+        [Xenomai path (only for RTDM Interface)]
     ),
     [
         xenomaidir=[$withval]
+        xeno=1
     ],
     [
         xenomaidir=""
+        xeno=0
     ]
 )
 
@@ -418,7 +423,8 @@
 fi
 
 AC_SUBST(XENOMAI_DIR,[$xenomaidir])
-
+AM_CONDITIONAL(ENABLE_XENOMAI, test "x$xeno" = "x1")
+AC_SUBST(ENABLE_XENOMAI,[$xeno])
 
 #------------------------------------------------------------------------------
 # RTDM Interface (optional)
@@ -689,6 +695,7 @@
         examples/user/Makefile
         examples/xenomai/Makefile
         examples/xenomai_posix/Makefile
+        examples/rtai_rtdm/Makefile
         include/Makefile
         lib/Makefile
         m4/Makefile
--- a/examples/Makefile.am	Mon Mar 21 21:07:38 2011 +0100
+++ b/examples/Makefile.am	Tue Mar 22 09:15:19 2011 +0100
@@ -41,9 +41,16 @@
 endif
 
 if ENABLE_RTDM
+if ENABLE_XENOMAI
 SUBDIRS += \
 	xenomai \
-	xenomai_posix
+	xenomai_posix 
+endif
+
+if ENABLE_RTAI
+SUBDIRS += \
+	rtai_rtdm
+endif
 endif
 
 DIST_SUBDIRS = \
@@ -54,6 +61,7 @@
 	tty \
 	xenomai \
 	xenomai_posix \
+	rtai_rtdm \
 	user
 
 EXTRA_DIST = \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/rtai_rtdm/Makefile.am	Tue Mar 22 09:15:19 2011 +0100
@@ -0,0 +1,40 @@
+#------------------------------------------------------------------------------
+#
+#  $Id$
+#
+#  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
+#
+#  This file is part of the IgH EtherCAT Master.
+#
+#  The IgH EtherCAT Master is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License version 2, as
+#  published by the Free Software Foundation.
+#
+#  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+#  Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License along with
+#  the IgH EtherCAT Master; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#
+#  ---
+#  
+#  The license mentioned above concerns the source code only. Using the
+#  EtherCAT technology and brand is only permitted in compliance with the
+#  industrial property and similar rights of Beckhoff Automation GmbH.
+#
+#------------------------------------------------------------------------------
+
+
+AM_CFLAGS = -Wall
+
+noinst_PROGRAMS = ec_rtai_rtdm_example
+
+ec_rtai_rtdm_example_SOURCES = main.c
+ec_rtai_rtdm_example_CFLAGS = -I. -I/opt/etherlab/rtai/include -O2 -I/home/ab/Download/rtai/linux-2.6.32.11/include -Wall -Wstrict-prototypes -pipe -I$(top_srcdir)/include
+ec_rtai_rtdm_example_LDFLAGS = -L/opt/etherlab/rtai/lib -llxrt -lrtdm -lpthread -L$(top_builddir)/lib/.libs -lethercat 
+
+
+#------------------------------------------------------------------------------
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/rtai_rtdm/main.c	Tue Mar 22 09:15:19 2011 +0100
@@ -0,0 +1,748 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  main.c	        Copyright (C) 2011       IgH Andreas Stewering-Bone
+ *
+ *  This file is part of ethercatrtdm interface to IgH EtherCAT master 
+ *  
+ *  The IgH EtherCAT master is free software; you can
+ *  redistribute it and/or modify it under the terms of the GNU Lesser General
+ *  Public License as published by the Free Software Foundation; version 2.1
+ *  of the License.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT master userspace library is distributed in the hope that
+ *  it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public License
+ *  along with the IgH EtherCAT master userspace library. If not, see
+ *  <http://www.gnu.org/licenses/>.
+ *  
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ *****************************************************************************/
+
+
+
+
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#include <rtai_lxrt.h>
+#include <rtdm/rtdm.h>
+
+#include "../../include/ecrt.h"
+#include "../../include/ec_rtdm.h"
+
+
+
+#define rt_printf(X,Y) 
+
+#define NSEC_PER_SEC 1000000000
+
+RT_TASK* task;    // Stores a handle.
+int priority=0;   // Highest
+int stack_size=0; // Use default (512)
+int msg_size=0;   // Use default (256)
+
+static unsigned int cycle = 1000; /* 1 ms */
+
+int rt_fd = -1;
+int run=0;
+
+unsigned int sync_ref_counter = 0;
+
+CstructMstrAttach MstrAttach;
+
+/****************************************************************************/
+
+
+
+
+// Optional features
+#define CONFIGURE_PDOS  1
+
+
+/****************************************************************************/
+
+// EtherCAT
+static ec_master_t *master = NULL;
+static ec_master_state_t master_state = {};
+
+static ec_domain_t *domain1 = NULL;
+static ec_domain_state_t domain1_state = {};
+
+
+/****************************************************************************/
+static uint8_t *domain1_pd = NULL;
+
+// process data
+
+#define BusCoupler01_Pos    0, 0
+#define DigOutSlave01_Pos   0, 1
+#define DigOutSlave02_Pos   0, 2
+#define DigInSlave01_Pos    0, 3
+#define AnaOutSlave01_Pos   0, 4
+#define AnaInSlave01_Pos    0, 5
+#define BusCoupler02_Pos    0, 6
+#define AnaInSlave02_Pos    0, 7
+
+
+#define Beckhoff_EK1100 0x00000002, 0x044c2c52
+#define Beckhoff_EL1014 0x00000002, 0x03f63052
+#define Beckhoff_EL2004 0x00000002, 0x07d43052
+#define Beckhoff_EL4132 0x00000002, 0x10243052
+#define Beckhoff_EL3102 0x00000002, 0x0c1e3052
+#define Beckhoff_EL4102 0x00000002, 0x10063052
+#define Beckhoff_EL6731 0x00000002, 0x1a4b3052
+#define Beckhoff_EL6600 0x00000002, 0x19c93052
+#define Beckhoff_EL3602 0x00000002, 0x0e123052
+#define Beckhoff_EL5151 0x00000002, 0x141f3052
+
+
+// offsets for PDO entries
+static unsigned int off_dig_out0      = 0;
+static unsigned int off_dig_out1      = 0;
+static unsigned int off_dig_out2      = 0;
+static unsigned int off_dig_out3      = 0;
+static unsigned int off_dig_in0       = 0;
+static unsigned int off_ana_out0      = 0;
+static unsigned int off_ana_out1      = 0;
+static unsigned int off_ana_in0_status = 0;
+static unsigned int off_ana_in0_value  = 0;
+static unsigned int off_ana_in1_status = 0;
+static unsigned int off_ana_in1_value  = 0;
+
+
+// process data
+unsigned int bit_position0=0; /* Pointer to a variable to store a bit */
+unsigned int bit_position1=0; /* Pointer to a variable to store a bit */
+unsigned int bit_position2=0; /* Pointer to a variable to store a bit */
+unsigned int bit_position3=0; /* Pointer to a variable to store a bit */
+
+const static ec_pdo_entry_reg_t domain1_regs[] = {
+   {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7000, 0x01, &off_dig_out0, &bit_position0},
+   {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7010, 0x01, &off_dig_out1, &bit_position1},
+   {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7020, 0x01, &off_dig_out2, &bit_position2},
+   {DigOutSlave01_Pos, Beckhoff_EL2004, 0x7030, 0x01, &off_dig_out3, &bit_position3},
+   {DigInSlave01_Pos,  Beckhoff_EL1014, 0x6000, 0x01, &off_dig_in0},
+   {AnaOutSlave01_Pos, Beckhoff_EL4132, 0x3001, 0x01, &off_ana_out0},
+   {AnaOutSlave01_Pos, Beckhoff_EL4132, 0x3002, 0x01, &off_ana_out1},
+   {AnaInSlave01_Pos,  Beckhoff_EL3102, 0x3101, 0x01, &off_ana_in0_status},
+   {AnaInSlave01_Pos,  Beckhoff_EL3102, 0x3101, 0x02, &off_ana_in0_value},
+   {AnaInSlave01_Pos,  Beckhoff_EL3102, 0x3102, 0x01, &off_ana_in1_status},
+   {AnaInSlave01_Pos,  Beckhoff_EL3102, 0x3102, 0x02, &off_ana_in1_value},
+   {}
+};
+
+char rt_dev_file[64];
+static unsigned int blink = 0;
+
+static ec_slave_config_t *sc_dig_out_01 = NULL;
+
+static ec_slave_config_t *sc_dig_out_02 = NULL;
+
+static ec_slave_config_t *sc_dig_in_01 = NULL;
+
+static ec_slave_config_t *sc_ana_out_01 = NULL;
+
+static ec_slave_config_t *sc_ana_in_01 = NULL;
+
+static ec_slave_config_t *sc_ana_in_02 = NULL;
+
+/*****************************************************************************/
+
+/* Slave 1, "EL2004"
+ * Vendor ID:       0x00000002
+ * Product code:    0x07d43052
+ * Revision number: 0x00100000
+ */
+
+ec_pdo_entry_info_t slave_1_pdo_entries[] = {
+   {0x7000, 0x01, 1}, /* Output */
+   {0x7010, 0x01, 1}, /* Output */
+   {0x7020, 0x01, 1}, /* Output */
+   {0x7030, 0x01, 1}, /* Output */
+};
+
+ec_pdo_info_t slave_1_pdos[] = {
+   {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
+   {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
+   {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
+   {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
+};
+
+ec_sync_info_t slave_1_syncs[] = {
+   {0, EC_DIR_OUTPUT, 4, slave_1_pdos + 0, EC_WD_ENABLE},
+   {0xff}
+};
+
+/* Slave 2, "EL2004"
+ * Vendor ID:       0x00000002
+ * Product code:    0x07d43052
+ * Revision number: 0x00100000
+ */
+
+ec_pdo_entry_info_t slave_2_pdo_entries[] = {
+   {0x7000, 0x01, 1}, /* Output */
+   {0x7010, 0x01, 1}, /* Output */
+   {0x7020, 0x01, 1}, /* Output */
+   {0x7030, 0x01, 1}, /* Output */
+};
+
+ec_pdo_info_t slave_2_pdos[] = {
+   {0x1600, 1, slave_2_pdo_entries + 0}, /* Channel 1 */
+   {0x1601, 1, slave_2_pdo_entries + 1}, /* Channel 2 */
+   {0x1602, 1, slave_2_pdo_entries + 2}, /* Channel 3 */
+   {0x1603, 1, slave_2_pdo_entries + 3}, /* Channel 4 */
+};
+
+ec_sync_info_t slave_2_syncs[] = {
+   {0, EC_DIR_OUTPUT, 4, slave_2_pdos + 0, EC_WD_ENABLE},
+   {0xff}
+};
+
+/* Slave 3, "EL1014"
+ * Vendor ID:       0x00000002
+ * Product code:    0x03f63052
+ * Revision number: 0x00100000
+ */
+
+ec_pdo_entry_info_t slave_3_pdo_entries[] = {
+   {0x6000, 0x01, 1}, /* Input */
+   {0x6010, 0x01, 1}, /* Input */
+   {0x6020, 0x01, 1}, /* Input */
+   {0x6030, 0x01, 1}, /* Input */
+};
+
+ec_pdo_info_t slave_3_pdos[] = {
+   {0x1a00, 1, slave_3_pdo_entries + 0}, /* Channel 1 */
+   {0x1a01, 1, slave_3_pdo_entries + 1}, /* Channel 2 */
+   {0x1a02, 1, slave_3_pdo_entries + 2}, /* Channel 3 */
+   {0x1a03, 1, slave_3_pdo_entries + 3}, /* Channel 4 */
+};
+
+ec_sync_info_t slave_3_syncs[] = {
+   {0, EC_DIR_INPUT, 4, slave_3_pdos + 0, EC_WD_DISABLE},
+   {0xff}
+};
+
+/* Slave 4, "EL4132"
+ * Vendor ID:       0x00000002
+ * Product code:    0x10243052
+ * Revision number: 0x03f90000
+ */
+
+ec_pdo_entry_info_t slave_4_pdo_entries[] = {
+   {0x3001, 0x01, 16}, /* Output */
+   {0x3002, 0x01, 16}, /* Output */
+};
+
+ec_pdo_info_t slave_4_pdos[] = {
+   {0x1600, 1, slave_4_pdo_entries + 0}, /* RxPDO 01 mapping */
+   {0x1601, 1, slave_4_pdo_entries + 1}, /* RxPDO 02 mapping */
+};
+
+ec_sync_info_t slave_4_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 2, slave_4_pdos + 0, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {0xff}
+};
+
+/* Slave 5, "EL3102"
+ * Vendor ID:       0x00000002
+ * Product code:    0x0c1e3052
+ * Revision number: 0x00000000
+ */
+
+ec_pdo_entry_info_t slave_5_pdo_entries[] = {
+   {0x3101, 0x01, 8}, /* Status */
+   {0x3101, 0x02, 16}, /* Value */
+   {0x3102, 0x01, 8}, /* Status */
+   {0x3102, 0x02, 16}, /* Value */
+};
+
+ec_pdo_info_t slave_5_pdos[] = {
+   {0x1a00, 2, slave_5_pdo_entries + 0}, /* TxPDO 001 mapping */
+   {0x1a01, 2, slave_5_pdo_entries + 2}, /* TxPDO 002 mapping */
+};
+
+ec_sync_info_t slave_5_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 2, slave_5_pdos + 0, EC_WD_DISABLE},
+   {0xff}
+};
+
+/* Slave 6, "EL6731-0010"
+ * Vendor ID:       0x00000002
+ * Product code:    0x1a4b3052
+ * Revision number: 0x0011000a
+ */
+
+ec_sync_info_t slave_6_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+};
+
+
+/* Slave 7, "EL6601"
+ * Vendor ID:       0x00000002
+ * Product code:    0x19c93052
+ * Revision number: 0x00110000
+ */
+/*
+ec_sync_info_t slave_7_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {0xff}
+};
+*/
+
+/* Master 0, Slave 7, "EL3602"
+ * Vendor ID:       0x00000002
+ * Product code:    0x0e123052
+ * Revision number: 0x00100000
+ */
+ec_pdo_entry_info_t slave_7_pdo_entries[] = {
+   {0x6000, 0x01, 1}, /* Underrange */
+   {0x6000, 0x02, 1}, /* Overrange */
+   {0x6000, 0x03, 2}, /* Limit 1 */
+   {0x6000, 0x05, 2}, /* Limit 2 */
+   {0x6000, 0x07, 1}, /* Error */
+   {0x0000, 0x00, 7}, /* Gap */
+   {0x1800, 0x07, 1},
+   {0x1800, 0x09, 1},
+   {0x6000, 0x11, 32}, /* Value */
+   {0x6010, 0x01, 1}, /* Underrange */
+   {0x6010, 0x02, 1}, /* Overrange */
+   {0x6010, 0x03, 2}, /* Limit 1 */
+   {0x6010, 0x05, 2}, /* Limit 2 */
+   {0x6010, 0x07, 1}, /* Error */
+   {0x0000, 0x00, 7}, /* Gap */
+   {0x1801, 0x07, 1},
+   {0x1801, 0x09, 1},
+   {0x6010, 0x11, 32}, /* Value */
+};
+
+ec_pdo_info_t slave_7_pdos[] = {
+   {0x1a00, 9, slave_7_pdo_entries + 0}, /* AI TxPDO-Map Inputs Ch.1 */
+   {0x1a01, 9, slave_7_pdo_entries + 9}, /* AI TxPDO-Map Inputs Ch.2 */
+};
+
+ec_sync_info_t slave_7_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 2, slave_7_pdos + 0, EC_WD_DISABLE},
+   {0xff}
+};
+
+/* Master 0, Slave 8, "EL5151"
+ * Vendor ID:       0x00000002
+ * Product code:    0x141f3052
+ * Revision number: 0x00130000
+ */
+
+ec_pdo_entry_info_t slave_8_pdo_entries[] = {
+   {0x6000, 0x01, 1},
+   {0x6000, 0x02, 1},
+   {0x6000, 0x03, 1},
+   {0x0000, 0x00, 4}, /* Gap */
+   {0x6000, 0x08, 1},
+   {0x6000, 0x09, 1},
+   {0x6000, 0x0a, 1},
+   {0x6000, 0x0b, 1},
+   {0x0000, 0x00, 1}, /* Gap */
+   {0x6000, 0x0d, 1},
+   {0x1c32, 0x20, 1},
+   {0x0000, 0x00, 1}, /* Gap */
+   {0x1800, 0x09, 1},
+   {0x6000, 0x11, 32},
+   {0x6000, 0x12, 32},
+   {0x6000, 0x14, 32},
+};
+
+ec_pdo_info_t slave_8_pdos[] = {
+   {0x0000, 0, NULL},
+   {0x1a00, 15, slave_8_pdo_entries + 0},
+   {0x1a02, 1, slave_8_pdo_entries + 15},
+};
+
+ec_sync_info_t slave_8_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 1, slave_8_pdos + 0, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 2, slave_8_pdos + 1, EC_WD_DISABLE},
+   {0xff}
+};
+
+
+/*****************************************************************************/
+
+
+void rt_check_domain_state(void)
+{
+    ec_domain_state_t ds;
+
+    if (rt_fd>=0)
+      {
+          ecrt_rtdm_domain_state(rt_fd,&ds);  
+      }
+
+    if (ds.working_counter != domain1_state.working_counter)
+     {
+        rt_printf("Domain1: WC %u.\n", ds.working_counter);
+     }
+    if (ds.wc_state != domain1_state.wc_state)
+     {
+    	rt_printf("Domain1: State %u.\n", ds.wc_state);
+     }
+
+    domain1_state = ds;
+}
+
+void rt_check_master_state(void)
+{
+    ec_master_state_t ms;
+
+    if (rt_fd>=0)
+      {
+          ecrt_rtdm_master_state(rt_fd,&ms);
+      }
+
+    if (ms.slaves_responding != master_state.slaves_responding)
+    {
+        rt_printf("%u slave(s).\n", ms.slaves_responding);
+    }
+    if (ms.al_states != master_state.al_states)
+    {
+        rt_printf("AL states: 0x%02X.\n", ms.al_states);
+    }
+    if (ms.link_up != master_state.link_up)
+    {
+        rt_printf("Link is %s.\n", ms.link_up ? "up" : "down");
+    }
+    master_state = ms;
+}
+
+
+
+
+void rt_sync()
+{
+
+  uint64_t now_ns;
+  now_ns =  rt_get_real_time_ns();
+
+
+  if (rt_fd>=0)
+  {
+      ecrt_rtdm_master_application_time(rt_fd, &now_ns);
+  }
+
+  if (sync_ref_counter) {
+     sync_ref_counter--;
+  } else {
+     sync_ref_counter = 9;
+     if (rt_fd>=0)
+     {
+         ecrt_rtdm_master_sync_reference_clock(rt_fd);
+     }
+  }
+  if (rt_fd>=0)
+  {
+      ecrt_rtdm_master_sync_slave_clocks(rt_fd) ;
+  }
+}
+
+/*****************************************************************************/
+
+/**********************************************************/
+void cleanup_all(void)
+{
+    run = 0;   
+}
+
+void catch_signal(int sig)
+{
+    cleanup_all();    
+}
+
+
+
+
+
+void my_cyclic(void)
+{
+
+    int counter = 0;
+    int divcounter = 0;
+    int divider = 10;
+    int period;
+
+    rt_set_periodic_mode();
+    period = (int) nano2count((RTIME)cycle*1000);
+    start_rt_timer(period);
+    rt_make_hard_real_time();
+
+
+    while(1) {
+ 
+        rt_task_wait_period();
+
+
+        counter++;
+        if (counter>60000) {
+            run=0;
+        }
+
+        if(run ==  0) {
+            rt_make_soft_real_time();
+            return;
+        }
+
+        
+        // receive ethercat
+        ecrt_rtdm_master_recieve(rt_fd);
+        ecrt_rtdm_domain_process(rt_fd);
+        
+        rt_check_domain_state();
+        
+        if (divcounter ==0)
+            {
+                divcounter=divider;
+                rt_check_master_state();
+            }
+        divcounter--;
+        if ((counter % 200)==0)
+            {
+                blink = !blink;
+                
+            }
+      
+
+        EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0x0 : 0x0F);
+        EC_WRITE_U16(domain1_pd + off_ana_out0, blink ? 0x0: 0xfff);
+        
+        //sync DC
+        rt_sync();
+        
+        // send process data
+        ecrt_rtdm_domain_queque(rt_fd);
+        ecrt_rtdm_master_send(rt_fd);   
+    }
+    return;
+}
+
+
+
+int main(int argc, char *argv[])
+{
+    ec_slave_config_t *sc;
+    int rtstatus;
+
+
+
+    signal(SIGTERM, catch_signal);
+    signal(SIGINT, catch_signal);
+    signal(SIGHUP, catch_signal);
+
+    mlockall(MCL_CURRENT|MCL_FUTURE);
+
+
+
+    MstrAttach.masterindex = 0;
+    
+    printf("request master\n");
+    master = ecrt_request_master(MstrAttach.masterindex);
+    if (!master)
+        return -1;
+    
+    
+    domain1 = ecrt_master_create_domain(master);
+    if (!domain1)
+        return -1;
+    
+    
+#ifdef CONFIGURE_PDOS
+
+    printf("Configuring PDOs...\n");
+    
+    printf("Get Configuring el2004...\n");
+    sc_dig_out_01 = ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2004);
+    if (!sc_dig_out_01) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+    }
+    
+    printf("Configuring EL2004...\n");
+    if (ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_1_syncs))
+        {
+            fprintf(stderr, "Failed to configure PDOs.\n");
+            return -1;
+        }
+    
+    printf("Get Configuring el2004...\n");
+    sc_dig_out_02 = ecrt_master_slave_config(master, DigOutSlave02_Pos, Beckhoff_EL2004);
+    if (!sc_dig_out_02) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+    }
+
+    printf("Configuring EL2004...\n");
+    if (ecrt_slave_config_pdos(sc_dig_out_02, EC_END, slave_2_syncs)) {
+        fprintf(stderr, "Failed to configure PDOs.\n");
+        return -1;
+    }
+    
+    printf("Get Configuring el1014...\n");
+    sc_dig_in_01 = ecrt_master_slave_config(master, DigInSlave01_Pos, Beckhoff_EL1014);
+    if (!sc_dig_in_01) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+    }
+    
+    printf("Configuring EL1014...\n");
+    if (ecrt_slave_config_pdos(sc_dig_in_01, EC_END, slave_3_syncs)) {
+        fprintf(stderr, "Failed to configure PDOs.\n");
+        return -1;
+    }
+
+    printf("Get Configuring EL4132...\n");
+    sc_ana_out_01 = ecrt_master_slave_config(master, AnaOutSlave01_Pos, Beckhoff_EL4132);
+    if (!sc_ana_out_01) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+    }
+
+    printf("Configuring EL4132...\n");
+    if (ecrt_slave_config_pdos(sc_ana_out_01, EC_END, slave_4_syncs)) {
+        fprintf(stderr, "Failed to configure PDOs.\n");
+        return -1;
+    }
+
+    printf("Get Configuring EL3102...\n");
+    sc_ana_in_01 = ecrt_master_slave_config(master, AnaInSlave01_Pos, Beckhoff_EL3102);
+    if (!sc_ana_in_01) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+    }
+
+    printf("Configuring EL3102...\n");
+    if (ecrt_slave_config_pdos(sc_ana_in_01, EC_END, slave_5_syncs)) {
+        fprintf(stderr, "Failed to configure PDOs.\n");
+        return -1;
+    }
+
+    printf("Get Configuring EL3602...\n");
+	sc_ana_in_02 = ecrt_master_slave_config(master, AnaInSlave02_Pos, Beckhoff_EL3602);
+	if (!sc_ana_in_02) {
+        fprintf(stderr, "Failed to get slave configuration.\n");
+        return -1;
+	}
+    
+
+	printf("Configuring EL3602...\n");
+	if (ecrt_slave_config_pdos(sc_ana_in_02, EC_END, slave_7_syncs)) {
+        fprintf(stderr, "Failed to configure PDOs.\n");
+        return -1;
+	}
+    
+#endif
+    
+    // Create configuration for bus coupler
+    sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
+    if (!sc)
+        return -1;
+    
+#ifdef CONFIGURE_PDOS
+    if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
+        fprintf(stderr, "PDO entry registration failed!\n");
+        return -1;
+    }
+#endif
+
+
+        
+    sprintf(&rt_dev_file[0],"%s%u",EC_RTDM_DEV_FILE_NAME,0);
+    
+    
+    rt_fd = rt_dev_open( &rt_dev_file[0], 0);
+    if (rt_fd < 0) {
+        printf("can't open %s\n", &rt_dev_file[0]);
+        return -1;
+    }
+    
+    MstrAttach.domainindex = ecrt_domain_index(domain1);
+    
+    // attach the master over rtdm driver
+    rtstatus=ecrt_rtdm_master_attach(rt_fd, &MstrAttach);
+    if (rtstatus < 0)
+        {
+            printf("cannot attach to master over rtdm\n");
+            return -1;
+        }
+    
+    printf("Activating master...\n");
+    if (ecrt_master_activate(master))
+        return -1;
+    
+    if (!(domain1_pd = ecrt_domain_data(domain1))) {
+        return -1;
+    }
+    fprintf(stderr, "domain1_pd:  0x%.6x\n", (unsigned int)domain1_pd);
+    
+    
+    
+    run=1;
+
+    /* Create cyclic RT-thread */
+    struct sched_param param;
+    param.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
+    if( sched_setscheduler( 0, SCHED_FIFO, &param ) == -1 ) {
+      puts("ERROR IN SETTING THE SCHEDULER");
+      perror("errno");
+      exit(1);
+    }
+
+    task = rt_task_init( nam2num("ec_rtai_rtdm_example"), priority, stack_size, msg_size);
+
+    
+    my_cyclic();
+
+    rt_task_delete(task);
+
+    if (rt_fd >= 0)
+        {
+            printf("closing rt device %s\n", &rt_dev_file[0]);
+            
+            rt_dev_close(rt_fd);
+            
+        }
+    
+    printf("End of Program\n");
+    ecrt_release_master(master);
+
+    return 0;
+
+}
+
--- a/rtdm/Kbuild.in	Mon Mar 21 21:07:38 2011 +0100
+++ b/rtdm/Kbuild.in	Tue Mar 22 09:15:19 2011 +0100
@@ -36,6 +36,7 @@
 ec_rtdm-objs := \
         module.o
 
+ifeq (@ENABLE_XENOMAI@,1)
 
 ### Xenomai directory, xeno-config and library directory ###########
 XENO_DIR          := @XENOMAI_DIR@
@@ -48,7 +49,27 @@
 		hg id -i $(src)/.. 2>/dev/null || echo "unknown"; \
 	fi)
 
-CFLAGS_module.o := -DREV=$(REV) -I$(XENO_DIR)/include 
+CFLAGS_module.o := -DREV=$(REV) -I$(XENO_DIR)/include -DENABLE_XENOMAI
+
+endif
+
+ifeq (@ENABLE_RTAI@,1)
+
+### RTAI directory, rtai_config and library directory ###########
+RTAI_DIR          := @RTAI_DIR@
+RTAI_CONFIG       := $(RTAI_DIR)/bin/rtai-config
+RTAI_LIB_DIR      := $(shell $(RTAI_CONFIG) --library-dir)
+
+REV := $(shell if test -s $(src)/../revision; then \
+		cat $(src)/../revision; \
+	else \
+		hg id -i $(src)/.. 2>/dev/null || echo "unknown"; \
+	fi)
+
+CFLAGS_module.o := -DREV=$(REV) -I$(RTAI_DIR)/include -DENABLE_RTAI
+
+endif
+
 
 KBUILD_EXTRA_SYMBOLS := \
         @abs_top_builddir@/$(LINUX_SYMVERS) \
--- a/rtdm/module.c	Mon Mar 21 21:07:38 2011 +0100
+++ b/rtdm/module.c	Tue Mar 22 09:15:19 2011 +0100
@@ -30,15 +30,44 @@
 
 #include <linux/module.h>
 #include <linux/mman.h>
-#include <rtdm/rtdm_driver.h>
+
+
+#ifdef ENABLE_XENOMAI
 #include <native/task.h>
 #include <native/sem.h>
 #include <native/mutex.h>
 #include <native/timer.h>
+#endif
+
+#ifdef ENABLE_RTAI
+#include <rtai_sched.h>
+#include <rtai_sem.h>
+#endif
+
+
+#include <rtdm/rtdm_driver.h>
 
 #include "../include/ecrt.h"
 #include "../include/ec_rtdm.h"
 
+#ifdef ENABLE_XENOMAI
+#define my_mutex_create(X,Y)  rt_mutex_create(X, Y)
+#define my_mutex_acquire(X,Y) rt_mutex_acquire(X,Y)
+#define my_mutex_release(X)   rt_mutex_release(X)
+#define my_mutex_delete(X)    rt_mutex_delete(X)
+#endif
+
+#ifdef ENABLE_RTAI
+#define my_mutex_create(X,Y)  rt_sem_init(X, 1)
+#define my_mutex_acquire(X,Y) rt_sem_wait(X)
+#define my_mutex_release(X)   rt_sem_signal(X)
+#define my_mutex_delete(X)    rt_sem_delete(X)
+#define TM_INFINITE
+#endif
+
+
+
+
 #define EC_RTDM_MAX_MASTERS 5 /**< Maximum number of masters. */
 
 #define EC_RTDM_GINFO(fmt, args...) \
@@ -66,8 +95,13 @@
 typedef struct _EC_RTDM_DRV_STRUCT {
     unsigned int	    isattached;
     ec_master_t *	    master;
-    ec_domain_t *	    domain;				   
-    RT_MUTEX                masterlock;
+    ec_domain_t *	    domain;	
+#ifdef ENABLE_XENOMAI			   
+    RT_MUTEX           masterlock;
+#endif
+#ifdef ENABLE_RTAI
+    SEM                masterlock;
+#endif
     unsigned int	    sendcnt;
     unsigned int	    reccnt;
     unsigned int	    sendcntlv;
@@ -139,9 +173,9 @@
     pdrvstruc = (EC_RTDM_DRV_STRUCT*)cb_data;
     if (pdrvstruc->master)
         {
-            rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+            my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
             ecrt_master_send_ext(pdrvstruc->master);
-            rt_mutex_release(&pdrvstruc->masterlock);
+            my_mutex_release(&pdrvstruc->masterlock);
       }
 }
 
@@ -154,9 +188,9 @@
     pdrvstruc = (EC_RTDM_DRV_STRUCT*)cb_data;
     if (pdrvstruc->master)
       {
-          rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+          my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
           ecrt_master_receive(pdrvstruc->master);
-          rt_mutex_release(&pdrvstruc->masterlock);      
+          my_mutex_release(&pdrvstruc->masterlock);      
     }
 }
 
@@ -171,7 +205,7 @@
           EC_RTDM_INFO(pdrvstruc->masterno,"reseting callbacks!\n");
           ecrt_master_callbacks(pdrvstruc->master,NULL,NULL,NULL);
           EC_RTDM_INFO(pdrvstruc->masterno,"deleting mutex!\n");
-          rt_mutex_delete(&pdrvstruc->masterlock);
+          my_mutex_delete(&pdrvstruc->masterlock);
           pdrvstruc->master = NULL;
           pdrvstruc->isattached=0;
           EC_RTDM_INFO(pdrvstruc->masterno,"master detach done!\n");
@@ -278,11 +312,11 @@
             }
 		if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 
                 ecrt_master_state(pdrvstruc->master, &ms);
                 
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
                 
             }
         if  (rtdm_rw_user_ok(user_info, arg, sizeof(ms)))
@@ -304,11 +338,11 @@
             }
 	   	if (pdrvstruc->domain)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 
                 ecrt_domain_state(pdrvstruc->domain, &ds);
                 
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
             }
         if  (rtdm_rw_user_ok(user_info, arg, sizeof(ds)))
             {
@@ -326,10 +360,10 @@
             {
                 if (pdrvstruc->master)
                     {
-                        rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                        my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                         ecrt_master_receive(pdrvstruc->master);
                         pdrvstruc->reccnt++;
-                        rt_mutex_release(&pdrvstruc->masterlock);
+                        my_mutex_release(&pdrvstruc->masterlock);
                     }
             }
     }
@@ -338,9 +372,9 @@
     {	       
         if (pdrvstruc->isattached)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 ecrt_domain_process(pdrvstruc->domain);
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
             }
     }
     break;
@@ -351,10 +385,10 @@
             {
                 if (pdrvstruc->master)
                     {
-                        rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                        my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                         ecrt_master_send(pdrvstruc->master);
                         pdrvstruc->sendcnt++;
-                        rt_mutex_release(&pdrvstruc->masterlock);
+                        my_mutex_release(&pdrvstruc->masterlock);
                     }
             }
     }
@@ -363,9 +397,9 @@
     {	       
         if (pdrvstruc->isattached)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 ecrt_domain_queue(pdrvstruc->domain);
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
             }
     }
     break;
@@ -385,10 +419,10 @@
             
         if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 
                 ecrt_master_application_time(pdrvstruc->master, app_time);
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
                 
             }
     }
@@ -401,11 +435,11 @@
             }
         if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 
                 ecrt_master_sync_reference_clock(pdrvstruc->master);
                 
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
                 
             }
     }
@@ -418,11 +452,11 @@
             }
         if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 
                 ecrt_master_sync_slave_clocks(pdrvstruc->master);
                 
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
                 
             }
     }
@@ -435,9 +469,9 @@
             }
         if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 ecrt_master_sync_monitor_queue(pdrvstruc->master);
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
             }
     }
     break;
@@ -450,9 +484,9 @@
             }
         if (pdrvstruc->master)
             {
-                rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
+                my_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
                 ret = ecrt_master_sync_monitor_process(pdrvstruc->master);
-                rt_mutex_release(&pdrvstruc->masterlock);
+                my_mutex_release(&pdrvstruc->masterlock);
                 if (rtdm_safe_copy_to_user(user_info, arg, &ret, sizeof(ret)))
                     {
                         EC_RTDM_ERR(pdrvstruc->masterno,"copy to user param failed!\n");
@@ -525,7 +559,7 @@
                                 // set device name
                                 snprintf(&pdrvstruc->mutexname[0],sizeof(pdrvstruc->mutexname)-1,"ETHrtdmLOCK%d",pdrvstruc->masterno);
                                 EC_RTDM_INFO(pdrvstruc->masterno,"Creating Master mutex %s!\n",&pdrvstruc->mutexname[0]);
-                                rt_mutex_create(&pdrvstruc->masterlock,&pdrvstruc->mutexname[0]);
+                                my_mutex_create(&pdrvstruc->masterlock,&pdrvstruc->mutexname[0]);
                                 //ecrt_release_master(mstr);
                                 ecrt_master_callbacks(pdrvstruc->master, send_callback, receive_callback, pdrvstruc);
                                 EC_RTDM_INFO(pdrvstruc->masterno,"MSTR ATTACH done domain=%u!\n",(unsigned int)pdrvstruc->domain);