diff --git a/Sming/Arch/Rp2040/Components/rp2040/src/startup.cpp b/Sming/Arch/Rp2040/Components/rp2040/src/startup.cpp index 6505542e7f..77d50b6e13 100644 --- a/Sming/Arch/Rp2040/Components/rp2040/src/startup.cpp +++ b/Sming/Arch/Rp2040/Components/rp2040/src/startup.cpp @@ -94,15 +94,11 @@ void check_bootsel() } // namespace -extern void system_init_rtc(); - extern "C" int main(void) { extern void system_init_clocks(); system_init_clocks(); - system_init_rtc(); - system_soft_wdt_restart(); // Initialise hardware timers diff --git a/Sming/Arch/Rp2040/Platform/RTC.cpp b/Sming/Arch/Rp2040/Platform/RTC.cpp index a156498172..07c14c7ed8 100644 --- a/Sming/Arch/Rp2040/Platform/RTC.cpp +++ b/Sming/Arch/Rp2040/Platform/RTC.cpp @@ -9,44 +9,62 @@ ****/ #include -#include +#include +#include RtcClass RTC; -#define NS_PER_SECOND 1000000000 +#define NS_PER_SECOND 1'000'000'000 +#define US_PER_SECOND 1'000'000 -void system_init_rtc() +RtcClass::RtcClass() = default; + +namespace { - struct timespec ts { - }; - aon_timer_start(&ts); +int64_t epoch_sys_time_us; } -RtcClass::RtcClass() = default; +extern "C" int _gettimeofday(struct timeval* tv, void*) +{ + if(tv) { + auto us_since_epoch = epoch_sys_time_us + time_us_64(); + *tv = { + .tv_sec = time_t(us_since_epoch / US_PER_SECOND), + .tv_usec = suseconds_t(us_since_epoch % US_PER_SECOND), + }; + } + return 0; +} + +extern "C" int settimeofday(const struct timeval* tv, const struct timezone*) +{ + if(tv) { + auto us_since_epoch = tv->tv_sec * US_PER_SECOND + tv->tv_usec; + epoch_sys_time_us = us_since_epoch - time_us_64(); + } + return 0; +} uint64_t RtcClass::getRtcNanoseconds() { - struct timespec ts; - return aon_timer_get_time(&ts) ? timespec_to_us(&ts) * 1000ULL : 0; + return uint64_t(epoch_sys_time_us + time_us_64()) * 1000ULL; } uint32_t RtcClass::getRtcSeconds() { - struct timespec ts; - return aon_timer_get_time(&ts) ? ts.tv_sec : 0; + return (epoch_sys_time_us + time_us_64()) / US_PER_SECOND; } bool RtcClass::setRtcNanoseconds(uint64_t nanoseconds) { - struct timespec ts; - us_to_timespec(nanoseconds / 1000ULL, &ts); - return aon_timer_set_time(&ts); + auto us_since_epoch = nanoseconds / 1000; + epoch_sys_time_us = us_since_epoch - get_absolute_time(); + return true; } bool RtcClass::setRtcSeconds(uint32_t seconds) { - struct timespec ts { - .tv_sec = seconds - }; - return aon_timer_set_time(&ts); + auto us_since_epoch = int64_t(seconds) * US_PER_SECOND; + epoch_sys_time_us = us_since_epoch - time_us_64(); + return true; }