gettimeofday() returning erratic values

stefaanv
Posts: 5
Joined: Wed Sep 06, 2017 7:25 am

gettimeofday() returning erratic values

Postby stefaanv » Wed Dec 05, 2018 3:57 pm

We're developing an IoT application around the Wrover module. In this application, the ULP collects sensor data every five minutes while the main processors are in deep sleep. Once every hour, the main processor wakes up, collects the measurement data from the ULP and decides on proper actions.
Because it is crucial for us to like a correct timestamp to each measurement, we added a 32kHz crystal to the hardware, however in the first version of the hardware we didn't get the crystal type right so we're using the internal 150kHz RC oscillator. The selection for crystal or RC oscillator is done in the "make menuconfig" tool, we compile separate firmware for the batch with the wrong crystals.

Unfortunately, we see erratic behavior from the gettimeofday() function that we use in the main program to get the current RTC time. When calling this function successively, the returned values differ up to 100 seconds in both directions while the main program itself only takes about 3 seconds to run. A latter call can return a lesser (older) value.

Successive calls to the gettimeofday() function in successive program runs with a 1 hour deepsleep period in between return values that can be wrong by as much as 6000 seconds, also going in both directions.

The newer hardware which sync RTC time with a 32kHz crystal don't show this erratic behavior at all.
I'm surprised that such a simple, basic function can produce such erratic behavior. I'm also surprised how hard it is to find documentation about the gettimeofday() function. For instance, what is the meaning of the integer value that is returned ?

Has anyone else experienced the same problem ?
Is gettimeofday() the correct function to use to get RTC time ?
Any known cures for this problem ?

stefaanv
Posts: 5
Joined: Wed Sep 06, 2017 7:25 am

Re: gettimeofday() returning erratic values

Postby stefaanv » Wed Dec 05, 2018 4:41 pm

additional information:

The ULP wakeup every 5 minutes, which we assume is derived from the same clock source (either 32kHz crystal or 150kHr RC) does happen relatively sharp every 5 minutes. We do see a difference in accuracy between the crystal and the RC oscillator - as to be expected - but not the extreme variations we get from the gettimeofday() function.

ESP_Sprite
Posts: 9758
Joined: Thu Nov 26, 2015 4:08 am

Re: gettimeofday() returning erratic values

Postby ESP_Sprite » Thu Dec 06, 2018 2:26 am

Fyi, gettimeofday is a posix function, and from what I know, the newlib implementation we use confirms to that. No idea why it jitters so much for you; are you sure you're reading the seconds field of tv and not accidentally the microseconds field?

sergiomarina
Posts: 48
Joined: Wed Feb 05, 2020 6:29 pm

Re: gettimeofday() returning erratic values

Postby sergiomarina » Fri Feb 25, 2022 2:47 pm

i got a similar issue with settimeofday and gettimeofday that has been streamlined in the test here below:

============= an excerpt of my code ===========
time_t t = mktime(&tm);
now.tv_sec = t;
settimeofday(&now,NULL);
if (gettimeofday(&now,NULL) == 0)
printf("gettime success and now = %ld\n", now.tv_sec);
char *myDate = ctime(&now.tv_sec);
ESP_LOGI(TAG, "myDate: %s now %ld\n", myDate, now.tv_sec);
===========================================
returns
=============== from MONITOR ===============
now = 1665482054
gettime success and now = 1665480538
I (4641) EVT-HTTP_C: myDate: Tue Oct 11 09:28:58 2022
now 1665480538

===========================================

=======The initial time is acquired from a REST response as follows:
response = Tue Oct 11 09:54:14 2022
==================================================

since the settimeofday and the gettimeofday are adjacent they should return the same value for now.tv_sec
Vice versa they differ by "1516" (every time I run the code I got the same value)

do I make something wrong?
Thank you.

ESP_Sprite
Posts: 9758
Joined: Thu Nov 26, 2015 4:08 am

Re: gettimeofday() returning erratic values

Postby ESP_Sprite » Sun Feb 27, 2022 5:06 am

Do you zero-initialize 'now'? If there's garbage in the other fields, that may affect how the functions work.

sergiomarina
Posts: 48
Joined: Wed Feb 05, 2020 6:29 pm

Re: gettimeofday() returning erratic values

Postby sergiomarina » Mon Feb 28, 2022 9:25 am

Hi ESP_Sprite,
thank you for your answer.
Your hint was successful.
I tried the following:
struct timeval timeNow;
timeNow.tv_sec = 0;
but it did not work

At the end I come up to the following:
struct timeval timeNow;
settimeoftheday(&timeNow.tv_sec, NULL);
ESP_LOGI(TAG."myDate: %s and now: %ld\n", ctime(timeNow.tv_sec), timeNow.tv_sec);
which printed myDate: Thu Jan 1 01 00:00:06 1970
and now: 6

After that the "settimeoftheday + gettimeoftheday" is no more erratic.
I guess the dummy cycle above is needed for proper timeNow struct initializing.

Thank you.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: gettimeofday() returning erratic values

Postby WiFive » Mon Feb 28, 2022 2:24 pm

sergiomarina wrote: Hi ESP_Sprite,
thank you for your answer.
Your hint was successful.
I tried the following:
struct timeval timeNow;
timeNow.tv_sec = 0;
but it did not work

At the end I come up to the following:
struct timeval timeNow;
settimeoftheday(&timeNow.tv_sec, NULL);
ESP_LOGI(TAG."myDate: %s and now: %ld\n", ctime(timeNow.tv_sec), timeNow.tv_sec);
which printed myDate: Thu Jan 1 01 00:00:06 1970
and now: 6

After that the "settimeoftheday + gettimeoftheday" is no more erratic.
I guess the dummy cycle above is needed for proper timeNow struct initializing.

Thank you.
struct timeval timeNow = {0};

You can't just ignore the structure members you are not interested in.

settimeoftheday(&timeNow.tv_sec, NULL);

This happens to work because tv_sec is the first struct member but don't do this.

Who is online

Users browsing this forum: ESP_Sprite and 170 guests