Skip to content

Commit

Permalink
wip on xeno3
Browse files Browse the repository at this point in the history
make lib static

remove require 3.1 xenomai version

fix simple_ng compilation
  • Loading branch information
alessiomargan committed Sep 4, 2024
1 parent 83c6264 commit 296bf67
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 21 deletions.
40 changes: 28 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ if(WIN32)
set(OS_LIBS wpcap.lib Packet.lib Ws2_32.lib Winmm.lib)
elseif(UNIX AND NOT APPLE)
set(OS "linux")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
set(OS_LIBS pthread rt)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
#set(OS_LIBS pthread rt)
elseif(APPLE)
# This must come *before* linux or MacOSX will identify as Unix.
set(OS "macosx")
Expand All @@ -61,6 +61,22 @@ endif()

message(STATUS "OS is ${OS}")

option(CMAKE_USE_XENOMAI "Use Xenomai libraries" OFF)
if ( CMAKE_USE_XENOMAI )
find_package(Xenomai REQUIRED)
if(TARGET Xenomai::cobalt)
set(USE_COBALT True)
endif()
endif()
if ( Xenomai_FOUND )
set(OS_LIBS Xenomai::posix pthread rt)
set(LIB_TYPE _rt)
else()
set(OS_LIBS pthread rt)
set(LIB_TYPE _nrt)
endif()
set(SOEM_LIB_NAME soem${LIB_TYPE})

file(GLOB SOEM_SOURCES soem/*.c)
file(GLOB OSAL_SOURCES osal/${OS}/*.c)
file(GLOB OSHW_SOURCES oshw/${OS}/*.c)
Expand All @@ -69,33 +85,33 @@ file(GLOB SOEM_HEADERS soem/*.h)
file(GLOB OSAL_HEADERS osal/osal.h osal/${OS}/*.h)
file(GLOB OSHW_HEADERS oshw/${OS}/*.h)

add_library(soem STATIC
add_library(${SOEM_LIB_NAME} STATIC
${SOEM_SOURCES}
${OSAL_SOURCES}
${OSHW_SOURCES}
${OSHW_EXTRA_SOURCES})
target_link_libraries(soem ${OS_LIBS})
target_link_libraries(${SOEM_LIB_NAME} ${OS_LIBS})


target_include_directories(soem PUBLIC
target_include_directories(${SOEM_LIB_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/soem>
$<INSTALL_INTERFACE:include/soem>)

target_include_directories(soem PUBLIC
target_include_directories(${SOEM_LIB_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/osal>
$<INSTALL_INTERFACE:include/soem>)

target_include_directories(soem PUBLIC
target_include_directories(${SOEM_LIB_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/osal/${OS}>
$<INSTALL_INTERFACE:include/soem>)

target_include_directories(soem
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/oshw/${OS}>
$<INSTALL_INTERFACE:include/soem>
)
target_include_directories(${SOEM_LIB_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/oshw/${OS}>
$<INSTALL_INTERFACE:include/soem>)

message(STATUS "LIB_DIR: ${SOEM_LIB_INSTALL_DIR}")

install(TARGETS soem EXPORT soemConfig DESTINATION ${SOEM_LIB_INSTALL_DIR})
install(TARGETS ${SOEM_LIB_NAME} EXPORT soemConfig DESTINATION ${SOEM_LIB_INSTALL_DIR})

install(EXPORT soemConfig DESTINATION share/soem/cmake)

Expand Down
20 changes: 18 additions & 2 deletions oshw/linux/nicdrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
* This layer if fully transparent for the higher layers.
*/

#ifdef __COBALT__
#include <asm/ioctl.h>
#include <rtdm/rtdm.h>
//#include <rtnet.h>
#define RTIOC_TYPE_NETWORK RTDM_CLASS_NETWORK
#define RTNET_RTIOC_TIMEOUT _IOW(RTIOC_TYPE_NETWORK, 0x11, int64_t)
#endif

#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
Expand Down Expand Up @@ -91,7 +99,11 @@ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary)
{
int i;
int r, rval, ifindex;
#ifdef __COBALT__
int64_t timeout_ns;
#else
struct timeval timeout;
#endif
struct ifreq ifr;
struct sockaddr_ll sll;
int *psock;
Expand Down Expand Up @@ -144,14 +156,18 @@ int ecx_setupnic(ecx_portt *port, const char *ifname, int secondary)
}
/* we use RAW packet socket, with packet type ETH_P_ECAT */
*psock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ECAT));

