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.

TM4C123GH6PM: How to Restore RTC values after power cycle?

Part Number: TM4C123GH6PM


Hello all,

I'am using my own TM4C123GH6PM Custom Board  and I'am trying to implement the RTC of the MCU.

I'am using the 32.768 KHz crystal and connected a 3V Coin cell to the Vbat pin with an RC circuit(51 ohm & 0.1 uF) between VBat and Coin cell.

The code below is written for RTC 

time_t calendar_write;
time_t calendar_read;
struct tm tm1 = {0,15,16,2,3,2018-1900,1,0,0};
struct tm tm2 = {0};



int main()

{



SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // clock at 80 Mhz

ui32SysClkFreq = 80000000;

// Enable the Hibernation module.
SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE);

// Wait for the Hibernate module to be ready.
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_HIBERNATE))
{
}
HibernateEnableExpClk(ui32SysClkFreq);

//
// Wait an amount of time for the module to power up.
//

// Configure the clock source for Hibernation module and enable the
// RTC feature.
HibernateClockConfig(HIBERNATE_OSC_HIGHDRIVE);
HibernateRTCEnable();

HibernateCounterMode(HIBERNATE_COUNTER_RTC);

calendar_write = umktime(&tm1);

HibernateRTCSet(calendar_write);



while(1)

{

calendar_read = HibernateRTCGet();
ulocaltime(calendar_read, &tm2);

// format the output for uart
strftime((char *)buf_rz, 32, "%c\r\n", &tm2);

}

}

The issue is that I'am not able to restore the hour:minutes:seconds values after power cycle. The RTC resets when I power cycle the board.

  • just an update. I was able to solve the problem by changing the logic of my code. I made the user to set the time in my application . Once the time is set , it keeps on running after power off also till the time battery is not removed. just the same way as in mobile phones!
  • Hi Sumit,
    Glad your problem is solved. Do you mind to post your updated code so others in the community can also benefit? Thanks.
  • uint32_t XTime;
    uint32_t hours;
    uint32_t minutes;
    uint32_t second;
    
     void UART3IntHandler(void)
     {
       uint32_t    ui32Status1;
    
     ui32Status1 = UARTIntStatus(UART3_BASE, true);
    
      UARTIntClear(UART3_BASE, ui32Status1);
    
    Customhour[g]=UARTCharGet(UART3_BASE);
    Custominute[g]=UARTCharGet(UART3_BASE);
    
    hour = atoi(Customhour);
    min = atoi(Custominute);
    uint32_t total = (min*60) + (hour*3600);             
    HibernateRTCSet(total);
    
    }
    
    int main()
    
    {
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // clock at 80 Mhz
    
        ui32SysClkFreq = 80000000;
    
        // Enable the Hibernation module.
        SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE);
    
        // Wait for the Hibernate module to be ready.
        while(!SysCtlPeripheralReady(SYSCTL_PERIPH_HIBERNATE))
        {
        }
        HibernateEnableExpClk(ui32SysClkFreq);
    
        //
        // Wait an amount of time for the module to power up.
        //
    
        // Configure the clock source for Hibernation module and enable the
        // RTC feature.
        HibernateClockConfig(HIBERNATE_OSC_HIGHDRIVE);
        HibernateRTCEnable();
    
        HibernateCounterMode(HIBERNATE_COUNTER_RTC);
    
    while(1)
    {
    XTime = HibernateRTCGet();
    
            hours = ((XTime /3600)) % 24;
            minutes = ((XTime /60)) % 60;
            second = (XTime % 60);
    
            usprintf(buf_rz,"%d %d %d",hours,minutes,second);
    
    }
    }

    yes sure,. This code requires the application user to set the time . So you probably need a UI in your application.