Remove FullDuplexMode and HalfDuplexMode type params (#2373)

* Remove duplex type param

* Rename dma_ functions
This commit is contained in:
Dániel Buga 2024-10-24 09:22:21 +02:00 committed by GitHub
parent f03527f6fc
commit 2ac47868f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 511 additions and 823 deletions

View File

@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `AnyTwai`. (#2359)
- `Pins::steal()` to unsafely obtain GPIO. (#2335)
- `I2c::with_timeout` (#2361)
- `Spi::half_duplex_read` and `Spi::half_duplex_write` (#2373)
### Changed
@ -25,6 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Peripheral type erasure for I2S (#2367)
- Peripheral type erasure for I2C (#2361)
- Peripheral type erasure for TWAI (#2359)
- The SPI driver has been rewritten to allow using half-duplex and full-duplex functionality on the same bus. See the migration guide for details. (#2373)
- Renamed `SpiDma` functions: `dma_transfer` to `transfer`, `dma_write` to `write`, `dma_read` to `read`. (#2373)
### Fixed
@ -36,6 +39,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `i2s::{I2sWrite, I2sWriteDma, I2sRead, I2sReadDma, I2sWriteDmaAsync, I2sReadDmaAsync}` traits have been removed. (#2316)
- The `ledc::ChannelHW` trait is no longer generic. (#2387)
- The `I2c::new_with_timeout` constructors have been removed (#2361)
- The `spi::master::HalfDuplexReadWrite` trait has been removed. (#2373)
- The `Spi::with_pins` methods have been removed. (#2373)
- The `Spi::new_half_duplex` constructor have been removed. (#2373)
- The `HalfDuplexMode` and `FullDuplexMode` parameters have been removed from `Spi`. (#2373)
## [0.21.1]

View File

@ -60,3 +60,60 @@ The `with_timeout` constructors have been removed in favour of `set_timeout` or
-let i2c = I2c::new_with_timeout(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, 100.kHz(), timeout);
+let i2c = I2c::new(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, 100.kHz()).with_timeout(timeout);
```
## Changes to half-duplex SPI
The `HalfDuplexMode` and `FullDuplexMode` type parameters have been removed from SPI master and slave
drivers. It is now possible to execute half-duplex and full-duplex operations on the same SPI bus.
### Driver construction
- The `Spi::new_half_duplex` constructor has been removed. Use `new` (or `new_typed`) instead.
- The `with_pins` methods have been removed. Use the individual `with_*` functions instead.
- The `with_mosi` and `with_miso` functions now take input-output peripheral signals to support half-duplex mode.
> TODO(danielb): this means they are currently only usable with GPIO pins, but upcoming GPIO changes should allow using any output signal.
```diff
- let mut spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
- .with_pins(sck, mosi, miso, sio2, sio3, cs);
+ let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
+ .with_sck(sck)
+ .with_cs(cs)
+ .with_mosi(mosi)
+ .with_miso(miso)
+ .with_sio2(sio2)
+ .with_sio3(sio3);
```
### Transfer functions
The `Spi<'_, SPI, HalfDuplexMode>::read` and `Spi<'_, SPI, HalfDuplexMode>::write` functions have been replaced by
`half_duplex_read` and `half_duplex_write`.
```diff
let mut data = [0u8; 2];
let transfer = spi
- .read(
+ .half_duplex_read(
SpiDataMode::Single,
Command::Command8(0x90, SpiDataMode::Single),
Address::Address24(0x000000, SpiDataMode::Single),
0,
&mut data,
)
.unwrap();
let transfer = spi
- .write(
+ .half_duplex_write(
SpiDataMode::Quad,
Command::Command8(write as u16, command_data_mode),
Address::Address24(
write as u32 | (write as u32) << 8 | (write as u32) << 16,
SpiDataMode::Quad,
),
0,
dma_tx_buf,
)
.unwrap();
```

View File

@ -35,7 +35,10 @@
//! 100.kHz(),
//! SpiMode::Mode0,
//! )
//! .with_pins(sclk, mosi, miso, cs)
//! .with_sck(sclk)
//! .with_mosi(mosi)
//! .with_miso(miso)
//! .with_cs(cs)
//! .with_dma(dma_channel.configure(
//! false,
//! DmaPriority::Priority0,

File diff suppressed because it is too large Load Diff

View File

@ -76,9 +76,6 @@ pub enum SpiBitOrder {
LSBFirst,
}
/// Trait marker for defining SPI duplex modes.
pub trait DuplexMode: crate::private::Sealed {}
/// SPI data mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@ -91,16 +88,6 @@ pub enum SpiDataMode {
Quad,
}
/// Full-duplex operation
pub struct FullDuplexMode {}
impl DuplexMode for FullDuplexMode {}
impl crate::private::Sealed for FullDuplexMode {}
/// Half-duplex operation
pub struct HalfDuplexMode {}
impl DuplexMode for HalfDuplexMode {}
impl crate::private::Sealed for HalfDuplexMode {}
crate::any_peripheral! {
/// Any SPI peripheral.
pub peripheral AnySpi {

View File

@ -48,7 +48,7 @@
//! let mut send = tx_buffer;
//!
//! let transfer = spi
//! .dma_transfer(&mut receive, &mut send)
//! .transfer(&mut receive, &mut send)
//! .unwrap();
//!
//! transfer.wait().unwrap();
@ -69,9 +69,8 @@
//! `is_done()`.
//!
//! See [tracking issue](https://github.com/esp-rs/esp-hal/issues/469) for more information.
use core::marker::PhantomData;
use super::{Error, FullDuplexMode, SpiMode};
use super::{Error, SpiMode};
use crate::{
dma::{DescriptorChain, DmaChannelConvert, DmaEligible, PeripheralMarker, Rx, Tx},
gpio::{InputSignal, OutputSignal, PeripheralInput, PeripheralOutput},
@ -87,14 +86,13 @@ const MAX_DMA_SIZE: usize = 32768 - 32;
/// SPI peripheral driver.
///
/// See the [module-level documentation][self] for more details.
pub struct Spi<'d, M, T = AnySpi> {
pub struct Spi<'d, T = AnySpi> {
spi: PeripheralRef<'d, T>,
#[allow(dead_code)]
data_mode: SpiMode,
_mode: PhantomData<M>,
}
impl<'d> Spi<'d, FullDuplexMode> {
impl<'d> Spi<'d> {
/// Constructs an SPI instance in 8bit dataframe mode.
pub fn new<
SCK: PeripheralInput,
@ -108,12 +106,12 @@ impl<'d> Spi<'d, FullDuplexMode> {
miso: impl Peripheral<P = MISO> + 'd,
cs: impl Peripheral<P = CS> + 'd,
mode: SpiMode,
) -> Spi<'d, FullDuplexMode> {
) -> Spi<'d> {
Self::new_typed(spi, sclk, mosi, miso, cs, mode)
}
}
impl<'d, T> Spi<'d, FullDuplexMode, T>
impl<'d, T> Spi<'d, T>
where
T: Instance,
{
@ -130,7 +128,7 @@ where
miso: impl Peripheral<P = MISO> + 'd,
cs: impl Peripheral<P = CS> + 'd,
mode: SpiMode,
) -> Spi<'d, FullDuplexMode, T> {
) -> Spi<'d, T> {
crate::into_ref!(sclk, mosi, miso, cs);
let this = Self::new_internal(spi, mode);
@ -154,13 +152,12 @@ where
pub(crate) fn new_internal(
spi: impl Peripheral<P = impl Into<T> + 'd> + 'd,
mode: SpiMode,
) -> Spi<'d, FullDuplexMode, T> {
) -> Spi<'d, T> {
crate::into_ref!(spi);
let mut spi = Spi {
spi: spi.map_into(),
data_mode: mode,
_mode: PhantomData,
};
spi.spi.reset_peripheral();
spi.spi.enable_peripheral();
@ -193,7 +190,7 @@ pub mod dma {
Mode,
};
impl<'d, T> Spi<'d, FullDuplexMode, T>
impl<'d, T> Spi<'d, T>
where
T: InstanceDma,
{
@ -316,7 +313,7 @@ pub mod dma {
/// sent is 32736 bytes.
///
/// The write is driven by the SPI master's sclk signal and cs line.
pub fn dma_write<'t, TXBUF>(
pub fn write<'t, TXBUF>(
&'t mut self,
words: &'t TXBUF,
) -> Result<DmaTransferTx<'t, Self>, Error>
@ -351,7 +348,7 @@ pub mod dma {
/// received is 32736 bytes.
///
/// The read is driven by the SPI master's sclk signal and cs line.
pub fn dma_read<'t, RXBUF>(
pub fn read<'t, RXBUF>(
&'t mut self,
words: &'t mut RXBUF,
) -> Result<DmaTransferRx<'t, Self>, Error>
@ -387,7 +384,7 @@ pub mod dma {
///
/// The data transfer is driven by the SPI master's sclk signal and cs
/// line.
pub fn dma_transfer<'t, RXBUF, TXBUF>(
pub fn transfer<'t, RXBUF, TXBUF>(
&'t mut self,
read_buffer: &'t mut RXBUF,
words: &'t TXBUF,

View File

@ -59,7 +59,10 @@ async fn main(_spawner: Spawner) {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_pins(sclk, mosi, miso, cs)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0))
.with_buffers(dma_rx_buf, dma_tx_buf);

View File

@ -79,8 +79,13 @@ fn main() -> ! {
let mut dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let mut spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_pins(sclk, mosi, miso, sio2, sio3, cs)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_sio2(sio2)
.with_sio3(sio3)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
let delay = Delay::new();
@ -88,7 +93,7 @@ fn main() -> ! {
// write enable
dma_tx_buf.set_length(0);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::Command8(0x06, SpiDataMode::Single),
Address::None,
@ -102,7 +107,7 @@ fn main() -> ! {
// erase sector
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::Command8(0x20, SpiDataMode::Single),
Address::Address24(0x000000, SpiDataMode::Single),
@ -116,7 +121,7 @@ fn main() -> ! {
// write enable
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::Command8(0x06, SpiDataMode::Single),
Address::None,
@ -133,7 +138,7 @@ fn main() -> ! {
dma_tx_buf.as_mut_slice().fill(b'!');
dma_tx_buf.as_mut_slice()[0..][..5].copy_from_slice(&b"Hello"[..]);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Quad,
Command::Command8(0x32, SpiDataMode::Single),
Address::Address24(0x000000, SpiDataMode::Single),
@ -148,7 +153,7 @@ fn main() -> ! {
loop {
// quad fast read
let transfer = spi
.read(
.half_duplex_read(
SpiDataMode::Quad,
Command::Command8(0xeb, SpiDataMode::Single),
Address::Address32(0x000000 << 8, SpiDataMode::Quad),

View File

@ -33,7 +33,7 @@ use esp_hal::{
gpio::Io,
prelude::*,
spi::{
master::{Address, Command, HalfDuplexReadWrite, Spi},
master::{Address, Command, Spi},
SpiDataMode,
SpiMode,
},
@ -63,15 +63,20 @@ fn main() -> ! {
}
}
let mut spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_pins(sclk, mosi, miso, sio2, sio3, cs);
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_sio2(sio2)
.with_sio3(sio3)
.with_cs(cs);
let delay = Delay::new();
loop {
// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
spi.read(
spi.half_duplex_read(
SpiDataMode::Single,
Command::Command8(0x90, SpiDataMode::Single),
Address::Address24(0x000000, SpiDataMode::Single),
@ -84,7 +89,7 @@ fn main() -> ! {
// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
spi.read(
spi.half_duplex_read(
SpiDataMode::Dual,
Command::Command8(0x92, SpiDataMode::Single),
Address::Address32(0x000000_00, SpiDataMode::Dual),
@ -97,7 +102,7 @@ fn main() -> ! {
// READ MANUFACTURER ID FROM FLASH CHIP
let mut data = [0u8; 2];
spi.read(
spi.half_duplex_read(
SpiDataMode::Quad,
Command::Command8(0x94, SpiDataMode::Single),
Address::Address32(0x000000_00, SpiDataMode::Quad),

View File

@ -19,6 +19,7 @@ use esp_backtrace as _;
use esp_hal::{
delay::Delay,
gpio::Io,
peripheral::Peripheral,
prelude::*,
spi::{master::Spi, SpiMode},
};
@ -33,11 +34,13 @@ fn main() -> ! {
let miso_mosi = io.pins.gpio2;
let cs = io.pins.gpio5;
let miso = miso_mosi.peripheral_input();
let mosi = miso_mosi.into_peripheral_output();
let miso = unsafe { miso_mosi.clone_unchecked() };
let mut spi =
Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0).with_pins(sclk, mosi, miso, cs);
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(miso_mosi)
.with_miso(miso)
.with_cs(cs);
let delay = Delay::new();

View File

@ -54,7 +54,10 @@ fn main() -> ! {
let mut dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_pins(sclk, mosi, miso, cs)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
let delay = Delay::new();
@ -71,7 +74,7 @@ fn main() -> ! {
i = i.wrapping_add(1);
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
// here we could do something else while DMA transfer is in progress

View File

@ -2,7 +2,7 @@
//!
//! The following wiring is assumed:
//! - SCLK => GPIO42
//! - MISO => (loopback to MOSI via peripheral_input())
//! - MISO => (looped back to MOSI via the GPIO MUX)
//! - MOSI => GPIO48
//! - CS => GPIO38
//!
@ -27,6 +27,7 @@ use esp_hal::{
delay::Delay,
dma::{Dma, DmaBufBlkSize, DmaPriority, DmaRxBuf, DmaTxBuf},
gpio::Io,
peripheral::Peripheral,
prelude::*,
spi::{master::Spi, SpiMode},
};
@ -62,7 +63,7 @@ fn main() -> ! {
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio42;
let mosi = io.pins.gpio48;
let miso = mosi.peripheral_input();
let miso = unsafe { mosi.clone_unchecked() };
let cs = io.pins.gpio38;
let dma = Dma::new(peripherals.DMA);
@ -87,8 +88,13 @@ fn main() -> ! {
rx_descriptors.len()
);
let mut dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
// Need to set miso first so that mosi can overwrite the
// output connection (because we are using the same pin to loop back)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_pins(sclk, mosi, miso, cs)
.with_sck(sclk)
.with_miso(miso)
.with_mosi(mosi)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
delay.delay_millis(100); // delay to let the above messages display
@ -105,7 +111,7 @@ fn main() -> ! {
i = i.wrapping_add(1);
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();

View File

@ -107,11 +107,9 @@ fn main() -> ! {
println!("Iteration {i}");
println!("Do `dma_transfer`");
println!("Do `transfer`");
let transfer = spi
.dma_transfer(&mut slave_receive, &mut slave_send)
.unwrap();
let transfer = spi.transfer(&mut slave_receive, &mut slave_send).unwrap();
bitbang_master(
master_send,
@ -133,9 +131,9 @@ fn main() -> ! {
delay.delay_millis(250);
println!("Do `dma_read`");
println!("Do `read`");
slave_receive.fill(0xff);
let transfer = spi.dma_read(&mut slave_receive).unwrap();
let transfer = spi.read(&mut slave_receive).unwrap();
bitbang_master(
master_send,
@ -155,8 +153,8 @@ fn main() -> ! {
delay.delay_millis(250);
println!("Do `dma_write`");
let transfer = spi.dma_write(&mut slave_send).unwrap();
println!("Do `write`");
let transfer = spi.write(&mut slave_send).unwrap();
master_receive.fill(0);

View File

@ -15,7 +15,6 @@ use esp_hal::{
prelude::*,
spi::{
master::{Spi, SpiDma},
FullDuplexMode,
SpiMode,
},
timer::{timg::TimerGroup, AnyTimer},
@ -34,7 +33,7 @@ macro_rules! mk_static {
}
#[embassy_executor::task]
async fn interrupt_driven_task(spi: SpiDma<'static, FullDuplexMode, Async>) {
async fn interrupt_driven_task(spi: SpiDma<'static, Async>) {
let mut ticker = Ticker::every(Duration::from_millis(1));
let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(128);

View File

@ -10,11 +10,10 @@ use esp_hal::pcnt::{channel::EdgeMode, unit::Unit, Pcnt};
use esp_hal::{
dma::{Channel, Dma, DmaPriority, DmaRxBuf, DmaTxBuf},
dma_buffers,
gpio::{AnyPin, Input, Io, Level, NoPin, Output, Pull},
gpio::{AnyPin, Input, Io, Level, Output, Pull},
prelude::*,
spi::{
master::{Address, Command, Spi, SpiDma},
HalfDuplexMode,
SpiDataMode,
SpiMode,
},
@ -38,7 +37,7 @@ cfg_if::cfg_if! {
}
}
type SpiUnderTest = SpiDma<'static, HalfDuplexMode, Blocking>;
type SpiUnderTest = SpiDma<'static, Blocking>;
struct Context {
spi: esp_hal::peripherals::SPI2,
@ -54,7 +53,7 @@ fn transfer_read(
command: Command,
) -> (SpiUnderTest, DmaRxBuf) {
let transfer = spi
.read(SpiDataMode::Quad, command, Address::None, 0, dma_rx_buf)
.half_duplex_read(SpiDataMode::Quad, command, Address::None, 0, dma_rx_buf)
.map_err(|e| e.0)
.unwrap();
transfer.wait()
@ -67,7 +66,7 @@ fn transfer_write(
command_data_mode: SpiDataMode,
) -> (SpiUnderTest, DmaTxBuf) {
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Quad,
Command::Command8(write as u16, command_data_mode),
Address::Address24(
@ -226,8 +225,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, pin, NoPin, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(pin)
.with_dma(ctx.dma_channel);
super::execute_read(spi, pin_mirror, 0b0001_0001);
@ -239,8 +238,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, pin, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_miso(pin)
.with_dma(ctx.dma_channel);
super::execute_read(spi, pin_mirror, 0b0010_0010);
@ -252,8 +251,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, NoPin, pin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_sio2(pin)
.with_dma(ctx.dma_channel);
super::execute_read(spi, pin_mirror, 0b0100_0100);
@ -265,8 +264,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, NoPin, NoPin, pin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_sio3(pin)
.with_dma(ctx.dma_channel);
super::execute_read(spi, pin_mirror, 0b1000_1000);
@ -278,8 +277,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, pin, NoPin, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(pin)
.with_dma(ctx.dma_channel);
super::execute_write_read(spi, pin_mirror, 0b0001_0001);
@ -291,8 +290,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, pin, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_miso(pin)
.with_dma(ctx.dma_channel);
super::execute_write_read(spi, pin_mirror, 0b0010_0010);
@ -304,8 +303,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, NoPin, pin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_sio2(pin)
.with_dma(ctx.dma_channel);
super::execute_write_read(spi, pin_mirror, 0b0100_0100);
@ -317,8 +316,8 @@ mod tests {
let [pin, pin_mirror, _] = ctx.gpios;
let pin_mirror = Output::new(pin_mirror, Level::High);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, NoPin, NoPin, NoPin, pin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_sio3(pin)
.with_dma(ctx.dma_channel);
super::execute_write_read(spi, pin_mirror, 0b1000_1000);
@ -341,8 +340,8 @@ mod tests {
.channel0
.set_input_mode(EdgeMode::Hold, EdgeMode::Increment);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, mosi, NoPin, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(mosi)
.with_dma(ctx.dma_channel);
super::execute_write(unit0, unit1, spi, 0b0000_0001, false);
@ -370,8 +369,9 @@ mod tests {
.channel0
.set_input_mode(EdgeMode::Hold, EdgeMode::Increment);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, mosi, gpio, NoPin, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(mosi)
.with_miso(gpio)
.with_dma(ctx.dma_channel);
super::execute_write(unit0, unit1, spi, 0b0000_0010, true);
@ -399,8 +399,9 @@ mod tests {
.channel0
.set_input_mode(EdgeMode::Hold, EdgeMode::Increment);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, mosi, NoPin, gpio, NoPin, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(mosi)
.with_sio2(gpio)
.with_dma(ctx.dma_channel);
super::execute_write(unit0, unit1, spi, 0b0000_0100, true);
@ -428,8 +429,9 @@ mod tests {
.channel0
.set_input_mode(EdgeMode::Hold, EdgeMode::Increment);
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_pins(NoPin, mosi, NoPin, NoPin, gpio, NoPin)
let spi = Spi::new(ctx.spi, 100.kHz(), SpiMode::Mode0)
.with_mosi(mosi)
.with_sio3(gpio)
.with_dma(ctx.dma_channel);
super::execute_write(unit0, unit1, spi, 0b0000_1000, true);

View File

@ -15,8 +15,9 @@ use esp_hal::{
dma::{Dma, DmaDescriptor, DmaPriority, DmaRxBuf, DmaTxBuf},
dma_buffers,
gpio::{Io, Level, NoPin},
peripheral::Peripheral,
prelude::*,
spi::{master::Spi, FullDuplexMode, SpiMode},
spi::{master::Spi, SpiMode},
};
#[cfg(pcnt)]
use esp_hal::{
@ -34,7 +35,7 @@ cfg_if::cfg_if! {
}
struct Context {
spi: Spi<'static, FullDuplexMode>,
spi: Spi<'static>,
dma_channel: DmaChannelCreator,
// Reuse the really large buffer so we don't run out of DRAM with many tests
rx_buffer: &'static mut [u8],
@ -72,11 +73,12 @@ mod tests {
#[cfg(pcnt)]
let mosi_loopback_pcnt = mosi.peripheral_input();
let mosi_loopback = mosi.peripheral_input();
// Need to set miso first so that mosi can overwrite the
// output connection (because we are using the same pin to loop back)
let spi = Spi::new(peripherals.SPI2, 10000.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(mosi_loopback);
.with_miso(unsafe { mosi.clone_unchecked() })
.with_mosi(mosi);
let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(32000);
@ -209,11 +211,11 @@ mod tests {
for i in 1..4 {
dma_rx_buf.as_mut_slice().copy_from_slice(&[5, 5, 5, 5, 5]);
let transfer = spi.dma_read(dma_rx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.read(dma_rx_buf).map_err(|e| e.0).unwrap();
(spi, dma_rx_buf) = transfer.wait();
assert_eq!(dma_rx_buf.as_slice(), &[0, 0, 0, 0, 0]);
let transfer = spi.dma_write(dma_tx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.write(dma_tx_buf).map_err(|e| e.0).unwrap();
(spi, dma_tx_buf) = transfer.wait();
assert_eq!(unit.get_value(), (i * 3 * DMA_BUFFER_SIZE) as _);
}
@ -242,12 +244,12 @@ mod tests {
for i in 1..4 {
dma_rx_buf.as_mut_slice().copy_from_slice(&[5, 5, 5, 5, 5]);
let transfer = spi.dma_read(dma_rx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.read(dma_rx_buf).map_err(|e| e.0).unwrap();
(spi, dma_rx_buf) = transfer.wait();
assert_eq!(dma_rx_buf.as_slice(), &[0, 0, 0, 0, 0]);
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
(spi, (dma_rx_buf, dma_tx_buf)) = transfer.wait();
@ -274,7 +276,7 @@ mod tests {
dma_tx_buf.as_mut_slice()[0] = i as u8;
*dma_tx_buf.as_mut_slice().last_mut().unwrap() = i as u8;
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
@ -300,7 +302,7 @@ mod tests {
.spi
.with_dma(ctx.dma_channel.configure(false, DmaPriority::Priority0));
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
let (spi, (dma_rx_buf, mut dma_tx_buf)) = transfer.wait();
@ -311,7 +313,7 @@ mod tests {
dma_tx_buf.fill(&[0xaa, 0xdd, 0xef, 0xbe]);
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
let (_, (dma_rx_buf, dma_tx_buf)) = transfer.wait();
@ -469,18 +471,18 @@ mod tests {
dma_tx_buf.fill(&[0xde, 0xad, 0xbe, 0xef]);
let transfer = spi.dma_write(dma_tx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.write(dma_tx_buf).map_err(|e| e.0).unwrap();
let (spi, dma_tx_buf) = transfer.wait();
dma_rx_buf.as_mut_slice().fill(0);
let transfer = spi.dma_read(dma_rx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.read(dma_rx_buf).map_err(|e| e.0).unwrap();
let (spi, mut dma_rx_buf) = transfer.wait();
let transfer = spi.dma_write(dma_tx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.write(dma_tx_buf).map_err(|e| e.0).unwrap();
let (spi, _dma_tx_buf) = transfer.wait();
dma_rx_buf.as_mut_slice().fill(0);
let transfer = spi.dma_read(dma_rx_buf).map_err(|e| e.0).unwrap();
let transfer = spi.read(dma_rx_buf).map_err(|e| e.0).unwrap();
let (_, dma_rx_buf) = transfer.wait();
assert_eq!(&[0xff, 0xff, 0xff, 0xff], dma_rx_buf.as_slice());
@ -503,7 +505,7 @@ mod tests {
.with_dma(ctx.dma_channel.configure(false, DmaPriority::Priority0));
let mut transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
@ -526,7 +528,7 @@ mod tests {
.with_dma(ctx.dma_channel.configure(false, DmaPriority::Priority0));
let mut transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
@ -536,7 +538,7 @@ mod tests {
spi.change_bus_frequency(10000.kHz());
let transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();
@ -561,7 +563,7 @@ mod tests {
);
let mut transfer = spi
.dma_transfer(dma_rx_buf, dma_tx_buf)
.transfer(dma_rx_buf, dma_tx_buf)
.map_err(|e| e.0)
.unwrap();

View File

@ -11,8 +11,7 @@ use esp_hal::{
gpio::{Io, Level, Output},
prelude::*,
spi::{
master::{Address, Command, HalfDuplexReadWrite, Spi, SpiDma},
HalfDuplexMode,
master::{Address, Command, Spi, SpiDma},
SpiDataMode,
SpiMode,
},
@ -21,7 +20,7 @@ use esp_hal::{
use hil_test as _;
struct Context {
spi: SpiDma<'static, HalfDuplexMode, Blocking>,
spi: SpiDma<'static, Blocking>,
miso_mirror: Output<'static>,
}
@ -50,7 +49,7 @@ mod tests {
}
}
let spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_miso(miso)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
@ -72,7 +71,7 @@ mod tests {
let mut spi = ctx.spi;
let transfer = spi
.read(
.half_duplex_read(
SpiDataMode::Single,
Command::None,
Address::None,
@ -89,7 +88,7 @@ mod tests {
ctx.miso_mirror.set_high();
let transfer = spi
.read(
.half_duplex_read(
SpiDataMode::Single,
Command::None,
Address::None,
@ -120,7 +119,7 @@ mod tests {
ctx.miso_mirror.set_low();
let mut buffer = [0xAA; DMA_BUFFER_SIZE];
spi.read(
spi.half_duplex_read(
SpiDataMode::Single,
Command::None,
Address::None,
@ -134,7 +133,7 @@ mod tests {
// SPI should read '1's from the MISO pin
ctx.miso_mirror.set_high();
spi.read(
spi.half_duplex_read(
SpiDataMode::Single,
Command::None,
Address::None,

View File

@ -12,8 +12,7 @@ use esp_hal::{
pcnt::{channel::EdgeMode, unit::Unit, Pcnt},
prelude::*,
spi::{
master::{Address, Command, HalfDuplexReadWrite, Spi, SpiDma},
HalfDuplexMode,
master::{Address, Command, Spi, SpiDma},
SpiDataMode,
SpiMode,
},
@ -22,7 +21,7 @@ use esp_hal::{
use hil_test as _;
struct Context {
spi: SpiDma<'static, HalfDuplexMode, Blocking>,
spi: SpiDma<'static, Blocking>,
pcnt_unit: Unit<'static, 0>,
pcnt_source: InputSignal,
}
@ -53,7 +52,7 @@ mod tests {
let mosi_loopback = mosi.peripheral_input();
let spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
@ -84,7 +83,7 @@ mod tests {
dma_tx_buf.fill(&[0b0110_1010; DMA_BUFFER_SIZE]);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -98,7 +97,7 @@ mod tests {
assert_eq!(unit.get_value(), (3 * DMA_BUFFER_SIZE) as _);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -130,7 +129,7 @@ mod tests {
let buffer = [0b0110_1010; DMA_BUFFER_SIZE];
// Write the buffer where each byte has 3 pos edges.
spi.write(
spi.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -141,7 +140,7 @@ mod tests {
assert_eq!(unit.get_value(), (3 * DMA_BUFFER_SIZE) as _);
spi.write(
spi.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,

View File

@ -14,8 +14,7 @@ use esp_hal::{
pcnt::{channel::EdgeMode, unit::Unit, Pcnt},
prelude::*,
spi::{
master::{Address, Command, HalfDuplexReadWrite, Spi, SpiDma},
HalfDuplexMode,
master::{Address, Command, Spi, SpiDma},
SpiDataMode,
SpiMode,
},
@ -39,7 +38,7 @@ macro_rules! dma_alloc_buffer {
}
struct Context {
spi: SpiDma<'static, HalfDuplexMode, Blocking>,
spi: SpiDma<'static, Blocking>,
pcnt_unit: Unit<'static, 0>,
pcnt_source: InputSignal,
}
@ -65,7 +64,7 @@ mod tests {
let mosi_loopback = mosi.peripheral_input();
let spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
@ -99,7 +98,7 @@ mod tests {
// Fill the buffer where each byte has 3 pos edges.
dma_tx_buf.fill(&[0b0110_1010; DMA_BUFFER_SIZE]);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -113,7 +112,7 @@ mod tests {
assert_eq!(unit.get_value(), (3 * DMA_BUFFER_SIZE) as _);
let transfer = spi
.write(
.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -151,7 +150,7 @@ mod tests {
let buffer = [0b0110_1010; DMA_BUFFER_SIZE];
// Write the buffer where each byte has 3 pos edges.
spi.write(
spi.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,
@ -162,7 +161,7 @@ mod tests {
assert_eq!(unit.get_value(), (3 * DMA_BUFFER_SIZE) as _);
spi.write(
spi.half_duplex_write(
SpiDataMode::Single,
Command::None,
Address::None,

View File

@ -12,7 +12,7 @@ use esp_hal::{
dma::{Dma, DmaPriority},
dma_buffers,
gpio::{interconnect::InputSignal, Io, Level, Output, PeripheralInput},
spi::{slave::Spi, FullDuplexMode, SpiMode},
spi::{slave::Spi, SpiMode},
};
use hil_test as _;
@ -25,7 +25,7 @@ cfg_if::cfg_if! {
}
struct Context {
spi: Spi<'static, FullDuplexMode>,
spi: Spi<'static>,
dma_channel: DmaChannelCreator,
bitbang_spi: BitbangSpi,
}
@ -170,7 +170,7 @@ mod tests {
}
slave_receive.fill(0xFF);
let transfer = spi.dma_transfer(slave_receive, &slave_send).unwrap();
let transfer = spi.transfer(slave_receive, &slave_send).unwrap();
ctx.bitbang_spi.transfer_buf(master_receive, master_send);