10 years, 3 months ago.

How to set Date and Time of SD file

I am writing a new program, the system creates files periodicaly on an SD card,

I can create LONG file names & LONG directory names, but I do not appear to be able to set the date and time.

I have found :

/* File status structure (FILINFO) */

typedef struct {
    DWORD   fsize;          /* File size */
    WORD    fdate;          /* Last modified date */
    WORD    ftime;          /* Last modified time */
    BYTE    fattrib;        /* Attribute */
    TCHAR   fname[13];      /* Short file name (8.3 format) */
#if _USE_LFN
    TCHAR*  lfname;         /* Pointer to the LFN buffer */
    UINT    lfsize;         /* Size of LFN buffer in TCHAR */
#endif
} FILINFO;

in 'ff.h'

but I cannot find any way to safely set time,

I am using LPC1347 - on a custom PCB,

Thanks in advance

Ceri

EDIT:

Just tried this on LPC1768, setting RTC before above call, and got a file with the set time,

So: new question, how to "Set time" in LPC1347 ??

Cheers

Ceri

2 Answers

10 years, 3 months ago.

The time is handled by get_fattime(void) in FatFileSystem.cpp

DWORD get_fattime(void) {
    time_t rawtime;
    time(&rawtime);
    struct tm *ptm = localtime(&rawtime);
    return (DWORD)(ptm->tm_year - 80) << 25
         | (DWORD)(ptm->tm_mon + 1  ) << 21
         | (DWORD)(ptm->tm_mday     ) << 16
         | (DWORD)(ptm->tm_hour     ) << 11
         | (DWORD)(ptm->tm_min      ) << 5
         | (DWORD)(ptm->tm_sec/2    );
}

time() is reading the RTC.

HOW did I miss that, I am sure I trawled through EVERY file in SD File System ....

AAAAAAAAAAAAAARRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGG !!!!!!!!!!!!

Ceri

posted by ceri clatworthy 30 Jan 2014
10 years, 3 months ago.

I have just got this to work :)


DWORD get_fattime(void)
{
//    return (DWORD)(ptm->tm_year - 80) << 25
//         | (DWORD)(ptm->tm_mon + 1  ) << 21
//         | (DWORD)(ptm->tm_mday     ) << 16
//         | (DWORD)(ptm->tm_hour     ) << 11
//         | (DWORD)(ptm->tm_min      ) << 5
//         | (DWORD)(ptm->tm_sec/2    );
//



//    Get_DS3231_RTC();
//    Above just reads the first 7 Registers of DS3231
//    DS1307 look very similar.
//    Reg [3] is an arbitrary DAY, Not used normally.

    int RTC_INT[10];

    RTC_INT[0] = (RTC_Read[0] & 0x0f)    + (10 * (RTC_Read[0] >> 4));
    RTC_INT[1] = (RTC_Read[1] & 0x0f)    + (10 * (RTC_Read[1] >> 4));
    RTC_INT[2] = (RTC_Read[2] & 0x0f)    + (10 * (RTC_Read[2] >> 4));

    RTC_INT[4] = (RTC_Read[4] & 0x0f)    + (10 * (RTC_Read[4] >> 4));
    RTC_INT[5] = (RTC_Read[5] & 0x0f)    + (10 * (RTC_Read[5] >> 4));
    RTC_INT[6] = (RTC_Read[6] & 0x0f)    + (10 * (RTC_Read[6] >> 4));


    return   (DWORD)(RTC_INT[6] + 276) << 25
           | (DWORD)(RTC_INT[5]) << 21
           | (DWORD)(RTC_INT[4]) << 16
           
           | (DWORD)(RTC_INT[2]) << 11
           | (DWORD)(RTC_INT[1]) << 5
           | (DWORD)(RTC_INT[0]);
}


Note, YOU must read RTC Chip BEFORE File actions, to set the correct time.

Hope this helps, because there has been quite a few threads, with TIME, and little resolution.

especially as TIME-FULL Definition is no longer in Handbook search ??

Cheers

Ceri