diff --git a/src/dispatch.c b/src/dispatch.c index 601ee78..55d8327 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -791,3 +791,19 @@ int raw1394_read_cycle_timer(raw1394handle_t handle, return ieee1394_read_cycle_timer(handle->mode.ieee1394, cycle_timer, local_time); } + +int raw1394_read_cycle_timer_and_clock(raw1394handle_t handle, + u_int32_t *cycle_timer, u_int64_t *local_time, clockid_t clk_id) +{ + if (!handle) { + errno = EINVAL; + return -1; + } + if (handle->is_fw) + return fw_read_cycle_timer_and_clock(handle->mode.fw, + cycle_timer, local_time, clk_id); + else { + errno = ENOSYS; + return -1; + } +} diff --git a/src/fw-iso.c b/src/fw-iso.c index 15fa233..9599299 100644 --- a/src/fw-iso.c +++ b/src/fw-iso.c @@ -648,8 +648,8 @@ void fw_iso_shutdown(fw_handle_t handle) handle->iso.fd = -1; } -int fw_read_cycle_timer(fw_handle_t handle, - u_int32_t *cycle_timer, u_int64_t *local_time) +int fw_read_cycle_timer(fw_handle_t handle, u_int32_t *cycle_timer, + u_int64_t *local_time) { int err; struct fw_cdev_get_cycle_timer ctr = { 0 }; @@ -661,3 +661,18 @@ int fw_read_cycle_timer(fw_handle_t handle, } return err; } + +int fw_read_cycle_timer_and_clock(fw_handle_t handle, u_int32_t *cycle_timer, + u_int64_t *local_time, clockid_t clk_id) +{ + int err; + struct fw_cdev_get_cycle_timer2 ctr = {.clk_id = clk_id}; + + err = ioctl(handle->ioctl_fd, FW_CDEV_IOC_GET_CYCLE_TIMER2, &ctr); + if (!err) { + *cycle_timer = ctr.cycle_timer; + *local_time = ctr.tv_sec * 1000000ULL + + ctr.tv_nsec / 1000; + } + return err; +} diff --git a/src/fw.h b/src/fw.h index cb1df01..4fd660f 100644 --- a/src/fw.h +++ b/src/fw.h @@ -251,7 +251,9 @@ int fw_iso_recv_unlisten_channel(fw_handle_t handle, int fw_iso_recv_set_channel_mask(fw_handle_t handle, u_int64_t mask); void fw_iso_stop(fw_handle_t handle); void fw_iso_shutdown(fw_handle_t handle); -int fw_read_cycle_timer(fw_handle_t handle, - u_int32_t *cycle_timer, u_int64_t *local_time); +int fw_read_cycle_timer(fw_handle_t handle, u_int32_t *cycle_timer, + u_int64_t *local_time); +int fw_read_cycle_timer_and_clock(fw_handle_t handle, u_int32_t *cycle_timer, + u_int64_t *local_time, clockid_t clk_id); #endif diff --git a/src/raw1394.h b/src/raw1394.h index 312c3b3..a7a0df4 100644 --- a/src/raw1394.h +++ b/src/raw1394.h @@ -14,6 +14,7 @@ #define _LIBRAW1394_RAW1394_H #include +#include /* for clockid_t */ #define RAW1394_ARM_READ 1 #define RAW1394_ARM_WRITE 2 @@ -334,7 +335,22 @@ void raw1394_iso_shutdown(raw1394handle_t handle); * @cycle_timer: buffer for Isochronous Cycle Timer * @local_time: buffer for local system time in microseconds since Epoch * - * Simultaneously reads the cycle timer register together with the system clock. + * Same as raw1394_read_cycle_timer_and_clock() with clk_id = CLOCK_REALTIME, + * i.e. the non-monotonic system-wide clock. + * + * History: New function in libraw1394 v1.3.0. + **/ +int raw1394_read_cycle_timer(raw1394handle_t handle, + u_int32_t *cycle_timer, u_int64_t *local_time); + +/** + * raw1394_read_cycle_timer_and_clock - get the current value of the cycle timer + * @handle: libraw1394 handle + * @cycle_timer: buffer for Isochronous Cycle Timer + * @local_time: buffer for local system time in microseconds + * @clk_id: clock from which to get the system time + * + * Simultaneously reads the cycle timer register together with a system clock. * * Format of @cycle_timer, from MSB to LSB: 7 bits cycleSeconds (seconds, or * number of cycleCount rollovers), 13 bits cycleCount (isochronous cycles, or @@ -342,10 +358,20 @@ void raw1394_iso_shutdown(raw1394handle_t handle); * provided on some hardware). The union of cycleSeconds and cycleCount is the * current cycle number. The nominal duration of a cycle is 125 microseconds. * + * @clk_id chooses the system clock as in the clock_gettime() function. + * At least the clocks CLOCK_REALTIME, CLOCK_MONOTONIC, and CLOCK_MONOTONIC_RAW + * are supported by the underlying kernel call. + * + * In case of CLOCK_REALTIME, @local_time are microseconds since the Epoch. + * CLOCK_REALTIME is subject to resets; CLOCK_MONOTONIC is subject to gradual + * adjustments; CLOCK_MONOTONIC_RAW is the bare hardware clock. + * * Returns: the error code of the ioctl, or 0 if successful. + * + * History: New function in libraw1394 v2.1.0. **/ -int raw1394_read_cycle_timer(raw1394handle_t handle, - u_int32_t *cycle_timer, u_int64_t *local_time); +int raw1394_read_cycle_timer_and_clock(raw1394handle_t handle, + u_int32_t *cycle_timer, u_int64_t *local_time, clockid_t clk_id); typedef int raw1394_errcode_t; #define raw1394_make_errcode(ack, rcode) (((ack) << 16) | rcode) diff --git a/tools/testlibraw.c b/tools/testlibraw.c index 8fe8b26..b322a5d 100644 --- a/tools/testlibraw.c +++ b/tools/testlibraw.c @@ -194,12 +194,33 @@ read_cycle_timer(raw1394handle_t handle) perror("\n - raw1394_read_cycle_timer failed with error"); return; } - printf("\n - cycle timer: %d seconds, %d cycles, %d sub-cycles\n", ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff); seconds = local_time / 1000000; - printf(" local time: %lld us = %s", + printf(" local time from CLOCK_REALTIME: %lld us = %s", (unsigned long long)local_time, ctime(&seconds)); + + retval = raw1394_read_cycle_timer_and_clock(handle, &ct, &local_time, + CLOCK_MONOTONIC); + if (retval < 0) { + perror("\n raw1394_read_cycle_timer_and_clock failed with error"); + return; + } + printf(" cycle timer: %d seconds, %d cycles, %d sub-cycles\n", + ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff); + printf(" local time from CLOCK_MONOTONIC: %lld us\n", + (unsigned long long)local_time); + + retval = raw1394_read_cycle_timer_and_clock(handle, &ct, &local_time, + CLOCK_MONOTONIC_RAW); + if (retval < 0) { + perror("\n raw1394_read_cycle_timer_and_clock failed with error"); + return; + } + printf(" cycle timer: %d seconds, %d cycles, %d sub-cycles\n", + ct >> 25, (ct >> 12) & 0x1fff, ct & 0xfff); + printf(" local time from CLOCK_MONOTONIC_RAW: %lld us\n", + (unsigned long long)local_time); } int test_card(int card)