8000 shared/tinyusb: Buffer startup stdout/banner to send on cdc connection. · micropython/micropython@9b8d07d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b8d07d

Browse files
committed
shared/tinyusb: Buffer startup stdout/banner to send on cdc connection.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent e7b4332 commit 9b8d07d

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

lib/tinyusb

Submodule tinyusb updated 1068 files

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
6161
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
6262
ret |= MP_STREAM_POLL_RD;
6363
}
64-
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
65-
// When connected operate as blocking, only allow if space is available.
64+
if ((poll_flags & MP_STREAM_POLL_WR) &&
65+
(!tud_cdc_connected() || (tud_cdc_connected() && tud_cdc_write_available() > 0))) {
66+
// Always allow write when not connected, fifo will retain latest.
67+
// When connected operate as blocking, only allow if space is available
6668
ret |= MP_STREAM_POLL_WR;
6769
}
6870
return ret;
@@ -96,40 +98,55 @@ void tud_cdc_rx_cb(uint8_t itf) {
9698

9799
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len) {
98100
size_t i = 0;
99-
if (tud_cdc_connected()) {
100-
while (i < len) {
101-
uint32_t n = len - i;
102-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
103-
n = CFG_TUD_CDC_EP_BUFSIZE;
104-
}
105-
int timeout = 0;
106-
// Wait with a max of USC_CDC_TIMEOUT ms
107-
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
101+
while (i < len) {
102+
uint32_t n = len - i;
103+
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
104+
n = CFG_TUD_CDC_EP_BUFSIZE;
105+
}
106+
int timeout = 0;
107+
if (tud_cdc_connected()) {
108+
// If cdc port is connected but the buffer is full,
109+
// wait for up to USC_CDC_TIMEOUT ms
110+
while (n > tud_cdc_write_available()
111+
&& timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
108112
mp_event_wait_ms(1);
109113

110114
// Explicitly run the USB stack as the scheduler may be locked (eg we
111115
// are in an interrupt handler), while there is data pending.
112116
mp_usbd_task();
113117
}
114-
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
118+
// Limit write to available space in tx buffer when connected.
119+
n = MIN(n, tud_cdc_write_available());
120+
if (n == 0) {
115121
break;
116122
}
117-
uint32_t n2 = tud_cdc_write(str + i, n);
118-
tud_cdc_write_flush();
119-
i += n2;
120123
}
124+
// When not connected we always write to usb fifo, ensuring it has latest data.
125+
uint32_t n2 = tud_cdc_write(str + i, n);
126+
tud_cdc_write_flush();
127+
i += n2;
121128
}
122129
return i;
123130
}
124131

125-
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH && MICROPY_HW_ENABLE_USBDEV
132+
static int8_t cdc_connected_flush_delay = 0;
133+
134+
void tud_sof_cb(uint32_t frame_count) {
135+
if (--cdc_connected_flush_delay < 0) {
136+
// Finished on-connection delat, disable SOF interrupt again.
137+
tud_sof_cb_enable(false);
138+
tud_cdc_write_flush();
139+
}
140+
}
126141

142+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
127143
static mp_sched_node_t mp_bootloader_sched_node;
128144

129145
static void usbd_cdc_run_bootloader_task(mp_sched_node_t *node) {
130146
mp_hal_delay_ms(250);
131147
machine_bootloader(0, NULL);
132148
}
149+
#endif
133150

134151
void
135152
#if MICROPY_HW_USB_EXTERNAL_TINYUSB
@@ -138,6 +155,15 @@ mp_usbd_line_state_cb
138155
tud_cdc_line_state_cb
139156
#endif
140157
(uint8_t itf, bool dtr, bool rts) {
158+
159+
if (dtr) {
160+
// A host application has started to open the cdc serial port.
161+
// Wait a few ms for host to be ready then send tx buffer.
162+
// High speed connection SOF fires at 125us, full speed at 1ms.
163+
cdc_connected_flush_delay = (tud_speed_get() == TUSB_SPEED_HIGH) ? 128 : 16;
164+
tud_sof_cb_enable(true);
165+
}
166+
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH
141167
if (dtr == false && rts == false) {
142168
// Device is disconnected.
143169
cdc_line_coding_t line_coding;
@@ -147,7 +173,7 @@ tud_cdc_line_state_cb
147173
mp_sched_schedule_node(&mp_bootloader_sched_node, usbd_cdc_run_bootloader_task);
148174
}
149175
}
176+
#endif
150177
}
151178

152179
#endif
153-
#endif

shared/tinyusb/tusb_config.h

Lines changed: 1 addition & 0 deletions
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)
0