IGH移植步骤

准备工作:
1.在官网 http://www.etherlab.org/en/ethercat/ 下载ethercat-1.5.2.tar.bz2以及ethercat-1.5.2.pdf。
2.准备目标arm板运行的内核源码,编译通过。内核编译出现问题见附1-3.
3.安装或者解压目标arm板相应的交叉编译工具。安装见附1-1.

/**************Ubuntu平台下*************/

/*1.解压IGH源码进入目录*/
#tar xjf ethercat-1.5.2.tar.bz2
#cd ethercat-1.5.2/

/*********以下都是在ethercat-1.5.2/目录进行操作**********/
/*2.配置源码,具体参数对照ethercat-1.5.2.pdf第88页Table 9.1: Configuration options */
(注意:等号后面不能有空格)
/*****(1)海思H3520D.****/
./configure --prefix=/root/hixx/ethercat-1.5.2/output --with-linux-dir=/root/hixx/Hi3520D_SDK_V1.0.4.0/osdrv/kernel/linux-3.0.y --enable-8139too=no --enable-generic=yes CC=arm-none-linux-gnueabi-gcc --host=arm-none-linux-gnueabi

/*****(2)飞凌IMX6Q.*****/
./configure --prefix=/home/gec/ethercat-1.5.2/output --with-linux-dir=/opt/linux-3.0.35 --enable-8139too=no --enable-generic=yes CC=arm-none-linux-gnueabi-gcc --host=arm-none-linux-gnueabi


/*3.编译源码*/
#make

/*4.指定交叉编译工具,编译modules*/
#make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- modules
//编译通过会对应生成ethercat-1.5.2/devices/ec_generic.ko和ethercat-1.5.1/master/ec_master.ko

/*5.安装*/
#make install
//make install 会在当前目录下生成 output 文件夹,里面有编译生成的各种用户空间的文件

/*6.在output目录下创建modules文件夹,并复制ec_generic.ko和ec_master.ko到modules下*/
#mkdir -p output/modules
#cp devices/ec_generic.ko output/modules/
#cp master/ec_master.ko  output/modules/

/*7.将output文件夹打包*/
#tar -cjf output.tar.bz2 output/

/**************至此IGH交叉编译完成,下面是在对应arm目标板上的操作***************/
注意:
1.arm目标板上要使用的东西都在output文件夹下或者是output.tar.bz2
2.output文件夹下包含链接文件,可以通过nfs挂载的方式cp到板子上,不是通过挂载的方式只能下载output.tar.bz2到板子再解压
3.确保板子和电脑或者Ubuntu已经连上,可以手动指定IP:ifconfig eth0 192.168.1.106 //根据需要更改

/*1.通过nfs挂载开发板到Ubuntu的nfs目录*/
#cd /tmp
#mkdir -p nfs
#mount -t nfs -o nolock Ubuntu IP:/nfs  nfs
//挂载成功后,板子上/tmp/nfs 的内容就是Ubuntu下nfs的内容,可以在Ubuntu下将output目录全部内容复制到nfs下

/*2.将output复制到板子的根目录下*/
#cd nfs
#cp -r output/ /

/*3.进入/output目录下*/
#cd /output
#ls
bin  etc  include  lib  modules  sbin
//将output目录下各文件目录的内容复制到板子根文件系统根目录下相应目录下,例如:cp bin/ethercat /bin/

/*4.复制ec_master.ko到/lib/modules/内核版本号/ */
#cp /output/modules/ec_master.ko /lib/modules/2.6.32-38-generic/
//这里的内核版本是2.6.32-38-generic,可以通过uname -r 查看
#depmod

/*5.配置rules,创建设备号*/
#vi /etc/sysconfig/ethercat
将引号中的内容 “# echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules” 加进去。

/*6.获取板子MAC地址*/
#ifconfig
//eth2  Link encap:以太网  硬件地址 00:0c:29:01:69:aa

/**************至此板子上的配置结束,下面是启动EtherCAT ***************/
通过网线直连ethercat主从站,从站启动完成后,打开主站电源
/*1.配置主站的MAC地址*/
/*****飞凌IMX6Q******/
#modprobe ec_master main_devices=1E:ED:19:27:1A:B3

/*2.启动ethercat*/
#/etc/init.d/ethercat start

/*3.安装通用网卡驱动*/
#insmod /modules/ec_generic.ko

//上面两步加载了 EtherCAT 的内核模块,下面就可以使用 ethercat 工具来进行一些操作

