Skip to content

Fix host issue with with turnaround (inter-packet) timing #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/pio_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void __not_in_flash_func(pio_usb_bus_usb_transfer)(pio_port_t *pp,
continue;
}
pp->pio_usb_tx->irq = IRQ_TX_ALL_MASK; // clear complete flag
while (*pc < PIO_USB_TX_ENCODED_DATA_COMP) {
while (*pc <= PIO_USB_TX_ENCODED_DATA_COMP) {
continue;
}
}
Expand Down Expand Up @@ -206,6 +206,19 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
const uint16_t rx_buf_len = sizeof(pp->usb_rx_buffer) / sizeof(pp->usb_rx_buffer[0]);
int16_t idx = 0;

// Per USB Specs 7.1.18 for turnaround: We must wait at least 2 bit times for inter-packet delay.
// This is essential for working with LS device specially when we overlocked the mcu.
// Pre-calculate number of cycle per bit time
// - Lowspeed: 1 bit time = (cpufreq / 1.5 Mhz) = clk_div_ls_tx.div_int*6Mhz / 1.5 Mhz = 4 * clk_div_ls_tx.div_int
// - Fullspeed 1 bit time = (cpufreq / 12 Mhz) = clk_div_fs_tx.div_int*48Mhz / 12 Mhz = 4 * clk_div_fs_tx.div_int
// Since there is also overhead, we only wait 1.5 bit for LS and 1 bit for FS
uint32_t turnaround_in_cycle;
if (pp->low_speed) {
turnaround_in_cycle = 6 * pp->clk_div_ls_tx.div_int; // 1.5 bit time
} else {
turnaround_in_cycle = 4 * pp->clk_div_fs_tx.div_int; // 1 bit time
}

// Timeout in seven microseconds. That is enough time to receive one byte at low speed.
// This is to detect packets without an EOP because the device was unplugged.
uint32_t start = get_time_us_32();
Expand All @@ -227,8 +240,10 @@ int __no_inline_not_in_flash_func(pio_usb_bus_receive_packet_and_handshake)(
}
idx++;
} else if ((pp->pio_usb_rx->irq & IRQ_RX_COMP_MASK) != 0) {
// Exit early if we've gotten an EOP. There *might* be a race between EOP
// detection and NRZI decoding but it is unlikely.
// Exit since we've gotten an EOP.
// Timing critical: per USB specs, handshake must be sent within 2-7 bit-time strictly
busy_wait_at_least_cycles(turnaround_in_cycle); // wait for turnaround

if (handshake == USB_PID_ACK) {
// Only ACK if crc matches
if (idx >= 4 && crc_match) {
Expand Down
10 changes: 9 additions & 1 deletion src/pio_usb_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static __unused uint32_t int_stat;
static uint8_t sof_packet[4] = {USB_SYNC, USB_PID_SOF, 0x00, 0x10};
static uint8_t sof_packet_encoded[4 * 2 * 7 / 6 + 2];
static uint8_t sof_packet_encoded_len;
static uint8_t keepalive_encoded[1];

static bool sof_timer(repeating_timer_t *_rt);

Expand Down Expand Up @@ -84,6 +85,7 @@ usb_device_t *pio_usb_host_init(const pio_usb_configuration_t *c) {

sof_packet_encoded_len =
pio_usb_ll_encode_tx_data(sof_packet, sizeof(sof_packet), sof_packet_encoded);
pio_usb_ll_encode_tx_data(NULL, 0, keepalive_encoded);

if (!c->skip_alarm_pool) {
_alarm_pool = c->alarm_pool;
Expand Down Expand Up @@ -266,7 +268,13 @@ void __not_in_flash_func(pio_usb_host_frame)(void) {
continue;
}
configure_root_port(pp, root);
pio_usb_bus_usb_transfer(pp, sof_packet_encoded, sof_packet_encoded_len);
if (root->is_fullspeed) {
// Send SOF for full speed
pio_usb_bus_usb_transfer(pp, sof_packet_encoded, sof_packet_encoded_len);
} else {
// Send Keep alive for low speed
pio_usb_bus_usb_transfer(pp, keepalive_encoded, 1);
}
}

// Carry out all queued endpoint transaction
Expand Down
Loading