diff options
| author | Raul Alimbekov <[email protected]> | 2025-12-16 09:05:22 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-16 09:05:22 +0300 |
| commit | c9a04b4b732b7a3b696eb8223664c1a7942b1875 (patch) | |
| tree | 6dbe5c02e66eed8d8762f13f95afd24f8db2b38c /embassy-net-esp-hosted/src/iface.rs | |
| parent | cde24a3ef1117653ba5ed4184102b33f745782fb (diff) | |
| parent | 5ae6e060ec1c90561719aabdc29d5b6e7b8b0a82 (diff) | |
Merge branch 'main' into main
Diffstat (limited to 'embassy-net-esp-hosted/src/iface.rs')
| -rw-r--r-- | embassy-net-esp-hosted/src/iface.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/embassy-net-esp-hosted/src/iface.rs b/embassy-net-esp-hosted/src/iface.rs new file mode 100644 index 000000000..1f57851e0 --- /dev/null +++ b/embassy-net-esp-hosted/src/iface.rs | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | use embassy_time::Timer; | ||
| 2 | use embedded_hal::digital::InputPin; | ||
| 3 | use embedded_hal_async::digital::Wait; | ||
| 4 | use embedded_hal_async::spi::SpiDevice; | ||
| 5 | |||
| 6 | /// Physical interface trait for communicating with the ESP chip. | ||
| 7 | pub trait Interface { | ||
| 8 | /// Wait for the HANDSHAKE signal indicating the ESP is ready for a new transaction. | ||
| 9 | async fn wait_for_handshake(&mut self); | ||
| 10 | |||
| 11 | /// Wait for the READY signal indicating the ESP has data to send. | ||
| 12 | async fn wait_for_ready(&mut self); | ||
| 13 | |||
| 14 | /// Perform a SPI transfer, exchanging data with the ESP chip. | ||
| 15 | async fn transfer(&mut self, rx: &mut [u8], tx: &[u8]); | ||
| 16 | } | ||
| 17 | |||
| 18 | /// Standard SPI interface. | ||
| 19 | /// | ||
| 20 | /// This interface is what's implemented in the upstream `esp-hosted-fg` firmware. It uses: | ||
| 21 | /// - An `SpiDevice` for SPI communication (CS is handled by the device) | ||
| 22 | /// - A handshake pin that signals when the ESP is ready for a new transaction | ||
| 23 | /// - A ready pin that indicates when the ESP has data to send | ||
| 24 | pub struct SpiInterface<SPI, IN> { | ||
| 25 | spi: SPI, | ||
| 26 | handshake: IN, | ||
| 27 | ready: IN, | ||
| 28 | } | ||
| 29 | |||
| 30 | impl<SPI, IN> SpiInterface<SPI, IN> | ||
| 31 | where | ||
| 32 | SPI: SpiDevice, | ||
| 33 | IN: InputPin + Wait, | ||
| 34 | { | ||
| 35 | /// Create a new SpiInterface. | ||
| 36 | pub fn new(spi: SPI, handshake: IN, ready: IN) -> Self { | ||
| 37 | Self { spi, handshake, ready } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | impl<SPI, IN> Interface for SpiInterface<SPI, IN> | ||
| 42 | where | ||
| 43 | SPI: SpiDevice, | ||
| 44 | IN: InputPin + Wait, | ||
| 45 | { | ||
| 46 | async fn wait_for_handshake(&mut self) { | ||
| 47 | self.handshake.wait_for_high().await.unwrap(); | ||
| 48 | } | ||
| 49 | |||
| 50 | async fn wait_for_ready(&mut self) { | ||
| 51 | self.ready.wait_for_high().await.unwrap(); | ||
| 52 | } | ||
| 53 | |||
| 54 | async fn transfer(&mut self, rx: &mut [u8], tx: &[u8]) { | ||
| 55 | self.spi.transfer(rx, tx).await.unwrap(); | ||
| 56 | |||
| 57 | // The esp-hosted firmware deasserts the HANDSHAKE pin a few us AFTER ending the SPI transfer | ||
| 58 | // If we check it again too fast, we'll see it's high from the previous transfer, and if we send it | ||
| 59 | // data it will get lost. | ||
| 60 | Timer::after_micros(100).await; | ||
| 61 | } | ||
| 62 | } | ||
