From d9eaa54e14775ebcf6a78cb84c354e69e57903d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Thu, 3 Sep 2020 16:40:53 +0200 Subject: [PATCH 01/14] Remove connected check for write flushing --- src/class/cdc/cdc_device.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index b4ebfc92..91799b2d 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -165,11 +165,7 @@ uint32_t tud_cdc_n_write_flush (uint8_t itf) TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in), 0 ); uint16_t count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf)); - if ( count ) - { - TU_VERIFY( tud_cdc_n_connected(itf), 0 ); // fifo is empty if not connected - TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count), 0 ); - } + if ( count ) TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count), 0 ); return count; } From fd6cf9010b522019a4434ebd02fb447b567e7131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Thu, 3 Sep 2020 17:03:13 +0200 Subject: [PATCH 02/14] Turn transmit fifo overwritable in no DTR mode --- src/class/cdc/cdc_device.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 91799b2d..6c0f5fc7 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -197,7 +197,7 @@ void cdcd_init(void) // config fifo tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false); - tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, false); + tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true); #if CFG_FIFO_MUTEX tu_fifo_config_mutex(&p_cdc->rx_ff, osal_mutex_create(&p_cdc->rx_ff_mutex)); @@ -355,6 +355,9 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request p_cdc->line_state = (uint8_t) request->wValue; + // Disable fifo overwriting if DTR bit is set + p_cdc->tx_ff.overwritable = dtr ? false : true; + TU_LOG2(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts); tud_control_status(rhport, request); From 4071e490e259de5bf2891c9893ab7965b5be3d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Thu, 3 Sep 2020 17:21:32 +0200 Subject: [PATCH 03/14] New function to modify fifo overwritability --- src/class/cdc/cdc_device.c | 2 +- src/common/tusb_fifo.c | 19 +++++++++++++++++++ src/common/tusb_fifo.h | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 6c0f5fc7..6f71cb78 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -356,7 +356,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request p_cdc->line_state = (uint8_t) request->wValue; // Disable fifo overwriting if DTR bit is set - p_cdc->tx_ff.overwritable = dtr ? false : true; + tu_fifo_change_mode(&p_cdc->tx_ff, (dtr?false:true)); TU_LOG2(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts); diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index 6ab158cc..fdd8c3e3 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -319,3 +319,22 @@ bool tu_fifo_clear(tu_fifo_t *f) return true; } + +/******************************************************************************/ +/*! + @brief Change the fifo mode to overwritable or not overwritable + + @param[in] f + Pointer to the FIFO buffer to manipulate +*/ +/******************************************************************************/ +bool tu_fifo_change_mode(tu_fifo_t *f, bool overwritable) +{ + tu_fifo_lock(f); + + f->overwritable = overwritable; + + tu_fifo_unlock(f); + + return true; +} diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index fb0c896f..7957d83a 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -76,6 +76,7 @@ typedef struct .overwritable = _overwritable, \ } +bool tu_fifo_change_mode(tu_fifo_t *f, bool overwritable); bool tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable); From 9cc22b635c5c4b9426b1530c3623b899dc652302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Fri, 4 Sep 2020 15:40:23 +0200 Subject: [PATCH 04/14] Add functionality to abort an ongoing transfer --- src/class/cdc/cdc_device.c | 10 ++++++++++ src/device/dcd.h | 3 +++ src/device/usbd.c | 27 +++++++++++++++++++++++++++ src/device/usbd_pvt.h | 3 +++ 4 files changed, 43 insertions(+) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 6f71cb78..56cc94ab 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -353,6 +353,16 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request bool const dtr = tu_bit_test(request->wValue, 0); bool const rts = tu_bit_test(request->wValue, 1); +#if CFG_TUD_CDC_CLEAR_AT_CONNECTION + // DTE connected event (if DTE supports DTR bit) + if ( dtr && !tu_bit_test(p_cdc->line_state, 0) ) + { + // Clear not transmitted data + usbd_edpt_xfer_abort(rhport, p_cdc->ep_in); + tu_fifo_clear(&p_cdc->tx_ff); + } +#endif + p_cdc->line_state = (uint8_t) request->wValue; // Disable fifo overwriting if DTR bit is set diff --git a/src/device/dcd.h b/src/device/dcd.h index a4635346..ee41fefd 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -133,6 +133,9 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) TU_ATTR_WEAK; // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); +// Abort a ongoing transfer +TU_ATTR_WEAK bool dcd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr); + // Stall endpoint void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr); diff --git a/src/device/usbd.c b/src/device/usbd.c index 6b91048b..04ae31ce 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1100,6 +1100,33 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t } } +bool usbd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr) +{ + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); + + TU_LOG2(" Abort XFER EP %02X ... ", ep_addr); + + // Abort API is optional for DCD + if ( dcd_edpt_xfer_abort ) + { + if ( dcd_edpt_xfer_abort(rhport, ep_addr) ) + { + _usbd_dev.ep_status[epnum][dir].busy = false; + TU_LOG2("OK\r\n"); + return true; + }else + { + TU_LOG2("failed\r\n"); + return false; + } + }else + { + TU_LOG2("no DCD support\r\n"); + return false; + } +} + bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) { (void) rhport; diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index a5d22332..d87c07c0 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -70,6 +70,9 @@ void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr); // Submit a usb transfer bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); +// Abort a scheduled transfer +bool usbd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr); + // Check if endpoint transferring is complete bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr); From e09ebea7b99e3385bc1b1d326923b0a36003275d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Fri, 4 Sep 2020 17:25:32 +0200 Subject: [PATCH 05/14] Remove tud_cdc_connected check from examples --- examples/device/cdc_dual_ports/src/main.c | 15 ++++------ examples/device/cdc_msc/src/main.c | 31 ++++++++++----------- examples/device/cdc_msc_freertos/src/main.c | 31 ++++++++++----------- 3 files changed, 34 insertions(+), 43 deletions(-) diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index d6e38df8..81363917 100644 --- a/examples/device/cdc_dual_ports/src/main.c +++ b/examples/device/cdc_dual_ports/src/main.c @@ -83,18 +83,15 @@ static void cdc_task(void) for (itf = 0; itf < CFG_TUD_CDC; itf++) { - if ( tud_cdc_n_connected(itf) ) + if ( tud_cdc_n_available(itf) ) { - if ( tud_cdc_n_available(itf) ) - { - uint8_t buf[64]; + uint8_t buf[64]; - uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf)); + uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf)); - // echo back to both serial ports - echo_serial_port(0, buf, count); - echo_serial_port(1, buf, count); - } + // echo back to both serial ports + echo_serial_port(0, buf, count); + echo_serial_port(1, buf, count); } } } diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index a407e2dd..bcf05eaa 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -105,34 +105,31 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ void cdc_task(void) { - if ( tud_cdc_connected() ) + // is data available to read from rx fifo + if ( tud_cdc_available() ) { - // connected and there are data available - if ( tud_cdc_available() ) + uint8_t buf[64]; + + // read and echo back + uint32_t count = tud_cdc_read(buf, sizeof(buf)); + + for(uint32_t i=0; i Date: Thu, 10 Sep 2020 13:36:07 +0200 Subject: [PATCH 06/14] Set new define because of build failure --- src/class/cdc/cdc_device.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 145381c4..1d7a6482 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -43,6 +43,10 @@ #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #endif +#ifndef CFG_TUD_CDC_CLEAR_AT_CONNECTION + #define CFG_TUD_CDC_CLEAR_AT_CONNECTION 0 +#endif + #ifdef __cplusplus extern "C" { #endif From 54e29e9ff451d5e30eb59d0411adbe5d4993c59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Wed, 18 Nov 2020 09:42:50 +0100 Subject: [PATCH 07/14] Implementation of the discussed changes - remove usbd_edpt_xfer_abort - rename tu_fifo_change_mode to tu_fifo_set_mode - remove CFG_TUD_CDC_CLEAR_AT_CONNECTION definition - remove auto fifo clear at connection event - add tud_cdc_n_write_clear function --- src/class/cdc/cdc_device.c | 21 +++++++++++---------- src/class/cdc/cdc_device.h | 13 +++++++++---- src/common/tusb_fifo.c | 4 +++- src/common/tusb_fifo.h | 2 +- src/device/usbd.c | 27 --------------------------- src/device/usbd_pvt.h | 3 --- 6 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 7eace24f..2d0aed23 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -207,6 +207,10 @@ uint32_t tud_cdc_n_write_available (uint8_t itf) return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff); } +bool tud_cdc_n_write_clear (uint8_t itf) +{ + return tu_fifo_clear(&_cdcd_itf[itf].tx_ff); +} //--------------------------------------------------------------------+ // USBD Driver API @@ -229,6 +233,9 @@ void cdcd_init(void) // config fifo tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false); + // tx fifo is set to overwritable at initialization and will be changed to not overwritable + // if terminal supports DTR bit. Without DTR we do not know if data is actually polled by terminal. + // In this way, the most current data is prioritized. tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true); #if CFG_FIFO_MUTEX @@ -384,20 +391,14 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request bool const dtr = tu_bit_test(request->wValue, 0); bool const rts = tu_bit_test(request->wValue, 1); -#if CFG_TUD_CDC_CLEAR_AT_CONNECTION - // DTE connected event (if DTE supports DTR bit) - if ( dtr && !tu_bit_test(p_cdc->line_state, 0) ) - { - // Clear not transmitted data - usbd_edpt_xfer_abort(rhport, p_cdc->ep_in); - tu_fifo_clear(&p_cdc->tx_ff); - } -#endif + // TODO if terminal supports DTR we can check for an connection event here and + // clear the fifo as well as ongoing transfers with new usbd_edpt_xfer_abort api. + // Until then user can self clear the buffer with tud_cdc_n_write_clear in tud_cdc_line_state_cb p_cdc->line_state = (uint8_t) request->wValue; // Disable fifo overwriting if DTR bit is set - tu_fifo_change_mode(&p_cdc->tx_ff, (dtr?false:true)); + tu_fifo_set_mode(&p_cdc->tx_ff, !dtr); TU_LOG2(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts); diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 3bd2741b..34202596 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -43,10 +43,6 @@ #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #endif -#ifndef CFG_TUD_CDC_CLEAR_AT_CONNECTION - #define CFG_TUD_CDC_CLEAR_AT_CONNECTION 0 -#endif - #ifdef __cplusplus extern "C" { #endif @@ -106,6 +102,9 @@ uint32_t tud_cdc_n_write_flush (uint8_t itf); // Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation. uint32_t tud_cdc_n_write_available (uint8_t itf); +// Clear the transmit FIFO +bool tud_cdc_n_write_clear (uint8_t itf); + //--------------------------------------------------------------------+ // Application API (Single Port) //--------------------------------------------------------------------+ @@ -125,6 +124,7 @@ static inline uint32_t tud_cdc_write (void const* buffer, uint32_t buf static inline uint32_t tud_cdc_write_str (char const* str); static inline uint32_t tud_cdc_write_flush (void); static inline uint32_t tud_cdc_write_available (void); +static inline bool tud_cdc_write_clear (void); //--------------------------------------------------------------------+ // Application Callback API (weak is optional) @@ -234,6 +234,11 @@ static inline uint32_t tud_cdc_write_available(void) return tud_cdc_n_write_available(0); } +static inline bool tud_cdc_write_clear(void) +{ + return tud_cdc_n_write_clear(0); +} + /** @} */ /** @} */ diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index 470e26b7..444c60f9 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -603,9 +603,11 @@ bool tu_fifo_clear(tu_fifo_t *f) @param[in] f Pointer to the FIFO buffer to manipulate + @param[in] overwritable + Overwritable mode the fifo is set to */ /******************************************************************************/ -bool tu_fifo_change_mode(tu_fifo_t *f, bool overwritable) +bool tu_fifo_set_mode(tu_fifo_t *f, bool overwritable) { tu_fifo_lock(f); diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index 603a7460..abb301b5 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -89,7 +89,7 @@ typedef struct .non_used_index_space = 0xFFFF - 2*_depth-1, \ } -bool tu_fifo_change_mode(tu_fifo_t *f, bool overwritable); +bool tu_fifo_set_mode(tu_fifo_t *f, bool overwritable); bool tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable); diff --git a/src/device/usbd.c b/src/device/usbd.c index 8d3fbac5..90edc3dd 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1191,33 +1191,6 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t } } -bool usbd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr) -{ - uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); - - TU_LOG2(" Abort XFER EP %02X ... ", ep_addr); - - // Abort API is optional for DCD - if ( dcd_edpt_xfer_abort ) - { - if ( dcd_edpt_xfer_abort(rhport, ep_addr) ) - { - _usbd_dev.ep_status[epnum][dir].busy = false; - TU_LOG2("OK\r\n"); - return true; - }else - { - TU_LOG2("failed\r\n"); - return false; - } - }else - { - TU_LOG2("no DCD support\r\n"); - return false; - } -} - bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) { (void) rhport; diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 4eb17a50..09b28558 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -70,9 +70,6 @@ void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr); // Submit a usb transfer bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); -// Abort a scheduled transfer -bool usbd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr); - // Claim an endpoint before submitting a transfer. // If caller does not make any transfer, it must release endpoint for others. bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr); From e7069da7eb41c8af6801b1404acd81d745eb8b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Wed, 18 Nov 2020 09:47:39 +0100 Subject: [PATCH 08/14] Reset CDC examples to original state --- examples/device/cdc_dual_ports/src/main.c | 15 ++++++---- examples/device/cdc_msc/src/main.c | 31 +++++++++++---------- examples/device/cdc_msc_freertos/src/main.c | 31 +++++++++++---------- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index 81363917..d6e38df8 100644 --- a/examples/device/cdc_dual_ports/src/main.c +++ b/examples/device/cdc_dual_ports/src/main.c @@ -83,15 +83,18 @@ static void cdc_task(void) for (itf = 0; itf < CFG_TUD_CDC; itf++) { - if ( tud_cdc_n_available(itf) ) + if ( tud_cdc_n_connected(itf) ) { - uint8_t buf[64]; + if ( tud_cdc_n_available(itf) ) + { + uint8_t buf[64]; - uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf)); + uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf)); - // echo back to both serial ports - echo_serial_port(0, buf, count); - echo_serial_port(1, buf, count); + // echo back to both serial ports + echo_serial_port(0, buf, count); + echo_serial_port(1, buf, count); + } } } } diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index bcf05eaa..a407e2dd 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -105,31 +105,34 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ void cdc_task(void) { - // is data available to read from rx fifo - if ( tud_cdc_available() ) + if ( tud_cdc_connected() ) { - uint8_t buf[64]; - - // read and echo back - uint32_t count = tud_cdc_read(buf, sizeof(buf)); - - for(uint32_t i=0; i Date: Wed, 18 Nov 2020 10:16:32 +0100 Subject: [PATCH 09/14] Changes to CDC example code: - auto flush welcome message at connection event - provide information to the user if the terminal did not set DTR --- examples/device/cdc_msc/src/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index a407e2dd..e4477927 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -137,6 +137,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { // print initial message when connected tud_cdc_write_str("\r\nTinyUSB CDC MSC device example\r\n"); + tud_cdc_write_flush(); } } @@ -144,6 +145,16 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) void tud_cdc_rx_cb(uint8_t itf) { (void) itf; + uint8_t const line_state = tud_cdc_get_line_state(); + + // Provide information that terminal did not set DTR bit + if( !(line_state & 0x01) ) + { + tud_cdc_write_str("\r\nTinyUSB example: Your terminal did not set DTR bit\r\n"); + tud_cdc_write_flush(); + // Clear rx fifo since we do not read the data + tud_cdc_read_flush(); + } } //--------------------------------------------------------------------+ From 339b1a772350321d0f9d4f84e93c5360247e3f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20D=C3=BCmpelmann?= Date: Wed, 18 Nov 2020 11:12:06 +0100 Subject: [PATCH 10/14] Remove dcd_edpt_xfer_abort from DCD header --- src/device/dcd.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/device/dcd.h b/src/device/dcd.h index 2c4bf434..776f782b 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -133,9 +133,6 @@ void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) TU_ATTR_WEAK; // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); -// Abort a ongoing transfer -TU_ATTR_WEAK bool dcd_edpt_xfer_abort(uint8_t rhport, uint8_t ep_addr); - // Stall endpoint void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr); From 4b4f88078503648b38444feee1fda322b65baa43 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 23 Nov 2020 23:40:13 +0700 Subject: [PATCH 11/14] add tud_ready() check in tud_cdc_n_write_flush() other clean up --- examples/device/cdc_msc/src/main.c | 10 ---------- src/class/cdc/cdc_device.c | 19 +++++++++++++------ src/common/tusb_fifo.c | 2 +- src/common/tusb_fifo.h | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index e4477927..cf2bfd7c 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -145,16 +145,6 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) void tud_cdc_rx_cb(uint8_t itf) { (void) itf; - uint8_t const line_state = tud_cdc_get_line_state(); - - // Provide information that terminal did not set DTR bit - if( !(line_state & 0x01) ) - { - tud_cdc_write_str("\r\nTinyUSB example: Your terminal did not set DTR bit\r\n"); - tud_cdc_write_flush(); - // Clear rx fifo since we do not read the data - tud_cdc_read_flush(); - } } //--------------------------------------------------------------------+ diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index bd1d9b0d..58f485a5 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -178,6 +178,9 @@ uint32_t tud_cdc_n_write_flush (uint8_t itf) { cdcd_interface_t* p_cdc = &_cdcd_itf[itf]; + // Skip if usb is not ready yet + TU_VERIFY( tud_ready(), 0 ); + // No data to send if ( !tu_fifo_count(&p_cdc->tx_ff) ) return 0; @@ -231,9 +234,10 @@ void cdcd_init(void) p_cdc->line_coding.parity = 0; p_cdc->line_coding.data_bits = 8; - // config fifo + // Config RX fifo tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false); - // tx fifo is set to overwritable at initialization and will be changed to not overwritable + + // Config TX fifo as overwritable at initialization and will be changed to non-overwritable // if terminal supports DTR bit. Without DTR we do not know if data is actually polled by terminal. // In this way, the most current data is prioritized. tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true); @@ -251,9 +255,12 @@ void cdcd_reset(uint8_t rhport) for(uint8_t i=0; irx_ff); + tu_fifo_clear(&p_cdc->tx_ff); + tu_fifo_set_overwritable(&p_cdc->tx_ff, true); } } @@ -381,7 +388,7 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t p_cdc->line_state = (uint8_t) request->wValue; // Disable fifo overwriting if DTR bit is set - tu_fifo_set_mode(&p_cdc->tx_ff, !dtr); + tu_fifo_set_overwritable(&p_cdc->tx_ff, !dtr); TU_LOG2(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts); diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c index 444c60f9..608bfd08 100644 --- a/src/common/tusb_fifo.c +++ b/src/common/tusb_fifo.c @@ -607,7 +607,7 @@ bool tu_fifo_clear(tu_fifo_t *f) Overwritable mode the fifo is set to */ /******************************************************************************/ -bool tu_fifo_set_mode(tu_fifo_t *f, bool overwritable) +bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable) { tu_fifo_lock(f); diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h index abb301b5..b965386a 100644 --- a/src/common/tusb_fifo.h +++ b/src/common/tusb_fifo.h @@ -89,7 +89,7 @@ typedef struct .non_used_index_space = 0xFFFF - 2*_depth-1, \ } -bool tu_fifo_set_mode(tu_fifo_t *f, bool overwritable); +bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable); bool tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable); From 7860469661711bc9f73b2ae84529511693245d7b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:06:59 +0700 Subject: [PATCH 12/14] comment out and add note for tud_cdc_connected() in cdc_msc examples --- examples/device/cdc_dual_ports/src/main.c | 2 ++ examples/device/cdc_msc/src/main.c | 6 ++++-- examples/device/cdc_msc_freertos/src/main.c | 9 ++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index d6e38df8..198c4252 100644 --- a/examples/device/cdc_dual_ports/src/main.c +++ b/examples/device/cdc_dual_ports/src/main.c @@ -83,6 +83,8 @@ static void cdc_task(void) for (itf = 0; itf < CFG_TUD_CDC; itf++) { + // connected() check for DTR bit + // Most but not all terminal client set this when making connection if ( tud_cdc_n_connected(itf) ) { if ( tud_cdc_n_available(itf) ) diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index cf2bfd7c..6b13ba5a 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -105,7 +105,9 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ void cdc_task(void) { - if ( tud_cdc_connected() ) + // connected() check for DTR bit + // Most but not all terminal client set this when making connection + // if ( tud_cdc_connected() ) { // connected and there are data available if ( tud_cdc_available() ) @@ -133,7 +135,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) (void) itf; // connected - if ( dtr && rts ) + if ( dtr ) { // print initial message when connected tud_cdc_write_str("\r\nTinyUSB CDC MSC device example\r\n"); diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 1371b84c..3d2e6dc4 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -168,9 +168,11 @@ void cdc_task(void* params) // RTOS forever loop while ( 1 ) { - if ( tud_cdc_connected() ) + // connected() check for DTR bit + // Most but not all terminal client set this when making connection + // if ( tud_cdc_connected() ) { - // connected and there are data available + // There are data available if ( tud_cdc_available() ) { uint8_t buf[64]; @@ -200,10 +202,11 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) (void) itf; // connected - if ( dtr && rts ) + if ( dtr ) { // print initial message when connected tud_cdc_write_str("\r\nTinyUSB CDC MSC device with FreeRTOS example\r\n"); + tud_cdc_write_flush(); } } From 409a5fb7fc772b9f5f0c590c1688f5c5d001d167 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:18:18 +0700 Subject: [PATCH 13/14] fix ci build --- examples/device/cdc_msc_freertos/src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 3d2e6dc4..e90c3f0d 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -200,6 +200,7 @@ void cdc_task(void* params) void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { (void) itf; + (void) rts; // connected if ( dtr ) From 494e125432ca3eb6faa5de2896cfb8e2e7143ddb Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:47:06 +0700 Subject: [PATCH 14/14] more ci --- examples/device/cdc_msc/src/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index 6b13ba5a..a20a80fc 100644 --- a/examples/device/cdc_msc/src/main.c +++ b/examples/device/cdc_msc/src/main.c @@ -133,6 +133,7 @@ void cdc_task(void) void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) { (void) itf; + (void) rts; // connected if ( dtr )