mirror of
https://github.com/apache/nuttx.git
synced 2026-08-01 20:28:58 +00:00
drivers/timers/ptp: support ptp clock dummy driver
This patch adds a dummy PTP clock driver implementation that provides a software-based PTP clock for testing and development purposes. Key changes include: 1. Dummy PTP clock driver implementation: - Added drivers/timers/ptp_clock_dummy.c - Provides software-based PTP clock using system monotonic clock - Implements all required lower-half driver operations 2. Lower-half driver operations: - adjfine: Frequency adjustment (stores adjustment value) - adjtime: Time offset adjustment (applies delta to base time) - gettime: Returns current PTP clock time - settime: Sets PTP clock time - getcaps: Reports clock capabilities (1 billion PPB max adjustment) - getcrosststamp: Provides system/device cross-timestamp 3. Driver initialization: - Added ptp_clock_dummy_init() in drivers/drivers_initialize.c - Auto-registration under CONFIG_PTP_CLOCK_DUMMY configuration - Creates /dev/ptp0 character device node 4. Build system integration: - Added CONFIG_PTP_CLOCK_DUMMY Kconfig option (depends on PTP_CLOCK) - Updated CMakeLists.txt and Make.defs - Added include/nuttx/timers/ptp_clock_dummy.h header 5. Implementation details: - Uses clock_gettime(CLOCK_MONOTONIC) as time base - Tracks time offset and frequency adjustment internally - Suitable for testing PTP clock applications without hardware This dummy driver enables development and testing of PTP clock applications on platforms without dedicated PTP hardware support. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
parent
31eb339356
commit
314cccf166
6 changed files with 297 additions and 0 deletions
|
|
@ -50,6 +50,7 @@
|
|||
#include <nuttx/syslog/syslog.h>
|
||||
#include <nuttx/syslog/syslog_console.h>
|
||||
#include <nuttx/thermal.h>
|
||||
#include <nuttx/timers/ptp_clock_dummy.h>
|
||||
#include <nuttx/trace.h>
|
||||
#include <nuttx/usrsock/usrsock_rpmsg.h>
|
||||
#include <nuttx/vhost/vhost.h>
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
203
drivers/timers/ptp_clock_dummy.c
Normal file
203
drivers/timers/ptp_clock_dummy.c
Normal file
|
|
@ -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 <nuttx/config.h>
|
||||
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/timers/ptp_clock.h>
|
||||
|
||||
/****************************************************************************
|
||||
* 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);
|
||||
}
|
||||
74
include/nuttx/timers/ptp_clock_dummy.h
Normal file
74
include/nuttx/timers/ptp_clock_dummy.h
Normal file
|
|
@ -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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue