Skip to content

Commit

Permalink
Refactor the spi module
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyharden committed May 22, 2022
1 parent 8139d7a commit 7fff744
Show file tree
Hide file tree
Showing 17 changed files with 1,347 additions and 1,765 deletions.
2 changes: 1 addition & 1 deletion boards/feather_m0/examples/adalogger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn main() -> ! {
controller
.device()
.spi()
.reconfigure(|c| c.set_baud(4.mhz()));
.reconfigure(|mut c| c.set_baud(4.mhz()));
usbserial_write!("OK!\r\nCard size...\r\n");
match controller.device().card_size_bytes() {
Ok(size) => usbserial_write!("{} bytes\r\n", size),
Expand Down
2 changes: 1 addition & 1 deletion boards/feather_m0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub type SpiPads = spi::Pads<SpiSercom, Miso, Mosi, Sclk>;
/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type Spi = spi::Spi<spi::Config<SpiPads>, spi::Duplex>;
pub type Spi = spi::Spi<SpiPads>;

/// Convenience for setting up the labelled SPI peripheral.
/// This powers up the SPI SERCOM and configures it for use as an
Expand Down
2 changes: 1 addition & 1 deletion boards/feather_m4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub type SpiPads = spi::Pads<SpiSercom, UndocIoSet1, Miso, Mosi, Sclk>;
/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type Spi = spi::Spi<spi::Config<SpiPads>, spi::Duplex>;
pub type Spi = spi::Spi<SpiPads>;

/// Convenience for setting up the labelled SPI peripheral.
/// This powers up SERCOM1 and configures it for use as an
Expand Down
4 changes: 2 additions & 2 deletions boards/metro_m0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub type SpiPads = spi::Pads<Sercom4, Miso, Mosi, Sclk>;
/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type Spi = spi::Spi<spi::Config<SpiPads>, spi::Duplex>;
pub type Spi = spi::Spi<SpiPads>;

/// Convenience for setting up the 2x3 header block for SPI.
/// This powers up SERCOM4 and configures it for use as an
Expand Down Expand Up @@ -241,7 +241,7 @@ pub type FlashPads = spi::Pads<Sercom5, FlashMiso, FlashMosi, FlashSclk>;
/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type FlashSpi = (spi::Spi<spi::Config<FlashPads>, spi::Duplex>, FlashCs);
pub type FlashSpi = (spi::Spi<FlashPads>, FlashCs);

/// Convenience for accessing the on-board SPI Flash device.
/// This powers up SERCOM5 and configures it for use as an
Expand Down
2 changes: 1 addition & 1 deletion boards/metro_m4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub type SpiPads = spi::Pads<SpiSercom, IoSet1, Miso, Mosi, Sclk>;
/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type Spi = spi::Spi<spi::Config<SpiPads>, spi::Duplex>;
pub type Spi = spi::Spi<SpiPads>;

/// Convenience for setting up the 2x3 header block for SPI.
/// This powers up SERCOM2 and configures it for use as an
Expand Down
2 changes: 1 addition & 1 deletion boards/wio_terminal/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Display {
}

pub type LcdPads = spi::Pads<Sercom7, IoSet4, NoneT, LcdMosi, LcdSck>;
pub type LcdSpi = spi::Spi<spi::Config<LcdPads>, spi::Tx>;
pub type LcdSpi = spi::Spi<LcdPads>;

/// Type alias for the ILI9341 LCD display.
pub type LCD = Ili9341<SPIInterface<LcdSpi, LcdDc, LcdCs>, LcdReset>;
Expand Down
7 changes: 5 additions & 2 deletions boards/wio_terminal/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct SDCard {
}

pub type SdPads = spi::Pads<Sercom6, IoSet1, SdMiso, SdMosi, SdSck>;
pub type SdSpi = spi::Spi<spi::Config<SdPads>, spi::Duplex>;
pub type SdSpi = spi::Spi<SdPads>;
type Controller<TS> = embedded_sdmmc::Controller<SdMmcSpi<SdSpi, SdCs>, TS>;

/// An initialized SPI SDMMC controller.
Expand All @@ -71,7 +71,10 @@ impl<TS: TimeSource> SDCardController<TS> {
/// Initializes the MMC card. An error is returned if there is no card
/// or a communications error occurs.
pub fn set_baud<B: Into<Hertz>>(&mut self, baud: B) {
self.cont.device().spi().reconfigure(|c| c.set_baud(baud));
self.cont
.device()
.spi()
.reconfigure(|mut c| c.set_baud(baud));
}
}

Expand Down
41 changes: 22 additions & 19 deletions hal/src/sercom/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,14 @@ where
// SPI DMA transfers
//=============================================================================

unsafe impl<C, A> Buffer for Spi<C, A>
unsafe impl<P, M, Z> Buffer for Spi<P, M, Z>
where
C: spi::ValidConfig,
C::OpMode: spi::MasterMode,
C::Size: spi::AtomicSize<Word = C::Word>,
C::Word: Beat,
A: spi::Capability,
P: spi::ValidPads,
M: spi::MasterMode,
Z: spi::AtomicSize,
Z::Word: Beat,
{
type Beat = C::Word;
type Beat = Z::Word;

#[inline]
fn dma_ptr(&mut self) -> *mut Self::Beat {
Expand All @@ -303,11 +302,13 @@ where
}
}

impl<C, A> Spi<C, A>
impl<P, M, Z> Spi<P, M, Z>
where
C: spi::ValidConfig,
A: spi::Transmit,
Self: Buffer<Beat = C::Word>,
P: spi::ValidPads,
P::Capability: spi::Transmit,
M: spi::OpMode,
Z: spi::Size,
Self: Buffer<Beat = Z::Word>,
{
/// Transform an [`Spi`] into a DMA [`Transfer`]) and
/// start a send transaction.
Expand All @@ -320,7 +321,7 @@ where
) -> Transfer<Channel<Ch::Id, Busy>, BufferPair<B, Self>, W>
where
Ch: AnyChannel<Status = Ready>,
B: Buffer<Beat = C::Word> + 'static,
B: Buffer<Beat = Z::Word> + 'static,
W: FnOnce(CallbackStatus) + 'static,
{
channel
Expand All @@ -337,15 +338,17 @@ where
// for `B`, and the fact that the buffer length of an `Spi` is always 1.
let xfer = unsafe { Transfer::new_unchecked(channel, buf, self, false) };
xfer.with_waker(waker)
.begin(C::Sercom::DMA_TX_TRIGGER, trigger_action)
.begin(P::Sercom::DMA_TX_TRIGGER, trigger_action)
}
}

impl<C, A> Spi<C, A>
impl<P, M, Z> Spi<P, M, Z>
where
C: spi::ValidConfig,
A: spi::Receive,
Self: Buffer<Beat = C::Word>,
P: spi::ValidPads,
P::Capability: spi::Receive,
M: spi::OpMode,
Z: spi::Size,
Self: Buffer<Beat = Z::Word>,
{
/// Transform an [`Spi`] into a DMA [`Transfer`]) and
/// start a receive transaction.
Expand All @@ -358,7 +361,7 @@ where
) -> Transfer<Channel<Ch::Id, Busy>, BufferPair<Self, B>, W>
where
Ch: AnyChannel<Status = Ready>,
B: Buffer<Beat = C::Word> + 'static,
B: Buffer<Beat = Z::Word> + 'static,
W: FnOnce(CallbackStatus) + 'static,
{
channel
Expand All @@ -375,6 +378,6 @@ where
// for `B`, and the fact that the buffer length of an `Spi` is always 1.
let xfer = unsafe { Transfer::new_unchecked(channel, self, buf, false) };
xfer.with_waker(waker)
.begin(C::Sercom::DMA_RX_TRIGGER, trigger_action)
.begin(P::Sercom::DMA_RX_TRIGGER, trigger_action)
}
}
Loading

0 comments on commit 7fff744

Please sign in to comment.