From bf730060b59ff8694fe7fbea1410507e772cb5d0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Jul 2023 16:25:00 +0200 Subject: [PATCH] SD shutdown handling non-blocking --- bsp_q7s/core/CoreController.cpp | 53 ++++++++++++++++++++++++++++++--- bsp_q7s/core/CoreController.h | 1 + tmtc | 2 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index 6d8f60c5..60ef389e 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -1229,12 +1229,17 @@ ReturnValue_t CoreController::gracefulShutdownTasks(xsc::Chip chip, xsc::Copy co // Ensure that all writes/reads do finish. sync(); - // Attempt graceful shutdown by unmounting and switching off SD cards - sdcMan->switchOffSdCard(sd::SdCard::SLOT_0); - sdcMan->switchOffSdCard(sd::SdCard::SLOT_1); + // Unmount and switch off SD cards. This could possibly fix issues with the SD card and is + // the more graceful way to reboot the system. + ReturnValue_t result = handleSwitchingSdCardsOffNonBlocking(); + if(result != returnvalue::OK) { + sif::error << "CoreController::gracefulShutdownTasks: Issues unmounting or switching SD cards off" + << std::endl; + } + // If any boot copies are unprotected. // Actually this function only ensures that reboots to the own image are protected.. - ReturnValue_t result = setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true, + result = setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true, protOpPerformed, false); if (result == returnvalue::OK and protOpPerformed) { // TODO: Would be nice to notify operator. But we can't use the filesystem anymore @@ -2583,6 +2588,46 @@ void CoreController::announceSdInfo(SdCardManager::SdStatePair sdStates) { triggerEvent(core::ACTIVE_SD_INFO, p1, p2); } +ReturnValue_t CoreController::handleSwitchingSdCardsOffNonBlocking() { + Countdown maxWaitTimeCd(6000); + Stopwatch watch; + + sdcMan->setBlocking(false); + SdCardManager::Operations op; + auto waitingForFinish = [&]() { + while (sdcMan->checkCurrentOp(op) == SdCardManager::OpStatus::ONGOING) { + TaskFactory::delayTask(50); + if (maxWaitTimeCd.hasTimedOut()) { + return returnvalue::FAILED; + } + } + return returnvalue::OK; + }; + + sdcMan->unmountSdCard(sd::SdCard::SLOT_0); + ReturnValue_t result = waitingForFinish(); + if (result != returnvalue::OK) { + return result; + } + sdcMan->unmountSdCard(sd::SdCard::SLOT_1); + result = waitingForFinish(); + if (result != returnvalue::OK) { + return result; + } + + sdcMan->switchOffSdCard(sd::SdCard::SLOT_0, false); + result = waitingForFinish(); + if (result != returnvalue::OK) { + return result; + } + sdcMan->switchOffSdCard(sd::SdCard::SLOT_1, false); + result = waitingForFinish(); + if (result != returnvalue::OK) { + return result; + } + return result; +} + bool CoreController::isNumber(const std::string &s) { return !s.empty() && std::find_if(s.begin(), s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end(); diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index b6382d36..7c17de6f 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -374,6 +374,7 @@ class CoreController : public ExtendedControllerBase, public ReceivesParameterMe ReturnValue_t gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy, bool& protOpPerformed); ReturnValue_t handleProtInfoUpdateLine(std::string nextLine); + ReturnValue_t handleSwitchingSdCardsOffNonBlocking(); int handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect, bool& protOperationPerformed, bool selfChip, bool selfCopy, bool allChips, bool allCopies, uint8_t arrIdx); diff --git a/tmtc b/tmtc index a82cbff5..95b69541 160000 --- a/tmtc +++ b/tmtc @@ -1 +1 @@ -Subproject commit a82cbff5a83eb37c68234bdeecd3b7d308d65eb1 +Subproject commit 95b69541751c9ea7146e0a342122da83bdb50f29