-
Notifications
You must be signed in to change notification settings - Fork 186
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
Guide for driver authors and embedded-hal
users
#246
Comments
Hopefully this is a good place to ask a question that I hope the docs could cover; how should hal implementations, device drivers and user code deal with concurrency? If the answer is "in the user code" (which seems reasonable), this would be nice to hint on in the docs how one can plug the limited resources given by a HAL implementation into the ownership expectations of drivers. Take this code that I have written to use an I2C LCD driver: let i2c = hal::i2c_master(
&mut clocks,
270.khz(),
peripherals.SERCOM3,
&mut peripherals.PM,
pins.sda,
pins.scl,
&mut pins.port,
);
let delay = Delay::new(core.SYST, &mut clocks);
let lcd = LCDImpl::new(LCDDriver::new_i2c(i2c, LCD_I2C_ADDRESS, delay)) The LCD driver will now take ownership of both the Delay and I2C resources provided by the HAL implementation. This problem has been discussed before for busses in rust-embedded/embedded-hal#35. However, it seems like |
yeah concurrency is a wee bit tricky, unfortunately i haven't dug into RTIC to comment on that part. the best approach for sharing peripherals is Rahix/shared-bus, though there are some outstanding issues with transaction ordering and locking that can come into play with SPI but i do not think these are an issue for I2C. i don't know about the HAL you're using but afaik |
@ryankurte Thanks for confirming
I'm using the atsamd HAL. Here is the Delay implementation. I looked at some of the HALs in the stm32-rs organization, and they seem to be similar (here's the stm32f3xx). They all appear to be using the SysTick peripheral, which itself requires access to a number of registers. |
Ahh, I see you point! IMO this seems like a slight oversight on the In embedded C it's pretty normal to have a |
As long as the counter is not reconfigurable after initialisation, Copy/Clone should be reasonable. The only guarantee |
The reason that I ask about all of this is that I noticed recently an LCD driver implementation that uses It seems like in this specific case, the HAL implementations could provide at least one instance of something that implements Would it be true to say that device driver implementations should assume that there will be an implementation of the HAL trait that can be owned when making decisions as to their API? Or is that more a trait-specific thing? Would it be true to say that HAL implementors should think about how they can provide multiple instances/references of a trait, as has been thought about in the above These are the types of questions I was hoping could be answered in this section of docs (fully realizing that perhaps my question is too general, or the answers too implementation specific). |
This is a good point and also, somehow viscerally upsetting 🙃
Yep, basically all drivers make this assumption and to not do so, as you note, creates a nightmarish API.
This one is much more dependent on the trait. Where it's possible to have multiple implementations (ie. I believe we will look to adopt |
It would be good to have a guide for driver authors and other users of embedded hal providing best practices for defining and consuming drivers, and detailing the use of generics in both (as this can be a complex gotcha for new rust users).
Related to rust-embedded/embedded-hal#135, rust-embedded/wg#411
The text was updated successfully, but these errors were encountered: