From f192ccce2c3e4f6798632db01e45d63fcff94ed6 Mon Sep 17 00:00:00 2001 From: hedger Date: Sat, 1 Apr 2023 17:50:30 +0400 Subject: [PATCH] FatFS: use rtc for timestamping (#2555) * fatfs: use rtc * fatfs: removed duplicate get_fattime declaration * pvs: fixed warnings for fatfs timestamp * fatfs: fixed seconds packing * FuriHal: critical section around RTC datetime access * FatFS: remove unused configration defines, update documentation * FuriHal: checks instead of assets in RTC --------- Co-authored-by: Aleksandr Kutuzov --- firmware/targets/f7/fatfs/fatfs.c | 16 ++++++++++------ firmware/targets/f7/fatfs/ffconf.h | 5 +---- firmware/targets/f7/furi_hal/furi_hal_rtc.c | 6 ++++++ lib/fatfs/diskio.h | 1 - 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/firmware/targets/f7/fatfs/fatfs.c b/firmware/targets/f7/fatfs/fatfs.c index 2c0e77fe..5a8912cb 100644 --- a/firmware/targets/f7/fatfs/fatfs.c +++ b/firmware/targets/f7/fatfs/fatfs.c @@ -1,4 +1,5 @@ #include "fatfs.h" +#include "furi_hal_rtc.h" /** logical drive path */ char fatfs_path[4]; @@ -9,11 +10,14 @@ void fatfs_init(void) { FATFS_LinkDriver(&sd_fatfs_driver, fatfs_path); } -/** - * @brief Gets Time from RTC - * @param None - * @retval Time in DWORD +/** Gets Time from RTC + * + * @return Time in DWORD (toasters per square washing machine) */ -DWORD get_fattime(void) { - return 0; +DWORD get_fattime() { + FuriHalRtcDateTime furi_time; + furi_hal_rtc_get_datetime(&furi_time); + + return ((uint32_t)(furi_time.year - 1980) << 25) | furi_time.month << 21 | + furi_time.day << 16 | furi_time.hour << 11 | furi_time.minute << 5 | furi_time.second; } diff --git a/firmware/targets/f7/fatfs/ffconf.h b/firmware/targets/f7/fatfs/ffconf.h index a4452155..8408a1ec 100644 --- a/firmware/targets/f7/fatfs/ffconf.h +++ b/firmware/targets/f7/fatfs/ffconf.h @@ -219,10 +219,7 @@ / When enable exFAT, also LFN needs to be enabled. (_USE_LFN >= 1) / Note that enabling exFAT discards C89 compatibility. */ -#define _FS_NORTC 1 -#define _NORTC_MON 7 -#define _NORTC_MDAY 20 -#define _NORTC_YEAR 2021 +#define _FS_NORTC 0 /* The option _FS_NORTC switches timestamp functiton. If the system does not have / any RTC function or valid timestamp is not needed, set _FS_NORTC = 1 to disable / the timestamp function. All objects modified by FatFs will have a fixed timestamp diff --git a/firmware/targets/f7/furi_hal/furi_hal_rtc.c b/firmware/targets/f7/furi_hal/furi_hal_rtc.c index e011406a..84e7fe39 100644 --- a/firmware/targets/f7/furi_hal/furi_hal_rtc.c +++ b/firmware/targets/f7/furi_hal/furi_hal_rtc.c @@ -281,8 +281,10 @@ FuriHalRtcLocaleDateFormat furi_hal_rtc_get_locale_dateformat() { } void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) { + furi_check(!FURI_IS_IRQ_MODE()); furi_assert(datetime); + FURI_CRITICAL_ENTER(); /* Disable write protection */ LL_RTC_DisableWriteProtection(RTC); @@ -319,13 +321,17 @@ void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) { /* Enable write protection */ LL_RTC_EnableWriteProtection(RTC); + FURI_CRITICAL_EXIT(); } void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) { + furi_check(!FURI_IS_IRQ_MODE()); furi_assert(datetime); + FURI_CRITICAL_ENTER(); uint32_t time = LL_RTC_TIME_Get(RTC); // 0x00HHMMSS uint32_t date = LL_RTC_DATE_Get(RTC); // 0xWWDDMMYY + FURI_CRITICAL_EXIT(); datetime->second = __LL_RTC_CONVERT_BCD2BIN((time >> 0) & 0xFF); datetime->minute = __LL_RTC_CONVERT_BCD2BIN((time >> 8) & 0xFF); diff --git a/lib/fatfs/diskio.h b/lib/fatfs/diskio.h index 5b61e570..0ccc9d46 100644 --- a/lib/fatfs/diskio.h +++ b/lib/fatfs/diskio.h @@ -37,7 +37,6 @@ DSTATUS disk_status (BYTE pdrv); DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); -DWORD get_fattime (void); /* Disk Status Bits (DSTATUS) */