Merge branch 'feat/ptpd_2upstream' into 'master'

Feat/ptpd 2upstream

See merge request espressif/esp-idf!47246
This commit is contained in:
Ondrej Kosta
2026-04-10 15:11:39 +08:00
10 changed files with 1127 additions and 883 deletions
@@ -9,7 +9,7 @@ menu "PTP Daemon Configuration"
default y
#depends on NET_IPv4
#depends on NET_IGMP
#depends on NET_UDP
#depends on NET_UDP || NET_PKT
help
Build a minimal implementation of IEEE-1588 precision time protocol.
Uses system gettimeofday() and adjtime() calls to synchronize clock
@@ -18,38 +18,15 @@ menu "PTP Daemon Configuration"
if NETUTILS_PTPD
config NETUTILS_PTPD_DEBUG
bool "Enable PTP debug messages"
default n
depends on DEBUG_INFO
help
Enable PTP debug messages even if CONFIG_DEBUG_NET_INFO is not enabled.
# ESP_PTP
#config NETUTILS_PTPD_STACKSIZE
# int "PTP daemon stack stack size"
# default DEFAULT_TASK_STACKSIZE
config NETUTILS_PTPD_CLIENT
bool "Enable client support"
default y
help
Act as a PTP client, synchronizing the NuttX clock to a remote master
clock.
config NETUTILS_PTPD_SERVER
bool "Enable server support"
default n
help
Act as a PTP server, providing NuttX clock time to other systems.
Both server and client can be simultaneously enabled. NuttX will then
synchronize to a higher priority master clock, or act as a master
clock itself if it has the highest priority.
Refer to Best Master Clock algorithm in IEEE-1588 for details.
config NETUTILS_PTPD_STACKSIZE
int "PTP daemon stack stack size"
default 4096 # DEFAULT_TASK_STACKSIZE
config NETUTILS_PTPD_SERVERPRIO
int "PTP daemon priority"
default 100
# ESP_PTP
#config NETUTILS_PTPD_SERVERPRIO
# int "PTP daemon priority"
# default 100
config NETUTILS_PTPD_DOMAIN
int "PTP domain selection"
@@ -59,135 +36,147 @@ menu "PTP Daemon Configuration"
Set PTP domain to participate in. Default domain is 0, other domains
can be used to isolate reference clocks from each other.
if NETUTILS_PTPD_SERVER
config NETUTILS_PTPD_PRIORITY1
int "PTP server priority1"
default 128
range 0 255
help
Set clock priority to announce when acting as a PTP server.
Lower value is higher priority.
config NETUTILS_PTPD_PRIORITY1
int "PTP server priority1"
default 128
range 0 255
help
Set clock priority to announce when acting as a PTP server.
Lower value is higher priority.
A higher priority1 clock will be selected without regard to announced
clock quality fields.
Refer to Best Master Clock algorithm in IEEE-1588 for details.
A higher priority1 clock will be selected without regard to announced
clock quality fields.
Refer to Best Master Clock algorithm in IEEE-1588 for details.
config NETUTILS_PTPD_PRIORITY2
int "PTP server priority2"
default 128
range 0 255
help
Set clock subpriority to announce when acting as a PTP server.
This will distinguish between two clocks that are equivalent in
priority1, class and accuracy values.
Lower value is higher priority.
config NETUTILS_PTPD_PRIORITY2
int "PTP server priority2"
default 128
range 0 255
help
Set clock subpriority to announce when acting as a PTP server.
This will distinguish between two clocks that are equivalent in
priority1, class and accuracy values.
Lower value is higher priority.
config NETUTILS_PTPD_CLASS
int "PTP server class"
default 248
range 0 255
help
Set master clock class to announce when acting as a PTP server.
Lower value means higher quality clock source.
248 is the default for unknown class.
config NETUTILS_PTPD_CLASS
int "PTP server class"
default 248
range 0 255
help
Set master clock class to announce when acting as a PTP server.
Lower value means higher quality clock source.
248 is the default for unknown class.
config NETUTILS_PTPD_ACCURACY
int "PTP server accuracy"
default 254
range 0 255
help
Set master clock accuracy to announce when acting as a PTP server.
Logarithmic scale is defined in IEEE-1588:
32: +- 25 ns
33: +- 100 ns
34: +- 250 ns
35: +- 1 us
36: +- 2.5 us
37: +- 10 us
38: +- 25 us
39: +- 100 us
40: +- 250 us
41: +- 1 ms
42: +- 2.5 ms
43: +- 10 ms
44: +- 25 ms
45: +- 100 ms
46: +- 250 ms
47: +- 1 s
48: +- 10 s
49: +- more than 10 s
254: Unknown
config NETUTILS_PTPD_ACCURACY
int "PTP server accuracy"
default 254
range 0 255
help
Set master clock accuracy to announce when acting as a PTP server.
Logarithmic scale is defined in IEEE-1588:
32: +- 25 ns
33: +- 100 ns
34: +- 250 ns
35: +- 1 us
36: +- 2.5 us
37: +- 10 us
38: +- 25 us
39: +- 100 us
40: +- 250 us
41: +- 1 ms
42: +- 2.5 ms
43: +- 10 ms
44: +- 25 ms
45: +- 100 ms
46: +- 250 ms
47: +- 1 s
48: +- 10 s
49: +- more than 10 s
254: Unknown
config NETUTILS_PTPD_CLOCKSOURCE
int "PTP server clock source type"
default 160
range 0 255
help
Set clock source type to announce when acting as a PTP server.
Common values:
32: GPS
64: PTP
80: NTP
144: Other
160: Internal oscillator
config NETUTILS_PTPD_CLOCKSOURCE
int "PTP server clock source type"
default 160
range 0 255
help
Set clock source type to announce when acting as a PTP server.
Common values:
32: GPS
64: PTP
80: NTP
144: Other
160: Internal oscillator
config NETUTILS_PTPD_SYNC_INTERVAL_MSEC
int "PTP server sync transmit interval (ms)"
default 1000
help
How often to transmit sync packets in server mode.
config NETUTILS_PTPD_SYNC_INTERVAL_MSEC
int "PTP server sync transmit interval (ms)"
default 1000
help
How often to transmit sync packets in server mode.
config NETUTILS_PTPD_ANNOUNCE_INTERVAL_MSEC
int "PTP server announce transmit interval (ms)"
default 10000
help
How often to transmit announce packets in server mode.
config NETUTILS_PTPD_ANNOUNCE_INTERVAL_MSEC
int "PTP server announce transmit interval (ms)"
default 10000
help
How often to transmit announce packets in server mode.
config NETUTILS_PTPD_TWOSTEP_SYNC
bool "PTP server sends two-step synchronization packets"
default y
help
If enabled, sends a follow-up packet after every sync packet.
This helps compensate for the time taken to initiate the transmission.
config NETUTILS_PTPD_TWOSTEP_SYNC
bool "PTP server sends two-step synchronization packets"
default y
help
If enabled, sends a follow-up packet after every sync packet.
This helps compensate for the time taken to initiate the transmission.
config NETUTILS_PTPD_DELAYRESP_INTERVAL
int "PTP server suggested interval of delay requests"
range 0 255
default 4
help
When responding to a delay request, the server can inform the client
how often it should test path delay. This is done using header field
logMessageInterval. The delay will be 2^N seconds.
config NETUTILS_PTPD_DELAYRESP_INTERVAL
int "PTP server suggested interval of delay requests"
range 0 255
default 4
help
When responding to a delay request, the server can inform the client
how often it should test path delay. This is done using header field
logMessageInterval. The delay will be 2^N seconds.
Default value 4 results in 16 second interval.
Default value 4 results in 16 second interval.
endif # NETUTILS_PTPD_SERVER
config NETUTILS_PTPD_TIMEOUT_MS
int "PTP client timeout for changing clock source (ms)"
default 60000
help
If no packets are being received from currently chosen clock source,
fall back to next best clock source after this many seconds.
if NETUTILS_PTPD_CLIENT
config NETUTILS_PTPD_SETTIME_THRESHOLD_MS
int "PTP client threshold for changing system time (ms)"
default 1000
help
If difference between local and remote clock exceeds this threshold,
time is reset with settimeofday() instead of changing the rate with
adjtime().
config NETUTILS_PTPD_TIMEOUT_MS
int "PTP client timeout for changing clock source (ms)"
default 60000
help
If no packets are being received from currently chosen clock source,
fall back to next best clock source after this many seconds.
config NETUTILS_PTPD_ADJTIME_THRESHOLD_NS
int "PTP client threshold for using accumulated ppb to adjust system time (ns)"
default 500
help
If difference between local and remote clock exceeds this threshold,
the PTP client can utilize current ppb instead of accumulated ppb to
accelerate system time adjustment.
config NETUTILS_PTPD_SETTIME_THRESHOLD_MS
int "PTP client threshold for changing system time (ms)"
default 1000
help
If difference between local and remote clock exceeds this threshold,
time is reset with settimeofday() instead of changing the rate with
adjtime().
config NETUTILS_PTPD_MULTICAST_TIMEOUT_MS
int "PTP client timeout to rejoin multicast group (ms)"
default 30000
help
If no PTP multicast packets are being received, attempt to rejoin the
multicast group. This can be necessary if network topology changes, or
depending on hardware, after some error recovery events.
Set to 0 to disable.
config NETUTILS_PTPD_DRIFT_AVERAGE_S
int "PTP client clock drift rate averaging time (s)"
default 600
range 10 86400
help
Clock drift rate is averaged over this time period. Larger value
gives more stable estimate but reacts slower to crystal oscillator speed
changes (such as caused by temperature changes).
config NETUTILS_PTPD_DRIFT_AVERAGE_S
int "PTP client clock drift rate averaging time (s)"
default 600
range 10 86400
help
Clock drift rate is averaged over this time pediod. Larger value
gives more stable estimate but reacts slower to crystal oscillator speed
changes (such as caused by temperature changes).
# Commented options not used by ESP_PTP
#config NETUTILS_PTPD_MULTICAST_TIMEOUT_MS
@@ -200,39 +189,34 @@ menu "PTP Daemon Configuration"
# depending on hardware, after some error recovery events.
# Set to 0 to disable.
config NETUTILS_PTPD_SEND_DELAYREQ
bool "PTP client enable delay requests"
default n
help
If enabled, sends delay request messages to measure the network delay
to server. If disabled, assumes zero delay.
config NETUTILS_PTPD_MAX_PATH_DELAY_NS
int "PTP client maximum path delay (ns)"
default 100000
range 1 1000000000
help
Measured path delay longer than this is ignored. Delay requests are
also not transmitted until clock synchronization is better than this.
if NETUTILS_PTPD_SEND_DELAYREQ
config NETUTILS_PTPD_DELAYREQ_AVGCOUNT
int "PTP client path delay averaging count"
default 100
help
Measured path delay is averaged over this many samples.
config NETUTILS_PTPD_MAX_PATH_DELAY_NS
int "PTP client maximum path delay (ns)"
default 100000
range 1 1000000000
help
Measured path delay longer than this is ignored. Delay requests are
also not transmitted until clock synchronization is better than this.
# ESP_PTP specific
config NETUTILS_PTPD_USE_ADJ_FREQ
bool "PTP client use adj_freq"
default y
help
Use frequency to adjust the clock drift. Note this is more accurate then adjusting the time offset.
config NETUTILS_PTPD_DELAYREQ_AVGCOUNT
int "PTP client path delay averaging count"
default 100
help
Measured path delay is averaged over this many samples.
config NETUTILS_PTPD_PATH_DELAY_STABILITY_NS
int "PTP client send delay request when clock stability (ns)"
default 250
help
Sends path delay request only once the internal clock is stable and skews only
in defined interval.
endif # NETUTILS_PTPD_SEND_DELAYREQ
endif # NETUTILS_PTPD_CLIENT
# ESP_PTP specific
config NETUTILS_PTPD_PATH_DELAY_STABILITY_NS
int "PTP client send delay request when clock stability (ns)"
default 250
help
Sends path delay request only once the internal clock is stable and skews only
in defined interval.
endif # NETUTILS_PTPD
@@ -1,6 +1,8 @@
############################################################################
# apps/netutils/ptpd/Make.defs
#
# 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
@@ -1,6 +1,8 @@
############################################################################
# apps/netutils/ptpd/Makefile
#
# 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
@@ -1,3 +1,12 @@
# NuttX PTP Daemon Port Example
This example component presents port of [Nuttx PTP Daemon](https://github.com/apache/nuttx-apps/tree/master/netutils/ptpd) for ESP-IDF.
## Upstream Information
| Field | Value |
| --- | --- |
| upstream | https://github.com/apache/nuttx-apps |
| imported app | netutils/ptpd |
| upstream commit | `54bef649ea14d293128545f24c9db2594197a327` |
| date imported | 4/2/2026 |
@@ -3,12 +3,14 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2024-2026 Espressif Systems (Shanghai) CO LTD
*/
/****************************************************************************
* apps/include/netutils/ptpd.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
@@ -29,16 +31,24 @@
#ifndef __APPS_INCLUDE_NETUTILS_PTPD_H
#define __APPS_INCLUDE_NETUTILS_PTPD_H
// ESP_PTP
#include <time.h>
#ifndef FAR
#define FAR
#endif
#define ESP_PTP 1
/****************************************************************************
* Included Files
****************************************************************************/
#ifdef ESP_PTP
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <sys/socket.h>
#ifndef FAR
#define FAR
#endif
#else
#include <sys/types.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@@ -47,6 +57,17 @@
* Public Types
****************************************************************************/
struct ptpd_config_s
{
FAR const char *interface;
FAR const char *clock;
bool client_only;
bool hardware_ts;
bool delay_e2e;
bool bmca;
sa_family_t af;
};
/* PTPD status information structure */
struct ptpd_status_s
@@ -130,7 +151,8 @@ extern "C"
* Start the PTP daemon and bind it to specified interface.
*
* Input Parameters:
* interface - Name of the network interface to bind to, e.g. "eth0"
* ESP_PTP: interface - Name of the network interface to bind to
* NuttX: config - The configs of PTP daemon
*
* Returned Value:
* On success, the non-negative task ID of the PTP daemon is returned;
@@ -138,7 +160,7 @@ extern "C"
*
****************************************************************************/
int ptpd_start(FAR const char *interface);
int ptpd_start(FAR const struct ptpd_config_s *config);
/****************************************************************************
* Name: ptpd_status
File diff suppressed because it is too large Load Diff
+22 -13
View File
@@ -3,12 +3,14 @@
*
* SPDX-License-Identifier: Apache-2.0
*
* SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2024-2026 Espressif Systems (Shanghai) CO LTD
*/
/****************************************************************************
* apps/netutils/ptpd/ptpv2.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
@@ -33,6 +35,13 @@
* Included Files
****************************************************************************/
#ifndef ESP_PTP
#include <nuttx/compiler.h>
#else
#define begin_packed_struct
#define end_packed_struct __attribute__((packed))
#endif
#include <stdint.h>
/****************************************************************************
@@ -73,7 +82,7 @@
/* Common header for all message types */
struct ptp_header_s
begin_packed_struct struct ptp_header_s
{
uint8_t messagetype;
uint8_t version;
@@ -88,11 +97,11 @@ struct ptp_header_s
uint8_t sequenceid[2];
uint8_t controlfield;
uint8_t logmessageinterval;
};
} end_packed_struct;
/* Announce a master clock */
struct ptp_announce_s
begin_packed_struct struct ptp_announce_s
{
struct ptp_header_s header;
uint8_t origintimestamp[10];
@@ -104,40 +113,40 @@ struct ptp_announce_s
uint8_t gm_identity[8];
uint8_t stepsremoved[2];
uint8_t timesource;
};
} end_packed_struct;
/* Sync: transmit timestamp from master clock */
struct ptp_sync_s
begin_packed_struct struct ptp_sync_s
{
struct ptp_header_s header;
uint8_t origintimestamp[10];
};
} end_packed_struct;
/* FollowUp: actual timestamp of when sync message was sent */
struct ptp_follow_up_s
begin_packed_struct struct ptp_follow_up_s
{
struct ptp_header_s header;
uint8_t origintimestamp[10];
};
} end_packed_struct;
/* DelayReq: request delay measurement */
struct ptp_delay_req_s
begin_packed_struct struct ptp_delay_req_s
{
struct ptp_header_s header;
uint8_t origintimestamp[10];
};
} end_packed_struct;
/* DelayResp: response to DelayReq */
struct ptp_delay_resp_s
begin_packed_struct struct ptp_delay_resp_s
{
struct ptp_header_s header;
uint8_t receivetimestamp[10];
uint8_t reqidentity[8];
uint8_t reqportindex[2];
};
} end_packed_struct;
#endif /* __APPS_NETUTILS_PTPD_PTPV2_H */
+37 -1
View File
@@ -1,7 +1,35 @@
menu "Example Configuration"
menu "PTP Example Configuration"
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
config EXAMPLE_PTPD_CLIENT_ONLY
bool "Set to client only mode"
default n
help
Act as a PTP client only, synchronizing the clock to a remote master
clock.
When disabled, both server and client are simultaneously enabled.
PTPd will then synchronize to a higher priority master clock, or
act as a master clock itself if it has the highest priority.
Refer to Best Master Clock algorithm in IEEE-1588 for details.
config EXAMPLE_PTPD_SEND_DELAYREQ
bool "PTP client enable delay requests"
default n
help
If enabled, sends delay request messages to measure the network delay
to server. If disabled, assumes zero delay.
config EXAMPLE_PTPD_BMCA
bool "Enable Best Master Clock Algorithm (BMCA)"
default y
help
When enabled, PTPd participates in IEEE 1588 best master clock
selection (compares remote masters and may become grandmaster when
appropriate). When disabled, BMCA-related state transitions are
skipped.
choice EXAMPLE_PTP_PULSE_SOURCE
prompt "Pulse Source"
default EXAMPLE_PTP_PULSE_EMAC_PPS
@@ -49,4 +77,12 @@ menu "Example Configuration"
function, width accuracy may vary or it may be lost completely when you select
very short pulse width.
config EXAMPLE_PTPD_STACKSIZE
int "PTP daemon stack stack size"
default 2048
config EXAMPLE_PTPD_TASKPRIO
int "PTP daemon task priority"
default 5
endmenu
+32 -4
View File
@@ -12,6 +12,7 @@
#include "ethernet_init.h"
#include "esp_vfs_l2tap.h"
#include "driver/gpio.h"
#include <sys/socket.h>
#include "ptpd.h"
#if CONFIG_EXAMPLE_PTP_PULSE_CALLBACK
@@ -90,15 +91,42 @@ IRAM_ATTR bool ts_callback(esp_eth_mediator_t *eth, void *user_args)
}
#endif //CONFIG_EXAMPLE_PTP_PULSE_CALLBACK
void ptp_daemon_task(void *task_param)
{
struct ptpd_config_s ptp_config = {
.interface = ETH_IF_KEY,
.clock = "ptpclock",
#ifdef CONFIG_EXAMPLE_PTPD_CLIENT_ONLY
.client_only = true,
#endif
.hardware_ts = true,
#ifdef CONFIG_EXAMPLE_PTPD_SEND_DELAYREQ
.delay_e2e = true,
#endif
#ifdef CONFIG_EXAMPLE_PTPD_BMCA
.bmca = true,
#endif
.af = AF_INET,
};
if (ptpd_start(&ptp_config) < 0) {
ESP_LOGE(TAG, "PTP daemon exited with error");
}
vTaskDelete(NULL);
}
void ptp_deamon_start(void)
{
xTaskCreate(ptp_daemon_task, "PTPD", CONFIG_EXAMPLE_PTPD_STACKSIZE,
NULL, CONFIG_EXAMPLE_PTPD_TASKPRIO, NULL);
}
void app_main(void)
{
ESP_LOGI(TAG, "Starting PTP example");
init_ethernet_and_netif();
int pid = ptpd_start(ETH_IF_KEY);
ptp_deamon_start();
#if CONFIG_EXAMPLE_PTP_PULSE_EMAC_PPS
(void)pid; // suppress compiler warning about unused variable
esp_eth_handle_t eth_handle = esp_netif_get_io_driver(esp_netif_get_handle_from_ifkey(ETH_IF_KEY));
esp_eth_mac_t *mac;
ESP_ERROR_CHECK(esp_eth_get_mac_instance(eth_handle, &mac));
@@ -131,7 +159,7 @@ void app_main(void)
while (1) {
struct ptpd_status_s ptp_status;
// if valid PTP status
if (ptpd_status(pid, &ptp_status) == 0) {
if (ptpd_status(0, &ptp_status) == 0) {
if (ptp_status.clock_source_valid) {
clock_source_valid_cnt++;
} else {
-2
View File
@@ -6,5 +6,3 @@ CONFIG_ETHERNET_INTERNAL_SUPPORT=y
CONFIG_ETHERNET_PHY_IP101=y
CONFIG_NETUTILS_PTPD=y
CONFIG_NETUTILS_PTPD_CLIENT=y
CONFIG_NETUTILS_PTPD_SERVER=y