Skip to content

Commit

Permalink
Merge pull request #6 from sittner/machinekit-posix
Browse files Browse the repository at this point in the history
added posix support (linuxcnc sim target)
  • Loading branch information
sittner committed Mar 11, 2015
2 parents c9fa7e8 + 944fd86 commit d3f051e
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 111 deletions.
22 changes: 15 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
all:
$(MAKE) -C src all
.PHONY: all configure install clean

clean:
$(MAKE) -C src realclean
all: configure
@$(MAKE) -C src all

install: all
$(MAKE) -C src install-rt install-user
$(MAKE) -C examples install-examples
clean: configure
@$(MAKE) -C src clean
rm -f config.mk

install: configure
@$(MAKE) -C src install
@$(MAKE) -C examples install-examples

configure: config.mk

config.mk: configure.mk
@$(MAKE) -s -f configure.mk > config.mk

33 changes: 33 additions & 0 deletions configure.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
COMP ?= comp

.PHONY: configure
configure:
@echo "BUILDSYS = $(BUILDSYS)"
@echo "KERNELDIR = $(KERNELDIR)"
@echo "CC = $(CC)"
@echo "RTAI = $(RTAI)"
@echo "RTAIDIR = $(RTAIDIR)"
@echo "RTFLAGS = $(RTFLAGS)"
@echo "KERNELRELEASE = $(KERNELRELEASE)"
@echo "EXTRA_CFLAGS = $(EXTRA_CFLAGS)"
@echo "USE_RTLIBM = $(USE_RTLIBM)"
@echo "EMC2_HOME = $(EMC2_HOME)"
@echo "RUN_IN_PLACE = $(RUN_IN_PLACE)"
@echo "RTLIBDIR = $(RTLIBDIR)"
@echo "LIBDIR = $(LIBDIR)"
@echo "prefix = $(prefix)"

# include modinc
MODINC=$(shell $(COMP) --print-modinc)
ifeq (, $(MODINC))
$(error Unable to get modinc path)
endif

include $(MODINC)

# dirty workaround to get the RTAI directory
RTAIINCDIR = $(subst /rtai.h,,$(firstword $(wildcard $(foreach i,$(subst -I,,$(filter -I%,$(RTFLAGS))), $(i)/rtai.h))))
ifneq ($(RTAIINCDIR),)
RTAIDIR = $(realpath $(RTAIINCDIR)/..)
endif

4 changes: 1 addition & 3 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
MODINC=$(shell comp --print-modinc)
include ../config.mk

SUBDIRS = generic-complex swm-fm45a

include $(MODINC)

install-examples:
mkdir -p $(DESTDIR)$(EMC2_HOME)/share/linuxcnc-ethercat/examples
cp -R $(SUBDIRS) $(DESTDIR)$(EMC2_HOME)/share/linuxcnc-ethercat/examples
Expand Down
20 changes: 20 additions & 0 deletions src/Kbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
obj-m += lcec.o
lcec-objs := \
lcec_main.o \
lcec_generic.o \
lcec_el1xxx.o \
lcec_el2521.o \
lcec_el2xxx.o \
lcec_el31x2.o \
lcec_el40x1.o \
lcec_el40x2.o \
lcec_el41x2.o \
lcec_el5101.o \
lcec_el5151.o \
lcec_el5152.o \
lcec_el7342.o \
lcec_el95xx.o \
lcec_em7004.o \
lcec_stmds5k.o \
lcec_deasda.o

59 changes: 10 additions & 49 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,53 +1,14 @@
MODINC=$(shell comp --print-modinc)
.PHONY: all install clean

all: rt user
all:
@$(MAKE) -f user.mk all
@$(MAKE) -f realtime.mk all

realclean:
rm -f rm *.ko *.mod.c *.o .*.cmd
rm -f modules.order Module.symvers
rm -rf .tmp_versions
rm -f lcec_conf
install:
@$(MAKE) -f user.mk install
@$(MAKE) -f realtime.mk install

