Skip to content

Add sub-ms resolution for time.monotonic_ns, on samd and nrf ports #2342

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

Closed
wants to merge 2 commits into from

Conversation

jepler
Copy link

@jepler jepler commented Nov 30, 2019

This also addresses a problem where current_tick() / wait_until() could be wrong by 1ms on nRF, by copying systick code from the samd port.

Testing performed: on pyportal and cpb, looked at time.monotonic_ns values. The values always went forward, and usually by "consistent" amounts in a small loop:

# Sub-millisecond times on pyportal with patch
>>> l = [time.monotonic_ns() for i in range(99)]
>>> all(l[i] < l[i+1] for i in range(len(l)-1))
True
>>> [l[i] - l[i-1] for i in range(1,len(l))]
[28850, 27825, 27650, 27808, ..., 28183, 28134]

# on nRF (slower CPU speed)
[43172, 42938, 42937, 42875, ..., 42875, 42937]

On samd builds, this new code isn't enabled for small builds to avoid increasing code size. So those boards will still have just 1ms resolution in monotonic_ns. Also, the resolution of time.monotonic isn't increased, since very soon after boot even 1ms resolution is not possible in CP floating-point values.

Assuming the samd code is right, this fixes a rare problem where a timeout
would be ~1ms too short; the problem could also be observed with the
monotonic_ns code which could incorrectly jump forward 1ms.
.. which, on some ports, can return sub-millisecond precision thanks to
how SysTick works.
@jepler
Copy link
Author

jepler commented Nov 30, 2019

I think this is ready for review.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this!

Comment on lines +219 to 220
uint64_t time64 = common_hal_time_monotonic_ns();
return mp_obj_new_int_from_ll((long long) time64);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is STM32 different than the others? Maybe it should also share the same code?

@jepler
Copy link
Author

jepler commented Dec 10, 2019

I started doing a more extensive re-factor but it kinda got out of control. I feel like background and tick could both have a lot more commonality than they have, especially on systems which directly use ARM SysTick. I'd be content closing this unmerged now and returning to it in 6.0 -- nobody's assigned this a 5-ish milestone. Thoughts? @tannewt @dhalbert

@tannewt
Copy link
Member

tannewt commented Dec 10, 2019

I'm impartial. Either way is fine with me.

@dhalbert
Copy link
Collaborator

We want to redo timekeeping anyway to be able to sleep, so ok by me to wait.

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

Successfully merging this pull request may close these issues.

3 participants