Skip to content

Commit cd9008e

Browse files
committed
add tuh_cdc_tx_complete_cb() callback
1 parent 84a483f commit cd9008e

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ void tuh_cdc_rx_cb(uint8_t idx)
8787

8888
void tuh_cdc_mount_cb(uint8_t idx)
8989
{
90-
tuh_cdc_itf_info_t itf_info;
90+
tuh_cdc_itf_info_t itf_info = { 0 };
9191
tuh_cdc_itf_get_info(idx, &itf_info);
9292

9393
printf("CDC Interface is mounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
9494
}
9595

9696
void tuh_cdc_umount_cb(uint8_t idx)
9797
{
98-
tuh_cdc_itf_info_t itf_info;
98+
tuh_cdc_itf_info_t itf_info = { 0 };
9999
tuh_cdc_itf_get_info(idx, &itf_info);
100100

101101
printf("CDC Interface is unmounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);

src/class/cdc/cdc_host.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,19 @@ static inline cdch_interface_t* get_itf(uint8_t idx)
238238
return (p_cdc->daddr != 0) ? p_cdc : NULL;
239239
}
240240

241-
TU_ATTR_ALWAYS_INLINE
242-
static inline uint8_t itf2idx(cdch_interface_t* p_cdc)
243-
{
244-
return (uint8_t) (p_cdc - cdch_data);
245-
}
246-
247-
static inline cdch_interface_t* get_itf_by_ep_addr(uint8_t daddr, uint8_t ep_addr)
241+
static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr)
248242
{
249243
for(uint8_t i=0; i<CFG_TUH_CDC; i++)
250244
{
251245
cdch_interface_t* p_cdc = &cdch_data[i];
252246
if ( (p_cdc->daddr == daddr) &&
253247
(ep_addr == p_cdc->ep_notif || ep_addr == p_cdc->stream.rx.ep_addr || ep_addr == p_cdc->stream.tx.ep_addr))
254248
{
255-
return p_cdc;
249+
return i;
256250
}
257251
}
258252

259-
return NULL;
253+
return TUSB_INDEX_INVALID;
260254
}
261255

262256

@@ -478,11 +472,15 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
478472
// TODO handle stall response, retry failed transfer ...
479473
TU_ASSERT(event == XFER_RESULT_SUCCESS);
480474

481-
cdch_interface_t * p_cdc = get_itf_by_ep_addr(daddr, ep_addr);
475+
uint8_t const idx = get_idx_by_ep_addr(daddr, ep_addr);
476+
cdch_interface_t * p_cdc = get_itf(idx);
482477
TU_ASSERT(p_cdc);
483478

484479
if ( ep_addr == p_cdc->stream.tx.ep_addr )
485480
{
481+
// invoke tx complete callback to possibly refill tx fifo
482+
if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx);
483+
486484
if ( 0 == tu_edpt_stream_write_xfer(daddr, &p_cdc->stream.tx) )
487485
{
488486
// If there is no data left, a ZLP should be sent if needed
@@ -496,7 +494,7 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t
496494
if (xferred_bytes) tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes);
497495

498496
// invoke receive callback
499-
if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(itf2idx(p_cdc));
497+
if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx);
500498

501499
// prepare for next transfer if needed
502500
tu_edpt_stream_read_xfer(daddr, &p_cdc->stream.rx);

src/class/cdc/cdc_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx);
154154
// Invoked when received new data
155155
TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx);
156156

157+
// Invoked when a TX is complete and therefore space becomes available in TX buffer
158+
TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx);
159+
157160
//--------------------------------------------------------------------+
158161
// CDC APPLICATION CALLBACKS
159162
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)