beaglebone(AM335X)安装igh-ethercat

本文基于《beaglebone(AM335X)更换3.8.13内核、打xenomai补丁》,安装igh-ethercat1.5.2,并进行实验。
去igh官网下载资源http://www.etherlab.org/en/ethercat/index.php

1.配置编译环境

在ubuntu中解压文件,进入ethercat-1.5.2目录,并创建output目录。
编译前建议把上文的xenomai生成目录拷贝至ubuntu的/usr目录中,否则igh编译会有问题。

./configure --prefix=/home/vmuser/workspace/beaglebone/ethercat-1.5.2/output --with-linux-dir=/home/vmuser/workspace/beaglebone/linux-3.8.13-bone86 --enable-rtdm=yes --with-xenomai-dir=/usr/xenomai --enable-8139too=no --enable-generic=yes CC=arm-linux-gnueabihf-gcc --host=arm-linux-gnueabihf

2.编译igh

make
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
make install
//make DESTDIR=/media/vmuser/rootfs modules_install

将生成的ec_master.ko和ec_generic.ko拷贝至output目录
beaglebone(AM335X)安装igh-ethercat_第1张图片
需要将xenomai和igh生成的lib文件拷贝至SD卡的/lib文件夹中。

3.测试

输入ifconfig命令,查看HWaddr 74:E1:82:5C:EC:B5。

root@am335x-evm:~/igh-output# ifconfig
eth0      Link encap:Ethernet  HWaddr 74:E1:82:5C:EC:B5
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:40

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:700 (700.0 B)  TX bytes:700 (700.0 B)

设备使用倍福的EK1100和EL2008。

安装驱动、结果如下

root@am335x-evm:~/igh-output# insmod ec_master.ko main_devices=74:E1:82:5C:EC:B5
[ 2897.070790] EtherCAT: Master driver 1.5.2 2eff7c993a63vices=74:E1:82:5C:EC:B5
[ 2897.080753] EtherCAT 0: Registering RTDM device EtherCAT.
[ 2897.086856] EtherCAT: 1 master waiting for devices.

使用./ethercat start开启EtherCAT

root@am335x-evm:~/igh-output# insmod ec_generic.ko
[ 3103.680202] ec_generic: EtherCAT master generic Ethernet device module 1.5.2 2eff7c993a63
[ 3103.689363] EtherCAT: Accepting 74:E1:82:5C:EC:B5 as main device for master 0.
[ 3103.697267] ec_generic: Binding socket to interface 2 (eth0).
[ 3103.703560] EtherCAT 0: Starting EtherCAT-IDLE thread.

输入./ethercat pdos可以查看到3个所连接的倍福从站设备的对象字典等信息。

root@am335x-evm:~/igh-output# ./ethercat pdos
=== Master 0, Slave 1 ===
SM0: PhysAddr 0x0f00, DefaultSize    0, ControlRegister 0x44, Enable 9
  RxPDO 0x1600 "Channel 1"
    PDO entry 0x7000:01,  1 bit, "Output"
  RxPDO 0x1601 "Channel 2"
    PDO entry 0x7010:01,  1 bit, "Output"
  RxPDO 0x1602 "Channel 3"
    PDO entry 0x7020:01,  1 bit, "Output"
  RxPDO 0x1603 "Channel 4"
    PDO entry 0x7030:01,  1 bit, "Output"
  RxPDO 0x1604 "Channel 5"
    PDO entry 0x7040:01,  1 bit, "Output"
  RxPDO 0x1605 "Channel 6"
    PDO entry 0x7050:01,  1 bit, "Output"
  RxPDO 0x1606 "Channel 7"
    PDO entry 0x7060:01,  1 bit, "Output"
  RxPDO 0x1607 "Channel 8"
    PDO entry 0x7070:01,  1 bit, "Output"
=== Master 0, Slave 2 ===
SM0: PhysAddr 0x0f00, DefaultSize    0, ControlRegister 0x44, Enable 9
  RxPDO 0x1600 "Channel 1"
    PDO entry 0x7000:01,  1 bit, "Output"
  RxPDO 0x1601 "Channel 2"
    PDO entry 0x7010:01,  1 bit, "Output"
  RxPDO 0x1602 "Channel 3"
    PDO entry 0x7020:01,  1 bit, "Output"
  RxPDO 0x1603 "Channel 4"
    PDO entry 0x7030:01,  1 bit, "Output"
  RxPDO 0x1604 "Channel 5"
    PDO entry 0x7040:01,  1 bit, "Output"
  RxPDO 0x1605 "Channel 6"
    PDO entry 0x7050:01,  1 bit, "Output"
  RxPDO 0x1606 "Channel 7"
    PDO entry 0x7060:01,  1 bit, "Output"
  RxPDO 0x1607 "Channel 8"
    PDO entry 0x7070:01,  1 bit, "Output"
