Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #31235

Re: newlib and time()

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.arch.embedded
Subject Re: newlib and time()
Date 2022-09-30 09:04 +0200
Organization A noiseless patient Spider
Message-ID <th64eh$uo0n$1@dont-email.me> (permalink)
References <th62c5$ufge$1@dont-email.me>

Show all headers | View raw


On 30/09/2022 08:29, pozz wrote:
> I often use newlib standard C libraries with gcc toolchain for Cortex-M 
> platforms. It sometimes happens I need to manage calendar time: seconds 
> from 1970 or broken down time. And it sometimes happens I need to manage 
> timezone too, because the time reference comes from NTP (that is UTC).
> 
> newlib as expected defines a time() function that calls a syscall 
> function _gettimeofday(). It should be defined as in [1].
> 
> What is it? There's an assembler instruction that I don't understand:
> 
>    asm ("swi %a1; mov %0, r0" : "=r" (value): "i" (SWI_Time) : "r0");
> 

It is a "software interrupt" instruction.  If you have a separation of 
user-space and supervisor-space code in your system, this is the way you 
make a call to supervisor mode.

> What is the cleanest way to override this behaviour and let newlib 
> time() to return a custom calendar time, maybe counted by a local RTC, 
> synchronized with a NTP?
> 
> The solution that comes to my mind is to override _gettimeofday() by 
> defining a custom function.
> 

Yes, that's the way to do it.

Or define your own time functions that are appropriate to the task.  I 
have almost never had a use for the standard library time functions - 
they are too much for most embedded systems which rarely need all the 
locale stuff, time zones, and tracking leap seconds, while lacking the 
stuff you /do/ need like high precision time counts.

Use a single 64-bit monotonic timebase running at high speed (if your 
microcontroller doesn't support that directly, use a timer with an 
interrupt for tracking the higher part).  That's enough for nanosecond 
precision for about 600 years.

For human-friendly time and dates, either update every second or write 
your own simple second-to-human converter.   It's easier if you have 
your base point relatively recently (there's no need to calculate back 
to 01.01.1970).

If you have an internet connection, NTP is pretty simple if you are 
happy to use the NTP pools as a rough reference without trying to do 
millisecond synchronisation.


> 
> 
> [1] https://github.com/eblot/newlib/blob/master/libgloss/arm/syscalls.c
> 

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

newlib and time() pozz <pozzugno@gmail.com> - 2022-09-30 08:29 +0200
  Re: newlib and time() David Brown <david.brown@hesbynett.no> - 2022-09-30 09:04 +0200
    Re: newlib and time() pozz <pozzugno@gmail.com> - 2022-09-30 11:29 +0200
      Re: newlib and time() Clifford Heath <no_spam@please.net> - 2022-09-30 21:51 +1000
        Re: newlib and time() pozz <pozzugno@gmail.com> - 2022-09-30 15:32 +0200
          Re: newlib and time() Richard Damon <Richard@Damon-Family.org> - 2022-09-30 09:48 -0400
            Re: newlib and time() Clifford Heath <no_spam@please.net> - 2022-10-01 08:43 +1000
        Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2022-09-30 11:55 -0700
          Re: newlib and time() Clifford Heath <no_spam@please.net> - 2022-10-01 08:48 +1000
            Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2022-09-30 16:11 -0700
      Re: newlib and time() David Brown <david.brown@hesbynett.no> - 2022-09-30 14:38 +0200
      Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2022-09-30 11:42 -0700
        Re: newlib and time() pozz <pozzugno@gmail.com> - 2022-10-03 00:09 +0200
          Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2022-10-02 19:02 -0700
            Re: newlib and time() pozz <pozzugno@gmail.com> - 2022-12-30 17:08 +0100
              Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2022-12-30 17:35 -0700
                Re: newlib and time() pozz <pozzugno@gmail.com> - 2023-01-04 17:09 +0100
                Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2023-01-04 12:19 -0700
                Re: newlib and time() George Neuner <gneuner2@comcast.net> - 2023-01-04 20:12 -0500
                Re: newlib and time() Don Y <blockedofcourse@foo.invalid> - 2023-01-04 19:40 -0700
          Re: newlib and time() Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2022-10-03 10:20 +0300
  Re: newlib and time() Clifford Heath <no_spam@please.net> - 2022-09-30 17:12 +1000
  Re: newlib and time() Theo <theom+news@chiark.greenend.org.uk> - 2022-09-30 14:06 +0100

csiph-web