Skip to content

Commit 0ca3657

Browse files
Merge pull request #159 from hathach/retry-host-transaction
retry transaction up to 3 times for usb host
2 parents 442af43 + ace22e0 commit 0ca3657

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
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: 29 additions & 6 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
@@ -598,13 +615,19 @@ static int __no_inline_not_in_flash_func(usb_setup_transaction)(
598615
pio_usb_bus_wait_handshake(pp);
599616
pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false);
600617

601-
ep->actual_len = 8;
602-
603618
if (pp->usb_rx_buffer[0] == USB_SYNC && pp->usb_rx_buffer[1] == USB_PID_ACK) {
619+
ep->actual_len = 8;
604620
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS);
605621
} else {
606622
res = -1;
607-
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
623+
ep->data_id = USB_PID_SETUP; // retry setup
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)