diff options
| -rw-r--r-- | embassy-nrf/src/twis.rs | 52 | ||||
| -rw-r--r-- | examples/nrf/src/bin/twis.rs | 11 |
2 files changed, 35 insertions, 28 deletions
diff --git a/embassy-nrf/src/twis.rs b/embassy-nrf/src/twis.rs index b8cb2eeb8..4091b017e 100644 --- a/embassy-nrf/src/twis.rs +++ b/embassy-nrf/src/twis.rs | |||
| @@ -507,7 +507,7 @@ impl<'d, T: Instance> Twis<'d, T> { | |||
| 507 | }) | 507 | }) |
| 508 | } | 508 | } |
| 509 | 509 | ||
| 510 | fn setup_write_from_ram(&mut self, buffer: &[u8], inten: bool) -> Result<(), Error> { | 510 | fn setup_respond_from_ram(&mut self, buffer: &[u8], inten: bool) -> Result<(), Error> { |
| 511 | let r = T::regs(); | 511 | let r = T::regs(); |
| 512 | 512 | ||
| 513 | compiler_fence(SeqCst); | 513 | compiler_fence(SeqCst); |
| @@ -532,14 +532,14 @@ impl<'d, T: Instance> Twis<'d, T> { | |||
| 532 | Ok(()) | 532 | Ok(()) |
| 533 | } | 533 | } |
| 534 | 534 | ||
| 535 | fn setup_write(&mut self, wr_buffer: &[u8], inten: bool) -> Result<(), Error> { | 535 | fn setup_respond(&mut self, wr_buffer: &[u8], inten: bool) -> Result<(), Error> { |
| 536 | match self.setup_write_from_ram(wr_buffer, inten) { | 536 | match self.setup_respond_from_ram(wr_buffer, inten) { |
| 537 | Ok(_) => Ok(()), | 537 | Ok(_) => Ok(()), |
| 538 | Err(Error::DMABufferNotInDataMemory) => { | 538 | Err(Error::DMABufferNotInDataMemory) => { |
| 539 | trace!("Copying TWIS tx buffer into RAM for DMA"); | 539 | trace!("Copying TWIS tx buffer into RAM for DMA"); |
| 540 | let tx_ram_buf = &mut [0; FORCE_COPY_BUFFER_SIZE][..wr_buffer.len()]; | 540 | let tx_ram_buf = &mut [0; FORCE_COPY_BUFFER_SIZE][..wr_buffer.len()]; |
| 541 | tx_ram_buf.copy_from_slice(wr_buffer); | 541 | tx_ram_buf.copy_from_slice(wr_buffer); |
| 542 | self.setup_write_from_ram(&tx_ram_buf, inten) | 542 | self.setup_respond_from_ram(&tx_ram_buf, inten) |
| 543 | } | 543 | } |
| 544 | Err(error) => Err(error), | 544 | Err(error) => Err(error), |
| 545 | } | 545 | } |
| @@ -609,18 +609,19 @@ impl<'d, T: Instance> Twis<'d, T> { | |||
| 609 | Ok(Command::Read) | 609 | Ok(Command::Read) |
| 610 | } | 610 | } |
| 611 | 611 | ||
| 612 | /// Write to an I2C master. | 612 | /// Respond to an I2C master READ command. |
| 613 | /// Returns the number of bytes written. | 613 | /// Returns the number of bytes written. |
| 614 | /// The buffer must have a length of at most 255 bytes on the nRF52832 | 614 | /// The buffer must have a length of at most 255 bytes on the nRF52832 |
| 615 | /// and at most 65535 bytes on the nRF52840. | 615 | /// and at most 65535 bytes on the nRF52840. |
| 616 | pub fn blocking_write(&mut self, buffer: &[u8]) -> Result<usize, Error> { | 616 | pub fn blocking_respond_to_read(&mut self, buffer: &[u8]) -> Result<usize, Error> { |
| 617 | self.setup_write(buffer, false)?; | 617 | self.setup_respond(buffer, false)?; |
| 618 | self.blocking_wait() | 618 | self.blocking_wait() |
| 619 | } | 619 | } |
| 620 | 620 | ||
| 621 | /// Same as [`blocking_write`](Twis::blocking_write) but will fail instead of copying data into RAM. Consult the module level documentation to learn more. | 621 | /// Same as [`blocking_respond_to_read`](Twis::blocking_respond_to_read) but will fail instead of copying data into RAM. |
| 622 | pub fn blocking_write_from_ram(&mut self, buffer: &[u8]) -> Result<usize, Error> { | 622 | /// Consult the module level documentation to learn more. |
| 623 | self.setup_write_from_ram(buffer, false)?; | 623 | pub fn blocking_respond_to_read_from_ram(&mut self, buffer: &[u8]) -> Result<usize, Error> { |
| 624 | self.setup_respond_from_ram(buffer, false)?; | ||
| 624 | self.blocking_wait() | 625 | self.blocking_wait() |
| 625 | } | 626 | } |
| 626 | 627 | ||
| @@ -643,19 +644,24 @@ impl<'d, T: Instance> Twis<'d, T> { | |||
| 643 | Ok(Command::Read) | 644 | Ok(Command::Read) |
| 644 | } | 645 | } |
| 645 | 646 | ||
| 646 | /// Write to an I2C master with timeout. | 647 | /// Respond to an I2C master READ command with timeout. |
| 647 | /// Returns the number of bytes written. | 648 | /// Returns the number of bytes written. |
| 648 | /// See [`blocking_write`]. | 649 | /// See [`blocking_respond_to_read`]. |
| 649 | #[cfg(feature = "time")] | 650 | #[cfg(feature = "time")] |
| 650 | pub fn blocking_write_timeout(&mut self, buffer: &[u8], timeout: Duration) -> Result<usize, Error> { | 651 | pub fn blocking_respond_to_read_timeout(&mut self, buffer: &[u8], timeout: Duration) -> Result<usize, Error> { |
| 651 | self.setup_write(buffer, false)?; | 652 | self.setup_respond(buffer, false)?; |
| 652 | self.blocking_wait_timeout(timeout) | 653 | self.blocking_wait_timeout(timeout) |
| 653 | } | 654 | } |
| 654 | 655 | ||
| 655 | /// Same as [`blocking_write`](Twis::blocking_write) but will fail instead of copying data into RAM. Consult the module level documentation to learn more. | 656 | /// Same as [`blocking_respond_to_read_timeout`](Twis::blocking_respond_to_read_timeout) but will fail instead of copying data into RAM. |
| 657 | /// Consult the module level documentation to learn more. | ||
| 656 | #[cfg(feature = "time")] | 658 | #[cfg(feature = "time")] |
| 657 | pub fn blocking_write_from_ram_timeout(&mut self, buffer: &[u8], timeout: Duration) -> Result<usize, Error> { | 659 | pub fn blocking_respond_to_read_from_ram_timeout( |
| 658 | self.setup_write_from_ram(buffer, false)?; | 660 | &mut self, |
| 661 | buffer: &[u8], | ||
| 662 | timeout: Duration, | ||
| 663 | ) -> Result<usize, Error> { | ||
| 664 | self.setup_respond_from_ram(buffer, false)?; | ||
| 659 | self.blocking_wait_timeout(timeout) | 665 | self.blocking_wait_timeout(timeout) |
| 660 | } | 666 | } |
| 661 | 667 | ||
| @@ -677,18 +683,18 @@ impl<'d, T: Instance> Twis<'d, T> { | |||
| 677 | Ok(Command::Read) | 683 | Ok(Command::Read) |
| 678 | } | 684 | } |
| 679 | 685 | ||
| 680 | /// Async write to an I2C master. | 686 | /// Respond to an I2C master READ command, asynchronously. |
| 681 | /// Returns the number of bytes written. | 687 | /// Returns the number of bytes written. |
| 682 | /// The buffer must have a length of at most 255 bytes on the nRF52832 | 688 | /// The buffer must have a length of at most 255 bytes on the nRF52832 |
| 683 | /// and at most 65535 bytes on the nRF52840. | 689 | /// and at most 65535 bytes on the nRF52840. |
| 684 | pub async fn write(&mut self, buffer: &[u8]) -> Result<usize, Error> { | 690 | pub async fn respond_to_read(&mut self, buffer: &[u8]) -> Result<usize, Error> { |
| 685 | self.setup_write(buffer, true)?; | 691 | self.setup_respond(buffer, true)?; |
| 686 | self.async_wait().await | 692 | self.async_wait().await |
| 687 | } | 693 | } |
| 688 | 694 | ||
| 689 | /// Same as [`write`](Twis::write) but will fail instead of copying data into RAM. Consult the module level documentation to learn more. | 695 | /// Same as [`respond_to_read`](Twis::respond_to_read) but will fail instead of copying data into RAM. Consult the module level documentation to learn more. |
| 690 | pub async fn write_from_ram(&mut self, buffer: &[u8]) -> Result<usize, Error> { | 696 | pub async fn respond_to_read_from_ram(&mut self, buffer: &[u8]) -> Result<usize, Error> { |
| 691 | self.setup_write_from_ram(buffer, true)?; | 697 | self.setup_respond_from_ram(buffer, true)?; |
| 692 | self.async_wait().await | 698 | self.async_wait().await |
| 693 | } | 699 | } |
| 694 | } | 700 | } |
diff --git a/examples/nrf/src/bin/twis.rs b/examples/nrf/src/bin/twis.rs index a34bb2711..54cba9494 100644 --- a/examples/nrf/src/bin/twis.rs +++ b/examples/nrf/src/bin/twis.rs | |||
| @@ -22,20 +22,21 @@ async fn main(_spawner: Spawner) { | |||
| 22 | 22 | ||
| 23 | info!("Listening..."); | 23 | info!("Listening..."); |
| 24 | loop { | 24 | loop { |
| 25 | let response = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
| 26 | // This buffer is used if the i2c master performs a Write or WriteRead | ||
| 25 | let mut buf = [0u8; 16]; | 27 | let mut buf = [0u8; 16]; |
| 26 | let tx_buf = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
| 27 | match i2c.listen(&mut buf).await { | 28 | match i2c.listen(&mut buf).await { |
| 28 | Ok(Command::Read) => { | 29 | Ok(Command::Read) => { |
| 29 | info!("Got READ command. Writing back data:\n{:?}\n", tx_buf); | 30 | info!("Got READ command. Respond with data:\n{:?}\n", response); |
| 30 | if let Err(e) = i2c.write(&tx_buf).await { | 31 | if let Err(e) = i2c.respond_to_read(&response).await { |
| 31 | error!("{:?}", e); | 32 | error!("{:?}", e); |
| 32 | } | 33 | } |
| 33 | } | 34 | } |
| 34 | Ok(Command::Write(n)) => info!("Got WRITE command with data:\n{:?}\n", buf[..n]), | 35 | Ok(Command::Write(n)) => info!("Got WRITE command with data:\n{:?}\n", buf[..n]), |
| 35 | Ok(Command::WriteRead(n)) => { | 36 | Ok(Command::WriteRead(n)) => { |
| 36 | info!("Got WRITE/READ command with data:\n{:?}", buf[..n]); | 37 | info!("Got WRITE/READ command with data:\n{:?}", buf[..n]); |
| 37 | info!("Writing back data:\n{:?}\n", tx_buf); | 38 | info!("Respond with data:\n{:?}\n", response); |
| 38 | if let Err(e) = i2c.write(&tx_buf).await { | 39 | if let Err(e) = i2c.respond_to_read(&response).await { |
| 39 | error!("{:?}", e); | 40 | error!("{:?}", e); |
| 40 | } | 41 | } |
| 41 | } | 42 | } |
