adafruit / adafruit/Adafruit_CircuitPython_MiniMQTT

get_monotonic_time doesn't preserve resolution

Open
#209 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
81
Forks
52
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the get_monotonic_time entry point and inspect how monotonic_ns values are converted and compared for timeout checks. Reproduce the low-timeout behavior on the RP2040/W5500_EVB_PICO context if available; done means elapsed-time calculations retain nanosecond timing precision and no longer produce false timeout errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.