refactores rtems task module

This commit is contained in:
Robin Müller 2022-05-29 20:52:52 +02:00 committed by Gitea
parent 24e849ed9c
commit 28873fc87b
9 changed files with 70 additions and 160 deletions

View File

@ -11,9 +11,11 @@ PeriodicTask::PeriodicTask(const char* name, TaskPriority setPriority, TaskStack
BaseType_t status = xTaskCreate(taskEntryPoint, name, stackSize, this, setPriority, &handle);
if (status != pdPASS) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::debug << "PeriodicTask::PeriodicTask Insufficient heap memory remaining. Status: " << status << std::endl;
sif::debug << "PeriodicTask::PeriodicTask Insufficient heap memory remaining. Status: "
<< status << std::endl;
#else
sif::printDebug("PeriodicTask::PeriodicTask: Insufficient heap memory remaining. Status: %d\n", status);
sif::printDebug("PeriodicTask::PeriodicTask: Insufficient heap memory remaining. Status: %d\n",
status);
#endif
}
}

View File

@ -1,19 +1,13 @@
#include "fsfw/osal/rtems/FixedTimeslotTask.h"
#include <rtems/bspIo.h>
#include <rtems/io.h>
#include <rtems/rtems/ratemon.h>
#include <rtems/rtems/status.h>
#include <rtems/rtems/tasks.h>
#include <rtems/rtems/types.h>
#include <sys/_stdint.h>
#include "fsfw/objectmanager/ObjectManager.h"
#include "fsfw/objectmanager/SystemObjectIF.h"
#include "fsfw/osal/rtems/RtemsBasic.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw/tasks/FixedSequenceSlot.h"
#if FSFW_CPP_OSTREAM_ENABLED == 1
#include <iostream>
@ -25,18 +19,17 @@
uint32_t FixedTimeslotTask::deadlineMissedCount = 0;
FixedTimeslotTask::FixedTimeslotTask(const char *name, rtems_task_priority setPriority,
size_t setStack, uint32_t setOverallPeriod,
void (*setDeadlineMissedFunc)(void))
: RTEMSTaskBase(setPriority, setStack, name), periodId(0), pst(setOverallPeriod) {
// All additional attributes are applied to the object.
this->deadlineMissedFunc = setDeadlineMissedFunc;
}
size_t setStack, TaskPeriod setOverallPeriod,
TaskDeadlineMissedFunction dlmFunc_)
: FixedTimeslotTaskBase(setOverallPeriod, dlmFunc_),
RTEMSTaskBase(setPriority, setStack, name),
periodId(0) {}
FixedTimeslotTask::~FixedTimeslotTask() {}
FixedTimeslotTask::~FixedTimeslotTask() = default;
rtems_task FixedTimeslotTask::taskEntryPoint(rtems_task_argument argument) {
/* The argument is re-interpreted as a FixedTimeslotTask */
FixedTimeslotTask *originalTask(reinterpret_cast<FixedTimeslotTask *>(argument));
auto *originalTask(reinterpret_cast<FixedTimeslotTask *>(argument));
/* The task's functionality is called. */
return originalTask->taskFunctionality();
/* Should never be reached */
@ -79,54 +72,35 @@ ReturnValue_t FixedTimeslotTask::startTask() {
}
}
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, uint32_t slotTimeMs,
int8_t executionStep) {
ExecutableObjectIF *object = ObjectManager::instance()->get<ExecutableObjectIF>(componentId);
if (object != nullptr) {
pst.addSlot(componentId, slotTimeMs, executionStep, object, this);
return HasReturnvaluesIF::RETURN_OK;
}
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "Component " << std::hex << componentId << " not found, not adding it to pst"
<< std::endl;
#endif
return HasReturnvaluesIF::RETURN_FAILED;
}
uint32_t FixedTimeslotTask::getPeriodMs() const { return pst.getLengthMs(); }
ReturnValue_t FixedTimeslotTask::checkSequence() const { return pst.checkSequence(); }
void FixedTimeslotTask::taskFunctionality() {
[[noreturn]] void FixedTimeslotTask::taskFunctionality() {
/* A local iterator for the Polling Sequence Table is created to find the start time for
the first entry. */
FixedSlotSequence::SlotListIter it = pst.current;
auto it = pollingSeqTable.current;
/* Initialize the PST with the correct calling task */
pst.intializeSequenceAfterTaskCreation();
pollingSeqTable.intializeSequenceAfterTaskCreation();
/* The start time for the first entry is read. */
rtems_interval interval = RtemsBasic::convertMsToTicks(it->pollingTimeMs);
RTEMSTaskBase::setAndStartPeriod(interval, &periodId);
// The task's "infinite" inner loop is entered.
while (1) {
if (pst.slotFollowsImmediately()) {
while (true) {
if (pollingSeqTable.slotFollowsImmediately()) {
/* Do nothing */
} else {
/* The interval for the next polling slot is selected. */
interval = RtemsBasic::convertMsToTicks(this->pst.getIntervalToNextSlotMs());
interval = RtemsBasic::convertMsToTicks(pollingSeqTable.getIntervalToNextSlotMs());
/* The period is checked and restarted with the new interval.
If the deadline was missed, the deadlineMissedFunc is called. */
rtems_status_code status = RTEMSTaskBase::restartPeriod(interval, periodId);
if (status == RTEMS_TIMEOUT) {
if (this->deadlineMissedFunc != nullptr) {
this->deadlineMissedFunc();
if (dlmFunc != nullptr) {
dlmFunc();
}
}
}
/* The device handler for this slot is executed and the next one is chosen. */
this->pst.executeAndAdvance();
this->pollingSeqTable.executeAndAdvance();
}
}

View File

@ -1,11 +1,11 @@
#ifndef FSFW_OSAL_RTEMS_FIXEDTIMESLOTTASK_H_
#define FSFW_OSAL_RTEMS_FIXEDTIMESLOTTASK_H_
#include "../../tasks/FixedSlotSequence.h"
#include "../../tasks/FixedTimeslotTaskIF.h"
#include "RTEMSTaskBase.h"
#include "fsfw/tasks/FixedSlotSequence.h"
#include "fsfw/tasks/FixedTimeslotTaskBase.h"
class FixedTimeslotTask : public RTEMSTaskBase, public FixedTimeslotTaskIF {
class FixedTimeslotTask : public FixedTimeslotTaskBase, public RTEMSTaskBase {
public:
/**
* @brief The standard constructor of the class.
@ -17,7 +17,7 @@ class FixedTimeslotTask : public RTEMSTaskBase, public FixedTimeslotTaskIF {
* @param getPst The object id of the completely initialized polling sequence.
*/
FixedTimeslotTask(const char *name, rtems_task_priority setPriority, size_t setStackSize,
uint32_t overallPeriod, void (*setDeadlineMissedFunc)());
TaskPeriod overallPeriod, TaskDeadlineMissedFunction dlmFunc);
/**
* @brief The destructor of the class.
@ -25,9 +25,9 @@ class FixedTimeslotTask : public RTEMSTaskBase, public FixedTimeslotTaskIF {
* The destructor frees all heap memory that was allocated on thread initialization
* for the PST andthe device handlers. This is done by calling the PST's destructor.
*/
virtual ~FixedTimeslotTask(void);
~FixedTimeslotTask() override;
ReturnValue_t startTask(void);
ReturnValue_t startTask() override;
/**
* This static function can be used as #deadlineMissedFunc.
* It counts missedDeadlines and prints the number of missed deadlines every 10th time.
@ -38,31 +38,13 @@ class FixedTimeslotTask : public RTEMSTaskBase, public FixedTimeslotTaskIF {
*/
static uint32_t deadlineMissedCount;
ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs, int8_t executionStep);
uint32_t getPeriodMs() const;
ReturnValue_t checkSequence() const;
ReturnValue_t sleepFor(uint32_t ms);
ReturnValue_t sleepFor(uint32_t ms) override;
protected:
/**
* @brief id of the associated OS period
*/
rtems_id periodId;
FixedSlotSequence pst;
/**
* @brief This attribute holds a function pointer that is executed when a deadline was missed.
*
* @details
* Another function may be announced to determine the actions to perform when a deadline
* was missed. Currently, only one function for missing any deadline is allowed.
* If not used, it shall be declared NULL.
*/
void (*deadlineMissedFunc)(void) = nullptr;
/**
* @brief This is the entry point in a new polling thread.
* @details This method is the entry point in the new thread
@ -76,7 +58,7 @@ class FixedTimeslotTask : public RTEMSTaskBase, public FixedTimeslotTaskIF {
* It links the functionalities provided by FixedSlotSequence with the OS's system calls to
* keep the timing of the periods.
*/
void taskFunctionality(void);
[[noreturn]] void taskFunctionality();
};
#endif /* FSFW_OSAL_RTEMS_FIXEDTIMESLOTTASK_H_ */

View File

@ -37,8 +37,8 @@ class MessageQueue : public MessageQueueBase {
* This should be left default.
*/
explicit MessageQueue(size_t message_depth = 3,
size_t max_message_size = MessageQueueMessage::MAX_MESSAGE_SIZE,
MqArgs* args = nullptr);
size_t max_message_size = MessageQueueMessage::MAX_MESSAGE_SIZE,
MqArgs* args = nullptr);
/** Copying message queues forbidden */
MessageQueue(const MessageQueue&) = delete;
@ -55,8 +55,7 @@ class MessageQueue : public MessageQueueBase {
ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override;
ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo, MessageQueueMessageIF* message,
MessageQueueId_t sentFrom,
bool ignoreFault) override;
MessageQueueId_t sentFrom, bool ignoreFault) override;
private:
/**

View File

@ -5,12 +5,12 @@
#include "fsfw/tasks/ExecutableObjectIF.h"
PeriodicTask::PeriodicTask(const char* name, rtems_task_priority setPriority, size_t setStack,
rtems_interval setPeriod, void (*setDeadlineMissedFunc)())
: RTEMSTaskBase(setPriority, setStack, name),
periodTicks(RtemsBasic::convertMsToTicks(setPeriod)),
deadlineMissedFunc(setDeadlineMissedFunc) {}
TaskPeriod setPeriod, TaskDeadlineMissedFunction dlmFunc_)
: PeriodicTaskBase(setPeriod, dlmFunc_),
RTEMSTaskBase(setPriority, setStack, name),
periodTicks(RtemsBasic::convertMsToTicks(static_cast<uint32_t>(setPeriod * 1000.0))) {}
PeriodicTask::~PeriodicTask(void) {
PeriodicTask::~PeriodicTask() {
/* Do not delete objects, we were responsible for pointers only. */
rtems_rate_monotonic_delete(periodId);
}
@ -18,7 +18,7 @@ PeriodicTask::~PeriodicTask(void) {
rtems_task PeriodicTask::taskEntryPoint(rtems_task_argument argument) {
/* The argument is re-interpreted as MultiObjectTask. The Task object is global,
so it is found from any place. */
PeriodicTask* originalTask(reinterpret_cast<PeriodicTask*>(argument));
auto* originalTask(reinterpret_cast<PeriodicTask*>(argument));
return originalTask->taskFunctionality();
;
}
@ -28,8 +28,10 @@ ReturnValue_t PeriodicTask::startTask() {
rtems_task_start(id, PeriodicTask::taskEntryPoint, rtems_task_argument((void*)this));
if (status != RTEMS_SUCCESSFUL) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "ObjectTask::startTask for " << std::hex << this->getId() << std::dec
<< " failed." << std::endl;
sif::error << "PeriodicTask::startTask for " << std::hex << this->getId() << std::dec
<< " failed" << std::endl;
#else
sif::printError("PeriodicTask::startTask for 0x%08x failed\n", getId());
#endif
}
switch (status) {
@ -47,38 +49,20 @@ ReturnValue_t PeriodicTask::startTask() {
ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) { return RTEMSTaskBase::sleepFor(ms); }
void PeriodicTask::taskFunctionality() {
[[noreturn]] void PeriodicTask::taskFunctionality() {
RTEMSTaskBase::setAndStartPeriod(periodTicks, &periodId);
for (const auto& object : objectList) {
object->initializeAfterTaskCreation();
}
initObjsAfterTaskCreation();
/* The task's "infinite" inner loop is entered. */
while (1) {
for (const auto& object : objectList) {
object->performOperation();
while (true) {
for (const auto& objectPair : objectList) {
objectPair.first->performOperation(objectPair.second);
}
rtems_status_code status = RTEMSTaskBase::restartPeriod(periodTicks, periodId);
if (status == RTEMS_TIMEOUT) {
if (this->deadlineMissedFunc != nullptr) {
this->deadlineMissedFunc();
if (dlmFunc != nullptr) {
dlmFunc();
}
}
}
}
ReturnValue_t PeriodicTask::addComponent(object_id_t object) {
ExecutableObjectIF* newObject = ObjectManager::instance()->get<ExecutableObjectIF>(object);
return addComponent(newObject);
}
ReturnValue_t PeriodicTask::addComponent(ExecutableObjectIF* object) {
if (object == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
objectList.push_back(object);
object->setTaskIF(this);
return HasReturnvaluesIF::RETURN_OK;
}
uint32_t PeriodicTask::getPeriodMs() const { return RtemsBasic::convertTicksToMs(periodTicks); }

View File

@ -3,9 +3,10 @@
#include <vector>
#include "../../objectmanager/ObjectManagerIF.h"
#include "../../tasks/PeriodicTaskIF.h"
#include "RTEMSTaskBase.h"
#include "fsfw/objectmanager/ObjectManagerIF.h"
#include "fsfw/tasks/PeriodicTaskBase.h"
#include "fsfw/tasks/PeriodicTaskIF.h"
class ExecutableObjectIF;
@ -18,7 +19,7 @@ class ExecutableObjectIF;
* @author baetz
* @ingroup task_handling
*/
class PeriodicTask : public RTEMSTaskBase, public PeriodicTaskIF {
class PeriodicTask : public PeriodicTaskBase, public RTEMSTaskBase {
public:
/**
* @brief Standard constructor of the class.
@ -36,12 +37,12 @@ class PeriodicTask : public RTEMSTaskBase, public PeriodicTaskIF {
* that shall be assigned.
*/
PeriodicTask(const char *name, rtems_task_priority setPriority, size_t setStack,
rtems_interval setPeriod, void (*setDeadlineMissedFunc)());
TaskPeriod setPeriod, TaskDeadlineMissedFunction dlmFunc);
/**
* @brief Currently, the executed object's lifetime is not coupled with the task object's
* lifetime, so the destructor is empty.
*/
virtual ~PeriodicTask(void);
~PeriodicTask() override;
/**
* @brief The method to start the task.
@ -50,33 +51,11 @@ class PeriodicTask : public RTEMSTaskBase, public PeriodicTaskIF {
* The address of the task object is passed as an argument
* to the system call.
*/
ReturnValue_t startTask(void);
/**
* Adds an object to the list of objects to be executed.
* The objects are executed in the order added.
* @param object Id of the object to add.
* @return RETURN_OK on success, RETURN_FAILED if the object could not be added.
*/
ReturnValue_t addComponent(object_id_t object) override;
/**
* Adds an object to the list of objects to be executed.
* The objects are executed in the order added.
* @param object pointer to the object to add.
* @return RETURN_OK on success, RETURN_FAILED if the object could not be added.
*/
ReturnValue_t addComponent(ExecutableObjectIF *object) override;
uint32_t getPeriodMs() const override;
ReturnValue_t startTask() override;
ReturnValue_t sleepFor(uint32_t ms) override;
protected:
typedef std::vector<ExecutableObjectIF *> ObjectList; //!< Typedef for the List of objects.
/**
* @brief This attribute holds a list of objects to be executed.
*/
ObjectList objectList;
/**
* @brief The period of the task.
* @details The period determines the frequency of the task's execution. It is expressed in
@ -87,14 +66,7 @@ class PeriodicTask : public RTEMSTaskBase, public PeriodicTaskIF {
* @brief id of the associated OS period
*/
rtems_id periodId = 0;
/**
* @brief The pointer to the deadline-missed function.
* @details This pointer stores the function that is executed if the task's deadline is missed.
* So, each may react individually on a timing failure. The pointer may be
* nullptr, then nothing happens on missing the deadline. The deadline is equal to the next
* execution of the periodic task.
*/
void (*deadlineMissedFunc)(void);
/**
* @brief This is the function executed in the new task's context.
* @details It converts the argument back to the thread object type and copies the class
@ -110,7 +82,7 @@ class PeriodicTask : public RTEMSTaskBase, public PeriodicTaskIF {
* are called. Afterwards the checkAndRestartPeriod system call blocks the task until the next
* period. On missing the deadline, the deadlineMissedFunction is executed.
*/
void taskFunctionality(void);
[[noreturn]] void taskFunctionality();
};
#endif /* FSFW_OSAL_RTEMS_PERIODICTASK_H_ */

View File

@ -32,7 +32,7 @@ RTEMSTaskBase::RTEMSTaskBase(rtems_task_priority set_priority, size_t stack_size
RTEMSTaskBase::~RTEMSTaskBase() { rtems_task_delete(id); }
rtems_id RTEMSTaskBase::getId() { return this->id; }
rtems_id RTEMSTaskBase::getId() const { return this->id; }
ReturnValue_t RTEMSTaskBase::sleepFor(uint32_t ms) {
rtems_status_code status = rtems_task_wake_after(RtemsBasic::convertMsToTicks(ms));

View File

@ -36,9 +36,9 @@ class RTEMSTaskBase {
/**
* @brief This method returns the task id of this class.
*/
rtems_id getId();
rtems_id getId() const;
ReturnValue_t sleepFor(uint32_t ms);
static ReturnValue_t sleepFor(uint32_t ms);
static ReturnValue_t setAndStartPeriod(rtems_interval period, rtems_id *periodId);
static rtems_status_code restartPeriod(rtems_interval period, rtems_id periodId);

View File

@ -1,7 +1,6 @@
#include "fsfw/tasks/TaskFactory.h"
#include "fsfw/osal/rtems/FixedTimeslotTask.h"
#include "fsfw/osal/rtems/InitTask.h"
#include "fsfw/osal/rtems/PeriodicTask.h"
#include "fsfw/osal/rtems/RtemsBasic.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
@ -9,29 +8,29 @@
// TODO: Different variant than the lazy loading in QueueFactory. What's better and why?
TaskFactory* TaskFactory::factoryInstance = new TaskFactory();
TaskFactory::~TaskFactory() {}
TaskFactory::TaskFactory() = default;
TaskFactory::~TaskFactory() = default;
TaskFactory* TaskFactory::instance() { return TaskFactory::factoryInstance; }
PeriodicTaskIF* TaskFactory::createPeriodicTask(
TaskName name_, TaskPriority taskPriority_, TaskStackSize stackSize_,
TaskPeriod periodInSeconds_, TaskDeadlineMissedFunction deadLineMissedFunction_) {
rtems_interval taskPeriod = periodInSeconds_ * Clock::getTicksPerSecond();
return static_cast<PeriodicTaskIF*>(
new PeriodicTask(name_, taskPriority_, stackSize_, taskPeriod, deadLineMissedFunction_));
return static_cast<PeriodicTaskIF*>(new PeriodicTask(name_, taskPriority_, stackSize_,
periodInSeconds_, deadLineMissedFunction_));
}
FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(
TaskName name_, TaskPriority taskPriority_, TaskStackSize stackSize_,
TaskPeriod periodInSeconds_, TaskDeadlineMissedFunction deadLineMissedFunction_) {
rtems_interval taskPeriod = periodInSeconds_ * Clock::getTicksPerSecond();
return static_cast<FixedTimeslotTaskIF*>(
new FixedTimeslotTask(name_, taskPriority_, stackSize_, taskPeriod, deadLineMissedFunction_));
return static_cast<FixedTimeslotTaskIF*>(new FixedTimeslotTask(
name_, taskPriority_, stackSize_, periodInSeconds_, deadLineMissedFunction_));
}
ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) {
// TODO not implemented
// This should call the OS specific destructor
delete (dynamic_cast<PeriodicTask*>(task));
return HasReturnvaluesIF::RETURN_FAILED;
}
@ -45,5 +44,3 @@ void TaskFactory::printMissedDeadline() {
/* TODO: Implement */
return;
}
TaskFactory::TaskFactory() {}