From 80cde7276d238feb6396381a1845166d30041402 Mon Sep 17 00:00:00 2001 From: "Mr. Green's Workshop" <76992231+MrGreensWorkshop@users.noreply.github.com> Date: Wed, 10 Aug 2022 23:16:44 +0900 Subject: [PATCH] Enable/disable connection check made with DTR (#932) * Enable/disable connection check made with DTR this gives users the option to disable DTR check. Co-authored-by: Graham Sanderson --- .../pico_stdio_usb/include/pico/stdio_usb.h | 5 +++++ src/rp2_common/pico_stdio_usb/stdio_usb.c | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h b/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h index 8ac8244..565b6c3 100644 --- a/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h +++ b/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h @@ -97,6 +97,11 @@ #define PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS 100 #endif +// PICO_CONFIG: PICO_STDIO_USB_CONNECTION_WITHOUT_DTR, Disable use of DTR for connection checking meaning connection is assumed to be valid, type=bool, default=0, group=pico_stdio_usb +#ifndef PICO_STDIO_USB_CONNECTION_WITHOUT_DTR +#define PICO_STDIO_USB_CONNECTION_WITHOUT_DTR 0 +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/src/rp2_common/pico_stdio_usb/stdio_usb.c b/src/rp2_common/pico_stdio_usb/stdio_usb.c index 576626c..d7a649c 100644 --- a/src/rp2_common/pico_stdio_usb/stdio_usb.c +++ b/src/rp2_common/pico_stdio_usb/stdio_usb.c @@ -92,7 +92,7 @@ static void stdio_usb_out_chars(const char *buf, int length) { if (owner == get_core_num()) return; // would deadlock otherwise mutex_enter_blocking(&stdio_usb_mutex); } - if (tud_cdc_connected()) { + if (stdio_usb_connected()) { for (int i = 0; i < length;) { int n = length - i; int avail = (int) tud_cdc_write_available(); @@ -106,7 +106,7 @@ static void stdio_usb_out_chars(const char *buf, int length) { } else { tud_task(); tud_cdc_write_flush(); - if (!tud_cdc_connected() || + if (!stdio_usb_connected() || (!tud_cdc_write_available() && time_us_64() > last_avail_time + PICO_STDIO_USB_STDOUT_TIMEOUT_US)) { break; } @@ -126,7 +126,7 @@ int stdio_usb_in_chars(char *buf, int length) { mutex_enter_blocking(&stdio_usb_mutex); } int rc = PICO_ERROR_NO_DATA; - if (tud_cdc_connected() && tud_cdc_available()) { + if (stdio_usb_connected() && tud_cdc_available()) { int count = (int) tud_cdc_read(buf, (uint32_t) length); rc = count ? count : PICO_ERROR_NO_DATA; } else { @@ -210,8 +210,14 @@ bool stdio_usb_init(void) { } bool stdio_usb_connected(void) { +#if PICO_STDIO_USB_CONNECTION_WITHOUT_DTR + return tud_ready(); +#else + // this actually checks DTR return tud_cdc_connected(); +#endif } + #else #warning stdio USB was configured along with user use of TinyUSB device mode, but CDC is not enabled bool stdio_usb_init(void) {