@@ -10,16 +10,16 @@ NX_PTP_DATE_TIME *ptp_date_time;
1010
1111UINT interrupt_save ;
1212
13- static UINT us_to_second_ticks (ULONG ns , UINT second_fractions )
13+ static UINT us_to_second_ticks (ULONG us , UINT second_fractions )
1414{
1515 // Second fraction = SS / (PREDIV_S + 1)
16- return ns * (second_fractions + 1L ) /
16+ return ( uint64_t ) us * (second_fractions + 1L ) /
1717 1000000L ; // TODO: double check overflow issues
1818}
1919
2020static ULONG second_ticks_to_us (UINT second_ticks , UINT second_fractions )
2121{
22- return 1000000L * second_ticks /
22+ return ( uint64_t ) 1000000L * second_ticks /
2323 (second_fractions + 1 ); // TODO: double check overflow issues
2424}
2525
@@ -47,7 +47,7 @@ static LONG diff_ptp_date_time(NX_PTP_DATE_TIME *time1, NX_PTP_DATE_TIME *time2)
4747#define SECS_PER_DAY (24 * SECS_PER_HOUR)
4848#define SECS_PER_YEAR (365 * SECS_PER_DAY)
4949
50- LONG seconds_diff = 0 ;
50+ int64_t seconds_diff = 0 ;
5151
5252 seconds_diff += (time2 -> year - time1 -> year ) * SECS_PER_YEAR ;
5353 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,
6565{
6666 switch (operation ) {
6767 case NX_PTP_CLIENT_CLOCK_INIT :
68- HAL_RTC_Init (
69- & hrtc1 ); // do I have to or is this done elsewhere?
7068 break ;
7169
7270 case NX_PTP_CLIENT_CLOCK_SET :
@@ -99,6 +97,8 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
9997
10098 HAL_RTC_SetTime (& hrtc1 , & rtp_time , RTC_FORMAT_BCD );
10199
100+ HAL_RTC_SetDate (& hrtc1 , & rtp_date , RTC_FORMAT_BCD );
101+
102102 set_subsecond (rtc_sub_seconds .SecondFraction -
103103 rtc_sub_seconds .SubSeconds ,
104104 rtc_sub_seconds .SubSeconds );
@@ -110,7 +110,8 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
110110 RTC_DateTypeDef rtc_date = {};
111111
112112 HAL_RTC_GetTime (& hrtc1 , & rtc_time , RTC_FORMAT_BCD );
113- HAL_RTC_GetDate (& hrtc1 , & rtc_date , RTC_FORMAT_BCD );
113+ HAL_RTCEx_GetTimeStamp (& hrtc1 , & rtc_time , & rtc_date ,
114+ RTC_FORMAT_BCD );
114115
115116 NX_PTP_DATE_TIME rtc_ptp_date_time = {
116117 .year = rtp_date .Year ,
@@ -126,13 +127,13 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
126127 rtc_time .SecondFraction )
127128 };
128129
129- LONG secondsDiff = diff_ptp_date_time (
130+ int64_t secondsDiff = diff_ptp_date_time (
130131 ptp_date_time , & rtc_ptp_date_time );
131132
132133 NX_PTP_TIME current_ptp_time = {
133134 .second_high = ptp_time -> second_high ,
134135 .second_low = ptp_time -> second_low ,
135- .nanosecond = rtc_ptp_date_time . nanosecond
136+ .nanosecond = 0
136137 };
137138
138139 if (secondsDiff >
@@ -150,7 +151,9 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
150151
151152 time_ptr -> second_high = current_ptp_time .second_high ;
152153 time_ptr -> second_low = current_ptp_time .second_low ;
153- time_ptr -> nanosecond = current_ptp_time .nanosecond ;
154+ time_ptr -> nanosecond =
155+ rtc_ptp_date_time
156+ .nanosecond ; // pull directly from rtc
154157
155158 TX_RESTORE
156159 break ;
0 commit comments