From e09ebea7b99e3385bc1b1d326923b0a36003275d Mon Sep 17 00:00:00 2001 From: Jan Dümpelmann Date: Fri, 4 Sep 2020 17:25:32 +0200 Subject: Remove tud_cdc_connected check from examples --- examples/device/cdc_dual_ports/src/main.c | 15 ++++++--------- examples/device/cdc_msc/src/main.c | 29 +++++++++++++---------------- examples/device/cdc_msc_freertos/src/main.c | 29 +++++++++++++---------------- 3 files changed, 32 insertions(+), 41 deletions(-) (limited to 'examples') diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index d6e38df85..813639179 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 a407e2ddd..bcf05eaac 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)); + uint8_t buf[64]; - for(uint32_t i=0; i Date: Wed, 18 Nov 2020 09:47:39 +0100 Subject: Reset CDC examples to original state --- examples/device/cdc_dual_ports/src/main.c | 15 +++++++++------ examples/device/cdc_msc/src/main.c | 29 ++++++++++++++++------------- examples/device/cdc_msc_freertos/src/main.c | 29 ++++++++++++++++------------- 3 files changed, 41 insertions(+), 32 deletions(-) (limited to 'examples') diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index 813639179..d6e38df85 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 bcf05eaac..a407e2ddd 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]; + // 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)); + // 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: 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(+) (limited to 'examples') diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index a407e2ddd..e4477927f 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(); + } } //--------------------------------------------------------------------+ -- cgit v1.3.1 From 4b4f88078503648b38444feee1fda322b65baa43 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 23 Nov 2020 23:40:13 +0700 Subject: 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(-) (limited to 'examples') diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index e4477927f..cf2bfd7c9 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 bd1d9b0d4..58f485a5d 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 444c60f96..608bfd081 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 abb301b5f..b965386ab 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); -- cgit v1.3.1 From 7860469661711bc9f73b2ae84529511693245d7b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:06:59 +0700 Subject: 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(-) (limited to 'examples') diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index d6e38df85..198c4252b 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 cf2bfd7c9..6b13ba5a4 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 1371b84c3..3d2e6dc49 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(); } } -- cgit v1.3.1 From 409a5fb7fc772b9f5f0c590c1688f5c5d001d167 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:18:18 +0700 Subject: fix ci build --- examples/device/cdc_msc_freertos/src/main.c | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 3d2e6dc49..e90c3f0d9 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 ) -- cgit v1.3.1 From 494e125432ca3eb6faa5de2896cfb8e2e7143ddb Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Nov 2020 00:47:06 +0700 Subject: more ci --- examples/device/cdc_msc/src/main.c | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c index 6b13ba5a4..a20a80fcd 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 ) -- cgit v1.3.1