Skip to content

Commit

Permalink
Merge pull request ARMmbed#15473 from cyliangtw/master
Browse files Browse the repository at this point in the history
USBCDC support ZLP
  • Loading branch information
0xc0170 authored Dec 19, 2023
2 parents dd99580 + 126d767 commit 6403ec7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 6403ec7

Please sign in to comment.