diff options
Diffstat (limited to 'embassy-rp/src')
| -rw-r--r-- | embassy-rp/src/i2c.rs | 107 | ||||
| -rw-r--r-- | embassy-rp/src/spi.rs | 1 |
2 files changed, 30 insertions, 78 deletions
diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index e48e16d81..40e85c66f 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs | |||
| @@ -490,14 +490,14 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { | |||
| 490 | } | 490 | } |
| 491 | } | 491 | } |
| 492 | 492 | ||
| 493 | fn read_blocking_internal(&mut self, buffer: &mut [u8], restart: bool, send_stop: bool) -> Result<(), Error> { | 493 | fn read_blocking_internal(&mut self, read: &mut [u8], restart: bool, send_stop: bool) -> Result<(), Error> { |
| 494 | if buffer.is_empty() { | 494 | if read.is_empty() { |
| 495 | return Err(Error::InvalidReadBufferLength); | 495 | return Err(Error::InvalidReadBufferLength); |
| 496 | } | 496 | } |
| 497 | 497 | ||
| 498 | let p = T::regs(); | 498 | let p = T::regs(); |
| 499 | let lastindex = buffer.len() - 1; | 499 | let lastindex = read.len() - 1; |
| 500 | for (i, byte) in buffer.iter_mut().enumerate() { | 500 | for (i, byte) in read.iter_mut().enumerate() { |
| 501 | let first = i == 0; | 501 | let first = i == 0; |
| 502 | let last = i == lastindex; | 502 | let last = i == lastindex; |
| 503 | 503 | ||
| @@ -524,15 +524,15 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { | |||
| 524 | Ok(()) | 524 | Ok(()) |
| 525 | } | 525 | } |
| 526 | 526 | ||
| 527 | fn write_blocking_internal(&mut self, bytes: &[u8], send_stop: bool) -> Result<(), Error> { | 527 | fn write_blocking_internal(&mut self, write: &[u8], send_stop: bool) -> Result<(), Error> { |
| 528 | if bytes.is_empty() { | 528 | if write.is_empty() { |
| 529 | return Err(Error::InvalidWriteBufferLength); | 529 | return Err(Error::InvalidWriteBufferLength); |
| 530 | } | 530 | } |
| 531 | 531 | ||
| 532 | let p = T::regs(); | 532 | let p = T::regs(); |
| 533 | 533 | ||
| 534 | for (i, byte) in bytes.iter().enumerate() { | 534 | for (i, byte) in write.iter().enumerate() { |
| 535 | let last = i == bytes.len() - 1; | 535 | let last = i == write.len() - 1; |
| 536 | 536 | ||
| 537 | // NOTE(unsafe) We have &mut self | 537 | // NOTE(unsafe) We have &mut self |
| 538 | unsafe { | 538 | unsafe { |
| @@ -572,21 +572,21 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { | |||
| 572 | // Blocking public API | 572 | // Blocking public API |
| 573 | // ========================= | 573 | // ========================= |
| 574 | 574 | ||
| 575 | pub fn blocking_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error> { | 575 | pub fn blocking_read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Error> { |
| 576 | Self::setup(address.into())?; | 576 | Self::setup(address.into())?; |
| 577 | self.read_blocking_internal(buffer, true, true) | 577 | self.read_blocking_internal(read, true, true) |
| 578 | // Automatic Stop | 578 | // Automatic Stop |
| 579 | } | 579 | } |
| 580 | 580 | ||
| 581 | pub fn blocking_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Error> { | 581 | pub fn blocking_write(&mut self, address: u8, write: &[u8]) -> Result<(), Error> { |
| 582 | Self::setup(address.into())?; | 582 | Self::setup(address.into())?; |
| 583 | self.write_blocking_internal(bytes, true) | 583 | self.write_blocking_internal(write, true) |
| 584 | } | 584 | } |
| 585 | 585 | ||
| 586 | pub fn blocking_write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { | 586 | pub fn blocking_write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error> { |
| 587 | Self::setup(address.into())?; | 587 | Self::setup(address.into())?; |
| 588 | self.write_blocking_internal(bytes, false)?; | 588 | self.write_blocking_internal(write, false)?; |
| 589 | self.read_blocking_internal(buffer, true, true) | 589 | self.read_blocking_internal(read, true, true) |
| 590 | // Automatic Stop | 590 | // Automatic Stop |
| 591 | } | 591 | } |
| 592 | } | 592 | } |
| @@ -644,48 +644,22 @@ mod eh1 { | |||
| 644 | } | 644 | } |
| 645 | 645 | ||
| 646 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::I2c for I2c<'d, T, M> { | 646 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::I2c for I2c<'d, T, M> { |
| 647 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | 647 | fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { |
| 648 | self.blocking_read(address, buffer) | 648 | self.blocking_read(address, read) |
| 649 | } | 649 | } |
| 650 | 650 | ||
| 651 | fn write(&mut self, address: u8, buffer: &[u8]) -> Result<(), Self::Error> { | 651 | fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { |
| 652 | self.blocking_write(address, buffer) | 652 | self.blocking_write(address, write) |
| 653 | } | ||
| 654 | |||
| 655 | fn write_iter<B>(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> | ||
| 656 | where | ||
| 657 | B: IntoIterator<Item = u8>, | ||
| 658 | { | ||
| 659 | let mut peekable = bytes.into_iter().peekable(); | ||
| 660 | Self::setup(address.into())?; | ||
| 661 | |||
| 662 | while let Some(tx) = peekable.next() { | ||
| 663 | self.write_blocking_internal(&[tx], peekable.peek().is_none())?; | ||
| 664 | } | ||
| 665 | Ok(()) | ||
| 666 | } | ||
| 667 | |||
| 668 | fn write_iter_read<B>(&mut self, address: u8, bytes: B, buffer: &mut [u8]) -> Result<(), Self::Error> | ||
| 669 | where | ||
| 670 | B: IntoIterator<Item = u8>, | ||
| 671 | { | ||
| 672 | let peekable = bytes.into_iter().peekable(); | ||
| 673 | Self::setup(address.into())?; | ||
| 674 | |||
| 675 | for tx in peekable { | ||
| 676 | self.write_blocking_internal(&[tx], false)? | ||
| 677 | } | ||
| 678 | self.read_blocking_internal(buffer, true, true) | ||
| 679 | } | 653 | } |
| 680 | 654 | ||
| 681 | fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { | 655 | fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { |
| 682 | self.blocking_write_read(address, wr_buffer, rd_buffer) | 656 | self.blocking_write_read(address, write, read) |
| 683 | } | 657 | } |
| 684 | 658 | ||
| 685 | fn transaction<'a>( | 659 | fn transaction( |
| 686 | &mut self, | 660 | &mut self, |
| 687 | address: u8, | 661 | address: u8, |
| 688 | operations: &mut [embedded_hal_1::i2c::Operation<'a>], | 662 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], |
| 689 | ) -> Result<(), Self::Error> { | 663 | ) -> Result<(), Self::Error> { |
| 690 | Self::setup(address.into())?; | 664 | Self::setup(address.into())?; |
| 691 | for i in 0..operations.len() { | 665 | for i in 0..operations.len() { |
| @@ -697,22 +671,6 @@ mod eh1 { | |||
| 697 | } | 671 | } |
| 698 | Ok(()) | 672 | Ok(()) |
| 699 | } | 673 | } |
| 700 | |||
| 701 | fn transaction_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> | ||
| 702 | where | ||
| 703 | O: IntoIterator<Item = embedded_hal_1::i2c::Operation<'a>>, | ||
| 704 | { | ||
| 705 | Self::setup(address.into())?; | ||
| 706 | let mut peekable = operations.into_iter().peekable(); | ||
| 707 | while let Some(operation) = peekable.next() { | ||
| 708 | let last = peekable.peek().is_none(); | ||
| 709 | match operation { | ||
| 710 | embedded_hal_1::i2c::Operation::Read(buf) => self.read_blocking_internal(buf, false, last)?, | ||
| 711 | embedded_hal_1::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, | ||
| 712 | } | ||
| 713 | } | ||
| 714 | Ok(()) | ||
| 715 | } | ||
| 716 | } | 674 | } |
| 717 | } | 675 | } |
| 718 | #[cfg(all(feature = "unstable-traits", feature = "nightly"))] | 676 | #[cfg(all(feature = "unstable-traits", feature = "nightly"))] |
| @@ -727,36 +685,29 @@ mod nightly { | |||
| 727 | A: AddressMode + Into<u16> + 'static, | 685 | A: AddressMode + Into<u16> + 'static, |
| 728 | T: Instance + 'd, | 686 | T: Instance + 'd, |
| 729 | { | 687 | { |
| 730 | async fn read<'a>(&'a mut self, address: A, read: &'a mut [u8]) -> Result<(), Self::Error> { | 688 | async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> { |
| 731 | let addr: u16 = address.into(); | 689 | let addr: u16 = address.into(); |
| 732 | 690 | ||
| 733 | Self::setup(addr)?; | 691 | Self::setup(addr)?; |
| 734 | self.read_async_internal(read, false, true).await | 692 | self.read_async_internal(read, false, true).await |
| 735 | } | 693 | } |
| 736 | 694 | ||
| 737 | async fn write<'a>(&'a mut self, address: A, write: &'a [u8]) -> Result<(), Self::Error> { | 695 | async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> { |
| 738 | let addr: u16 = address.into(); | 696 | let addr: u16 = address.into(); |
| 739 | 697 | ||
| 740 | Self::setup(addr)?; | 698 | Self::setup(addr)?; |
| 741 | self.write_async_internal(write.iter().copied(), true).await | 699 | self.write_async_internal(write.iter().copied(), true).await |
| 742 | } | 700 | } |
| 743 | async fn write_read<'a>( | 701 | |
| 744 | &'a mut self, | 702 | async fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { |
| 745 | address: A, | ||
| 746 | write: &'a [u8], | ||
| 747 | read: &'a mut [u8], | ||
| 748 | ) -> Result<(), Self::Error> { | ||
| 749 | let addr: u16 = address.into(); | 703 | let addr: u16 = address.into(); |
| 750 | 704 | ||
| 751 | Self::setup(addr)?; | 705 | Self::setup(addr)?; |
| 752 | self.write_async_internal(write.iter().cloned(), false).await?; | 706 | self.write_async_internal(write.iter().cloned(), false).await?; |
| 753 | self.read_async_internal(read, false, true).await | 707 | self.read_async_internal(read, false, true).await |
| 754 | } | 708 | } |
| 755 | async fn transaction<'a, 'b>( | 709 | |
| 756 | &'a mut self, | 710 | async fn transaction(&mut self, address: A, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> { |
| 757 | address: A, | ||
| 758 | operations: &'a mut [Operation<'b>], | ||
| 759 | ) -> Result<(), Self::Error> { | ||
| 760 | let addr: u16 = address.into(); | 711 | let addr: u16 = address.into(); |
| 761 | 712 | ||
| 762 | let mut iterator = operations.iter_mut(); | 713 | let mut iterator = operations.iter_mut(); |
diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs index ebd621ecf..742a35d49 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs | |||
| @@ -19,6 +19,7 @@ pub enum Error { | |||
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | #[non_exhaustive] | 21 | #[non_exhaustive] |
| 22 | #[derive(Clone)] | ||
| 22 | pub struct Config { | 23 | pub struct Config { |
| 23 | pub frequency: u32, | 24 | pub frequency: u32, |
| 24 | pub phase: Phase, | 25 | pub phase: Phase, |