=== Master 0, Slave 3 ===
SM0: PhysAddr 0x0f00, DefaultSize    0, ControlRegister 0x44, Enable 9
  RxPDO 0x1600 "Channel 1"
    PDO entry 0x7000:01,  1 bit, "Output"
  RxPDO 0x1601 "Channel 2"
    PDO entry 0x7010:01,  1 bit, "Output"
  RxPDO 0x1602 "Channel 3"
    PDO entry 0x7020:01,  1 bit, "Output"
  RxPDO 0x1603 "Channel 4"
    PDO entry 0x7030:01,  1 bit, "Output"
  RxPDO 0x1604 "Channel 5"
    PDO entry 0x7040:01,  1 bit, "Output"
  RxPDO 0x1605 "Channel 6"
    PDO entry 0x7050:01,  1 bit, "Output"
  RxPDO 0x1606 "Channel 7"
    PDO entry 0x7060:01,  1 bit, "Output"
  RxPDO 0x1607 "Channel 8"
    PDO entry 0x7070:01,  1 bit, "Output"

4.控制从站小灯闪烁

自己写了一个小程序,用于控制从站1的小灯闪烁,参考了igh的官方例程。

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include "ecrt.h"
    RT_TASK my_task;
    static int run = 1;
    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;
    static ec_slave_config_t *sc_dig_out_01 = NULL;
    #define BusCoupler01_Pos  0, 0
    #define DigOutSlave01_Pos 0, 1
    #define Beckhoff_EK1100 0x00000002, 0x044c2c52
    #define Beckhoff_EL2008 0x00000002, 0x07d83052
    static unsigned int off_dig_out0 = 0;
    const static ec_pdo_entry_reg_t domain1_regs[] = {
       {DigOutSlave01_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out0, NULL},
    };
    /* Slave 1, "EL2008"
     * Vendor ID:       0x00000002
     * Product code:    0x07d83052
     * Revision number:"#x00100000"
     */
    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 */
        {0x7040, 0x01, 1}, /* Output */
        {0x7050, 0x01, 1}, /* Output */
        {0x7060, 0x01, 1}, /* Output */
        {0x7070, 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 */
        {0x1604, 1, slave_1_pdo_entries + 4},
        {0x1605, 1, slave_1_pdo_entries + 5},
        {0x1606, 1, slave_1_pdo_entries + 6},
        {0x1607, 1, slave_1_pdo_entries + 7} /* Channel 8 */
    };
    ec_sync_info_t slave_el2008_syncs[] = {
       {0, EC_DIR_OUTPUT, 8, slave_1_pdos + 0, EC_WD_ENABLE},
       {0xff}
    };
    void rt_check_domain_state(void)
    {
        ec_domain_state_t ds = {};
    	ecrt_domain_state(domain1, &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;
    	ecrt_master_state(master, &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 my_task_proc(void *arg)
    {
    	int cycle_counter = 0;
        unsigned int blink = 0;
    	rt_task_set_periodic(NULL, TM_NOW, 1000000); // ns
    	while (cycle_counter<10000) {
    		rt_task_wait_period(NULL);
    		cycle_counter++;
    		ecrt_master_receive(master);
    		ecrt_domain_process(domain1);
    		rt_check_domain_state();
    		if (!(cycle_counter % 1000)) {
    			rt_check_master_state();
    		}
    		if (!(cycle_counter % 200)) {
    			blink = !blink;
    		}
            EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0xFF: 0x00);
    		ecrt_domain_queue(domain1);
    		ecrt_master_send(master);
    	}
    
        run = 0;
    }
    void signal_handler(int sig)
    {
        run = 0;
    }
    int main(int argc, char *argv[])
    {
        ec_slave_config_t *sc;
        int ret;
        rt_print_auto_init(1);
        signal(SIGTERM, signal_handler);
        signal(SIGINT, signal_handler);
        mlockall(MCL_CURRENT | MCL_FUTURE);
        printf("Requesting master...\n");
        master = ecrt_request_master(0);
        if (!master) {
            return -1;
        }
        domain1 = ecrt_master_create_domain(master);
        printf("Creating slave configurations...\n");
        sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
        if (!sc) {
            return -1;
        }
        sc_dig_out_01 =ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2008);
        ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_el2008_syncs);
        ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs);
        printf("Activating master...\n");
        ecrt_master_activate(master);
        if (!(domain1_pd = ecrt_domain_data(domain1))) {
            fprintf(stderr, "Failed to get domain data pointer.\n");
            return -1;
        }
        ret = rt_task_create(&my_task, "my_task", 0, 80, T_FPU);
        if (ret < 0) {
            fprintf(stderr, "Failed to create task: %s\n", strerror(-ret));
            return -1;
        }
        printf("Starting my_task...\n");
        ret = rt_task_start(&my_task, &my_task_proc, NULL);
        if (ret < 0) {
            fprintf(stderr, "Failed to start task: %s\n", strerror(-ret));
            return -1;
        }
    	while (run) {
    		sched_yield();
    	}
        printf("Deleting realtime task...\n");
        rt_task_delete(&my_task);
        printf("End of Program\n");
        ecrt_release_master(master);
        return 0;
    }

编译程序需要用到xenomai和igh的生成文件,自己写一个makefile。
beaglebone(AM335X)安装igh-ethercat_第2张图片
在命令行输入./igh即可运行程序。
程序可能会上传。

你可能感兴趣的:(ethercat)