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.

CC3220: How to calculate current time for SD Card

Part Number: CC3220

Hi all,
I am using the below function to calculate the time to represent the date in the SD Card when a file is being stored:

int32_t fatfs_getFatTime(void)
{
time_t seconds;
uint32_t fatTime;
struct tm *pTime;

/*
* TI time() returns seconds elapsed since 1900, while other tools
* return seconds from 1970. However, both TI and GNU localtime()
* sets tm tm_year to number of years since 1900.
*/
seconds = time(NULL);

pTime = localtime(&seconds);

/*
* localtime() sets pTime->tm_year to number of years
* since 1900, so subtract 80 from tm_year to get FAT time
* offset from 1980.
*/
fatTime = ((uint32_t)(pTime->tm_year - 80) << 25) |
((uint32_t)(pTime->tm_mon) << 21) |
((uint32_t)(pTime->tm_mday) << 16) |
((uint32_t)(pTime->tm_hour) << 11) |
((uint32_t)(pTime->tm_min) << 5) |
((uint32_t)(pTime->tm_sec) >> 1);

return ((int32_t)fatTime);
}

seconds = fatfs_getFatTime();
ts.tv_sec = seconds;

But ia m getting different values other than the current date, can anyone suggest me how to represent the exact time, when the file is stored in the SD Card.

Thanks & regards
Sretha

  • Hi Sretha,

    What version of the SDK are you using? I cannot locate the time() function in 2_30_00_05?

    Jesu

  • Hi Jesu,

    I am using SDK 2_30_00_05 , please go through the fatsd.c file in fatsd SDK example.

    time() is a general function found in #include <time.h> header file, i tried one more method for calculating and representing time in SD card which i explained below:

    struct tm t;
    t.tm_year = 54; (2024-1970)
    t.tm_mon = 1; // Month, (should have to give in format 0 - jan, but in ccs it is accepting like 1 - jan)
    t.tm_mday = 1; // Day of the month
    t.tm_hour = 15;
    t.tm_min = 20;
    t.tm_sec = 42;
    t.tm_isdst = 0;

    time_t seconds;
    seconds = mktime(&t);
    seconds = seconds;
    Display_printf(display, 0, 0, "seconds from epoch = %d\n",seconds);

    struct timespec tms = {
    .tv_sec = seconds,
    .tv_nsec = 0
    };

    /* Initialize real-time clock */

    clock_settime(CLOCK_REALTIME, &tms);

    According to the above calculation i am getting correct epoch seconds , as the value which i got is similar to seconds ( https://www.epochconverter.com ) calculated from this website .

    But i am not getting any date and time in the SD card when i am assigning this value to .tv_sec = seconds , usually i have to give t.tm_mon = 1; // Month, (0- jan), as it is not working i assigned  t.tm_mon = 1; // Month, (1- jan),  i am getting the assigned date and time in the SD card.

    By this method also i am not getting correct value for all the days, as for example if i am giving jan-31-2024 i am getting feb-2-2024 and if i am giving jan-31-2023 i am getting feb-3-2023

    can i know why i am getting like this, is there are documentation for this calculation.

    please help me to solve this issue.

    Thanks & regards

    ~Sretha

  • Sretha,

    I did some searching and I found the following resources:

    processors.wiki.ti.com/.../Time_and_clock_RTS_Functions
    processors.wiki.ti.com/.../CIO_System_Call_Protocol

    All FatFS functionality is 3rd party and you can find documentation for it here:
    elm-chan.org/.../appnote.html

    If problem persist, a workaround maybe to just account for the two day offset in code.

    Jesu
  • Hi Jesu,

    thank you for your reply,  i already went through this post : processors.wiki.ti.com/.../Time_and_clock_RTS_Functions but didn't got any information regarding time system for SD Card.

    But what i understood is :

    /* Initialize real-time clock */

    clock_settime(CLOCK_REALTIME, &tms);

    Because of this function only, i am getting different clock times in the SD Caard,

     so can you suggest me is there any other function, to set real-time clock other than this function, 

    Thanks & regards

    ~Sretha

  • Hi Sretha,

    clock_settime is a POSIX function which I'm not entirely sure how it works. You can access set the RTC match value using PRCMSlowClkCtrMatchSet().

    Description of API at www.ti.com/.../swru465.pdf page 506.

    Jesu
  • Hi Sretha,

    I'm assuming my last post solved your question due to inactivity. If you have a new question feel free to create a new thread.

    Jesu