Skip to content

Commit

Permalink
Merge pull request #159 from hathach/retry-host-transaction
Browse files Browse the repository at this point in the history
retry transaction up to 3 times for usb host
  • Loading branch information
sekigon-gonnoc authored Mar 7, 2025
2 parents 442af43 + ace22e0 commit 0ca3657
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/pio_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ bool __no_inline_not_in_flash_func(pio_usb_ll_transfer_start)(endpoint_t *ep,
ep->app_buf = buffer;
ep->total_len = buflen;
ep->actual_len = 0;
ep->failed_count = 0;

if (ep->is_tx) {
prepare_tx_data(ep);
Expand Down
35 changes: 29 additions & 6 deletions src/pio_usb_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "usb_rx.pio.h"
#include "usb_tx.pio.h"

enum {
TRANSACTION_MAX_RETRY = 3, // Number of times to retry a failed transaction
};

static alarm_pool_t *_alarm_pool = NULL;
static repeating_timer_t sof_rt;
// The sof_count may be incremented and then read on different cores.
Expand Down Expand Up @@ -535,7 +539,14 @@ static int __no_inline_not_in_flash_func(usb_in_transaction)(pio_port_t *pp,
if ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) == 0) {
res = -2;
}
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);

if (++ep->failed_count >= TRANSACTION_MAX_RETRY) {
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS); // failed after 3 consecutive retries
}
}

if (res == 0) {
ep->failed_count = 0; // reset failed count if we got a sound response
}

pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false);
Expand Down Expand Up @@ -569,7 +580,14 @@ static int __no_inline_not_in_flash_func(usb_out_transaction)(pio_port_t *pp,
} else if (receive_token == USB_PID_STALL) {
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_STALLED_BITS);
} else {
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
res = -1;
if (++ep->failed_count >= TRANSACTION_MAX_RETRY) {
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
}
}

if (res == 0) {
ep->failed_count = 0;// reset failed count if we got a sound response
}

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

static int __no_inline_not_in_flash_func(usb_setup_transaction)(
pio_port_t *pp, endpoint_t *ep) {

int res = 0;

// Setup token
Expand All @@ -598,13 +615,19 @@ static int __no_inline_not_in_flash_func(usb_setup_transaction)(
pio_usb_bus_wait_handshake(pp);
pio_sm_set_enabled(pp->pio_usb_rx, pp->sm_rx, false);

ep->actual_len = 8;

if (pp->usb_rx_buffer[0] == USB_SYNC && pp->usb_rx_buffer[1] == USB_PID_ACK) {
ep->actual_len = 8;
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS);
} else {
res = -1;
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
ep->data_id = USB_PID_SETUP; // retry setup
if (++ep->failed_count >= TRANSACTION_MAX_RETRY) {
pio_usb_ll_transfer_complete(ep, PIO_USB_INTS_ENDPOINT_ERROR_BITS);
}
}

if (res == 0) {
ep->failed_count = 0;// reset failed count if we got a sound response
}

pp->usb_rx_buffer[1] = 0; // reset buffer
Expand Down
2 changes: 2 additions & 0 deletions src/usb_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ typedef struct {

uint8_t buffer[(64 + 4) * 2 * 7 / 6 + 2];
uint8_t encoded_data_len;
uint8_t failed_count;

uint8_t *app_buf;
uint16_t total_len;
uint16_t actual_len;
Expand Down

0 comments on commit 0ca3657

Please sign in to comment.