#ifdef __COBALT__
timeout_ns = 10000LL;
if ( ioctl(*psock, RTNET_RTIOC_TIMEOUT, &timeout_ns) < 0 )
printf("ioctl RTNET_RTIOC_TIMEOUT failed\n");
#else
timeout.tv_sec = 0;
timeout.tv_usec = 1;
r = setsockopt(*psock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
r = setsockopt(*psock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
i = 1;
r = setsockopt(*psock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i));
/* connect socket to NIC by name */
#endif /* connect socket to NIC by name */
strcpy(ifr.ifr_name, ifname);
r = ioctl(*psock, SIOCGIFINDEX, &ifr);
ifindex = ifr.ifr_ifindex;
Expand Down
44 changes: 44 additions & 0 deletions soem/ethercatconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,49 @@ int ecx_detect_slaves(ecx_contextt *context)
return wkc;
}

static int reset_slaves(ecx_contextt *context) {

int wkc = 0;
uint16_t power_on_gpio = 1;
uint16_t power_in_gpio = 0;
uint8_t try_cnt = 3;

///
/// Motor Controller C2000 dsp is powered on by et1100
///
///
if ( context->ec_reset_micro ) {

power_on_gpio = 1;
wkc = ec_BWR(0x0000, 0x0f10, sizeof(power_on_gpio), &power_on_gpio, EC_TIMEOUTSAFE);
osal_usleep(250000);
power_on_gpio = 3;
wkc = ec_BWR(0x0000, 0x0f10, sizeof(power_on_gpio), &power_on_gpio, EC_TIMEOUTSAFE);
osal_usleep(250000);
power_on_gpio = 1;
wkc = ec_BWR(0x0000, 0x0f10, sizeof(power_on_gpio), &power_on_gpio, EC_TIMEOUTSAFE);
osal_usleep(500000);
//printf("wkc = %d\n",wkc);
while ( try_cnt-- ) {
ec_BRD(0x0000, 0x0f18, sizeof(power_in_gpio), &power_in_gpio, EC_TIMEOUTSAFE);
if ( power_in_gpio ) {
break;
}
}
if ( power_in_gpio == 0 ) {
printf("[ECat_master] Failed to reset slaves!\n");
//return 0;
}
//
osal_usleep(1000000);
printf("[ECat_master] POWER ON slaves.\n");
} else {
printf("[ECat_master] reset_micro set to FALSE \n");
}

return wkc;
}

