-
Notifications
You must be signed in to change notification settings - Fork 211
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 a Rc<RefCell<Bus>>-based implementation of SpiDevice and I2C #613
Conversation
LGTM! can you add it to I2C too for consistency? |
I can, yeah; do you want it in a separate PR? |
either separate or this one works, up to you. |
Might as well then :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Looks good to me.
Could you add some documentation about the alloc feature to the readme and also add an entry to the changelog?
Co-authored-by: Diego Barrios Romero <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
The current implementation of
RefCellDevice
for sharing an SPI bus usingRefCell
works fine, but restricts users of the library into patterns where the compiler is able to guarantee that the bus reference will outlive all usage of its devices.In my current work, this isn't really the case, as I would like to hide the
SpiBus
/OutputPin
details behind some structures a shared trait, so that my code can interface with foreign microcontrollers as it would interface with its local peripherals.This means that I either need to manually manage the memory used for the
SpiBus
, or I need to use something likeRc
to safely manage it for me.Right now, I can create an ad-hoc
ExclusiveDevice
with the reference from myRc<RefCell<SpiBus>>
whenever I need to do a transaction, and drop it immediately after, but this is a bit of a hacky solution.This PR proposes a more ellegant approach, which simply offers an alternative to
RefCellDevice
that uses reference-counting for its SPI bus.I hesitated on adding a utility method for acquiring a copy of the
Rc<RefCell<SpiBus>>
, but I decided against it, since this is a rather niche need and one could just keep aWeak<RefCell<SpiBus>>
around and not occur any penalty.I'm unsure if I did things well regarding adding a new feature,
alloc
, and making sure that it renders correctly on docs.rsEdit: also added an implementation for I2C, for consistency. I noticed that the I2C implementations could benefit from
Clone
, but that's out of the scope of this PR.