aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Ickstadt <[email protected]>2023-07-31 13:47:03 -0500
committerMatt Ickstadt <[email protected]>2023-07-31 13:47:03 -0500
commit26cc0e634d1e0f51e7c4c5bb55fea4f7bec299aa (patch)
tree704e6d2ecfbc540508e52366ccf8513c426fb270
parent1b0f4ee65390ec0e511c2455c0f26c8d4f661ec3 (diff)
stm32: add async timeout functions to I2c and TimeoutI2c
-rw-r--r--embassy-stm32/src/i2c/timeout.rs74
-rw-r--r--embassy-stm32/src/i2c/v2.rs70
2 files changed, 135 insertions, 9 deletions
diff --git a/embassy-stm32/src/i2c/timeout.rs b/embassy-stm32/src/i2c/timeout.rs
index bb6e105de..830de8e9a 100644
--- a/embassy-stm32/src/i2c/timeout.rs
+++ b/embassy-stm32/src/i2c/timeout.rs
@@ -27,6 +27,80 @@ impl<'a, 'd: 'a, T: Instance, TXDMA, RXDMA> TimeoutI2c<'a, 'd, T, TXDMA, RXDMA>
27 Self { i2c, timeout } 27 Self { i2c, timeout }
28 } 28 }
29 29
30 // =========================
31 // Async public API
32
33 pub async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Error>
34 where
35 TXDMA: crate::i2c::TxDma<T>,
36 {
37 self.write_timeout(address, write, self.timeout).await
38 }
39
40 pub async fn write_timeout(&mut self, address: u8, write: &[u8], timeout: Duration) -> Result<(), Error>
41 where
42 TXDMA: crate::i2c::TxDma<T>,
43 {
44 self.i2c.write_timeout(address, write, timeout_fn(timeout)).await
45 }
46
47 pub async fn write_vectored(&mut self, address: u8, write: &[&[u8]]) -> Result<(), Error>
48 where
49 TXDMA: crate::i2c::TxDma<T>,
50 {
51 self.write_vectored_timeout(address, write, self.timeout).await
52 }
53
54 pub async fn write_vectored_timeout(&mut self, address: u8, write: &[&[u8]], timeout: Duration) -> Result<(), Error>
55 where
56 TXDMA: crate::i2c::TxDma<T>,
57 {
58 self.i2c
59 .write_vectored_timeout(address, write, timeout_fn(timeout))
60 .await
61 }
62
63 pub async fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Error>
64 where
65 RXDMA: crate::i2c::RxDma<T>,
66 {
67 self.read_timeout(address, buffer, self.timeout).await
68 }
69
70 pub async fn read_timeout(&mut self, address: u8, buffer: &mut [u8], timeout: Duration) -> Result<(), Error>
71 where
72 RXDMA: crate::i2c::RxDma<T>,
73 {
74 self.i2c.read_timeout(address, buffer, timeout_fn(timeout)).await
75 }
76
77 pub async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error>
78 where
79 TXDMA: super::TxDma<T>,
80 RXDMA: super::RxDma<T>,
81 {
82 self.write_read_timeout(address, write, read, self.timeout).await
83 }
84
85 pub async fn write_read_timeout(
86 &mut self,
87 address: u8,
88 write: &[u8],
89 read: &mut [u8],
90 timeout: Duration,
91 ) -> Result<(), Error>
92 where
93 TXDMA: super::TxDma<T>,
94 RXDMA: super::RxDma<T>,
95 {
96 self.i2c
97 .write_read_timeout(address, write, read, timeout_fn(timeout))
98 .await
99 }
100
101 // =========================
102 // Blocking public API
103
30 /// Blocking read with a custom timeout 104 /// Blocking read with a custom timeout
31 pub fn blocking_read_timeout(&mut self, addr: u8, read: &mut [u8], timeout: Duration) -> Result<(), Error> { 105 pub fn blocking_read_timeout(&mut self, addr: u8, read: &mut [u8], timeout: Duration) -> Result<(), Error> {
32 self.i2c.blocking_read_timeout(addr, read, timeout_fn(timeout)) 106 self.i2c.blocking_read_timeout(addr, read, timeout_fn(timeout))
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index eaf980a4d..4327899bb 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -598,10 +598,22 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
598 where 598 where
599 TXDMA: crate::i2c::TxDma<T>, 599 TXDMA: crate::i2c::TxDma<T>,
600 { 600 {
601 self.write_timeout(address, write, || Ok(())).await
602 }
603
604 pub async fn write_timeout(
605 &mut self,
606 address: u8,
607 write: &[u8],
608 check_timeout: impl Fn() -> Result<(), Error>,
609 ) -> Result<(), Error>
610 where
611 TXDMA: crate::i2c::TxDma<T>,
612 {
601 if write.is_empty() { 613 if write.is_empty() {
602 self.write_internal(address, write, true, || Ok(())) 614 self.write_internal(address, write, true, check_timeout)
603 } else { 615 } else {
604 self.write_dma_internal(address, write, true, true, || Ok(())).await 616 self.write_dma_internal(address, write, true, true, check_timeout).await
605 } 617 }
606 } 618 }
607 619
@@ -609,6 +621,18 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
609 where 621 where
610 TXDMA: crate::i2c::TxDma<T>, 622 TXDMA: crate::i2c::TxDma<T>,
611 { 623 {
624 self.write_vectored_timeout(address, write, || Ok(())).await
625 }
626
627 pub async fn write_vectored_timeout(
628 &mut self,
629 address: u8,
630 write: &[&[u8]],
631 check_timeout: impl Fn() -> Result<(), Error>,
632 ) -> Result<(), Error>
633 where
634 TXDMA: crate::i2c::TxDma<T>,
635 {
612 if write.is_empty() { 636 if write.is_empty() {
613 return Err(Error::ZeroLengthTransfer); 637 return Err(Error::ZeroLengthTransfer);
614 } 638 }
@@ -620,7 +644,8 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
620 let next = iter.next(); 644 let next = iter.next();
621 let is_last = next.is_none(); 645 let is_last = next.is_none();
622 646
623 self.write_dma_internal(address, c, first, is_last, || Ok(())).await?; 647 self.write_dma_internal(address, c, first, is_last, || check_timeout())
648 .await?;
624 first = false; 649 first = false;
625 current = next; 650 current = next;
626 } 651 }
@@ -631,10 +656,22 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
631 where 656 where
632 RXDMA: crate::i2c::RxDma<T>, 657 RXDMA: crate::i2c::RxDma<T>,
633 { 658 {
659 self.read_timeout(address, buffer, || Ok(())).await
660 }
661
662 pub async fn read_timeout(
663 &mut self,
664 address: u8,
665 buffer: &mut [u8],
666 check_timeout: impl Fn() -> Result<(), Error>,
667 ) -> Result<(), Error>
668 where
669 RXDMA: crate::i2c::RxDma<T>,
670 {
634 if buffer.is_empty() { 671 if buffer.is_empty() {
635 self.read_internal(address, buffer, false, || Ok(())) 672 self.read_internal(address, buffer, false, check_timeout)
636 } else { 673 } else {
637 self.read_dma_internal(address, buffer, false, || Ok(())).await 674 self.read_dma_internal(address, buffer, false, check_timeout).await
638 } 675 }
639 } 676 }
640 677
@@ -643,16 +680,31 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
643 TXDMA: super::TxDma<T>, 680 TXDMA: super::TxDma<T>,
644 RXDMA: super::RxDma<T>, 681 RXDMA: super::RxDma<T>,
645 { 682 {
683 self.write_read_timeout(address, write, read, || Ok(())).await
684 }
685
686 pub async fn write_read_timeout(
687 &mut self,
688 address: u8,
689 write: &[u8],
690 read: &mut [u8],
691 check_timeout: impl Fn() -> Result<(), Error>,
692 ) -> Result<(), Error>
693 where
694 TXDMA: super::TxDma<T>,
695 RXDMA: super::RxDma<T>,
696 {
646 if write.is_empty() { 697 if write.is_empty() {
647 self.write_internal(address, write, false, || Ok(()))?; 698 self.write_internal(address, write, false, || check_timeout())?;
648 } else { 699 } else {
649 self.write_dma_internal(address, write, true, true, || Ok(())).await?; 700 self.write_dma_internal(address, write, true, true, || check_timeout())
701 .await?;
650 } 702 }
651 703
652 if read.is_empty() { 704 if read.is_empty() {
653 self.read_internal(address, read, true, || Ok(()))?; 705 self.read_internal(address, read, true, check_timeout)?;
654 } else { 706 } else {
655 self.read_dma_internal(address, read, true, || Ok(())).await?; 707 self.read_dma_internal(address, read, true, check_timeout).await?;
656 } 708 }
657 709
658 Ok(()) 710 Ok(())