Skip to content

Commit 22b62f8

Browse files
committed
add tu_edpt_stream_write_zlp_if_needed()
1 parent 76021c7 commit 22b62f8

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/class/cdc/cdc_host.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,46 @@ bool tu_edpt_stream_clear(tu_edpt_stream_t* s)
7676
return tu_fifo_clear(&s->ff);
7777
}
7878

79-
uint32_t tud_edpt_stream_write_xfer(uint8_t dev_addr, tu_edpt_stream_t* s)
79+
bool tu_edpt_stream_write_zlp_if_needed(uint8_t daddr, tu_edpt_stream_t* s, uint32_t last_xferred_bytes)
8080
{
81-
// No data to send
82-
if ( !tu_fifo_count(&s->ff) ) return 0;
81+
uint16_t const bulk_packet_size = (tuh_speed_get(daddr) == TUSB_SPEED_HIGH) ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
82+
83+
// ZLP condition: no pending data, last transferred bytes is multiple of packet size
84+
TU_VERIFY( !tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (bulk_packet_size-1))) );
85+
86+
if ( usbh_edpt_claim(daddr, s->ep_addr) )
87+
{
88+
TU_ASSERT( usbh_edpt_xfer(daddr, s->ep_addr, NULL, 0) );
89+
}
90+
91+
return true;
92+
}
93+
94+
uint32_t tu_edpt_stream_write_xfer(uint8_t daddr, tu_edpt_stream_t* s)
95+
{
96+
// skip if no data
97+
TU_VERIFY( tu_fifo_count(&s->ff), 0 );
8398

8499
// Claim the endpoint
85100
// uint8_t const rhport = 0;
86101
// TU_VERIFY( usbd_edpt_claim(rhport, p_cdc->ep_in), 0 );
87-
TU_VERIFY( usbh_edpt_claim(dev_addr, s->ep_addr) );
102+
TU_VERIFY( usbh_edpt_claim(daddr, s->ep_addr) );
88103

89104
// Pull data from FIFO -> EP buf
90105
uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize);
91106

92107
if ( count )
93108
{
94109
//TU_ASSERT( usbd_edpt_xfer(rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0 );
95-
TU_ASSERT( usbh_edpt_xfer(dev_addr, s->ep_addr, s->ep_buf, count), 0 );
110+
TU_ASSERT( usbh_edpt_xfer(daddr, s->ep_addr, s->ep_buf, count), 0 );
96111
return count;
97112
}else
98113
{
99114
// Release endpoint since we don't make any transfer
100115
// Note: data is dropped if terminal is not connected
101116
//usbd_edpt_release(rhport, p_cdc->ep_in);
102117

103-
usbh_edpt_release(dev_addr, s->ep_addr);
118+
usbh_edpt_release(daddr, s->ep_addr);
104119
return 0;
105120
}
106121
}
@@ -114,7 +129,7 @@ uint32_t tu_edpt_stream_write(uint8_t daddr, tu_edpt_stream_t* s, void const *bu
114129
if ( (tu_fifo_count(&s->ff) >= bulk_packet_size)
115130
/* || ((CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE) && tu_fifo_full(&p_cdc->tx_ff)) */ )
116131
{
117-
tud_edpt_stream_write_xfer(daddr, s);
132+
tu_edpt_stream_write_xfer(daddr, s);
118133
}
119134

120135
return ret;
@@ -302,7 +317,7 @@ uint32_t tuh_cdc_write_flush(uint8_t idx)
302317
cdch_interface_t* p_cdc = get_itf(idx);
303318
TU_VERIFY(p_cdc);
304319

305-
return tud_edpt_stream_write_xfer(p_cdc->daddr, &p_cdc->stream.tx);
320+
return tu_edpt_stream_write_xfer(p_cdc->daddr, &p_cdc->stream.tx);
306321
}
307322

308323
uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize)
@@ -435,18 +450,11 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
435450

436451
if ( ep_addr == p_cdc->stream.tx.ep_addr )
437452
{
438-
if ( 0 == tud_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) )
453+
if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) )
439454
{
440-
// If there is no data left, a ZLP should be sent if
455+
// If there is no data left, a ZLP should be sent if needed
441456
// xferred_bytes is multiple of EP Packet size and not zero
442-
uint16_t const bulk_packet_size = (tuh_speed_get(daddr) == TUSB_SPEED_HIGH) ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS;
443-
if ( !tu_fifo_count(&p_cdc->stream.tx.ff) && xferred_bytes && (0 == (xferred_bytes & (bulk_packet_size-1))) )
444-
{
445-
if ( usbh_edpt_claim(daddr, p_cdc->stream.tx.ep_addr) )
446-
{
447-
usbh_edpt_xfer(daddr, p_cdc->stream.tx.ep_addr, NULL, 0);
448-
}
449-
}
457+
tu_edpt_stream_write_zlp_if_needed(daddr, &p_cdc->stream.tx, xferred_bytes);
450458
}
451459
}
452460
else if ( ep_addr == p_cdc->stream.rx.ep_addr )

0 commit comments

Comments
 (0)