Skip to content

Commit 8809ae7

Browse files
pi-anldpgeorge
authored andcommitted
shared/tinyusb: Buffer startup CDC data to send to host on connection.
At startup, buffer initial stdout / MicroyPthon banner so that it can be sent to the host on initial connection of the USB serial port. This buffering also works for when the CDC becomes disconnected and the device is still printing to stdout, and when CDC is reconnected the most recent part of stdout (depending on how big the internal USB FIFO is) is flushed to the host. This change is most obvious when you've first plugged in a MicroPython device (or hit reset), when it's a board that uses USB (CDC) serial in the chip itself for the REPL interface. This doesn't apply to UART going via a separate USB-serial chip. The stm32 port already has this buffering behaviour (it doesn't use TinyUSB) and this commit extends such behaviour to rp2, mimxrt, samd and renesas-ra ports, which do use TinyUSB. Signed-off-by: Andrew Leech <[email protected]>
1 parent d144f06 commit 8809ae7

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

ports/mimxrt/tusb_config.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
#define CFG_TUD_CDC (1)
3333
#define CFG_TUD_CDC_RX_BUFSIZE (512)
3434
#define CFG_TUD_CDC_TX_BUFSIZE (512)
35+
#define CFG_TUD_CDC_PERSISTENT_TX_BUFF (1)
3536

3637
#endif // MICROPY_INCLUDED_MIMXRT_TUSB_CONFIG_H

shared/tinyusb/mp_usbd_cdc.c

+40-13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#if MICROPY_HW_USB_CDC && MICROPY_HW_ENABLE_USBDEV && !MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC
3636

3737
static uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
38+
static int8_t cdc_connected_flush_delay = 0;
3839

3940
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
4041
uintptr_t ret = 0;
@@ -58,7 +59,9 @@ uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
5859
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
5960
ret |= MP_STREAM_POLL_RD;
6061
}
61-
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
62+
if ((poll_flags & MP_STREAM_POLL_WR) &&
63+
(!tud_cdc_connected() || (tud_cdc_connected() && tud_cdc_write_available() > 0))) {
64+
// Always allow write when not connected, fifo will retain latest.
6265
// When connected operate as blocking, only allow if space is available.
6366
ret |= MP_STREAM_POLL_WR;
6467
}
@@ -93,42 +96,55 @@ void tud_cdc_rx_cb(uint8_t itf) {
9396

9497
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len) {
9598
size_t i = 0;
96-
if (tud_cdc_connected()) {
97-
while (i < len) {
98-
uint32_t n = len - i;
99-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
100-
n = CFG_TUD_CDC_EP_BUFSIZE;
101-
}
99+
while (i < len) {
100+
uint32_t n = len - i;
101+
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
102+
n = CFG_TUD_CDC_EP_BUFSIZE;
103+
}
104+
if (tud_cdc_connected()) {
102105
int timeout = 0;
103-
// Wait with a max of USC_CDC_TIMEOUT ms
106+
// If CDC port is connected but the buffer is full, wait for up to USC_CDC_TIMEOUT ms.
104107
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
105108
mp_event_wait_ms(1);
106109

107110
// Explicitly run the USB stack as the scheduler may be locked (eg we
108111
// are in an interrupt handler), while there is data pending.
109112
mp_usbd_task();
110113
}
111-
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
114+
// Limit write to available space in tx buffer when connected.
115+
n = MIN(n, tud_cdc_write_available());
116+
if (n == 0) {
112117
break;
113118
}
114-
uint32_t n2 = tud_cdc_write(str + i, n);
115-
tud_cdc_write_flush();
116-
i += n2;
117119
}
120+
// When not connected we always write to usb fifo, ensuring it has latest data.
121+
uint32_t n2 = tud_cdc_write(str + i, n);
122+
tud_cdc_write_flush();
123+
i += n2;
118124
}
119125
return i;
120126
}
121127

128+
void tud_sof_cb(uint32_t frame_count) {
129+
if (--cdc_connected_flush_delay < 0) {
130+
// Finished on-connection delay, disable SOF interrupt again.
131+
tud_sof_cb_enable(false);
132+
tud_cdc_write_flush();
133+
}
134+
}
135+
122136
#endif
123137

124-
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV
138+
#if MICROPY_HW_ENABLE_USBDEV && (MICROPY_HW_USB_CDC_1200BPS_TOUCH || MICROPY_HW_USB_CDC)
125139

140+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
126141
static mp_sched_node_t mp_bootloader_sched_node;
127142

128143
static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
129144
mp_hal_delay_ms(250);
130145
machine_bootloader(0, NULL);
131146
}
147+
#endif
132148

133149
void
134150
#if MICROPY_HW_USB_EXTERNAL_TINYUSB
@@ -137,6 +153,16 @@ mp_usbd_line_state_cb
137153
tud_cdc_line_state_cb
138154
#endif
139155
(uint8_t itf, bool dtr, bool rts) {
156+
#if MICROPY_HW_USB_CDC && !MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC
157+
if (dtr) {
158+
// A host application has started to open the cdc serial port.
159+
// Wait a few ms for host to be ready then send tx buffer.
160+
// High speed connection SOF fires at 125us, full speed at 1ms.
161+
cdc_connected_flush_delay = (tud_speed_get() == TUSB_SPEED_HIGH) ? 128 : 16;
162+
tud_sof_cb_enable(true);
163+
}
164+
#endif
165+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
140166
if (dtr == false && rts == false) {
141167
// Device is disconnected.
142168
cdc_line_coding_t line_coding;
@@ -146,6 +172,7 @@ tud_cdc_line_state_cb
146172
mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
147173
}
148174
}
175+
#endif
149176
}
150177

151178
#endif

shared/tinyusb/tusb_config.h

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#if CFG_TUD_CDC
8080
#define CFG_TUD_CDC_RX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 256)
8181
#define CFG_TUD_CDC_TX_BUFSIZE ((CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED) ? 512 : 256)
82+
#define CFG_TUD_CDC_PERSISTENT_TX_BUFF (1)
8283
#endif
8384

8485
// MSC Configuration

0 commit comments

Comments
 (0)