xiaoxiang781216 commented on code in PR #21857: URL: https://github.com/apache/nuttx/pull/21857#discussion_r4574188898
########## include/nuttx/timers/ptp_pps.h: ########## @@ -0,0 +1,191 @@ +/**************************************************************************** + * include/nuttx/timers/ptp_pps.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_NUTTX_TIMERS_PTP_PPS_H +#define __INCLUDE_NUTTX_TIMERS_PTP_PPS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <nuttx/timers/ptp_clock.h> + +#ifdef CONFIG_PTP_PPS + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* PPS disciplined software PTP clock. + * + * The clock is built on a free running, 32-bit, up counting hardware + * counter (for example a timer clocked by the CPU clock). The time of the + * clock is computed from the counter with a 64-bit fixed point conversion + * whose rate is changed by clock_adjtime(ADJ_FREQUENCY), so the clock can + * be disciplined with a resolution better than 1 ppb. + * + * External time stamp inputs latch the counter in hardware when an edge is + * seen (for example a timer input capture channel wired to the PPS output + * of a GNSS receiver). The lower half reports the latched value with + * ptp_pps_capture() from its interrupt handler. Because the counter is + * latched by hardware, the time stamp does not depend on the interrupt + * latency at all: the interrupt only has to be serviced before the + * counter wraps around. + * + * PPS pin --> [timer input capture] --CCR--> ptp_pps_capture() + * | + * counter -> ns conversion <---+ + * | + * /dev/ptpX read(): struct ptp_extts_event + */ + +struct ptp_pps_lowerhalf_s; + +struct ptp_pps_ops_s +{ + /************************************************************************** + * Name: read + * + * Description: + * Return the current value of the free running counter. The counter + * must count up at the frequency given in ptp_pps_lowerhalf_s::freq and + * wrap around from 0xffffffff to 0. This is called with interrupts + * disabled and must be fast. + * + **************************************************************************/ + + CODE uint32_t (*read)(FAR struct ptp_pps_lowerhalf_s *lower); + + /************************************************************************** + * Name: enable + * + * Description: + * Enable or disable the capture of an external time stamp input. + * + * Input Parameters: + * lower - The lower half instance. + * index - The capture input, 0 .. n_ext_ts - 1. + * flags - PTP_RISING_EDGE and/or PTP_FALLING_EDGE. + * on - true to enable the input, false to disable it. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. A driver + * that cannot capture the requested edges returns -EOPNOTSUPP. + * + **************************************************************************/ + + CODE int (*enable)(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, bool on); +}; + +struct ptp_pps_lowerhalf_s Review Comment: why not reuse include/nuttx/timers/capture.h? and implement pps clock driver on top of cap_lowerhalf_s? ########## include/nuttx/timers/ptp_pps.h: ########## @@ -0,0 +1,191 @@ +/**************************************************************************** + * include/nuttx/timers/ptp_pps.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_NUTTX_TIMERS_PTP_PPS_H +#define __INCLUDE_NUTTX_TIMERS_PTP_PPS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> +#include <stdint.h> + +#include <nuttx/timers/ptp_clock.h> + +#ifdef CONFIG_PTP_PPS + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* PPS disciplined software PTP clock. + * + * The clock is built on a free running, 32-bit, up counting hardware + * counter (for example a timer clocked by the CPU clock). The time of the + * clock is computed from the counter with a 64-bit fixed point conversion + * whose rate is changed by clock_adjtime(ADJ_FREQUENCY), so the clock can + * be disciplined with a resolution better than 1 ppb. + * + * External time stamp inputs latch the counter in hardware when an edge is + * seen (for example a timer input capture channel wired to the PPS output + * of a GNSS receiver). The lower half reports the latched value with + * ptp_pps_capture() from its interrupt handler. Because the counter is + * latched by hardware, the time stamp does not depend on the interrupt + * latency at all: the interrupt only has to be serviced before the + * counter wraps around. + * + * PPS pin --> [timer input capture] --CCR--> ptp_pps_capture() + * | + * counter -> ns conversion <---+ + * | + * /dev/ptpX read(): struct ptp_extts_event + */ + +struct ptp_pps_lowerhalf_s; + +struct ptp_pps_ops_s +{ + /************************************************************************** + * Name: read + * + * Description: + * Return the current value of the free running counter. The counter + * must count up at the frequency given in ptp_pps_lowerhalf_s::freq and + * wrap around from 0xffffffff to 0. This is called with interrupts + * disabled and must be fast. + * + **************************************************************************/ + + CODE uint32_t (*read)(FAR struct ptp_pps_lowerhalf_s *lower); + + /************************************************************************** + * Name: enable + * + * Description: + * Enable or disable the capture of an external time stamp input. + * + * Input Parameters: + * lower - The lower half instance. + * index - The capture input, 0 .. n_ext_ts - 1. + * flags - PTP_RISING_EDGE and/or PTP_FALLING_EDGE. + * on - true to enable the input, false to disable it. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. A driver + * that cannot capture the requested edges returns -EOPNOTSUPP. + * + **************************************************************************/ + + CODE int (*enable)(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, bool on); +}; + +struct ptp_pps_lowerhalf_s +{ + FAR const struct ptp_pps_ops_s *ops; /* Lower half operations */ + uint32_t freq; /* Nominal counter frequency (Hz) */ + uint8_t n_ext_ts; /* Number of capture inputs */ + + /* Private data of the upper half, set by ptp_pps_register() */ + + FAR void *priv; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: ptp_pps_register + * + * Description: + * Create a PTP clock on top of the counter of the lower half and register + * it as /dev/ptp<devno>. The clock starts at zero; set it with + * clock_settime() or let the gnsspps application do it. + * + * Input Parameters: + * lower - The lower half instance. It must persist as long as the + * driver persists. + * devno - The device number, the device is registered as /dev/ptp<devno> + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int ptp_pps_register(FAR struct ptp_pps_lowerhalf_s *lower, int devno); + +/**************************************************************************** + * Name: ptp_pps_capture + * + * Description: + * Report a counter value latched by a capture input. The value is + * converted to the time of the clock and queued as a struct + * ptp_extts_event for the readers of /dev/ptpX. + * + * This is normally called from the capture interrupt handler. It uses + * OS services so it must not be called from a zero latency + * (CONFIG_ARCH_HIPRI_INTERRUPT) handler. + * + * The capture must be reported less than 2^31 counter periods after it + * happened (about 10 seconds with a 200 MHz counter). + * + * Input Parameters: + * lower - The lower half instance. + * index - The capture input that latched the counter. + * flags - The edge that was captured, PTP_RISING_EDGE, PTP_FALLING_EDGE + * or 0 when unknown. + * count - The latched counter value. + * + ****************************************************************************/ + +void ptp_pps_capture(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + uint32_t count); + +#ifdef CONFIG_PTP_PPS_DUMMY +/**************************************************************************** + * Name: ptp_pps_dummy_initialize + * + * Description: + * Register a PPS disciplined clock with a simulated counter and a + * simulated PPS input, for testing. + * + ****************************************************************************/ + +int ptp_pps_dummy_initialize(int devno); Review Comment: let's implement a dummy cap_lowerhalf_s instead ########## drivers/timers/ptp_pps_dummy.c: ########## @@ -0,0 +1,263 @@ +/**************************************************************************** + * drivers/timers/ptp_pps_dummy.c + * + * 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. + * + ****************************************************************************/ + +/* Simulated counter and PPS input for the PPS disciplined PTP clock. + * + * up_perf_gettime() is used as the "true" time. The simulated counter + * runs CONFIG_PTP_PPS_DUMMY_ERROR_PPB faster than its nominal frequency, + * like a real crystal would, and a simulated PPS edge is produced at every + * whole second of the true time. As with a hardware input capture, the + * counter value is latched exactly at the edge (plus a configurable + * jitter) and reported later from a watchdog, so the latency of the + * watchdog does not affect the time stamps. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <errno.h> + +#include <nuttx/arch.h> +#include <nuttx/clock.h> +#include <nuttx/debug.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/ptp_clock.h> +#include <nuttx/timers/ptp_pps.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Report the simulated edge this long after the true second */ + +#define PTP_PPS_DUMMY_REPORT_NS (2 * NSEC_PER_MSEC) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ptp_pps_dummy_s +{ + struct ptp_pps_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* Simulated PPS */ + int64_t whole; /* Actual frequency, integer Hz */ + int64_t milli; /* Actual frequency, mHz remainder */ + time_t last; /* Second of the last edge */ + uint32_t rand; /* Jitter pseudo random state */ + bool enabled; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static uint32_t ptp_pps_dummy_read(FAR struct ptp_pps_lowerhalf_s *lower); +static int ptp_pps_dummy_enable(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + bool on); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ptp_pps_ops_s g_ptp_pps_dummy_ops = +{ + ptp_pps_dummy_read, /* read */ + ptp_pps_dummy_enable, /* enable */ +}; + +static struct ptp_pps_dummy_s g_ptp_pps_dummy; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_pps_dummy_truetime + ****************************************************************************/ + +static void ptp_pps_dummy_truetime(FAR struct timespec *ts) +{ + up_perf_convert(up_perf_gettime(), ts); +} + +/**************************************************************************** + * Name: ptp_pps_dummy_count + * + * Description: + * The value of the simulated counter at a given true time. + * + ****************************************************************************/ + +static int64_t ptp_pps_dummy_count(FAR struct ptp_pps_dummy_s *priv, + time_t sec, long nsec) +{ + return (int64_t)sec * priv->whole + + (int64_t)sec * priv->milli / 1000 + + (int64_t)nsec * priv->whole / NSEC_PER_SEC; +} + +/**************************************************************************** + * Name: ptp_pps_dummy_jitter + * + * Description: + * Uniform jitter in [-JITTER, +JITTER] ns, converted to counts. + * + ****************************************************************************/ + +static int64_t ptp_pps_dummy_jitter(FAR struct ptp_pps_dummy_s *priv) +{ +#if CONFIG_PTP_PPS_DUMMY_JITTER_NS > 0 + int64_t ns; + + priv->rand = priv->rand * 1664525u + 1013904223u; + ns = (int64_t)(priv->rand >> 8) % (2 * CONFIG_PTP_PPS_DUMMY_JITTER_NS + 1) + - CONFIG_PTP_PPS_DUMMY_JITTER_NS; + return ns * priv->whole / NSEC_PER_SEC; +#else + UNUSED(priv); + return 0; +#endif +} + +/**************************************************************************** + * Name: ptp_pps_dummy_read + ****************************************************************************/ + +static uint32_t ptp_pps_dummy_read(FAR struct ptp_pps_lowerhalf_s *lower) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)lower; + struct timespec ts; + + ptp_pps_dummy_truetime(&ts); + return (uint32_t)ptp_pps_dummy_count(priv, ts.tv_sec, ts.tv_nsec); +} + +/**************************************************************************** + * Name: ptp_pps_dummy_enable + ****************************************************************************/ + +static int ptp_pps_dummy_enable(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + bool on) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)lower; + + if (index != 0) + { + return -EINVAL; + } + + if (on && (flags & PTP_FALLING_EDGE) != 0) + { + return -EOPNOTSUPP; + } + + priv->enabled = on; + return OK; +} + +/**************************************************************************** + * Name: ptp_pps_dummy_wdog + * + * Description: + * Called a little after each true second: report the counter value + * latched at the second. + * + ****************************************************************************/ + +static void ptp_pps_dummy_wdog(wdparm_t arg) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)arg; + struct timespec ts; + int64_t delay; + + ptp_pps_dummy_truetime(&ts); + + if (ts.tv_sec != priv->last) + { + priv->last = ts.tv_sec; + if (priv->enabled) + { + int64_t cnt = ptp_pps_dummy_count(priv, ts.tv_sec, 0) + + ptp_pps_dummy_jitter(priv); + + ptp_pps_capture(&priv->lower, 0, PTP_RISING_EDGE, (uint32_t)cnt); + } + } + + /* Wake up again just after the next true second */ + + delay = NSEC_PER_SEC - ts.tv_nsec + PTP_PPS_DUMMY_REPORT_NS; + wd_start(&priv->wdog, (clock_t)((delay + NSEC_PER_TICK - 1) / Review Comment: call NSEC2TICK ########## drivers/timers/ptp_pps.c: ########## @@ -0,0 +1,524 @@ +/**************************************************************************** + * drivers/timers/ptp_pps.c + * + * 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. + * + ****************************************************************************/ + +/* PPS disciplined software PTP clock. + * + * The time of the clock is a linear function of a free running 32-bit + * hardware counter: + * + * t(cnt) = base_ns + ((cnt - base_cnt) * mult + base_frac) >> shift + * + * mult is the length of one counter period in units of 2^-shift ns. shift + * is chosen so that mult is just below 2^31, which gives a frequency + * resolution of better than 1 ppb for any counter frequency and keeps the + * 32x32 bit product within 64 bits. The remainder of the conversion is + * kept in base_frac, so no time is lost when the clock is re-based. + * + * Frequency changes re-base the clock first, so they are phase + * continuous. A watchdog re-bases the clock periodically so that the + * counter difference never exceeds 2^30, whatever the counter frequency. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <stdint.h> +#include <string.h> +#include <assert.h> +#include <errno.h> + +#include <nuttx/clock.h> +#include <nuttx/debug.h> +#include <nuttx/irq.h> +#include <nuttx/kmalloc.h> +#include <nuttx/spinlock.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/ptp_clock.h> +#include <nuttx/timers/ptp_pps.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The clock is re-based at least every 2^30 counter periods, a quarter of + * the wrap around period of the counter. + */ + +#define PTP_PPS_REBASE_COUNTS (UINT32_C(1) << 30) + +/* Largest mult, leaves room for any frequency adjustment up to 100% */ + +#define PTP_PPS_MULT_MAX UINT32_C(0x7fffffff) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ptp_pps_s +{ + struct ptp_lowerhalf_s ptp; /* Must be first */ + FAR struct ptp_pps_lowerhalf_s *lower; /* Counter and capture inputs */ + spinlock_t lock; /* Protects the time base */ + struct wdog_s wdog; /* Periodic re-base */ + clock_t period; /* Re-base period, in ticks */ + + /* Time base, see the description at the top of the file */ + + int64_t base_ns; + uint32_t base_cnt; + uint32_t base_frac; + uint32_t mult; + uint32_t mult_nom; + uint8_t shift; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int ptp_pps_adjfine(FAR struct ptp_lowerhalf_s *lower, long ppb); +static int ptp_pps_adjtime(FAR struct ptp_lowerhalf_s *lower, + int64_t delta); +static int ptp_pps_gettime(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *ts, + FAR struct ptp_system_timestamp *sts); +static int ptp_pps_getcrosststamp(FAR struct ptp_lowerhalf_s *lower, + FAR struct system_device_crosststamp *cts); +static int ptp_pps_settime(FAR struct ptp_lowerhalf_s *lower, + FAR const struct timespec *ts); +static int ptp_pps_getres(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *res); +static int ptp_pps_enable(FAR struct ptp_lowerhalf_s *lower, + FAR struct ptp_clock_request *rq, bool on); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ptp_ops_s g_ptp_pps_ops = +{ + ptp_pps_adjfine, /* adjfine */ + NULL, /* adjphase */ + ptp_pps_adjtime, /* adjtime */ + ptp_pps_gettime, /* gettime */ + ptp_pps_getcrosststamp, /* getcrosststamp */ + ptp_pps_settime, /* settime */ + ptp_pps_getres, /* getres */ + NULL, /* control */ + ptp_pps_enable, /* enable */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_pps_ns2ts + ****************************************************************************/ + +static void ptp_pps_ns2ts(int64_t ns, FAR struct timespec *ts) +{ + int64_t sec = ns / NSEC_PER_SEC; + long nsec = (long)(ns - sec * NSEC_PER_SEC); Review Comment: remove ALL cast ########## arch/arm/src/common/stm32/stm32_ptp_pps.c: ########## @@ -0,0 +1,337 @@ +/**************************************************************************** + * arch/arm/src/common/stm32/stm32_ptp_pps.c Review Comment: let's reuse cap_lowerhalf_s implementation instead ########## drivers/timers/ptp_pps_dummy.c: ########## @@ -0,0 +1,263 @@ +/**************************************************************************** + * drivers/timers/ptp_pps_dummy.c + * + * 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. + * + ****************************************************************************/ + +/* Simulated counter and PPS input for the PPS disciplined PTP clock. + * + * up_perf_gettime() is used as the "true" time. The simulated counter + * runs CONFIG_PTP_PPS_DUMMY_ERROR_PPB faster than its nominal frequency, + * like a real crystal would, and a simulated PPS edge is produced at every + * whole second of the true time. As with a hardware input capture, the + * counter value is latched exactly at the edge (plus a configurable + * jitter) and reported later from a watchdog, so the latency of the + * watchdog does not affect the time stamps. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <errno.h> + +#include <nuttx/arch.h> +#include <nuttx/clock.h> +#include <nuttx/debug.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/ptp_clock.h> +#include <nuttx/timers/ptp_pps.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Report the simulated edge this long after the true second */ + +#define PTP_PPS_DUMMY_REPORT_NS (2 * NSEC_PER_MSEC) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ptp_pps_dummy_s +{ + struct ptp_pps_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* Simulated PPS */ + int64_t whole; /* Actual frequency, integer Hz */ + int64_t milli; /* Actual frequency, mHz remainder */ + time_t last; /* Second of the last edge */ + uint32_t rand; /* Jitter pseudo random state */ + bool enabled; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static uint32_t ptp_pps_dummy_read(FAR struct ptp_pps_lowerhalf_s *lower); +static int ptp_pps_dummy_enable(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + bool on); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ptp_pps_ops_s g_ptp_pps_dummy_ops = +{ + ptp_pps_dummy_read, /* read */ + ptp_pps_dummy_enable, /* enable */ +}; + +static struct ptp_pps_dummy_s g_ptp_pps_dummy; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_pps_dummy_truetime + ****************************************************************************/ + +static void ptp_pps_dummy_truetime(FAR struct timespec *ts) +{ + up_perf_convert(up_perf_gettime(), ts); +} + +/**************************************************************************** + * Name: ptp_pps_dummy_count + * + * Description: + * The value of the simulated counter at a given true time. + * + ****************************************************************************/ + +static int64_t ptp_pps_dummy_count(FAR struct ptp_pps_dummy_s *priv, + time_t sec, long nsec) +{ + return (int64_t)sec * priv->whole + + (int64_t)sec * priv->milli / 1000 + + (int64_t)nsec * priv->whole / NSEC_PER_SEC; +} + +/**************************************************************************** + * Name: ptp_pps_dummy_jitter + * + * Description: + * Uniform jitter in [-JITTER, +JITTER] ns, converted to counts. + * + ****************************************************************************/ + +static int64_t ptp_pps_dummy_jitter(FAR struct ptp_pps_dummy_s *priv) +{ +#if CONFIG_PTP_PPS_DUMMY_JITTER_NS > 0 + int64_t ns; + + priv->rand = priv->rand * 1664525u + 1013904223u; + ns = (int64_t)(priv->rand >> 8) % (2 * CONFIG_PTP_PPS_DUMMY_JITTER_NS + 1) + - CONFIG_PTP_PPS_DUMMY_JITTER_NS; + return ns * priv->whole / NSEC_PER_SEC; +#else + UNUSED(priv); + return 0; +#endif +} + +/**************************************************************************** + * Name: ptp_pps_dummy_read + ****************************************************************************/ + +static uint32_t ptp_pps_dummy_read(FAR struct ptp_pps_lowerhalf_s *lower) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)lower; + struct timespec ts; + + ptp_pps_dummy_truetime(&ts); + return (uint32_t)ptp_pps_dummy_count(priv, ts.tv_sec, ts.tv_nsec); +} + +/**************************************************************************** + * Name: ptp_pps_dummy_enable + ****************************************************************************/ + +static int ptp_pps_dummy_enable(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + bool on) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)lower; + + if (index != 0) + { + return -EINVAL; + } + + if (on && (flags & PTP_FALLING_EDGE) != 0) + { + return -EOPNOTSUPP; + } + + priv->enabled = on; + return OK; +} + +/**************************************************************************** + * Name: ptp_pps_dummy_wdog + * + * Description: + * Called a little after each true second: report the counter value + * latched at the second. + * + ****************************************************************************/ + +static void ptp_pps_dummy_wdog(wdparm_t arg) +{ + FAR struct ptp_pps_dummy_s *priv = (FAR struct ptp_pps_dummy_s *)arg; + struct timespec ts; + int64_t delay; + + ptp_pps_dummy_truetime(&ts); + + if (ts.tv_sec != priv->last) + { + priv->last = ts.tv_sec; + if (priv->enabled) + { + int64_t cnt = ptp_pps_dummy_count(priv, ts.tv_sec, 0) + + ptp_pps_dummy_jitter(priv); + + ptp_pps_capture(&priv->lower, 0, PTP_RISING_EDGE, (uint32_t)cnt); + } + } + + /* Wake up again just after the next true second */ + + delay = NSEC_PER_SEC - ts.tv_nsec + PTP_PPS_DUMMY_REPORT_NS; + wd_start(&priv->wdog, (clock_t)((delay + NSEC_PER_TICK - 1) / + NSEC_PER_TICK), + ptp_pps_dummy_wdog, arg); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_pps_dummy_initialize + * + * Description: + * Register a PPS disciplined clock with a simulated counter and a + * simulated PPS input, for testing. + * + ****************************************************************************/ + +int ptp_pps_dummy_initialize(int devno) +{ + FAR struct ptp_pps_dummy_s *priv = &g_ptp_pps_dummy; Review Comment: kmm_zalloc or remove devno from ptp_pps_dummy_initialize ########## drivers/timers/ptp_pps_dummy.c: ########## @@ -0,0 +1,263 @@ +/**************************************************************************** + * drivers/timers/ptp_pps_dummy.c + * + * 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. + * + ****************************************************************************/ + +/* Simulated counter and PPS input for the PPS disciplined PTP clock. + * + * up_perf_gettime() is used as the "true" time. The simulated counter + * runs CONFIG_PTP_PPS_DUMMY_ERROR_PPB faster than its nominal frequency, + * like a real crystal would, and a simulated PPS edge is produced at every + * whole second of the true time. As with a hardware input capture, the + * counter value is latched exactly at the edge (plus a configurable + * jitter) and reported later from a watchdog, so the latency of the + * watchdog does not affect the time stamps. + */ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <errno.h> + +#include <nuttx/arch.h> +#include <nuttx/clock.h> +#include <nuttx/debug.h> +#include <nuttx/wdog.h> +#include <nuttx/timers/ptp_clock.h> +#include <nuttx/timers/ptp_pps.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Report the simulated edge this long after the true second */ + +#define PTP_PPS_DUMMY_REPORT_NS (2 * NSEC_PER_MSEC) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ptp_pps_dummy_s +{ + struct ptp_pps_lowerhalf_s lower; /* Must be first */ + struct wdog_s wdog; /* Simulated PPS */ + int64_t whole; /* Actual frequency, integer Hz */ + int64_t milli; /* Actual frequency, mHz remainder */ + time_t last; /* Second of the last edge */ + uint32_t rand; /* Jitter pseudo random state */ + bool enabled; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static uint32_t ptp_pps_dummy_read(FAR struct ptp_pps_lowerhalf_s *lower); +static int ptp_pps_dummy_enable(FAR struct ptp_pps_lowerhalf_s *lower, + unsigned int index, unsigned int flags, + bool on); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ptp_pps_ops_s g_ptp_pps_dummy_ops = +{ + ptp_pps_dummy_read, /* read */ + ptp_pps_dummy_enable, /* enable */ +}; + +static struct ptp_pps_dummy_s g_ptp_pps_dummy; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_pps_dummy_truetime + ****************************************************************************/ + +static void ptp_pps_dummy_truetime(FAR struct timespec *ts) +{ + up_perf_convert(up_perf_gettime(), ts); +} + +/**************************************************************************** + * Name: ptp_pps_dummy_count + * + * Description: + * The value of the simulated counter at a given true time. + * + ****************************************************************************/ + +static int64_t ptp_pps_dummy_count(FAR struct ptp_pps_dummy_s *priv, + time_t sec, long nsec) +{ + return (int64_t)sec * priv->whole + Review Comment: remove all cast -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