static void ecx_set_slaves_to_default(ecx_contextt *context)
{
uint8 b;
Expand Down Expand Up @@ -321,6 +364,7 @@ int ecx_config_init(ecx_contextt *context, uint8 usetable)
wkc = ecx_detect_slaves(context);
if (wkc > 0)
{
reset_slaves(context);
ecx_set_slaves_to_default(context);
for (slave = 1; slave <= *(context->slavecount); slave++)
{
Expand Down
7 changes: 6 additions & 1 deletion soem/ethercatmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ static ec_eepromSMt ec_SM;
static ec_eepromFMMUt ec_FMMU;
/** Global variable TRUE if error available in error stack */
boolean EcatError = FALSE;

/** */
static boolean ec_reset_micro = TRUE;
int64 ec_DCtime;

ecx_portt ecx_port;
Expand Down Expand Up @@ -120,9 +121,13 @@ ecx_contextt ecx_context = {
NULL, // .EOEhook()
0, // .manualstatechange
NULL, // .userdata
&ec_reset_micro, // .reset_micro =
};
#endif

void ec_reset_micro_slaves(boolean reset_micro) {
ec_reset_micro = reset_micro;
}
/** Create list over available network adapters.
*
* @return First element in list over available network adapters.
Expand Down
3 changes: 3 additions & 0 deletions soem/ethercatmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ struct ecx_context
/** userdata, promotes application configuration esp. in EC_VER2 with multiple
* ec_context instances. Note: userdata memory is managed by application, not SOEM */
void *userdata;
/** */
boolean *ec_reset_micro;
};

#ifdef EC_VER1
Expand All @@ -443,6 +445,7 @@ extern ec_groupt ec_group[EC_MAXGROUP];
extern boolean EcatError;
extern int64 ec_DCtime;

void ec_reset_micro_slaves(boolean reset_micro);
void ec_pusherror(const ec_errort *Ec);
boolean ec_poperror(ec_errort *Ec);
boolean ec_iserror(void);
Expand Down
2 changes: 1 addition & 1 deletion test/linux/eepromtool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

set(SOURCES eepromtool.c)
add_executable(eepromtool ${SOURCES})
target_link_libraries(eepromtool soem)
target_link_libraries(eepromtool ${SOEM_LIB_NAME})
install(TARGETS eepromtool DESTINATION bin)
7 changes: 6 additions & 1 deletion test/linux/simple_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

set(SOURCES simple_test.c)
add_executable(simple_test ${SOURCES})
target_link_libraries(simple_test soem)
if ( Xenomai_FOUND )
xenomai_target_bootstrap(simple_test
SKINS posix
)
endif()
target_link_libraries(simple_test PRIVATE ${SOEM_LIB_NAME})
install(TARGETS simple_test DESTINATION bin)
21 changes: 20 additions & 1 deletion test/linux/simple_test/simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
#include <string.h>
#include <inttypes.h>

#ifdef __COBALT__
#include <cobalt/uapi/signal.h>
#include <xenomai/init.h>

#ifdef __cplusplus
extern "C" {
#endif
int xenomai_bootstrap_getargv(int *argc, char *const** argv);
#ifdef __cplusplus
}
#endif
#endif

#include "ethercat.h"

#define EC_TIMEOUTMON 500
Expand All @@ -34,6 +47,8 @@ void simpletest(char *ifname)

printf("Starting simple test\n");

ec_reset_micro_slaves(TRUE);

/* initialise SOEM, bind socket to ifname */
if (ec_init(ifname))
{
Expand Down Expand Up @@ -228,10 +243,14 @@ OSAL_THREAD_FUNC ecatcheck( void *ptr )
}
}

int main(int argc, char *argv[])
int main(int argc, char * const argv[])
{
printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n");

#ifdef __COBALT__
xenomai_bootstrap_getargv(&argc, &argv);
#endif

if (argc > 1)
{
/* create thread to handle slave error handling in OP */
Expand Down
7 changes: 6 additions & 1 deletion test/linux/slaveinfo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

set(SOURCES slaveinfo.c)
add_executable(slaveinfo ${SOURCES})
target_link_libraries(slaveinfo soem)
if ( Xenomai_FOUND )
xenomai_target_bootstrap(slaveinfo
SKINS posix
)
endif()
target_link_libraries(slaveinfo PRIVATE ${SOEM_LIB_NAME})
install(TARGETS slaveinfo DESTINATION bin)
21 changes: 20 additions & 1 deletion test/linux/slaveinfo/slaveinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
#include <string.h>
#include <inttypes.h>

#ifdef __COBALT__
#include <cobalt/uapi/signal.h>
#include <xenomai/init.h>

#ifdef __cplusplus
extern "C" {
#endif
int xenomai_bootstrap_getargv(int *argc, char *const** argv);
#ifdef __cplusplus
}
#endif
#endif

#include "ethercat.h"

char IOmap[4096];
Expand Down Expand Up @@ -590,6 +603,8 @@ void slaveinfo(char *ifname)

printf("Starting slaveinfo\n");

ec_reset_micro_slaves(TRUE);

/* initialise SOEM, bind socket to ifname */
if (ec_init(ifname))
{
Expand Down Expand Up @@ -696,11 +711,15 @@ void slaveinfo(char *ifname)

char ifbuf[1024];

int main(int argc, char *argv[])
int main(int argc, char *const argv[])
{
ec_adaptert * adapter = NULL;
printf("SOEM (Simple Open EtherCAT Master)\nSlaveinfo\n");

#ifdef __COBALT__
xenomai_bootstrap_getargv(&argc, &argv);
#endif

if (argc > 1)
{
if ((argc > 2) && (strncmp(argv[2], "-sdo", sizeof("-sdo")) == 0)) printSDO = TRUE;
Expand Down
7 changes: 6 additions & 1 deletion test/simple_ng/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
set(SOURCES simple_ng.c)
add_executable(simple_ng ${SOURCES})
target_link_libraries(simple_ng soem)
if ( Xenomai_FOUND )
xenomai_target_bootstrap(simple_test
SKINS posix
)
endif()
target_link_libraries(simple_ng PRIVATE ${SOEM_LIB_NAME})
install(TARGETS simple_ng DESTINATION bin)

0 comments on commit 296bf67

Please sign in to comment.