/*4.通过ethercat查看信息*/
#ethercat
//输入ethercat得到帮助菜单

/*****************到此移植过程结束********************/


/**************在Ubuntu下编译使用IGH******************/
/*1.解压IGH源码进入目录*/
#tar xjf ethercat-1.5.2.tar.bz2
#cd ethercat-1.5.2/

/*2.配置源码*/
./configure
//3.5版本内核不支持8139too driver,加上 --enable-8139too=no --enable-generic=yes。
即./configure --enable-8139too=no --enable-generic=yes

/*3.编译源码*/
#make

/*4.编译modules*/
#make modules

/*5.安装*/
#make install
#make modules_install
//make install 会在当前/opt目录下生成 etherlab 文件夹,里面有编译生成的各种用户空间的文件

/*6.进入etherlab目录下*/
#cd /opt/etherlab

/*7.复制文件ethercat*/
#cp etc/sysconfig/ethercat /etc/sysconfig/
//复制不成功的话先到/etc下创建sysconfig文件夹

/*8.创建软链接,文件路径使用绝对路径*/
#ln -s /opt/etherlab/etc/init.d/ethercat /etc/init.d/

/*9.复制ec_master.ko到/lib/modules/内核版本号/ 并且使能*/
#cp ethercat-1.5.2/master/ec_master.ko /lib/modules/2.6.32-38-generic/
//这里的内核版本是2.6.32-38-generic,可以通过uname -r 查看
#depmod

/*10.配置rules,创建设备号*/
#gedit /etc/sysconfig/ethercat
将引号中的内容 “# echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules” 加进去。

/*11.获取板子MAC地址*/
#ifconfig
//eth2  Link encap:以太网  硬件地址 00:0c:29:fb:59:24

/*12.配置主站的MAC地址*/
#modprobe ec_master main_devices=00:0c:29:fb:59:24

通过网线直连ethercat主从站,从站启动完成后,打开主站电源
/*13.启动ethercat*/
#/etc/init.d/ethercat start

/*14.安装通用网卡驱动*/
#insmod ethercat-1.5.2/devices/ec_generic.ko

//上面两步加载了 EtherCAT 的内核模块,下面就可以使用 ethercat 工具来进行一些操作

/*15.通过ethercat查看信息*/
#ethercat
//输入ethercat得到帮助菜单


附1-1:
/*****************IMX6Q交叉编译器的安装*****************/
/*1.将文件 gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12.tar.gz 拷贝到 Ubuntu 根目录下*/
#cp gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12.tar.gz /

/*2.进入根目录,创建新的文件夹并解压*/
#cd  /                                //进入根目录
#mkdir -p /opt/freescale/usr/local    //创建目录,若目录已存在会提示错误,跳过即可
#tar jxvf gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12.tar.gz -C /opt/freescale/usr/local
//编译器解压到/opt/freescale/usr/local

/*3.编辑环境变量*/
#gedit /etc/environment
//添加以下双引号中的内容(不包括双引号)到PATH里面
//“:/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin”

/*4.使环境变量生效*/
#source /etc/environment

/*5.查看交叉编译器是否安装成功*/
# arm-fsl-linux-gnueabi-gcc -v
//出现以下内容即安装成功:
/*Using built-in specs.
COLLECT_GCC=arm-fsl-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/opt/freescale/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/
fsl-linaro-toolchain/bin/../libexec/gcc/arm-fsl-linux-gnueabi/4.6.2/lto-wrapper
Target: arm-fsl-linux-gnueabi
Configured with: /work/build/.build/src/gcc-linaro-4.6-2011.06-0/configure
--build=i686-build_pc-linux-gnu --host=i686-build_pc-linux-gnu --target=arm-fsl-linux-gnueabi
--prefix=/work/fsl-linaro-toolchain-2.13 --with-sysroot=/work/fsl-linaro-toolchain-2.13/arm-fsl-linux-gnueabi/multi-libs
--enable-languages=c,c++ --with-pkgversion='Freescale MAD -- Linaro 2011.07
-- Built at 2011/08/10 09:20' --enable-__cxa_atexit --disable-libmudflap
--disable-libgomp --disable-libssp --with-gmp=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-mpfr=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-mpc=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-ppl=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-cloog=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-libelf=/work/build/.build/arm-fsl-linux-gnueabi/build/static
--with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic
-lm -L/work/build/.build/arm-fsl-linux-gnueabi/build/static/lib -lpwl'
--enable-threads=posix --enable-target-optspace --enable-plugin --enable-multilib
--with-local-prefix=/work/fsl-linaro-toolchain-2.13/arm-fsl-linux-gnueabi/multi-libs
--disable-nls --enable-c99 --enable-long-long --with-system-zlib
Thread model: posix
gcc version 4.6.2 20110630 (prerelease) (Freescale MAD -- Linaro 2011.07 -- Built at 2011/08/10 09:20)
*/

