Skip to content

Commit 876f49f

Browse files
authored
Update vendor_device.c
1 parent faaed19 commit 876f49f

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/class/vendor/vendor_device.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef struct
5959
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE];
6060
} vendord_interface_t;
6161

62-
CFG_TUSB_MEM_SECTION tu_static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR];
62+
CFG_TUSB_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR];
6363

6464
#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff)
6565

@@ -86,15 +86,20 @@ static void _prep_out_transaction (vendord_interface_t* p_itf)
8686
{
8787
uint8_t const rhport = 0;
8888

89-
// skip if previous transfer not complete
90-
if ( usbd_edpt_busy(rhport, p_itf->ep_out) ) return;
89+
// claim endpoint
90+
TU_VERIFY(usbd_edpt_claim(rhport, p_itf->ep_out), );
9191

9292
// Prepare for incoming data but only allow what we can store in the ring buffer.
9393
uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff);
9494
if ( max_read >= CFG_TUD_VENDOR_EPSIZE )
9595
{
9696
usbd_edpt_xfer(rhport, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE);
9797
}
98+
else
99+
{
100+
// Release endpoint since we don't make any transfer
101+
usbd_edpt_release(rhport, p_itf->ep_out);
102+
}
98103
}
99104

100105
uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize)
@@ -115,37 +120,47 @@ void tud_vendor_n_read_flush (uint8_t itf)
115120
//--------------------------------------------------------------------+
116121
// Write API
117122
//--------------------------------------------------------------------+
118-
static uint16_t maybe_transmit(vendord_interface_t* p_itf)
119-
{
120-
uint8_t const rhport = 0;
121-
122-
// skip if previous transfer not complete
123-
TU_VERIFY( !usbd_edpt_busy(rhport, p_itf->ep_in) );
124-
125-
uint16_t count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE);
126-
if (count > 0)
127-
{
128-
TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_in, p_itf->epin_buf, count) );
129-
}
130-
return count;
131-
}
132-
133123
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize)
134124
{
135125
vendord_interface_t* p_itf = &_vendord_itf[itf];
136126
uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, (uint16_t) bufsize);
127+
128+
// flush if queue more than packet size
137129
if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) {
138-
maybe_transmit(p_itf);
130+
tud_vendor_n_write_flush(itf);
139131
}
140132
return ret;
141133
}
142134

143-
uint32_t tud_vendor_n_flush (uint8_t itf)
135+
uint32_t tud_vendor_n_write_flush (uint8_t itf)
144136
{
145137
vendord_interface_t* p_itf = &_vendord_itf[itf];
146-
uint32_t ret = maybe_transmit(p_itf);
147138

148-
return ret;
139+
// Skip if usb is not ready yet
140+
TU_VERIFY( tud_ready(), 0 );
141+
142+
// No data to send
143+
if ( !tu_fifo_count(&p_itf->tx_ff) ) return 0;
144+
145+
uint8_t const rhport = 0;
146+
147+
// Claim the endpoint
148+
TU_VERIFY( usbd_edpt_claim(rhport, p_itf->ep_in), 0 );
149+
150+
// Pull data from FIFO
151+
uint16_t const count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, sizeof(p_itf->epin_buf));
152+
153+
if ( count )
154+
{
155+
TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_in, p_itf->epin_buf, count), 0 );
156+
return count;
157+
}else
158+
{
159+
// Release endpoint since we don't make any transfer
160+
// Note: data is dropped if terminal is not connected
161+
usbd_edpt_release(rhport, p_itf->ep_in);
162+
return 0;
163+
}
149164
}
150165

151166
uint32_t tud_vendor_n_write_available (uint8_t itf)
@@ -225,10 +240,10 @@ uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, ui
225240
// Prepare for incoming data
226241
if ( p_vendor->ep_out )
227242
{
228-
TU_ASSERT(usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf)), 0);
243+
_prep_out_transaction(p_vendor);
229244
}
230245

231-
if ( p_vendor->ep_in ) maybe_transmit(p_vendor);
246+
if ( p_vendor->ep_in ) tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf));
232247
}
233248

234249
return (uint16_t) ((uintptr_t) p_desc - (uintptr_t) desc_itf);
@@ -263,7 +278,7 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
263278
{
264279
if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes);
265280
// Send complete, try to send more if possible
266-
maybe_transmit(p_itf);
281+
tud_vendor_n_write_flush(itf);
267282
}
268283

269284
return true;

0 commit comments

Comments
 (0)