2021-07-14 10:50:44 +02:00
|
|
|
#include "fsfw/timemanager/Clock.h"
|
2021-01-01 21:19:42 +01:00
|
|
|
|
2021-01-02 19:40:00 +01:00
|
|
|
#include <rtems/rtems/clockimpl.h>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <rtems/score/todimpl.h>
|
|
|
|
|
|
|
|
#include "fsfw/ipc/MutexGuard.h"
|
|
|
|
#include "fsfw/osal/rtems/RtemsBasic.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
|
2023-01-26 11:33:40 +01:00
|
|
|
ReturnValue_t Clock::setClock(const timeval* time) {
|
|
|
|
TimeOfDay_t time_tod;
|
|
|
|
ReturnValue_t result = convertTimevalToTimeOfDay(time, &time_tod);
|
|
|
|
if (result != returnvalue::OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2022-02-02 10:29:30 +01:00
|
|
|
rtems_time_of_day timeRtems;
|
2023-01-26 11:33:40 +01:00
|
|
|
timeRtems.year = time_tod.year;
|
|
|
|
timeRtems.month = time_tod.month;
|
|
|
|
timeRtems.day = time_tod.day;
|
|
|
|
timeRtems.hour = time_tod.hour;
|
|
|
|
timeRtems.minute = time_tod.minute;
|
|
|
|
timeRtems.second = time_tod.second;
|
|
|
|
timeRtems.ticks = static_cast<uint64_t>(time_tod.usecond) * rtems_clock_get_ticks_per_second() / 1e6;
|
2022-02-02 10:29:30 +01:00
|
|
|
rtems_status_code status = rtems_clock_set(&timeRtems);
|
|
|
|
switch (status) {
|
2021-03-04 23:24:57 +01:00
|
|
|
case RTEMS_SUCCESSFUL:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2021-03-04 23:24:57 +01:00
|
|
|
case RTEMS_INVALID_ADDRESS:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-03-04 23:24:57 +01:00
|
|
|
case RTEMS_INVALID_CLOCK:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-03-04 23:24:57 +01:00
|
|
|
default:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
2022-02-02 10:29:30 +01:00
|
|
|
// Callable from ISR
|
|
|
|
rtems_status_code status = rtems_clock_get_tod_timeval(time);
|
|
|
|
switch (status) {
|
2021-03-04 23:24:57 +01:00
|
|
|
case RTEMS_SUCCESSFUL:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::OK;
|
2021-03-04 23:24:57 +01:00
|
|
|
case RTEMS_NOT_DEFINED:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2021-03-04 23:24:57 +01:00
|
|
|
default:
|
2022-08-15 20:28:16 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-02-02 10:29:30 +01:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
2023-01-26 11:33:40 +01:00
|
|
|
timeval Clock::getUptime() {
|
2022-02-02 10:29:30 +01:00
|
|
|
// According to docs.rtems.org for rtems 5 this method is more accurate than
|
|
|
|
// rtems_clock_get_ticks_since_boot
|
2023-01-26 11:33:40 +01:00
|
|
|
timeval time_timeval;
|
2022-02-02 10:29:30 +01:00
|
|
|
timespec time;
|
|
|
|
rtems_status_code status = rtems_clock_get_uptime(&time);
|
2023-01-26 11:33:40 +01:00
|
|
|
time_timeval.tv_sec = time.tv_sec;
|
|
|
|
time_timeval.tv_usec = time.tv_nsec / 1000;
|
|
|
|
return time_timeval;
|
|
|
|
}
|