Skip to content

Commit

Permalink
Fix USB serial transmit autoflush race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Jul 9, 2021
1 parent 875221c commit fdfd18f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions teensy4/usb_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ int usb_serial_write(const void *buffer, uint32_t size)

if (!usb_configuration) return 0;
while (size > 0) {
tx_noautoflush = 1;
transfer_t *xfer = tx_transfer + tx_head;
int waiting=0;
uint32_t wait_begin_at=0;
Expand All @@ -339,6 +340,8 @@ int usb_serial_write(const void *buffer, uint32_t size)
transmit_previous_timeout = 0;
break;
}
asm("dsb" ::: "memory");
tx_noautoflush = 0;
if (!waiting) {
wait_begin_at = systick_millis_count;
waiting = 1;
Expand All @@ -357,6 +360,7 @@ int usb_serial_write(const void *buffer, uint32_t size)
}
if (!usb_configuration) return sent;
yield();
tx_noautoflush = 1;
}
//digitalWriteFast(3, LOW);
uint8_t *txdata = txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available);
Expand All @@ -381,6 +385,8 @@ int usb_serial_write(const void *buffer, uint32_t size)
size = 0;
timer_start_oneshot();
}
asm("dsb" ::: "memory");
tx_noautoflush = 0;
}
return sent;
}
Expand All @@ -393,6 +399,7 @@ int usb_serial_write_buffer_free(void)
if (i == tx_head) continue;
if (!(usb_transfer_status(tx_transfer + i) & 0x80)) sum += TX_SIZE;
}
asm("dsb" ::: "memory");
tx_noautoflush = 0;
return sum;
}
Expand All @@ -411,6 +418,7 @@ void usb_serial_flush_output(void)
usb_transmit(CDC_TX_ENDPOINT, xfer);
if (++tx_head >= TX_NUM) tx_head = 0;
tx_available = 0;
asm("dsb" ::: "memory");
tx_noautoflush = 0;
}

Expand Down

0 comments on commit fdfd18f

Please sign in to comment.