Skip to content

Commit

Permalink
Use struct instead of tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominaezzz committed Jun 5, 2024
1 parent 539b0f4 commit db942a8
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 50 deletions.
40 changes: 26 additions & 14 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! let mosi = io.pins.gpio13;
//! let cs = io.pins.gpio10;
//!
//! let (spi, mut fifo) = hal::spi::Spi::new(
//! let SpiParts { spi, mut buf } = hal::spi::Spi::new(
//! peripherals.SPI2,
//! 100.kHz(),
//! SpiMode::Mode0,
Expand All @@ -24,7 +24,7 @@
//! let mut spi = spi.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs));
//!
//! // Convenience wrapper
//! let spi_bus = hal::spi::SpiFifo::new(spi, fifo);
//! let spi_bus = hal::spi::SpiFifo::new(spi, buf);
//! ```
//!
//! ## Exclusive access to the SPI bus
Expand Down Expand Up @@ -590,6 +590,12 @@ impl<'d, T, M> Debug for Spi<'d, T, M> {
}
}

#[non_exhaustive]
pub struct SpiParts<'d, T, M> {
pub spi: Spi<'d, T, M>,
pub buf: SpiBuf<'d, T, WholeBuf>,
}

/// SPI peripheral driver
pub struct SpiFifo<'d, T: Instance, M> {
state: DriverState<'d, T, M>,
Expand Down Expand Up @@ -753,16 +759,19 @@ where
frequency: HertzU32,
mode: SpiMode,
clocks: &Clocks,
) -> (Spi<'d, T, FullDuplexMode>, SpiBuf<'d, T, WholeBuf>)
) -> SpiParts<'d, T, FullDuplexMode>
where
T: Peripheral<P = T>,
{
crate::into_ref!(spi);
let fifo = SpiBuf {
spi: unsafe { spi.clone_unchecked() },
state: WholeBuf,
};
(Self::new_internal(spi, frequency, mode, clocks), fifo)
SpiParts {
buf: SpiBuf {
spi: unsafe { spi.clone_unchecked() },
state: WholeBuf,
},
spi: Self::new_internal(spi, frequency, mode, clocks),
_hidden: (),
}
}

pub fn with_sck<SCK: OutputPin>(self, sck: impl Peripheral<P = SCK> + 'd) -> Self {
Expand Down Expand Up @@ -914,16 +923,19 @@ where
frequency: HertzU32,
mode: SpiMode,
clocks: &Clocks,
) -> (Spi<'d, T, HalfDuplexMode>, SpiBuf<'d, T, WholeBuf>)
) -> SpiParts<'d, T, HalfDuplexMode>
where
T: Peripheral<P = T>,
{
crate::into_ref!(spi);
let fifo = SpiBuf {
spi: unsafe { spi.clone_unchecked() },
state: WholeBuf,
};
(Self::new_internal(spi, frequency, mode, clocks), fifo)
SpiParts {
buf: SpiBuf {
spi: unsafe { spi.clone_unchecked() },
state: WholeBuf,
},
spi: Self::new_internal(spi, frequency, mode, clocks),
_hidden: (),
}
}

