Skip to content

Commit 34a65fa

Browse files
kkasperczyk-nombolivar-nordic
authored andcommitted
[nrf fromtree] shell: uart: Add waiting on DTR signal before sending data
Problem: In some cases, as described in: zephyrproject-rtos/zephyr#36948 shell backend sends characters to output before serial device is ready for it. It results in observing additional characters inserted on the shell input after device boot. Solution: Added waiting on DTR signal before sending anything to the output. Fixes: #36948 Signed-off-by: Kamil Kasperczyk <[email protected]> (cherry picked from commit d7c629f)
1 parent 09e2184 commit 34a65fa

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

subsys/shell/Kconfig.backends

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ config SHELL_BACKEND_SERIAL_RX_POLL_PERIOD
8181
help
8282
Determines how often UART is polled for RX byte.
8383

84+
config SHELL_BACKEND_SERIAL_CHECK_DTR
85+
bool "Check DTR signal before TX"
86+
default y if USB_UART_CONSOLE
87+
depends on UART_LINE_CTRL
88+
help
89+
Check DTR signal before TX.
90+
8491
module = SHELL_BACKEND_SERIAL
8592
default-timeout = 100
8693
source "subsys/shell/Kconfig.template.shell_log_queue_timeout"

subsys/shell/shell_uart.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ static void uart_rx_handle(const struct device *dev,
101101
}
102102
}
103103

104+
static void uart_dtr_wait(const struct device *dev)
105+
{
106+
if (IS_ENABLED(CONFIG_SHELL_BACKEND_SERIAL_CHECK_DTR)) {
107+
int dtr, err;
108+
109+
while (true) {
110+
err = uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
111+
if (err == -ENOSYS || err == -ENOTSUP) {
112+
break;
113+
}
114+
if (dtr) {
115+
break;
116+
}
117+
/* Give CPU resources to low priority threads. */
118+
k_sleep(K_MSEC(100));
119+
}
120+
}
121+
}
122+
104123
static void uart_tx_handle(const struct device *dev,
105124
const struct shell_uart *sh_uart)
106125
{
@@ -111,6 +130,8 @@ static void uart_tx_handle(const struct device *dev,
111130
len = ring_buf_get_claim(sh_uart->tx_ringbuf, (uint8_t **)&data,
112131
sh_uart->tx_ringbuf->size);
113132
if (len) {
133+
/* Wait for DTR signal before sending anything to output. */
134+
uart_dtr_wait(dev);
114135
len = uart_fifo_fill(dev, data, len);
115136
err = ring_buf_get_finish(sh_uart->tx_ringbuf, len);
116137
__ASSERT_NO_MSG(err == 0);

0 commit comments

Comments
 (0)