BareMetal-OS-legacy icon indicating copy to clipboard operation
BareMetal-OS-legacy copied to clipboard

Newlib - Clean up gettimeofday()

Open IanSeyler opened this issue 12 years ago • 8 comments

Have the RTC interrupt increment a 64-bit value every second. On the first call to gettimeofday() check this value. If the value is less than 946684800 (2000/1/1 00:00:00) then we have an invalid date. gettimeofday() can then call the RTC hardware and calculate the real epoch time. gettimeofday() will then update the OS timer.

IanSeyler avatar Jul 05 '13 14:07 IanSeyler

As you just mentioned in #43, there is the possibility of a custom callback.

Is this callback replacing the original handler?

Because if it is, then I see no clean way of repeatedly increasing a 64-bit integer.

Else, there could be problems if this would be done in a custom handler (in case there can only be one of them).

(just throwing in some ideas)

benaryorg avatar Jul 13 '15 18:07 benaryorg

The custom callbacks do not affect the system interrupt code. The callback code is executed as soon as the interrupt handler is finished.

IanSeyler avatar Jul 13 '15 18:07 IanSeyler

So, you are planning to have the system interrupt triggered by the RTC to count up a 64-bit integer and have the gettimeofday (and possible other functions) return an accurate time based on that integer and the time saved by the hardware?

benaryorg avatar Jul 13 '15 18:07 benaryorg

Yes, that is correct.

IanSeyler avatar Jul 13 '15 19:07 IanSeyler

Then you could have some memory location (which is by default initialized to 0) which is used by all time functions to save the hardware time to (on first access just compare if it is zero) and in all of these functions just copy the hardwaretime into a register and add the counter.

benaryorg avatar Jul 13 '15 19:07 benaryorg

Preferably you could write one function (similar to time(0)) which just returns the result of the addition, which then is used in all others. No need to repeat yourself then.

benaryorg avatar Jul 13 '15 19:07 benaryorg

If you update the hardware time with this setup, you would just have to overwrite that memorylocation where the hardwaretime is saved in the firstplace with 0. Then all time functions would update automagically.

benaryorg avatar Jul 13 '15 19:07 benaryorg

Please note that this would assume that the RTC interrupt only happens once per second, if an application needs a different interval and wants to use the RTC for that, the whole thing would break.

benaryorg avatar Jul 13 '15 19:07 benaryorg