Skip to content

Commit 9c8df30

Browse files
committed
retry transaction up to 3 times for usb host
1 parent 442af43 commit 9c8df30

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/pio_usb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ bool __no_inline_not_in_flash_func(pio_usb_ll_transfer_start)(endpoint_t *ep,
511511
ep->app_buf = buffer;
512512
ep->total_len = buflen;
513513
ep->actual_len = 0;
514+
ep->failed_count = 0;
514515

515516
if (ep->is_tx) {
516517
prepare_tx_data(ep);

src/pio_usb_host.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "usb_rx.pio.h"
2222
#include "usb_tx.pio.h"
2323

24+
enum {
25+
TRANSACTION_MAX_RETRY = 3, // Number of times to retry a failed transaction
26+
};
27+
2428
static alarm_pool_t *_alarm_pool = NULL;
2529
static repeating_timer_t sof_rt;
2630
// The sof_count may be incremented and then read on different cores.
@@ -535,7 +539,14 @@ static int __no_inline_not_in_flash_func(usb_in_transaction)(pio_port_t *pp,
535539
if ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
536540
res = -2;
537541
}
538-
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
542+
543+
if (++ep->failed_count > TRANSACTION_MAX_RETRY) {
544+
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS); // failed after 3 consecutive retries
545+
}
546+
}
547+
548+
if (res == 0) {
549+
ep->failed_count = 0; // reset failed count if we got a sound response
539550
}
540551

541552
pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false);
@@ -569,7 +580,14 @@ static int __no_inline_not_in_flash_func(usb_out_transaction)(pio_port_t *pp,
569580
} else if (receive_token == USB_PID_STALL) {
570581
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_STALLED_BITS);
571582
} else {
572-
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
583+
res = -1;
584+
if (++ep->failed_count > TRANSACTION_MAX_RETRY) {
585+
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
586+
}
587+
}
588+
589+
if (res == 0) {
590+
ep->failed_count = 0;// reset failed count if we got a sound response
573591
}
574592

575593
pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false);
@@ -581,7 +599,6 @@ static int __no_inline_not_in_flash_func(usb_out_transaction)(pio_port_t *pp,
581599

582600
static int __no_inline_not_in_flash_func(usb_setup_transaction)(
583601
pio_port_t *pp, endpoint_t *ep) {
584-
585602
int res = 0;
586603

587604
// Setup token
@@ -604,7 +621,13 @@ static int __no_inline_not_in_flash_func(usb_setup_transaction)(
604621
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS);
605622
} else {
606623
res = -1;
607-
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
624+
if (++ep->failed_count > TRANSACTION_MAX_RETRY) {
625+
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
626+
}
627+
}
628+
629+
if (res == 0) {
630+
ep->failed_count = 0;// reset failed count if we got a sound response
608631
}
609632

610633
pp->usb_rx_buffer[1] = 0; // reset buffer

src/usb_definitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ typedef struct {
7575

7676
uint8_t buffer[(64 + 4) * 2 * 7 / 6 + 2];
7777
uint8_t encoded_data_len;
78+
uint8_t failed_count;
79+
7880
uint8_t *app_buf;
7981
uint16_t total_len;
8082
uint16_t actual_len;

0 commit comments

Comments
 (0)