From d5124435f09bf1d80a4888a3a6d00bb7b6c4651c Mon Sep 17 00:00:00 2001 From: Hans-Erik Floryd Date: Thu, 26 Jun 2025 13:45:23 +0200 Subject: [PATCH] Let coetest/redtest build on other platforms Use the new osal functions to let tests build on all supported platforms. Change-Id: I6519ee55588e7bf0dff8d412679db268abbe86ce --- CMakeLists.txt | 4 ++-- samples/coetest/coetest.c | 27 ++++++++++----------------- samples/red_test/red_test.c | 27 ++++++++++----------------- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43c89ee..cf7e3d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,16 +158,16 @@ install(FILES ) if(SOEM_BUILD_UTILS) + add_subdirectory(samples/coetest) add_subdirectory(samples/eepromtool) add_subdirectory(samples/firm_update) + add_subdirectory(samples/red_test) add_subdirectory(samples/simple_ng) add_subdirectory(samples/simple_test) add_subdirectory(samples/slaveinfo) if (${CMAKE_SYSTEM_NAME} STREQUAL Linux) - add_subdirectory(samples/coetest) add_subdirectory(samples/eoe_test) - add_subdirectory(samples/red_test) endif() find_package (Python3 QUIET) diff --git a/samples/coetest/coetest.c b/samples/coetest/coetest.c index 5189713..8e5d362 100644 --- a/samples/coetest/coetest.c +++ b/samples/coetest/coetest.c @@ -68,21 +68,14 @@ static ecx_contextt ctx = { .userdata = NULL, }; -/* add ns to timespec */ -void add_timespec(struct timespec *ts, int64 addtime) +/* add ns to ec_timet */ +void add_time_ns(ec_timet *ts, int64 addtime) { - int64 sec, nsec; + ec_timet addts; - nsec = addtime % NSEC_PER_SEC; - sec = (addtime - nsec) / NSEC_PER_SEC; - ts->tv_sec += sec; - ts->tv_nsec += nsec; - if (ts->tv_nsec >= NSEC_PER_SEC) - { - nsec = ts->tv_nsec % NSEC_PER_SEC; - ts->tv_sec += (ts->tv_nsec - nsec) / NSEC_PER_SEC; - ts->tv_nsec = nsec; - } + addts.tv_nsec = addtime % NSEC_PER_SEC; + addts.tv_sec = (addtime - addts.tv_nsec) / NSEC_PER_SEC; + osal_timespecadd(ts, &addts, ts); } static float pgain = 0.01f; @@ -109,7 +102,7 @@ void ec_sync(int64 reftime, int64 cycletime, int64 *offsettime) /* RT EtherCAT thread */ OSAL_THREAD_FUNC_RT ecatthread(void) { - struct timespec ts, tleft; + ec_timet ts; int ht, wkc; static int64_t toff = 0; @@ -118,16 +111,16 @@ OSAL_THREAD_FUNC_RT ecatthread(void) { osal_usleep(100); } - clock_gettime(CLOCK_MONOTONIC, &ts); + osal_get_monotonic_time(&ts); ht = (ts.tv_nsec / 1000000) + 1; /* round to nearest ms */ ts.tv_nsec = ht * 1000000; ecx_send_processdata(&ctx); while (1) { /* calculate next cycle start */ - add_timespec(&ts, cycletime + toff); + add_time_ns(&ts, cycletime + toff); /* wait to cycle start */ - clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &tleft); + osal_monotonic_sleep(&ts); if (dorun > 0) { wkc = ecx_receive_processdata(&ctx, EC_TIMEOUTRET); diff --git a/samples/red_test/red_test.c b/samples/red_test/red_test.c index d86cf3a..b09c930 100644 --- a/samples/red_test/red_test.c +++ b/samples/red_test/red_test.c @@ -197,21 +197,14 @@ void redtest(char *ifname, char *ifname2) } } -/* add ns to timespec */ -void add_timespec(struct timespec *ts, int64 addtime) +/* add ns to ec_timet */ +void add_time_ns(ec_timet *ts, int64 addtime) { - int64 sec, nsec; + ec_timet addts; - nsec = addtime % NSEC_PER_SEC; - sec = (addtime - nsec) / NSEC_PER_SEC; - ts->tv_sec += sec; - ts->tv_nsec += nsec; - if (ts->tv_nsec >= NSEC_PER_SEC) - { - nsec = ts->tv_nsec % NSEC_PER_SEC; - ts->tv_sec += (ts->tv_nsec - nsec) / NSEC_PER_SEC; - ts->tv_nsec = nsec; - } + addts.tv_nsec = addtime % NSEC_PER_SEC; + addts.tv_sec = (addtime - addts.tv_nsec) / NSEC_PER_SEC; + osal_timespecadd(ts, &addts, ts); } /* PI calculation to get linux time synced to DC time */ @@ -240,11 +233,11 @@ void ec_sync(int64 reftime, int64 cycletime, int64 *offsettime) /* RT EtherCAT thread */ OSAL_THREAD_FUNC_RT ecatthread(void *ptr) { - struct timespec ts, tleft; + ec_timet ts; int ht; int64 cycletime; - clock_gettime(CLOCK_MONOTONIC, &ts); + osal_get_monotonic_time(&ts); ht = (ts.tv_nsec / 1000000) + 1; /* round to nearest ms */ ts.tv_nsec = ht * 1000000; if (ts.tv_nsec >= NSEC_PER_SEC) @@ -259,9 +252,9 @@ OSAL_THREAD_FUNC_RT ecatthread(void *ptr) while (1) { /* calculate next cycle start */ - add_timespec(&ts, cycletime + toff); + add_time_ns(&ts, cycletime + toff); /* wait to cycle start */ - clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &tleft); + osal_monotonic_sleep(&ts); if (dorun > 0) { wkc = ecx_receive_processdata(&ctx, EC_TIMEOUTRET); -- 2.43.0