diff --git a/cube/User/Inc/wizHelper.h b/cube/User/Inc/wizHelper.h index 9e10d5c..0996d61 100644 --- a/cube/User/Inc/wizHelper.h +++ b/cube/User/Inc/wizHelper.h @@ -7,9 +7,18 @@ #define NTP_SERVER "0.de.pool.ntp.org" +typedef struct { + uint64_t seconds; + uint32_t missedUpdates; + bool valid; +} t_seconds; + int wizInit(); bool isNetworkAvailable(); uint8_t* wizGetIPAddress(); bool wizDnsQuery(char *name, uint8_t *ip); +t_seconds* wizGetSeconds(); + + #endif // _WIZHELPER_H_ diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index 9df7dde..c33c7fa 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -29,7 +29,8 @@ void my_errorHandler() { } void second_tick(void *handle) { - coloredMsg(LOG_GREEN, "Tick"); + t_seconds *seconds = wizGetSeconds(); + coloredMsg(LOG_GREEN, "Tick %d %ld %lld", seconds->valid, seconds->missedUpdates, seconds->seconds); } diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index 3a076b0..d0c9b94 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -28,7 +28,7 @@ static uint8_t dnsBuffer[DNS_BUFFER_SIZE]; static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE]; -static datetime curTime; +static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false }; extern const uint8_t DHCP_SOCK; extern const uint8_t DNS_SOCK; @@ -134,7 +134,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) { static void wizSNTPHandler(void *handle) { - if (networkAvailable) { coloredMsg(LOG_BLUE, "wizsh, about to call SNTP"); @@ -142,22 +141,36 @@ static void wizSNTPHandler(void *handle) { if (wizDnsQuery(NTP_SERVER, ntpServer)) { SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer); for (uint8_t i = 0; i < 16; i++) { - int8_t res = SNTP_run(&curTime); - coloredMsg(LOG_BLUE, "wizsh, res: %d", res); - if (res == 1) { - coloredMsg(LOG_BLUE, "wizsh, curTime: %04d-%02d-%02d %02d:%02d:%02d", - curTime.yy, curTime.mo, curTime.dd, - curTime.hh, curTime.mm, curTime.ss); - break; - } + datetime curTime; + int8_t res = SNTP_run(&curTime); + coloredMsg(LOG_BLUE, "wizsh, res: %d", res); + if (res == 1) { + if (seconds.seconds != curTime.seconds) { + coloredMsg(LOG_BLUE, "wizsh, time deviation: %lld %lld", + seconds.seconds, curTime.seconds); + } + seconds.seconds = curTime.seconds; + seconds.valid = true; + coloredMsg(LOG_BLUE, "wizsh, curTime: %lld", seconds.seconds); + break; + } } } else { + seconds.missedUpdates += 1; coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name"); } } } +static void wizSecondsHandler(void *handle) { + seconds.seconds += 1; +} + + +t_seconds* wizGetSeconds() { + return &seconds; +} static void wizPhyLinkHandler(void *handle) { // this handler is anyhow called with a 1s period, so we reuse it for the DNS timer @@ -270,5 +283,7 @@ int wizInit() { schAdd(wizPhyLinkHandler, NULL, 0, 1000); coloredMsg(LOG_BLUE, "wizI, PhyLink handler scheduled"); + schAdd(wizSecondsHandler, NULL, 0, 1000); + coloredMsg(LOG_BLUE, "wizI, Seconds handler scheduled"); return 0; } diff --git a/cube/ioLibrary_Driver b/cube/ioLibrary_Driver index 27f6468..0957fc4 160000 --- a/cube/ioLibrary_Driver +++ b/cube/ioLibrary_Driver @@ -1 +1 @@ -Subproject commit 27f646860da3b17dd1cf489988e39329ca1cc92d +Subproject commit 0957fc483052e5784b4d650ddf572ad91142e34a