-
Notifications
You must be signed in to change notification settings - Fork 201
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
Solution for dealing with clocks and time in embedded systems #211
Comments
I see only duration types. What about frequencies and baudrates? |
@burrbull There is a |
@burrbull Initial frequency-type functionality has been added in v0.5.0 |
Thank you for your work very much. |
Looks excellent. One thing that could be useful is a bridge to |
@therealprof While I don't think that |
@therealprof Added with v0.5.2 |
I'm curious whether it would be appropriate to using |
Looks good to me in general, appreciate the effort. I'll need to find some time to check it out in detail though. |
This looks really great - are there any plans to slowly integrate this into embedded-hal through PRs? I don't think this will see widespread adoption otherwise since most of the downstream embedded crates are already using their own time primitives etc. It would be nice to see your crate added as an embedded-hal dependency and then exposed through the embedded-hal public API at very least. Though I suspect people would be more comfortable just cloning the functionality to embedded-hal to keep the control within the embedded org... |
embedded-time is trying to solve a different problem than embedded-hal so I don't think integration here makes too much sense. What we can do is encourage HAL impls to use embedded-time rather than rolling their own types and this is probably best done by someone doing PRs to do the switcheroo. I happen to be the maintainer of a few HAL impls so if anyone is looking for crates which could serve as the first stepping stone to general adoption feel free to use do PRs against https://github.com/stm32-rs/stm32f0xx-hal or https://github.com/stm32-rs/stm32f4xx-hal and I will help driving that to completion. |
@therealprof Thanks for clearing up the embedded-hal question, I wasn't at all sure whether it would be a good fit there. I will do whatever I can do to support PRs for individual HAL impls including opening them up myself unless I have updates needed in the embedded-time crate itself. |
After some feedback from a HAL implementor, I created the this PR (FluenTech/embedded-time#22) which would change all the "inner" types to unsigned. I would love to get some feedback about this change before I merge it. |
@therealprof I am a little confused, actually, about what you said about embedded-hal. I'm still trying to wrap my head around what the objective of |
The purpose of
I don't see it why it needs to be an integral part of |
Sorry, I wasn't commenting on where the code should be, but rather saying that (whether as a dependency or direct integration), it seems to me that
Would it make sense to split it up? The only hardware-abstraction part of it is the I guess as long as |
I'd wait and see where this goes. I can totally see this being useful for a lot of applications, if it gets picked up by other crates and turns out to be useful I'd have no regrets integrating it more deeply. At the moment I see most of the usefulness in the HAL impls themselves with a hint of use in the timer related stuff in e-h.
We could also do it the other way around and provide all the fun clock, timer and countdown traits via Also it would be great if we could have something like a monotonic system clock, real time clock, alarms, scheduling... |
As things sit right now, the The next release will probably be with @therealprof I'm certain you have spent much more time thinking about where embedded rust is right now, where we want to go, and how to get there, so I would greatly appreciate any guidance. |
I dont think embedded time is trying to solve a different problem at all - Just look at all the issues noted in the original post mentioning the need for some crucial clock traits like periods and frequencies, and to tie them to the std lib. Could the traits and Period struct at least be moved into embedded-hal to ensure comparability? I don't think it's wise to expect the ecosystem to depend on a 3rd party crate to ensure cross-ecosystem compatibility for timings - that should be something that embedded-hal does since it is the "blessed" crate for the normal hardware abstractions one would expect no? |
Hey thanks for working on this! There's lots of useful stuff here, and how / what we integrate is an interesting question. I think i'd echo @therealprof in that for now I would continue this as a separate project, and to try out integrating it with HALs and other components. This gives us space to experiment a bit (as we require for new hal additions) and means we're not tied to the HAL for releases (you may have noticed there's a lot going on at the moment). To me it seems that the possible steps from there are: I think the key component delineation to me is whether this is intended to be
However I can also see the benefits to going in either direction. In terms of moving traits into the hal, it is reasonably straightforward to add a trait and then import and re-export that from the original crate, so we can move traits into the hal later should this be desired, without breaking any external dependencies. |
I just release v0.6.0 of embedded-time. With feedback and advice from @eldruin and @TheZoq2. I made a number of changes. Added
Timers
Changed
|
I like that idea. I think embedded-hal (as well as embedded-time) are both still pretty fluid. It's my intention to continue development in a way that serves my purposes, but also keeps an eye toward maybe being suitable for incorporation at some later date.
There is only one part that must implemented in a hardware-specific manner and that's the Here's a rough snapshot of some of the clock and instant interfaces:
And here is an actual pub struct SysClock {
low: nrf52::pac::TIMER0,
high: nrf52::pac::TIMER1,
capture_task: nrf52::pac::EGU0,
}
impl SysClock {
pub fn take(
low: nrf52::pac::TIMER0,
high: nrf52::pac::TIMER1,
capture_task: nrf52::pac::EGU0,
) -> Self {
Self {
low,
high,
capture_task,
}
}
}
impl time::Clock for SysClock {
type Rep = u64;
const PERIOD: time::Period = <time::Period>::new(1, 16_000_000);
type ImplError = Infallible;
fn now(&self) -> Result<time::Instant<Self>, time::clock::Error<Self::ImplError>> {
self.capture_task.tasks_trigger[0].write(|write| unsafe { write.bits(1) });
let ticks =
self.low.cc[0].read().bits() as u64 | ((self.high.cc[0].read().bits() as u64) << 32);
Ok(time::Instant::new(ticks as Self::Rep))
}
} |
|
@ryankurte, Thanks for taking a look at the crate. There are a lot of changes happening at the moment. With this chip, I can trigger a signal from software that causes both timers to capture atomically ( |
ahh nice, it looks like a similar approach is possible on ST cores but, might require checking for overflow. |
Some new releases of embedded-time 0.9.1 - 2020-08-07Changed
0.9.0 - 2020-08-05Added
Changed
Removed
|
As a developer who is quite new to Rust but has a decent amount of experience with C/C++ and STM32 development, my thoughts on this topic when developing a device driver:
|
I just started with embedded development in rust and I want to say that I am in line with @sourcebox's points above. I am trying to write a simple embedded-hal driver on the ESP32C3. I need to measure how long a GPIO pin stays high. For that I do not need a sophisticated timer: access to a monotonic timer (tick count + frequency) is sufficient. It would be especially interesting on the ESP32C3 where there are only 2 general purpose timers. With access to the system clock I would not need to consume a timer. For what it's worth, I think basic time access functionalities should be intergrated in I see that this discussion started years ago. Is there still life in it? How does it move forward? |
I never wanted to write my own crate for dealing with time, but to have a solution for my own projects until something "official" is released: https://github.com/sourcebox/emtick-rs I don't know if the general concept behind it is good enough as solution within e-h, maybe it could be discussed. |
Hi, @sourcebox , what are the main differences between your implementation and fugit? I saw that yours does not have any dependency, and that is always desirable (I believe). However, it would be great to have a performance comparative. Also, memory footprint should be studied. I agree with you, we just need a decent toolbox for dealing with time and timers in embedded systems. It would be great if e-h adopts/integrates either fugit or your new crate. The main drawback of your solution (I think) is that several HALs already use fugit, and maybe it is worth it to adopt it in e-h as well. I'll take a look to your code later with more time. |
As far as I understand fugit, it tries to eleminate runtime cost completely. This however leads to the fact that you have to deal with types like My solution is a more pragmatic one. It should be easier to use but has some runtime overhead when converting from/to natural time units. So doing this should be kept minimal. The amount of impact on overall performance is hard to decide. In my code, time calculations are typically used rarely compared to the overall coverage. Optimizing this to the max would not have too much effect globally. But this may vary depending on the use case. I would suggest that you do a real world example using |
@PTaylor-us I've been using the The project seems stale: I haven't seen any updates, or activity in the issues/PR space. Would you be happy to see this taken over by someone and/or absorbed into the |
@sourcebox 100%. I would add that the more advanced functionalities can be incrementally added as the community asks for them. Starting with a trait with an API that sources a tick-count and mapping to seconds (I say seconds because it's the standard unit of time, but ideally, this would be generic between seconds, us, ms, etc). The more advanced stuff can be added down the line. |
Quoting @Dirbaio in the chat for a comparison of
|
I've put together an RFC in the wg repo: rust-embedded/wg#762 I believe I've addressed most of the points:
|
I have just released
embedded-time
to handle clocks, instants, durations more easily in embedded contexts. It follows a similar idea to the C++std::chrono
library. I'd love to get some feedback, suggestions, PRs, etc.embedded-time
provides a comprehensive library for implementing abstractions over hardware and work with clocks, instants, durations, periods, and frequencies in a more intuitive way.core::time::Duration
Example Usage:
Related:
#122 #207 #24 #46 #201 #129 #103 #59 #186
The text was updated successfully, but these errors were encountered: