Skip to content

Commit 6403ec7

Browse files
authored
Merge pull request #15473 from cyliangtw/master
USBCDC support ZLP
2 parents dd99580 + 126d767 commit 6403ec7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

drivers/usb/include/usb/USBCDC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class USBCDC: public USBDevice {
230230
uint8_t _rx_buffer[CDC_MAX_PACKET_SIZE];
231231
uint8_t *_rx_buf;
232232
uint32_t _rx_size;
233+
bool _trans_zlp;
233234
};
234235

235236
/** @}*/

drivers/usb/source/USBCDC.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class USBCDC::AsyncWrite: public AsyncOp {
3939
AsyncWrite(USBCDC *serial, uint8_t *buf, uint32_t size):
4040
serial(serial), tx_buf(buf), tx_size(size), result(false)
4141
{
42-
42+
need_zlp = (size % CDC_MAX_PACKET_SIZE == 0) ? true : false;
4343
}
4444

4545
virtual ~AsyncWrite()
@@ -59,6 +59,12 @@ class USBCDC::AsyncWrite: public AsyncOp {
5959
tx_size -= actual_size;
6060
tx_buf += actual_size;
6161
if (tx_size == 0) {
62+
// For ZLP case, not ending yet and need one more time to invoke process to send zero packet.
63+
if (need_zlp) {
64+
need_zlp = false;
65+
serial->_send_isr_start();
66+
return false;
67+
}
6268
result = true;
6369
return true;
6470
}
@@ -72,6 +78,7 @@ class USBCDC::AsyncWrite: public AsyncOp {
7278
uint8_t *tx_buf;
7379
uint32_t tx_size;
7480
bool result;
81+
bool need_zlp;
7582
};
7683

7784
class USBCDC::AsyncRead: public AsyncOp {
@@ -186,6 +193,7 @@ void USBCDC::_init()
186193
_rx_in_progress = false;
187194
_rx_buf = _rx_buffer;
188195
_rx_size = 0;
196+
_trans_zlp = false;
189197
}
190198

191199
void USBCDC::callback_reset()
@@ -383,10 +391,16 @@ void USBCDC::send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now)
383391
uint32_t free = sizeof(_tx_buffer) - _tx_size;
384392
uint32_t write_size = free > size ? size : free;
385393
if (size > 0) {
386-
memcpy(_tx_buf, buffer, write_size);
394+
memcpy(_tx_buf + _tx_size, buffer, write_size);
387395
}
388396
_tx_size += write_size;
389397
*actual = write_size;
398+
399+
/* Enable ZLP flag as while send_nb() zero size */
400+
if (size == 0) {
401+
_trans_zlp = true;
402+
}
403+
390404
if (now) {
391405
_send_isr_start();
392406
}
@@ -404,6 +418,14 @@ void USBCDC::_send_isr_start()
404418
_tx_in_progress = true;
405419
}
406420
}
421+
422+
/* Send ZLP write start */
423+
if (!_tx_in_progress && _trans_zlp) {
424+
if (USBDevice::write_start(_bulk_in, _tx_buffer, 0)) {
425+
_tx_in_progress = true;
426+
_trans_zlp = false;
427+
}
428+
}
407429
}
408430

409431
/*

0 commit comments

Comments
 (0)