From 3b17af9d07adb14d0114a8064ec29aeb6976eb56 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Thu, 23 Feb 2023 10:09:04 +0100 Subject: [PATCH] added local parameter handler to PdecConfig to store persistent parameters --- bsp_q7s/fs/FilesystemHelper.cpp | 2 +- bsp_q7s/memory/LocalParameterHandler.cpp | 9 ++- bsp_q7s/memory/LocalParameterHandler.h | 4 ++ linux/fsfwconfig/returnvalues/classIds.h | 1 + linux/ipcore/PdecConfig.cpp | 18 +++-- linux/ipcore/PdecHandler.cpp | 87 +++++++++++++++--------- linux/ipcore/PdecHandler.h | 13 ++++ mission/devices/PayloadPcduHandler.cpp | 2 +- 8 files changed, 89 insertions(+), 47 deletions(-) diff --git a/bsp_q7s/fs/FilesystemHelper.cpp b/bsp_q7s/fs/FilesystemHelper.cpp index bc435d1c..8f49d109 100644 --- a/bsp_q7s/fs/FilesystemHelper.cpp +++ b/bsp_q7s/fs/FilesystemHelper.cpp @@ -22,7 +22,7 @@ ReturnValue_t FilesystemHelper::checkPath(std::string path) { } } else if (path.substr(0, sizeof(config::SD_1_MOUNT_POINT)) == std::string(config::SD_1_MOUNT_POINT)) { - if (!sdcMan->isSdCardUsable(sd::SLOT_0)) { + if (!sdcMan->isSdCardUsable(sd::SLOT_1)) { sif::warning << "FilesystemHelper::checkPath: SD card 1 not mounted" << std::endl; return SD_NOT_MOUNTED; } diff --git a/bsp_q7s/memory/LocalParameterHandler.cpp b/bsp_q7s/memory/LocalParameterHandler.cpp index 953d79a2..b24a3e08 100644 --- a/bsp_q7s/memory/LocalParameterHandler.cpp +++ b/bsp_q7s/memory/LocalParameterHandler.cpp @@ -8,7 +8,14 @@ LocalParameterHandler::~LocalParameterHandler() { } ReturnValue_t LocalParameterHandler::initialize() { - std::string mountPrefix = sdcMan->getCurrentMountPrefix(); + std::string mountPrefix; + auto activeSd = sdcMan->getActiveSdCard(); + if (activeSd and sdcMan->isSdCardUsable(activeSd.value())) { + mountPrefix = sdcMan->getCurrentMountPrefix(); + } else { + return SD_NOT_READY; + } + mountPrefix = sdcMan->getCurrentMountPrefix(); std::string fullname = mountPrefix + "/" + sdRelativeName; setFullName(fullname); ReturnValue_t result = readJsonFile(); diff --git a/bsp_q7s/memory/LocalParameterHandler.h b/bsp_q7s/memory/LocalParameterHandler.h index 4f872131..37fa7f2c 100644 --- a/bsp_q7s/memory/LocalParameterHandler.h +++ b/bsp_q7s/memory/LocalParameterHandler.h @@ -13,6 +13,10 @@ */ class LocalParameterHandler : public NVMParameterBase { public: + + static constexpr uint8_t INTERFACE_ID = CLASS_ID::LOCAL_PARAM_HANDLER; + + static constexpr ReturnValue_t SD_NOT_READY = returnvalue::makeCode(INTERFACE_ID, 0); /** * @brief Constructor * diff --git a/linux/fsfwconfig/returnvalues/classIds.h b/linux/fsfwconfig/returnvalues/classIds.h index abd8f785..e42c3c3f 100644 --- a/linux/fsfwconfig/returnvalues/classIds.h +++ b/linux/fsfwconfig/returnvalues/classIds.h @@ -15,6 +15,7 @@ enum { CLASS_ID_START = COMMON_CLASS_ID_END, SD_CARD_MANAGER, // SDMA SCRATCH_BUFFER, // SCBU + LOCAL_PARAM_HANDLER, // LPH CLASS_ID_END // [EXPORT] : [END] }; } diff --git a/linux/ipcore/PdecConfig.cpp b/linux/ipcore/PdecConfig.cpp index f299b060..4051fe09 100644 --- a/linux/ipcore/PdecConfig.cpp +++ b/linux/ipcore/PdecConfig.cpp @@ -4,7 +4,7 @@ #include "fsfw/serviceinterface/ServiceInterface.h" #include "pdecconfigdefs.h" -PdecConfig::PdecConfig() : localParameterHandler("conf/pdecconfig", SdCardManager::instance()) {} +PdecConfig::PdecConfig() : localParameterHandler("conf/pdecconfig.json", SdCardManager::instance()) {} PdecConfig::~PdecConfig() {} @@ -35,15 +35,13 @@ ReturnValue_t PdecConfig::write() { ReturnValue_t PdecConfig::initializePersistentParameters() { ReturnValue_t result = localParameterHandler.initialize(); - if (result != returnvalue::OK) { - if (result == HasFileSystemIF::FILE_DOES_NOT_EXIST) { - result = createPersistentConfig(); - if (result != returnvalue::OK) { - return result; - } - } - } - return returnvalue::OK; + if (result == HasFileSystemIF::FILE_DOES_NOT_EXIST) { + result = createPersistentConfig(); + if (result != returnvalue::OK) { + return result; + } + } + return result; } ReturnValue_t PdecConfig::createPersistentConfig() { diff --git a/linux/ipcore/PdecHandler.cpp b/linux/ipcore/PdecHandler.cpp index 0f43c6f7..ebf9376a 100644 --- a/linux/ipcore/PdecHandler.cpp +++ b/linux/ipcore/PdecHandler.cpp @@ -77,22 +77,6 @@ ReturnValue_t PdecHandler::initialize() { return returnvalue::FAILED; } - result = pdecConfig.write(); - if (result != returnvalue::OK) { - sif::error << "PdecHandler::initialize: Failed to write PDEC config" << std::endl; - return ObjectManagerIF::CHILD_INIT_FAILED; - } - - result = releasePdec(); - if (result != returnvalue::OK) { - return ObjectManagerIF::CHILD_INIT_FAILED; - } - - // This configuration must be done while the PDEC is not held in reset. - if (OP_MODE == Modes::IRQ) { - // Configure interrupt mask register to enable interrupts - *(registerBaseAddress + PDEC_IMR_OFFSET) = pdecConfig.getImrReg(); - } result = actionHelper.initialize(commandQueue); if (result != returnvalue::OK) { return result; @@ -101,6 +85,36 @@ ReturnValue_t PdecHandler::initialize() { return returnvalue::OK; } +ReturnValue_t PdecHandler::firstLoop() { + ReturnValue_t result = pdecConfig.write(); + if (result != returnvalue::OK) { + if (result == LocalParameterHandler::SD_NOT_READY) { + return result; + } else { + sif::error << "PdecHandler::firstLoop: Failed to write PDEC config" << std::endl; + } + return returnvalue::FAILED; + } + + result = releasePdec(); + if (result != returnvalue::OK) { + return returnvalue::FAILED; + } + + // This configuration must be done while the PDEC is not held in reset. + if (OP_MODE == Modes::IRQ) { + // Configure interrupt mask register to enable interrupts + *(registerBaseAddress + PDEC_IMR_OFFSET) = pdecConfig.getImrReg(); + } + result = resetFarStatFlag(); + if (result != returnvalue::OK) { + // Requires reconfiguration and reinitialization of PDEC + triggerEvent(INVALID_FAR); + return result; + } + return returnvalue::OK; +} + ReturnValue_t PdecHandler::performOperation(uint8_t operationCode) { if (OP_MODE == Modes::POLLED) { return polledOperation(); @@ -111,19 +125,11 @@ ReturnValue_t PdecHandler::performOperation(uint8_t operationCode) { } ReturnValue_t PdecHandler::polledOperation() { - ReturnValue_t result = returnvalue::OK; readCommandQueue(); switch (state) { case State::INIT: - resetFarStatFlag(); - if (result != returnvalue::OK) { - // Requires reconfiguration and reinitialization of PDEC - triggerEvent(INVALID_FAR); - state = State::WAIT_FOR_RECOVERY; - break; - } - state = State::RUNNING; + handleInitState(); break; case State::RUNNING: if (newTcReceived()) { @@ -143,7 +149,6 @@ ReturnValue_t PdecHandler::polledOperation() { // See https://yurovsky.github.io/2014/10/10/linux-uio-gpio-interrupt.html for more information. ReturnValue_t PdecHandler::irqOperation() { - ReturnValue_t result = returnvalue::OK; int fd = open(uioNames.irq, O_RDWR); if (fd < 0) { sif::error << "PdecHandler::irqOperation: Opening UIO IRQ file" << uioNames.irq << " failed" @@ -166,14 +171,7 @@ ReturnValue_t PdecHandler::irqOperation() { readCommandQueue(); switch (state) { case State::INIT: - result = resetFarStatFlag(); - if (result != returnvalue::OK) { - // Requires reconfiguration and reinitialization of PDEC - triggerEvent(INVALID_FAR); - state = State::WAIT_FOR_RECOVERY; - return result; - } - state = State::RUNNING; + handleInitState(); break; case State::RUNNING: { checkAndHandleIrqs(fd, info); @@ -194,6 +192,27 @@ ReturnValue_t PdecHandler::irqOperation() { return returnvalue::OK; } +void PdecHandler::handleInitState() { + ReturnValue_t result = firstLoop(); + if (result != returnvalue::OK) { + if (result == LocalParameterHandler::SD_NOT_READY) { + TaskFactory::delayTask(400); + if (initTries == MAX_INIT_TRIES) { + sif::error << "PdecHandler::handleInitState: SD card never " + "becomes ready" + << std::endl; + state = State::WAIT_FOR_RECOVERY; + return; + } else { + state = State::INIT; + return; + } + } + state = State::WAIT_FOR_RECOVERY; + } + state = State::RUNNING; +} + ReturnValue_t PdecHandler::checkAndHandleIrqs(int fd, uint32_t& info) { ssize_t nb = write(fd, &info, sizeof(info)); if (nb != static_cast(sizeof(info))) { diff --git a/linux/ipcore/PdecHandler.h b/linux/ipcore/PdecHandler.h index 2c8da14c..d43c86e3 100644 --- a/linux/ipcore/PdecHandler.h +++ b/linux/ipcore/PdecHandler.h @@ -169,6 +169,8 @@ class PdecHandler : public SystemObject, static const uint32_t NO_RF_MASK = 0x8000; static const uint32_t NO_BITLOCK_MASK = 0x4000; + static const uint32_t MAX_INIT_TRIES = 20; + class ParameterId { public: // ID of the parameter to update the positive window of AD frames @@ -258,6 +260,16 @@ class PdecHandler : public SystemObject, PdecConfig pdecConfig; + uint32_t initTries = 0; + + /** + * @brief Performs initialization stuff which must be performed in first + * loop of running task + * + * @return OK if successful, otherwise FAILED + */ + ReturnValue_t firstLoop(); + /** * @brief Reads and handles messages stored in the commandQueue */ @@ -265,6 +277,7 @@ class PdecHandler : public SystemObject, ReturnValue_t polledOperation(); ReturnValue_t irqOperation(); + void handleInitState(); ReturnValue_t checkAndHandleIrqs(int fd, uint32_t& info); uint32_t readFar(); diff --git a/mission/devices/PayloadPcduHandler.cpp b/mission/devices/PayloadPcduHandler.cpp index 44dd667c..efefb16b 100644 --- a/mission/devices/PayloadPcduHandler.cpp +++ b/mission/devices/PayloadPcduHandler.cpp @@ -664,7 +664,7 @@ ReturnValue_t PayloadPcduHandler::getParameter(uint8_t domainId, uint8_t uniqueI } void PayloadPcduHandler::handleFailureInjection(std::string output, Event event) { - sif::info << "PayloadPcduHandler::checkAdcValues: " << output + sif::info << "PayloadPcduHandler::handleFailureInjection: " << output << " failure injection. " "Transitioning back to off" << std::endl;