obj-m += lcec.o
lcec-objs := \
lcec_main.o \
lcec_generic.o \
lcec_el1xxx.o \
lcec_el2521.o \
lcec_el2xxx.o \
lcec_el31x2.o \
lcec_el40x1.o \
lcec_el40x2.o \
lcec_el41x2.o \
lcec_el5101.o \
lcec_el5151.o \
lcec_el5152.o \
lcec_el7342.o \
lcec_el95xx.o \
lcec_em7004.o \
lcec_stmds5k.o \
lcec_deasda.o \
$(MATHSTUB)

include $(MODINC)

# dirty workaround to get the RTAI directory
RTAI_INCLUDE_DIR = $(subst /rtai.h,,$(firstword $(wildcard $(foreach i,$(subst -I,,$(filter -I%,$(RTFLAGS))), $(i)/rtai.h))))
RTAI_DIR = $(RTAI_INCLUDE_DIR)/..

user: lcec_conf

lcec_conf: lcec_conf.c
gcc $(EXTRA_CFLAGS) -Wframe-larger-than=16384 -URTAPI -U__MODULE__ -DULAPI -Os -o $@ $< -Wl,-rpath,$(LIBDIR) -L$(LIBDIR) -llinuxcnchal -lexpat

install-user:
mkdir -p $(DESTDIR)$(EMC2_HOME)/bin
cp lcec_conf $(DESTDIR)$(EMC2_HOME)/bin

rt:
$(MAKE) KBUILD_EXTRA_SYMBOLS="$(RTLIBDIR)/Module.symvers $(RTAI_DIR)/modules/ethercat/Module.symvers" -C $(KERNELDIR) SUBDIRS=`pwd` CC=$(CC) V=0 modules

install-rt:
mkdir -p $(DESTDIR)$(RTLIBDIR)
$(MAKE) install
clean:
@$(MAKE) -f user.mk $@
@$(MAKE) -f realtime.mk $@

4 changes: 1 addition & 3 deletions src/lcec.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

#include "hal.h"

#include "rtapi.h"
#include "rtapi_ctype.h"
#include "rtapi_app.h"
#include "rtapi_string.h"
#include "rtapi_math.h"

