mirror of
https://gitlab.com/etherlab.org/ethercat.git
synced 2026-09-21 09:33:47 +08:00
Memory-mapped process data.
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "master.h"
|
||||
#include "master/ioctl.h"
|
||||
@@ -65,6 +66,9 @@ ec_master_t *ecrt_request_master(unsigned int master_index)
|
||||
return 0;
|
||||
}
|
||||
|
||||
master->process_data = NULL;
|
||||
master->process_data_size = 0;
|
||||
|
||||
snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
|
||||
|
||||
master->fd = open(path, O_RDWR);
|
||||
@@ -89,6 +93,10 @@ ec_master_t *ecrt_request_master(unsigned int master_index)
|
||||
|
||||
void ecrt_release_master(ec_master_t *master)
|
||||
{
|
||||
if (master->process_data) {
|
||||
munmap(master->process_data, master->process_data_size);
|
||||
}
|
||||
|
||||
close(master->fd);
|
||||
free(master);
|
||||
}
|
||||
|
||||
+33
-1
@@ -38,7 +38,15 @@
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "domain.h"
|
||||
#include "master.h"
|
||||
#include "master/ioctl.h"
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
@@ -68,19 +76,43 @@ int ecrt_domain_reg_pdo_entry_list(ec_domain_t *domain,
|
||||
|
||||
uint8_t *ecrt_domain_data(ec_domain_t *domain)
|
||||
{
|
||||
return 0;
|
||||
if (!domain->process_data) {
|
||||
int offset = 0;
|
||||
|
||||
offset = ioctl(domain->master->fd, EC_IOCTL_DOMAIN_OFFSET,
|
||||
domain->index);
|
||||
if (offset == -1) {
|
||||
fprintf(stderr, "Failed to get domain offset: %s\n",
|
||||
strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
domain->process_data = domain->master->process_data + offset;
|
||||
}
|
||||
|
||||
return domain->process_data;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
void ecrt_domain_process(ec_domain_t *domain)
|
||||
{
|
||||
if (ioctl(domain->master->fd, EC_IOCTL_DOMAIN_PROCESS,
|
||||
domain->index) == -1) {
|
||||
fprintf(stderr, "Failed to process domain offset: %s\n",
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
void ecrt_domain_queue(ec_domain_t *domain)
|
||||
{
|
||||
if (ioctl(domain->master->fd, EC_IOCTL_DOMAIN_QUEUE,
|
||||
domain->index) == -1) {
|
||||
fprintf(stderr, "Failed to queue domain offset: %s\n",
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
struct ec_domain {
|
||||
unsigned int index;
|
||||
ec_master_t *master;
|
||||
uint8_t *process_data;
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
+18
-1
@@ -36,6 +36,7 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "master.h"
|
||||
#include "domain.h"
|
||||
@@ -64,6 +65,7 @@ ec_domain_t *ecrt_master_create_domain(ec_master_t *master)
|
||||
|
||||
domain->index = (unsigned int) index;
|
||||
domain->master = master;
|
||||
domain->process_data = NULL;
|
||||
return domain;
|
||||
}
|
||||
|
||||
@@ -106,12 +108,27 @@ ec_slave_config_t *ecrt_master_slave_config(ec_master_t *master,
|
||||
|
||||
int ecrt_master_activate(ec_master_t *master)
|
||||
{
|
||||
if (ioctl(master->fd, EC_IOCTL_ACTIVATE, NULL) == -1) {
|
||||
if (ioctl(master->fd, EC_IOCTL_ACTIVATE,
|
||||
&master->process_data_size) == -1) {
|
||||
fprintf(stderr, "Failed to activate master: %s\n",
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (master->process_data_size) {
|
||||
master->process_data = mmap(0, master->process_data_size,
|
||||
PROT_READ | PROT_WRITE, MAP_PRIVATE, master->fd, 0);
|
||||
if (master->process_data == MAP_FAILED) {
|
||||
fprintf(stderr, "Failed to map process data: %s", strerror(errno));
|
||||
master->process_data = NULL;
|
||||
master->process_data_size = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Access the mapped region to cause the initial page fault
|
||||
printf("pd: %x\n", master->process_data[0]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
|
||||
struct ec_master {
|
||||
int fd;
|
||||
uint8_t *process_data;
|
||||
size_t process_data_size;
|
||||
};
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
Reference in New Issue
Block a user