From c393728e563a7972db6294e1a2d38153c400b675 Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Sun, 15 Feb 2026 16:36:24 -0500 Subject: [PATCH 01/12] might work --- .gitignore | 1 + NetX/inc/rtc.h | 12 +++ NetX/src/rtc.c | 185 +++++++++++++++++++++++++++++++++++++++ NetX/src/u_nx_ethernet.c | 3 +- 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 NetX/inc/rtc.h create mode 100644 NetX/src/rtc.c diff --git a/.gitignore b/.gitignore index e19e25a0..ea1d8b70 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/ +.idea/ __pycache__/ *.egg-info/ diff --git a/NetX/inc/rtc.h b/NetX/inc/rtc.h new file mode 100644 index 00000000..20f2ab0c --- /dev/null +++ b/NetX/inc/rtc.h @@ -0,0 +1,12 @@ +#ifndef TSECU_SHEPHERD_RTC_H +#define TSECU_SHEPHERD_RTC_H + +#include "nxd_ptp_client.h" +#include "nx_api.h" + +UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, + UINT operation, NX_PTP_TIME *time_ptr, + NX_PACKET *packet_ptr, + VOID *callback_data); + +#endif //TSECU_SHEPHERD_RTC_H \ No newline at end of file diff --git a/NetX/src/rtc.c b/NetX/src/rtc.c new file mode 100644 index 00000000..cbb81893 --- /dev/null +++ b/NetX/src/rtc.c @@ -0,0 +1,185 @@ +#include "rtc.h" +#include +#include +#include "stm32h5xx_hal_rtc.h" + +#define ptp_utc_offset 0 // UTC 0 +extern RTC_HandleTypeDef hrtc1; + +NX_PTP_TIME *ptp_time; +NX_PTP_DATE_TIME *ptp_date_time; + +UINT interrupt_save; + +static UINT us_to_second_ticks(ULONG ns, UINT second_fractions) +{ + // Second fraction = SS / (PREDIV_S + 1) + return ns * (second_fractions + 1L) / + 1000000L; // TODO: double check overflow issues +} + +static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) +{ + return 1000000L * second_ticks / + (second_fractions + 1); // TODO: double check overflow issues +} + +static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) +{ + UINT rtp_sub_second_tick = us_to_second_ticks( + ptp_date_time->nanosecond / 1000, second_fractions); + + UINT offset_tick = 0; // ticks to go backwards + UINT offset_ahead_1s = 0; + if (rtc_sub_second_tick > rtp_sub_second_tick) { // local ahead + offset_tick = rtc_sub_second_tick - rtp_sub_second_tick; + } else { // local behind + offset_ahead_1s = 1; + offset_tick = second_fractions + rtc_sub_second_tick - + rtp_sub_second_tick; + } + HAL_RTCEx_SetSynchroShift(&hrtc1, offset_tick, offset_ahead_1s); +} + +static LONG diff_ptp_date_time(NX_PTP_DATE_TIME *time1, NX_PTP_DATE_TIME *time2) +{ +#define SECS_PER_MINUTE 60 +#define SECS_PER_HOUR (60 * SECS_PER_MINUTE) +#define SECS_PER_DAY (24 * SECS_PER_HOUR) +#define SECS_PER_YEAR (365 * SECS_PER_DAY) + + LONG seconds_diff = 0; + + seconds_diff += (time2->year - time1->year) * SECS_PER_YEAR; + seconds_diff += (time2->day - time1->day) * SECS_PER_DAY; + seconds_diff += (time2->hour - time1->hour) * SECS_PER_HOUR; + seconds_diff += (time2->minute - time1->minute) * SECS_PER_MINUTE; + seconds_diff += (time2->second - time1->second) * 1; + + return seconds_diff; +} + +UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, + UINT operation, NX_PTP_TIME *time_ptr, + NX_PACKET *packet_ptr, + VOID *callback_data) +{ + switch (operation) { + case NX_PTP_CLIENT_CLOCK_INIT: + HAL_RTC_Init( + &hrtc1); // do I have to or is this done elsewhere? + break; + + case NX_PTP_CLIENT_CLOCK_SET: + TX_DISABLE + + ptp_time = time_ptr; + + nx_ptp_client_utility_convert_time_to_date( + ptp_time, -ptp_utc_offset, ptp_date_time); + + RTC_TimeTypeDef rtp_time = { + .Hours = ptp_date_time->hour, + .Minutes = ptp_date_time->minute, + .Seconds = ptp_date_time->second, + .TimeFormat = 0, + }; + + RTC_DateTypeDef rtp_date = { + .Year = ptp_date_time->year, + .Month = ptp_date_time->month, + .Date = ptp_date_time->day, + .WeekDay = ptp_date_time->weekday, + }; + + // NOTE: 24 hours RTC assumed. + RTC_TimeTypeDef rtc_sub_seconds = {}; + + HAL_RTC_GetTime(&hrtc1, &rtc_sub_seconds, + RTC_FORMAT_BCD); + + HAL_RTC_SetTime(&hrtc1, &rtp_time, RTC_FORMAT_BCD); + + set_subsecond(rtc_sub_seconds.SecondFraction - + rtc_sub_seconds.SubSeconds, + rtc_sub_seconds.SubSeconds); + TX_RESTORE + case NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT: // FALL THROUGH + case NX_PTP_CLIENT_CLOCK_GET: + TX_DISABLE + RTC_TimeTypeDef rtc_time = {}; + RTC_DateTypeDef rtc_date = {}; + + HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); + HAL_RTC_GetDate(&hrtc1, &rtc_date, RTC_FORMAT_BCD); + + NX_PTP_DATE_TIME rtc_ptp_date_time = { + .year = rtp_date.Year, + .month = rtp_date.Month, + .weekday = rtp_date.WeekDay, + .day = rtp_date.Date, + .hour = rtc_time.Hours, + .minute = rtc_time.Minutes, + .second = rtc_time.Seconds, + .nanosecond = + 1000 * second_ticks_to_us( + rtc_time.SubSeconds, + rtc_time.SecondFraction) + }; + + LONG secondsDiff = diff_ptp_date_time( + ptp_date_time, &rtc_ptp_date_time); + + NX_PTP_TIME current_ptp_time = { + .second_high = ptp_time->second_high, + .second_low = ptp_time->second_low, + .nanosecond = rtc_ptp_date_time.nanosecond + }; + + if (secondsDiff > + 0) { // ahead of previous ptp time stamp + _nx_ptp_client_utility_add64( + ¤t_ptp_time.second_high, + ¤t_ptp_time.second_low, 0, + secondsDiff); + } else { + _nx_ptp_client_utility_sub64( + ¤t_ptp_time.second_high, + ¤t_ptp_time.second_low, 0, + secondsDiff); + } + + time_ptr->second_high = current_ptp_time.second_high; + time_ptr->second_low = current_ptp_time.second_low; + time_ptr->nanosecond = current_ptp_time.nanosecond; + + TX_RESTORE + break; + case NX_PTP_CLIENT_CLOCK_ADJUST: + TX_DISABLE + ptp_time = time_ptr; + + nx_ptp_client_utility_convert_time_to_date( + ptp_time, -ptp_utc_offset, ptp_date_time); + + HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); + + set_subsecond( + rtc_time.SecondFraction - rtc_time.SubSeconds, + rtc_time.SubSeconds); // (between 0 and PREDIV_S (aka SecondFraction), counting up) + + TX_RESTORE + break; + case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE: + nx_ptp_client_packet_timestamp_notify( + client_ptr, packet_ptr, ptp_time); + break; + case NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE: // do nothing + break; + default: + printf("How Did We Get Here? (rtc.h)"); + break; + } + + return NX_SUCCESS; +} diff --git a/NetX/src/u_nx_ethernet.c b/NetX/src/u_nx_ethernet.c index 54a28bd9..044688f6 100644 --- a/NetX/src/u_nx_ethernet.c +++ b/NetX/src/u_nx_ethernet.c @@ -1,5 +1,6 @@ // clang-format off #include "u_nx_ethernet.h" +#include "rtc.h" #include "nx_stm32_eth_driver.h" #include "nxd_ptp_client.h" #include "u_nx_debug.h" @@ -231,7 +232,7 @@ uint8_t ethernet_init(ethernet_node_t node_id, DriverFunction driver, OnRecieve /* Create the PTP client instance */ status = nx_ptp_client_create(&device.ptp_client, &device.ip, 0, &device.packet_pool, _PTP_THREAD_PRIORITY, (UCHAR *)&device.ptp_stack, sizeof(device.ptp_stack), - _nx_ptp_client_soft_clock_callback, NX_NULL); + nx_ptp_client_hard_clock_callback, NX_NULL); if(status != NX_SUCCESS) { PRINTLN_ERROR("Failed to create PTP client (Status: %d/%s).", status, nx_status_toString(status)); return status; From f031faad4654a438f4ae3c6b04404b023ecde3e7 Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Mon, 16 Feb 2026 11:32:28 -0500 Subject: [PATCH 02/12] Fixes based on review --- NetX/src/rtc.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/NetX/src/rtc.c b/NetX/src/rtc.c index cbb81893..d65d906b 100644 --- a/NetX/src/rtc.c +++ b/NetX/src/rtc.c @@ -1,9 +1,8 @@ #include "rtc.h" #include -#include #include "stm32h5xx_hal_rtc.h" -#define ptp_utc_offset 0 // UTC 0 +#define PTP_UTC_OFFSET 0 // UTC 0 extern RTC_HandleTypeDef hrtc1; NX_PTP_TIME *ptp_time; @@ -30,11 +29,11 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) ptp_date_time->nanosecond / 1000, second_fractions); UINT offset_tick = 0; // ticks to go backwards - UINT offset_ahead_1s = 0; + UINT offset_ahead_1s = RTC_SHIFTADD1S_RESET; if (rtc_sub_second_tick > rtp_sub_second_tick) { // local ahead offset_tick = rtc_sub_second_tick - rtp_sub_second_tick; } else { // local behind - offset_ahead_1s = 1; + offset_ahead_1s = RTC_SHIFTADD1S_SET; offset_tick = second_fractions + rtc_sub_second_tick - rtp_sub_second_tick; } @@ -76,7 +75,7 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, ptp_time = time_ptr; nx_ptp_client_utility_convert_time_to_date( - ptp_time, -ptp_utc_offset, ptp_date_time); + ptp_time, -PTP_UTC_OFFSET, ptp_date_time); RTC_TimeTypeDef rtp_time = { .Hours = ptp_date_time->hour, @@ -160,7 +159,7 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, ptp_time = time_ptr; nx_ptp_client_utility_convert_time_to_date( - ptp_time, -ptp_utc_offset, ptp_date_time); + ptp_time, -PTP_UTC_OFFSET, ptp_date_time); HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); @@ -182,4 +181,4 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, } return NX_SUCCESS; -} +} \ No newline at end of file From 9c28f8e10f2aa55f3caeee4e1ac49d21a34f4bd6 Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 15:47:02 -0500 Subject: [PATCH 03/12] potential fixes --- NetX/src/rtc.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/NetX/src/rtc.c b/NetX/src/rtc.c index d65d906b..d06f7699 100644 --- a/NetX/src/rtc.c +++ b/NetX/src/rtc.c @@ -10,16 +10,16 @@ NX_PTP_DATE_TIME *ptp_date_time; UINT interrupt_save; -static UINT us_to_second_ticks(ULONG ns, UINT second_fractions) +static UINT us_to_second_ticks(ULONG us, UINT second_fractions) { // Second fraction = SS / (PREDIV_S + 1) - return ns * (second_fractions + 1L) / + return (uint64_t)us * (second_fractions + 1L) / 1000000L; // TODO: double check overflow issues } static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) { - return 1000000L * second_ticks / + return (uint64_t)1000000L * second_ticks / (second_fractions + 1); // TODO: double check overflow issues } @@ -47,7 +47,7 @@ static LONG diff_ptp_date_time(NX_PTP_DATE_TIME *time1, NX_PTP_DATE_TIME *time2) #define SECS_PER_DAY (24 * SECS_PER_HOUR) #define SECS_PER_YEAR (365 * SECS_PER_DAY) - LONG seconds_diff = 0; + int64_t seconds_diff = 0; seconds_diff += (time2->year - time1->year) * SECS_PER_YEAR; seconds_diff += (time2->day - time1->day) * SECS_PER_DAY; @@ -65,8 +65,6 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, { switch (operation) { case NX_PTP_CLIENT_CLOCK_INIT: - HAL_RTC_Init( - &hrtc1); // do I have to or is this done elsewhere? break; case NX_PTP_CLIENT_CLOCK_SET: @@ -99,6 +97,8 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, HAL_RTC_SetTime(&hrtc1, &rtp_time, RTC_FORMAT_BCD); + HAL_RTC_SetDate(&hrtc1, &rtp_date, RTC_FORMAT_BCD); + set_subsecond(rtc_sub_seconds.SecondFraction - rtc_sub_seconds.SubSeconds, rtc_sub_seconds.SubSeconds); @@ -110,7 +110,8 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, RTC_DateTypeDef rtc_date = {}; HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); - HAL_RTC_GetDate(&hrtc1, &rtc_date, RTC_FORMAT_BCD); + HAL_RTCEx_GetTimeStamp(&hrtc1, &rtc_time, &rtc_date, + RTC_FORMAT_BCD); NX_PTP_DATE_TIME rtc_ptp_date_time = { .year = rtp_date.Year, @@ -126,13 +127,13 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, rtc_time.SecondFraction) }; - LONG secondsDiff = diff_ptp_date_time( + int64_t secondsDiff = diff_ptp_date_time( ptp_date_time, &rtc_ptp_date_time); NX_PTP_TIME current_ptp_time = { .second_high = ptp_time->second_high, .second_low = ptp_time->second_low, - .nanosecond = rtc_ptp_date_time.nanosecond + .nanosecond = 0 }; if (secondsDiff > @@ -150,7 +151,9 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, time_ptr->second_high = current_ptp_time.second_high; time_ptr->second_low = current_ptp_time.second_low; - time_ptr->nanosecond = current_ptp_time.nanosecond; + time_ptr->nanosecond = + rtc_ptp_date_time + .nanosecond; // pull directly from rtc TX_RESTORE break; From dc2ac2b52786aefe56af1d7573bd3e3f681be89e Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 16:36:25 -0500 Subject: [PATCH 04/12] Sync maybe works --- NetX/src/rtc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/NetX/src/rtc.c b/NetX/src/rtc.c index d06f7699..19b48d29 100644 --- a/NetX/src/rtc.c +++ b/NetX/src/rtc.c @@ -13,29 +13,31 @@ UINT interrupt_save; static UINT us_to_second_ticks(ULONG us, UINT second_fractions) { // Second fraction = SS / (PREDIV_S + 1) - return (uint64_t)us * (second_fractions + 1L) / - 1000000L; // TODO: double check overflow issues + return (uint64_t)us * (second_fractions + 1) / 1000000L; } static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) { - return (uint64_t)1000000L * second_ticks / - (second_fractions + 1); // TODO: double check overflow issues + return (uint64_t)1000000L * second_ticks / (second_fractions + 1); } static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) { + if (~(rtc_sub_second_tick >> 15 & 0b1)) { + printf("rtc SS overflow ig"); + } + UINT rtp_sub_second_tick = us_to_second_ticks( ptp_date_time->nanosecond / 1000, second_fractions); UINT offset_tick = 0; // ticks to go backwards UINT offset_ahead_1s = RTC_SHIFTADD1S_RESET; - if (rtc_sub_second_tick > rtp_sub_second_tick) { // local ahead + if (rtc_sub_second_tick >= rtp_sub_second_tick) { // local ahead offset_tick = rtc_sub_second_tick - rtp_sub_second_tick; } else { // local behind offset_ahead_1s = RTC_SHIFTADD1S_SET; - offset_tick = second_fractions + rtc_sub_second_tick - - rtp_sub_second_tick; + offset_tick = second_fractions - + (rtc_sub_second_tick - rtp_sub_second_tick); } HAL_RTCEx_SetSynchroShift(&hrtc1, offset_tick, offset_ahead_1s); } From 1771f2b860808ac60f57e624ca6ddbb0f1bedc0b Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 16:47:32 -0500 Subject: [PATCH 05/12] rename --- NetX/src/{rtc.c => u_rtc.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename NetX/src/{rtc.c => u_rtc.c} (100%) diff --git a/NetX/src/rtc.c b/NetX/src/u_rtc.c similarity index 100% rename from NetX/src/rtc.c rename to NetX/src/u_rtc.c From d8509ace7e32391ba44cbe0aa4b151f32384c574 Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 16:58:45 -0500 Subject: [PATCH 06/12] extern --- NetX/src/u_rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 19b48d29..68343d0a 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -3,7 +3,7 @@ #include "stm32h5xx_hal_rtc.h" #define PTP_UTC_OFFSET 0 // UTC 0 -extern RTC_HandleTypeDef hrtc1; +extern RTC_HandleTypeDef hrtc; NX_PTP_TIME *ptp_time; NX_PTP_DATE_TIME *ptp_date_time; From 2131fac9954a939661c7f08074f2923b68f3dbde Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 17:04:35 -0500 Subject: [PATCH 07/12] Update u_rtc.c --- NetX/src/u_rtc.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 68343d0a..fa343411 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -39,7 +39,7 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) offset_tick = second_fractions - (rtc_sub_second_tick - rtp_sub_second_tick); } - HAL_RTCEx_SetSynchroShift(&hrtc1, offset_tick, offset_ahead_1s); + HAL_RTCEx_SetSynchroShift(&hrtc, offset_tick, offset_ahead_1s); } static LONG diff_ptp_date_time(NX_PTP_DATE_TIME *time1, NX_PTP_DATE_TIME *time2) @@ -94,12 +94,12 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, // NOTE: 24 hours RTC assumed. RTC_TimeTypeDef rtc_sub_seconds = {}; - HAL_RTC_GetTime(&hrtc1, &rtc_sub_seconds, + HAL_RTC_GetTime(&hrtc, &rtc_sub_seconds, RTC_FORMAT_BCD); - HAL_RTC_SetTime(&hrtc1, &rtp_time, RTC_FORMAT_BCD); + HAL_RTC_SetTime(&hrtc, &rtp_time, RTC_FORMAT_BCD); - HAL_RTC_SetDate(&hrtc1, &rtp_date, RTC_FORMAT_BCD); + HAL_RTC_SetDate(&hrtc, &rtp_date, RTC_FORMAT_BCD); set_subsecond(rtc_sub_seconds.SecondFraction - rtc_sub_seconds.SubSeconds, @@ -111,8 +111,8 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, RTC_TimeTypeDef rtc_time = {}; RTC_DateTypeDef rtc_date = {}; - HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); - HAL_RTCEx_GetTimeStamp(&hrtc1, &rtc_time, &rtc_date, + HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BCD); + HAL_RTCEx_GetTimeStamp(&hrtc, &rtc_time, &rtc_date, RTC_FORMAT_BCD); NX_PTP_DATE_TIME rtc_ptp_date_time = { @@ -166,7 +166,7 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, nx_ptp_client_utility_convert_time_to_date( ptp_time, -PTP_UTC_OFFSET, ptp_date_time); - HAL_RTC_GetTime(&hrtc1, &rtc_time, RTC_FORMAT_BCD); + HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BCD); set_subsecond( rtc_time.SecondFraction - rtc_time.SubSeconds, From b058637a8ebfc6c23729710293e7810529543a2d Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 17:06:25 -0500 Subject: [PATCH 08/12] Update u_rtc.c --- NetX/src/u_rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index fa343411..3d866649 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -23,7 +23,7 @@ static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) { - if (~(rtc_sub_second_tick >> 15 & 0b1)) { + if (~(rtc_sub_second_tick >> 15 & 1)) { printf("rtc SS overflow ig"); } From ae8e09d4dc468b9fcd439241b6d5ed23fed801d8 Mon Sep 17 00:00:00 2001 From: ninjadrknss <116192448+ninjadrknss@users.noreply.github.com> Date: Tue, 24 Feb 2026 17:40:49 -0500 Subject: [PATCH 09/12] Build fixes --- NetX/src/u_rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 3d866649..8f21d329 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -1,6 +1,6 @@ #include "rtc.h" #include -#include "stm32h5xx_hal_rtc.h" +#include "stm32h5xx_hal.h" #define PTP_UTC_OFFSET 0 // UTC 0 extern RTC_HandleTypeDef hrtc; From 3b486bc534313c393f925429cce48cf5e6e64cfe Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 6 Apr 2026 00:03:45 -0400 Subject: [PATCH 10/12] some simplifications, yet untested --- NetX/src/u_rtc.c | 158 +++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 93 deletions(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 8f21d329..8c00518b 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -1,14 +1,14 @@ +#include "nxd_ptp_client.h" #include "rtc.h" #include #include "stm32h5xx_hal.h" +#include "u_tx_debug.h" +#include #define PTP_UTC_OFFSET 0 // UTC 0 extern RTC_HandleTypeDef hrtc; -NX_PTP_TIME *ptp_time; -NX_PTP_DATE_TIME *ptp_date_time; - -UINT interrupt_save; +UINT interrupt_save; // do not remove static UINT us_to_second_ticks(ULONG us, UINT second_fractions) { @@ -21,14 +21,14 @@ static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) return (uint64_t)1000000L * second_ticks / (second_fractions + 1); } -static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) +static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, NX_PTP_DATE_TIME *current_time) { - if (~(rtc_sub_second_tick >> 15 & 1)) { - printf("rtc SS overflow ig"); + if (rtc_sub_second_tick > second_fractions) { + PRINTLN_ERROR("rtc SS overflow"); } UINT rtp_sub_second_tick = us_to_second_ticks( - ptp_date_time->nanosecond / 1000, second_fractions); + current_time->nanosecond / 1000, second_fractions); UINT offset_tick = 0; // ticks to go backwards UINT offset_ahead_1s = RTC_SHIFTADD1S_RESET; @@ -36,35 +36,21 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions) offset_tick = rtc_sub_second_tick - rtp_sub_second_tick; } else { // local behind offset_ahead_1s = RTC_SHIFTADD1S_SET; - offset_tick = second_fractions - - (rtc_sub_second_tick - rtp_sub_second_tick); + UINT delta = rtp_sub_second_tick - rtc_sub_second_tick; + offset_tick = (second_fractions + 1) - delta; } HAL_RTCEx_SetSynchroShift(&hrtc, offset_tick, offset_ahead_1s); } -static LONG diff_ptp_date_time(NX_PTP_DATE_TIME *time1, NX_PTP_DATE_TIME *time2) -{ -#define SECS_PER_MINUTE 60 -#define SECS_PER_HOUR (60 * SECS_PER_MINUTE) -#define SECS_PER_DAY (24 * SECS_PER_HOUR) -#define SECS_PER_YEAR (365 * SECS_PER_DAY) - - int64_t seconds_diff = 0; - - seconds_diff += (time2->year - time1->year) * SECS_PER_YEAR; - seconds_diff += (time2->day - time1->day) * SECS_PER_DAY; - seconds_diff += (time2->hour - time1->hour) * SECS_PER_HOUR; - seconds_diff += (time2->minute - time1->minute) * SECS_PER_MINUTE; - seconds_diff += (time2->second - time1->second) * 1; - - return seconds_diff; -} - UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, UINT operation, NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr, VOID *callback_data) { + NX_PTP_DATE_TIME *current_date_time; + RTC_TimeTypeDef rtc_time = {0}; + RTC_DateTypeDef rtc_date = {0}; + switch (operation) { case NX_PTP_CLIENT_CLOCK_INIT: break; @@ -72,87 +58,71 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, case NX_PTP_CLIENT_CLOCK_SET: TX_DISABLE - ptp_time = time_ptr; - nx_ptp_client_utility_convert_time_to_date( - ptp_time, -PTP_UTC_OFFSET, ptp_date_time); + time_ptr, -PTP_UTC_OFFSET, current_date_time); - RTC_TimeTypeDef rtp_time = { - .Hours = ptp_date_time->hour, - .Minutes = ptp_date_time->minute, - .Seconds = ptp_date_time->second, + rtc_time = (RTC_TimeTypeDef) { + .Hours = current_date_time->hour, + .Minutes = current_date_time->minute, + .Seconds = current_date_time->second, .TimeFormat = 0, }; - RTC_DateTypeDef rtp_date = { - .Year = ptp_date_time->year, - .Month = ptp_date_time->month, - .Date = ptp_date_time->day, - .WeekDay = ptp_date_time->weekday, + rtc_date = (RTC_DateTypeDef) { + .Year = current_date_time->year % 100, + .Month = current_date_time->month, + .Date = current_date_time->day, + .WeekDay = current_date_time->weekday, }; - // NOTE: 24 hours RTC assumed. - RTC_TimeTypeDef rtc_sub_seconds = {}; - - HAL_RTC_GetTime(&hrtc, &rtc_sub_seconds, - RTC_FORMAT_BCD); - - HAL_RTC_SetTime(&hrtc, &rtp_time, RTC_FORMAT_BCD); - - HAL_RTC_SetDate(&hrtc, &rtp_date, RTC_FORMAT_BCD); - - set_subsecond(rtc_sub_seconds.SecondFraction - - rtc_sub_seconds.SubSeconds, - rtc_sub_seconds.SubSeconds); + HAL_RTC_SetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); + HAL_RTC_SetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); + + // After SetTime, SubSeconds is reset to SecondFraction (0 elapsed). + // Shift from 0 elapsed ticks to wherever PTP nanosecond says we should be. + RTC_TimeTypeDef after_set = {}; + HAL_RTC_GetTime(&hrtc, &after_set, RTC_FORMAT_BIN); // to get a valid SecondFraction + set_subsecond(0, after_set.SecondFraction, current_date_time); + + // dummy get date + HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); TX_RESTORE + break; case NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT: // FALL THROUGH case NX_PTP_CLIENT_CLOCK_GET: TX_DISABLE - RTC_TimeTypeDef rtc_time = {}; - RTC_DateTypeDef rtc_date = {}; - HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BCD); - HAL_RTCEx_GetTimeStamp(&hrtc, &rtc_time, &rtc_date, - RTC_FORMAT_BCD); + HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); + HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); NX_PTP_DATE_TIME rtc_ptp_date_time = { - .year = rtp_date.Year, - .month = rtp_date.Month, - .weekday = rtp_date.WeekDay, - .day = rtp_date.Date, + .year = rtc_date.Year, + .month = rtc_date.Month, + .weekday = rtc_date.WeekDay, + .day = rtc_date.Date, .hour = rtc_time.Hours, .minute = rtc_time.Minutes, .second = rtc_time.Seconds, .nanosecond = 1000 * second_ticks_to_us( - rtc_time.SubSeconds, - rtc_time.SecondFraction) + rtc_time.SecondFraction - rtc_time.SubSeconds, + rtc_time.SecondFraction) }; - int64_t secondsDiff = diff_ptp_date_time( - ptp_date_time, &rtc_ptp_date_time); + // helpful way to get UNIX time from RTC so we can give it to NetX layer + time_t current_time_unix = { 0 }; + struct tm tim = {0}; + tim.tm_year = rtc_date.Year + 100; + tim.tm_mon = rtc_date.Month - 1; + tim.tm_mday = rtc_date.Date; + tim.tm_hour = rtc_time.Hours; + tim.tm_min = rtc_time.Minutes; + tim.tm_sec = rtc_time.Seconds; + current_time_unix = mktime(&tim); - NX_PTP_TIME current_ptp_time = { - .second_high = ptp_time->second_high, - .second_low = ptp_time->second_low, - .nanosecond = 0 - }; - if (secondsDiff > - 0) { // ahead of previous ptp time stamp - _nx_ptp_client_utility_add64( - ¤t_ptp_time.second_high, - ¤t_ptp_time.second_low, 0, - secondsDiff); - } else { - _nx_ptp_client_utility_sub64( - ¤t_ptp_time.second_high, - ¤t_ptp_time.second_low, 0, - secondsDiff); - } - - time_ptr->second_high = current_ptp_time.second_high; - time_ptr->second_low = current_ptp_time.second_low; + time_ptr->second_high = 0; // todo fix by 2038 + time_ptr->second_low = (ULONG) current_time_unix; time_ptr->nanosecond = rtc_ptp_date_time .nanosecond; // pull directly from rtc @@ -161,29 +131,31 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, break; case NX_PTP_CLIENT_CLOCK_ADJUST: TX_DISABLE - ptp_time = time_ptr; nx_ptp_client_utility_convert_time_to_date( - ptp_time, -PTP_UTC_OFFSET, ptp_date_time); + time_ptr, -PTP_UTC_OFFSET, current_date_time); - HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BCD); + HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); set_subsecond( rtc_time.SecondFraction - rtc_time.SubSeconds, - rtc_time.SubSeconds); // (between 0 and PREDIV_S (aka SecondFraction), counting up) + rtc_time.SecondFraction, current_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up) TX_RESTORE + + // dummy get date + HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); break; case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE: nx_ptp_client_packet_timestamp_notify( - client_ptr, packet_ptr, ptp_time); + client_ptr, packet_ptr, time_ptr); // this is probably wrong, instead we need to fetch the time via RTC break; case NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE: // do nothing break; default: - printf("How Did We Get Here? (rtc.h)"); + PRINTLN_ERROR("How Did We Get Here? (rtc.h)"); break; } return NX_SUCCESS; -} \ No newline at end of file +} From 1f7b7aa5665edfb16569e6800d0ced70ca7039e1 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 6 Apr 2026 15:21:04 -0400 Subject: [PATCH 11/12] it prints time! --- NetX/inc/rtc.h | 6 +-- NetX/src/nxd_ptp_client.c | 4 +- NetX/src/u_rtc.c | 103 +++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 52 deletions(-) diff --git a/NetX/inc/rtc.h b/NetX/inc/rtc.h index 20f2ab0c..26b0f510 100644 --- a/NetX/inc/rtc.h +++ b/NetX/inc/rtc.h @@ -1,5 +1,5 @@ -#ifndef TSECU_SHEPHERD_RTC_H -#define TSECU_SHEPHERD_RTC_H +#ifndef RTC_H +#define RTC_H #include "nxd_ptp_client.h" #include "nx_api.h" @@ -9,4 +9,4 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, VOID *callback_data); -#endif //TSECU_SHEPHERD_RTC_H \ No newline at end of file +#endif // RTC_H diff --git a/NetX/src/nxd_ptp_client.c b/NetX/src/nxd_ptp_client.c index 98804715..9477177b 100644 --- a/NetX/src/nxd_ptp_client.c +++ b/NetX/src/nxd_ptp_client.c @@ -6152,6 +6152,6 @@ LONG ns; /* return result */ time_ptr -> second_high = sec_hi; time_ptr -> second_low = sec_lo; - time_ptr -> nanosecond = ns; + time_ptr -> nanosecond = ns; } - // clang-format on \ No newline at end of file + // clang-format on diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 8c00518b..4ed629c0 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -2,6 +2,7 @@ #include "rtc.h" #include #include "stm32h5xx_hal.h" +#include "stm32h5xx_hal_rtc.h" #include "u_tx_debug.h" #include @@ -42,12 +43,33 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, NX_PT HAL_RTCEx_SetSynchroShift(&hrtc, offset_tick, offset_ahead_1s); } +static UINT rtc_to_nx_time(RTC_TimeTypeDef *rtc_time, RTC_DateTypeDef *rtc_date, NX_PTP_TIME *time_ptr) { + // helpful way to get UNIX time from RTC so we can give it to NetX layer + time_t current_time_unix = { 0 }; + struct tm tim = {0}; + tim.tm_year = rtc_date->Year + 100; + tim.tm_mon = rtc_date->Month - 1; + tim.tm_mday = rtc_date->Date; + tim.tm_hour = rtc_time->Hours; + tim.tm_min = rtc_time->Minutes; + tim.tm_sec = rtc_time->Seconds; + current_time_unix = mktime(&tim); + + time_ptr->second_high = 0; // todo fix by 2038 + time_ptr->second_low = (ULONG) current_time_unix; + time_ptr->nanosecond = 1000 * second_ticks_to_us( + rtc_time->SecondFraction - rtc_time->SubSeconds, + rtc_time->SecondFraction); // pull directly from rtc + + return 0; +} + UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, UINT operation, NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr, VOID *callback_data) { - NX_PTP_DATE_TIME *current_date_time; + NX_PTP_DATE_TIME current_date_time = { 0 }; RTC_TimeTypeDef rtc_time = {0}; RTC_DateTypeDef rtc_date = {0}; @@ -58,32 +80,39 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, case NX_PTP_CLIENT_CLOCK_SET: TX_DISABLE - nx_ptp_client_utility_convert_time_to_date( - time_ptr, -PTP_UTC_OFFSET, current_date_time); + UINT status = nx_ptp_client_utility_convert_time_to_date( + time_ptr, -PTP_UTC_OFFSET, ¤t_date_time); rtc_time = (RTC_TimeTypeDef) { - .Hours = current_date_time->hour, - .Minutes = current_date_time->minute, - .Seconds = current_date_time->second, + .Hours = current_date_time.hour, + .Minutes = current_date_time.minute, + .Seconds = current_date_time.second, .TimeFormat = 0, }; rtc_date = (RTC_DateTypeDef) { - .Year = current_date_time->year % 100, - .Month = current_date_time->month, - .Date = current_date_time->day, - .WeekDay = current_date_time->weekday, + .Year = current_date_time.year % 100, + .Month = current_date_time.month, + .Date = current_date_time.day, + .WeekDay = current_date_time.weekday, }; + // PRINTLN_INFO("GOT TIME SET: %d, sending NX time (%lu, %lu) from year %d, month %d, day %d, hour %d, minute %d, second %d", + // status, + // time_ptr->second_high, time_ptr->second_low, + // current_date_time.year, current_date_time.month, current_date_time.day, current_date_time.hour, current_date_time.minute, current_date_time.second); + // PRINTLN_INFO("GOT TIME SET, sending RTC from year %d, month %d, day %d, hour %d, minute %d, second %d", + // rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds); + HAL_RTC_SetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); HAL_RTC_SetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); // After SetTime, SubSeconds is reset to SecondFraction (0 elapsed). // Shift from 0 elapsed ticks to wherever PTP nanosecond says we should be. - RTC_TimeTypeDef after_set = {}; + RTC_TimeTypeDef after_set = { 0 }; HAL_RTC_GetTime(&hrtc, &after_set, RTC_FORMAT_BIN); // to get a valid SecondFraction - set_subsecond(0, after_set.SecondFraction, current_date_time); - + set_subsecond(0, after_set.SecondFraction, ¤t_date_time); + // dummy get date HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); TX_RESTORE @@ -95,37 +124,10 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); - NX_PTP_DATE_TIME rtc_ptp_date_time = { - .year = rtc_date.Year, - .month = rtc_date.Month, - .weekday = rtc_date.WeekDay, - .day = rtc_date.Date, - .hour = rtc_time.Hours, - .minute = rtc_time.Minutes, - .second = rtc_time.Seconds, - .nanosecond = - 1000 * second_ticks_to_us( - rtc_time.SecondFraction - rtc_time.SubSeconds, - rtc_time.SecondFraction) - }; - - // helpful way to get UNIX time from RTC so we can give it to NetX layer - time_t current_time_unix = { 0 }; - struct tm tim = {0}; - tim.tm_year = rtc_date.Year + 100; - tim.tm_mon = rtc_date.Month - 1; - tim.tm_mday = rtc_date.Date; - tim.tm_hour = rtc_time.Hours; - tim.tm_min = rtc_time.Minutes; - tim.tm_sec = rtc_time.Seconds; - current_time_unix = mktime(&tim); - + rtc_to_nx_time(&rtc_time, &rtc_date, time_ptr); - time_ptr->second_high = 0; // todo fix by 2038 - time_ptr->second_low = (ULONG) current_time_unix; - time_ptr->nanosecond = - rtc_ptp_date_time - .nanosecond; // pull directly from rtc + // PRINTLN_INFO("GOT TIME REQ, recv NX from year %d, month %d, day %d, hour %d, minute %d, second %d", + // rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds); TX_RESTORE break; @@ -133,13 +135,13 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, TX_DISABLE nx_ptp_client_utility_convert_time_to_date( - time_ptr, -PTP_UTC_OFFSET, current_date_time); + time_ptr, -PTP_UTC_OFFSET, ¤t_date_time); HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); set_subsecond( rtc_time.SecondFraction - rtc_time.SubSeconds, - rtc_time.SecondFraction, current_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up) + rtc_time.SecondFraction, ¤t_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up) TX_RESTORE @@ -147,8 +149,15 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); break; case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE: - nx_ptp_client_packet_timestamp_notify( - client_ptr, packet_ptr, time_ptr); // this is probably wrong, instead we need to fetch the time via RTC + HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); + HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); + + rtc_to_nx_time(&rtc_time, &rtc_date, time_ptr); + + // PRINTLN_INFO("GOT TIME REQ, recv NX from year %d, month %d, day %d, hour %d, minute %d, second %d", + // rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds); + + nx_ptp_client_packet_timestamp_notify(client_ptr, packet_ptr, time_ptr); break; case NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE: // do nothing break; From a410b2e5745cb4b69b7dbce1417bbf8afdeccfd3 Mon Sep 17 00:00:00 2001 From: Jack Rubacha Date: Mon, 6 Apr 2026 15:31:04 -0400 Subject: [PATCH 12/12] further simplifications, working? --- NetX/src/u_rtc.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/NetX/src/u_rtc.c b/NetX/src/u_rtc.c index 4ed629c0..867d6529 100644 --- a/NetX/src/u_rtc.c +++ b/NetX/src/u_rtc.c @@ -1,8 +1,6 @@ #include "nxd_ptp_client.h" #include "rtc.h" -#include #include "stm32h5xx_hal.h" -#include "stm32h5xx_hal_rtc.h" #include "u_tx_debug.h" #include @@ -22,14 +20,14 @@ static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions) return (uint64_t)1000000L * second_ticks / (second_fractions + 1); } -static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, NX_PTP_DATE_TIME *current_time) +static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, ULONG nanoseconds) { if (rtc_sub_second_tick > second_fractions) { PRINTLN_ERROR("rtc SS overflow"); } UINT rtp_sub_second_tick = us_to_second_ticks( - current_time->nanosecond / 1000, second_fractions); + nanoseconds / 1000, second_fractions); UINT offset_tick = 0; // ticks to go backwards UINT offset_ahead_1s = RTC_SHIFTADD1S_RESET; @@ -40,7 +38,7 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, NX_PT UINT delta = rtp_sub_second_tick - rtc_sub_second_tick; offset_tick = (second_fractions + 1) - delta; } - HAL_RTCEx_SetSynchroShift(&hrtc, offset_tick, offset_ahead_1s); + HAL_RTCEx_SetSynchroShift(&hrtc, offset_ahead_1s, offset_tick); } static UINT rtc_to_nx_time(RTC_TimeTypeDef *rtc_time, RTC_DateTypeDef *rtc_date, NX_PTP_TIME *time_ptr) { @@ -111,7 +109,7 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, // Shift from 0 elapsed ticks to wherever PTP nanosecond says we should be. RTC_TimeTypeDef after_set = { 0 }; HAL_RTC_GetTime(&hrtc, &after_set, RTC_FORMAT_BIN); // to get a valid SecondFraction - set_subsecond(0, after_set.SecondFraction, ¤t_date_time); + set_subsecond(0, after_set.SecondFraction, time_ptr->nanosecond); // dummy get date HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); @@ -134,19 +132,17 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr, case NX_PTP_CLIENT_CLOCK_ADJUST: TX_DISABLE - nx_ptp_client_utility_convert_time_to_date( - time_ptr, -PTP_UTC_OFFSET, ¤t_date_time); - HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN); set_subsecond( rtc_time.SecondFraction - rtc_time.SubSeconds, - rtc_time.SecondFraction, ¤t_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up) + rtc_time.SecondFraction, time_ptr->nanosecond); // (between 0 and PREDIV_S (aka SecondFraction), counting up) - TX_RESTORE // dummy get date HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN); + + TX_RESTORE break; case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE: HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);