diff options
| author | Timo Kröger <[email protected]> | 2024-03-07 11:17:39 +0100 |
|---|---|---|
| committer | Timo Kröger <[email protected]> | 2024-03-12 08:14:42 +0100 |
| commit | 4d0e3838168a7447b3580135ef78d65baa578933 (patch) | |
| tree | e012b234d5242b0b089195b8e21f3d19c43045b4 | |
| parent | a3b12226170d6b1a9ded47cc043cc09489cee278 (diff) | |
[UCPD] Prepare for PD communication implementation
| -rw-r--r-- | embassy-stm32/src/ucpd.rs | 47 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/usb_c_pd.rs | 15 |
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; | |||
| 22 | use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; | 22 | use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef}; |
| 23 | use embassy_sync::waitqueue::AtomicWaker; | 23 | use embassy_sync::waitqueue::AtomicWaker; |
| 24 | 24 | ||
| 25 | use crate::dma::AnyChannel; | ||
| 25 | use crate::interrupt; | 26 | use crate::interrupt; |
| 26 | pub use crate::pac::ucpd::vals::TypecVstateCc as CcVState; | ||
| 27 | use crate::pac::ucpd::vals::{Anamode, Ccenable, PscUsbpdclk}; | 27 | use crate::pac::ucpd::vals::{Anamode, Ccenable, PscUsbpdclk}; |
| 28 | pub use crate::pac::ucpd::vals::{Phyccsel as CcSel, TypecVstateCc as CcVState}; | ||
| 28 | use crate::rcc::RccPeripheral; | 29 | use 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. | ||
| 208 | pub struct PdRx<'d, T: Instance> { | ||
| 209 | _ucpd: &'d Ucpd<'d, T>, | ||
| 210 | dma_ch: PeripheralRef<'d, AnyChannel>, | ||
| 211 | dma_req: Request, | ||
| 212 | } | ||
| 213 | |||
| 214 | impl<'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. | ||
| 221 | pub 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 | ||
| 4 | use defmt::{info, Format}; | 4 | use defmt::{info, Format}; |
| 5 | use embassy_executor::Spawner; | 5 | use embassy_executor::Spawner; |
| 6 | use embassy_stm32::ucpd::{self, CcPull, CcVState, Ucpd}; | 6 | use embassy_stm32::ucpd::{self, CcPull, CcSel, CcVState, Ucpd}; |
| 7 | use embassy_stm32::Config; | 7 | use embassy_stm32::Config; |
| 8 | use embassy_time::{with_timeout, Duration}; | 8 | use embassy_time::{with_timeout, Duration}; |
| 9 | use {defmt_rtt as _, panic_probe as _}; | 9 | use {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 | } |
