How to set the date and time for SD card files with NCS

nRF52840
NCS 2.4.1

I am logging to the FS backend.  All the files have a date of 12/31/21 11:00.

How do you give the file system access to UTC time?

I have a UTC time source and a timestamp getter (uint64_t currenttime(void)) that the log system uses.  The timestamps for the log messages are correct, though I would like to change the format to show date, not just hours.  Is there a way to change the format?  But, my main concern is the file timestamps.

main.c

    ret =  log_set_timestamp_func(currenttime, 1);
	printk("log_set_timestamp_func() returned %d", ret);

prj.conf

# SD card 
CONFIG_DISK_ACCESS=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_DISK_DRIVER_SDMMC=y
#CONFIG_SD_LOG_LEVEL_DBG=y

# Log to SD (does not support LOG_MODE_IMMEDIATE)
CONFIG_LOG_BACKEND_FS=y
CONFIG_LOG_BACKEND_FS_OUTPUT_TEXT=n
CONFIG_LOG_BACKEND_FS_DIR="/SD:"
CONFIG_LOG_BACKEND_FS_FILE_PREFIX="log"
CONFIG_LOG_BACKEND_FS_FILES_LIMIT=5
CONFIG_LOG_BACKEND_FS_FILE_SIZE=4096
CONFIG_LOG_BACKEND_FS_OVERWRITE=y

also tried this:

		sync_instant.local = k_uptime_get() / 1000;
		sync_instant.ref = now;                             // UTC, seconds since Epoch (1970)
		ret = timeutil_sync_state_update(&sync_state, &sync_instant);
		LOG_INF("Sync time. ret = %d", ret);

Is there a similar mechanism for the file system? Or a way to set UTC time for zephyr?

Mary

Parents
  • Hello,

    I have not looked into how to set and read time in zephyr, but in terms of how to set time on a file it looks like the following will do the job:

        // Set FF_USE_CHMOD 1 in ffconf.h
        
        FILINFO fno;
        fno.fdate = ((2024 - 1980) << 9) | (5 << 5) | 21; // Date: 2024/05/21
        fno.ftime = (15 << 11) | (45 << 5) | (30 / 2);   // Time: 15:45:30
    
        FRESULT res = f_utime(path, &fno);

    Kenneth

  • It seems that the fatfs is not configured to support time stamping of files.

    <sdk>/zephyr/modules/fatfs/zephyr_fatfs_config.h

    /*
     * Overrides of FF_ options from ffconf.h
     */
     
     
    #undef FF_FS_NORTC
    #define FF_FS_NORTC 1

    <sdk>/modules/fs/fatfs/ff.c

    /* Timestamp */
    #if FF_FS_NORTC == 1
    #if FF_NORTC_YEAR < 1980 || FF_NORTC_YEAR > 2107 || FF_NORTC_MON < 1 || FF_NORTC_MON > 12 || FF_NORTC_MDAY < 1 || FF_NORTC_MDAY > 31
    #error Invalid FF_FS_NORTC settings
    #endif
    #define GET_FATTIME()	((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16)
    #else
    #define GET_FATTIME()	get_fattime()
    #endif

    Is there a way to define FF_FS_NORTC as 0, but just for this project?  I don't want to modify the SDK.

    Then I could provide my own function get_fattime() that would return UTC time.

    Mary

Reply
  • It seems that the fatfs is not configured to support time stamping of files.

    <sdk>/zephyr/modules/fatfs/zephyr_fatfs_config.h

    /*
     * Overrides of FF_ options from ffconf.h
     */
     
     
    #undef FF_FS_NORTC
    #define FF_FS_NORTC 1

    <sdk>/modules/fs/fatfs/ff.c

    /* Timestamp */
    #if FF_FS_NORTC == 1
    #if FF_NORTC_YEAR < 1980 || FF_NORTC_YEAR > 2107 || FF_NORTC_MON < 1 || FF_NORTC_MON > 12 || FF_NORTC_MDAY < 1 || FF_NORTC_MDAY > 31
    #error Invalid FF_FS_NORTC settings
    #endif
    #define GET_FATTIME()	((DWORD)(FF_NORTC_YEAR - 1980) << 25 | (DWORD)FF_NORTC_MON << 21 | (DWORD)FF_NORTC_MDAY << 16)
    #else
    #define GET_FATTIME()	get_fattime()
    #endif

    Is there a way to define FF_FS_NORTC as 0, but just for this project?  I don't want to modify the SDK.

    Then I could provide my own function get_fattime() that would return UTC time.

    Mary

Children
Related