Skip to content

Added micros64 and used to fix and improve gettimeofday. #3830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ void analogWriteRange(uint32_t range);

unsigned long millis(void);
unsigned long micros(void);
uint64_t micros64(void);
void delay(unsigned long);
void delayMicroseconds(unsigned int us);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
Expand Down
7 changes: 7 additions & 0 deletions cores/esp8266/core_esp8266_wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ unsigned long ICACHE_RAM_ATTR micros() {
return system_get_time();
}

uint64_t ICACHE_RAM_ATTR micros64() {
uint32_t low32_us = system_get_time();
uint32_t high32_us = micros_overflow_count + ((low32_us < micros_at_last_overflow_tick) ? 1 : 0);
uint64_t duration64_us = (uint64_t)high32_us << 32 | low32_us;
return duration64_us;
}

void ICACHE_RAM_ATTR delayMicroseconds(unsigned int us) {
os_delay_us(us);
}
Expand Down
19 changes: 12 additions & 7 deletions cores/esp8266/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,27 @@ struct timeval {

extern char* sntp_asctime(const struct tm *t);
extern struct tm* sntp_localtime(const time_t *clock);
extern uint64_t micros64();

// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
#define DIFF1900TO1970 2208988800UL

static int s_daylightOffset_sec = 0;
static long s_timezone_sec = 0;
static time_t s_bootTime = 0;
static bool s_bootTimeSet = false;
static uint64_t s_bootTime_us = 0;

// calculate offset used in gettimeofday
static void ensureBootTimeIsSet()
{
if (!s_bootTime)
// Check just a bool flag instead of the full 64-bit s_bootTime for zero.
if (!s_bootTimeSet)
{
time_t now = sntp_get_current_timestamp();
if (now)
time_t now_s = sntp_get_current_timestamp();
if (now_s)
{
s_bootTime = now - millis() / 1000;
s_bootTime_us = now_s * 1000000ULL - micros64();
s_bootTimeSet = true;
}
}
}
Expand Down Expand Up @@ -100,8 +104,9 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
if (tp)
{
ensureBootTimeIsSet();
tp->tv_sec = s_bootTime + millis() / 1000;
tp->tv_usec = micros();
uint64_t currentTime_us = s_bootTime_us + micros64();
tp->tv_sec = currentTime_us / 1000000ULL;
tp->tv_usec = currentTime_us % 1000000ULL;
}
return 0;
}