diff --git a/CHANGELOG.md b/CHANGELOG.md index 45cdd648..c5d562e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added +- FreeRTOS monotonic clock which is not subjected to time jumps of the system clock - add CFDP subsystem ID https://egit.irs.uni-stuttgart.de/fsfw/fsfw/pulls/742 - `PusTmZcWriter` now exposes API to set message counter field. diff --git a/src/fsfw/osal/freertos/Clock.cpp b/src/fsfw/osal/freertos/Clock.cpp index 59ccc109..43ff7b72 100644 --- a/src/fsfw/osal/freertos/Clock.cpp +++ b/src/fsfw/osal/freertos/Clock.cpp @@ -5,6 +5,7 @@ #include "FreeRTOS.h" #include "fsfw/globalfunctions/timevalOperations.h" +#include "fsfw/serviceinterface/ServiceInterfacePrinter.h" #include "fsfw/osal/freertos/Timekeeper.h" #include "task.h" @@ -47,8 +48,8 @@ ReturnValue_t Clock::getClock(timeval* time) { } ReturnValue_t Clock::getClockMonotonic(timeval* time) { - // TODO: I don't actually know if the timekeeper is monotonic.. - return getClock_timeval(time); + *time = Timekeeper::instance()->getMonotonicClockOffset() + getUptime(); + return returnvalue::OK; } ReturnValue_t Clock::getUptime(timeval* uptime) { @@ -58,7 +59,7 @@ ReturnValue_t Clock::getUptime(timeval* uptime) { } timeval Clock::getUptime() { - TickType_t ticksSinceStart = xTaskGetTickCount(); + TickType_t ticksSinceStart = Timekeeper::instance()->getTicks(); return Timekeeper::ticksToTimeval(ticksSinceStart); } diff --git a/src/fsfw/osal/freertos/Timekeeper.cpp b/src/fsfw/osal/freertos/Timekeeper.cpp index 6649e099..ca0fe5d0 100644 --- a/src/fsfw/osal/freertos/Timekeeper.cpp +++ b/src/fsfw/osal/freertos/Timekeeper.cpp @@ -17,7 +17,13 @@ Timekeeper* Timekeeper::instance() { return myinstance; } -void Timekeeper::setOffset(const timeval& offset) { this->offset = offset; } +void Timekeeper::setOffset(const timeval& offset) { + if (not monotonicClockInitialized) { + this->monotonicClockOffset = offset; + monotonicClockInitialized = true; + } + this->offset = offset; +} timeval Timekeeper::ticksToTimeval(TickType_t ticks) { timeval uptime; @@ -33,3 +39,7 @@ timeval Timekeeper::ticksToTimeval(TickType_t ticks) { } TickType_t Timekeeper::getTicks() { return xTaskGetTickCount(); } + +const timeval Timekeeper::getMonotonicClockOffset() const { + return monotonicClockOffset; +} diff --git a/src/fsfw/osal/freertos/Timekeeper.h b/src/fsfw/osal/freertos/Timekeeper.h index 9068b902..f7970bc8 100644 --- a/src/fsfw/osal/freertos/Timekeeper.h +++ b/src/fsfw/osal/freertos/Timekeeper.h @@ -18,9 +18,14 @@ class Timekeeper { Timekeeper(); timeval offset; + // Set when offset is initialized the first time + timeval monotonicClockOffset; + bool monotonicClockInitialized = false; static Timekeeper* myinstance; + void setMonotonicClockOffset(const timeval& monotonicClockOffset); + public: static Timekeeper* instance(); virtual ~Timekeeper(); @@ -34,6 +39,7 @@ class Timekeeper { const timeval& getOffset() const; void setOffset(const timeval& offset); + const timeval getMonotonicClockOffset() const; }; #endif /* FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ */ diff --git a/src/fsfw/timemanager/Clock.h b/src/fsfw/timemanager/Clock.h index 6fe6486c..e9c1dd05 100644 --- a/src/fsfw/timemanager/Clock.h +++ b/src/fsfw/timemanager/Clock.h @@ -192,6 +192,8 @@ class Clock { static MutexIF *timeMutex; static uint16_t leapSeconds; static bool leapSecondsSet; + static bool monotonicClockInitialized; + static timeval monotonicClockOffset; }; #endif /* FSFW_TIMEMANAGER_CLOCK_H_ */