AP_HAL_Linux: tidy set/get of hw RTC

This commit is contained in:
Peter Barker 2021-11-23 13:40:39 +11:00 committed by Peter Barker
parent f50d48f005
commit d8e4669e07
2 changed files with 18 additions and 5 deletions

View File

@ -62,12 +62,23 @@ void Util::commandline_arguments(uint8_t &argc, char * const *&argv)
argv = saved_argv;
}
uint64_t Util::get_hw_rtc() const
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
const uint64_t seconds = ts.tv_sec;
const uint64_t nanoseconds = ts.tv_nsec;
return (seconds * 1000000ULL + nanoseconds/1000ULL);
}
void Util::set_hw_rtc(uint64_t time_utc_usec)
{
// don't reset the HW clock time on people's laptops.
#if CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE
// call superclass method to set time. We've guarded this so we
// don't reset the HW clock time on people's laptops.
AP_HAL::Util::set_hw_rtc(time_utc_usec);
timespec ts;
ts.tv_sec = time_utc_usec/1000000ULL;
ts.tv_nsec = (time_utc_usec % 1000000ULL) * 1000ULL;
clock_settime(CLOCK_REALTIME, &ts);
#endif
}
@ -342,4 +353,4 @@ bool Util::parse_cpu_set(const char *str, cpu_set_t *cpu_set) const
} while (*endptr != '\0');
return true;
}
}

View File

@ -37,9 +37,11 @@ public:
void commandline_arguments(uint8_t &argc, char * const *&argv) override;
/*
set system clock in UTC microseconds
get/set system clock in UTC microseconds
*/
void set_hw_rtc(uint64_t time_utc_usec) override;
uint64_t get_hw_rtc() const override;
const char *get_custom_log_directory() const override final { return custom_log_directory; }
const char *get_custom_terrain_directory() const override final { return custom_terrain_directory; }
const char *get_custom_storage_directory() const override final { return custom_storage_directory; }