mirror of
https://github.com/apache/nuttx.git
synced 2026-05-10 23:40:21 +08:00
752a405f86
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>
130 lines
5.2 KiB
C
130 lines
5.2 KiB
C
/****************************************************************************
|
|
* include/sys/timex.h
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright ownership. The
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#ifndef __INCLUDE_SYS_TIMEX_H
|
|
#define __INCLUDE_SYS_TIMEX_H
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <sys/time.h>
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/* Mode codes (timex.mode) */
|
|
|
|
#define ADJ_OFFSET 0x0001 /* time offset */
|
|
#define ADJ_FREQUENCY 0x0002 /* frequency offset */
|
|
#define ADJ_MAXERROR 0x0004 /* maximum time error */
|
|
#define ADJ_ESTERROR 0x0008 /* estimated time error */
|
|
#define ADJ_STATUS 0x0010 /* clock status */
|
|
#define ADJ_TIMECONST 0x0020 /* pll time constant */
|
|
#define ADJ_TAI 0x0080 /* set TAI offset */
|
|
#define ADJ_SETOFFSET 0x0100 /* add 'time' to current time */
|
|
#define ADJ_MICRO 0x1000 /* select microsecond resolution */
|
|
#define ADJ_NANO 0x2000 /* select nanosecond resolution */
|
|
#define ADJ_TICK 0x4000 /* tick value */
|
|
#define ADJ_OFFSET_SINGLESHOT 0x8001 /* old-fashioned adjtime */
|
|
#define ADJ_OFFSET_SS_READ 0xa001 /* read-only adjtime */
|
|
|
|
/****************************************************************************
|
|
* Public Types
|
|
****************************************************************************/
|
|
|
|
struct timex
|
|
{
|
|
unsigned int modes; /* mode selector */
|
|
long offset; /* time offset (usec) */
|
|
long freq; /* frequency offset (scaled ppm) */
|
|
long maxerror; /* maximum error (usec) */
|
|
long esterror; /* estimated error (usec) */
|
|
int status; /* clock command/status */
|
|
long constant; /* pll time constant */
|
|
long precision; /* clock precision (usec) (read only) */
|
|
long tolerance; /* clock frequency tolerance (ppm) (read only) */
|
|
struct timeval time; /* (read only, except for ADJ_SETOFFSET) */
|
|
long tick; /* (modified) usecs between clock ticks */
|
|
long ppsfreq; /* pps frequency (scaled ppm) (ro) */
|
|
long jitter; /* pps jitter (us) (ro) */
|
|
int shift; /* interval duration (s) (shift) (ro) */
|
|
long stabil; /* pps stability (scaled ppm) (ro) */
|
|
long jitcnt; /* jitter limit exceeded (ro) */
|
|
long calcnt; /* calibration intervals (ro) */
|
|
long errcnt; /* calibration errors (ro) */
|
|
long stbcnt; /* stability limit exceeded (ro) */
|
|
int tai; /* TAI offset (ro) */
|
|
};
|
|
|
|
/****************************************************************************
|
|
* Public Function Prototypes
|
|
****************************************************************************/
|
|
|
|
#undef EXTERN
|
|
#if defined(__cplusplus)
|
|
#define EXTERN extern "C"
|
|
extern "C"
|
|
{
|
|
#else
|
|
#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)
|
|
}
|
|
#endif
|
|
|
|
#endif /* __INCLUDE_SYS_TIMEX_H */
|