diff --git a/drivers/drivers_initialize.c b/drivers/drivers_initialize.c index 43b85aa4208..fec2a985758 100644 --- a/drivers/drivers_initialize.c +++ b/drivers/drivers_initialize.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -293,5 +294,9 @@ void drivers_initialize(void) thermal_init(); #endif +#ifdef CONFIG_PTP_CLOCK_DUMMY + ptp_clock_dummy_initialize(0); +#endif + drivers_trace_end(); } diff --git a/drivers/timers/CMakeLists.txt b/drivers/timers/CMakeLists.txt index 0b56569c697..0f1370b71be 100644 --- a/drivers/timers/CMakeLists.txt +++ b/drivers/timers/CMakeLists.txt @@ -94,4 +94,8 @@ if(CONFIG_PTP_CLOCK) list(APPEND SRCS ptp_clock.c) endif() +if(CONFIG_PTP_CLOCK_DUMMY) + list(APPEND SRCS ptp_clock_dummy.c) +endif() + target_sources(drivers PRIVATE ${SRCS}) diff --git a/drivers/timers/Kconfig b/drivers/timers/Kconfig index 5317fb0a464..b96322734c9 100644 --- a/drivers/timers/Kconfig +++ b/drivers/timers/Kconfig @@ -565,4 +565,11 @@ config PTP_CLOCK devices. If you want to use a PTP clock, then you should also enable at least one clock driver as well. +config PTP_CLOCK_DUMMY + bool "the dummy test driver for ptp clock" + default n + select PTP_CLOCK + ---help--- + The dummy test driver. + endmenu # Timer Driver Support diff --git a/drivers/timers/Make.defs b/drivers/timers/Make.defs index c8b6d21cdf4..b9ab14e0fbd 100644 --- a/drivers/timers/Make.defs +++ b/drivers/timers/Make.defs @@ -128,6 +128,10 @@ ifeq ($(CONFIG_PTP_CLOCK),y) CSRCS += ptp_clock.c endif +ifeq ($(CONFIG_PTP_CLOCK_DUMMY),y) + CSRCS += ptp_clock_dummy.c +endif + # Include timer build support (if any were selected) DEPPATH += $(TMRDEPPATH) diff --git a/drivers/timers/ptp_clock_dummy.c b/drivers/timers/ptp_clock_dummy.c new file mode 100644 index 00000000000..bc16559344f --- /dev/null +++ b/drivers/timers/ptp_clock_dummy.c @@ -0,0 +1,203 @@ +/**************************************************************************** + * drivers/timers/ptp_clock_dummy.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct ptp_clock_dummy_s +{ + struct ptp_lowerhalf_s lower; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int ptp_clock_dummy_adjfine(FAR struct ptp_lowerhalf_s *lower, + long scaled_ppm); +static int ptp_clock_dummy_adjphase(FAR struct ptp_lowerhalf_s *lower, + int32_t phase); +static int ptp_clock_dummy_adjtime(FAR struct ptp_lowerhalf_s *lower, + int64_t delta); +static int ptp_clock_dummy_gettime(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *ts, + FAR struct ptp_system_timestamp *sts); +static int ptp_clock_dummy_getcrosststamp(FAR struct ptp_lowerhalf_s *lower, + FAR struct system_device_crosststamp *cts); +static int ptp_clock_dummy_settime(FAR struct ptp_lowerhalf_s *lower, + FAR const struct timespec *ts); +static int ptp_clock_dummy_getres(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *res); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct ptp_ops_s g_ptp_clock_dummy_ops = +{ + ptp_clock_dummy_adjfine, /* adjfine */ + ptp_clock_dummy_adjphase, /* adjphase */ + ptp_clock_dummy_adjtime, /* adjtime */ + ptp_clock_dummy_gettime, /* gettime */ + ptp_clock_dummy_getcrosststamp, /* getcroasststamp */ + ptp_clock_dummy_settime, /* settime */ + ptp_clock_dummy_getres, /* getres */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int ptp_clock_dummy_adjfine(FAR struct ptp_lowerhalf_s *lower, + long scaled_ppm) +{ + ptpinfo("ptp_clock_dummy_adjfine ppm:%ld\n", scaled_ppm); + return 0; +} + +static int ptp_clock_dummy_adjphase(FAR struct ptp_lowerhalf_s *lower, + int32_t phase) +{ + ptpinfo("ptp_clock_dummy_adjphase phase:%"PRIi32"\n", phase); + return 0; +} + +static int ptp_clock_dummy_adjtime(FAR struct ptp_lowerhalf_s *lower, + int64_t delta) +{ + ptpinfo("ptp_clock_dummy_adjtime delta:%"PRIi64"\n", delta); + return 0; +} + +static int ptp_clock_dummy_gettime(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *ts, + FAR struct ptp_system_timestamp *sts) +{ + clock_gettime(CLOCK_REALTIME, ts); + + if (sts != NULL) + { + sts->pre_ts.tv_sec = ts->tv_sec; + sts->pre_ts.tv_nsec = ts->tv_nsec; + sts->post_ts.tv_sec = ts->tv_sec; + sts->post_ts.tv_nsec = ts->tv_nsec; + } + + ptpinfo("ptp_clock_dummy_gettime sec:%ld, nsec:%ld\n", ts->tv_sec, + ts->tv_nsec); + return 0; +} + +static int +ptp_clock_dummy_getcrosststamp(FAR struct ptp_lowerhalf_s *lower, + FAR struct system_device_crosststamp *cts) +{ + struct timespec ts; + + if (cts == NULL) + { + return -EINVAL; + } + + clock_systime_timespec(&ts); + cts->device.tv_sec = ts.tv_sec; + cts->device.tv_nsec = ts.tv_nsec; + + clock_gettime(CLOCK_REALTIME, &ts); + cts->realtime.tv_sec = ts.tv_sec; + cts->realtime.tv_nsec = ts.tv_nsec; + + clock_gettime(CLOCK_MONOTONIC, &ts); + cts->monoraw.tv_sec = ts.tv_sec; + cts->monoraw.tv_nsec = ts.tv_sec; + + ptpinfo("ptp_clock_dummy_getcrosststamp sec:%ld, nsec:%ld\n", + (long)ts.tv_sec, ts.tv_nsec); + return 0; +} + +static int ptp_clock_dummy_settime(FAR struct ptp_lowerhalf_s *lower, + FAR const struct timespec *ts) +{ + ptpinfo("ptp_clock_dummy_settime sec:%ld, nsec:%ld\n", (long)ts->tv_sec, + ts->tv_nsec); + return 0; +} + +static int ptp_clock_dummy_getres(FAR struct ptp_lowerhalf_s *lower, + FAR struct timespec *res) +{ + res->tv_sec = 0; + res->tv_nsec = 1; + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ptp_clock_dummy_initialize + * + * Description: + * This function will initialize dummy ptp device driver for test. + * + * Input Parameters: + * devno - The user specifies number of device. ex: /dev/ptpX. + * + * Returned Value: + * OK if the driver was successfully initialize; A negated errno value is + * returned on any failure. + * + ****************************************************************************/ + +int ptp_clock_dummy_initialize(int devno) +{ + FAR struct ptp_clock_dummy_s *priv; + + /* Allocate the upper-half data structure */ + + priv = kmm_zalloc(sizeof(struct ptp_clock_dummy_s)); + if (!priv) + { + ptperr("ERROR: Allocation failed\n"); + return -ENOMEM; + } + + priv->lower.ops = &g_ptp_clock_dummy_ops; + + return ptp_clock_register(&priv->lower, 1000000, devno); +} diff --git a/include/nuttx/timers/ptp_clock_dummy.h b/include/nuttx/timers/ptp_clock_dummy.h new file mode 100644 index 00000000000..8562d74ff63 --- /dev/null +++ b/include/nuttx/timers/ptp_clock_dummy.h @@ -0,0 +1,74 @@ +/**************************************************************************** + * include/nuttx/timers/ptp_clock_dummy.h + * + * 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_CLOCK_DUMMY_H +#define __INCLUDE_NUTTX_TIMERS_PTP_CLOCK_DUMMY_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: ptp_clock_dummy_initialize + * + * Description: + * This function will initialize dummy ptp device driver for test. + * + * Input Parameters: + * devno - The user specifies number of device. ex: /dev/ptpX. + * + * Returned Value: + * OK if the driver was successfully initialize; A negated errno value is + * returned on any failure. + * + ****************************************************************************/ + +int ptp_clock_dummy_initialize(int devno); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif +#endif /* __INCLUDE_NUTTX_TIMERS_PTP_CLOCK_DUMMY_H */