First working RTDM Version,

to be reviewed,
further testing and optimation needed

Thanks to Moehwald GmbH B.Benner for Basic Code
This commit is contained in:
Andreas Stewering-Bone 2011-03-16 22:12:23 +01:00
parent 68709df653
commit 76ff88c43c
19 changed files with 7441 additions and 3 deletions

View File

@ -35,4 +35,8 @@ ifeq (@ENABLE_TTY@,1)
obj-m += tty/
endif
ifeq (@ENABLE_RTDM@,1)
obj-m += rtdm/
endif
#------------------------------------------------------------------------------

View File

@ -47,6 +47,10 @@ if ENABLE_TTY
SUBDIRS += tty
endif
if ENABLE_RTDM
SUBDIRS += rtdm
endif
# userspace example depends on lib/
SUBDIRS += examples
@ -59,6 +63,7 @@ DIST_SUBDIRS = \
master \
script \
tool \
rtdm \
tty
noinst_HEADERS = \

View File

@ -365,7 +365,7 @@ AC_SUBST(KERNEL_R8169,[$kernel_r8169])
AC_ARG_WITH([rtai-dir],
AC_HELP_STRING(
[--with-rtai-dir=<DIR>],
[RTAI path (only for RTAI examples)]
[RTAI path (only for RTDM Interface or RTAI examples)]
),
[
rtaidir=[$withval]
@ -388,6 +388,67 @@ fi
AC_SUBST(RTAI_DIR,[$rtaidir])
#------------------------------------------------------------------------------
# Xenomai path (optional)
#------------------------------------------------------------------------------
AC_ARG_WITH([xenomai-dir],
AC_HELP_STRING(
[--with-xenomai-dir=<DIR>],
[Xenomai path (only for RTDM Interface or Xenomai examples)]
),
[
xenomaidir=[$withval]
],
[
xenomaidir=""
]
)
AC_MSG_CHECKING([for Xenomai path])
if test -z "${xenomaidir}"; then
AC_MSG_RESULT([not specified.])
else
if test \! -r ${xenomaidir}/include/xeno_config.h; then
AC_MSG_ERROR([no Xenomai installation found in ${xenomaidir}!])
fi
AC_MSG_RESULT([$xenomaidir])
fi
AC_SUBST(XENOMAI_DIR,[$xenomaidir])
#------------------------------------------------------------------------------
# RTDM Interface (optional)
#------------------------------------------------------------------------------
AC_ARG_ENABLE([rtdm],
AC_HELP_STRING(
[--enable-rtdm],
[Enable RTDM Interface, depends on RTAI or Xenomai]
),
[
case "${enableval}" in
yes) rtdm=1
;;
no) rtdm=0
;;
*) AC_MSG_ERROR([Invalid value for --enable-rtdm])
;;
esac
],
[rtdm=0]
)
if test "x${rtdm}" = "x1"; then
AC_DEFINE([EC_RTDM], [1], [RTDM interfaces enabled])
fi
AM_CONDITIONAL(ENABLE_RTDM, test "x$rtdm" = "x1")
AC_SUBST(ENABLE_RTDM,[$rtdm])
#------------------------------------------------------------------------------
# Debug interface
#------------------------------------------------------------------------------
@ -626,6 +687,7 @@ AC_CONFIG_FILES([
examples/tty/Kbuild
examples/tty/Makefile
examples/user/Makefile
examples/xenomai/Makefile
include/Makefile
lib/Makefile
m4/Makefile
@ -638,6 +700,8 @@ AC_CONFIG_FILES([
tool/Makefile
tty/Kbuild
tty/Makefile
rtdm/Makefile
rtdm/Kbuild
])
AC_OUTPUT

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -65,6 +65,8 @@ noinst_HEADERS = \
8139too-2.6.33-orig.c \
8139too-2.6.34-ethercat.c \
8139too-2.6.34-orig.c \
8139too-2.6.35-ethercat.c \
8139too-2.6.35-orig.c \
e100-2.6.20-ethercat.c \
e100-2.6.20-orig.c \
e100-2.6.24-ethercat.c \

View File

@ -35,4 +35,5 @@ ifeq (@ENABLE_TTY@,1)
obj-m += tty/
endif
#------------------------------------------------------------------------------

View File

@ -40,12 +40,18 @@ SUBDIRS += \
tty
endif
if ENABLE_RTDM
SUBDIRS += \
xenomai
endif
DIST_SUBDIRS = \
dc_rtai \
dc_user \
mini \
rtai \
tty \
xenomai \
user
EXTRA_DIST = \

View File

@ -0,0 +1,39 @@
#------------------------------------------------------------------------------
#
# $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_xenomai_example
ec_xenomai_example_SOURCES = main.c
ec_xenomai_example_CFLAGS = -I/opt/etherlab/xenomai/include -D_GNU_SOURCE -D_REENTRANT -Wall -pipe -D__XENO__ -I$(top_srcdir)/include
ec_xenomai_example_LDFLAGS = -lrtdm -L$(top_builddir)/lib/.libs -lethercat -lnative -L/opt/etherlab/xenomai/lib -lxenomai -lpthread
#------------------------------------------------------------------------------

918
examples/xenomai/main.c Normal file
View File

@ -0,0 +1,918 @@
/******************************************************************************
*
* $Id$
*
* usermdtest.c Copyright (C) 2009-2010 Moehwald GmbH B.Benner
* 2011 IgH Andreas Stewering-Bone
*
* This file is part of ethercatrtdm interface to IgH EtherCAT master
*
* The Moehwald ethercatrtdm interface 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 <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
#include <rtdm/rtdm.h>
#include <native/task.h>
#include <native/sem.h>
#include <native/mutex.h>
#include <native/timer.h>
#include <pthread.h>
/****************************************************************************/
#include "../../include/ecrt.h"
#include "../../include/ec_rtdm.h"
RT_TASK my_task;
int rt_fd = -1;
int run=0;
struct timeval tv;
unsigned int sync_ref_counter = 0;
CstructMstrAttach MstrAttach;
/****************************************************************************/
// Application parameters
#define FREQUENCY 1000
#define PRIORITY 1
// Optional features
#define CONFIGURE_PDOS 1
//#define SDO_ACCESS 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 ec_slave_config_t *sc_ana_in = NULL;
//static ec_slave_config_state_t sc_ana_in_state = {};
// Timer
static unsigned int sig_alarms = 0;
//static unsigned int user_alarms = 0;
/****************************************************************************/
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_out4 = 0;
static unsigned int off_dig_out5 = 0;
static unsigned int off_dig_out6 = 0;
static unsigned int off_dig_out7 = 0;
static unsigned int off_dig_in0 = 0;
static unsigned int off_dig_in1 = 0;
static unsigned int off_dig_in2 = 0;
static unsigned int off_dig_in3 = 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;
static unsigned int off_ana_in2_status = 0;
static unsigned int off_ana_in2_value = 0;
static unsigned int off_ana_in3_status = 0;
static unsigned int off_ana_in3_value = 0;
//static unsigned int off_dp_slave;
// 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 counter = 0;
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_dpslv_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}
};
/*****************************************************************************/
#if SDO_ACCESS
static ec_sdo_request_t *sdo;
uint8_t *sdo_adr = NULL;
#endif
void rt_check_domain_state(void)
{
ec_domain_state_t ds;
unsigned int printed=0;
if (rt_fd>=0)
{
ecrt_rtdm_domain_state(rt_fd,&ds);
}
/* if (ds.working_counter != domain1_state.working_counter)
{
printf("Domain1: WC %u.\n", ds.working_counter);
printed=1;
}
if (ds.wc_state != domain1_state.wc_state)
{
printf("Domain1: State %u.\n", ds.wc_state);
printed=1;
}
if (printed)
{
int ret;
// return to realtime mode after print
ret = rt_task_set_mode(0, T_PRIMARY, NULL);
if (ret)
{
printf("error while rt_task_set_mode, code %d\n",ret);
}
}
*/
domain1_state = ds;
}
void rt_check_master_state(void)
{
ec_master_state_t ms;
unsigned int printed=0;
if (rt_fd>=0)
{
ecrt_rtdm_master_state(rt_fd,&ms);
}
/*
if (ms.slaves_responding != master_state.slaves_responding)
{
printf("%u slave(s).\n", ms.slaves_responding);
printed=1;
}
if (ms.al_states != master_state.al_states)
{
printf("AL states: 0x%02X.\n", ms.al_states);
printed=1;
}
if (ms.link_up != master_state.link_up)
{
printf("Link is %s.\n", ms.link_up ? "up" : "down");
printed=1;
}
if (printed)
{
int ret;
// return to realtime mode after print
ret = rt_task_set_mode(0, T_PRIMARY, NULL);
if (ret)
{
printf("error while rt_task_set_mode, code %d\n",ret);
}
}*/
master_state = ms;
}
void rt_receive()
{
if (rt_fd>=0)
{
ecrt_rtdm_master_recieve(rt_fd);
}
}
void rt_send()
{
if (rt_fd>=0)
{
ecrt_rtdm_master_send(rt_fd);
}
}
void rt_sync()
{
RTIME now;
int ret;
now = rt_timer_read();
//now -= 946684800ULL * 1000000000ULL;
printf("Write Sync Time %i\n",now);
ret = rt_task_set_mode(0, T_PRIMARY, NULL);
if (ret)
{
printf("error while rt_task_set_mode, code %d\n",ret);
}
if (rt_fd>=0)
{
ecrt_rtdm_master_application_time(rt_fd, &now);
}
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) ;
}
}
/*****************************************************************************/
#if SDO_ACCESS
void read_sdo(void)
{
switch (ecrt_sdo_request_state(sdo))
{
case EC_REQUEST_UNUSED: // request was not used yet
ecrt_sdo_request_read(sdo); // trigger first read
break;
case EC_REQUEST_BUSY:
fprintf(stderr, "Still busy...\n");
break;
case EC_REQUEST_SUCCESS:
fprintf(stderr, "SDO value: 0x%04X\n",
EC_READ_U16(ecrt_sdo_request_data(sdo)));
ecrt_sdo_request_read(sdo); // trigger next read
break;
case EC_REQUEST_ERROR:
fprintf(stderr, "Failed to read SDO!\n");
ecrt_sdo_request_read(sdo); // retry reading
break;
}
}
void PrintSDOState(void)
{
switch (ecrt_sdo_request_state(sdo))
{
case EC_REQUEST_UNUSED: // request was not used yet
fprintf(stderr, "SDO State: EC_REQUEST_UNUSED\n"); // trigger first read
break;
case EC_REQUEST_BUSY:
fprintf(stderr, "SDO State: EC_REQUEST_BUSY\n");
break;
case EC_REQUEST_SUCCESS:
fprintf(stderr, "SDO State: EC_REQUEST_SUCCESS\n");
break;
case EC_REQUEST_ERROR:
fprintf(stderr, "SDO State: EC_REQUEST_ERROR\n");
break;
default:
fprintf(stderr, "SDO State: undefined\n");
break;
}
}
#endif
static int cyccount=0;
/****************************************************************************/
void signal_handler(int signum) {
switch (signum) {
case SIGALRM:
sig_alarms++;
break;
}
}
/**********************************************************/
/* REAL TIME TASK */
/**********************************************************/
void my_task_proc(void *arg)
{
int counter = 0;
int divcounter = 0;
int divider = 10;
int ret;
RTIME periodns;
float period;
period=1E-4;
periodns=(RTIME)(((double)period * 1E9) + 0.4999999);
rt_task_set_periodic(NULL, TM_NOW, periodns);
run=1;
ret = rt_task_set_mode(0, T_PRIMARY, NULL);
if (ret) {
printf("error while rt_task_set_mode, code %d\n",ret);
return;
}
while (run) {
rt_task_wait_period(NULL);
counter++;
if (counter>600000) {
run=0;
return;
}
// receive ethercat
rt_receive();
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
rt_send();
}
}
/**********************************************************/
/* CLEANING UP */
/**********************************************************/
void cleanup_all(void)
{
printf("delete my_task\n");
rt_task_delete(&my_task);
if (rt_fd >= 0) {
printf("closing rt device %s\n", &rt_dev_file[0]);
rt_dev_close(rt_fd);
}
}
/****************************************************************************/
void catch_signal(int sig) {
cleanup_all();
printf("exit\n");
exit(0);
return;
}
/****************************************************************************/
int main(int argc, char **argv)
{
ec_slave_config_t *sc;
int rtstatus;
mlockall(MCL_CURRENT | MCL_FUTURE);
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
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;
}
// DP Slave Parameter Set
/* printf( "Creating SDO requests...\n");
if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in_02, 0x8000, 0x06, 1))) {
fprintf(stderr, "Failed to create SDO request.\n");
return -1;
}
ecrt_sdo_request_timeout(sdo, 500); // ms
EC_WRITE_U8(ecrt_sdo_request_data(sdo), 00);
PrintSDOState();
ecrt_sdo_request_write(sdo);
PrintSDOState();
*/
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);
int ret;
run=1;
ret = rt_task_create(&my_task,"my_task",0,80,T_FPU);
printf("starting my_task\n");
ret = rt_task_start(&my_task,&my_task_proc,NULL);
while (run)
{
sched_yield();
}
rt_task_delete(&my_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;
}
/****************************************************************************/

