views:

801

answers:

4

I have an embedded system (ARM 9263) running an RTOS, IAR tools. The system supports the standard time() function which gives me the current time. I need the reverse call, that is I need to set the time - is there a "C" standard way to do this? I've googled around, sure thought it would be obvious, but perhaps it is platform dependent? I'm not sure why, since time() is not - any ideas? Thanks!

+2  A: 

There is settimeofday(2) which allows you to do that -- but it is POSIX and not ANSI C.

You may also want to check clock_settime, which perhaps may be more applicable to embedded systems. See this manpage, for example.

nimrodm
+5  A: 

Using the IAR toolset the time of day C runtime API (time()) can be overridden using the example in ARM\src\lib\time.c. The default routine always returns -1, an indication that the CRT has no idea what time it is. Once you provide your own implementation of time(), which will obtain the time of day from a source that depends on your tartget platform and/or RTOS, you can set the time of day by updating whatever that time source is. IAR may well have already done this for their RTOS - I haven't used IAR's PowerPac RTOS.

The details of how this works for another RTOS or a system with no RTOS is outlined in the IAR C/C++ Development Guide.

For example, on a system I've worked on that uses an ST Micro STM32 microscontroller, the real time clock (RTC) is set to tick once per second, and the time() library function simply returns the value in the RTC. Setting a new date/time is a matter of setting the RTC with a new value. The RTC's time counter is set with a Unix epoch value (seconds since 1 Jan 1970), which allows the rest of the library functions from time.h to work just fine (up to some time around in 2035 when 32-bit overflows start wreaking havoc).

The calendar routines in the IAR DLIB C runtime library support dates through 2035-12-31 (they overflow before 2038 I suspect because internal calcuations use a 1 Jan 1900 epoch). If you use the Unic epoch, the other DLIB routines more or less just work - I'm not sure what level of effort would be required to use a different epoch.

Michael Burr
+1  A: 

There is no standard way to do this in C or C++. You'll have to look toward your OS vendor for an API and/or your compiler or standard library vendor for some kind of extension.

Brian Neal
+1  A: 

The IAR tools are not target specific, so nothing in the library knows how to set the time on your hardware (or read it for that matter), that is down to your board-support package (which typically maps your hardware to the standard library amongst other things).

Not all targets even have an RTC. IAR's tools do provide support and for a number of specific microcontrollers and off-the-shelf development boards, so you may find that it is already done for you - you'll just need to look.

Your micro may have an internal RTC, or you may have one externally, but "ARM 9263" does not itself define an RTC, so you'd have to be more specific - RTC's are typically added as vendor specific peripherals. Then it would be a case of getting the part's user manual and writing the necessary registers.

Clifford