From c30af6a7dfccccbe44d5e5e6c9bd4b9fcb03f031 Mon Sep 17 00:00:00 2001 From: Chris Liang Date: Wed, 6 Dec 2023 19:19:27 +0800 Subject: [PATCH] To judge send ZLP or not in USBCDC::AsyncWrite --- drivers/usb/source/USBCDC.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/drivers/usb/source/USBCDC.cpp b/drivers/usb/source/USBCDC.cpp index 3b0f8021f34..827e7536e5a 100644 --- a/drivers/usb/source/USBCDC.cpp +++ b/drivers/usb/source/USBCDC.cpp @@ -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() @@ -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; } @@ -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 { @@ -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; } @@ -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; + } + } } /* @@ -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;