附1-2:
/************编译应用程序**************/
arm-none-linux-gnueabi-gcc main.c -o ethercat_test -I../../include/ -L../../output/lib -lethercat

附1-3:
/************编译内核源码出现以下问题的解决方法**************/
1.“mkimage” command not found – U-Boot images will not be built
1)正确编译对应的U-boot源码
2)复制U-boot/tools下的mkimage到/usr/bin/
#cp mkimage /usr/bin/
3)重新make uImage

附1-4:主站测试代码
复制以下内容到 ethercat-1.5.2\examples\user\main.c
从站是AM3359 ICE V2 运行Simple Demo Application
/*****************************************************************************
 *
 *  $Id: main.c,v 6a6dec6fc806 2012/09/19 17:46:58 fp $
 *
 *  Copyright (C) 2007-2009  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.
 *
 ****************************************************************************/

#include
#include
#include
#include
#include
#include
#include
#include

/****************************************************************************/

#include "ecrt.h"

/****************************************************************************/

// Application parameters
#define FREQUENCY 100
#define PRIORITY 1

// 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 ec_domain_t *domain2 = NULL;
static ec_domain_state_t domain2_state = {};

static ec_slave_config_t *sc;
static ec_slave_config_state_t sc_ana_in_state = {};

// Timer
static unsigned int sig_alarms = 0;
static unsigned int user_alarms = 0;

/****************************************************************************/

// process data
static uint8_t *domain1_pd = NULL;
static uint8_t *domain2_pd = NULL;

#define BusCouplerPos  0, 0

#define TI_AM3359ICE    0xE000059D, 0x54490001

// offsets for PDO entries
static unsigned int off_dig_out2;
static unsigned int off_dig_in2;

static unsigned int counter = 0;
static unsigned int blink = 0x00;

/*****************************************************************************/

#if CONFIGURE_PDOS
ec_pdo_entry_info_t slave_0_pdo_entries[] = {
    {0x7010, 0x00, 32},
    {0x6000, 0x00, 4*8},
};
ec_pdo_info_t slave_0_pdos[] = {
    {0x1601, 1, slave_0_pdo_entries + 0},
    {0x1a00, 1, slave_0_pdo_entries + 1},
};
static ec_sync_info_t slave_0_pdo_syncs[] = {
    {2, EC_DIR_OUTPUT, 1, slave_0_pdos + 0},
    {3, EC_DIR_INPUT,  1, slave_0_pdos + 1},
    {0xff}
};

#endif

/*****************************************************************************/

void check_domain1_state(void)
{
    ec_domain_state_t ds;

    ecrt_domain_state(domain1, &ds);

    if (ds.working_counter != domain1_state.working_counter)
        printf("Domain1: WC %u.\n", ds.working_counter);
    if (ds.wc_state != domain1_state.wc_state)
        printf("Domain1: State %u.\n", ds.wc_state);

    domain1_state = ds;
}

/*****************************************************************************/
void check_domain2_state(void)
{
    ec_domain_state_t ds;

    ecrt_domain_state(domain2, &ds);

    if (ds.working_counter != domain2_state.working_counter)
        printf("Domain2: WC %u.\n", ds.working_counter);
    if (ds.wc_state != domain2_state.wc_state)
        printf("Domain2: State %u.\n", ds.wc_state);

    domain2_state = ds;
}

/*****************************************************************************/

void check_master_state(void)
{
    ec_master_state_t ms;

    ecrt_master_state(master, &ms);

    if (ms.slaves_responding != master_state.slaves_responding)
        printf("%u slave(s).\n", ms.slaves_responding);
    if (ms.al_states != master_state.al_states)
        printf("AL states: 0x%02X.\n", ms.al_states);
    if (ms.link_up != master_state.link_up)
        printf("Link is %s.\n", ms.link_up ? "up" : "down");

    master_state = ms;
}

/*****************************************************************************/

