Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ptp_clock.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
14#define ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
15
24
25#include <errno.h>
26#include <zephyr/kernel.h>
27#include <stdint.h>
28#include <zephyr/device.h>
30#include <zephyr/sys/util.h>
31#include <zephyr/net/ptp_time.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* Name of the PTP clock driver */
38#if !defined(PTP_CLOCK_NAME)
39#define PTP_CLOCK_NAME "PTP_CLOCK"
40#endif
41
43#define PTP_CLOCK_SCALED_PPM_SHIFT 16
44
46#define PTP_CLOCK_SCALED_PPM_ONE (INT64_C(1) << PTP_CLOCK_SCALED_PPM_SHIFT)
47
52
57typedef int (*ptp_clock_api_set_t)(const struct device *dev, struct net_ptp_time *tm);
58
63typedef int (*ptp_clock_api_get_t)(const struct device *dev, struct net_ptp_time *tm);
64
69typedef int (*ptp_clock_api_adjust_t)(const struct device *dev, int increment);
70
77typedef int (*ptp_clock_api_rate_adjust_t)(const struct device *dev, double ratio);
78
83typedef int (*ptp_clock_api_adjust_rate_t)(const struct device *dev, int64_t scaled_ppm);
84
116
125{
126 /* ppb = scaled_ppm * 1000 / 2^16 = scaled_ppm * 125 / 2^13 */
127 int64_t rem = (scaled_ppm % 8192) * 125;
128
129 return (scaled_ppm / 8192) * 125 + (rem + ((rem < 0) ? -4096 : 4096)) / 8192;
130}
131
145static inline int ptp_clock_adjust_by_scaled_ppm(uint32_t base, int64_t scaled_ppm,
146 uint32_t *result)
147{
148 const int64_t limit = 1000000 * PTP_CLOCK_SCALED_PPM_ONE;
149 int128_t product;
150 uint64_t value;
151 uint64_t diff;
152 uint64_t frac;
153
154 if ((scaled_ppm < -limit) || (scaled_ppm > limit)) {
155 return -ERANGE;
156 }
157
158 /*
159 * diff = base * |scaled_ppm| / (1000000 * 2^16), where 1000000 * 2^16 = 15625 * 2^22.
160 * The product can exceed 64 bits, but fits again once it is shifted right by 22.
161 */
162 i128_multiply_i64_i64(base, (scaled_ppm < 0) ? -scaled_ppm : scaled_ppm, &product);
163 diff = (product.high << 42) | (product.low >> 22);
164 frac = ((diff % 15625U) << 22) | (product.low & BIT64_MASK(22));
165 diff /= 15625U;
166 if (frac >= (UINT64_C(15625) << 21)) {
167 diff++;
168 }
169
170 value = (scaled_ppm < 0) ? (base - diff) : (base + diff);
171 if (value > UINT32_MAX) {
172 return -ERANGE;
173 }
174
175 *result = (uint32_t)value;
176
177 return 0;
178}
179
181
190static inline int ptp_clock_set(const struct device *dev,
191 struct net_ptp_time *tm)
192{
193 return DEVICE_API_GET(ptp_clock, dev)->set(dev, tm);
194}
195
204__syscall int ptp_clock_get(const struct device *dev, struct net_ptp_time *tm);
205
206static inline int z_impl_ptp_clock_get(const struct device *dev,
207 struct net_ptp_time *tm)
208{
209 return DEVICE_API_GET(ptp_clock, dev)->get(dev, tm);
210}
211
220static inline int ptp_clock_adjust(const struct device *dev, int increment)
221{
222 return DEVICE_API_GET(ptp_clock, dev)->adjust(dev, increment);
223}
224
240__deprecated static inline int ptp_clock_rate_adjust(const struct device *dev, double rate)
241{
242 const struct ptp_clock_driver_api *api = DEVICE_API_GET(ptp_clock, dev);
243 double scaled_ppm;
244
245 TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
246 if (api->rate_adjust != NULL) {
247 return api->rate_adjust(dev, rate);
248 }
249 TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
250
251 scaled_ppm = (rate - 1.0) * (1000000.0 * PTP_CLOCK_SCALED_PPM_ONE);
252 scaled_ppm += (scaled_ppm < 0.0) ? -0.5 : 0.5;
253
254 /* Written so that NaN is rejected as well. */
255 if (!((scaled_ppm >= (double)INT64_MIN) && (scaled_ppm < -(double)INT64_MIN))) {
256 return -ERANGE;
257 }
258
259 return api->adjust_rate(dev, (int64_t)scaled_ppm);
260}
261
277static inline int ptp_clock_adjust_rate(const struct device *dev, int64_t scaled_ppm)
278{
279 const struct ptp_clock_driver_api *api = DEVICE_API_GET(ptp_clock, dev);
280
281 if (unlikely(api->adjust_rate == NULL)) {
282 /* Drivers that still implement the deprecated operation are served as well. */
283 TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
284 return api->rate_adjust(dev, 1.0 + (double)scaled_ppm /
285 (1000000.0 * PTP_CLOCK_SCALED_PPM_ONE));
286 TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_DEPRECATED_DECLARATIONS)
287 }
288
289 return api->adjust_rate(dev, scaled_ppm);
290}
291
292#ifdef __cplusplus
293}
294#endif
295
296#include <zephyr/syscalls/ptp_clock.h>
297
301
302#endif /* ZEPHYR_INCLUDE_DRIVERS_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
System error numbers.
static void i128_multiply_i64_i64(int64_t a, int64_t b, int128_t *result)
Multiply two signed 64-bit integers and store the result in a 128-bit integer.
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
#define TOOLCHAIN_DISABLE_WARNING(warning)
Disable the specified compiler warning for all compilers.
Definition toolchain.h:318
#define TOOLCHAIN_ENABLE_WARNING(warning)
Re-enable the specified compiler warning for all compilers.
Definition toolchain.h:330
Public kernel APIs.
Extra arithmetic and bit-manipulation functions.
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
Definition time.h:24
Misc utilities.