Skip to content

Commit d17f8ef

Browse files
Merge pull request #155 from wiredopposite/main
Fix hang on disconnect of non-compliant devices: Added timeout to RX …
2 parents 528616d + 447ea43 commit d17f8ef

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/pio_usb.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
178178
bool crc_match = false;
179179
int16_t t = 240;
180180
uint16_t idx = 0;
181+
uint16_t nak_timeout = 10000;
182+
const uint16_t rx_buf_len = sizeof(pp->usb_rx_buffer) / sizeof(pp->usb_rx_buffer[0]);
181183

182184
while (t--) {
183185
if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) {
@@ -192,7 +194,7 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
192194
// timing critical start
193195
if (t > 0) {
194196
if (handshake == USB_PID_ACK) {
195-
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
197+
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && idx < rx_buf_len - 1) {
196198
if (pio_sm_get_rx_fifo_level(pp->pio_usb_rx, pp->sm_rx)) {
197199
uint8_t data = pio_sm_get(pp->pio_usb_rx, pp->sm_rx) >> 24;
198200
crc_prev2 = crc_prev;
@@ -212,7 +214,7 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
212214
}
213215
} else {
214216
// just discard received data since we NAK/STALL anyway
215-
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
217+
while ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0 && nak_timeout--) {
216218
continue;
217219
}
218220
pio_sm_clear_fifos(pp->pio_usb_rx, pp->sm_rx);

0 commit comments

Comments
 (0)