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

Add Duration and Instant support to Rust. #1

Merged
merged 13 commits into from
Sep 17, 2024
Merged

Add Duration and Instant support to Rust. #1

merged 13 commits into from
Sep 17, 2024

Conversation

d3zd3z
Copy link
Collaborator

@d3zd3z d3zd3z commented Sep 11, 2024

This is a migration of the open PR zephyrproject-rtos/zephyr#78193. To the module version. The only changes from that PR are the squashing a few of the fixup commits.

@d3zd3z
Copy link
Collaborator Author

d3zd3z commented Sep 12, 2024

zephyrproject-rtos/zephyr#78306 incorporates this PR into Zephyr, notably the CI results can be seen there.

The fugit crate implements tick-based timers, providing a `Duration` and
`Instant` type that we can use for timeouts.

This is an external crate, covered under the Apache-2.0 or MIT license.
This will need to be vendored due to Rust policies.

Signed-off-by: David Brown <david.brown@linaro.org>
The `zephyr-sys` crate contains all of the generated bindings to the C
API in Zephyr.

The `zephyr::sys` module wraps these abstractions in as thin of a way as
possible, but still allowing them to be used without 'unsafe'.  Although
the interfaces are "safe", because they are just wrappers around C
calls, most aren't directly useful without unsafe, and this crate
provides higher-level abstractions around these underlying primitives.

This commit adds a definition for `K_FOREVER`, and `K_NO_WAIT`, which
are too complicated for bindgen to autogenerate bindings for.  We will
rely on a test to ensure that the values match those in the C headers.

Signed-off-by: David Brown <david.brown@linaro.org>
Use specific instances of `fugit::Duration` and `fugit::Instant`, with
the clock parameter based on the configured clock rate.  As the fugit
times are fixed, this precludes using these time types with dynamically
changing clocks.

With this, is a local Timeout type that simply wraps `k_timeout_t`.
Having this wrapper allows us to implement `From` from each of the
timeout types.  The zephyr timeout type can support either durations or
instants, as well as two magic times, forever and no wait.  Forever and
NoWait are implemented as singleton types that also have conversions.

The `zephyr::time::sleep` function then can take anything that can be
converted to this wrapped Timeout type, including Forever, and NoWait.
This template will be used for future API calls that need a timeout.

Signed-off-by: David Brown <david.brown@linaro.org>
Rewrite the doc comment for `zephyr::time`.  A little bit of LLM help
and this is much clearer.

Signed-off-by: David Brown <david.brown@linaro.org>
Add a test to test that the time values in Rust (Duration and Instance)
and up calculating the same results as the various macros used in the C
code.

Signed-off-by: David Brown <david.brown@linaro.org>
If we have printk enabled, use that to output the desperation panic
message when panic is called from Rust code.

Signed-off-by: David Brown <david.brown@linaro.org>
Too much rust code formatting leaks into my C.  Fix these various
whitespace errors.

Signed-off-by: David Brown <david.brown@linaro.org>
When the panic happens, call into the Zephyr `k_panic()` handler instead
of just spinning.  This should halt the whole system, not just the
current thread.

Signed-off-by: David Brown <david.brown@linaro.org>
Add some additional comments to clarify that not all of the fields are
used for all of the test types.

Signed-off-by: David Brown <david.brown@linaro.org>
When debug assertions are enabled, ensure that the time conversions are
sensible.  If there is an overflow on time with the assertions disabled,
just use Default, which will be zero.

Signed-off-by: David Brown <david.brown@linaro.org>
Comment cleanups.  Fix several minor typos in the comments.

Signed-off-by: David Brown <david.brown@linaro.org>
Indicate that we have test in this module.  CI will run these
automatically when the module changes.

Signed-off-by: David Brown <david.brown@linaro.org>
Now that we've added a test, test these as well.

Signed-off-by: David Brown <david.brown@linaro.org>
@d3zd3z
Copy link
Collaborator Author

d3zd3z commented Sep 17, 2024

This is the same code as zephyrproject-rtos/zephyr#78193, which as an approval from @sigvarthmh, and there is the one from ithinuel so I'll consider this as having two approvals.

@d3zd3z d3zd3z merged commit d2734f4 into main Sep 17, 2024
2 checks passed
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.

2 participants