Skip to content

Commit c83e28c

Browse files
authored
Merge pull request #2589 from HiFiPhile/dwc_close
dwc2: flush fifo in dcd_edpt_close_all()
2 parents 6bc4aae + 79cbe93 commit c83e28c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/portable/synopsys/dwc2/dcd_dwc2.c

+21-16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ static inline uint16_t calc_grxfsiz(uint16_t max_ep_size, uint8_t ep_count) {
112112
return 15 + 2 * (max_ep_size / 4) + 2 * ep_count;
113113
}
114114

115+
TU_ATTR_ALWAYS_INLINE static inline void fifo_flush_tx(dwc2_regs_t* dwc2, uint8_t epnum) {
116+
// flush TX fifo and wait for it cleared
117+
dwc2->grstctl = GRSTCTL_TXFFLSH | (epnum << GRSTCTL_TXFNUM_Pos);
118+
while (dwc2->grstctl & GRSTCTL_TXFFLSH_Msk) {}
119+
}
120+
TU_ATTR_ALWAYS_INLINE static inline void fifo_flush_rx(dwc2_regs_t* dwc2) {
121+
// flush RX fifo and wait for it cleared
122+
dwc2->grstctl = GRSTCTL_RXFFLSH;
123+
while (dwc2->grstctl & GRSTCTL_RXFFLSH_Msk) {}
124+
}
125+
115126
static bool fifo_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t packet_size) {
116127
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
117128
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
@@ -225,8 +236,7 @@ static void edpt_disable(uint8_t rhport, uint8_t ep_addr, bool stall) {
225236
}
226237

227238
// Flush the FIFO, and wait until we have confirmed it cleared.
228-
dwc2->grstctl = ((epnum << GRSTCTL_TXFNUM_Pos) | GRSTCTL_TXFFLSH);
229-
while ((dwc2->grstctl & GRSTCTL_TXFFLSH_Msk) != 0) {}
239+
fifo_flush_tx(dwc2, epnum);
230240
} else {
231241
dwc2_epout_t* epout = dwc2->epout;
232242

@@ -270,13 +280,8 @@ static void bus_reset(uint8_t rhport) {
270280
dwc2->epout[n].doepctl |= DOEPCTL_SNAK;
271281
}
272282

273-
// flush all TX fifo and wait for it cleared
274-
dwc2->grstctl = GRSTCTL_TXFFLSH | (0x10u << GRSTCTL_TXFNUM_Pos);
275-
while (dwc2->grstctl & GRSTCTL_TXFFLSH_Msk) {}
276-
277-
// flush RX fifo and wait for it cleared
278-
dwc2->grstctl = GRSTCTL_RXFFLSH;
279-
while (dwc2->grstctl & GRSTCTL_RXFFLSH_Msk) {}
283+
fifo_flush_tx(dwc2, 0x10); // all tx fifo
284+
fifo_flush_rx(dwc2);
280285

281286
// 2. Set up interrupt mask
282287
dwc2->daintmsk = TU_BIT(DAINTMSK_OEPM_Pos) | TU_BIT(DAINTMSK_IEPM_Pos);
@@ -584,13 +589,8 @@ void dcd_init(uint8_t rhport) {
584589
// (non zero-length packet), send STALL back and discard.
585590
dwc2->dcfg |= DCFG_NZLSOHSK;
586591

587-
// flush all TX fifo and wait for it cleared
588-
dwc2->grstctl = GRSTCTL_TXFFLSH | (0x10u << GRSTCTL_TXFNUM_Pos);
589-
while (dwc2->grstctl & GRSTCTL_TXFFLSH_Msk) {}
590-
591-
// flush RX fifo and wait for it cleared
592-
dwc2->grstctl = GRSTCTL_RXFFLSH;
593-
while (dwc2->grstctl & GRSTCTL_RXFFLSH_Msk) {}
592+
fifo_flush_tx(dwc2, 0x10); // all tx fifo
593+
fifo_flush_rx(dwc2);
594594

595595
// Clear all interrupts
596596
uint32_t int_mask = dwc2->gintsts;
@@ -708,8 +708,13 @@ void dcd_edpt_close_all(uint8_t rhport) {
708708
xfer_status[n][TUSB_DIR_IN].max_size = 0;
709709
}
710710

711+
// reset allocated fifo OUT
712+
dwc2->grxfsiz = calc_grxfsiz(64, ep_count);
711713
// reset allocated fifo IN
712714
_allocated_fifo_words_tx = 16;
715+
716+
fifo_flush_tx(dwc2, 0x10); // all tx fifo
717+
fifo_flush_rx(dwc2);
713718
}
714719

715720
bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size) {

0 commit comments

Comments
 (0)