11#include "nxd_ptp_client.h"
22#include "rtc.h"
3- #include <stdio.h>
43#include "stm32h5xx_hal.h"
5- #include "stm32h5xx_hal_rtc.h"
64#include "u_tx_debug.h"
75#include <time.h>
86
@@ -22,14 +20,14 @@ static ULONG second_ticks_to_us(UINT second_ticks, UINT second_fractions)
2220 return (uint64_t )1000000L * second_ticks / (second_fractions + 1 );
2321}
2422
25- static void set_subsecond (UINT rtc_sub_second_tick , UINT second_fractions , NX_PTP_DATE_TIME * current_time )
23+ static void set_subsecond (UINT rtc_sub_second_tick , UINT second_fractions , ULONG nanoseconds )
2624{
2725 if (rtc_sub_second_tick > second_fractions ) {
2826 PRINTLN_ERROR ("rtc SS overflow" );
2927 }
3028
3129 UINT rtp_sub_second_tick = us_to_second_ticks (
32- current_time -> nanosecond / 1000 , second_fractions );
30+ nanoseconds / 1000 , second_fractions );
3331
3432 UINT offset_tick = 0 ; // ticks to go backwards
3533 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
4038 UINT delta = rtp_sub_second_tick - rtc_sub_second_tick ;
4139 offset_tick = (second_fractions + 1 ) - delta ;
4240 }
43- HAL_RTCEx_SetSynchroShift (& hrtc , offset_tick , offset_ahead_1s );
41+ HAL_RTCEx_SetSynchroShift (& hrtc , offset_ahead_1s , offset_tick );
4442}
4543
4644static 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,
111109 // Shift from 0 elapsed ticks to wherever PTP nanosecond says we should be.
112110 RTC_TimeTypeDef after_set = { 0 };
113111 HAL_RTC_GetTime (& hrtc , & after_set , RTC_FORMAT_BIN ); // to get a valid SecondFraction
114- set_subsecond (0 , after_set .SecondFraction , & current_date_time );
112+ set_subsecond (0 , after_set .SecondFraction , time_ptr -> nanosecond );
115113
116114 // dummy get date
117115 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,
134132 case NX_PTP_CLIENT_CLOCK_ADJUST :
135133 TX_DISABLE
136134
137- nx_ptp_client_utility_convert_time_to_date (
138- time_ptr , - PTP_UTC_OFFSET , & current_date_time );
139-
140135 HAL_RTC_GetTime (& hrtc , & rtc_time , RTC_FORMAT_BIN );
141136
142137 set_subsecond (
143138 rtc_time .SecondFraction - rtc_time .SubSeconds ,
144- rtc_time .SecondFraction , & current_date_time ); // (between 0 and PREDIV_S (aka SecondFraction), counting up)
139+ rtc_time .SecondFraction , time_ptr -> nanosecond ); // (between 0 and PREDIV_S (aka SecondFraction), counting up)
145140
146- TX_RESTORE
147141
148142 // dummy get date
149143 HAL_RTC_GetDate (& hrtc , & rtc_date , RTC_FORMAT_BIN );
144+
145+ TX_RESTORE
150146 break ;
151147 case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE :
152148 HAL_RTC_GetTime (& hrtc , & rtc_time , RTC_FORMAT_BIN );
0 commit comments