aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimo Kröger <[email protected]>2024-03-07 11:17:39 +0100
committerTimo Kröger <[email protected]>2024-03-12 08:14:42 +0100
commit4d0e3838168a7447b3580135ef78d65baa578933 (patch)
treee012b234d5242b0b089195b8e21f3d19c43045b4
parenta3b12226170d6b1a9ded47cc043cc09489cee278 (diff)
[UCPD] Prepare for PD communication implementation
-rw-r--r--embassy-stm32/src/ucpd.rs47
-rw-r--r--examples/stm32g4/src/bin/usb_c_pd.rs15
2 files changed, 60 insertions, 2 deletions
diff --git a/embassy-stm32/src/ucpd.rs b/embassy-stm32/src/ucpd.rs
index a366fadda..96cd92764 100644
--- a/embassy-stm32/src/ucpd.rs
+++ b/embassy-stm32/src/ucpd.rs
@@ -22,9 +22,10 @@ use embassy_hal_internal::drop::OnDrop;
22use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; 22use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
23use embassy_sync::waitqueue::AtomicWaker; 23use embassy_sync::waitqueue::AtomicWaker;
24 24
25use crate::dma::AnyChannel;
25use crate::interrupt; 26use crate::interrupt;
26pub use crate::pac::ucpd::vals::TypecVstateCc as CcVState;
27use crate::pac::ucpd::vals::{Anamode, Ccenable, PscUsbpdclk}; 27use crate::pac::ucpd::vals::{Anamode, Ccenable, PscUsbpdclk};
28pub use crate::pac::ucpd::vals::{Phyccsel as CcSel, TypecVstateCc as CcVState};
28use crate::rcc::RccPeripheral; 29use crate::rcc::RccPeripheral;
29 30
30/// Pull-up or Pull-down resistor state of both CC lines. 31/// Pull-up or Pull-down resistor state of both CC lines.
@@ -177,6 +178,50 @@ impl<'d, T: Instance> Ucpd<'d, T> {
177 }) 178 })
178 }); 179 });
179 } 180 }
181
182 /// Returns PD receiver and transmitter.
183 pub fn pd(
184 &mut self,
185 rx_dma: impl Peripheral<P = impl RxDma<T>> + 'd,
186 tx_dma: impl Peripheral<P = impl TxDma<T>> + 'd,
187 cc_sel: CcSel,
188 ) -> (PdRx<'_, T>, PdTx<'_, T>) {
189 into_ref!(rx_dma, tx_dma);
190 let rx_dma_req = rx_dma.request();
191 let tx_dma_req = tx_dma.request();
192 (
193 PdRx {
194 _ucpd: self,
195 dma_ch: rx_dma.map_into(),
196 dma_req: rx_dma_req,
197 },
198 PdTx {
199 _ucpd: self,
200 dma_ch: tx_dma.map_into(),
201 dma_req: tx_dma_req,
202 },
203 )
204 }
205}
206
207/// Power Delivery (PD) Receiver.
208pub struct PdRx<'d, T: Instance> {
209 _ucpd: &'d Ucpd<'d, T>,
210 dma_ch: PeripheralRef<'d, AnyChannel>,
211 dma_req: Request,
212}
213
214impl<'d, T: Instance> Drop for PdRx<'d, T> {
215 fn drop(&mut self) {
216 T::REGS.cr().modify(|w| w.set_phyrxen(false));
217 }
218}
219
220/// Power Delivery (PD) Transmitter.
221pub struct PdTx<'d, T: Instance> {
222 _ucpd: &'d Ucpd<'d, T>,
223 dma_ch: PeripheralRef<'d, AnyChannel>,
224 dma_req: Request,
180} 225}
181 226
182/// Interrupt handler. 227/// Interrupt handler.
diff --git a/examples/stm32g4/src/bin/usb_c_pd.rs b/examples/stm32g4/src/bin/usb_c_pd.rs
index 7a0065087..1443cb773 100644
--- a/examples/stm32g4/src/bin/usb_c_pd.rs
+++ b/examples/stm32g4/src/bin/usb_c_pd.rs
@@ -3,7 +3,7 @@
3 3
4use defmt::{info, Format}; 4use defmt::{info, Format};
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_stm32::ucpd::{self, CcPull, CcVState, Ucpd}; 6use embassy_stm32::ucpd::{self, CcPull, CcSel, CcVState, Ucpd};
7use embassy_stm32::Config; 7use embassy_stm32::Config;
8use embassy_time::{with_timeout, Duration}; 8use embassy_time::{with_timeout, Duration};
9use {defmt_rtt as _, panic_probe as _}; 9use {defmt_rtt as _, panic_probe as _};
@@ -56,5 +56,18 @@ async fn main(_spawner: Spawner) {
56 let cable_orientation = wait_attached(&mut ucpd).await; 56 let cable_orientation = wait_attached(&mut ucpd).await;
57 info!("USB cable connected, orientation: {}", cable_orientation); 57 info!("USB cable connected, orientation: {}", cable_orientation);
58 58
59 let cc_sel = match cable_orientation {
60 CableOrientation::Normal => {
61 info!("Starting PD communication on CC1 pin");
62 CcSel::CC1
63 }
64 CableOrientation::Flipped => {
65 info!("Starting PD communication on CC2 pin");
66 CcSel::CC2
67 }
68 CableOrientation::DebugAccessoryMode => panic!("No PD communication in DAM"),
69 };
70 let (mut _rx, mut _tx) = ucpd.pd(p.DMA1_CH1, p.DMA1_CH2, cc_sel);
71
59 loop {} 72 loop {}
60} 73}