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.

RTC Tiva-Cortex M4(Development Kit-DK)

Other Parts Discussed in Thread: TM4C123GH6PGE, TM4C129XNCZAD

Hi,

we need example codes for RTC(i.e., built in Tiva Cortex M4- TM4C123GH6PGE) using Tiva M4 Development kit-DK.

Please Share us code related to RTC and steps to follow. 

  • Moving this to the Tiva forum.

  • Hello Srinivas

    The information is highly unclear as to you want an RTC but we do not have any clue on how the RTC you want to see implemented. Secondly, there is a data sheet which comes along with every device, did you happen to go through it before making a post of "in-the-box" solution?

    Suggested reading: General Purpose Timer and Hibernate Modules

    Regards

    Amit

  • hello,

    We need to show date and time in graphical display (OLED), using in-buit RTC for Tiva Cortex M4 microcontroller.

    We didnt find any datasheets along with device.

    yes we have hibernation module using RTC. But in this how we need to configure and Enable the RTC??

  • Hello Srinivas,

    You can download the data sheet from ti website. Since you have a Development Kit there are only two possibilities here: That the part is a TM4C123GH6PGE or a TM4C129XNCZAD. The data sheet for both can be downloaded from

    http://www.ti.com/product/TM4C123GH6PGE

    or

    http://www.ti.com/product/TM4C129XNCZAD

    The first thing you would need to do is to use the Hibernate module to get the RTC Counting. Once you can see RTC counting in Hibernate (either through debugger or through UART), then we can see how to interface the OLED.

    Regards

    Amit

  • Thank you for ur info.

    I have the datasheet as u mentioned above, and i already tested the counting in hibernate code.

    I need procedure for inserting date and time in display,using M4 controller.

    Please share the procedure.

  • Hello Srinivas,

    Since you plan to use the DK-TM4C123 board with the integrated OLED screen, you can use the following example in TIVAWare

    C:\ti\TivaWare_C_Series-2.1.0.12573\examples\boards\dk-tm4c123g\grlib_demo

    as the base start up code to get the RTC on screen

    Regards

    Amit

  • yes, I was already used grlib_demo example project for displaying some values on screen. in that example there is no RTC.

  • we are working on the Development kit only, already working on grlib_demo example. we requested that, need example code for RTC to display  date and time in graphical display(OLED).

  • Hi,

    To use the RTC for date/time display you need to use two functions: umktime() and ulocaltime() found in Tiva/utils/stdlib.c file.

    These are for expressing the date/time structure as a number of seconds to be loaded into HIBRTCLD register and read back and forming a tm struct. Then each member of this structure can be converted to string and sent to display.

    Petrei

  • Hi petrei,

    Thank u so much, 

    how can i use these function in grlib_demo example? i am displaying some parameters like voltage, current etc.,

    along with these, i need to display date and time.

    can i use directly in grlib_demo example,  function which u r mentioned above.?

  • Hi,

    Yes, of coarse, you can use all those functions. Just read the documentation or the source code - what you need to send as parameters and what you get.

    Petrei

  • Hi petrei,

    iam using  ucmptime(), umktime(), ulocaltime() in my grlib_demo project.

    how can i call above functions,and how can dispaly??

  • Hi,

    Try something like this:

    #include <time.h>
    //
    time_t seconds-to-write;
    struct tm my_date_time;
    //
    // first fill up the structure my_date_time with needed data 2014 07 02 11:34:32 etc.
    seconds_to_write = umktime(&my_date_time);
    if(seconds_to_write == (uint32_t)(-1)) // alert-mistake
    else HWREG(HIB_RTCLD) = seconds_to_write;
    
    // read back for display:
    ulocaltime(HWREG(HIB_RTCLD), &my_date_time);
    // now the structure is filled up; convert each member with sprint/snprintf to a buffer, then print the string on graphic display!
    

    Petrei

  • Hi,

    Iam trying to display date and time, but constant date and time displaying. no variation in the date and time.

    im following below steps. 

    ret=umktime(&sTimeGuess);
    //if(ret==-1){
    GrContextForegroundSet(&g_sContext, ClrYellow);
    // GrStringDraw(&g_sContext, "date:", -1, 1, 30, 0);
    //}
    //else{
    strftime(buff, sizeof(buff), "%c", &sTimeGuess);
    GrStringDraw(&g_sContext, buff, -1, 1, 30, 0);//}

  • Hi, 

    Your code is not like mine - it is missing important line HWREG(HIB_RTCLD) = seconds_to_write; which transfer the equivalent seconds to RTC module (if you use the hibernation module) . Not sure if you filled up first the tm structure.After that write allow a delay of several seconds before reading back; inside the time slot of less than a second nothing is supposed to happen, since the rtc timer has a resolution of one second. 

    Please review the user manual.

    Petrei

  • Hi,

    my code is,

    ////////////////////////////////////////////////////////////////

    time_t time_of_day=umktime(&tm1);

    HWREG(HIB_RTCLD)=time_of_day;

    ulocaltime(HWREG(HIB_RTCLD), &tm1);

    sprintf(buff,"%d",tm1.tm_hour);

    GrStringDraw(&g_sContext,buff, -1, 1, 30, 0);

    //////////////////////////////////////////////////////////////////////////////

    Im getting 0 inplace of tm_hour.

    buff is also zero(i added into watch window).

    what is the problem, please share with me.

  • Hi,

    First question; did you initialized the tm1 structure? if no, then initialize it, else if yes, then:

    a) start your debugger and set a breakpoint after the call of function umktime. Read/see the value of time_of_day variable. Post it, also verify it manually - should reflect the number of seconds since Jan 1, 1970 (approximative, some multiplies and additions).

    b) execute at once the next instruction and then verify if the RTCLD register is written with the value;

    c) insert into your code a SysCtlDelay(xxx) where xxx is a constant to spend time of 2..3 seconds !!! mandatory !!!

    d) then you can read back - use a second structure tm2 to be able to compare with the first; will be removed when all will work.

    Try as written here first, once the structures will look OK, go to display on LCD, not before. Just the debugger will be enough. 

  • Hi,

    Wouldn't a code attach be useful for the rest of us?

    Regards

    Amit

  • i added the breakpoint after umktime(),


    im getting the value like this, in time_of_day variable. 

    but in HIB_RTCLD not getting any value. in buff array also same value i.e, 0(like previous)

    //////////////////// watch window variable///////////////////////

    time_of_day    unsigned int    4294967295

    ////////////////////////////////////////////////////////////////////////////

  • Hi,

    You continue to keep quiet about initialization of tmx structure - did you made it or not?

    Second, you still ignore my code posted on Jul 2, 6:51 - 

    if(seconds_to_write == (uint32_t)(-1)) // alert-mistake

    and what you get is exactly 0xFFFFFFFF, aka (uint32_t)(-1) so you have an error in converting. Still may be related to initialization or something else. 

    Not clear also if you enabled the hibernation module. If enabled and started, then you may try to write something random to HIB_RTCLD and see if accepted and after several seconds modified. If modified, read it back and run/verify the last part (back conversion to tmx structure and display). You may use any big value for presumable RTCLD register to convert back and to have verified the display.

    Post your whole code (in a file) to be checked.

    Petrei

  • #include <stdint.h>
    #include <stdbool.h>
    #include <string.h>
    #include <time.h>
    #include "inc/hw_sysctl.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "grlib/grlib.h"
    #include "drivers/cfal96x64x16.h"
    #include "grlib/context.c"
    #include "grlib/string.c"
    #include "utils/ustdlib.h"
    #include "inc/hw_hibernate.h"
    
    
    tContext g_sContext;
    tRectangle sRect;
    
    char buff[20];
    
    void ClrScreen(void);
    static const time_t g_psDaysToMonth[12] =
    {
        0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
    };
    
    
    void ClrScreen()
    {
    sRect.i16XMin = 160;
    
    sRect.i16YMin = 132;
    
    sRect.i16XMax = 312;
    
    sRect.i16YMax = 232;
    
    GrContextForegroundSet(&g_sContext, ClrRed);
    
    GrRectFill(&g_sContext, &sRect);
    
    GrFlush(&g_sContext);
    }
    
    
    void
    ulocaltime(time_t timer, struct tm *tm)
    {
        time_t temp, months;
    
        //
        // Extract the number of seconds, converting time to the number of minutes.
        //
        temp = timer / 60;
        tm->tm_sec = timer - (temp * 60);
        timer = temp;
    
        //
        // Extract the number of minutes, converting time to the number of hours.
        //
        temp = timer / 60;
        tm->tm_min = timer - (temp * 60);
        timer = temp;
    
        //
        // Extract the number of hours, converting time to the number of days.
        //
        temp = timer / 24;
        tm->tm_hour = timer - (temp * 24);
        timer = temp;
    
        //
        // Compute the day of the week.
        //
        tm->tm_wday = (timer + 6) % 7;
    
        //
        // Compute the number of leap years that have occurred since 1968, the
        // first leap year before 1970.  For the beginning of a leap year, cut the
        // month loop below at March so that the leap day is classified as February
        // 29 followed by March 1, instead of March 1 followed by another March 1.
        //
        timer += 366 + 365;
        temp = timer / ((5 * 365) + 1);
        if((timer - (temp * ((5 * 365) + 1))) > (31 + 28))
        {
            temp++;
            months = 12;
        }
        else
        {
            months = 2;
        }
    
        //
        // Extract the year.
        //
        tm->tm_year = ((timer - temp) / 365) + 68;
        timer -= ((tm->tm_year - 68) * 365) + temp;
    
        //
        // Extract the month.
        //
        for(temp = 0; temp < months; temp++)
        {
            if(g_psDaysToMonth[temp] > timer)
            {
                break;
            }
        }
        tm->tm_mon = temp - 1;
    
        //
        // Extract the day of the month.
        //
        tm->tm_mday = timer - g_psDaysToMonth[temp - 1] + 1;
    }
    
    static int
    ucmptime(struct tm *t1, struct tm *t2)
    {
        //
        // Compare each field in descending signficance to determine if
        // greater than, less than, or equal.
        //
        if(t1->tm_year > t2->tm_year)
        {
            return(1);
        }
        else if(t1->tm_year < t2->tm_year)
        {
            return(-1);
        }
        else if(t1->tm_mon > t2->tm_mon)
        {
            return(1);
        }
        else if(t1->tm_mon < t2->tm_mon)
        {
            return(-1);
        }
        else if(t1->tm_mday > t2->tm_mday)
        {
            return(1);
        }
        else if(t1->tm_mday < t2->tm_mday)
        {
            return(-1);
        }
        else if(t1->tm_hour > t2->tm_hour)
        {
            return(1);
        }
        else if(t1->tm_hour < t2->tm_hour)
        {
            return(-1);
        }
        else if(t1->tm_min > t2->tm_min)
        {
            return(1);
        }
        else if(t1->tm_min < t2->tm_min)
        {
            return(-1);
        }
        else if(t1->tm_sec > t2->tm_sec)
        {
            return(1);
        }
        else if(t1->tm_sec < t2->tm_sec)
        {
            return(-1);
        }
        else
        {
            //
            // Reaching this branch of the conditional means that all of the
            // fields are equal, and thus the two times are equal.
            //
            return(0);
        }
    }
    
    
    time_t
    umktime(struct tm *timeptr)
    {
       // struct tm sTimeGuess;
        unsigned long ulTimeGuess = 0x80000000;
        unsigned long ulAdjust = 0x40000000;
        int iSign;
    
        //
        // Seed the binary search with the first guess.
        //
        ulocaltime(ulTimeGuess, &sTimeGuess);
    
        iSign = ucmptime(timeptr, &sTimeGuess);
    
        //
        // While the time is not yet found, execute a binary search.
        //
        while(iSign && ulAdjust)
        {
            //
            // Adjust the time guess up or down depending on the result of the
            // last compare.
            //
            ulTimeGuess = ((iSign > 0) ? (ulTimeGuess + ulAdjust) :
                           (ulTimeGuess - ulAdjust));
            ulAdjust /= 2;
    
            //
            // Compare the new time guess against the time pointed at by the
            // function parameters.
            //
            ulocaltime(ulTimeGuess, &sTimeGuess);
            iSign = ucmptime(timeptr, &sTimeGuess);
        }
    
        //
        // If the above loop was exited with iSign == 0, that means that the
        // time in seconds was found, so return that value to the caller.
        //
        if(iSign == 0)
        {
            return(ulTimeGuess);
        }
    
        //
        // Otherwise the time could not be converted so return an error.
        //
        else
        {
            return((unsigned long)-1);
        }
    }
    
    int
    main(void)
    {
        uint32_t ui32Idx;
        tRectangle sRect;
    
    
    
    ROM_FPULazyStackingEnable();
    
        //
        // Set the clocking to run from the PLL.
        //
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
                           SYSCTL_OSC_MAIN);
    
        //
        // Initialize the display driver.
        //
        CFAL96x64x16Init();
    
        //
        // Initialize the graphics context.
        //
        GrContextInit(&g_sContext, &g_sCFAL96x64x16);
    
    
    
     GrContextFontSet(&g_sContext, g_psFontCm18i);
    
    
    
     GrFlush(&g_sContext);
    
    
    
    while(1)
        {
        	struct tm my_date_time;
    
        	time_t seconds_to_write;
    
        	seconds_to_write=umktime(&my_date_time);
    
        	if(seconds_to_write ==(uint32_t)(-1))
        	{
    
        	}
    
        	else
        		HWREG(HIB_RTCLD) = seconds_to_write;
    
    
        	ulocaltime(HWREG(HIB_RTCLD), &my_date_time);
    
        	sprintf(buff,"%d",&my_date_time);
    
    
    
     	    sRect.i16XMin =0;
    
       	    sRect.i16YMin = 30;
    
       	    sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
    
       	    sRect.i16YMax = 56;
    
            GrContextForegroundSet(&g_sContext, ClrRed);
    
       	GrRectFill(&g_sContext, &sRect);
    
        	GrContextForegroundSet(&g_sContext, ClrYellow);
    
    	GrStringDraw(&g_sContext,buff, -1, 1, 30, 0);
    
    
    SysCtlDelay(5000000);
    
    
       }
    }
    

    please find the attachment and check it once. 

    i need to display time and date like., 4 july 2014 14:07:30 

    thanking you.

  • Hi,

    No, no, your program is not prepared to show real data. Read again what was written up to this post, and try to follow. I will try to correct your file, but still had something else on my head, so you must be patient.

    The structure "initialization" is completely missing... In the mean time, search your manuals or google how to initialize a structure and use it for tmx structure. 

    Petrei

  • ok...

    i will refere it.

    please provide the proper code to display.

    Thanking you.

  • Hi,

    Look at these references to learn how to init the tmx structure(s)

    A) http://www.tutorialspoint.com/c_standard_library/c_function_mktime.htm
    B) http://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm

    Take care the tmx structure used in your toolchain is a little bit different to that one in the examples. Compare them and adapt the example for your toolchain. 

    Petrei

  • Hi,

    im following the links sent by u.

    //////////////////////////////

    If i use, below fun

    time( &rawtime );
     info = localtime( &rawtime );

    Im getting system date and time with continues variation on display. If i remove the power supply or setting to reset, then its is changing to some other.

    If i use below code, constant time is displaying(what we are setting by using structure.) im not calling localtime().


    //////////////////////////////////////////////////////

    int
    main(void)
    {
    time_t rawtime;

    struct tm *info;
    char buffer[80];

    info.tm_year = 2014;
    info.tm_mon = 7 - 1;
    info.tm_mday = 1;
    info.tm_hour = 17;
    info.tm_min = 45;
    info.tm_sec = 35;
    info.tm_isdst = -1;

    while(1)
    {
    ret = mktime(&info);
    //time( &rawtime );
    // info = localtime( &rawtime );
    //strftime(buffer,80,"%I:%M%p", info); //"%x - %I:%M%p"

    if( ret == -1 )
    {
    // printf("Error: unable to make time using mktime\n");
    }
    else

    // strftime(buffer, sizeof(buffer), "%c", &info );
    strftime(buffer,80,"%I:%M%p", &info);

    GrContextForegroundSet(&g_sContext, ClrYellow);

    GrStringDraw(&g_sContext,buffer , -1, 1, 30, 0);

    SysCtlDelay(5000000);

    }

    //////////////////////////////////////////////////////////////////

  • Hi,

       I really do not understand your assertion here:

    Im getting system date and time with continues variation on display.

    Please enlighten me - do you mean you have enabled the hibernation module and you already see the clock/date changing on the screen? If yes seems you had succeeded -

    About removing the power and seeing something else is quite normal - i suppose you use the Launchpad and thisboard does not have a battery attached - read Launchapad's user manual. paragraph 2.2.2- Hibernate.

    Petrei

    EDIT - I missed the first post - you have the DK board...


  • Hi,

    Yes exactly, I have DK Board(TM4C123GH6PGE). it has the battery attachment. 

    we need to enable the hibernate module and display the clock/date on screen. When we remove the power, by using the battery clock/date has to work as normal mode. 

    please share related code to    srinivasv@cybermotionind.com

  • Hi,

    You have here an example of enabling the hibernation module, initializing the RTC and printing on UART0 the date/time each 5 seconds. This should run powered (Launchpad: VBat is linked to VDD, I do not have DK board)

    No special measures are taken for switching between power/batteries - keep in mind the pins for battery and VDD are separate and the battery is weak (does not support for long time to run micro).How to deal with these aspects are in the application hibernate in Tiva/your board.

    3426.RTCDayTime123.zip

    Petrei