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.

LAUNCHXL-CC1312R1: How to use RTC to keep track of updated current time

Part Number: LAUNCHXL-CC1312R1
Other Parts Discussed in Thread: SYSBIOS

Hello,

I want to use the RTC that is on the board to keep track of real time. I want to have the time updated even while it is asleep

I read over the SYS/BIOS TI-RTOS Kernel User Guide (pg. 131) Section 5.4 to write my program. But for some reason, the time is not incrementing real time. 

OUTPUT:

----------- CODE -----------

#include <stdio.h>
#include <time.h>
#include <ti/sysbios/hal/Seconds.h>


// Seconds module APIs
void Seconds_set(UInt32 seconds);
UInt32 Seconds_get(void);

// Function prototypes:
void RTC_set(UInt32 *sec);
void RTC_get();
void RTC_print();
void delay();

//Global variables
UInt32 sec;

int main(void) {

int i;

// Console prompt to ask for current time
printf("Insert current time in seconds since Epoch (Jan 1, 1970)\n");

// User input of time
scanf("%d", &sec);

// Set seconds count
RTC_set(&sec);

/* DEBUG TEST:
* GET CURRENT TIME FOR 10 ITERATIONS TO SEE IF TIME INCREMENTS REAL-TIME
* WITH A DELAY ADDED
*/
for (i=0; i < 10; i++)
{
RTC_get();
delay();

}
return 0;
}

//Functions

void delay() {
int t;
for (t = 0; t < 50000; t++) {
int i = Seconds_get(); // Bogus way of trying to get a delay
}
}

// Set the Seconds module
void RTC_set(UInt32 *sec) {

Seconds_set(*sec); 

RTC_get(); //Call get function
}

// Get the current time relative to Jan 1, 1970
void RTC_get()
{
UInt32 t;
t = Seconds_get();

RTC_print(); //Call print function
}

// Print out the current time
void RTC_print()
{
time_t t1;
struct tm *ltm;

t1 = time(NULL);
ltm = localtime(&t1);
printf("Time(GMT): %02d/%02d/%04d %02d:%02d:%02d\n", ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_year + 1900, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
}

Thank you.

  • Not sure what you are doing in the RTC_functions, and why you are using them.

    I tested the following, and as you can see, the time is updated by 5 s (sleep(5))

    siri

  • Thank you for your response. I want to utilize the RTC to be able to set the time and read the time. I want to use the REAL TIME as timestamps on my logs. 

  • I get errors when I implement this code. It says unresolved symbol main.

  • The seconds module uses the RTC, and you should not need to access the RTC directly (not recommended when using RTOS).

    The code was tested with the empty example from the latest SDK:

    C:\ti\simplelink_cc13x2_26x2_sdk_5_10_00_48\examples\rtos\CC1312R1_LAUNCHXL\drivers\empty\tirtos\ccs

    The empty.c file should contain the following code:

    #include <unistd.h>
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver configuration */
    #include "ti_drivers_config.h"
    
    #include <ti/sysbios/hal/Seconds.h>
    
    #include <xdc/runtime/System.h>
    #include <time.h>
    
    static uint32_t t;
    static time_t t1;
    static struct tm *ltm;
    static char *curTime1;
    
    void *mainThread(void *arg0)
    {
       /* set to today’s date in seconds since Jan 1, 1970 */
       Seconds_set(1624021374); /* Friday, June 18, 2021 1:02:54 PM */
       /* retrieve current time relative to Jan 1, 1970 */
       t = Seconds_get();
    
       t1 = time(NULL);
       ltm = localtime(&t1);
       curTime1 = asctime(ltm);
    
        while (1) {
    
            sleep(5);
    
            t = Seconds_get();
            t1 = time(NULL);
            ltm = localtime(&t1);
            curTime1 = asctime(ltm);
    
            System_printf("Time(GMT): %s\n", curTime1);
        }
    }
    

    and the release.cfg should have the following modifications to make System_printf work:

    *
     * The System.SupportProxy defines a low-level implementation of System
     * functions such as System_printf(), System_flush(), etc.
     *
     * Pick one pair:
     *  - SysMin
     *      This module maintains an internal configurable circular buffer that
     *      stores the output until System_flush() is called.
     *      The size of the circular buffer is set via SysMin.bufSize.
     *  - SysCallback
     *      SysCallback allows for user-defined implementations for System APIs.
     *      The SysCallback support proxy has a smaller code footprint and can be
     *      used to supply custom System_printf services.
     *      The default SysCallback functions point to stub functions. See the
     *      SysCallback module's documentation.
     */
    //var SysMin = xdc.useModule('xdc.runtime.SysMin');
    //SysMin.bufSize = 1024;
    //System.SupportProxy = SysMin;
    //var SysCallback = xdc.useModule('xdc.runtime.SysCallback');///
    //System.SupportProxy = SysCallback;///
    //SysCallback.abortFxn = "&myUserAbort";
    //SysCallback.exitFxn  = "&myUserExit";
    //SysCallback.flushFxn = "&myUserFlush";
    //SysCallback.putchFxn = "&myUserPutch";
    //SysCallback.readyFxn = "&myUserReady";
    var SysStd = xdc.useModule("xdc.runtime.SysStd");
    var System = xdc.useModule("xdc.runtime.System");
    System.SupportProxy = SysStd;

    Siri

  • Thank you! My time is incrementing now, but there is a lot of error. After about 5 minutes, the time is off by 3 minutes. If the seconds module is using the RTC, why is the discrepancy so big? 

    Could you share the full release.cfg file? Don't we need to add var Seconds = xdc.useModule("ti.sysbios.hal.Seconds.h") as well??

  • Yes, you need to add Seconds = ..... as well.

    I see the same thing when it comes to the accuracy and will need time to find someone that can help me look into this.

    BR

    Siri

  • The accuracy is good now. Instead of printing out to the CCS console, I implemented the code in the sensor sdk example so that I can print out the timestamps through the UART. Something with System_printf printing out onto the debugger console has a very big delay due to the target pausing and resuming most of the time. So printing it out through UART fixed the problem. Thank you for your help.