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

get_monotonic_time doesn't preserve resolution #209

Open
kevin-tritz opened this issue Mar 21, 2024 · 2 comments
Open

get_monotonic_time doesn't preserve resolution #209

kevin-tritz opened this issue Mar 21, 2024 · 2 comments

Comments

@kevin-tritz
Copy link
Contributor

kevin-tritz commented Mar 21, 2024

The library tries to use monotonic_ns when available to preserve timing resolution. However, monotonic_ns only preserves resolution when kept as an int. When used in a floating operation, such as dividing by 1000000000 in get_monotonic_time, it experiences the same loss of precision as the original monotonic() function.

This results in timeout errors due to loss of precision when timeouts are low and uptime is long. I've seen false timeout errors for 10-20ms timeouts after a few hours, and false timeout errors for 0.1s timeouts after a few days.

I hacked in a fix using ticks_diff and ticks_ms from adafruit_ticks, and presumably you could also make the fix by taking the difference of nanosecond integers, and dividing that result.

so this doesn't work:
time2_ns/1000000000 - time1_ns/1000000000 > timeout_s

but this should:
(time2_ns - time1_ns)/1000000000 > timeout_s

I should note this is for the RP2040 chip in the W5500_EVB_PICO board.

@dhalbert
Copy link
Contributor

Do you want to submit a PR for this? Such expressions could be rewritten as you indicated, or we could use //.

@kevin-tritz
Copy link
Contributor Author

kevin-tritz commented Jan 19, 2025

I submitted a PR using adafruit_ticks that should have solved the issue.

#210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants