Skip to content

Commit f09277b

Browse files
authored
Merge pull request #3858 from elagil/ucpd_logic_fix
Fix UCPD drop logic
2 parents c291e92 + 38799c6 commit f09277b

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

embassy-stm32/src/ucpd.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'d, T: Instance> Drop for CcPhy<'d, T> {
266266
// Check if the PdPhy part was dropped already.
267267
let drop_not_ready = &T::state().drop_not_ready;
268268
if drop_not_ready.load(Ordering::Relaxed) {
269-
drop_not_ready.store(true, Ordering::Relaxed);
269+
drop_not_ready.store(false, Ordering::Relaxed);
270270
} else {
271271
r.cfgr1().write(|w| w.set_ucpden(false));
272272
rcc::disable::<T>();
@@ -411,13 +411,14 @@ pub struct PdPhy<'d, T: Instance> {
411411

412412
impl<'d, T: Instance> Drop for PdPhy<'d, T> {
413413
fn drop(&mut self) {
414-
T::REGS.cr().modify(|w| w.set_phyrxen(false));
415-
// Check if the Type-C part was dropped already.
414+
let r = T::REGS;
415+
r.cr().modify(|w| w.set_phyrxen(false));
416+
// Check if the CcPhy part was dropped already.
416417
let drop_not_ready = &T::state().drop_not_ready;
417418
if drop_not_ready.load(Ordering::Relaxed) {
418-
drop_not_ready.store(true, Ordering::Relaxed);
419+
drop_not_ready.store(false, Ordering::Relaxed);
419420
} else {
420-
T::REGS.cfgr1().write(|w| w.set_ucpden(false));
421+
r.cfgr1().write(|w| w.set_ucpden(false));
421422
rcc::disable::<T>();
422423
T::Interrupt::disable();
423424
}
@@ -453,6 +454,8 @@ impl<'d, T: Instance> PdPhy<'d, T> {
453454
});
454455
});
455456

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

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

504-
Ok((sop, r.rx_payszr().read().rxpaysz().into()))
509+
Ok((sop, rxpaysz))
505510
}
506511

507512
fn enable_rx_interrupt(enable: bool) {

0 commit comments

Comments
 (0)