pub fn with_sck<SCK: OutputPin>(self, sck: impl Peripheral<P = SCK> + 'd) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/embassy_spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn main(_spawner: Spawner) {
let (mut descriptors, mut rx_descriptors) = dma_descriptors!(32000);

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure_for_async(
false,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/qspi_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn main() -> ! {
let (tx_buffer, mut tx_descriptors, rx_buffer, mut rx_descriptors) = dma_buffers!(256, 320);

let mut spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(
Some(sclk),
Some(mosi),
Expand Down
22 changes: 11 additions & 11 deletions examples/src/bin/spi_halfduplex_read_manufacturer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use esp_hal::{
peripherals::Peripherals,
prelude::*,
spi::{
master::{Address, Command, Spi},
master::{Address, Command, Spi, SpiParts},
SpiDataMode,
SpiMode,
},
Expand Down Expand Up @@ -68,7 +68,7 @@ fn main() -> ! {
}
}

let (spi, mut fifo) =
let SpiParts { spi, mut buf, .. } =
Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks);
let mut spi = spi.with_pins(
Some(sclk),
Expand All @@ -84,52 +84,52 @@ fn main() -> ! {
loop {
// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
(spi, fifo) = spi
(spi, buf) = spi
.read(
SpiDataMode::Single,
Command::Command8(0x90, SpiDataMode::Single),
Address::Address24(0x000000, SpiDataMode::Single),
0,
data.len(),
fifo,
buf,
)
.unwrap()
.wait();
fifo.read(&mut data);
buf.read(&mut data);
println!("Single {:x?}", data);
delay.delay_millis(250);

// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
(spi, fifo) = spi
(spi, buf) = spi
.read(
SpiDataMode::Dual,
Command::Command8(0x92, SpiDataMode::Single),
Address::Address32(0x000000_00, SpiDataMode::Dual),
0,
data.len(),
fifo,
buf,
)
.unwrap()
.wait();
fifo.read(&mut data);
buf.read(&mut data);
println!("Dual {:x?}", data);
delay.delay_millis(250);

// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
(spi, fifo) = spi
(spi, buf) = spi
.read(
SpiDataMode::Quad,
Command::Command8(0x94, SpiDataMode::Single),
Address::Address32(0x000000_00, SpiDataMode::Quad),
4,
data.len(),
fifo,
buf,
)
.unwrap()
.wait();
fifo.read(&mut data);
buf.read(&mut data);
println!("Quad {:x?}", data);
delay.delay_millis(1500);
}
Expand Down
14 changes: 9 additions & 5 deletions examples/src/bin/spi_loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use esp_hal::{
gpio::{any_pin::AnyPin, Io},
peripherals::Peripherals,
prelude::*,
spi::{master::Spi, SpiMode},
spi::{
master::{Spi, SpiParts},
SpiMode,
},
system::SystemControl,
};
use esp_println::println;
Expand All @@ -45,16 +48,17 @@ fn main() -> ! {
let miso = AnyPin::new(miso);
let mosi = AnyPin::new(mosi);

let (spi, mut fifo) = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks);
let SpiParts { spi, mut buf, .. } =
Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks);
let mut spi = spi.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs));

let delay = Delay::new(&clocks);

loop {
let mut data = [0xde, 0xca, 0xfb, 0xad];
fifo.write(&data);
(spi, fifo) = spi.transfer(data.len(), fifo).unwrap().wait();
fifo.read(&mut data);
buf.write(&data);
(spi, buf) = spi.transfer(data.len(), buf).unwrap().wait();
buf.read(&mut data);
println!("{:x?}", data);

delay.delay_millis(250);
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/spi_loopback_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> ! {
let (tx_buffer, mut tx_descriptors, rx_buffer, mut rx_descriptors) = dma_buffers!(32000);

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
Expand Down
25 changes: 14 additions & 11 deletions examples/src/bin/spi_loopback_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use esp_hal::{
gpio::{any_pin::AnyPin, Io},
peripherals::Peripherals,
prelude::*,
spi::{master::Spi, SpiMode},
spi::{
master::{Spi, SpiParts},
SpiMode,
},
system::SystemControl,
};
use esp_println::println;
Expand All @@ -45,27 +48,27 @@ fn main() -> ! {
let miso = AnyPin::new(miso);
let mosi = AnyPin::new(mosi);

let (spi, fifo) = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks);
let SpiParts { spi, buf, .. } = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks);
let spi = spi.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs));

let (mut fifo0, fifo1) = fifo.split();
let (mut buf0, buf1) = buf.split();

let delay = Delay::new(&clocks);

let data_to_send = [0xDA, 0xB0, 0xDE, 0xCA, 0xFB, 0xAD, 0xBE, 0xEF, 0xAF, 0x22];

fifo0.write(&data_to_send);
let mut active_transfer = spi.transfer(data_to_send.len(), fifo0).unwrap();
let mut available_fifo = fifo1;
buf0.write(&data_to_send);
let mut active_transfer = spi.transfer(data_to_send.len(), buf0).unwrap();
let mut available_buf = buf1;

loop {
available_fifo.write(&data_to_send);
let (spi, new_fifo) = active_transfer.wait();
active_transfer = spi.transfer(data_to_send.len(), available_fifo).unwrap();
available_fifo = new_fifo;
available_buf.write(&data_to_send);
let (spi, new_buf) = active_transfer.wait();
active_transfer = spi.transfer(data_to_send.len(), available_buf).unwrap();
available_buf = new_buf;

let mut data_to_receive = [0; 10];
available_fifo.read(&mut data_to_receive);
available_buf.read(&mut data_to_receive);

println!("{:x?}", data_to_receive);

Expand Down
7 changes: 4 additions & 3 deletions hil-test/tests/spi_full_duplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use esp_hal::{
peripherals::Peripherals,
prelude::*,
spi::{
master::{Spi, SpiFifo},
master::{Spi, SpiFifo, SpiParts},
FullDuplexMode,
SpiMode,
},
Expand All @@ -45,11 +45,12 @@ impl Context {
let mosi = io.pins.gpio4;
let cs = io.pins.gpio5;

let (spi, fifo) = Spi::new(peripherals.SPI2, 1000u32.kHz(), SpiMode::Mode0, &clocks);
let SpiParts { spi, buf, .. } =
Spi::new(peripherals.SPI2, 1000u32.kHz(), SpiMode::Mode0, &clocks);
let spi = spi.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs));

Context {
spi: SpiFifo::new(spi, fifo),
spi: SpiFifo::new(spi, buf),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions hil-test/tests/spi_full_duplex_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {
dma_buffers!(DMA_BUFFER_SIZE);

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
Expand Down Expand Up @@ -105,7 +105,7 @@ mod tests {
let (tx_buffer, mut tx_descriptors, rx_buffer, mut rx_descriptors) = dma_buffers!(4, 2);

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
Expand Down Expand Up @@ -151,7 +151,7 @@ mod tests {
dma_buffers!(DMA_BUFFER_SIZE);

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.0
.spi
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
Expand Down

0 comments on commit db942a8

Please sign in to comment.