-
Notifications
You must be signed in to change notification settings - Fork 242
Add mutex-traits
-based implementations of SpiDevice
and I2c
#683
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
Open
JalonWong
wants to merge
1
commit into
rust-embedded:master
Choose a base branch
from
JalonWong:mutex_trait
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use embedded_hal::i2c::{ErrorType, I2c}; | ||
use mutex::{BlockingMutex, RawMutex}; | ||
|
||
type Mutex<R, T> = BlockingMutex<R, T>; | ||
|
||
/// `mutex-traits`-based shared bus [`I2c`] implementation. | ||
/// | ||
/// Whether a single bus can be used across multiple threads depends on which | ||
/// implementation of `RawMutex` is used. | ||
pub struct MutexTraitsDevice<'a, R, T> { | ||
bus: &'a Mutex<R, T>, | ||
} | ||
|
||
impl<'a, R: RawMutex, T> MutexTraitsDevice<'a, R, T> { | ||
/// Create a new `MutexTraitsDevice`. | ||
#[inline] | ||
pub fn new(bus: &'a Mutex<R, T>) -> Self { | ||
Self { bus } | ||
} | ||
} | ||
|
||
impl<R, T> ErrorType for MutexTraitsDevice<'_, R, T> | ||
where | ||
T: I2c, | ||
{ | ||
type Error = T::Error; | ||
} | ||
|
||
impl<R: RawMutex, T> I2c for MutexTraitsDevice<'_, R, T> | ||
where | ||
T: I2c, | ||
{ | ||
#[inline] | ||
fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { | ||
let bus = &mut *self.bus.lock(); | ||
bus.read(address, read) | ||
} | ||
|
||
#[inline] | ||
fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { | ||
let bus = &mut *self.bus.lock(); | ||
bus.write(address, write) | ||
} | ||
|
||
#[inline] | ||
fn write_read( | ||
&mut self, | ||
address: u8, | ||
write: &[u8], | ||
read: &mut [u8], | ||
) -> Result<(), Self::Error> { | ||
let bus = &mut *self.bus.lock(); | ||
bus.write_read(address, write, read) | ||
} | ||
|
||
#[inline] | ||
fn transaction( | ||
&mut self, | ||
address: u8, | ||
operations: &mut [embedded_hal::i2c::Operation<'_>], | ||
) -> Result<(), Self::Error> { | ||
let bus = &mut *self.bus.lock(); | ||
bus.transaction(address, operations) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use embedded_hal::delay::DelayNs; | ||
use embedded_hal::digital::OutputPin; | ||
use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice}; | ||
use mutex::{BlockingMutex, RawMutex}; | ||
|
||
use super::DeviceError; | ||
use crate::spi::shared::transaction; | ||
|
||
type Mutex<R, T> = BlockingMutex<R, T>; | ||
|
||
/// `mutex-traits`-based shared bus [`SpiDevice`] implementation. | ||
/// | ||
/// This allows for sharing an [`SpiBus`], obtaining multiple [`SpiDevice`] instances, | ||
/// each with its own `CS` pin. | ||
/// | ||
/// Whether a single bus can be used across multiple threads depends on which | ||
/// implementation of `RawMutex` is used. | ||
pub struct MutexTraitsDevice<'a, R, BUS, CS, D> { | ||
bus: &'a Mutex<R, BUS>, | ||
cs: CS, | ||
delay: D, | ||
} | ||
|
||
impl<'a, R: RawMutex, BUS, CS, D> MutexTraitsDevice<'a, R, BUS, CS, D> { | ||
/// Create a new [`MutexTraitsDevice`]. | ||
/// | ||
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended | ||
/// to set the pin high the moment it's configured as an output, to avoid glitches. | ||
#[inline] | ||
pub fn new(bus: &'a Mutex<R, BUS>, mut cs: CS, delay: D) -> Result<Self, CS::Error> | ||
where | ||
CS: OutputPin, | ||
{ | ||
cs.set_high()?; | ||
Ok(Self { bus, cs, delay }) | ||
} | ||
} | ||
|
||
impl<'a, R: RawMutex, BUS, CS> MutexTraitsDevice<'a, R, BUS, CS, super::NoDelay> { | ||
/// Create a new [`MutexTraitsDevice`] without support for in-transaction delays. | ||
/// | ||
/// This sets the `cs` pin high, and returns an error if that fails. It is recommended | ||
/// to set the pin high the moment it's configured as an output, to avoid glitches. | ||
/// | ||
/// **Warning**: The returned instance *technically* doesn't comply with the `SpiDevice` | ||
/// contract, which mandates delay support. It is relatively rare for drivers to use | ||
/// in-transaction delays, so you might still want to use this method because it's more practical. | ||
/// | ||
/// Note that a future version of the driver might start using delays, causing your | ||
/// code to panic. This wouldn't be considered a breaking change from the driver side, because | ||
/// drivers are allowed to assume `SpiDevice` implementations comply with the contract. | ||
/// If you feel this risk outweighs the convenience of having `cargo` automatically upgrade | ||
/// the driver crate, you might want to pin the driver's version. | ||
/// | ||
/// # Panics | ||
/// | ||
/// The returned device will panic if you try to execute a transaction | ||
/// that contains any operations of type [`Operation::DelayNs`]. | ||
#[inline] | ||
pub fn new_no_delay(bus: &'a Mutex<R, BUS>, mut cs: CS) -> Result<Self, CS::Error> | ||
where | ||
CS: OutputPin, | ||
{ | ||
cs.set_high()?; | ||
Ok(Self { | ||
bus, | ||
cs, | ||
delay: super::NoDelay, | ||
}) | ||
} | ||
} | ||
|
||
impl<R, BUS, CS, D> ErrorType for MutexTraitsDevice<'_, R, BUS, CS, D> | ||
where | ||
R: RawMutex, | ||
BUS: ErrorType, | ||
CS: OutputPin, | ||
{ | ||
type Error = DeviceError<BUS::Error, CS::Error>; | ||
} | ||
|
||
impl<Word: Copy + 'static, R, BUS, CS, D> SpiDevice<Word> for MutexTraitsDevice<'_, R, BUS, CS, D> | ||
where | ||
R: RawMutex, | ||
BUS: SpiBus<Word>, | ||
CS: OutputPin, | ||
D: DelayNs, | ||
{ | ||
#[inline] | ||
fn transaction(&mut self, operations: &mut [Operation<'_, Word>]) -> Result<(), Self::Error> { | ||
let bus = &mut *self.bus.lock(); | ||
|
||
transaction(operations, bus, &mut self.delay, &mut self.cs) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm a bit confused:
The
mutex
crate itself contains the following explanation of the relationship betweenmutex
andmutex-traits
:As
embedded-hal-bus
is a library, shouldn't it depend onmutex-traits
?But then, I don't get how code like
MutexTraitsDevice
implementations should be written without using themutex
crate. So this is more likely a documentation issue inmutex
andmutex-traits
(or just me being particularly slow today) than an actual issue with this pull request.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.
The
mutex-traits
is used in the library that wants to provide a mutex implementation. For example a RTOS library.embedded-hal-bus
should usemutex
crate.