Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtl872x] fix millis() may jump after ~37 hours. #2750

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions hal/src/rtl872x/timer_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ constexpr int TIMER_PRESCALAR = 39; // Input clock = 40M
constexpr int TIMER_COUNTER_US_THRESHOLD = 1; // 1us = 1tick
constexpr uint64_t US_PER_OVERFLOW = 65536; // 65536us
constexpr int TIMER_IRQ_PRIORITY = 0; // Default priority in the close-source timer driver per FAE
constexpr uint64_t SYS_TIMER_US_PER_OVERFLOW = 134217727968; // (0xffffffff * TIMER_TICK_US_X4) / 4
constexpr IRQn_Type TIMER_IRQ[] = {
TIMER0_IRQ,
TIMER1_IRQ,
Expand All @@ -77,10 +78,6 @@ constexpr IRQn_Type TIMER_IRQ[] = {
TIMER5_IRQ
};

inline uint64_t calibrationTickToTimeUs(uint64_t tick) {
return tick * 1000000 / 32768;
}

#if MODULE_FUNCTION == MOD_FUNC_BOOTLOADER
#define HAL_TIMER_USE_SYSTIMER_ONLY
#endif // MODULE_FUNCTION == MOD_FUNC_BOOTLOADER
Expand Down Expand Up @@ -257,18 +254,20 @@ class RtlTimer {

TimerState getOverflowAdjustment() {
TimerState state = {};
uint64_t timeElapsedSysTimer = 0;
{
AtomicSection lk;
state.curSysTimerUs = sysTimerUs();
if (state.curSysTimerUs < lastOverflowSysTimer_) {
// Wraparound
state.curSysTimerUs += 0xffffffff;
timeElapsedSysTimer = state.curSysTimerUs + SYS_TIMER_US_PER_OVERFLOW - lastOverflowSysTimer_;
} else {
timeElapsedSysTimer = state.curSysTimerUs - lastOverflowSysTimer_;
}
state.curTimerTicks = getCounter();
state.overflowCounter = overflowCounter_ + 1;
}
uint64_t curTimerUs = rtcCounterAndTicksToUs(state.overflowCounter, state.curTimerTicks);
uint64_t timeElapsedSysTimer = state.curSysTimerUs - lastOverflowSysTimer_;
uint64_t timeElapsedTimer = curTimerUs - lastOverflowTimer_;
if (timeElapsedSysTimer > timeElapsedTimer) {
uint32_t diff = timeElapsedSysTimer - timeElapsedTimer;
Expand Down