This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Issue using time() from standard library

Other Parts Discussed in Thread: F28M36P63C2

Hello,


I am using TI-RTOS on LM3S9DN6 and F28M36P63C2. My project consists of one MCU having a RTC supplying the others with a valid time at bootup. When trying to set the time on these MCUs without RTC I experienced a problem. Reading back time using

time_t rawtime;
rawtime=time(NULL);
tinfo = localtime(&rawtime);

I got only useful results as long as the debugger was on. Without debug connection or after disconnecting the device via menu in CSS no more useful results were returned. Seemingly setting the time at bootup was not working too, because that did not set the MCU time and the above code returned a valid time already before time setting. Trying to read time without debug connection gave 3 times a different result and afterwards returned always the same wrong value.

What  do I need to do to make it work? I have already implemented another software clock/calender but some external supplied code relies on a working time() implementation (without debugger being connected ;-))

Thanks for your proposals, Oliver


By the way, CSS 5.4 did not program Rev. 1 F28M36P63C2 by default. Only a small change in nowFlash config F28M36x.M3.xml fixed this issue.

  • Hi Oliver Langguth44979,

    Oliver Langguth44979 said:

    Reading back time using

    time_t rawtime;
    rawtime=time(NULL);
    tinfo = localtime(&rawtime);

    I got only useful results as long as the debugger was on.

    Yes, this is expected.  The RTS time function must get the time from somewhere.  If your hardware has a RTC, then it could get the time from that.

    But if there is no RTC, then where can it get the time from?  One way to do it is to get the time from the host PC - which of course requires an emulation connection from the target to the host PC.  This is the default method for the RTS time functions to get the time.  This is why the time functions did not work properly when you ran you application without the emulator connected.

    You can verify this by changing the time & date on your host PC to be incorrect.  Then re-run your (debug) application and you will see that it prints out the (incorrect) time that your PC has been configured to.

    So, in order to get a correct time when the device is running stand alone, you either need a real time clock or you could use a clock in software that acts as a real time clock.  Such a clock could simply count the time for you, but it won't be as accurate as the h/w RTC, and will be more susceptible to time drift.

    The next problem you will run into is, how to initialize the clock to be the right time?  This is typically done over a network connection, by communicating with an NTP (network time protocol) server, which returns the current (accurate) time to your application.

    You can find an example of a software clock (used to track time in software when there's no RTC) and SNTP client (used to get the time from an NTP server) in the NDK 2.23.00.00 product (see the release notes for link to documentation on the SNTP and MYTIME modules).  The NDK is available as a stand alone product, or you can find it shipped in the latest TI-RTOS 1.20.00.28 release.

    Steve

  • Hello Steve,


    thanks for your answer, it confirms my finding but does not answer how to make the time() function from c-library work. Do I understand you correctly that there is no way to somehow provide a function counting timer ticks as input for time()? My problem is the externally supplied piece of code using that function. To be able to easily update that software, I don't want to change it to use my own time function, which I already have implemented. I would rather find ways to make time() work like expected. Currently I have one MCU with RTC sending the current time at bootup to all other MCUs without RTC setting the correct time for my software clock there. It is not using NTP, but it works fine so far. That means getting the correct time is no issue. Calculating the correct number of seconds starting value for time() is done via mktime(). Now the only missing part is how to make it run using a RTOS supplied clock. Creating a periodic clock to count up the number of seconds would also be no issue. The missing link is only the time() function returning that value to other parts of code using that function.

    Is there a way to overwrite the standard library function with my own providing the correct time, or do I have to implement another function giving the needed input for time()?

    Regards, Oliver 

  • Hi Oliver,

    According to this wiki:

    "When not running under CCS, the program needs to find some other source from which it can discover time. In order to do that, a new implementation of time() must be provided. If the program is running under an operating system, that operating system should provide an implementation of time(); otherwise, the user must provide one."

    Can you try writing your own implementation of the time() function and compiling that into your program?  This should work using a "linker override trick" - that is, if you ensure that the object file that defines your version of the time() function is linked in before the RTS library, then your time() should win.

    Please see the above linked wiki for further details.

    Steve