Skip to content

Commit e3688b4

Browse files
authored
Merge pull request #7041 from dhalbert/serial-first-write-delay
delay first serial write to allow host setup time
2 parents ab22d5a + 844cd17 commit e3688b4

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

supervisor/shared/serial.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929

3030
#include "py/mpconfig.h"
31+
#include "py/mphal.h"
3132

3233
#include "supervisor/shared/cpu.h"
3334
#include "supervisor/shared/display.h"
@@ -62,6 +63,13 @@ byte console_uart_rx_buf[64];
6263
#endif
6364
#endif
6465

66+
#if CIRCUITPY_USB || CIRCUITPY_CONSOLE_UART
67+
// Flag to note whether this is the first write after connection.
68+
// Delay slightly on the first write to allow time for the host to set up things,
69+
// including turning off echo mode.
70+
static bool _first_write_done = false;
71+
#endif
72+
6573
#if CIRCUITPY_USB_VENDOR
6674
bool tud_vendor_connected(void);
6775
#endif
@@ -144,6 +152,10 @@ void serial_early_init(void) {
144152
}
145153

146154
void serial_init(void) {
155+
#if CIRCUITPY_USB || CIRCUITPY_CONSOLE_UART
156+
_first_write_done = false;
157+
#endif
158+
147159
port_serial_init();
148160
}
149161

@@ -301,8 +313,11 @@ void serial_write_substring(const char *text, uint32_t length) {
301313
#endif
302314

303315
#if CIRCUITPY_CONSOLE_UART
316+
if (!_first_write_done) {
317+
mp_hal_delay_ms(50);
318+
_first_write_done = true;
319+
}
304320
int uart_errcode;
305-
306321
common_hal_busio_uart_write(&console_uart, (const uint8_t *)text, length, &uart_errcode);
307322
#endif
308323

@@ -321,6 +336,11 @@ void serial_write_substring(const char *text, uint32_t length) {
321336
#endif
322337

323338
#if CIRCUITPY_USB
339+
// Delay the very first write
340+
if (tud_cdc_connected() && !_first_write_done) {
341+
mp_hal_delay_ms(50);
342+
_first_write_done = true;
343+
}
324344
uint32_t count = 0;
325345
if (tud_cdc_connected()) {
326346
while (count < length) {

0 commit comments

Comments
 (0)