[其他ST产品] 基于AM4377的EtherCAT主站控制stm32从站

[复制链接]
1514|6
 楼主| 有何不可0365 发表于 2021-12-29 20:13 | 显示全部楼层 |阅读模式
本文为一个igh小例程,用于控制EtherCAT从站。主站控制3个倍福EL2008从站每秒钟实现1次亮灭,每秒读取stm32从站的AD数据并在串口中打印出来,stm32从站的8路输出由8路输入控制(用key控制led亮灭)。
程序参考了igh提供的example。
实验设备为安装了xenomai和igh的am4377开发板、倍福EL2008从站和淘宝店家“易元素电子”的stm32从站。
5992861cc50d8178b8.png

 楼主| 有何不可0365 发表于 2021-12-29 20:17 | 显示全部楼层
igh 主站代码

  1. #include <errno.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/resource.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <sys/mman.h>
  10. #include <rtdm/rtdm.h>
  11. #include <native/task.h>
  12. #include <native/sem.h>
  13. #include <native/mutex.h>
  14. #include <native/timer.h>
  15. #include <rtdk.h>
  16. #include <pthread.h>
  17. #include "ecrt.h"
  18. RT_TASK my_task;
  19. static int run = 1;
  20. /****************************************************************************/
  21. // EtherCAT
  22. static ec_master_t *master = NULL;
  23. static ec_master_state_t master_state = {};
  24. static ec_domain_t *domain1 = NULL;
  25. static ec_domain_state_t domain1_state = {};
  26. static uint8_t *domain1_pd = NULL;
  27. static ec_slave_config_t *sc_el2008_1 = NULL;
  28. static ec_slave_config_t *sc_el2008_2 = NULL;
  29. static ec_slave_config_t *sc_el2008_3 = NULL;
  30. static ec_slave_config_t *sc_stm = NULL;
  31. /****************************************************************************/
  32. // process data
  33. #define BusCoupler01_Pos  0, 0
  34. #define DigOutSlave01_Pos 0, 1
  35. #define DigOutSlave02_Pos 0, 2
  36. #define DigOutSlave03_Pos 0, 3
  37. #define DigOutSlave04_Pos 0, 4
  38. #define Beckhoff_EK1100 0x00000002, 0x044c2c52
  39. #define Beckhoff_EL2008 0x00000002, 0x07d83052
  40. #define stm32_lan9252 0x00000009, 0x00009252
  41. // offsets for PDO entries 所谓偏移,也相当于一个操作符号,不需要去写初始化,用户层只使用其地址
  42. static unsigned int off_dig_out0;
  43. static unsigned int off_dig_out1;
  44. static unsigned int off_dig_out2;
  45. static unsigned int off_dig_out3;
  46. static unsigned int off_dig_out4;
  47. static unsigned int off_dig_out5;
  48. // process data
  49. const static ec_pdo_entry_reg_t domain1_regs[] = {
  50.    {DigOutSlave01_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out0, NULL},
  51.    {DigOutSlave02_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out1, NULL},
  52.    {DigOutSlave03_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out2, NULL},
  53.    {DigOutSlave04_Pos, stm32_lan9252, 0x7010, 0x01, &off_dig_out3, NULL},
  54.    {DigOutSlave04_Pos, stm32_lan9252, 0x6000, 0x01, &off_dig_out4, NULL},
  55.    {DigOutSlave04_Pos, stm32_lan9252, 0x6020, 0x11, &off_dig_out5, NULL},
  56.    {}
  57. };
  58. /****************************************************************************/
  59. /* Slave 1, "EL2008"
  60. * Vendor ID:       0x00000002
  61. * Product code:    0x07d83052
  62. * Revision number:"#x00100000"
  63. */
  64. ec_pdo_entry_info_t slave_1_pdo_entries[] = {
  65.     {0x7000, 0x01, 1}, /* Output */
  66.     {0x7010, 0x01, 1}, /* Output */
  67.     {0x7020, 0x01, 1}, /* Output */
  68.     {0x7030, 0x01, 1}, /* Output */
  69.     {0x7040, 0x01, 1}, /* Output */
  70.     {0x7050, 0x01, 1}, /* Output */
  71.     {0x7060, 0x01, 1}, /* Output */
  72.     {0x7070, 0x01, 1} /* Output */
  73. };
  74. ec_pdo_info_t slave_1_pdos[] = {
  75.     {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
  76.     {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
  77.     {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
  78.     {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
  79.     {0x1604, 1, slave_1_pdo_entries + 4},
  80.     {0x1605, 1, slave_1_pdo_entries + 5},
  81.     {0x1606, 1, slave_1_pdo_entries + 6},
  82.     {0x1607, 1, slave_1_pdo_entries + 7} /* Channel 8 */
  83. };
  84. ec_sync_info_t slave_el2008_syncs[] = {
  85.    {0, EC_DIR_OUTPUT, 8, slave_1_pdos + 0, EC_WD_ENABLE},
  86.    {0xff}
  87. };
  88. /* Master 0, Slave 4, "LAN9252-EVB-HBI"
  89. * Vendor ID:       0x00000009
  90. * Product code:    0x00009252
  91. * Revision number: 0x00000001
  92. */
  93. ec_pdo_entry_info_t slave_stm_pdo_entries[] = {
  94.     {0x7010, 0x01, 1}, /* LED 1 */
  95.     {0x7010, 0x02, 1}, /* LED 2 */
  96.     {0x7010, 0x03, 1}, /* LED 3 */
  97.     {0x7010, 0x04, 1}, /* LED 4 */
  98.     {0x7010, 0x05, 1}, /* LED 5 */
  99.     {0x7010, 0x06, 1}, /* LED 6 */
  100.     {0x7010, 0x07, 1}, /* LED 7 */
  101.     {0x7010, 0x08, 1}, /* LED 8 */
  102.     {0x0000, 0x00, 8}, /* Gap */
  103.     {0x6000, 0x01, 1}, /* Switch 1 */
  104.     {0x6000, 0x02, 1}, /* Switch 2 */
  105.     {0x6000, 0x03, 1}, /* Switch 3 */
  106.     {0x6000, 0x04, 1}, /* Switch 4 */
  107.     {0x6000, 0x05, 1}, /* Switch 5 */
  108.     {0x6000, 0x06, 1}, /* Switch 6 */
  109.     {0x6000, 0x07, 1}, /* Switch 7 */
  110.     {0x6000, 0x08, 1}, /* Switch 8 */
  111.     {0x0000, 0x00, 8}, /* Gap */
  112.     {0x6020, 0x01, 1}, /* Underrange */
  113.     {0x6020, 0x02, 1}, /* Overrange */
  114.     {0x6020, 0x03, 2}, /* Limit 1 */
  115.     {0x6020, 0x05, 2}, /* Limit 2 */
  116.     {0x0000, 0x00, 8}, /* Gap */
  117.     {0x1802, 0x07, 1}, /* TxPDOState */
  118.     {0x1802, 0x09, 1}, /* TxPDO Toggle */
  119.     {0x6020, 0x11, 16}, /* Analog input */
  120. };
  121. ec_pdo_info_t slave_stm_pdos[] = {
  122.     {0x1601, 9, slave_stm_pdo_entries + 0}, /* DO RxPDO-Map */
  123.     {0x1a00, 9, slave_stm_pdo_entries + 9}, /* DI TxPDO-Map */
  124.     {0x1a02, 8, slave_stm_pdo_entries + 18}, /* AI TxPDO-Map */
  125. };
  126. //sm0 for MBoxOut;sm1 for MBoxIn;sm2 for Outputs;sm3 for Inputs
  127. ec_sync_info_t slave_stm_syncs[] = {
  128.     {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
  129.     {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
  130.     {2, EC_DIR_OUTPUT, 1, slave_stm_pdos + 0, EC_WD_ENABLE},
  131.     {3, EC_DIR_INPUT, 2, slave_stm_pdos + 1, EC_WD_DISABLE},
  132.     {0xff}
  133. };
  134. /*****************************************************************************
  135. * Realtime task
  136. ****************************************************************************/
  137. void rt_check_domain_state(void)
  138. {
  139.     ec_domain_state_t ds = {};
  140.     ecrt_domain_state(domain1, &ds);
  141.     if (ds.working_counter != domain1_state.working_counter) {
  142.         rt_printf("Domain1: WC %u.\n", ds.working_counter);
  143.     }
  144.     if (ds.wc_state != domain1_state.wc_state) {
  145.         rt_printf("Domain1: State %u.\n", ds.wc_state);
  146.     }
  147.     domain1_state = ds;
  148. }
  149. /****************************************************************************/
  150. void rt_check_master_state(void)
  151. {
  152.     ec_master_state_t ms;
  153.     ecrt_master_state(master, &ms);
  154.     if (ms.slaves_responding != master_state.slaves_responding) {
  155.         rt_printf("%u slave(s).\n", ms.slaves_responding);
  156.     }
  157.     if (ms.al_states != master_state.al_states) {
  158.         rt_printf("AL states: 0x%02X.\n", ms.al_states);
  159.     }
  160.     if (ms.link_up != master_state.link_up) {
  161.         rt_printf("Link is %s.\n", ms.link_up ? "up" : "down");
  162.     }
  163.     master_state = ms;
  164. }
  165. /****************************************************************************/
  166. #define _ms 1000000
  167. void my_task_proc(void *arg)
  168. {
  169.     int cycle_counter = 0;
  170.     unsigned int blink = 0;
  171.     rt_task_set_periodic(NULL, TM_NOW, 1*_ms); // ns
  172.         while (cycle_counter<10000) {
  173.                 rt_task_wait_period(NULL);
  174.                 cycle_counter++;
  175.                 // receive EtherCAT frames
  176.                 ecrt_master_receive(master);
  177.                 ecrt_domain_process(domain1);
  178.                 rt_check_domain_state();
  179.                 if (!(cycle_counter % 1000)) {
  180.                         rt_check_master_state();
  181.                 }
  182.                 if (!(cycle_counter % 1000)) {
  183.                         blink = !blink;
  184.             printf("ad input... %d\n",EC_READ_U16(domain1_pd + off_dig_out5));
  185.                 }
  186.                 EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0xFF: 0x00);
  187.         EC_WRITE_U8(domain1_pd + off_dig_out1, blink ? 0xFF: 0x00);
  188.         EC_WRITE_U8(domain1_pd + off_dig_out2, blink ? 0xFF: 0x00);
  189.         EC_WRITE_U8(domain1_pd + off_dig_out3, EC_READ_U8(domain1_pd + off_dig_out4));   
  190.                 // send process data
  191.                 ecrt_domain_queue(domain1);
  192.                 ecrt_master_send(master);
  193.         }
  194.     run = 0;
  195. }
  196. /****************************************************************************
  197. * Signal handler
  198. ***************************************************************************/
  199. void signal_handler(int sig)
  200. {
  201.     run = 0;
  202. }
  203. /****************************************************************************
  204. * Main function=
  205. ***************************************************************************/
  206. int main(int argc, char *argv[])
  207. {
  208.     int ret;
  209.     /* Perform auto-init of rt_print buffers if the task doesn't do so */
  210.     rt_print_auto_init(1);
  211.     // rt_print_init(1000,"LEE_RT");
  212.     signal(SIGTERM, signal_handler);
  213.     signal(SIGINT, signal_handler);
  214.     mlockall(MCL_CURRENT | MCL_FUTURE);
  215.     printf("Requesting master...\n");
  216.     master = ecrt_request_master(0);
  217.     if (!master) {
  218.         return -1;
  219.     }
  220.     printf("Creating slave configurations...\n");
  221.     // Create configuration for bus coupler EK1100
  222.     ec_slave_config_t *sc;
  223.     sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
  224.     if (!sc) {
  225.         return -1;
  226.     }
  227.     //获从站服配置
  228.     sc_el2008_1 = ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2008);
  229.     sc_el2008_2 = ecrt_master_slave_config(master, DigOutSlave02_Pos, Beckhoff_EL2008);
  230.     sc_el2008_3 = ecrt_master_slave_config(master, DigOutSlave03_Pos, Beckhoff_EL2008);
  231.     sc_stm = ecrt_master_slave_config(master, DigOutSlave04_Pos, stm32_lan9252);
  232.     //过程数据SM配置
  233.     ecrt_slave_config_pdos(sc_el2008_1, EC_END, slave_el2008_syncs);
  234.     ecrt_slave_config_pdos(sc_el2008_2, EC_END, slave_el2008_syncs);
  235.     ecrt_slave_config_pdos(sc_el2008_3, EC_END, slave_el2008_syncs);
  236.     ecrt_slave_config_pdos(sc_stm, EC_END, slave_stm_syncs);
  237.     //创建过程数据域
  238.     domain1 = ecrt_master_create_domain(master);
  239.     ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs);
  240.     printf("Activating master...\n");
  241.     ecrt_master_activate(master);
  242.     if (!(domain1_pd = ecrt_domain_data(domain1))) {
  243.         fprintf(stderr, "Failed to get domain1 data pointer.\n");
  244.         return -1;
  245.     }
  246.     ret = rt_task_create(&my_task, "my_task", 0, 99, T_FPU);
  247.     if (ret < 0) {
  248.         fprintf(stderr, "Failed to create task: %s\n", strerror(-ret));
  249.         return -1;
  250.     }
  251.     printf("Starting my_task...\n");
  252.     ret = rt_task_start(&my_task, &my_task_proc, NULL);
  253.     if (ret < 0) {
  254.         fprintf(stderr, "Failed to start task: %s\n", strerror(-ret));
  255.         return -1;
  256.     }
  257.     while (run) {
  258.         sched_yield();
  259.     }
  260.     printf("Deleting realtime task...\n");
  261.     rt_task_delete(&my_task);
  262.     printf("End of Program\n");
  263.     ecrt_release_master(master);
  264.     return 0;
  265. }

 楼主| 有何不可0365 发表于 2021-12-29 20:19 | 显示全部楼层
makefile
 楼主| 有何不可0365 发表于 2021-12-29 20:20 | 显示全部楼层
  1. CC       = arm-linux-gnueabihf-gcc
  2. INCPATH  = -I./igh-output/include -I./xenomai-Output/include -I./xenomai-Output/include/posix
  3. LIBS     = -L./igh-output/lib -lethercat_rtdm -L./xenomai-Output/lib -lrtdm -lpthread_rt -lnative  -lxenomai -lpthread -lrt -lm
  4. LFLAGS   = -Wl,@./xenomai-Output/lib/posix.wrappers
  5. DEFINES  = -D_GNU_SOURCE -D_REENTRANT -D__XENO__
  6. CFLAGS   = $(DEFINES)

  7. TARGET        = overwatch
  8. ####### Output directory
  9. OBJECTS_DIR   = ./
  10. ####### Files
  11. SOURCES       = overwatch.c        
  12. OBJECTS       = overwatch.o       
  13. ####### Build rules
  14. $(TARGET):  $(OBJECTS)  
  15.         $(CC) $(LFLAGS) -o $(TARGET) $(OBJECTS)  $(LIBS)

  16. ####### Compile
  17. $(OBJECTS): $(SOURCES)
  18.         $(CC) $(INCPATH) $(CFLAGS) -o $(OBJECTS) -c $(SOURCES)

  19. clean:
  20.         rm  overwatch *.o   

 楼主| 有何不可0365 发表于 2021-12-29 20:23 | 显示全部楼层
控制部分的代码

  1.                 if (!(cycle_counter % 1000)) {
  2.                         blink = !blink;
  3.             printf("ad input... %d\n",EC_READ_U16(domain1_pd + off_dig_out5));
  4.                 }
  5.                 EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0xFF: 0x00);
  6.         EC_WRITE_U8(domain1_pd + off_dig_out1, blink ? 0xFF: 0x00);
  7.         EC_WRITE_U8(domain1_pd + off_dig_out2, blink ? 0xFF: 0x00);
  8.         EC_WRITE_U8(domain1_pd + off_dig_out3, EC_READ_U8(domain1_pd + off_dig_out4));  
 楼主| 有何不可0365 发表于 2021-12-29 20:33 | 显示全部楼层
实验结果
6783961cc559b022ef.png
 楼主| 有何不可0365 发表于 2021-12-29 22:37 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

45

主题

781

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部