diff options
| author | TrAyZeN <[email protected]> | 2025-12-01 11:25:06 +0100 |
|---|---|---|
| committer | TrAyZeN <[email protected]> | 2025-12-01 11:54:41 +0100 |
| commit | ac89dbd3447993f7940b60400fb3e2d479956836 (patch) | |
| tree | 7c9d200acc1426ea98fce2fb78ff956f425d1b26 /embassy-stm32/src | |
| parent | 8e7d0e6406d8e701860a3138292ae87d75ddc8b0 (diff) | |
stm32/i2c: Add blocking_listen
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/i2c/v2.rs | 65 |
1 files changed, 49 insertions, 16 deletions
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 4f105adef..933cca9cb 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs | |||
| @@ -1673,6 +1673,54 @@ impl<'d, M: Mode> I2c<'d, M, MultiMaster> { | |||
| 1673 | Ok(()) | 1673 | Ok(()) |
| 1674 | } | 1674 | } |
| 1675 | 1675 | ||
| 1676 | /// Listen for incoming I2C messages. | ||
| 1677 | /// | ||
| 1678 | /// This method blocks until the slave address is matched by a master. | ||
| 1679 | pub fn blocking_listen(&mut self) -> Result<SlaveCommand, Error> { | ||
| 1680 | let timeout = self.timeout(); | ||
| 1681 | |||
| 1682 | self.info.regs.cr1().modify(|reg| { | ||
| 1683 | reg.set_addrie(true); | ||
| 1684 | trace!("Enable ADDRIE"); | ||
| 1685 | }); | ||
| 1686 | |||
| 1687 | loop { | ||
| 1688 | let isr = self.info.regs.isr().read(); | ||
| 1689 | if isr.addr() { | ||
| 1690 | break; | ||
| 1691 | } | ||
| 1692 | timeout.check()?; | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | trace!("ADDR triggered (address match)"); | ||
| 1696 | |||
| 1697 | // we do not clear the address flag here as it will be cleared by the dma read/write | ||
| 1698 | // if we clear it here the clock stretching will stop and the master will read in data before the slave is ready to send it | ||
| 1699 | self.slave_command() | ||
| 1700 | } | ||
| 1701 | |||
| 1702 | /// Determine the received slave command. | ||
| 1703 | fn slave_command(&self) -> Result<SlaveCommand, Error> { | ||
| 1704 | let isr = self.info.regs.isr().read(); | ||
| 1705 | |||
| 1706 | match isr.dir() { | ||
| 1707 | i2c::vals::Dir::WRITE => { | ||
| 1708 | trace!("DIR: write"); | ||
| 1709 | Ok(SlaveCommand { | ||
| 1710 | kind: SlaveCommandKind::Write, | ||
| 1711 | address: self.determine_matched_address()?, | ||
| 1712 | }) | ||
| 1713 | } | ||
| 1714 | i2c::vals::Dir::READ => { | ||
| 1715 | trace!("DIR: read"); | ||
| 1716 | Ok(SlaveCommand { | ||
| 1717 | kind: SlaveCommandKind::Read, | ||
| 1718 | address: self.determine_matched_address()?, | ||
| 1719 | }) | ||
| 1720 | } | ||
| 1721 | } | ||
| 1722 | } | ||
| 1723 | |||
| 1676 | /// Respond to a write command. | 1724 | /// Respond to a write command. |
| 1677 | /// | 1725 | /// |
| 1678 | /// Returns total number of bytes received. | 1726 | /// Returns total number of bytes received. |
| @@ -1708,22 +1756,7 @@ impl<'d> I2c<'d, Async, MultiMaster> { | |||
| 1708 | trace!("ADDR triggered (address match)"); | 1756 | trace!("ADDR triggered (address match)"); |
| 1709 | // we do not clear the address flag here as it will be cleared by the dma read/write | 1757 | // we do not clear the address flag here as it will be cleared by the dma read/write |
| 1710 | // if we clear it here the clock stretching will stop and the master will read in data before the slave is ready to send it | 1758 | // if we clear it here the clock stretching will stop and the master will read in data before the slave is ready to send it |
| 1711 | match isr.dir() { | 1759 | Poll::Ready(self.slave_command()) |
| 1712 | i2c::vals::Dir::WRITE => { | ||
| 1713 | trace!("DIR: write"); | ||
| 1714 | Poll::Ready(Ok(SlaveCommand { | ||
| 1715 | kind: SlaveCommandKind::Write, | ||
| 1716 | address: self.determine_matched_address()?, | ||
| 1717 | })) | ||
| 1718 | } | ||
| 1719 | i2c::vals::Dir::READ => { | ||
| 1720 | trace!("DIR: read"); | ||
| 1721 | Poll::Ready(Ok(SlaveCommand { | ||
| 1722 | kind: SlaveCommandKind::Read, | ||
| 1723 | address: self.determine_matched_address()?, | ||
| 1724 | })) | ||
| 1725 | } | ||
| 1726 | } | ||
| 1727 | } | 1760 | } |
| 1728 | }) | 1761 | }) |
| 1729 | .await | 1762 | .await |
