@@ -91,15 +91,20 @@ static void _prep_out_transaction (vendord_interface_t* p_itf)
91
91
{
92
92
uint8_t const rhport = 0 ;
93
93
94
- // skip if previous transfer not complete
95
- if ( usbd_edpt_busy (rhport , p_itf -> ep_out ) ) return ;
94
+ // claim endpoint
95
+ TU_VERIFY ( usbd_edpt_claim (rhport , p_itf -> ep_out ), ) ;
96
96
97
97
// Prepare for incoming data but only allow what we can store in the ring buffer.
98
98
uint16_t max_read = tu_fifo_remaining (& p_itf -> rx_ff );
99
99
if ( max_read >= CFG_TUD_VENDOR_EPSIZE )
100
100
{
101
101
usbd_edpt_xfer (rhport , p_itf -> ep_out , p_itf -> epout_buf , CFG_TUD_VENDOR_EPSIZE );
102
102
}
103
+ else
104
+ {
105
+ // Release endpoint since we don't make any transfer
106
+ usbd_edpt_release (rhport , p_itf -> ep_out );
107
+ }
103
108
}
104
109
105
110
uint32_t tud_vendor_n_read (uint8_t itf , void * buffer , uint32_t bufsize )
@@ -120,37 +125,47 @@ void tud_vendor_n_read_flush (uint8_t itf)
120
125
//--------------------------------------------------------------------+
121
126
// Write API
122
127
//--------------------------------------------------------------------+
123
- static uint16_t maybe_transmit (vendord_interface_t * p_itf )
124
- {
125
- uint8_t const rhport = 0 ;
126
-
127
- // skip if previous transfer not complete
128
- TU_VERIFY ( !usbd_edpt_busy (rhport , p_itf -> ep_in ) );
129
-
130
- uint16_t count = tu_fifo_read_n (& p_itf -> tx_ff , p_itf -> epin_buf , CFG_TUD_VENDOR_EPSIZE );
131
- if (count > 0 )
132
- {
133
- TU_ASSERT ( usbd_edpt_xfer (rhport , p_itf -> ep_in , p_itf -> epin_buf , count ) );
134
- }
135
- return count ;
136
- }
137
-
138
128
uint32_t tud_vendor_n_write (uint8_t itf , void const * buffer , uint32_t bufsize )
139
129
{
140
130
vendord_interface_t * p_itf = & _vendord_itf [itf ];
141
131
uint16_t ret = tu_fifo_write_n (& p_itf -> tx_ff , buffer , (uint16_t ) bufsize );
132
+
133
+ // flush if queue more than packet size
142
134
if (tu_fifo_count (& p_itf -> tx_ff ) >= CFG_TUD_VENDOR_EPSIZE ) {
143
- maybe_transmit ( p_itf );
135
+ tud_vendor_n_write_flush ( itf );
144
136
}
145
137
return ret ;
146
138
}
147
139
148
- uint32_t tud_vendor_n_flush (uint8_t itf )
140
+ uint32_t tud_vendor_n_write_flush (uint8_t itf )
149
141
{
150
142
vendord_interface_t * p_itf = & _vendord_itf [itf ];
151
- uint32_t ret = maybe_transmit (p_itf );
152
143
153
- return ret ;
144
+ // Skip if usb is not ready yet
145
+ TU_VERIFY ( tud_ready (), 0 );
146
+
147
+ // No data to send
148
+ if ( !tu_fifo_count (& p_itf -> tx_ff ) ) return 0 ;
149
+
150
+ uint8_t const rhport = 0 ;
151
+
152
+ // Claim the endpoint
153
+ TU_VERIFY ( usbd_edpt_claim (rhport , p_itf -> ep_in ), 0 );
154
+
155
+ // Pull data from FIFO
156
+ uint16_t const count = tu_fifo_read_n (& p_itf -> tx_ff , p_itf -> epin_buf , sizeof (p_itf -> epin_buf ));
157
+
158
+ if ( count )
159
+ {
160
+ TU_ASSERT ( usbd_edpt_xfer (rhport , p_itf -> ep_in , p_itf -> epin_buf , count ), 0 );
161
+ return count ;
162
+ }else
163
+ {
164
+ // Release endpoint since we don't make any transfer
165
+ // Note: data is dropped if terminal is not connected
166
+ usbd_edpt_release (rhport , p_itf -> ep_in );
167
+ return 0 ;
168
+ }
154
169
}
155
170
156
171
uint32_t tud_vendor_n_write_available (uint8_t itf )
@@ -230,10 +245,10 @@ uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, ui
230
245
// Prepare for incoming data
231
246
if ( p_vendor -> ep_out )
232
247
{
233
- TU_ASSERT ( usbd_edpt_xfer ( rhport , p_vendor -> ep_out , p_vendor -> epout_buf , sizeof ( p_vendor -> epout_buf )), 0 );
248
+ _prep_out_transaction ( p_vendor );
234
249
}
235
250
236
- if ( p_vendor -> ep_in ) maybe_transmit ( p_vendor );
251
+ if ( p_vendor -> ep_in ) tud_vendor_n_write_flush (( uint8_t )( p_vendor - _vendord_itf ) );
237
252
}
238
253
239
254
return (uint16_t ) ((uintptr_t ) p_desc - (uintptr_t ) desc_itf );
@@ -268,7 +283,7 @@ bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
268
283
{
269
284
if (tud_vendor_tx_cb ) tud_vendor_tx_cb (itf , (uint16_t ) xferred_bytes );
270
285
// Send complete, try to send more if possible
271
- maybe_transmit ( p_itf );
286
+ tud_vendor_n_write_flush ( itf );
272
287
}
273
288
274
289
return true;
0 commit comments