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.

Example for setting the system time

Other Parts Discussed in Thread: TM4C1294NCPDT

Please bear with me as although I've been developing software in higher level languages for decades, my experience with embedded development is measured in weeks. 

I am using the Tiva C Connected board (TM4C1294NCPDT) and Code Composer Studio 6 (TI-RTOS 2.0.2.36, compiler TI v5.1.6) to develop some logging software that needs to include the time. I have successfully written a simple program to start the beta SNTP client and pull the current time from an NTP server, but I'm lost as to how to write that time back to the system. Normally, I would use something like settimeofday, but that doesn't seem to be an option. Any help would be greatly appreciated.

Also, I've done some searching for books related to TI RTOS and haven't found a whole lot. Are there any recommended books available for getting familiar with the TI RTOS?

Thanks in advance.

  • Hi Brian --

    Did you find the MYTIME module in the documentation?    If you use 'SNTP_start(MYTIME_gettime, MYTIME_settime, 0)' then the SNTP client will use the MYTIME module to track time.

    We are working to integrate SNTP time with the runtime libraries 'time()', etc. APIs, but this work is still not complete.  That is one of the reasons why SNTP is tagged "beta".

    You should be able to call MYTIME_gettime() to get the current time.

    You can also override the rts library's time() function with a function of your own.  This will cause time() to call MYTIME_gettime().

    uint32_t time()
    {
        return (MYTIME_gettime());
    }


    We still have some work to integrate SNTP better with the runtime library's time() APIs and also the FAT filesystem.  We are actively working this and may override time() in a way similar to the above for the different compiler toolchains.  We will also be using the assorted device's built in RTC modules to track time instead of using the Clock module as is being done in the current MYTIME module.

    -Karl-

  • Karl,

    Thank you very much for your quick reply. I've implemented your suggestion and had to make a few tweaks to the time() function for a few reasons.

    1. The comments within the sntp.h file state that the first argument to SNTP_start() should be a function pointer that returns the number of seconds since 1/1/1970. I found this a bit strange considering that I believe NTP uses an epoch of 1/1/1900. Nonetheless, your suggestion to pass MYTIME_gettime and MYTIME_settime into SNTP_start doesn't seem to cause any issues.

    2. Before the SNTP client has set the time via MYTIME_settime, MYTIME_gettime, I needed to call MYTIME_init (and MYTIME_exit where necessary to clean up any resources). If this isn't done, the MYTIME clock will not count forward and will be seemingly "stuck" on the set time.

    3. It seems as though the SNTP client may have a bug related to requiring an IPv6 specific symbol definition even when not using IPv6. Line 246 of sntp.c is referencing IPV6_UNSPECIFIED_ADDRESS which isn't defined unless I enable IPv6 in my app.cfg. It may be that I'm missing an include somewhere, but I got around the issue by including the following definition in my code:

    IP6N  IPV6_UNSPECIFIED_ADDRESS;

    Anyway, my time() implementation is below for anyone who is interested. There are a few peculiarities, the biggest one is that both localtime() and gmtime() return GMT. This isn't a big deal for me, but may be an issue for someone else.

    const unsigned long seventyYears = 2208988800UL;
    
    uint32_t time(time_t *timer)
    {
    	time_t result = (time_t)MYTIME_gettime() + seventyYears;
    	if(timer) *timer = result;
    
    	return(result);
    }

    Thank you very much for your help. It was very useful.

    -Brian