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 Jun 13, 2022
1 parent d6f206f commit e2ce5b5
Show file tree
Hide file tree
Showing 16 changed files with 1,418 additions and 1,919 deletions.
6 changes: 3 additions & 3 deletions boards/feather_m0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ pub use pins::*;

/// SPI pads for the labelled SPI peripheral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
/// You can use these pads with other, user-defined configurations.
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 All @@ -259,7 +259,7 @@ pub fn spi_master(
let freq = clock.freq();
let (miso, mosi, sclk) = (miso.into(), mosi.into(), sclk.into());
let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);
spi::Config::new(pm, sercom, pads, freq)
spi::Spi::config(pm, sercom, pads, freq)
.baud(baud)
.spi_mode(spi::MODE_0)
.enable()
Expand Down
6 changes: 3 additions & 3 deletions boards/feather_m4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ hal::bsp_pins!(

/// SPI pads for the labelled SPI peripheral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
/// You can use these pads with other, user-defined configurations.
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 All @@ -194,7 +194,7 @@ pub fn spi_master(
let freq = clock.freq();
let (miso, mosi, sclk) = (miso.into(), mosi.into(), sclk.into());
let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);
spi::Config::new(mclk, sercom, pads, freq)
spi::Spi::config(mclk, sercom, pads, freq)
.baud(baud)
.spi_mode(spi::MODE_0)
.enable()
Expand Down
10 changes: 5 additions & 5 deletions boards/metro_m0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ pub use pins::*;

/// SPI pads for the labelled SPI peripheral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
/// You can use these pads with other, user-defined configurations.
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 All @@ -229,7 +229,7 @@ pub fn spi_master(
let freq = clock.freq();
let (miso, mosi, sclk) = (miso.into(), mosi.into(), sclk.into());
let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);
spi::Config::new(pm, sercom4, pads, freq)
spi::Spi::config(pm, sercom4, pads, freq)
.baud(baud)
.spi_mode(spi::MODE_0)
.enable()
Expand All @@ -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 All @@ -260,7 +260,7 @@ pub fn flash_spi_master(
let freq = clock.freq();
let (miso, mosi, sclk, mut cs) = (miso.into(), mosi.into(), sclk.into(), cs.into());
let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);
let spi = spi::Config::new(pm, sercom5, pads, freq)
let spi = spi::Spi::config(pm, sercom5, pads, freq)
.baud(MegaHertz(48))
.spi_mode(spi::MODE_0)
.enable();
Expand Down
6 changes: 3 additions & 3 deletions boards/metro_m4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ hal::bsp_pins!(

/// SPI pads for the labelled SPI peripheral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
/// You can use these pads with other, user-defined configurations.
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 All @@ -260,7 +260,7 @@ pub fn spi_master(
let freq = clock.freq();
let (miso, mosi, sclk) = (miso.into(), mosi.into(), sclk.into());
let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);
spi::Config::new(mclk, sercom, pads, freq)
spi::Spi::config(mclk, sercom, pads, freq)
.baud(baud)
.spi_mode(spi::MODE_0)
.enable()
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -60,7 +60,7 @@ impl Display {
let gclk0 = clocks.gclk0();
let clock = &clocks.sercom7_core(&gclk0).ok_or(())?;
let pads = spi::Pads::default().data_out(self.mosi).sclk(self.sck);
let spi = spi::Config::new(mclk, sercom7, pads, clock.freq())
let spi = spi::Spi::config(mclk, sercom7, pads, clock.freq())
.spi_mode(spi::MODE_0)
.baud(baud)
.enable();
Expand Down
9 changes: 6 additions & 3 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(|c| c.set_baud(baud));
}
}

Expand Down Expand Up @@ -104,7 +107,7 @@ impl SDCard {
.data_out(self.mosi)
.data_in(self.miso)
.sclk(self.sck);
let spi = spi::Config::new(mclk, sercom6, pads, sercom6_clk.freq())
let spi = spi::Spi::config(mclk, sercom6, pads, sercom6_clk.freq())
.spi_mode(spi::MODE_0)
.baud(400.khz())
.enable();
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 e2ce5b5

Please sign in to comment.