13#ifndef ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
14#define ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
38#if !defined(PTP_CLOCK_NAME)
39#define PTP_CLOCK_NAME "PTP_CLOCK"
43#define PTP_CLOCK_SCALED_PPM_SHIFT 16
46#define PTP_CLOCK_SCALED_PPM_ONE (INT64_C(1) << PTP_CLOCK_SCALED_PPM_SHIFT)
127 int64_t rem = (scaled_ppm % 8192) * 125;
129 return (scaled_ppm / 8192) * 125 + (rem + ((rem < 0) ? -4096 : 4096)) / 8192;
154 if ((scaled_ppm < -limit) || (scaled_ppm > limit)) {
163 diff = (product.
high << 42) | (product.
low >> 22);
164 frac = ((diff % 15625U) << 22) | (product.
low &
BIT64_MASK(22));
166 if (frac >= (UINT64_C(15625) << 21)) {
170 value = (scaled_ppm < 0) ? (base - diff) : (base + diff);
206static inline int z_impl_ptp_clock_get(
const struct device *dev,
252 scaled_ppm += (scaled_ppm < 0.0) ? -0.5 : 0.5;
284 return api->
rate_adjust(dev, 1.0 + (
double)scaled_ppm /
296#include <zephyr/syscalls/ptp_clock.h>
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1505
static int64_t ptp_clock_scaled_ppm_to_ppb(int64_t scaled_ppm)
Convert a scaled parts-per-million rate offset to parts per billion.
Definition ptp_clock.h:124
int(* ptp_clock_api_get_t)(const struct device *dev, struct net_ptp_time *tm)
Get the time of the PTP clock.
Definition ptp_clock.h:63
static int ptp_clock_adjust_by_scaled_ppm(uint32_t base, int64_t scaled_ppm, uint32_t *result)
Apply a scaled parts-per-million rate offset to a nominal value.
Definition ptp_clock.h:145
int(* ptp_clock_api_adjust_t)(const struct device *dev, int increment)
Adjust the PTP clock time.
Definition ptp_clock.h:69
int(* ptp_clock_api_adjust_rate_t)(const struct device *dev, int64_t scaled_ppm)
Adjust the PTP clock rate by an offset from its nominal frequency.
Definition ptp_clock.h:83
int(* ptp_clock_api_rate_adjust_t)(const struct device *dev, double ratio)
Adjust the PTP clock rate ratio based on its nominal frequency.
Definition ptp_clock.h:77
int(* ptp_clock_api_set_t)(const struct device *dev, struct net_ptp_time *tm)
Set the time of the PTP clock.
Definition ptp_clock.h:57
static int ptp_clock_rate_adjust(const struct device *dev, double rate)
Adjust the PTP clock rate ratio based on its nominal frequency.
Definition ptp_clock.h:240
static int ptp_clock_set(const struct device *dev, struct net_ptp_time *tm)
Set the time of the PTP clock.
Definition ptp_clock.h:190
int ptp_clock_get(const struct device *dev, struct net_ptp_time *tm)
Get the time of the PTP clock.
#define PTP_CLOCK_SCALED_PPM_ONE
One part per million in the scaled rate-adjustment representation.
Definition ptp_clock.h:46
static int ptp_clock_adjust_rate(const struct device *dev, int64_t scaled_ppm)
Adjust the PTP clock rate by an offset from its nominal frequency.
Definition ptp_clock.h:277
static int ptp_clock_adjust(const struct device *dev, int increment)
Adjust the PTP clock time.
Definition ptp_clock.h:220
#define BIT64_MASK(n)
64-bit bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.
Definition util_macro.h:80
#define ERANGE
Result too large.
Definition errno.h:73
Public functions for the Precision Time Protocol time specification.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
#define INT64_MIN
Definition stdint.h:25
#define UINT32_MAX
Definition stdint.h:29
__INT64_TYPE__ int64_t
Definition stdint.h:75
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
128-bit integer structure.
Definition math_extras.h:189
uint64_t high
High-order 64 bits (includes sign bit).
Definition math_extras.h:193
uint64_t low
Low-order 64 bits.
Definition math_extras.h:191
(Generalized) Precision Time Protocol Timestamp format.
Definition ptp_time.h:111
<span class="mlabel">Driver Operations</span> PTP Clock driver operations
Definition ptp_clock.h:88
ptp_clock_api_adjust_rate_t adjust_rate
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition ptp_clock.h:114
ptp_clock_api_set_t set
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition ptp_clock.h:92
ptp_clock_api_get_t get
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition ptp_clock.h:96
ptp_clock_api_rate_adjust_t rate_adjust
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition ptp_clock.h:108
ptp_clock_api_adjust_t adjust
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition ptp_clock.h:100