@@ -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
7784class 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
191199void 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