Skip to content

Commit 43e9cab

Browse files
committed
use settimeofday's tv->usec (esp8266#1679)
1 parent e53cece commit 43e9cab

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

cores/esp8266/coredecls.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
extern "C" {
77
#endif
88

9-
extern bool s_bootTimeSet;
10-
119
// TODO: put declarations here, get rid of -Wno-implicit-function-declaration
1210

11+
extern bool timeshift64_is_set;
12+
13+
void tune_timeshift64 (uint64_t now_us);
1314
void sntp_force_request (void);
1415
void settimeofday_cb (void (*cb)(void));
1516

cores/esp8266/sntp-lwip2.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
6666
}
6767
if (tv) /* after*/
6868
{
69+
// can't call lwip1.4's static sntp_set_system_time()
6970
os_printf(stod14);
7071

7172
// reset time subsystem
72-
s_bootTimeSet = false;
73+
timeshift64_is_set = false;
7374

7475
return -1;
7576
}
@@ -448,11 +449,10 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
448449
}
449450
if (tv) /* after*/
450451
{
451-
sntp_set_system_time(tv->tv_sec);
452-
// XXX FIXME TODO: efficiently use provided tv->tv_sec
453-
454452
// reset time subsystem
455-
s_bootTimeSet = false;
453+
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
454+
455+
sntp_set_system_time(tv->tv_sec);
456456

457457
if (_settimeofday_cb)
458458
_settimeofday_cb();

cores/esp8266/time.c

+8-16
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,13 @@ extern uint64_t micros64();
3737
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
3838
#define DIFF1900TO1970 2208988800UL
3939

40-
bool s_bootTimeSet = false;
41-
static uint64_t s_bootTime_us = 0;
40+
bool timeshift64_is_set = false;
41+
static uint64_t timeshift64 = 0;
4242

43-
// calculate offset used in gettimeofday
44-
static void ensureBootTimeIsSet()
43+
void tune_timeshift64 (uint64_t now_us)
4544
{
46-
// Check just a bool flag instead of the full 64-bit s_bootTime for zero.
47-
if (!s_bootTimeSet)
48-
{
49-
time_t now_s = sntp_get_current_timestamp();
50-
if (now_s)
51-
{
52-
s_bootTime_us = now_s * 1000000ULL - micros64();
53-
s_bootTimeSet = true;
54-
}
55-
}
45+
timeshift64 = now_us - micros64();
46+
timeshift64_is_set = true;
5647
}
5748

5849
static void setServer(int id, const char* name_or_ip)
@@ -102,8 +93,9 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
10293
(void) tzp;
10394
if (tp)
10495
{
105-
ensureBootTimeIsSet();
106-
uint64_t currentTime_us = s_bootTime_us + micros64();
96+
if (!timeshift64_is_set)
97+
tune_timeshift64(sntp_get_current_timestamp() * 1000000ULL);
98+
uint64_t currentTime_us = timeshift64 + micros64();
10799
tp->tv_sec = currentTime_us / 1000000ULL;
108100
tp->tv_usec = currentTime_us % 1000000ULL;
109101
}

libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino

+25-9
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@
3636
#define TZ_SEC ((TZ)*3600)
3737
#define DST_SEC ((DST_MN)*60)
3838

39+
timeval cbtime; // time set in callback
40+
bool cbtime_set = false;
41+
3942
void time_is_set (void)
4043
{
44+
gettimeofday(&cbtime, NULL);
45+
cbtime_set = true;
4146
Serial.println("------------------ settimeofday() was called ------------------");
4247
}
4348

@@ -103,6 +108,17 @@ void loop() {
103108
Serial.println();
104109
Serial.println();
105110
}
111+
112+
if (cbtime_set)
113+
{
114+
Serial.print("gettimeofday at time-is-set-callback:");
115+
Serial.print((uint32_t)cbtime.tv_sec);
116+
Serial.print("/");
117+
Serial.print((uint32_t)cbtime.tv_usec);
118+
Serial.println("us");
119+
Serial.println();
120+
cbtime_set = false;
121+
}
106122

107123
// time from boot
108124
Serial.print("clock:");
@@ -147,24 +163,24 @@ void loop() {
147163
if (loop_count == 130) // 130=>13secs
148164
{
149165
Serial.println("------------------ WIFI ON ------------------");
150-
Serial.print("connected:");
151-
Serial.println(WiFi.status() == WL_CONNECTED);
166+
167+
timeval backtothefuture = { 0, 0 };
168+
settimeofday(&backtothefuture, nullptr);
169+
now = time(nullptr);
170+
Serial.print("reset time after settimeofday(0) (sec): ");
171+
Serial.println(now);
172+
152173
WiFi.mode(WIFI_STA);
153-
Serial.print("connected2:");
154-
Serial.println(WiFi.status() == WL_CONNECTED);
155174
WiFi.begin(SSID, SSIDPWD);
175+
Serial.print("reconnecting.");
156176
while (WiFi.status() != WL_CONNECTED)
157177
{
158178
delay(500);
159179
Serial.print(".");
160180
}
161181
Serial.println("reconnected");
162-
timeval backtothefuture = { 0, 0 };
163-
settimeofday(&backtothefuture, nullptr);
164-
now = time(nullptr);
165-
Serial.print("reset time: ");
166-
Serial.println(now);
167182
loop_count = 0;
183+
168184
sntp_force_request();
169185
}
170186

0 commit comments

Comments
 (0)