当前位置:网站首页>EtherCAT master station based on am4377 controls STM32 slave station
EtherCAT master station based on am4377 controls STM32 slave station
2022-06-21 17:57:00 【Marathon】
This article is a igh Small routine , Used to control the EtherCAT From station . Master station control 3 Gebeifu EL2008 The slave station realizes every second 1 Secondary light off , Read per second stm32 Slave station AD Data and print it out in the serial port ,stm32 Slave station 8 The channel output is controlled by 8 Channel input control ( use key control led Light out ).
The program refers to igh Provided example.
The experimental equipment is equipped with xenomai and igh Of am4377 Development board 、 Biff EL2008 From the station and Taobao store “ Easy element electron ” Of stm32 From station .
igh Master station code
#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 <rtdk.h>
#include <pthread.h>
#include "ecrt.h"
RT_TASK my_task;
static int run = 1;
/****************************************************************************/
// EtherCAT
static ec_master_t *master = NULL;
static ec_master_state_t master_state = {
};
static ec_domain_t *domain1 = NULL;
static ec_domain_state_t domain1_state = {
};
static uint8_t *domain1_pd = NULL;
static ec_slave_config_t *sc_el2008_1 = NULL;
static ec_slave_config_t *sc_el2008_2 = NULL;
static ec_slave_config_t *sc_el2008_3 = NULL;
static ec_slave_config_t *sc_stm = NULL;
/****************************************************************************/
// process data
#define BusCoupler01_Pos 0, 0
#define DigOutSlave01_Pos 0, 1
#define DigOutSlave02_Pos 0, 2
#define DigOutSlave03_Pos 0, 3
#define DigOutSlave04_Pos 0, 4
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
#define Beckhoff_EL2008 0x00000002, 0x07d83052
#define stm32_lan9252 0x00000009, 0x00009252
// offsets for PDO entries So called offset , It is also equivalent to an operation symbol , No need to write initialization , The user layer only uses its address
static unsigned int off_dig_out0;
static unsigned int off_dig_out1;
static unsigned int off_dig_out2;
static unsigned int off_dig_out3;
static unsigned int off_dig_out4;
static unsigned int off_dig_out5;
// process data
const static ec_pdo_entry_reg_t domain1_regs[] = {
{
DigOutSlave01_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out0, NULL},
{
DigOutSlave02_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out1, NULL},
{
DigOutSlave03_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out2, NULL},
{
DigOutSlave04_Pos, stm32_lan9252, 0x7010, 0x01, &off_dig_out3, NULL},
{
DigOutSlave04_Pos, stm32_lan9252, 0x6000, 0x01, &off_dig_out4, NULL},
{
DigOutSlave04_Pos, stm32_lan9252, 0x6020, 0x11, &off_dig_out5, 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}
};
/* Master 0, Slave 4, "LAN9252-EVB-HBI" * Vendor ID: 0x00000009 * Product code: 0x00009252 * Revision number: 0x00000001 */
ec_pdo_entry_info_t slave_stm_pdo_entries[] = {
{
0x7010, 0x01, 1}, /* LED 1 */
{
0x7010, 0x02, 1}, /* LED 2 */
{
0x7010, 0x03, 1}, /* LED 3 */
{
0x7010, 0x04, 1}, /* LED 4 */
{
0x7010, 0x05, 1}, /* LED 5 */
{
0x7010, 0x06, 1}, /* LED 6 */
{
0x7010, 0x07, 1}, /* LED 7 */
{
0x7010, 0x08, 1}, /* LED 8 */
{
0x0000, 0x00, 8}, /* Gap */
{
0x6000, 0x01, 1}, /* Switch 1 */
{
0x6000, 0x02, 1}, /* Switch 2 */
{
0x6000, 0x03, 1}, /* Switch 3 */
{
0x6000, 0x04, 1}, /* Switch 4 */
{
0x6000, 0x05, 1}, /* Switch 5 */
{
0x6000, 0x06, 1}, /* Switch 6 */
{
0x6000, 0x07, 1}, /* Switch 7 */
{
0x6000, 0x08, 1}, /* Switch 8 */
{
0x0000, 0x00, 8}, /* Gap */
{
0x6020, 0x01, 1}, /* Underrange */
{
0x6020, 0x02, 1}, /* Overrange */
{
0x6020, 0x03, 2}, /* Limit 1 */
{
0x6020, 0x05, 2}, /* Limit 2 */
{
0x0000, 0x00, 8}, /* Gap */
{
0x1802, 0x07, 1}, /* TxPDOState */
{
0x1802, 0x09, 1}, /* TxPDO Toggle */
{
0x6020, 0x11, 16}, /* Analog input */
};
ec_pdo_info_t slave_stm_pdos[] = {
{
0x1601, 9, slave_stm_pdo_entries + 0}, /* DO RxPDO-Map */
{
0x1a00, 9, slave_stm_pdo_entries + 9}, /* DI TxPDO-Map */
{
0x1a02, 8, slave_stm_pdo_entries + 18}, /* AI TxPDO-Map */
};
//sm0 for MBoxOut;sm1 for MBoxIn;sm2 for Outputs;sm3 for Inputs
ec_sync_info_t slave_stm_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_stm_pdos + 0, EC_WD_ENABLE},
{
3, EC_DIR_INPUT, 2, slave_stm_pdos + 1, EC_WD_DISABLE},
{
0xff}
};
/***************************************************************************** * Realtime task ****************************************************************************/
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;
}
/****************************************************************************/
#define _ms 1000000
void my_task_proc(void *arg)
{
int cycle_counter = 0;
unsigned int blink = 0;
rt_task_set_periodic(NULL, TM_NOW, 1*_ms); // ns
while (cycle_counter<10000) {
rt_task_wait_period(NULL);
cycle_counter++;
// receive EtherCAT frames
ecrt_master_receive(master);
ecrt_domain_process(domain1);
rt_check_domain_state();
if (!(cycle_counter % 1000)) {
rt_check_master_state();
}
if (!(cycle_counter % 1000)) {
blink = !blink;
printf("ad input... %d\n",EC_READ_U16(domain1_pd + off_dig_out5));
}
EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out1, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out2, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out3, EC_READ_U8(domain1_pd + off_dig_out4));
// send process data
ecrt_domain_queue(domain1);
ecrt_master_send(master);
}
run = 0;
}
/**************************************************************************** * Signal handler ***************************************************************************/
void signal_handler(int sig)
{
run = 0;
}
/**************************************************************************** * Main function= ***************************************************************************/
int main(int argc, char *argv[])
{
int ret;
/* Perform auto-init of rt_print buffers if the task doesn't do so */
rt_print_auto_init(1);
// rt_print_init(1000,"LEE_RT");
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;
}
printf("Creating slave configurations...\n");
// Create configuration for bus coupler EK1100
ec_slave_config_t *sc;
sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
if (!sc) {
return -1;
}
// Get the slave server configuration
sc_el2008_1 = ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2008);
sc_el2008_2 = ecrt_master_slave_config(master, DigOutSlave02_Pos, Beckhoff_EL2008);
sc_el2008_3 = ecrt_master_slave_config(master, DigOutSlave03_Pos, Beckhoff_EL2008);
sc_stm = ecrt_master_slave_config(master, DigOutSlave04_Pos, stm32_lan9252);
// Process data SM To configure
ecrt_slave_config_pdos(sc_el2008_1, EC_END, slave_el2008_syncs);
ecrt_slave_config_pdos(sc_el2008_2, EC_END, slave_el2008_syncs);
ecrt_slave_config_pdos(sc_el2008_3, EC_END, slave_el2008_syncs);
ecrt_slave_config_pdos(sc_stm, EC_END, slave_stm_syncs);
// Create process data fields
domain1 = ecrt_master_create_domain(master);
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 domain1 data pointer.\n");
return -1;
}
ret = rt_task_create(&my_task, "my_task", 0, 99, 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;
}
makefile
CC = arm-linux-gnueabihf-gcc
INCPATH = -I./igh-output/include -I./xenomai-Output/include -I./xenomai-Output/include/posix
LIBS = -L./igh-output/lib -lethercat_rtdm -L./xenomai-Output/lib -lrtdm -lpthread_rt -lnative -lxenomai -lpthread -lrt -lm
LFLAGS = -Wl,@./xenomai-Output/lib/posix.wrappers
DEFINES = -D_GNU_SOURCE -D_REENTRANT -D__XENO__
CFLAGS = $(DEFINES)
TARGET = overwatch
####### Output directory
OBJECTS_DIR = ./
####### Files
SOURCES = overwatch.c
OBJECTS = overwatch.o
####### Build rules
$(TARGET): $(OBJECTS)
$(CC) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(LIBS)
####### Compile
$(OBJECTS): $(SOURCES)
$(CC) $(INCPATH) $(CFLAGS) -o $(OBJECTS) -c $(SOURCES)
clean:
rm overwatch *.o
The code of the control part
if (!(cycle_counter % 1000)) {
blink = !blink;
printf("ad input... %d\n",EC_READ_U16(domain1_pd + off_dig_out5));
}
EC_WRITE_U8(domain1_pd + off_dig_out0, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out1, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out2, blink ? 0xFF: 0x00);
EC_WRITE_U8(domain1_pd + off_dig_out3, EC_READ_U8(domain1_pd + off_dig_out4));
experimental result


