From 526af512b471225dd223352a6fc750283289ea71 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 15 May 2024 14:40:23 +1000 Subject: [PATCH] shared/tinyusb: Buffer startup stdout/banner to send on cdc connection. Signed-off-by: Andrew Leech --- lib/tinyusb | 2 +- shared/tinyusb/mp_usbd_cdc.c | 65 +++++++++++++++++++++++------------- shared/tinyusb/tusb_config.h | 1 + 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 1fdf29075d4e6..99c2d26938cc4 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 1fdf29075d4e613eacfa881166015263797db0f6 +Subproject commit 99c2d26938cc4fab49d3b32ea6b39cbf95a62d9f diff --git a/shared/tinyusb/mp_usbd_cdc.c b/shared/tinyusb/mp_usbd_cdc.c index 55751d9622754..30cb9acaa04f9 100644 --- a/shared/tinyusb/mp_usbd_cdc.c +++ b/shared/tinyusb/mp_usbd_cdc.c @@ -29,8 +29,10 @@ #include "py/mpconfig.h" #include "extmod/modmachine.h" -#if MICROPY_HW_USB_CDC +#if MICROPY_HW_USB_CDC && MICROPY_HW_ENABLE_USBDEV #include "tusb.h" +#include "device/usbd.h" + #include "shared/tinyusb/mp_usbd.h" static uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll @@ -83,40 +85,50 @@ void tud_cdc_rx_cb(uint8_t itf) { mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len) { size_t i = 0; - if (tud_cdc_connected()) { - while (i < len) { - uint32_t n = len - i; - if (n > CFG_TUD_CDC_EP_BUFSIZE) { - n = CFG_TUD_CDC_EP_BUFSIZE; - } - int timeout = 0; - // Wait with a max of USC_CDC_TIMEOUT ms - while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) { - mp_event_wait_ms(1); + while (i < len) { + uint32_t n = len - i; + if (n > CFG_TUD_CDC_EP_BUFSIZE) { + n = CFG_TUD_CDC_EP_BUFSIZE; + } + int timeout = 0; + // Wait with a max of USC_CDC_TIMEOUT ms + while (n > tud_cdc_write_available() + && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT + && tud_cdc_connected()) { + mp_event_wait_ms(1); - // Explicitly run the USB stack as the scheduler may be locked (eg we - // are in an interrupt handler), while there is data pending. - mp_usbd_task(); - } - if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) { - break; - } - uint32_t n2 = tud_cdc_write(str + i, n); - tud_cdc_write_flush(); - i += n2; + // Explicitly run the USB stack as the scheduler may be locked (eg we + // are in an interrupt handler), while there is data pending. + mp_usbd_task(); + } + n = MIN(n, tud_cdc_write_available()); + if (n == 0) { + break; } + uint32_t n2 = tud_cdc_write(str + i, n); + tud_cdc_write_flush(); + i += n2; } return i; } -#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV +static int8_t cdc_connected_flush_delay = 0; +void tud_sof_cb(uint32_t frame_count) { + if (--cdc_connected_flush_delay < 0) { + tud_cdc_write_flush(); + tud_sof_cb_enable(false); + } +} + +#if MICROPY_HW_USB_CDC_1200BPS_TOUCH static mp_sched_node_t mp_bootloader_sched_node; static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) { mp_hal_delay_ms(250); machine_bootloader(0, NULL); } +#endif // MICROPY_HW_USB_CDC_1200BPS_TOUCH void #if MICROPY_HW_USB_EXTERNAL_TINYUSB @@ -125,6 +137,13 @@ mp_usbd_line_state_cb tud_cdc_line_state_cb #endif (uint8_t itf, bool dtr, bool rts) { + if (dtr) { + // The host has started to open the cdc serial port. + // Wait a few ms for host to be ready then send tx buffer. + cdc_connected_flush_delay = (tud_speed_get() == TUSB_SPEED_HIGH) ? 64 : 8; + tud_sof_cb_enable(true); + } + #if MICROPY_HW_USB_CDC_1200BPS_TOUCH if (dtr == false && rts == false) { // Device is disconnected. cdc_line_coding_t line_coding; @@ -134,7 +153,7 @@ tud_cdc_line_state_cb mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task); } } + #endif } #endif -#endif diff --git a/shared/tinyusb/tusb_config.h b/shared/tinyusb/tusb_config.h index ab47321afdc04..1f8f5e5f6dc56 100644 --- a/shared/tinyusb/tusb_config.h +++ b/shared/tinyusb/tusb_config.h @@ -79,6 +79,7 @@ #if CFG_TUD_CDC #define CFG_TUD_CDC_RX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 256) #define CFG_TUD_CDC_TX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 256) +#define CFG_TUD_CDC_PERSISTENT_TX_BUFF (1) #endif // MSC Configuration