Skip to content

Commit 1f7b7aa

Browse files
committed
it prints time!
1 parent 3b486bc commit 1f7b7aa

3 files changed

Lines changed: 61 additions & 52 deletions

File tree

NetX/inc/rtc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef TSECU_SHEPHERD_RTC_H
2-
#define TSECU_SHEPHERD_RTC_H
1+
#ifndef RTC_H
2+
#define RTC_H
33

44
#include "nxd_ptp_client.h"
55
#include "nx_api.h"
@@ -9,4 +9,4 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
99
NX_PACKET *packet_ptr,
1010
VOID *callback_data);
1111

12-
#endif //TSECU_SHEPHERD_RTC_H
12+
#endif // RTC_H

NetX/src/nxd_ptp_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6152,6 +6152,6 @@ LONG ns;
61526152
/* return result */
61536153
time_ptr -> second_high = sec_hi;
61546154
time_ptr -> second_low = sec_lo;
6155-
time_ptr -> nanosecond = ns;
6155+
time_ptr -> nanosecond = ns;
61566156
}
6157-
// clang-format on
6157+
// clang-format on

NetX/src/u_rtc.c

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "rtc.h"
33
#include <stdio.h>
44
#include "stm32h5xx_hal.h"
5+
#include "stm32h5xx_hal_rtc.h"
56
#include "u_tx_debug.h"
67
#include <time.h>
78

@@ -42,12 +43,33 @@ static void set_subsecond(UINT rtc_sub_second_tick, UINT second_fractions, NX_PT
4243
HAL_RTCEx_SetSynchroShift(&hrtc, offset_tick, offset_ahead_1s);
4344
}
4445

