Merge pull request #2893 from Ragarnoy/u16-spidevice
Make SpiDevice generic over Word size and implement u16 transfer
This commit is contained in:
commit
34a0f74456
2 changed files with 16 additions and 53 deletions
|
@ -55,13 +55,14 @@ where
|
||||||
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M, BUS, CS> spi::SpiDevice for SpiDevice<'_, M, BUS, CS>
|
impl<M, BUS, CS, Word> spi::SpiDevice<Word> for SpiDevice<'_, M, BUS, CS>
|
||||||
where
|
where
|
||||||
M: RawMutex,
|
M: RawMutex,
|
||||||
BUS: spi::SpiBus,
|
BUS: spi::SpiBus<Word>,
|
||||||
CS: OutputPin,
|
CS: OutputPin,
|
||||||
|
Word: Copy + 'static,
|
||||||
{
|
{
|
||||||
async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> {
|
async fn transaction(&mut self, operations: &mut [spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
|
||||||
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
||||||
return Err(SpiDeviceError::DelayNotSupported);
|
return Err(SpiDeviceError::DelayNotSupported);
|
||||||
}
|
}
|
||||||
|
@ -138,13 +139,14 @@ where
|
||||||
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M, BUS, CS> spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS>
|
impl<M, BUS, CS, Word> spi::SpiDevice<Word> for SpiDeviceWithConfig<'_, M, BUS, CS>
|
||||||
where
|
where
|
||||||
M: RawMutex,
|
M: RawMutex,
|
||||||
BUS: spi::SpiBus + SetConfig,
|
BUS: spi::SpiBus<Word> + SetConfig,
|
||||||
CS: OutputPin,
|
CS: OutputPin,
|
||||||
|
Word: Copy + 'static,
|
||||||
{
|
{
|
||||||
async fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> {
|
async fn transaction(&mut self, operations: &mut [spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
|
||||||
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
||||||
return Err(SpiDeviceError::DelayNotSupported);
|
return Err(SpiDeviceError::DelayNotSupported);
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,13 +48,14 @@ where
|
||||||
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDevice<'_, M, BUS, CS>
|
impl<BUS, M, CS, Word> embedded_hal_1::spi::SpiDevice<Word> for SpiDevice<'_, M, BUS, CS>
|
||||||
where
|
where
|
||||||
M: RawMutex,
|
M: RawMutex,
|
||||||
BUS: SpiBus,
|
BUS: SpiBus<Word>,
|
||||||
CS: OutputPin,
|
CS: OutputPin,
|
||||||
|
Word: Copy + 'static,
|
||||||
{
|
{
|
||||||
fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> {
|
fn transaction(&mut self, operations: &mut [embedded_hal_1::spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
|
||||||
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
||||||
return Err(SpiDeviceError::DelayNotSupported);
|
return Err(SpiDeviceError::DelayNotSupported);
|
||||||
}
|
}
|
||||||
|
@ -90,47 +91,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer<u8> for SpiDevice<'_, M, BUS, CS>
|
|
||||||
where
|
|
||||||
M: RawMutex,
|
|
||||||
BUS: embedded_hal_02::blocking::spi::Transfer<u8, Error = BusErr>,
|
|
||||||
CS: OutputPin<Error = CsErr>,
|
|
||||||
{
|
|
||||||
type Error = SpiDeviceError<BusErr, CsErr>;
|
|
||||||
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
|
||||||
self.bus.lock(|bus| {
|
|
||||||
let mut bus = bus.borrow_mut();
|
|
||||||
self.cs.set_low().map_err(SpiDeviceError::Cs)?;
|
|
||||||
let op_res = bus.transfer(words);
|
|
||||||
let cs_res = self.cs.set_high();
|
|
||||||
let op_res = op_res.map_err(SpiDeviceError::Spi)?;
|
|
||||||
cs_res.map_err(SpiDeviceError::Cs)?;
|
|
||||||
Ok(op_res)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write<u8> for SpiDevice<'_, M, BUS, CS>
|
|
||||||
where
|
|
||||||
M: RawMutex,
|
|
||||||
BUS: embedded_hal_02::blocking::spi::Write<u8, Error = BusErr>,
|
|
||||||
CS: OutputPin<Error = CsErr>,
|
|
||||||
{
|
|
||||||
type Error = SpiDeviceError<BusErr, CsErr>;
|
|
||||||
|
|
||||||
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
|
|
||||||
self.bus.lock(|bus| {
|
|
||||||
let mut bus = bus.borrow_mut();
|
|
||||||
self.cs.set_low().map_err(SpiDeviceError::Cs)?;
|
|
||||||
let op_res = bus.write(words);
|
|
||||||
let cs_res = self.cs.set_high();
|
|
||||||
let op_res = op_res.map_err(SpiDeviceError::Spi)?;
|
|
||||||
cs_res.map_err(SpiDeviceError::Cs)?;
|
|
||||||
Ok(op_res)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// SPI device on a shared bus, with its own configuration.
|
/// SPI device on a shared bus, with its own configuration.
|
||||||
///
|
///
|
||||||
/// This is like [`SpiDevice`], with an additional bus configuration that's applied
|
/// This is like [`SpiDevice`], with an additional bus configuration that's applied
|
||||||
|
@ -163,13 +123,14 @@ where
|
||||||
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
type Error = SpiDeviceError<BUS::Error, CS::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<BUS, M, CS> embedded_hal_1::spi::SpiDevice for SpiDeviceWithConfig<'_, M, BUS, CS>
|
impl<BUS, M, CS, Word> embedded_hal_1::spi::SpiDevice<Word> for SpiDeviceWithConfig<'_, M, BUS, CS>
|
||||||
where
|
where
|
||||||
M: RawMutex,
|
M: RawMutex,
|
||||||
BUS: SpiBus + SetConfig,
|
BUS: SpiBus<Word> + SetConfig,
|
||||||
CS: OutputPin,
|
CS: OutputPin,
|
||||||
|
Word: Copy + 'static,
|
||||||
{
|
{
|
||||||
fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> {
|
fn transaction(&mut self, operations: &mut [embedded_hal_1::spi::Operation<'_, Word>]) -> Result<(), Self::Error> {
|
||||||
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
if cfg!(not(feature = "time")) && operations.iter().any(|op| matches!(op, Operation::DelayNs(_))) {
|
||||||
return Err(SpiDeviceError::DelayNotSupported);
|
return Err(SpiDeviceError::DelayNotSupported);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue