diff options
Diffstat (limited to 'embassy-rp/src/i2c.rs')
| -rw-r--r-- | embassy-rp/src/i2c.rs | 230 |
1 files changed, 110 insertions, 120 deletions
diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index 4fe4b27eb..15095236a 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs | |||
| @@ -588,167 +588,157 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { | |||
| 588 | } | 588 | } |
| 589 | } | 589 | } |
| 590 | 590 | ||
| 591 | mod eh02 { | 591 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Read for I2c<'d, T, M> { |
| 592 | use super::*; | 592 | type Error = Error; |
| 593 | 593 | ||
| 594 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Read for I2c<'d, T, M> { | 594 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 595 | type Error = Error; | 595 | self.blocking_read(address, buffer) |
| 596 | |||
| 597 | fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { | ||
| 598 | self.blocking_read(address, buffer) | ||
| 599 | } | ||
| 600 | } | 596 | } |
| 597 | } | ||
| 601 | 598 | ||
| 602 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Write for I2c<'d, T, M> { | 599 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Write for I2c<'d, T, M> { |
| 603 | type Error = Error; | 600 | type Error = Error; |
| 604 | 601 | ||
| 605 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { | 602 | fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { |
| 606 | self.blocking_write(address, bytes) | 603 | self.blocking_write(address, bytes) |
| 607 | } | ||
| 608 | } | 604 | } |
| 605 | } | ||
| 609 | 606 | ||
| 610 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T, M> { | 607 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T, M> { |
| 611 | type Error = Error; | 608 | type Error = Error; |
| 612 | 609 | ||
| 613 | fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { | 610 | fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 614 | self.blocking_write_read(address, bytes, buffer) | 611 | self.blocking_write_read(address, bytes, buffer) |
| 615 | } | ||
| 616 | } | 612 | } |
| 613 | } | ||
| 617 | 614 | ||
| 618 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Transactional for I2c<'d, T, M> { | 615 | impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Transactional for I2c<'d, T, M> { |
| 619 | type Error = Error; | 616 | type Error = Error; |
| 620 | 617 | ||
| 621 | fn exec( | 618 | fn exec( |
| 622 | &mut self, | 619 | &mut self, |
| 623 | address: u8, | 620 | address: u8, |
| 624 | operations: &mut [embedded_hal_02::blocking::i2c::Operation<'_>], | 621 | operations: &mut [embedded_hal_02::blocking::i2c::Operation<'_>], |
| 625 | ) -> Result<(), Self::Error> { | 622 | ) -> Result<(), Self::Error> { |
| 626 | Self::setup(address.into())?; | 623 | Self::setup(address.into())?; |
| 627 | for i in 0..operations.len() { | 624 | for i in 0..operations.len() { |
| 628 | let last = i == operations.len() - 1; | 625 | let last = i == operations.len() - 1; |
| 629 | match &mut operations[i] { | 626 | match &mut operations[i] { |
| 630 | embedded_hal_02::blocking::i2c::Operation::Read(buf) => { | 627 | embedded_hal_02::blocking::i2c::Operation::Read(buf) => { |
| 631 | self.read_blocking_internal(buf, false, last)? | 628 | self.read_blocking_internal(buf, false, last)? |
| 632 | } | ||
| 633 | embedded_hal_02::blocking::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, | ||
| 634 | } | 629 | } |
| 630 | embedded_hal_02::blocking::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, | ||
| 635 | } | 631 | } |
| 636 | Ok(()) | ||
| 637 | } | 632 | } |
| 633 | Ok(()) | ||
| 638 | } | 634 | } |
| 639 | } | 635 | } |
| 640 | 636 | ||
| 641 | #[cfg(feature = "unstable-traits")] | 637 | impl embedded_hal_1::i2c::Error for Error { |
| 642 | mod eh1 { | 638 | fn kind(&self) -> embedded_hal_1::i2c::ErrorKind { |
| 643 | use super::*; | 639 | match *self { |
| 644 | 640 | Self::Abort(AbortReason::ArbitrationLoss) => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss, | |
| 645 | impl embedded_hal_1::i2c::Error for Error { | 641 | Self::Abort(AbortReason::NoAcknowledge) => { |
| 646 | fn kind(&self) -> embedded_hal_1::i2c::ErrorKind { | 642 | embedded_hal_1::i2c::ErrorKind::NoAcknowledge(embedded_hal_1::i2c::NoAcknowledgeSource::Address) |
| 647 | match *self { | ||
| 648 | Self::Abort(AbortReason::ArbitrationLoss) => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss, | ||
| 649 | Self::Abort(AbortReason::NoAcknowledge) => { | ||
| 650 | embedded_hal_1::i2c::ErrorKind::NoAcknowledge(embedded_hal_1::i2c::NoAcknowledgeSource::Address) | ||
| 651 | } | ||
| 652 | Self::Abort(AbortReason::TxNotEmpty(_)) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 653 | Self::Abort(AbortReason::Other(_)) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 654 | Self::InvalidReadBufferLength => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 655 | Self::InvalidWriteBufferLength => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 656 | Self::AddressOutOfRange(_) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 657 | Self::AddressReserved(_) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 658 | } | 643 | } |
| 644 | Self::Abort(AbortReason::TxNotEmpty(_)) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 645 | Self::Abort(AbortReason::Other(_)) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 646 | Self::InvalidReadBufferLength => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 647 | Self::InvalidWriteBufferLength => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 648 | Self::AddressOutOfRange(_) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 649 | Self::AddressReserved(_) => embedded_hal_1::i2c::ErrorKind::Other, | ||
| 659 | } | 650 | } |
| 660 | } | 651 | } |
| 652 | } | ||
| 661 | 653 | ||
| 662 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::ErrorType for I2c<'d, T, M> { | 654 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::ErrorType for I2c<'d, T, M> { |
| 663 | type Error = Error; | 655 | type Error = Error; |
| 664 | } | 656 | } |
| 665 | 657 | ||
| 666 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::I2c for I2c<'d, T, M> { | 658 | impl<'d, T: Instance, M: Mode> embedded_hal_1::i2c::I2c for I2c<'d, T, M> { |
| 667 | fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { | 659 | fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { |
| 668 | self.blocking_read(address, read) | 660 | self.blocking_read(address, read) |
| 669 | } | 661 | } |
| 670 | 662 | ||
| 671 | fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { | 663 | fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { |
| 672 | self.blocking_write(address, write) | 664 | self.blocking_write(address, write) |
| 673 | } | 665 | } |
| 674 | 666 | ||
| 675 | fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { | 667 | fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { |
| 676 | self.blocking_write_read(address, write, read) | 668 | self.blocking_write_read(address, write, read) |
| 677 | } | 669 | } |
| 678 | 670 | ||
| 679 | fn transaction( | 671 | fn transaction( |
| 680 | &mut self, | 672 | &mut self, |
| 681 | address: u8, | 673 | address: u8, |
| 682 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], | 674 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], |
| 683 | ) -> Result<(), Self::Error> { | 675 | ) -> Result<(), Self::Error> { |
| 684 | Self::setup(address.into())?; | 676 | Self::setup(address.into())?; |
| 685 | for i in 0..operations.len() { | 677 | for i in 0..operations.len() { |
| 686 | let last = i == operations.len() - 1; | 678 | let last = i == operations.len() - 1; |
| 687 | match &mut operations[i] { | 679 | match &mut operations[i] { |
| 688 | embedded_hal_1::i2c::Operation::Read(buf) => self.read_blocking_internal(buf, false, last)?, | 680 | embedded_hal_1::i2c::Operation::Read(buf) => self.read_blocking_internal(buf, false, last)?, |
| 689 | embedded_hal_1::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, | 681 | embedded_hal_1::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, |
| 690 | } | ||
| 691 | } | 682 | } |
| 692 | Ok(()) | ||
| 693 | } | 683 | } |
| 684 | Ok(()) | ||
| 694 | } | 685 | } |
| 695 | } | 686 | } |
| 696 | #[cfg(all(feature = "unstable-traits", feature = "nightly"))] | ||
| 697 | mod nightly { | ||
| 698 | use embedded_hal_1::i2c::Operation; | ||
| 699 | use embedded_hal_async::i2c::AddressMode; | ||
| 700 | 687 | ||
| 701 | use super::*; | 688 | impl<'d, A, T> embedded_hal_async::i2c::I2c<A> for I2c<'d, T, Async> |
| 689 | where | ||
| 690 | A: embedded_hal_async::i2c::AddressMode + Into<u16> + 'static, | ||
| 691 | T: Instance + 'd, | ||
| 692 | { | ||
| 693 | async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 694 | let addr: u16 = address.into(); | ||
| 702 | 695 | ||
| 703 | impl<'d, A, T> embedded_hal_async::i2c::I2c<A> for I2c<'d, T, Async> | 696 | Self::setup(addr)?; |
| 704 | where | 697 | self.read_async_internal(read, false, true).await |
| 705 | A: AddressMode + Into<u16> + 'static, | 698 | } |
| 706 | T: Instance + 'd, | ||
| 707 | { | ||
| 708 | async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> { | ||
| 709 | let addr: u16 = address.into(); | ||
| 710 | 699 | ||
| 711 | Self::setup(addr)?; | 700 | async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> { |
| 712 | self.read_async_internal(read, false, true).await | 701 | let addr: u16 = address.into(); |
| 713 | } | ||
| 714 | 702 | ||
| 715 | async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> { | 703 | Self::setup(addr)?; |
| 716 | let addr: u16 = address.into(); | 704 | self.write_async_internal(write.iter().copied(), true).await |
| 705 | } | ||
| 717 | 706 | ||
| 718 | Self::setup(addr)?; | 707 | async fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { |
| 719 | self.write_async_internal(write.iter().copied(), true).await | 708 | let addr: u16 = address.into(); |
| 720 | } | ||
| 721 | 709 | ||
| 722 | async fn write_read(&mut self, address: A, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { | 710 | Self::setup(addr)?; |
| 723 | let addr: u16 = address.into(); | 711 | self.write_async_internal(write.iter().cloned(), false).await?; |
| 712 | self.read_async_internal(read, true, true).await | ||
| 713 | } | ||
| 724 | 714 | ||
| 725 | Self::setup(addr)?; | 715 | async fn transaction( |
| 726 | self.write_async_internal(write.iter().cloned(), false).await?; | 716 | &mut self, |
| 727 | self.read_async_internal(read, true, true).await | 717 | address: A, |
| 728 | } | 718 | operations: &mut [embedded_hal_1::i2c::Operation<'_>], |
| 719 | ) -> Result<(), Self::Error> { | ||
| 720 | use embedded_hal_1::i2c::Operation; | ||
| 729 | 721 | ||
| 730 | async fn transaction(&mut self, address: A, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> { | 722 | let addr: u16 = address.into(); |
| 731 | let addr: u16 = address.into(); | ||
| 732 | 723 | ||
| 733 | if operations.len() > 0 { | 724 | if operations.len() > 0 { |
| 734 | Self::setup(addr)?; | 725 | Self::setup(addr)?; |
| 735 | } | 726 | } |
| 736 | let mut iterator = operations.iter_mut(); | 727 | let mut iterator = operations.iter_mut(); |
| 737 | 728 | ||
| 738 | while let Some(op) = iterator.next() { | 729 | while let Some(op) = iterator.next() { |
| 739 | let last = iterator.len() == 0; | 730 | let last = iterator.len() == 0; |
| 740 | 731 | ||
| 741 | match op { | 732 | match op { |
| 742 | Operation::Read(buffer) => { | 733 | Operation::Read(buffer) => { |
| 743 | self.read_async_internal(buffer, false, last).await?; | 734 | self.read_async_internal(buffer, false, last).await?; |
| 744 | } | 735 | } |
| 745 | Operation::Write(buffer) => { | 736 | Operation::Write(buffer) => { |
| 746 | self.write_async_internal(buffer.into_iter().cloned(), last).await?; | 737 | self.write_async_internal(buffer.into_iter().cloned(), last).await?; |
| 747 | } | ||
| 748 | } | 738 | } |
| 749 | } | 739 | } |
| 750 | Ok(()) | ||
| 751 | } | 740 | } |
| 741 | Ok(()) | ||
| 752 | } | 742 | } |
| 753 | } | 743 | } |
| 754 | 744 | ||