46+
static UINT rtc_to_nx_time(RTC_TimeTypeDef *rtc_time, RTC_DateTypeDef *rtc_date, NX_PTP_TIME *time_ptr) {
47+
// helpful way to get UNIX time from RTC so we can give it to NetX layer
48+
time_t current_time_unix = { 0 };
49+
struct tm tim = {0};
50+
tim.tm_year = rtc_date->Year + 100;
51+
tim.tm_mon = rtc_date->Month - 1;
52+
tim.tm_mday = rtc_date->Date;
53+
tim.tm_hour = rtc_time->Hours;
54+
tim.tm_min = rtc_time->Minutes;
55+
tim.tm_sec = rtc_time->Seconds;
56+
current_time_unix = mktime(&tim);
57+
58+
time_ptr->second_high = 0; // todo fix by 2038
59+
time_ptr->second_low = (ULONG) current_time_unix;
60+
time_ptr->nanosecond = 1000 * second_ticks_to_us(
61+
rtc_time->SecondFraction - rtc_time->SubSeconds,
62+
rtc_time->SecondFraction); // pull directly from rtc
63+
64+
return 0;
65+
}
66+
4567
UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
4668
UINT operation, NX_PTP_TIME *time_ptr,
4769
NX_PACKET *packet_ptr,
4870
VOID *callback_data)
4971
{
50-
NX_PTP_DATE_TIME *current_date_time;
72+
NX_PTP_DATE_TIME current_date_time = { 0 };
5173
RTC_TimeTypeDef rtc_time = {0};
5274
RTC_DateTypeDef rtc_date = {0};
5375

@@ -58,32 +80,39 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
5880
case NX_PTP_CLIENT_CLOCK_SET:
5981
TX_DISABLE
6082

61-
nx_ptp_client_utility_convert_time_to_date(
62-
time_ptr, -PTP_UTC_OFFSET, current_date_time);
83+
UINT status = nx_ptp_client_utility_convert_time_to_date(
84+
time_ptr, -PTP_UTC_OFFSET, &current_date_time);
6385

6486
rtc_time = (RTC_TimeTypeDef) {
65-
.Hours = current_date_time->hour,
66-
.Minutes = current_date_time->minute,
67-
.Seconds = current_date_time->second,
87+
.Hours = current_date_time.hour,
88+
.Minutes = current_date_time.minute,
89+
.Seconds = current_date_time.second,
6890
.TimeFormat = 0,
6991
};
7092

7193
rtc_date = (RTC_DateTypeDef) {
72-
.Year = current_date_time->year % 100,
73-
.Month = current_date_time->month,
74-
.Date = current_date_time->day,
75-
.WeekDay = current_date_time->weekday,
94+
.Year = current_date_time.year % 100,
95+
.Month = current_date_time.month,
96+
.Date = current_date_time.day,
97+
.WeekDay = current_date_time.weekday,
7698
};
7799

100+
// PRINTLN_INFO("GOT TIME SET: %d, sending NX time (%lu, %lu) from year %d, month %d, day %d, hour %d, minute %d, second %d",
101+
// status,
102+
// time_ptr->second_high, time_ptr->second_low,
103+
// current_date_time.year, current_date_time.month, current_date_time.day, current_date_time.hour, current_date_time.minute, current_date_time.second);
104+
// PRINTLN_INFO("GOT TIME SET, sending RTC from year %d, month %d, day %d, hour %d, minute %d, second %d",
105+
// rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds);
106+
78107
HAL_RTC_SetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
79108
HAL_RTC_SetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
80109

81110
// After SetTime, SubSeconds is reset to SecondFraction (0 elapsed).
82111
// Shift from 0 elapsed ticks to wherever PTP nanosecond says we should be.
83-
RTC_TimeTypeDef after_set = {};
112+
RTC_TimeTypeDef after_set = { 0 };
84113
HAL_RTC_GetTime(&hrtc, &after_set, RTC_FORMAT_BIN); // to get a valid SecondFraction
85-
set_subsecond(0, after_set.SecondFraction, current_date_time);
86-
114+
set_subsecond(0, after_set.SecondFraction, &current_date_time);
115+
87116
// dummy get date
88117
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
89118
TX_RESTORE
@@ -95,60 +124,40 @@ UINT nx_ptp_client_hard_clock_callback(NX_PTP_CLIENT *client_ptr,
95124
HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
96125
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
97126

98-
NX_PTP_DATE_TIME rtc_ptp_date_time = {
99-
.year = rtc_date.Year,
100-
.month = rtc_date.Month,
101-
.weekday = rtc_date.WeekDay,
102-
.day = rtc_date.Date,
103-
.hour = rtc_time.Hours,
104-
.minute = rtc_time.Minutes,
105-
.second = rtc_time.Seconds,
106-
.nanosecond =
107-
1000 * second_ticks_to_us(
108-
rtc_time.SecondFraction - rtc_time.SubSeconds,
109-
rtc_time.SecondFraction)
110-
};
111-
112-
// helpful way to get UNIX time from RTC so we can give it to NetX layer
113-
time_t current_time_unix = { 0 };
114-
struct tm tim = {0};
115-
tim.tm_year = rtc_date.Year + 100;
116-
tim.tm_mon = rtc_date.Month - 1;
117-
tim.tm_mday = rtc_date.Date;
118-
tim.tm_hour = rtc_time.Hours;
119-
tim.tm_min = rtc_time.Minutes;
120-
tim.tm_sec = rtc_time.Seconds;
121-
current_time_unix = mktime(&tim);
122-
127+
rtc_to_nx_time(&rtc_time, &rtc_date, time_ptr);
123128

124-
time_ptr->second_high = 0; // todo fix by 2038
125-
time_ptr->second_low = (ULONG) current_time_unix;
126-
time_ptr->nanosecond =
127-
rtc_ptp_date_time
128-
.nanosecond; // pull directly from rtc
129+
// PRINTLN_INFO("GOT TIME REQ, recv NX from year %d, month %d, day %d, hour %d, minute %d, second %d",
130+
// rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds);
129131

130132
TX_RESTORE
131133
break;
132134
case NX_PTP_CLIENT_CLOCK_ADJUST:
133135
TX_DISABLE
134136

135137
nx_ptp_client_utility_convert_time_to_date(
136-
time_ptr, -PTP_UTC_OFFSET, current_date_time);
138+
time_ptr, -PTP_UTC_OFFSET, &current_date_time);
137139

138140
HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
139141

140142
set_subsecond(
141143
rtc_time.SecondFraction - rtc_time.SubSeconds,
142-
rtc_time.SecondFraction, current_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up)
144+
rtc_time.SecondFraction, &current_date_time); // (between 0 and PREDIV_S (aka SecondFraction), counting up)
143145

144146
TX_RESTORE
145147

146148
// dummy get date
147149
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
148150
break;
149151
case NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE:
150-
nx_ptp_client_packet_timestamp_notify(
151-
client_ptr, packet_ptr, time_ptr); // this is probably wrong, instead we need to fetch the time via RTC
152+
HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN);
153+
HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN);
154+
155+
rtc_to_nx_time(&rtc_time, &rtc_date, time_ptr);
156+
157+
// PRINTLN_INFO("GOT TIME REQ, recv NX from year %d, month %d, day %d, hour %d, minute %d, second %d",
158+
// rtc_date.Year, rtc_date.Month, rtc_date.Date, rtc_time.Hours, rtc_time.Minutes, rtc_time.Seconds);
159+
160+
nx_ptp_client_packet_timestamp_notify(client_ptr, packet_ptr, time_ptr);
152161
break;
153162
case NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE: // do nothing
154163
break;

0 commit comments

Comments
 (0)