Expand Down Expand Up @@ -64,7 +62,7 @@ do { \
#define LCEC_DELTA_VID 0x000001dd

// SDO request timeout (ms)
#define LCEC_SDO_REQ_TIMEOUT 1000
#define LCEC_SDO_REQ_TIMEOUT LCEC_MS_TO_TICKS(1000)

struct lcec_master;
struct lcec_slave;
Expand Down
3 changes: 2 additions & 1 deletion src/lcec_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "rtapi.h"
#include "hal.h"

#include "lcec_rtapi.h"
#include "lcec_conf.h"

#define BUFFSIZE 8192
Expand Down Expand Up @@ -297,7 +298,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "%s: ERROR: couldn't allocate user/RT shared memory\n", modname);
goto fail4;
}
if (rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0) {
if (lcec_rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0) {
fprintf(stderr, "%s: ERROR: couldn't map user/RT shared memory\n", modname);
goto fail5;
}
Expand Down
52 changes: 27 additions & 25 deletions src/lcec_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "lcec_stmds5k.h"
#include "lcec_deasda.h"

#include "rtapi_app.h"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sascha Ittner <[email protected]>");
MODULE_DESCRIPTION("Driver for EtherCAT devices");
Expand Down Expand Up @@ -282,7 +284,7 @@ int rtapi_app_main(void) {
}

// initialize application time
RTAPI_GETTIMEOFDAY(&tv);
lcec_gettimeofday(&tv);
master->app_time = EC_TIMEVAL2NANO(tv);

// activating master
Expand Down Expand Up @@ -391,7 +393,7 @@ int lcec_parse_config(void) {
rtapi_print_msg (RTAPI_MSG_ERR, LCEC_MSG_PFX "couldn't allocate user/RT shared memory\n");
goto fail0;
}
if (rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0 ) {
if (lcec_rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0 ) {
rtapi_print_msg (RTAPI_MSG_ERR, LCEC_MSG_PFX "couldn't map user/RT shared memory\n");
goto fail1;
}
Expand All @@ -411,7 +413,7 @@ int lcec_parse_config(void) {
rtapi_print_msg (RTAPI_MSG_ERR, LCEC_MSG_PFX "couldn't allocate user/RT shared memory\n");
goto fail0;
}
if (rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0 ) {
if (lcec_rtapi_shmem_getptr(shmem_id, &shmem_ptr) < 0 ) {
rtapi_print_msg (RTAPI_MSG_ERR, LCEC_MSG_PFX "couldn't map user/RT shared memory\n");
goto fail1;
}
Expand All @@ -438,7 +440,7 @@ int lcec_parse_config(void) {
conf += sizeof(LCEC_CONF_MASTER_T);

// alloc master memory
master = kzalloc(sizeof(lcec_master_t), GFP_KERNEL);
master = lcec_zalloc(sizeof(lcec_master_t));
if (master == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate master %d structure memory\n", master_conf->index);
goto fail2;
Expand Down Expand Up @@ -481,7 +483,7 @@ int lcec_parse_config(void) {
}

// create new slave
slave = kzalloc(sizeof(lcec_slave_t), GFP_KERNEL);
slave = lcec_zalloc(sizeof(lcec_slave_t));
if (slave == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s structure memory\n", master->name, slave_conf->name);
goto fail2;
Expand Down Expand Up @@ -524,21 +526,21 @@ int lcec_parse_config(void) {
memset(generic_hal_data, 0, sizeof(lcec_generic_pin_t) * slave_conf->pdoMappingCount);

// alloc pdo entry memory
generic_pdo_entries = kzalloc(sizeof(ec_pdo_entry_info_t) * slave_conf->pdoEntryCount, GFP_KERNEL);
generic_pdo_entries = lcec_zalloc(sizeof(ec_pdo_entry_info_t) * slave_conf->pdoEntryCount);
if (generic_pdo_entries == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s generic pdo entry memory\n", master->name, slave_conf->name);
goto fail2;
}

// alloc pdo memory
generic_pdos = kzalloc(sizeof(ec_pdo_info_t) * slave_conf->pdoCount, GFP_KERNEL);
generic_pdos = lcec_zalloc(sizeof(ec_pdo_info_t) * slave_conf->pdoCount);
if (generic_pdos == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s generic pdo memory\n", master->name, slave_conf->name);
goto fail2;
}

// alloc sync manager memory
generic_sync_managers = kzalloc(sizeof(ec_sync_info_t) * (slave_conf->syncManagerCount + 1), GFP_KERNEL);
generic_sync_managers = lcec_zalloc(sizeof(ec_sync_info_t) * (slave_conf->syncManagerCount + 1));
if (generic_sync_managers == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s generic sync manager memory\n", master->name, slave_conf->name);
goto fail2;
Expand All @@ -548,7 +550,7 @@ int lcec_parse_config(void) {

// alloc sdo config memory
if (slave_conf->sdoConfigLength > 0) {
sdo_config = kzalloc(slave_conf->sdoConfigLength + sizeof(lcec_slave_sdoconf_t), GFP_KERNEL);
sdo_config = lcec_zalloc(slave_conf->sdoConfigLength + sizeof(lcec_slave_sdoconf_t));
if (sdo_config == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s generic pdo entry memory\n", master->name, slave_conf->name);
goto fail2;
Expand Down Expand Up @@ -591,7 +593,7 @@ int lcec_parse_config(void) {
}

// create new dc config
dc = kzalloc(sizeof(lcec_slave_dc_t), GFP_KERNEL);
dc = lcec_zalloc(sizeof(lcec_slave_dc_t));
if (dc == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s dc config memory\n", master->name, slave->name);
goto fail2;
Expand Down Expand Up @@ -626,7 +628,7 @@ int lcec_parse_config(void) {
}

// create new wd config
wd = kzalloc(sizeof(lcec_slave_watchdog_t), GFP_KERNEL);
wd = lcec_zalloc(sizeof(lcec_slave_watchdog_t));
if (wd == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate slave %s.%s watchdog config memory\n", master->name, slave->name);
goto fail2;
Expand Down Expand Up @@ -781,7 +783,7 @@ int lcec_parse_config(void) {

// allocate PDO entity memory
for (master = first_master; master != NULL; master = master->next) {
pdo_entry_regs = kzalloc(sizeof(ec_pdo_entry_reg_t) * (master->pdo_entry_count + 1), GFP_KERNEL);
pdo_entry_regs = lcec_zalloc(sizeof(ec_pdo_entry_reg_t) * (master->pdo_entry_count + 1));
if (pdo_entry_regs == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR, LCEC_MSG_PFX "Unable to allocate master %s PDO entry memory\n", master->name);
goto fail2;
Expand Down Expand Up @@ -820,24 +822,24 @@ void lcec_clear_config(void) {

// free slave
if (slave->sdo_config != NULL) {
kfree(slave->sdo_config);
lcec_free(slave->sdo_config);
}
if (slave->generic_pdo_entries != NULL) {
kfree(slave->generic_pdo_entries);
lcec_free(slave->generic_pdo_entries);
}
if (slave->generic_pdos != NULL) {
kfree(slave->generic_pdos);
lcec_free(slave->generic_pdos);
}
if (slave->generic_sync_managers != NULL) {
kfree(slave->generic_sync_managers);
lcec_free(slave->generic_sync_managers);
}
if (slave->dc_conf != NULL) {
kfree(slave->dc_conf);
lcec_free(slave->dc_conf);
}
if (slave->wd_conf != NULL) {
kfree(slave->wd_conf);
lcec_free(slave->wd_conf);
}
kfree(slave);
lcec_free(slave);
slave = prev_slave;
}

Expand All @@ -848,11 +850,11 @@ void lcec_clear_config(void) {

// free PDO entry memory
if (master->pdo_entry_regs != NULL) {
kfree(master->pdo_entry_regs);
lcec_free(master->pdo_entry_regs);
}

// free master
kfree(master);
lcec_free(master);
master = prev_master;
}
}
Expand Down Expand Up @@ -1086,7 +1088,7 @@ ec_sdo_request_t *lcec_read_sdo(struct lcec_slave *slave, uint16_t index, uint8_
lcec_master_t *master = slave->master;
ec_sdo_request_t *sdo;
ec_request_state_t sdo_state;
unsigned long jiffies_start;
long ticks_start;

// create request
if (!(sdo = ecrt_slave_config_create_sdo_request(slave->config, index, subindex, size))) {
Expand All @@ -1101,9 +1103,9 @@ ec_sdo_request_t *lcec_read_sdo(struct lcec_slave *slave, uint16_t index, uint8_
ecrt_sdo_request_read(sdo);

// wait for completition (master's time out does not work here. why???)
jiffies_start = jiffies;
while ((sdo_state = ecrt_sdo_request_state(sdo)) == EC_REQUEST_BUSY && (jiffies - jiffies_start) < (HZ * LCEC_SDO_REQ_TIMEOUT / 1000)) {
schedule();
ticks_start = lcec_get_ticks();
while ((sdo_state = ecrt_sdo_request_state(sdo)) == EC_REQUEST_BUSY && (lcec_get_ticks() - ticks_start) < LCEC_SDO_REQ_TIMEOUT) {
lcec_schedule();
}

// check state
Expand Down
Loading

0 comments on commit d3f051e

Please sign in to comment.