Skip to content

Commit

Permalink
Merge pull request #3858 from elagil/ucpd_logic_fix
Browse files Browse the repository at this point in the history
Fix UCPD drop logic
  • Loading branch information
Dirbaio authored Feb 10, 2025
2 parents c291e92 + 38799c6 commit f09277b
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions embassy-stm32/src/ucpd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl<'d, T: Instance> Drop for CcPhy<'d, T> {
// Check if the PdPhy part was dropped already.
let drop_not_ready = &T::state().drop_not_ready;
if drop_not_ready.load(Ordering::Relaxed) {
drop_not_ready.store(true, Ordering::Relaxed);
drop_not_ready.store(false, Ordering::Relaxed);
} else {
r.cfgr1().write(|w| w.set_ucpden(false));
rcc::disable::<T>();
Expand Down Expand Up @@ -411,13 +411,14 @@ pub struct PdPhy<'d, T: Instance> {

impl<'d, T: Instance> Drop for PdPhy<'d, T> {
fn drop(&mut self) {
T::REGS.cr().modify(|w| w.set_phyrxen(false));
// Check if the Type-C part was dropped already.
let r = T::REGS;
r.cr().modify(|w| w.set_phyrxen(false));
// Check if the CcPhy part was dropped already.
let drop_not_ready = &T::state().drop_not_ready;
if drop_not_ready.load(Ordering::Relaxed) {
drop_not_ready.store(true, Ordering::Relaxed);
drop_not_ready.store(false, Ordering::Relaxed);
} else {
T::REGS.cfgr1().write(|w| w.set_ucpden(false));
r.cfgr1().write(|w| w.set_ucpden(false));
rcc::disable::<T>();
T::Interrupt::disable();
}
Expand Down Expand Up @@ -453,6 +454,8 @@ impl<'d, T: Instance> PdPhy<'d, T> {
});
});

let mut rxpaysz = 0;

// Stop DMA reception immediately after receiving a packet, to prevent storing multiple packets in the same buffer.
poll_fn(|cx| {
let sr = r.sr().read();
Expand All @@ -466,6 +469,8 @@ impl<'d, T: Instance> PdPhy<'d, T> {
Poll::Ready(Err(RxError::HardReset))
} else if sr.rxmsgend() {
dma.request_stop();
// Should be read immediately on interrupt.
rxpaysz = r.rx_payszr().read().rxpaysz().into();

let ret = if sr.rxovr() {
Err(RxError::Overrun)
Expand Down Expand Up @@ -501,7 +506,7 @@ impl<'d, T: Instance> PdPhy<'d, T> {
_ => unreachable!(),
};

Ok((sop, r.rx_payszr().read().rxpaysz().into()))
Ok((sop, rxpaysz))
}

fn enable_rx_interrupt(enable: bool) {
Expand Down

0 comments on commit f09277b

Please sign in to comment.