Skip to content

Commit

Permalink
SetConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 4, 2024
1 parent 40da1e3 commit 7f8e7de
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 131 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `gpio::Input::{split(), into_peripheral_output()}` when used with output pins. (#2418)
- `gpio::Output::peripheral_input()` (#2418)
- `spi::master::Config` and `{Spi, SpiDma, SpiDmaBus}::apply_config` (#2448)
- `embassy_embedded_hal::SetConfig` is now implemented for `{Spi, SpiDma, SpiDmaBus}` (#2448)

### Changed

Expand Down
1 change: 1 addition & 0 deletions esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmt = { version = "0.3.8", optional = true }
delegate = "0.12.0"
digest = { version = "0.10.7", default-features = false, optional = true }
document-features = "0.2.10"
embassy-embedded-hal = "0.2.0"
embassy-futures = "0.1.1"
embassy-sync = "0.6.0"
embassy-usb-driver = { version = "0.1.0", optional = true }
Expand Down
14 changes: 14 additions & 0 deletions esp-hal/MIGRATING-0.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ the GPIO drivers (`Input`, `Output`, `OutputOpenDrain` and `Flex`) instead.
- bit order: MSB first
- mode: SPI mode 0
- There are new constructors (`new_with_config`, `new_typed_with_config`) and a new `apply_config` method to apply custom configuration.

```diff
-use esp_hal::spi::{master::Spi, SpiMode};
+use esp_hal::spi::{master::{Config, Spi}, SpiMode};
-Spi::new(SPI2, 100.kHz(), SpiMode::Mode1);
+Spi::new_with_config(
+ SPI2,
+ Config {
+ frequency: 100.kHz(),
+ mode: SpiMode::Mode0,
+ ..Config::default()
+ },
+)
```
11 changes: 7 additions & 4 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#![doc = crate::before_snippet!()]
//! # use esp_hal::dma_buffers;
//! # use esp_hal::gpio::Io;
//! # use esp_hal::spi::{master::Spi, SpiMode};
//! # use esp_hal::spi::{master::{Config, Spi}, SpiMode};
//! # use esp_hal::dma::{Dma, DmaPriority};
//! let dma = Dma::new(peripherals.DMA);
#![cfg_attr(any(esp32, esp32s2), doc = "let dma_channel = dma.spi2channel;")]
Expand All @@ -30,10 +30,13 @@
//! let mosi = io.pins.gpio4;
//! let cs = io.pins.gpio5;
//!
//! let mut spi = Spi::new(
//! let mut spi = Spi::new_with_config(
//! peripherals.SPI2,
//! 100.kHz(),
//! SpiMode::Mode0,
//! Config {
//! frequency: 100.kHz(),
//! mode: SpiMode::Mode0,
//! ..Config::default()
//! },
//! )
//! .with_sck(sclk)
//! .with_mosi(mosi)
Expand Down
61 changes: 54 additions & 7 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
//! ### Shared SPI access
//!
//! If you have multiple devices on the same SPI bus that each have their own CS
//! line, you may want to have a look at the implementations provided by
//! [`embedded-hal-bus`] and [`embassy-embedded-hal`].
//! line (and optionally, configuration), you may want to have a look at the
//! implementations provided by [`embedded-hal-bus`] and
//! [`embassy-embedded-hal`].
//!
//! ## Usage
//!
Expand All @@ -38,18 +39,21 @@
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::spi::SpiMode;
//! # use esp_hal::spi::master::Spi;
//! # use esp_hal::spi::master::{Config, Spi};
//! # use esp_hal::gpio::Io;
//! # let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
//! let sclk = io.pins.gpio0;
//! let miso = io.pins.gpio2;
//! let mosi = io.pins.gpio1;
//! let cs = io.pins.gpio5;
//!
//! let mut spi = Spi::new(
//! let mut spi = Spi::new_with_config(
//! peripherals.SPI2,
//! 100.kHz(),
//! SpiMode::Mode0,
//! Config {
//! frequency: 100.kHz(),
//! mode: SpiMode::Mode0,
//! ..Config::default()
//! },
//! )
//! .with_sck(sclk)
//! .with_mosi(mosi)
Expand All @@ -61,9 +65,10 @@
//! [`embedded-hal-bus`]: https://docs.rs/embedded-hal-bus/latest/embedded_hal_bus/spi/index.html
//! [`embassy-embedded-hal`]: https://docs.embassy.dev/embassy-embedded-hal/git/default/shared_bus/index.html

use core::marker::PhantomData;
use core::{convert::Infallible, marker::PhantomData};

pub use dma::*;
use embassy_embedded_hal::SetConfig;
#[cfg(gdma)]
use enumset::EnumSet;
#[cfg(gdma)]
Expand Down Expand Up @@ -677,6 +682,20 @@ where
}
}

impl<M, T> SetConfig for Spi<'_, M, T>
where
T: Instance,
M: Mode,
{
type Config = Config;
type ConfigError = Infallible;

fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.apply_config(config);
Ok(())
}
}

impl<'d, M, T> Spi<'d, M, T>
where
T: QspiInstance,
Expand Down Expand Up @@ -1204,6 +1223,20 @@ mod dma {
}
}

impl<M, T> SetConfig for SpiDma<'_, M, T>
where
T: Instance,
M: Mode,
{
type Config = Config;
type ConfigError = Infallible;

fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.apply_config(config);
Ok(())
}
}

/// A structure representing a DMA transfer for SPI.
///
/// This structure holds references to the SPI instance, DMA buffers, and
Expand Down Expand Up @@ -1767,6 +1800,20 @@ mod dma {
}
}

impl<M, T> SetConfig for SpiDmaBus<'_, M, T>
where
T: Instance,
M: Mode,
{
type Config = Config;
type ConfigError = Infallible;

fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.apply_config(config);
Ok(())
}
}

impl<T> embedded_hal_02::blocking::spi::Transfer<u8> for SpiDmaBus<'_, Blocking, T>
where
T: Instance,
Expand Down
3 changes: 2 additions & 1 deletion esp-hal/src/spi/slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
//! let cs = io.pins.gpio3;
//!
//! let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) =
//! dma_buffers!(32000); let mut spi = Spi::new(
//! dma_buffers!(32000);
//! let mut spi = Spi::new(
//! peripherals.SPI2,
//! sclk,
//! mosi,
Expand Down
28 changes: 19 additions & 9 deletions examples/src/bin/embassy_spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use esp_hal::{
dma_buffers,
gpio::Io,
prelude::*,
spi::{master::Spi, SpiMode},
spi::{
master::{Config, Spi},
SpiMode,
},
timer::timg::TimerGroup,
};

Expand Down Expand Up @@ -58,14 +61,21 @@ async fn main(_spawner: Spawner) {
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();

let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0))
.with_buffers(dma_rx_buf, dma_tx_buf)
.into_async();
let mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0))
.with_buffers(dma_rx_buf, dma_tx_buf)
.into_async();

let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7];
loop {
Expand Down
25 changes: 16 additions & 9 deletions examples/src/bin/qspi_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use esp_hal::{
gpio::Io,
prelude::*,
spi::{
master::{Address, Command, Spi},
master::{Address, Command, Config, Spi},
SpiDataMode,
SpiMode,
},
Expand Down Expand Up @@ -79,14 +79,21 @@ 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(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 mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.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();

Expand Down
23 changes: 15 additions & 8 deletions examples/src/bin/spi_halfduplex_read_manufacturer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use esp_hal::{
gpio::Io,
prelude::*,
spi::{
master::{Address, Command, Spi},
master::{Address, Command, Config, Spi},
SpiDataMode,
SpiMode,
},
Expand Down Expand Up @@ -63,13 +63,20 @@ fn main() -> ! {
}
}

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 mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_sio2(sio2)
.with_sio3(sio3)
.with_cs(cs);

let delay = Delay::new();

Expand Down
22 changes: 16 additions & 6 deletions examples/src/bin/spi_loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use esp_hal::{
gpio::Io,
peripheral::Peripheral,
prelude::*,
spi::{master::Spi, SpiMode},
spi::{
master::{Config, Spi},
SpiMode,
},
};
use esp_println::println;

Expand All @@ -36,11 +39,18 @@ fn main() -> ! {

let miso = unsafe { miso_mosi.clone_unchecked() };

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 mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.with_sck(sclk)
.with_mosi(miso_mosi)
.with_miso(miso)
.with_cs(cs);

let delay = Delay::new();

Expand Down
24 changes: 17 additions & 7 deletions examples/src/bin/spi_loopback_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use esp_hal::{
dma_buffers,
gpio::Io,
prelude::*,
spi::{master::Spi, SpiMode},
spi::{
master::{Config, Spi},
SpiMode,
},
};
use esp_println::println;

Expand Down Expand Up @@ -53,12 +56,19 @@ 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(peripherals.SPI2, 100.kHz(), SpiMode::Mode0)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
let mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.with_sck(sclk)
.with_mosi(mosi)
.with_miso(miso)
.with_cs(cs)
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));

let delay = Delay::new();

Expand Down
Loading

0 comments on commit 7f8e7de

Please sign in to comment.