-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
209 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,75 +1,70 @@ | ||
use super::*; | ||
|
||
pub use embedded_hal_02::spi::{Mode, Phase, Polarity}; | ||
use embedded_hal_02::{blocking::spi as blocking, spi}; | ||
|
||
impl<SPI, REMAP, PINS, FrameSize, OP> spi::FullDuplex<FrameSize> | ||
for Spi<SPI, REMAP, PINS, FrameSize, OP> | ||
impl From<Polarity> for super::Polarity { | ||
fn from(p: Polarity) -> Self { | ||
match p { | ||
Polarity::IdleLow => Self::IdleLow, | ||
Polarity::IdleHigh => Self::IdleHigh, | ||
} | ||
} | ||
} | ||
|
||
impl From<Phase> for super::Phase { | ||
fn from(p: Phase) -> Self { | ||
match p { | ||
Phase::CaptureOnFirstTransition => Self::CaptureOnFirstTransition, | ||
Phase::CaptureOnSecondTransition => Self::CaptureOnSecondTransition, | ||
} | ||
} | ||
} | ||
|
||
impl From<Mode> for super::Mode { | ||
fn from(m: Mode) -> Self { | ||
Self { | ||
polarity: m.polarity.into(), | ||
phase: m.phase.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<SPI, REMAP, PINS, W, OP> spi::FullDuplex<W> for Spi<SPI, REMAP, PINS, W, OP> | ||
where | ||
SPI: Instance, | ||
FrameSize: Copy, | ||
W: Copy, | ||
{ | ||
type Error = Error; | ||
|
||
fn read(&mut self) -> nb::Result<FrameSize, Error> { | ||
let sr = self.spi.sr.read(); | ||
|
||
Err(if sr.ovr().bit_is_set() { | ||
Error::Overrun.into() | ||
} else if sr.modf().bit_is_set() { | ||
Error::ModeFault.into() | ||
} else if sr.crcerr().bit_is_set() { | ||
Error::Crc.into() | ||
} else if sr.rxne().bit_is_set() { | ||
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows | ||
// reading a half-word) | ||
return Ok(self.read_data_reg()); | ||
} else { | ||
nb::Error::WouldBlock | ||
}) | ||
fn read(&mut self) -> nb::Result<W, Error> { | ||
self.read_nonblocking() | ||
} | ||
|
||
fn send(&mut self, data: FrameSize) -> nb::Result<(), Error> { | ||
let sr = self.spi.sr.read(); | ||
|
||
// NOTE: Error::Overrun was deleted in #408. Need check | ||
Err(if sr.modf().bit_is_set() { | ||
Error::ModeFault.into() | ||
} else if sr.crcerr().bit_is_set() { | ||
Error::Crc.into() | ||
} else if sr.txe().bit_is_set() { | ||
// NOTE(write_volatile) see note above | ||
self.write_data_reg(data); | ||
return Ok(()); | ||
} else { | ||
nb::Error::WouldBlock | ||
}) | ||
fn send(&mut self, data: W) -> nb::Result<(), Error> { | ||
self.write_nonblocking(data) | ||
} | ||
} | ||
|
||
impl<SPI, REMAP, PINS, FrameSize, OP> blocking::transfer::Default<FrameSize> | ||
for Spi<SPI, REMAP, PINS, FrameSize, OP> | ||
impl<SPI, REMAP, PINS, W, OP> blocking::transfer::Default<W> for Spi<SPI, REMAP, PINS, W, OP> | ||
where | ||
SPI: Instance, | ||
FrameSize: Copy, | ||
W: Copy, | ||
{ | ||
} | ||
|
||
impl<SPI: Instance, REMAP, PINS, OP> blocking::Write<u8> for Spi<SPI, REMAP, PINS, u8, OP> { | ||
type Error = Error; | ||
|
||
// Implement write as per the "Transmit only procedure" page 712 | ||
// of RM0008 Rev 20. This is more than twice as fast as the | ||
// default Write<> implementation (which reads and drops each | ||
// received value) | ||
fn write(&mut self, words: &[u8]) -> Result<(), Error> { | ||
self.spi_write(words) | ||
self.write(words) | ||
} | ||
} | ||
|
||
impl<SPI: Instance, REMAP, PINS, OP> blocking::Write<u16> for Spi<SPI, REMAP, PINS, u16, OP> { | ||
type Error = Error; | ||
|
||
fn write(&mut self, words: &[u16]) -> Result<(), Error> { | ||
self.spi_write(words) | ||
self.write(words) | ||
} | ||
} |
This file contains 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,93 @@ | ||
use super::*; | ||
pub use embedded_hal::spi::{ErrorKind, ErrorType, Mode, Phase, Polarity}; | ||
|
||
impl From<Polarity> for super::Polarity { | ||
fn from(p: Polarity) -> Self { | ||
match p { | ||
Polarity::IdleLow => Self::IdleLow, | ||
Polarity::IdleHigh => Self::IdleHigh, | ||
} | ||
} | ||
} | ||
|
||
impl From<Phase> for super::Phase { | ||
fn from(p: Phase) -> Self { | ||
match p { | ||
Phase::CaptureOnFirstTransition => Self::CaptureOnFirstTransition, | ||
Phase::CaptureOnSecondTransition => Self::CaptureOnSecondTransition, | ||
} | ||
} | ||
} | ||
|
||
impl From<Mode> for super::Mode { | ||
fn from(m: Mode) -> Self { | ||
Self { | ||
polarity: m.polarity.into(), | ||
phase: m.phase.into(), | ||
} | ||
} | ||
} | ||
|
||
impl embedded_hal::spi::Error for Error { | ||
fn kind(&self) -> ErrorKind { | ||
match self { | ||
Self::Overrun => ErrorKind::Overrun, | ||
Self::ModeFault => ErrorKind::ModeFault, | ||
Self::Crc => ErrorKind::Other, | ||
} | ||
} | ||
} | ||
|
||
impl<SPI: Instance, REMAP, PINS, W, OP> ErrorType for Spi<SPI, REMAP, PINS, W, OP> { | ||
type Error = Error; | ||
} | ||
|
||
mod nb { | ||
use super::{Error, Instance, Spi}; | ||
use embedded_hal_nb::spi::FullDuplex; | ||
|
||
impl<SPI, REMAP, PINS, W, OP> FullDuplex<W> for Spi<SPI, REMAP, PINS, W, OP> | ||
where | ||
SPI: Instance, | ||
W: Copy, | ||
{ | ||
fn read(&mut self) -> nb::Result<W, Error> { | ||
self.read_nonblocking() | ||
} | ||
|
||
fn write(&mut self, data: W) -> nb::Result<(), Error> { | ||
self.write_nonblocking(data) | ||
} | ||
} | ||
} | ||
|
||
mod blocking { | ||
use super::super::{Instance, Spi}; | ||
use embedded_hal::spi::SpiBus; | ||
|
||
impl<SPI: Instance, REMAP, PINS, W, OP> SpiBus<W> for Spi<SPI, REMAP, PINS, W, OP> | ||
where | ||
SPI: Instance, | ||
W: Copy + 'static, | ||
{ | ||
fn transfer_in_place(&mut self, _words: &mut [W]) -> Result<(), Self::Error> { | ||
todo!() | ||
} | ||
|
||
fn transfer(&mut self, _buff: &mut [W], _data: &[W]) -> Result<(), Self::Error> { | ||
todo!() | ||
} | ||
|
||
fn read(&mut self, _words: &mut [W]) -> Result<(), Self::Error> { | ||
todo!() | ||
} | ||
|
||
fn write(&mut self, words: &[W]) -> Result<(), Self::Error> { | ||
self.write(words) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Self::Error> { | ||
Ok(()) | ||
} | ||
} | ||
} |