This commit is contained in:
PoliEcho 2025-09-13 11:07:46 +02:00
parent d6c8082338
commit 5010544ba9

View File

@ -29,12 +29,19 @@
#define PIN_MOSI 19 #define PIN_MOSI 19
extern "C" void sync_system_time(unsigned int sec, unsigned int usec) { extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
if (sec == 0) {
printf("SNTP: Invalid timestamp received\n");
return;
}
printf("SNTP callback received: %lu\n", (unsigned long)sec); printf("SNTP callback received: %lu\n", (unsigned long)sec);
time_t unix_time = (time_t)sec;
// Convert NTP timestamp to Unix timestamp
#define NTP_DELTA 2208988800UL
time_t unix_time = (time_t)(sec - NTP_DELTA);
u16_t timezone_offset = 7200; u16_t timezone_offset = 7200;
// Apply timezone offset for local time
unix_time += timezone_offset; unix_time += timezone_offset;
struct tm *time_info = gmtime(&unix_time); struct tm *time_info = gmtime(&unix_time);
@ -43,13 +50,29 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
return; return;
} }
datetime_t dt = {.year = static_cast<int16_t>(time_info->tm_year + 1900), printf("Got SNTP response: %02d/%02d/%04d %02d:%02d:%02d\n",
time_info->tm_mday, time_info->tm_mon + 1, time_info->tm_year + 1900,
time_info->tm_hour, time_info->tm_min, time_info->tm_sec);
datetime_t dt = {
.year = static_cast<int16_t>(time_info->tm_year + 1900),
.month = static_cast<int8_t>(time_info->tm_mon + 1), .month = static_cast<int8_t>(time_info->tm_mon + 1),
.day = static_cast<int8_t>(time_info->tm_mday), .day = static_cast<int8_t>(time_info->tm_mday),
.dotw = static_cast<int8_t>(time_info->tm_wday), .dotw = static_cast<int8_t>(time_info->tm_wday),
.hour = static_cast<int8_t>(time_info->tm_hour), .hour = static_cast<int8_t>(time_info->tm_hour),
.min = static_cast<int8_t>(time_info->tm_min), .min = static_cast<int8_t>(time_info->tm_min),
.sec = static_cast<int8_t>(time_info->tm_sec)}; .sec = static_cast<int8_t>(time_info->tm_sec)
};
// Set RTC
if (rtc_set_datetime(&dt)) {
printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n",
dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec,
(timezone_offset == 0) ? "UTC" : "Local");
} else {
printf("SNTP: Failed to set RTC\n");
}
// Sanity checks // Sanity checks
if (dt.year < 2020 || dt.year > 2100 || dt.month < 1 || dt.month > 12 || if (dt.year < 2020 || dt.year > 2100 || dt.month < 1 || dt.month > 12 ||
@ -58,10 +81,10 @@ extern "C" void sync_system_time(unsigned int sec, unsigned int usec) {
return; return;
} }
// Set RTC // Set RTC with error handling
if (rtc_set_datetime(&dt)) { if (rtc_set_datetime(&dt)) {
printf("SNTP: Time set to %04d-%02d-%02d %02d:%02d:%02d %s\n", dt.year, printf("SNTP: Time synchronized to %04d-%02d-%02d %02d:%02d:%02d %s\n",
dt.month, dt.day, dt.hour, dt.min, dt.sec, dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec,
(timezone_offset == 0) ? "UTC" : "Local"); (timezone_offset == 0) ? "UTC" : "Local");
} else { } else {
printf("SNTP: Failed to set RTC\n"); printf("SNTP: Failed to set RTC\n");