Skip to content

Commit

Permalink
Fix RTC implementation, fails HostTests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Nov 30, 2024
1 parent 76ead86 commit c814dff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
4 changes: 0 additions & 4 deletions Sming/Arch/Rp2040/Components/rp2040/src/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 36 additions & 18 deletions Sming/Arch/Rp2040/Platform/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,62 @@
****/

#include <Platform/RTC.h>
#include <pico/aon_timer.h>
#include <sys/time.h>
#include <pico/time.h>

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;
}

0 comments on commit c814dff

Please sign in to comment.