From 8fe887198b63520c23af9f4c27d9733f6bb18e90 Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 18 Jun 2020 01:13:44 -0700 Subject: [PATCH 1/4] Add tx callback to cdc device Useful for continuous transmission of data, which is difficult currently because there is no notification of tx completion. --- src/class/cdc/cdc_device.c | 3 +++ src/class/cdc/cdc_device.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 9180065c4..b979b11f1 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,6 +416,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); + if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index a2523d8d0..13b59416b 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -128,6 +128,9 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); // Invoked when received `wanted_char` TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); +// Invoked when space becomes available in TX buffer +TU_ATTR_WEAK void tud_cdc_tx_cb(uint8_t itf); + // Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); From 7ae47a93979071ec3c7724c35f1ef55572ad1a1f Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Sat, 20 Jun 2020 22:12:10 -0700 Subject: [PATCH 2/4] Call tud_cdc_tx_cb right after flush to keep tx fifo full --- src/class/cdc/cdc_device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index b979b11f1..d8be48688 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,9 +416,6 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - // invoke transmit callback to possibly refill tx fifo - if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); - if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if @@ -428,6 +425,9 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, NULL, 0); } } + + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); } // nothing to do with notif endpoint for now From 15b063beb287785b2bf84d00361000036afaba2c Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 20 Aug 2020 02:20:01 -0700 Subject: [PATCH 3/4] Smarter CDC TX refill logic --- src/class/cdc/cdc_device.c | 10 ++++++---- src/class/cdc/cdc_device.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index d8be48688..da9e94133 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,7 +416,12 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - if ( 0 == tud_cdc_n_write_flush(itf) ) + uint32_t flushed = tud_cdc_n_write_flush(itf); + + // invoke transmit callback to possibly refill tx fifo + if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf); + + if ( 0 == flushed && tu_fifo_empty(&p_cdc->tx_ff) ) { // There is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero @@ -425,9 +430,6 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, NULL, 0); } } - - // invoke transmit callback to possibly refill tx fifo - if ( tud_cdc_tx_cb && !tu_fifo_full(&p_cdc->tx_ff) ) tud_cdc_tx_cb(itf); } // nothing to do with notif endpoint for now diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 13b59416b..7e9e8ca21 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -129,7 +129,7 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); // Invoked when space becomes available in TX buffer -TU_ATTR_WEAK void tud_cdc_tx_cb(uint8_t itf); +TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf); // Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); From 72183c7bb44ff117d9abac7b6e9c373c0945fb0b Mon Sep 17 00:00:00 2001 From: Gavin Li Date: Thu, 20 Aug 2020 09:59:23 -0700 Subject: [PATCH 4/4] Slight optimization for cdc tx refill --- src/class/cdc/cdc_device.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index da9e94133..513fe203b 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -416,12 +416,10 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_ // Though maybe the baudrate is not really important !!! if ( ep_addr == p_cdc->ep_in ) { - uint32_t flushed = tud_cdc_n_write_flush(itf); - // invoke transmit callback to possibly refill tx fifo if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf); - if ( 0 == flushed && tu_fifo_empty(&p_cdc->tx_ff) ) + if ( 0 == tud_cdc_n_write_flush(itf) ) { // There is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero