Skip to content

Commit

Permalink
To judge send ZLP or not in USBCDC::AsyncWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
cyliangtw committed Dec 8, 2023
1 parent ca616c8 commit c30af6a
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions drivers/usb/source/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class USBCDC::AsyncWrite: public AsyncOp {
AsyncWrite(USBCDC *serial, uint8_t *buf, uint32_t size):
serial(serial), tx_buf(buf), tx_size(size), result(false)
{

need_zlp = (size % CDC_MAX_PACKET_SIZE == 0) ? true : false;
}

virtual ~AsyncWrite()
Expand All @@ -59,6 +59,12 @@ class USBCDC::AsyncWrite: public AsyncOp {
tx_size -= actual_size;
tx_buf += actual_size;
if (tx_size == 0) {
// For ZLP case, not ending yet and need one more time to invoke process to send zero packet.
if (need_zlp) {
need_zlp = false;
serial->_send_isr_start();
return false;
}
result = true;
return true;
}
Expand All @@ -72,6 +78,7 @@ class USBCDC::AsyncWrite: public AsyncOp {
uint8_t *tx_buf;
uint32_t tx_size;
bool result;
bool need_zlp;
};

class USBCDC::AsyncRead: public AsyncOp {
Expand Down Expand Up @@ -388,7 +395,9 @@ void USBCDC::send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now)
}
_tx_size += write_size;
*actual = write_size;
if ((CDC_MAX_PACKET_SIZE == size) && (CDC_MAX_PACKET_SIZE == write_size)) {

/* Enable ZLP flag as while send_nb() zero size */
if (size == 0) {
_trans_zlp = true;
}

Expand All @@ -409,6 +418,14 @@ void USBCDC::_send_isr_start()
_tx_in_progress = true;
}
}

/* Send ZLP write start */
if (!_tx_in_progress && _trans_zlp) {
if (USBDevice::write_start(_bulk_in, _tx_buffer, 0)) {
_tx_in_progress = true;
_trans_zlp = false;
}
}
}

/*
Expand All @@ -419,11 +436,6 @@ void USBCDC::_send_isr()
{
assert_locked();

/* Send ZLP write start after last alignment packet sent */
if (_trans_zlp && USBDevice::write_start(_bulk_in, _tx_buffer, 0)) {
_trans_zlp = false;
}

write_finish(_bulk_in);
_tx_buf = _tx_buffer;
_tx_size = 0;
Expand Down

0 comments on commit c30af6a

Please sign in to comment.