mirror of
https://github.com/apache/nuttx.git
synced 2026-05-25 18:27:56 +08:00
sched/clock: support using CLOCKFD to call clock_adjtime
This patch implements clock_adjtime() with CLOCKFD support, enabling
precise time and frequency adjustments for PTP clocks through the
standard POSIX clock API.
Key changes include:
1. New clock_adjtime() system call:
- Added sched/clock/clock_adjtime.c implementation
- Provides POSIX-compliant time adjustment interface
- Supports both standard clocks and CLOCKFD-based dynamic clocks
- Added CONFIG_CLOCK_ADJTIME Kconfig option
2. CLOCKFD support in clock_adjtime():
- Detects CLOCKFD-encoded clockids via CLOCKFD_TO_FD() check
- Extracts file descriptor and validates through fs_getfilep()
- Issues PTP_CLOCK_ADJTIME ioctl to underlying PTP clock device
- Returns clock status and adjustment results via struct timex
3. Enhanced struct timex support:
- Extended include/sys/timex.h with additional ADJ_* flags
- Added ADJ_OFFSET, ADJ_FREQUENCY, ADJ_MAXERROR, ADJ_ESTERROR
- Added ADJ_STATUS, ADJ_TIMECONST, ADJ_TAI, ADJ_SETOFFSET
- Added ADJ_MICRO, ADJ_NANO for time unit selection
- Added STA_* status flags for clock state reporting
4. Clock operations supported:
- ADJ_SETOFFSET: Apply time offset adjustment
- ADJ_FREQUENCY: Adjust clock frequency (PPM)
- ADJ_MAXERROR: Set maximum error estimate
- ADJ_ESTERROR: Set estimated error
- ADJ_STATUS: Modify clock status bits
- ADJ_NANO: Use nanosecond resolution
- ADJ_SETOFFSET: Set absolute time offset
5. API declarations:
- Added clock_adjtime() prototype in include/nuttx/clock.h
- Enabled by CONFIG_CLOCK_ADJTIME configuration
- Compatible with Linux clock_adjtime() interface
Usage example:
int fd = open("/dev/ptp0", O_RDWR);
struct timex tx = {0};
tx.modes = ADJ_FREQUENCY;
tx.freq = 10000000; /* Adjust frequency by 10 PPM */
clock_adjtime(CLOCKFD(fd), &tx);
close(fd);
This completes the PTP clock framework's POSIX clock API integration,
providing comprehensive time control capabilities for precision timing
applications including PTP synchronization daemons (ptp4l, timemaster).
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
committed by
Alan C. Assis
parent
6802d3510b
commit
752a405f86
@@ -90,6 +90,37 @@ extern "C"
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: clock_adjtime
|
||||
*
|
||||
* Description:
|
||||
* Adjust the frequency and/or phase of a clock.
|
||||
* This function allows the adjustment of the frequency and/or phase of a
|
||||
* specified clock. It can be used to synchronize the clock with an
|
||||
* external time source or to apply a frequency offset.
|
||||
*
|
||||
* Input Parameters:
|
||||
* clk_id - The identifier of the clock to be adjusted. This is typically
|
||||
* one of the predefined clock IDs such as CLOCK_REALTIME,
|
||||
* CLOCK_MONOTONIC, or CLOCK_BOOTTIME.
|
||||
*
|
||||
* buf - A pointer to a `timex` structure that specifies the adjustment
|
||||
* parameters. This structure includes fields for the frequency
|
||||
* adjustment (`freq`), the maximum frequency error (`maxerror`),
|
||||
* the estimated error (`esterror`), the phase offset (`offset`),
|
||||
* and flags to indicate the type of adjustment (`status`).
|
||||
*
|
||||
* Returned Value:
|
||||
* Return On success, the function returns 0. On error, it returns
|
||||
* -1 and sets 'errno` to indicate the specific error that
|
||||
* occurred.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_CLOCK_ADJTIME
|
||||
int clock_adjtime(clockid_t clk_id, FAR struct timex *buf);
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user