shell: uart: Add waiting on DTR signal before sending data

Problem:
In some cases, as described in: https://github.com/zephyrproject-rtos/zephyr/issues/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 <kamil.kasperczyk@nordicsemi.no>
This commit is contained in:
Kamil Kasperczyk 2021-09-01 12:46:48 +02:00 committed by Anas Nashif
parent f51cf9ab86
commit e9f238889b
2 changed files with 28 additions and 0 deletions

View file

@ -71,6 +71,13 @@ config SHELL_BACKEND_SERIAL_RX_POLL_PERIOD
help
Determines how often UART is polled for RX byte.
config SHELL_BACKEND_SERIAL_CHECK_DTR
bool "Check DTR signal before TX"
default y if USB_UART_CONSOLE
depends on UART_LINE_CTRL
help
Check DTR signal before TX.
module = SHELL_BACKEND_SERIAL
default-timeout = 100
source "subsys/shell/Kconfig.template.shell_log_queue_timeout"

View file

@ -101,6 +101,25 @@ static void uart_rx_handle(const struct device *dev,
}
}
static void uart_dtr_wait(const struct device *dev)
{
if (IS_ENABLED(CONFIG_SHELL_BACKEND_SERIAL_CHECK_DTR)) {
int dtr, err;
while (true) {
err = uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
if (err == -ENOSYS || err == -ENOTSUP) {
break;
}
if (dtr) {
break;
}
/* Give CPU resources to low priority threads. */
k_sleep(K_MSEC(100));
}
}
}
static void uart_tx_handle(const struct device *dev,
const struct shell_uart *sh_uart)
{
@ -111,6 +130,8 @@ static void uart_tx_handle(const struct device *dev,
len = ring_buf_get_claim(sh_uart->tx_ringbuf, (uint8_t **)&data,
sh_uart->tx_ringbuf->size);
if (len) {
/* Wait for DTR signal before sending anything to output. */
uart_dtr_wait(dev);
len = uart_fifo_fill(dev, data, len);
err = ring_buf_get_finish(sh_uart->tx_ringbuf, len);
__ASSERT_NO_MSG(err == 0);