@@ -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 {
@@ -388,7 +395,9 @@ void USBCDC::send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now)
388395 }
389396 _tx_size += write_size;
390397 *actual = write_size;
391- if ((CDC_MAX_PACKET_SIZE == size) && (CDC_MAX_PACKET_SIZE == write_size)) {
398+
399+ /* Enable ZLP flag as while send_nb() zero size */
400+ if (size == 0 ) {
392401 _trans_zlp = true ;
393402 }
394403
@@ -409,6 +418,14 @@ void USBCDC::_send_isr_start()
409418 _tx_in_progress = true ;
410419 }
411420 }
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+ }
412429}
413430
414431/*
@@ -419,11 +436,6 @@ void USBCDC::_send_isr()
419436{
420437 assert_locked ();
421438
422- /* Send ZLP write start after last alignment packet sent */
423- if (_trans_zlp && USBDevice::write_start (_bulk_in, _tx_buffer, 0 )) {
424- _trans_zlp = false ;
425- }
426-
427439 write_finish (_bulk_in);
428440 _tx_buf = _tx_buffer;
429441 _tx_size = 0 ;
0 commit comments