View File

@ -33,6 +33,7 @@
include_HEADERS = \
ecrt.h \
ectty.h
ectty.h \
ec_rtdm.h
#------------------------------------------------------------------------------

72
include/ec_rtdm.h Normal file
View File

@ -0,0 +1,72 @@
/******************************************************************************
*
* $Id$
*
* ec_rtdm.h Copyright (C) 2009-2010 Moehwald GmbH B.Benner
* 2011 IgH Andreas Stewering-Bone
*
* This file is used for Prisma RT to interface to EtherCAT devices
*
* This file is part of ethercatrtdm interface to IgH EtherCAT master
*
* The Moehwald ethercatrtdm interface 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 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.
*
*****************************************************************************/
#ifndef __ECRT_RTDM_H
#define __ECRT_RTDM_H
//
// Basefilename of RTDM device
//
#define EC_RTDM_DEV_FILE_NAME "ec_rtdm"
//
// IOCTRL Values for RTDM_EXTENSION
//
// Realtime IOCTRL function
#define EC_RTDM_MSTRATTACH 1 // attach to a running master
#define EC_RTDM_MSTRGETMUTNAME 2 // return the mutexname
#define EC_RTDM_MSTRRECEIVE 3 // call the master receive
#define EC_RTDM_MSTRSEND 4 // call the master send
#define EC_RTDM_DOMAINSTATE 5 // get domain state
#define EC_RTDM_MASTERSTATE 6 // get master state
#define EC_RTDM_MASTER_APP_TIME 7
#define EC_RTDM_SYNC_REF_CLOCK 8
#define EC_RTDM_SYNC_SLAVE_CLOCK 9
typedef struct _CstructMstrAttach
{
unsigned int domainindex;
unsigned int masterindex;
} CstructMstrAttach;
#define ecrt_rtdm_master_attach(X,Y) rt_dev_ioctl(X, EC_RTDM_MSTRATTACH, Y)
#define ecrt_rtdm_master_recieve(X) rt_dev_ioctl(X, EC_RTDM_MSTRRECEIVE)
#define ecrt_rtdm_master_send(X) rt_dev_ioctl(X, EC_RTDM_MSTRSEND)
#define ecrt_rtdm_domain_state(X,Y) rt_dev_ioctl(X, EC_RTDM_DOMAINSTATE, Y)
#define ecrt_rtdm_master_state(X,Y) rt_dev_ioctl(X, EC_RTDM_MASTERSTATE, Y)
#define ecrt_rtdm_master_application_time(X,Y) rt_dev_ioctl(X, EC_RTDM_MASTER_APP_TIME, Y)
#define ecrt_rtdm_master_sync_reference_clock(X) rt_dev_ioctl(X, EC_RTDM_SYNC_REF_CLOCK)
#define ecrt_rtdm_master_sync_slave_clocks(X) rt_dev_ioctl(X, EC_RTDM_SYNC_SLAVE_CLOCK);
#endif

