91 lines
2.5 KiB
C++
91 lines
2.5 KiB
C++
#include "fsfw/timemanager/Clock.h"
|
|
|
|
#include <rtems/rtems/clockimpl.h>
|
|
#include <rtems/score/todimpl.h>
|
|
|
|
#include "fsfw/ipc/MutexGuard.h"
|
|
#include "fsfw/osal/rtems/RtemsBasic.h"
|
|
|
|
uint32_t Clock::getTicksPerSecond(void) {
|
|
rtems_interval ticks_per_second = rtems_clock_get_ticks_per_second();
|
|
return static_cast<uint32_t>(ticks_per_second);
|
|
}
|
|
|
|
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
|
|
rtems_time_of_day timeRtems;
|
|
timeRtems.year = time->year;
|
|
timeRtems.month = time->month;
|
|
timeRtems.day = time->day;
|
|
timeRtems.hour = time->hour;
|
|
timeRtems.minute = time->minute;
|
|
timeRtems.second = time->second;
|
|
timeRtems.ticks = time->usecond * getTicksPerSecond() / 1e6;
|
|
rtems_status_code status = rtems_clock_set(&timeRtems);
|
|
switch (status) {
|
|
case RTEMS_SUCCESSFUL:
|
|
return returnvalue::OK;
|
|
case RTEMS_INVALID_ADDRESS:
|
|
return returnvalue::FAILED;
|
|
case RTEMS_INVALID_CLOCK:
|
|
return returnvalue::FAILED;
|
|
default:
|
|
return returnvalue::FAILED;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t Clock::setClock(const timeval* time) {
|
|
TimeOfDay_t time_tod;
|
|
ReturnValue_t result = convertTimevalToTimeOfDay(time, &time_tod);
|
|
if (result != returnvalue::OK) {
|
|
return result;
|
|
}
|
|
return setClock(&time_tod);
|
|
}
|
|
|
|
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
|
// Callable from ISR
|
|
rtems_status_code status = rtems_clock_get_tod_timeval(time);
|
|
switch (status) {
|
|
case RTEMS_SUCCESSFUL:
|
|
return returnvalue::OK;
|
|
case RTEMS_NOT_DEFINED:
|
|
return returnvalue::FAILED;
|
|
default:
|
|
return returnvalue::FAILED;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t Clock::getUptime(timeval* uptime) {
|
|
// According to docs.rtems.org for rtems 5 this method is more accurate than
|
|
// rtems_clock_get_ticks_since_boot
|
|
timespec time;
|
|
rtems_status_code status = rtems_clock_get_uptime(&time);
|
|
uptime->tv_sec = time.tv_sec;
|
|
uptime->tv_usec = time.tv_nsec / 1000;
|
|
switch (status) {
|
|
case RTEMS_SUCCESSFUL:
|
|
return returnvalue::OK;
|
|
default:
|
|
return returnvalue::FAILED;
|
|
}
|
|
}
|
|
|
|
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {
|
|
// 32bit counter overflows after 50 days
|
|
uint64_t uptime = rtems_clock_get_uptime_nanoseconds() / 1e6;
|
|
*uptimeMs = uptime & 0xffffffff;
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
|
|
timeval temp_time;
|
|
rtems_status_code returnValue = rtems_clock_get_tod_timeval(&temp_time);
|
|
*time = ((uint64_t)temp_time.tv_sec * 1000000) + temp_time.tv_usec;
|
|
switch (returnValue) {
|
|
case RTEMS_SUCCESSFUL:
|
|
return returnvalue::OK;
|
|
default:
|
|
return returnvalue::FAILED;
|
|
}
|
|
}
|