Skip to content
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

USBCDC support ZLP #15473

Merged
merged 6 commits into from
Dec 19, 2023
Merged
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
1 change: 1 addition & 0 deletions drivers/usb/include/usb/USBCDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class USBCDC: public USBDevice {
uint8_t _rx_buffer[CDC_MAX_PACKET_SIZE];
uint8_t *_rx_buf;
uint32_t _rx_size;
bool _trans_zlp;
};

/** @}*/
Expand Down
26 changes: 24 additions & 2 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 @@ -186,6 +193,7 @@ void USBCDC::_init()
_rx_in_progress = false;
_rx_buf = _rx_buffer;
_rx_size = 0;
_trans_zlp = false;
}

void USBCDC::callback_reset()
Expand Down Expand Up @@ -383,10 +391,16 @@ void USBCDC::send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now)
uint32_t free = sizeof(_tx_buffer) - _tx_size;
uint32_t write_size = free > size ? size : free;
if (size > 0) {
memcpy(_tx_buf, buffer, write_size);
memcpy(_tx_buf + _tx_size, buffer, write_size);
}
_tx_size += write_size;
*actual = write_size;

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

if (now) {
_send_isr_start();
}
Expand All @@ -404,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 Down
Loading