View File

@ -455,6 +455,23 @@ void ecrt_release_master(
ec_master_t *master /**< EtherCAT master */
);
#ifdef __KERNEL__
/** Attach to a running master
*
* This function returns the master handle for the RTDM-Interface
*
* \return Pointer to the opened master, otherwise \a NULL.
*/
ec_master_t *ecrt_attach_master(
unsigned int master_index /**< Index of the master to request. */
);
#endif // #ifdef __KERNEL__
/******************************************************************************
* Master methods
*****************************************************************************/
@ -500,8 +517,38 @@ void ecrt_master_callbacks(
*/
);
/** Returns domain structure pointer
*
* This functions return the domain structure pointer for usage inside the
* RTDM-Interface.
*
* \return Pointer to the domain on success, else NULL.
*/
ec_domain_t *ecrt_master_find_domain(
ec_master_t *master,
unsigned int index);
#endif /* __KERNEL__ */
#ifndef __KERNEL__
/** Return the domain index of a given domain strucure
*
* Return the domain index of a given domain strucure. Usage inside of the
* RTDM Interface
*
* \return Index of the domain strucure
*
*/
unsigned int ecrt_domain_index(
ec_domain_t *domain
);
#endif // #ifndef __KERNEL__
/** Creates a new process data domain.
*
* For process data exchange, at least one process data domain is needed.
@ -515,6 +562,7 @@ ec_domain_t *ecrt_master_create_domain(
ec_master_t *master /**< EtherCAT master. */
);
/** Obtains a slave configuration.
*
* Creates a slave configuration object for the given \a alias and \a position

View File

@ -52,6 +52,13 @@ void ec_domain_clear(ec_domain_t *domain)
// nothing to do
}
/*****************************************************************************/
unsigned int ecrt_domain_index(ec_domain_t *domain)
{
return domain->index;
}
/*****************************************************************************/
int ecrt_domain_reg_pdo_entry_list(ec_domain_t *domain,

View File

@ -1636,6 +1636,20 @@ ec_domain_t *ec_master_find_domain(
EC_FIND_DOMAIN;
}
/** Wrapper Function for external usage
*
* \return Domain pointer, or \a NULL if not found.
*/
ec_domain_t *ecrt_master_find_domain(
ec_master_t *master, /**< EtherCAT master. */
unsigned int index /**< Domain index. */
)
{
return ec_master_find_domain(
master,
index);
}
/** Get a domain via its position in the list.
*
* Const version.
@ -2535,7 +2549,7 @@ EXPORT_SYMBOL(ecrt_master_sync_monitor_process);
EXPORT_SYMBOL(ecrt_master_write_idn);
EXPORT_SYMBOL(ecrt_master_read_idn);
EXPORT_SYMBOL(ecrt_master_reset);
EXPORT_SYMBOL(ecrt_master_find_domain);
/** \endcond */
/*****************************************************************************/

View File

@ -611,6 +611,37 @@ unsigned int ecrt_version_magic(void)
return ECRT_VERSION_MAGIC;
}
/** Return pointer to running master
/*****************************************************************************/
ec_master_t *ecrt_attach_master(unsigned int master_index)
{
ec_master_t *master = NULL;
EC_INFO("Requesting master %u...\n", master_index);
if (master_index >= master_count) {
EC_ERR("Invalid master index %u.\n", master_index);
return master;
}
master = &masters[master_index];
if (master->reserved)
{
// ok master is attached
EC_INFO("attaching Master %u!\n", master_index);
}
else
{
EC_ERR("No Master %u in use!\n", master_index);
master = NULL;
}
return master;
}
/*****************************************************************************/
/** Global request state type translation table.
@ -637,6 +668,7 @@ EXPORT_SYMBOL(ecdev_offer);
EXPORT_SYMBOL(ecrt_request_master);
EXPORT_SYMBOL(ecrt_release_master);
EXPORT_SYMBOL(ecrt_version_magic);
EXPORT_SYMBOL(ecrt_attach_master);
/** \endcond */

