gettimeofday() returning erratic values
Posted: 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 ?
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 ?