主题中讨论的其他器件:EK-TM4C129EXL
您好!
我使用的是 EK-TM4C129EXL、Tiva TI-RTOS。 我能够使用 SDSPI 和 USB 主机大容量存储驱动程序成功创建字符并向文件写入字符。 问题是文件上的时间戳不正确。 如何将文件时间戳设置为当前时间? 看起来 stdio.h 用于创建和写入该文件。 我在 stdio 中查看了函数、但找不到与更改时间戳有关的任何内容。 我也可以完全没有时间戳。 有办法摆脱文件上的问题吗? 感谢任何帮助。
谢谢
AJ
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.
您好!
我使用的是 EK-TM4C129EXL、Tiva TI-RTOS。 我能够使用 SDSPI 和 USB 主机大容量存储驱动程序成功创建字符并向文件写入字符。 问题是文件上的时间戳不正确。 如何将文件时间戳设置为当前时间? 看起来 stdio.h 用于创建和写入该文件。 我在 stdio 中查看了函数、但找不到与更改时间戳有关的任何内容。 我也可以完全没有时间戳。 有办法摆脱文件上的问题吗? 感谢任何帮助。
谢谢
AJ
您好、AJ、
我希望这篇文章能够为您的问题提供一些指导。
尊敬的 Charles:
感谢您的答复。
这看起来文章讨论如何为我的 MCU 设置正确的时间戳、但它似乎与保存在 SD 卡或 USB 闪存上的文件的时间戳无关
当我尝试运行 fatsd 和 fatsdusbcopy 的示例时、文件会成功写入存储设备、但文件上的时间不正确。 它与我当前的时间不符。 您知道当您将文件保存到目录时、该文件有一个时间戳。 文件上的时间显示2023年4月11日、但显示时间应为2023年5月10日
谢谢
AJ
尊敬的 Charles:
我已经做了更多的调查、看起来 seconds_set 函数与文件上的时间戳相关。 在示例代码中、不使用秒模块、从不调用秒集。 在我的应用程序中、我使用 seconds_set (UTC_time)来验证认证 我知道我得到了正确的 UTC 时间、因为我从 NTP 服务器获得它、并且我将此值与显示1970年1月(epoch 时间)以来当前秒数的网站进行比较、以确保我得到了正确的值。 我刚刚意识到文件上的时间戳离该文件有一个月的时间了。 我创建文件时的 UTC 时间是2023年5月12日1:51。 创建的文件的日期为2023年4月12日1:51。 休息了一个月。 但是、我输入了以秒为单位的正确 UTC 时间、因为我真的不知道发生了什么。
我做的一个测试是在创建文件之前通过调用 seconds_set (0)将时间设置为零。 当我执行此操作时、保存的文件没有时间戳。 我确信文件时间戳会受到秒模块的影响。 从秒模块获取时间时、文件 API 中是否存在错误?
此致、
AJ
您好、AJ、
如果您能够从 NTP 获取时间、那么您可以查看下面的示例代码以获取正确的时间并查看它是否有用。
/* * ======== sntp.c ======== * SNTP Client example application */ #include <string.h> #include <time.h> /* XDCtools Header files */ #include <xdc/runtime/Error.h> #include <xdc/runtime/System.h> /* TI-RTOS Header files */ #include <ti/sysbios/BIOS.h> #include <ti/sysbios/hal/Seconds.h> #include <ti/sysbios/knl/Task.h> #include <ti/sysbios/knl/Semaphore.h> #include <ti/drivers/GPIO.h> #include <ti/net/http/httpcli.h> #include <ti/net/sntp/sntp.h> /* Example/Board Header file */ #include "Board.h" #include <sys/socket.h> /* * NTP server and port to obtain network time and number of servers to attempt */ #define NTP_HOSTNAME "north-america.pool.ntp.org" #define NTP_PORT "123" #define NTP_SERVERS 3 #define NTP_SERVERS_SIZE (NTP_SERVERS * sizeof(struct sockaddr_in)) #define NTPTASKSTACKSIZE 2048 /* * Time Zone Adjustment. * TODO: Adjust your local time zone with respect to Greenwich Mean Time (GMT) * For example, North America Central Time Zone is 6 hours behind GMT */ #define CENTRAL_TIME_ADJUST -6*60*60 #define TIME_ZONE_ADJUST CENTRAL_TIME_ADJUST unsigned char ntpServers[NTP_SERVERS_SIZE]; static Semaphore_Handle semHandle = NULL; /* * ======== printError ======== */ void printError(char *errString, int code) { System_printf("Error! code = %d, desc = %s\n", code, errString); BIOS_exit(code); } /* * ======== timeUpdateHook ======== * Called after NTP time sync */ void timeUpdateHook(void *p) { Semaphore_post(semHandle); } /* * ======== startNTP ======== * This is the function that generates the request to the NTP server for network time. */ void startNTP(void) { int ret; int currPos; time_t ts; struct sockaddr_in ntpAddr; struct addrinfo hints; struct addrinfo *addrs; struct addrinfo *currAddr; Semaphore_Params semParams; /* * Clear the addrinfo first. */ memset(&hints, 0, sizeof(struct addrinfo)); /* * Use SOCK_DGRAM (UDP) as the socket type */ hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; /* * First resolve the IP Address of the NTP server using the DNS service.. * The getaddrinfo can return a linked-list of IP addresses */ ret = getaddrinfo(NTP_HOSTNAME, NTP_PORT, NULL, &addrs); if (ret != 0) { printError("startNTP: NTP host cannot be resolved!", ret); } currPos = 0; /* * Loop through the NTP servers */ for (currAddr = addrs; currAddr != NULL; currAddr = currAddr->ai_next) { if (currPos < NTP_SERVERS_SIZE) { ntpAddr = *(struct sockaddr_in *)(currAddr->ai_addr); /* * Copy the list of NTP servers to netServers */ memcpy(ntpServers + currPos, &ntpAddr, sizeof(struct sockaddr_in)); currPos += sizeof(struct sockaddr_in); } else { break; } } freeaddrinfo(addrs); /* * Initialize and start the SNTP client Task. Called to create and start SNTP * client Task and Semaphores. User must pass in pointers to functions for * getting and setting the current time and the list of NTP servers to * communicate with. timeUpdateHook() is called upon successful time * synchronization. */ ret = SNTP_start(Seconds_get, Seconds_set, timeUpdateHook, (struct sockaddr *)&ntpServers, NTP_SERVERS, 0); if (ret == 0) { printError("startNTP: SNTP cannot be started!", -1); } /* * Create a semaphore that will block until after successful NTP time * synchronization. Once unblocked, we will print the current time to the * console. */ Semaphore_Params_init(&semParams); semParams.mode = Semaphore_Mode_BINARY; semHandle = Semaphore_create(0, &semParams, NULL); if (semHandle == NULL) { printError("startNTP: Cannot create semaphore!", -1); } SNTP_forceTimeSync(); /* * Wait here until the semaphore is posted. */ Semaphore_pend(semHandle, BIOS_WAIT_FOREVER); /* * Adjust for the Central Time Zone. */ Seconds_set((unsigned int)((int)Seconds_get()+TIME_ZONE_ADJUST)); ts = time(NULL); /* * Print the current time to the console. */ System_printf("Current time: %s\n", ctime(&ts)); System_flush(); } /* * ======== netIPAddrHook ======== * This function is called when IP Addr is added/deleted */ void netIPAddrHook(unsigned int IPAddr, unsigned int IfIdx, unsigned int fAdd) { static Task_Handle taskHandle; Task_Params taskParams; Error_Block eb; /* Create a NTP task when the IP address is added */ if (fAdd && !taskHandle) { Error_init(&eb); Task_Params_init(&taskParams); taskParams.stackSize = NTPTASKSTACKSIZE; taskParams.priority = 1; taskHandle = Task_create((Task_FuncPtr)startNTP, &taskParams, &eb); if (taskHandle == NULL) { printError("netIPAddrHook: Failed to create NTP Task\n", -1); } } } /* * ======== main ======== */ int main(void) { /* Call board init functions */ Board_initGeneral(); Board_initGPIO(); Board_initEMAC(); /* Turn on user LED */ GPIO_write(Board_LED0, Board_LED_ON); System_printf("Starting the SNTP example\nSystem provider is set to " "SysMin. Halt the target to view any SysMin contents in ROV.\n"); /* SysMin will only print to the console when you call flush or exit */ System_flush(); /* Start BIOS */ BIOS_start(); return (0); }