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.

MSP430FR5994: how to initialize RTC using the built in time library?

Part Number: MSP430FR5994

Hello everybody

I am trying to initialize the RTC_C on the MSP430FR5994 Launchpad by assigning values from the time library.

when I use hard coded values in 0x00 format, the downstream coding behaves differently than when I use the variable assignment

as shown below. I thought the problem was a type mismatch and tried casting, examining values etc but I am unable to resolve the 

problem- calendar is not interpreting the variable assignment the same way as the hard coded values. My RTC initialization uses

binary so this is not a hexadecimal issue. The time lbirary fields (tm_sec, tm_min, etc) are integers while calendar fields are uint8_t.

any help is greatly appreciated.

thanks

jim

 

void Init_RTC()
{

    struct tm *info;

    time_t rawtime;  //type is long. used to hold encoded calendar time
    time(&rawtime);   //function to get current system time relative to 1/1/1970 measured in seconds
    info=localtime(&rawtime); //returns pointer to tm structure that stores date and time
    mktime(info); //normalize values
    
    // really lame assignments for debug
    uint8_t sec =(uint8_t)(info->tm_sec);
    uint8_t min= (uint8_t)(info->tm_min);
    uint8_t hours = (uint8_t)(info->tm_hour);
    uint8_t weekDay = (uint8_t)(info->tm_wday);
    uint8_t month = (uint8_t)(info->tm_mon);
    uint8_t dayOfmonth = (uint8_t)(info->tm_mday);
    uint8_t year = (uint8_t)(info->tm_year);

    //Setup Current Time for Calendar  
    calendar.Seconds    =  sec; //0x25;
    calendar.Minutes    =  min; //0x05;
    calendar.Hours      =  hours; //0x0C; //12
    calendar.DayOfWeek  =  weekDay;//0x00;
    calendar.DayOfMonth =  month;//0x05;
    calendar.Month      =  month+1;//0x02;
    calendar.Year       = 0x07e3; //0x2019;


    // Initialize RTC with the specified Calendar above
    RTC_C_initCalendar(RTC_C_BASE,
                       &calendar,
                       RTC_C_FORMAT_BINARY); //using RTC_C_FORMAT_BCD scrambles file time stamp

    RTC_C_setCalendarEvent(RTC_C_BASE,
                           RTC_C_CALENDAREVENT_MINUTECHANGE
                           );

    RTC_C_clearInterrupt(RTC_C_BASE,
                         RTC_C_TIME_EVENT_INTERRUPT
                         );

    RTC_C_enableInterrupt(RTC_C_BASE,
                          RTC_C_TIME_EVENT_INTERRUPT
                          );

    //Start RTC Clock
    RTC_C_startClock(RTC_C_BASE);
}

  • Part Number: MSP430FR5994

    Tool/software: TI C/C++ Compiler

    Hello everybody,

    I am trying to initialize the RTC_C on the MSP430FR5994 launchpad by assigning values from the time library.

    When I use hard coded values in 0x00 format the downstream code behaves differently than when I use the assignment as

    currently shown below. I thought the problem was a type mismatch* and tried casting, examining the values, etc but

    still the problem remains - calendar fields are not interpreting the variable the same as the hard coded values.


    My RTC_C initialization uses binary format so this is not a hexadecimal issue.


    thank you
    jim


    *The time library fields (i.e. tm_sec, tm_min, etc) are integers, while calendar fields expects uint8_t.
    
    
    void Init_RTC()
    {
    
        struct tm *info;
    
        time_t rawtime;  //type is long. used to hold encoded calendar time
        time(&rawtime);   //function to get current system time relative to 1/1/1970 measured in seconds
        info=localtime(&rawtime); //returns pointer to tm structure that stores date and time
        mktime(info); //normalize values
    
        uint8_t sec =(uint8_t)(info->tm_sec);
        uint8_t min= (uint8_t)(info->tm_min);
        uint8_t hours = (uint8_t)(info->tm_hour);
        uint8_t weekDay = (uint8_t)(info->tm_wday);
        uint8_t month = (uint8_t)(info->tm_mon);
        uint8_t dayOfmonth = (uint8_t)(info->tm_mday);
        uint8_t year = (uint8_t)(info->tm_year);
    
        //Setup Current Time for Calendar 
        calendar.Seconds    =  sec; //0x25;
        calendar.Minutes    =  min; //0x05;
        calendar.Hours      =  hours; //0x0C; //12
        calendar.DayOfWeek  =  weekDay;//0x00;
        calendar.DayOfMonth =  month;//0x05;
        calendar.Month      =  month+1;//0x02;
        calendar.Year       = 0x07e3; //0x2019;
    
    
        // Initialize RTC with the specified Calendar above
        RTC_C_initCalendar(RTC_C_BASE,
                           &calendar,
                           RTC_C_FORMAT_BINARY); //using RTC_C_FORMAT_BCD scrambles file time stamp
    
        RTC_C_setCalendarEvent(RTC_C_BASE,
                               RTC_C_CALENDAREVENT_MINUTECHANGE
                               );
    
        RTC_C_clearInterrupt(RTC_C_BASE,
                             RTC_C_TIME_EVENT_INTERRUPT
                             );
    
        RTC_C_enableInterrupt(RTC_C_BASE,
                              RTC_C_TIME_EVENT_INTERRUPT
                              );
    
        //Start RTC Clock
        RTC_C_startClock(RTC_C_BASE);
    }
    

  • You have not told us what the problem is.

    But I suspect that time() does not return anything useful. (Where would it get it from? From the RTC?)
  • hi
    thank you for responding. time is a built in library on the C language, it fetches the time from the system clock. It should be reasonable time source for my purposes. The problem is that file on the sdcard have lost their FatFs creation dates. When hard coded, the FatFs dates are there. Even if Time returns incorrect dates, I should not be losing the file creation dates. I've traced the problem to how calendar is initialized. Hard coding works, so why doesn't variable initialization work? The only thing I can think of is that variables are not in the right format...
    jim
  • A microcontroller does not have a system clock.

    I do not know what time() returns, but it will certainly not be what you expect.
  • I think I understand now. what you are saying is that the built in library needs a real clock running to return a legitimate value at RUN time, it is not a compile time option. is this correct?

    thanks

    jim

  • I do not know which library you are using (this depends on the compiler).

    But there is no operating system, and no system clock, so I guess time() does not contain any useful code.
  • actually I think it still does contain some useful code. I've used it to produce an epoch/unix time stamp for which it only needs a static date (with the year adjusted relative to 1970). Thank you so much for helping. Stepping back I now realize just how lame my question was (too close to trees to see the forest).
  • Not lame, merely circular. The source for the library is buried in the CCS install folder, and there might be a way (as Clemens suggests) to insert the contents of the RTC into the time() functions.


    But you still have to seed the RTC somehow. As far as I know the FR5994 doesn't have a battery-backup capability (nominally VBAT) for the RTC, so you'd have to set the time (from somewhere) after every power cycle.

**Attention** This is a public forum