From adfd47020f0874a1963a802e2d636dfc64ede218 Mon Sep 17 00:00:00 2001 From: Frederick Date: Fri, 18 Nov 2022 18:23:04 +0000 Subject: [PATCH 1/6] allows to specify spi transfer_size chunk length --- src/spi.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/spi.rs b/src/spi.rs index 69cad809176..126c1e76637 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -155,6 +155,16 @@ pub mod config { } } + #[derive(Copy, Clone)] + pub enum Chunk { + EightBits = 1, + SixteenBits = 2, + TwentyFourBits = 3, + ThirtyTwoBits = 4, + FourtyEightBits = 6, + SixtyFourBits = 8, + } + /// SPI Device configuration #[derive(Copy, Clone)] pub struct Config { @@ -167,6 +177,10 @@ pub mod config { pub write_only: bool, pub duplex: Duplex, pub cs_active_high: bool, + /// How many bits should be in one transfer unit e.g 8bit 32bit. + /// A transfer unit is an continoues clkout signal without a gap. + /// Some Devices can only work with 8bit transfer units + pub chunk_size: Chunk, } impl Config { @@ -200,6 +214,11 @@ pub mod config { self.cs_active_high = true; self } + + pub fn transfer_by_chunk(mut self, chunk_size: Chunk) -> Self { + self.chunk_size = chunk_size; + self + } } impl Default for Config { @@ -210,6 +229,7 @@ pub mod config { write_only: false, cs_active_high: false, duplex: Duplex::Full, + chunk_size: Chunk::SixtyFourBits, } } } @@ -342,7 +362,8 @@ impl<'d> SpiBus for SpiBusDriver<'d> { pub struct SpiDriver<'d> { host: u8, - max_transfer_size: usize, + // max_transfer_size: usize, + dma: Dma, _p: PhantomData<&'d mut ()>, } @@ -379,7 +400,7 @@ impl<'d> SpiDriver<'d> { Ok(Self { host: SPI::device() as _, - max_transfer_size, + dma, _p: PhantomData, }) } @@ -464,6 +485,7 @@ pub struct SpiDeviceDriver<'d, T> { handle: spi_device_handle_t, driver: T, with_cs_pin: bool, + transfer_unit_size: u8, _p: PhantomData<&'d ()>, } @@ -505,6 +527,9 @@ where ) -> Result { let cs = cs.map(|cs| cs.into_ref().pin()).unwrap_or(-1); + println!("Current chunk_size: {:?}", config.chunk_size as u8); + let transfer_unit_size = config.chunk_size as u8; + let conf = spi_device_interface_config_t { spics_io_num: cs, clock_speed_hz: config.baudrate.0 as i32, @@ -532,6 +557,7 @@ where handle, driver, with_cs_pin: cs >= 0, + transfer_unit_size, _p: PhantomData, }) } @@ -548,7 +574,11 @@ where E: From, { // if DMA used -> get trans length info from driver - let trans_len = self.driver.borrow().max_transfer_size; + let trans_len = if self.driver.borrow().dma == Dma::Disabled { + self.transfer_unit_size as usize + } else { + self.driver.borrow().dma.max_transfer_size() + }; let mut bus = SpiBusDriver { handle: self.handle, @@ -630,9 +660,12 @@ where fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { let _lock = self.lock_bus()?; - let mut chunks = words - .chunks_mut(self.driver.borrow().max_transfer_size) - .peekable(); + let trans_len = if self.driver.borrow().dma == Dma::Disabled { + self.transfer_unit_size as usize + } else { + self.driver.borrow().dma.max_transfer_size() + }; + let mut chunks = words.chunks_mut(trans_len).peekable(); while let Some(chunk) = chunks.next() { let ptr = chunk.as_mut_ptr(); @@ -652,9 +685,12 @@ where fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { let _lock = self.lock_bus()?; - let mut chunks = words - .chunks(self.driver.borrow().max_transfer_size) - .peekable(); + let trans_len = if self.driver.borrow().dma == Dma::Disabled { + self.transfer_unit_size as usize + } else { + self.driver.borrow().dma.max_transfer_size() + }; + let mut chunks = words.chunks(trans_len).peekable(); while let Some(chunk) = chunks.next() { polling_transmit( @@ -941,11 +977,12 @@ fn polling_transmit( // This unfortunately means that this implementation is incorrect for esp-idf < 4.4. // The CS pin should be kept active through transactions. #[cfg(not(esp_idf_version = "4.3"))] - let flags = if _keep_cs_active { + let mut flags = if _keep_cs_active { SPI_TRANS_CS_KEEP_ACTIVE } else { 0 }; + //flags = flags | SPI_TRANS_MODE_OCT; let mut transaction = spi_transaction_t { flags, From 5e4b5d2dfa0593f9c22e3fe85b4d1f718b9a31da Mon Sep 17 00:00:00 2001 From: Frederick Date: Fri, 18 Nov 2022 18:56:15 +0000 Subject: [PATCH 2/6] cleanup + fmt --- src/spi.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/spi.rs b/src/spi.rs index 126c1e76637..db7263a6f78 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -396,7 +396,7 @@ impl<'d> SpiDriver<'d> { sdi: Option + 'd>, dma: Dma, ) -> Result { - let max_transfer_size = Self::new_internal(SPI::device(), sclk, sdo, sdi, dma)?; + Self::new_internal(SPI::device(), sclk, sdo, sdi, dma)?; Ok(Self { host: SPI::device() as _, @@ -415,11 +415,10 @@ impl<'d> SpiDriver<'d> { sdo: impl Peripheral

