From 65e71508acb260045ffd740eeaac9c3927c9a3b0 Mon Sep 17 00:00:00 2001 From: Mark Thompson <129641948+NotherNgineer@users.noreply.github.com> Date: Mon, 9 Oct 2023 05:04:46 -0500 Subject: [PATCH] Incrementing date workaround for dead RTC battery (#1479) * Enable f_utime function * Added file_update_date() function * Added rtc_battery_workaround() function * Update ui_navigation.cpp --- firmware/application/file.cpp | 8 ++++ firmware/application/file.hpp | 1 + firmware/application/ui_navigation.cpp | 56 ++++++++++++++++++++++++-- firmware/application/ui_navigation.hpp | 4 ++ firmware/common/ffconf.h | 2 +- 5 files changed, 66 insertions(+), 5 deletions(-) diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp index b22c7133..57ae0899 100644 --- a/firmware/application/file.cpp +++ b/firmware/application/file.cpp @@ -325,6 +325,14 @@ FATTimestamp file_created_date(const std::filesystem::path& file_path) { return {filinfo.fdate, filinfo.ftime}; } +std::filesystem::filesystem_error file_update_date(const std::filesystem::path& file_path, FATTimestamp timestamp) { + FILINFO filinfo{}; + + filinfo.fdate = timestamp.FAT_date; + filinfo.ftime = timestamp.FAT_time; + return f_utime(reinterpret_cast(file_path.c_str()), &filinfo); +} + std::filesystem::filesystem_error make_new_file( const std::filesystem::path& file_path) { File f; diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp index d8662c9a..35632685 100644 --- a/firmware/application/file.hpp +++ b/firmware/application/file.hpp @@ -270,6 +270,7 @@ std::filesystem::filesystem_error rename_file(const std::filesystem::path& file_ std::filesystem::filesystem_error copy_file(const std::filesystem::path& file_path, const std::filesystem::path& dest_path); FATTimestamp file_created_date(const std::filesystem::path& file_path); +std::filesystem::filesystem_error file_update_date(const std::filesystem::path& file_path, FATTimestamp timestamp); std::filesystem::filesystem_error make_new_file(const std::filesystem::path& file_path); std::filesystem::filesystem_error make_new_directory(const std::filesystem::path& dir_path); std::filesystem::filesystem_error ensure_directory(const std::filesystem::path& dir_path); diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 2d0ea6ec..925d3dca 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -159,6 +159,8 @@ SystemStatusView::SystemStatusView( &status_icons, }); + rtc_battery_workaround(); + if (pmem::should_use_sdcard_for_pmem()) { pmem::load_persistent_settings_from_file(); } @@ -360,6 +362,55 @@ void SystemStatusView::on_title() { nav_.pop(); } +void SystemStatusView::rtc_battery_workaround() { + if (sd_card::status() != sd_card::Status::Mounted) + return; + + uint16_t year; + uint8_t month; + uint8_t day; + FATTimestamp timestamp; + rtc::RTC datetime; + + rtcGetTime(&RTCD1, &datetime); + + // if year is 0000, assume RTC battery is dead + if (datetime.year() == 0) { + // if timestamp file is present, use it's date and add 1 day + if (std::filesystem::file_exists(DATE_FILEFLAG)) { + timestamp = file_created_date(DATE_FILEFLAG); + + year = (timestamp.FAT_date >> 9) + 1980; + month = (timestamp.FAT_date >> 5) & 0xF; + day = timestamp.FAT_date & 0x1F; + + // bump to next month at 28 days for simplicity + if (++day > 28) { + day = 1; + if (++month > 12) { + month = 1; + ++year; + } + } + } else { + make_new_file(DATE_FILEFLAG); + + year = 1980; + month = 1; + day = 1; + } + + // update RTC (keeps ticking while powered on regardless of RTC battery condition) + rtc::RTC new_datetime{year, month, day, datetime.hour(), datetime.minute(), datetime.second()}; + rtcSetTime(&RTCD1, &new_datetime); + + // update file date + timestamp.FAT_date = ((year - 1980) << 9) | ((uint16_t)month << 5) | day; + timestamp.FAT_time = 0; + file_update_date(DATE_FILEFLAG, timestamp); + } +} + /* Information View *****************************************************/ InformationView::InformationView( @@ -376,11 +427,8 @@ InformationView::InformationView( <ime}); version.set_style(&style_infobar); - - ltime.set_hide_clock(pmem::hide_clock()); ltime.set_style(&style_infobar); - ltime.set_seconds_enabled(true); - ltime.set_date_enabled(pmem::clock_with_date()); + refresh(); set_dirty(); } diff --git a/firmware/application/ui_navigation.hpp b/firmware/application/ui_navigation.hpp index d7e00afe..d155686c 100644 --- a/firmware/application/ui_navigation.hpp +++ b/firmware/application/ui_navigation.hpp @@ -44,6 +44,9 @@ #include #include +// for incrementing fake date when RTC battery is dead +#define DATE_FILEFLAG u"/SETTINGS/DATE_FILEFLAG" + using namespace sd_card; namespace ui { @@ -235,6 +238,7 @@ class SystemStatusView : public View { void on_title(); void refresh(); void on_clk(); + void rtc_battery_workaround(); MessageHandlerRegistration message_handler_refresh{ Message::ID::StatusRefresh, diff --git a/firmware/common/ffconf.h b/firmware/common/ffconf.h index 7737c2ba..6d03383f 100644 --- a/firmware/common/ffconf.h +++ b/firmware/common/ffconf.h @@ -47,7 +47,7 @@ #define _USE_EXPAND 0 /* This option switches f_expand function. (0:Disable or 1:Enable) */ -#define _USE_CHMOD 0 +#define _USE_CHMOD 1 /* This option switches attribute manipulation functions, f_chmod() and f_utime(). / (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */