Skip to content

Commit

Permalink
Adding new_without_sclk SpiDriver constructor in SPI driver (#440)
Browse files Browse the repository at this point in the history
* added new_without_sclk SpiDriver constructor for more flexibility

* cargo fmt

* update CHANGELOG.md
  • Loading branch information
okhsunrog authored Jun 18, 2024
1 parent 2dfbf41 commit f237ab1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* adc: legacy adc driver is now behind a feature flag `adc-oneshot-legacy` starting with >= ESP-IDF v5.0. For justification, look at https://github.com/espressif/esp-idf/issues/13938. (#433)
* spi: cs_pre/post_trans_delay() config option included. (#266)
### Added
* spi: added new_without_sclk SpiDriver constructor (#440)
* rmt: `Symbol` now derives `Clone` and `Copy` (#386)
* reset: restart() function (#383)
* pcnt: Can now be used with esp32c6. (#407)
Expand Down
32 changes: 26 additions & 6 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ use esp_idf_sys::*;
use heapless::Deque;

use crate::delay::{self, Ets, BLOCK};
use crate::gpio::{AnyOutputPin, InputPin, Level, Output, OutputMode, OutputPin, PinDriver};
use crate::gpio::{
AnyIOPin, AnyOutputPin, InputPin, Level, Output, OutputMode, OutputPin, PinDriver,
};
use crate::interrupt::asynch::HalIsrNotification;
use crate::interrupt::InterruptType;
use crate::peripheral::Peripheral;
Expand Down Expand Up @@ -376,7 +378,7 @@ impl<'d> SpiDriver<'d> {
sdi: Option<impl Peripheral<P = crate::gpio::Gpio8> + 'd>,
config: &config::DriverConfig,
) -> Result<Self, EspError> {
let max_transfer_size = Self::new_internal(SPI1::device(), sclk, sdo, sdi, config)?;
let max_transfer_size = Self::new_internal(SPI1::device(), Some(sclk), sdo, sdi, config)?;

Ok(Self {
host: SPI1::device() as _,
Expand All @@ -394,7 +396,24 @@ impl<'d> SpiDriver<'d> {
sdi: Option<impl Peripheral<P = impl InputPin> + 'd>,
config: &config::DriverConfig,
) -> Result<Self, EspError> {
let max_transfer_size = Self::new_internal(SPI::device(), sclk, sdo, sdi, config)?;
let max_transfer_size = Self::new_internal(SPI::device(), Some(sclk), sdo, sdi, config)?;

Ok(Self {
host: SPI::device() as _,
max_transfer_size,
bus_async_lock: Mutex::new(()),
_p: PhantomData,
})
}

pub fn new_without_sclk<SPI: SpiAnyPins>(
_spi: impl Peripheral<P = SPI> + 'd,
sdo: impl Peripheral<P = impl OutputPin> + 'd,
sdi: Option<impl Peripheral<P = impl InputPin> + 'd>,
config: &config::DriverConfig,
) -> Result<Self, EspError> {
let max_transfer_size =
Self::new_internal(SPI::device(), Option::<AnyIOPin>::None, sdo, sdi, config)?;

Ok(Self {
host: SPI::device() as _,
Expand All @@ -410,21 +429,22 @@ impl<'d> SpiDriver<'d> {

fn new_internal(
host: spi_host_device_t,
sclk: impl Peripheral<P = impl OutputPin> + 'd,
sclk: Option<impl Peripheral<P = impl OutputPin> + 'd>,
sdo: impl Peripheral<P = impl OutputPin> + 'd,
sdi: Option<impl Peripheral<P = impl InputPin> + 'd>,
config: &config::DriverConfig,
) -> Result<usize, EspError> {
crate::into_ref!(sclk, sdo);
let sdo = sdo.into_ref();
let sdi = sdi.map(|sdi| sdi.into_ref());
let sclk = sclk.map(|sclk| sclk.into_ref());

let max_transfer_sz = config.dma.max_transfer_size();
let dma_chan: spi_dma_chan_t = config.dma.into();

#[allow(clippy::needless_update)]
let bus_config = spi_bus_config_t {
flags: SPICOMMON_BUSFLAG_MASTER,
sclk_io_num: sclk.pin(),
sclk_io_num: sclk.as_ref().map_or(-1, |p| p.pin()),

data4_io_num: -1,
data5_io_num: -1,
Expand Down

0 comments on commit f237ab1

Please sign in to comment.