56
rtdm/Kbuild.in Normal file
View File

@ -0,0 +1,56 @@
#------------------------------------------------------------------------------
#
# $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.
#
# ---
#
# vi: syntax=make
#
#------------------------------------------------------------------------------
obj-m := ec_rtdm.o
ec_rtdm-objs := \
module.o
### Xenomai directory, xeno-config and library directory ###########
XENO_DIR := @XENOMAI_DIR@
XENO_CONFIG := $(XENO_DIR)/bin/xeno-config
XENO_LIB_DIR := $(shell $(XENO_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$(XENO_DIR)/include
KBUILD_EXTRA_SYMBOLS := \
@abs_top_builddir@/$(LINUX_SYMVERS) \
@abs_top_builddir@/master/$(LINUX_SYMVERS)
#------------------------------------------------------------------------------

51
rtdm/Makefile.am Normal file
View File

@ -0,0 +1,51 @@
#------------------------------------------------------------------------------
#
# $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.
#
#------------------------------------------------------------------------------
# using HEADERS to enable tags target
noinst_HEADERS = \
module.c
EXTRA_DIST = \
Kbuild.in
BUILT_SOURCES = \
Kbuild
modules:
$(MAKE) -C "$(LINUX_SOURCE_DIR)" M="@abs_srcdir@" modules
modules_install:
$(MAKE) -C "$(LINUX_SOURCE_DIR)" M="@abs_srcdir@" \
INSTALL_MOD_DIR="$(INSTALL_MOD_DIR)" modules_install
clean-local:
$(MAKE) -C "$(LINUX_SOURCE_DIR)" M="@abs_srcdir@" clean
#------------------------------------------------------------------------------

719
rtdm/module.c Normal file
View File

@ -0,0 +1,719 @@
/******************************************************************************
*
* $Id$
*
* ec_rtdm.c Copyright (C) 2009-2010 Moehwald GmbH B.Benner
* 2011 IgH Andreas Stewering-Bone
*
* This file is used for Prisma RT to interface to EtherCAT devices
*
* This file is part of ethercatrtdm interface to IgH EtherCAT master
*
* The Moehwald ec_rtdm interface 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 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 <linux/module.h>
#include <linux/mman.h>
#include <rtdm/rtdm_driver.h>
#include <native/task.h>
#include <native/sem.h>
#include <native/mutex.h>
#include <native/timer.h>
#include "../include/ecrt.h"
#include "../include/ec_rtdm.h"
#define EC_RTDM_MAX_MASTERS 5 /**< Maximum number of masters. */
#define EC_RTDM_GINFO(fmt, args...) \
printk(KERN_INFO "EtherCATrtdm: " fmt, ##args)
#define EC_RTDM_GERR(fmt, args...) \
printk(KERN_ERR "EtherCATrtdm ERROR: " fmt, ##args)
#define EC_RTDM_GWARN(fmt, args...) \
printk(KERN_WARNING "EtherCATrtdm WARNING: " fmt, ##args)
#define EC_RTDM_INFO(devno, fmt, args...) \
printk(KERN_INFO "EtherCATrtdm %u: " fmt, devno, ##args)
#define EC_RTDM_ERR(devno, fmt, args...) \
printk(KERN_ERR "EtherCATrtdm %u ERROR: " fmt, devno, ##args)
#define EC_RTDM_WARN(devno, fmt, args...) \
printk(KERN_WARNING "EtherCATrtdm %u WARNING: " fmt, devno, ##args)
typedef struct _EC_RTDM_DRV_STRUCT {
unsigned int isattached;
ec_master_t * master;
ec_domain_t * domain;
RT_MUTEX masterlock;
unsigned int sendcnt;
unsigned int reccnt;
unsigned int sendcntlv;
unsigned int reccntlv;
char mutexname[64];
unsigned int masterno;
} EC_RTDM_DRV_STRUCT;
static EC_RTDM_DRV_STRUCT ec_rtdm_masterintf[EC_RTDM_MAX_MASTERS];
/* import from ethercat */
ec_master_t *ecrt_attach_master(unsigned int master_index /**< Index of the master to request. */
);
// driver context struct: used for storing various information
typedef struct _EC_RTDM_DRV_CONTEXT {
int dev_id;
EC_RTDM_DRV_STRUCT* pdrvstruc;
} EC_RTDM_DRV_CONTEXT;
/**********************************************************/
/* Utilities */
/**********************************************************/
static int _atoi(const char* text)
{
char b;
int wd=-1;
int nfak=1;
wd=0;
while ((*text==' ') || (*text=='\t')) text++;
if (*text=='-')
{
nfak=-1;
text++;
}
if (*text=='+')
{
text++;
}
while (*text!=0)
{
b = *text;
if ( (b>='0') && (b<='9') )
{
b=b-'0';
wd=wd*10+b;
}
text++;
}
return (nfak*wd);
}
/**********************************************************/
/* DRIVER sendcallback */
/**********************************************************/
void send_callback(void *cb_data)
{
EC_RTDM_DRV_STRUCT * pdrvstruc;
pdrvstruc = (EC_RTDM_DRV_STRUCT*)cb_data;
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_send_ext(pdrvstruc->master);
rt_mutex_release(&pdrvstruc->masterlock);
}
}
/*****************************************************************************/
void receive_callback(void *cb_data)
{
EC_RTDM_DRV_STRUCT * pdrvstruc;
pdrvstruc = (EC_RTDM_DRV_STRUCT*)cb_data;
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_receive(pdrvstruc->master);
rt_mutex_release(&pdrvstruc->masterlock);
}
}
void receive_process(EC_RTDM_DRV_STRUCT * pdrvstruc)
{
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_receive(pdrvstruc->master);
ecrt_domain_process(pdrvstruc->domain);
pdrvstruc->reccnt++;
rt_mutex_release(&pdrvstruc->masterlock);
}
}
void send_process(EC_RTDM_DRV_STRUCT * pdrvstruc)
{
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_domain_queue(pdrvstruc->domain);
ecrt_master_send(pdrvstruc->master);
pdrvstruc->sendcnt++;
rt_mutex_release(&pdrvstruc->masterlock);
}
}
void detach_master(EC_RTDM_DRV_STRUCT * pdrvstruc)
{
if (pdrvstruc->isattached)
{
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);
pdrvstruc->master = NULL;
pdrvstruc->isattached=0;
EC_RTDM_INFO(pdrvstruc->masterno,"master detach done!\n");
}
}
/**********************************************************/
/* DRIVER OPEN */
/**********************************************************/
int ec_rtdm_open_rt(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info,
int oflags)
{
EC_RTDM_DRV_CONTEXT* my_context;
EC_RTDM_DRV_STRUCT * pdrvstruc;
const char * p;
int dev_no;
unsigned int namelen;
//int ret;
int dev_id;
// get the context for our driver - used to store driver info
my_context = (EC_RTDM_DRV_CONTEXT*)context->dev_private;
dev_no = -1;
namelen = strlen(context->device->driver_name);
p = &context->device->driver_name[namelen-1];
if (p!=&context->device->driver_name[0])
{
while ((*p>='0') && (*p<='9'))
{
p--;
if (p==&context->device->driver_name[0]) break;
}
dev_no=_atoi(p);
if ((dev_no!=-1) && (dev_no<EC_RTDM_MAX_MASTERS))
{
dev_id = context->device->device_id;
pdrvstruc = (EC_RTDM_DRV_STRUCT*)&ec_rtdm_masterintf[dev_no];
my_context->dev_id = dev_id;
my_context->pdrvstruc = pdrvstruc;
// enable interrupt in RTDM
return 0;
}
}
EC_RTDM_GERR("open - Cannot detect master device no\n");
return -EFAULT;
}
/**********************************************************/
/* DRIVER CLOSE */
/**********************************************************/
int ec_rtdm_close_rt(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info)
{
EC_RTDM_DRV_CONTEXT* my_context;
EC_RTDM_DRV_STRUCT * pdrvstruc;
// get the context
my_context = (EC_RTDM_DRV_CONTEXT*)context->dev_private;
pdrvstruc = my_context->pdrvstruc;
EC_RTDM_INFO(pdrvstruc->masterno,"close called!\n");
detach_master(pdrvstruc);
return 0;
}
/**********************************************************/
/* DRIVER IOCTL */
/**********************************************************/
int ec_rtdm_ioctl_rt(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info,
int request,
void *arg)
{
EC_RTDM_DRV_CONTEXT* my_context;
EC_RTDM_DRV_STRUCT * pdrvstruc;
int ret;
unsigned int l_ioctlvalue[]={0,0,0,0,0,0,0,0};
ec_domain_state_t ds;
ec_master_state_t ms;
uint64_t app_time;
ret = 0;
// get the context
my_context = (EC_RTDM_DRV_CONTEXT*)context->dev_private;
pdrvstruc = my_context->pdrvstruc;
switch (request) {
case EC_RTDM_MASTERSTATE:
{
if (!pdrvstruc->isattached)
{
return -EFAULT;
}
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_state(pdrvstruc->master, &ms);
rt_mutex_release(&pdrvstruc->masterlock);
}
if (rtdm_rw_user_ok(user_info, arg, sizeof(ms)))
{
// copy data to user
if (rtdm_copy_to_user(user_info, arg, &ms,sizeof(ms)))
{
return -EFAULT;
}
}
}
break;
case EC_RTDM_DOMAINSTATE:
{
if (!pdrvstruc->isattached)
{
return -EFAULT;
}
if (pdrvstruc->domain)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_domain_state(pdrvstruc->domain, &ds);
rt_mutex_release(&pdrvstruc->masterlock);
}
if (rtdm_rw_user_ok(user_info, arg, sizeof(ds)))
{
// copy data to user
if (rtdm_copy_to_user(user_info, arg, &ds,sizeof(ds)))
{
return -EFAULT;
}
}
}
break;
case EC_RTDM_MSTRRECEIVE:
{
if (pdrvstruc->isattached)
{
receive_process(pdrvstruc);
}
}
break;
case EC_RTDM_MSTRSEND:
{
if (pdrvstruc->isattached)
{
send_process(pdrvstruc);
}
}
break;
case EC_RTDM_MASTER_APP_TIME:
{
if (!pdrvstruc->isattached)
{
return -EFAULT;
}
if (rtdm_rw_user_ok(user_info, arg, sizeof(app_time)))
{
// copy data from user
if (rtdm_copy_from_user(user_info, &app_time, arg, sizeof(app_time)))
{
return -EFAULT;
}
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_application_time(pdrvstruc->master, app_time);
rt_mutex_release(&pdrvstruc->masterlock);
}
}
}
break;
case EC_RTDM_SYNC_REF_CLOCK:
{
if (!pdrvstruc->isattached)
{
return -EFAULT;
}
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_sync_reference_clock(pdrvstruc->master);
rt_mutex_release(&pdrvstruc->masterlock);
}
}
break;
case EC_RTDM_SYNC_SLAVE_CLOCK:
{
if (!pdrvstruc->isattached)
{
return -EFAULT;
}
if (pdrvstruc->master)
{
rt_mutex_acquire(&pdrvstruc->masterlock,TM_INFINITE);
ecrt_master_sync_slave_clocks(pdrvstruc->master);
rt_mutex_release(&pdrvstruc->masterlock);
}
}
break;
case EC_RTDM_MSTRATTACH:
{
unsigned int mstridx;
mstridx = 0;
ret = 0;
EC_RTDM_INFO(pdrvstruc->masterno,"Master attach start!\n");
if (user_info)
{
if (rtdm_read_user_ok(user_info, arg, sizeof(unsigned int)))
{
if (rtdm_copy_from_user(user_info, &l_ioctlvalue[0], arg,sizeof(unsigned int))==0)
{
pdrvstruc->domain = (ec_domain_t*)l_ioctlvalue[0];
}
else
{
EC_RTDM_ERR(pdrvstruc->masterno,"copy user param failed!\n");
ret=-EFAULT;
}
}
else
{
EC_RTDM_ERR(pdrvstruc->masterno,"user parameter domain missing!\n");
ret=-EFAULT;
}
}
if (ret!=0)
{
return ret;
}
if ( (pdrvstruc->master) && (pdrvstruc->isattached))
// master is allready attached
{
// master is allready attached
EC_RTDM_ERR(pdrvstruc->masterno,"Master is allready attached!\n");
ret = -EFAULT;
}
else
{
//mstr=ecrt_request_master(0);
mstridx = pdrvstruc->masterno;
pdrvstruc->master=ecrt_attach_master(mstridx);
if (pdrvstruc->master)
{
// Ok
EC_RTDM_INFO(pdrvstruc->masterno,"Master searching for domain!\n");
pdrvstruc->domain = ecrt_master_find_domain(pdrvstruc->master,l_ioctlvalue[0]);
if (!pdrvstruc->domain)
{
//
EC_RTDM_ERR(pdrvstruc->masterno,"Cannot find domain from index %u!\n",l_ioctlvalue[0]);
ret = -EFAULT;
}
else
{
// 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]);
//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);
pdrvstruc->isattached=1;
ret = 0;
}
}
else
{
EC_RTDM_ERR(pdrvstruc->masterno,"Master attach failed!\n");
pdrvstruc->master = NULL;
ret = -EFAULT;
}
}
}
break;
default:
ret = -ENOTTY;
}
return ret;
}
/**********************************************************/
/* DRIVER READ */
/**********************************************************/
int ec_rtdm_read_rt(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info, void *buf, size_t nbyte)
{
int ret;
#if defined(USE_THIS)
EC_RTDM_DRV_CONTEXT* my_context;
char *out_pos;
int dev_id;
rtdm_toseq_t timeout_seq;
int ret;
out_pos = (char *)buf;
my_context = (EC_RTDM_DRV_CONTEXT*)context->dev_private;
// zero bytes requested ? return!
if (nbyte == 0)
return 0;
// check if R/W actions to user-space are allowed
if (user_info && !rtdm_rw_user_ok(user_info, buf, nbyte))
return -EFAULT;
dev_id = my_context->dev_id;
// in case we need to check if reading is allowed (locking)
/* if (test_and_set_bit(0, &ctx->in_lock))
return -EBUSY;
*/
/* // if we need to do some stuff with preemption disabled:
rtdm_lock_get_irqsave(&ctx->lock, lock_ctx);
// stuff here
rtdm_lock_put_irqrestore(&ctx->lock, lock_ctx);
*/
// wait: if ctx->timeout = 0, it will block infintely until
// rtdm_event_signal(&ctx->irq_event); is called from our
// interrupt routine
//ret = rtdm_event_timedwait(&ctx->irq_event, ctx->timeout, &timeout_seq);
// now write the requested stuff to user-space
if (rtdm_copy_to_user(user_info, out_pos,
dummy_buffer, BUFSIZE) != 0) {
ret = -EFAULT;
} else {
ret = BUFSIZE;
}
#else
ret = -EFAULT;
#endif
return ret;
}
/**********************************************************/
/* DRIVER WRITE */
/**********************************************************/
int ec_rtdm_write_rt(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info,
const void *buf, size_t nbyte)
{
int ret;
#if defined(USE_THIS)
int dev_id;
char *in_pos = (char *)buf;
EC_RTDM_DRV_CONTEXT* my_context;
my_context = (EC_RTDM_DRV_CONTEXT*)context->dev_private;
if (nbyte == 0)
return 0;
if (user_info && !rtdm_read_user_ok(user_info, buf, nbyte))
return -EFAULT;
dev_id = my_context->dev_id;
if (rtdm_copy_from_user(user_info, dummy_buffer,
in_pos, BUFSIZE) != 0) {
ret = -EFAULT;
} else {
ret = BUFSIZE;
}
#else
ret = -EFAULT;
#endif
// used when it is atomic
// rtdm_mutex_unlock(&ctx->out_lock);
return ret;
}
/**********************************************************/
/* DRIVER OPERATIONS */
/**********************************************************/
// Template
static struct rtdm_device ec_rtdm_device_t = {
struct_version: RTDM_DEVICE_STRUCT_VER,
device_flags: RTDM_NAMED_DEVICE,
context_size: sizeof(EC_RTDM_DRV_CONTEXT),
device_name: EC_RTDM_DEV_FILE_NAME,
/* open and close functions are not real-time safe due kmalloc
and kfree. If you do not use kmalloc and kfree, and you made
sure that there is no syscall in the open/close handler, you
can declare the open_rt and close_rt handler.
*/
open_rt: NULL,
open_nrt: ec_rtdm_open_rt,
ops: {
close_rt: NULL,
close_nrt: ec_rtdm_close_rt,
ioctl_rt: ec_rtdm_ioctl_rt,
ioctl_nrt: ec_rtdm_ioctl_rt, // rtdm_mmap_to_user is not RT safe
read_rt: ec_rtdm_read_rt,
read_nrt: NULL,
write_rt: ec_rtdm_write_rt,
write_nrt: NULL,
recvmsg_rt: NULL,
recvmsg_nrt: NULL,
sendmsg_rt: NULL,
sendmsg_nrt: NULL,
},
device_class: RTDM_CLASS_EXPERIMENTAL,
device_sub_class: 222,
driver_name: EC_RTDM_DEV_FILE_NAME,
driver_version: RTDM_DRIVER_VER(1,0,1),
peripheral_name: EC_RTDM_DEV_FILE_NAME,
provider_name: "Moehwald GmbH - Bosch Group",
// proc_name: ethcatrtdm_device.device_name,
};
static struct rtdm_device ec_rtdm_devices[EC_RTDM_MAX_MASTERS];
/**********************************************************/
/* INIT DRIVER */
/**********************************************************/
int init_module(void)
{
unsigned int i;
int ret;
ret = 0;
EC_RTDM_GINFO("Initlializing EtherCAT RTDM Interface to Igh EtherCAT Master\n");
memset(&ec_rtdm_masterintf[0],0,sizeof(ec_rtdm_masterintf));
for (i=0;( (i<EC_RTDM_MAX_MASTERS) && (ret==0) ) ;i++)
{
// master no to struct
ec_rtdm_masterintf[i].masterno = i;
// copy from template
memcpy(&ec_rtdm_devices[i],&ec_rtdm_device_t,sizeof(ec_rtdm_devices[0]));
// set device name
snprintf(&ec_rtdm_devices[i].device_name[0],RTDM_MAX_DEVNAME_LEN,"%s%d",EC_RTDM_DEV_FILE_NAME,i);
// set proc_name
ec_rtdm_devices[i].proc_name = &ec_rtdm_devices[i].device_name[0];
ec_rtdm_devices[i].driver_name = &ec_rtdm_devices[i].device_name[0];
ec_rtdm_devices[i].peripheral_name = &ec_rtdm_devices[i].device_name[0];
EC_RTDM_GINFO("Registering device %s!\n",ec_rtdm_devices[i].driver_name);
ret = rtdm_dev_register(&ec_rtdm_devices[i]);
}
if (ret!=0)
{
// register m
EC_RTDM_GERR("Initialization of EtherCAT RTDM Interface failed\n");
}
return ret;
}
/**********************************************************/
/* CLEANUP DRIVER */
/**********************************************************/
void cleanup_module(void)
{
unsigned int i;
EC_RTDM_GINFO("Cleanup EtherCAT RTDM Interface to Igh EtherCAT Master - Moehwald GmbH\n");
for (i=0;i<EC_RTDM_MAX_MASTERS;i++)
{
if (ec_rtdm_masterintf[i].isattached)
{
detach_master(&ec_rtdm_masterintf[i]);
}
EC_RTDM_GINFO("Unregistering device %s!\n",ec_rtdm_devices[i].driver_name);
rtdm_dev_unregister(&ec_rtdm_devices[i],1000);
}
}
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("EtherCAT RTDM Interface to Igh EtherCAT Master");