+ 'd, sdi: Option + 'd>, dma: Dma, - ) -> Result { + ) -> Result<(), EspError> { crate::into_ref!(sclk, sdo); let sdi = sdi.map(|sdi| sdi.into_ref()); - let max_transfer_sz = dma.max_transfer_size(); let dma_chan: spi_dma_chan_t = dma.into(); #[cfg(not(esp_idf_version = "4.3"))] @@ -447,7 +446,7 @@ impl<'d> SpiDriver<'d> { quadhd_io_num: -1, //data3_io_num: -1, }, - max_transfer_sz: max_transfer_sz as i32, + max_transfer_sz: dma.max_transfer_size() as i32, ..Default::default() }; @@ -467,7 +466,7 @@ impl<'d> SpiDriver<'d> { esp!(unsafe { spi_bus_initialize(host, &bus_config, dma_chan) })?; - Ok(max_transfer_sz) + Ok(()) } } @@ -527,7 +526,6 @@ where ) -> Result { let cs = cs.map(|cs| cs.into_ref().pin()).unwrap_or(-1); - println!("Current chunk_size: {:?}", config.chunk_size as u8); let transfer_unit_size = config.chunk_size as u8; let conf = spi_device_interface_config_t { @@ -982,7 +980,6 @@ fn polling_transmit( } else { 0 }; - //flags = flags | SPI_TRANS_MODE_OCT; let mut transaction = spi_transaction_t { flags, From fbe56c7cd7f11e9c0d597eb7b936d32a7ad11ad7 Mon Sep 17 00:00:00 2001 From: Frederick Date: Fri, 18 Nov 2022 19:15:51 +0000 Subject: [PATCH 3/6] make clippy happy --- src/spi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spi.rs b/src/spi.rs index db7263a6f78..384c81b204e 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -975,7 +975,7 @@ fn polling_transmit( // This unfortunately means that this implementation is incorrect for esp-idf < 4.4. // The CS pin should be kept active through transactions. #[cfg(not(esp_idf_version = "4.3"))] - let mut flags = if _keep_cs_active { + let flags = if _keep_cs_active { SPI_TRANS_CS_KEEP_ACTIVE } else { 0 From e04a179b89cefdbb098a5187e0636db6ad6d1bf2 Mon Sep 17 00:00:00 2001 From: Frederick Date: Fri, 18 Nov 2022 19:36:06 +0000 Subject: [PATCH 4/6] fix SPI1 + esp_idf 4.3 --- src/spi.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/spi.rs b/src/spi.rs index 384c81b204e..0655e89256f 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -362,7 +362,6 @@ impl<'d> SpiBus for SpiBusDriver<'d> { pub struct SpiDriver<'d> { host: u8, - // max_transfer_size: usize, dma: Dma, _p: PhantomData<&'d mut ()>, } @@ -383,7 +382,7 @@ impl<'d> SpiDriver<'d> { Ok(Self { host: SPI1::device() as _, - max_transfer_size, + dma, _p: PhantomData, }) } @@ -460,7 +459,7 @@ impl<'d> SpiDriver<'d> { quadwp_io_num: -1, quadhd_io_num: -1, - max_transfer_sz: max_transfer_sz as i32, + max_transfer_sz: dma.max_transfer_size() as i32, ..Default::default() }; From 409e2984e53104740a11a360f8252345853a67b8 Mon Sep 17 00:00:00 2001 From: Frederick Date: Fri, 18 Nov 2022 19:52:46 +0000 Subject: [PATCH 5/6] fix spi1 --- src/spi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spi.rs b/src/spi.rs index 0655e89256f..c6d7c9374e6 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -378,7 +378,7 @@ impl<'d> SpiDriver<'d> { sdi: Option + 'd>, dma: Dma, ) -> Result { - let max_transfer_size = Self::new_internal(SPI1::device(), sclk, sdo, sdi, dma)?; + Self::new_internal(SPI1::device(), sclk, sdo, sdi, dma)?; Ok(Self { host: SPI1::device() as _, From 4ac5dc84b86597e8dff5892c1afd75c4b8bffdbc Mon Sep 17 00:00:00 2001 From: Frederick Date: Mon, 12 Dec 2022 13:19:47 +0000 Subject: [PATCH 6/6] backw. comp. -> default chunk to None (64 byte) --- src/spi.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/spi.rs b/src/spi.rs index 0976c938c68..65e8de80423 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -163,6 +163,7 @@ pub mod config { ThirtyTwoBits = 4, FourtyEightBits = 6, SixtyFourBits = 8, + None = 64, } /// SPI Device configuration @@ -229,7 +230,7 @@ pub mod config { write_only: false, cs_active_high: false, duplex: Duplex::Full, - chunk_size: Chunk::SixtyFourBits, + chunk_size: Chunk::None, } } }