void check_slave_config_states(void)
{
    ec_slave_config_state_t s;

    ecrt_slave_config_state(sc, &s);

    if (s.al_state != sc_ana_in_state.al_state)
        printf("AnaIn: State 0x%02X.\n", s.al_state);
    
    if (s.online != sc_ana_in_state.online)
        printf("AnaIn: %s.\n", s.online ? "online" : "offline");
    if (s.operational != sc_ana_in_state.operational)
        printf("AnaIn: %soperational.\n",
                s.operational ? "" : "Not ");

    sc_ana_in_state = s;
}

/*****************************************************************************/

void cyclic_task()
{
    // receive process data
    ecrt_master_receive(master);
    ecrt_domain_process(domain1);
    ecrt_domain_process(domain2);

    // check process data state (optional)
    check_domain1_state();
    check_domain2_state();

//    if (counter) {
//        counter--;
//    } else { // do this at 1 Hz
        counter = FREQUENCY;

        // calculate new process data
        blink ++;

        // check for master state (optional)
        check_master_state();

        // check for islave configuration state(s) (optional)
        check_slave_config_states();
    //    printf("AnaIn: value=0x%x\n", EC_READ_U32(domain2_pd + off_dig_in2));

printf("AnaIn: value=0x%x\n", EC_READ_U32(domain2_pd + off_dig_in2));
//printf("AnaIn: value=0x%x\n", EC_READ_U32(domain2_pd + off_dig_in2 + 4));
        EC_WRITE_U32(domain1_pd + off_dig_out2, blink);
//        printf("AnaIn: value=0x%x\n", EC_READ_U32(domain1_pd + off_dig_out2));
//    }
    // send process data
    ecrt_domain_queue(domain1);
    ecrt_domain_queue(domain2);
    ecrt_master_send(master);
}

/****************************************************************************/

void signal_handler(int signum) {
    switch (signum) {
        case SIGALRM:
            sig_alarms++;
            break;
    }
}

/****************************************************************************/

int main(int argc, char **argv)
{
    struct sigaction sa;
    struct itimerval tv;

    master = ecrt_request_master(0);
    if (!master)
        return -1;

    domain1 = ecrt_master_create_domain(master);
    if (!domain1)
        return -1;
    
    domain2 = ecrt_master_create_domain(master);
    if (!domain2)
        return -1;

#if CONFIGURE_PDOS
    if (!(sc = ecrt_master_slave_config(
                        master, BusCouplerPos, TI_AM3359ICE))) {
            fprintf(stderr, "Failed to get slave configuration.\n");
            return -1;
        }

    if (ecrt_slave_config_pdos(sc, EC_END, slave_0_pdo_syncs)) {
        fprintf(stderr, "Failed to configure PDOs.\n");
        return -1;
    }
    printf("Configuring PDOs...\n");
                    
    // Create configuration for bus coupler
    off_dig_out2 = ecrt_slave_config_reg_pdo_entry(sc,
            0x7010, 0, domain1, NULL);
        if (off_dig_out2 < 0)
            return -1;
    off_dig_in2 = ecrt_slave_config_reg_pdo_entry(sc,
            0x6000, 0, domain2, NULL);
        if (off_dig_in2 < 0)
            return -1;
#endif

    printf("Activating master...\n");
    if (ecrt_master_activate(master))
        return -1;

    if (!(domain1_pd = ecrt_domain_data(domain1))) {
        return -1;
    }
    if (!(domain2_pd = ecrt_domain_data(domain2))) {
        return -1;
    }

    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGALRM, &sa, 0)) {
        fprintf(stderr, "Failed to install signal handler!\n");
        return -1;
    }

    printf("Starting timer...\n");
    tv.it_interval.tv_sec = 0;
    tv.it_interval.tv_usec = 1000000 / FREQUENCY;
    tv.it_value.tv_sec = 0;
    tv.it_value.tv_usec = 2000;
    if (setitimer(ITIMER_REAL, &tv, NULL)) {
        fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
        return 1;
    }

    printf("Started.\n");
    while (1) {
        pause();
        while (sig_alarms != user_alarms) {
            cyclic_task();
            user_alarms++;
        }
    }
    return 0;
}

/****************************************************************************/


附1-5:
/***********IGH demo编译*****************/
arm-none-linux-gnueabi-gcc main.c -o ethercat_test -I../../output/include/ -L../../output/lib -lethercat

你可能感兴趣的:(移植_MCU_工业控制,linux)