From 51eb4572f564b3e177e8d1920bf933118b35c74f Mon Sep 17 00:00:00 2001 From: Pavel Kral Date: Mon, 15 Jul 2019 19:02:29 +0200 Subject: [PATCH] subsystem: console: tty init checks and support for polled-only devices This patch add tty runtime initialization check for console support routines. Without it callers of routines API are not aware that initialization of tty was failed. This patch basically checks availability of console device and also its support for interrupt driven transfers if routines are configured to use it. Signed-off-by: Pavel Kral --- include/console/console.h | 4 ++-- subsys/console/getchar.c | 22 ++++++++++++++++++++-- subsys/console/tty.c | 4 ++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/console/console.h b/include/console/console.h index 05136ce7298..56fe74ef7ed 100644 --- a/include/console/console.h +++ b/include/console/console.h @@ -23,9 +23,9 @@ extern "C" { * and incompatible with, callback (push-style) console handling * (via console_input_fn callback, etc.). * - * @return N/A + * @return 0 on success, error code (<0) otherwise */ -void console_init(void); +int console_init(void); /** * @brief Read data from console. diff --git a/subsys/console/getchar.c b/subsys/console/getchar.c index 7d805556d38..3c53a400f2c 100644 --- a/subsys/console/getchar.c +++ b/subsys/console/getchar.c @@ -8,6 +8,7 @@ #include #include #include +#include static struct tty_serial console_serial; @@ -46,12 +47,29 @@ int console_getchar(void) return c; } -void console_init(void) +int console_init(void) { struct device *uart_dev; + int ret; uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); - tty_init(&console_serial, uart_dev); + ret = tty_init(&console_serial, uart_dev); + + if (ret) { + return ret; + } + + /* Checks device driver supports for interrupt driven data transfers. */ + if (CONFIG_CONSOLE_GETCHAR_BUFSIZE + CONFIG_CONSOLE_PUTCHAR_BUFSIZE) { + const struct uart_driver_api *api = + (const struct uart_driver_api *)uart_dev->driver_api; + if (!api->irq_callback_set) { + return -ENOTSUP; + } + } + tty_set_tx_buf(&console_serial, console_txbuf, sizeof(console_txbuf)); tty_set_rx_buf(&console_serial, console_rxbuf, sizeof(console_rxbuf)); + + return 0; } diff --git a/subsys/console/tty.c b/subsys/console/tty.c index a73d255e801..4451bfc234e 100644 --- a/subsys/console/tty.c +++ b/subsys/console/tty.c @@ -238,6 +238,10 @@ ssize_t tty_read(struct tty_serial *tty, void *buf, size_t size) int tty_init(struct tty_serial *tty, struct device *uart_dev) { + if (!uart_dev) { + return -ENODEV; + } + tty->uart_dev = uart_dev; /* We start in unbuffer mode. */