So far I've successfully implemented file writing to my SD card using the STM32SD library. However, the timestamp for file creation and modification defaults to 1 January 1970 at 01:00 (UNIX time +1 hour?). I tried the STM32RTC library for setting the internal clock (rtc.setEpoch(1710682932)) and even via the Arduino Time library (adjustTime(1710682933)), but without any succes. How can I adjust the timestamp used for writing files, and is this even possible with the STM32SD library?
STM32SD library: https://github.com/stm32duino/STM32SD
STM32RTC library: https://github.com/stm32duino/STM32RTC
STM32SD adjust file timestamps?
Re: STM32SD adjust file timestamps?
you probably want to figure out when stm32sd writes the time stamp in the directory, where it obtains the timing information and provide that from the rtc library.
Re: STM32SD adjust file timestamps?
Something I'll spend more time on coming week, but together with the SDFat library the following callback seems to work:
Sources:
https://forum.arduino.cc/t/which-sd-lib ... /72070/15
https://www.if.ufrj.br/~pef/producao_ac ... stamp.ino
https://github.com/greiman/SdFat/blob/m ... pTest.ino
https://gist.github.com/Marzogh/8dc8011934eecf47e154
Code: Select all
void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
DateTime now = rtc.now();
// Return date using FS_DATE macro to format fields.
*date = FS_DATE(now.year(), now.month(), now.day());
// Return time using FS_TIME macro to format fields.
*time = FS_TIME(now.hour(), now.minute(), now.second());
// Return low time bits in units of 10 ms, 0 <= ms10 <= 199.
*ms10 = now.second() & 1 ? 100 : 0;
}
Sources:
https://forum.arduino.cc/t/which-sd-lib ... /72070/15
https://www.if.ufrj.br/~pef/producao_ac ... stamp.ino
https://github.com/greiman/SdFat/blob/m ... pTest.ino
https://gist.github.com/Marzogh/8dc8011934eecf47e154
Re: STM32SD adjust file timestamps?
apparently STM32SD
https://github.com/stm32duino/STM32SD
depends on FatFs
https://github.com/stm32duino/FatFs
which has a getfattime() callback
https://github.com/stm32duino/FatFs/blo ... /ff.h#L295
https://github.com/stm32duino/FatFs/blo ... /ff.c#L492
https://github.com/stm32duino/FatFs/blo ... ff.c#L3364
hence, you would need to return a DWORD (i'd think 32 bits) that matches a fat date time format
a google search stumbled into this
https://shullich.blogspot.com/2009/12/t ... ormat.html
https://web.mit.edu/freebsd/head/sys/ke ... _fattime.c
hence, you need to implement DWORD getfattime() that returns a 32 bit timestamp value perhaps in that format.
getting time from rtc should be 'easy', there are 'standard' libs for that.
then convert that to fat datetime stamp and return it, it would take experimenting as it is uncertain if this is after all the format.
https://github.com/stm32duino/STM32SD
depends on FatFs
https://github.com/stm32duino/FatFs
which has a getfattime() callback
https://github.com/stm32duino/FatFs/blo ... /ff.h#L295
https://github.com/stm32duino/FatFs/blo ... /ff.c#L492
https://github.com/stm32duino/FatFs/blo ... ff.c#L3364
hence, you would need to return a DWORD (i'd think 32 bits) that matches a fat date time format
a google search stumbled into this
https://shullich.blogspot.com/2009/12/t ... ormat.html
https://web.mit.edu/freebsd/head/sys/ke ... _fattime.c
hence, you need to implement DWORD getfattime() that returns a 32 bit timestamp value perhaps in that format.
Code: Select all
24 16 8 0
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
|Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
\___________/\________/\_________/ \________/\____________/\_________/
year month day hour minute second
then convert that to fat datetime stamp and return it, it would take experimenting as it is uncertain if this is after all the format.
Re: STM32SD adjust file timestamps?
Thank you for your reply! It appears that implementing the basics was less challenging than expected. Within the context of FatFs, when only declaring the following in global, the 'creation date' of every new file is being successfully updated (in this test, to the 1st of January 2000).
Currently I have the following snippet working with my DateTime instance, although I am uncertain if my method of passing the datetime object is best practise? On the other hand, passing a DateTime instance like for example DWORD get_fattime(const DateTime &datetime = systemTime) has not worked for me so far.
Code: Select all
DWORD get_fattime(void) {
BYTE year = 20; // 1980+20 = 2000
BYTE month = 1;
BYTE day = 1;
BYTE hour = 0;
BYTE minute = 0;
BYTE second = 0; // can only be set with two second precision
return ((DWORD)(year) << 25)
| ((DWORD)(month) << 21)
| ((DWORD)(day) << 16)
| ((DWORD)(hour) << 11)
| ((DWORD)(minute) << 5)
| ((DWORD)(second / 2));
}
Currently I have the following snippet working with my DateTime instance, although I am uncertain if my method of passing the datetime object is best practise? On the other hand, passing a DateTime instance like for example DWORD get_fattime(const DateTime &datetime = systemTime) has not worked for me so far.
Code: Select all
TwoWire wire2 = TwoWire(PB3, PB10);
DS3231 ds3231(wire2);
RTClib rtclib;
DateTime systemTime;
DWORD get_fattime(void) {
DateTime datetime = systemTime;
return ((DWORD)(datetime.year() - 1980) << 25)
| ((DWORD)datetime.month() << 21)
| ((DWORD)datetime.day() << 16)
| ((DWORD)datetime.hour() << 11)
| ((DWORD)datetime.minute() << 5)
| ((DWORD)datetime.second() >> 1);
}
void setup() {
systemTime = rtclib.now(wire2);
}