@@ -112,6 +112,17 @@ static inline uint16_t calc_grxfsiz(uint16_t max_ep_size, uint8_t ep_count) {
112
112
return 15 + 2 * (max_ep_size / 4 ) + 2 * ep_count ;
113
113
}
114
114
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
+
115
126
static bool fifo_alloc (uint8_t rhport , uint8_t ep_addr , uint16_t packet_size ) {
116
127
dwc2_regs_t * dwc2 = DWC2_REG (rhport );
117
128
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) {
225
236
}
226
237
227
238
// 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 );
230
240
} else {
231
241
dwc2_epout_t * epout = dwc2 -> epout ;
232
242
@@ -270,13 +280,8 @@ static void bus_reset(uint8_t rhport) {
270
280
dwc2 -> epout [n ].doepctl |= DOEPCTL_SNAK ;
271
281
}
272
282
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 );
280
285
281
286
// 2. Set up interrupt mask
282
287
dwc2 -> daintmsk = TU_BIT (DAINTMSK_OEPM_Pos ) | TU_BIT (DAINTMSK_IEPM_Pos );
@@ -584,13 +589,8 @@ void dcd_init(uint8_t rhport) {
584
589
// (non zero-length packet), send STALL back and discard.
585
590
dwc2 -> dcfg |= DCFG_NZLSOHSK ;
586
591
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 );
594
594
595
595
// Clear all interrupts
596
596
uint32_t int_mask = dwc2 -> gintsts ;
@@ -708,8 +708,13 @@ void dcd_edpt_close_all(uint8_t rhport) {
708
708
xfer_status [n ][TUSB_DIR_IN ].max_size = 0 ;
709
709
}
710
710
711
+ // reset allocated fifo OUT
712
+ dwc2 -> grxfsiz = calc_grxfsiz (64 , ep_count );
711
713
// reset allocated fifo IN
712
714
_allocated_fifo_words_tx = 16 ;
715
+
716
+ fifo_flush_tx (dwc2 , 0x10 ); // all tx fifo
717
+ fifo_flush_rx (dwc2 );
713
718
}
714
719
715
720
bool dcd_edpt_iso_alloc (uint8_t rhport , uint8_t ep_addr , uint16_t largest_packet_size ) {
0 commit comments