边栏推荐
- Stack awareness - stack overflow instance (ret2libc)
- Application architecture principles
- Development of digital collection system and construction of NFT artwork trading platform
- list的模拟实现
- Yrcloudfile of Yanrong technology has completed the compatibility certification with ANTP to jointly create a new blueprint for storage
- 超分之RLSP
- The source code of the online live broadcast system enables you to request the list interface and touch the bottom page to load
- 堆栈认知——堆简介
- National administrative division
- Seventy years of neural network: review and Prospect
猜你喜欢

ViT杀疯了,10+视觉Transformer模型详解
![[Oracle] is there a](/img/21/3c481be79a4f19b06a2a93ded4159f.png)
[Oracle] is there a "time" data type in oracle-- Research on Oracle data types

Interceptor to realize web user login

润迈德医疗通过上市聆讯:预计亏损将增加,霍云飞兄弟持股约33%

Addition of 3DE grid coordinate points and objects

Google play application signature key certificate, upload signature certificate difference

Nacos registry ----- built and used from 0

Simulation Implementation of list

Why is rediscluster designed with 16384 slots?

The way for programmers to learn
随机推荐
Common setting modes
Nacos注册中心-----从0开始搭建和使用
Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition
EtherCAT igh源码的ecrt_slave_config_dc()函数的理解。
不是一流大学毕业,却通过自学软件测试,进了阿里年薪初始22K
海恩法则和费曼学习法
POSIX信号量
Postman association to complete interface automation test
I am about to enter my senior year and begin my most sincere confession
Nacos registry ----- built and used from 0
Convert longitude and latitude to distance
3DE 三維模型視圖看不到怎麼調整
How to write technical documents software engineering at Google
大型网站技术架构 | 信息加密技术及密匙安全管理
How to adjust 3DE 3D model view if you can't see it
性能测试---locust的on_start 和 on_stop 方法
Jetpack compose management status (I)
Stack cognition - stack overflow instance (ret2shellcode)
堆栈认知——栈溢出实例(ret2shellcode)
堆栈认知——栈溢出实例(ret2libc)