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 <graham.sanderson@gmail.com>
This commit is contained in:
Mr. Green's Workshop 2022-08-10 23:16:44 +09:00 committed by GitHub
parent a33a11ea16
commit 80cde7276d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -97,6 +97,11 @@
#define PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS 100 #define PICO_STDIO_USB_RESET_RESET_TO_FLASH_DELAY_MS 100
#endif #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 #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View File

@ -92,7 +92,7 @@ static void stdio_usb_out_chars(const char *buf, int length) {
if (owner == get_core_num()) return; // would deadlock otherwise if (owner == get_core_num()) return; // would deadlock otherwise
mutex_enter_blocking(&stdio_usb_mutex); mutex_enter_blocking(&stdio_usb_mutex);
} }
if (tud_cdc_connected()) { if (stdio_usb_connected()) {
for (int i = 0; i < length;) { for (int i = 0; i < length;) {
int n = length - i; int n = length - i;
int avail = (int) tud_cdc_write_available(); int avail = (int) tud_cdc_write_available();
@ -106,7 +106,7 @@ static void stdio_usb_out_chars(const char *buf, int length) {
} else { } else {
tud_task(); tud_task();
tud_cdc_write_flush(); 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)) { (!tud_cdc_write_available() && time_us_64() > last_avail_time + PICO_STDIO_USB_STDOUT_TIMEOUT_US)) {
break; break;
} }
@ -126,7 +126,7 @@ int stdio_usb_in_chars(char *buf, int length) {
mutex_enter_blocking(&stdio_usb_mutex); mutex_enter_blocking(&stdio_usb_mutex);
} }
int rc = PICO_ERROR_NO_DATA; 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); int count = (int) tud_cdc_read(buf, (uint32_t) length);
rc = count ? count : PICO_ERROR_NO_DATA; rc = count ? count : PICO_ERROR_NO_DATA;
} else { } else {
@ -210,8 +210,14 @@ bool stdio_usb_init(void) {
} }
bool stdio_usb_connected(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(); return tud_cdc_connected();
#endif
} }
#else #else
#warning stdio USB was configured along with user use of TinyUSB device mode, but CDC is not enabled #warning stdio USB was configured along with user use of TinyUSB device mode, but CDC is not enabled
bool stdio_usb_init(void) { bool stdio_usb_init(void) {