diff --git a/include/tty.h b/include/tty.h index 0aebf9d790..8afe9101f2 100644 --- a/include/tty.h +++ b/include/tty.h @@ -37,21 +37,19 @@ struct tty_serial { * "tty" device provides support for buffered, interrupt-driven, * timeout-controlled access to an underlying UART device. For * completeness, it also support non-interrupt-driven, busy-polling - * access mode. + * access mode. After initialization, tty is in the "most conservative" + * unbuffered mode with infinite timeouts (this is guaranteed to work + * on any hardware). Users should configure buffers and timeouts as + * they need using functions tty_set_rx_buf(), tty_set_tx_buf(), + * tty_set_rx_timeout(), tty_set_tx_timeout(). * * @param tty tty device structure to initialize * @param uart_dev underlying UART device to use (should support * interrupt-driven operation) - * @param rxbuf pointer to receive buffer - * @param rxbuf_sz size of receive buffer - * @param txbuf pointer to transmit buffer - * @param txbuf_sz size of transmit buffer * - * @return N/A + * @return 0 on success, error code (<0) otherwise */ -void tty_init(struct tty_serial *tty, struct device *uart_dev, - u8_t *rxbuf, u16_t rxbuf_sz, - u8_t *txbuf, u16_t txbuf_sz); +int tty_init(struct tty_serial *tty, struct device *uart_dev); /** * @brief Set receive timeout for tty device. diff --git a/subsys/console/getchar.c b/subsys/console/getchar.c index 0700ba332f..dfc771bf28 100644 --- a/subsys/console/getchar.c +++ b/subsys/console/getchar.c @@ -51,7 +51,7 @@ void console_init(void) struct device *uart_dev; uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); - tty_init(&console_serial, uart_dev, - console_rxbuf, sizeof(console_rxbuf), - console_txbuf, sizeof(console_txbuf)); + tty_init(&console_serial, uart_dev); + tty_set_tx_buf(&console_serial, console_txbuf, sizeof(console_txbuf)); + tty_set_rx_buf(&console_serial, console_rxbuf, sizeof(console_rxbuf)); } diff --git a/subsys/console/tty.c b/subsys/console/tty.c index 39e9f28bd2..41830c75fe 100644 --- a/subsys/console/tty.c +++ b/subsys/console/tty.c @@ -238,24 +238,24 @@ ssize_t tty_read(struct tty_serial *tty, void *buf, size_t size) return out_size; } -void tty_init(struct tty_serial *tty, struct device *uart_dev, - u8_t *rxbuf, u16_t rxbuf_sz, - u8_t *txbuf, u16_t txbuf_sz) +int tty_init(struct tty_serial *tty, struct device *uart_dev) { tty->uart_dev = uart_dev; - tty->rx_ringbuf = rxbuf; - tty->rx_ringbuf_sz = rxbuf_sz; - tty->tx_ringbuf = txbuf; - tty->tx_ringbuf_sz = txbuf_sz; + + /* We start in unbuffer mode. */ + tty->rx_ringbuf = NULL; + tty->rx_ringbuf_sz = 0; + tty->tx_ringbuf = NULL; + tty->tx_ringbuf_sz = 0; + tty->rx_get = tty->rx_put = tty->tx_get = tty->tx_put = 0; - k_sem_init(&tty->rx_sem, 0, UINT_MAX); - k_sem_init(&tty->tx_sem, txbuf_sz - 1, UINT_MAX); tty->rx_timeout = K_FOREVER; tty->tx_timeout = K_FOREVER; uart_irq_callback_user_data_set(uart_dev, tty_uart_isr, tty); - uart_irq_rx_enable(uart_dev); + + return 0; } int tty_set_rx_buf(struct tty_serial *tty, void *buf, size_t size) @@ -266,6 +266,7 @@ int tty_set_rx_buf(struct tty_serial *tty, void *buf, size_t size) tty->rx_ringbuf_sz = size; if (size > 0) { + k_sem_init(&tty->rx_sem, 0, UINT_MAX); uart_irq_rx_enable(tty->uart_dev); } @@ -279,6 +280,8 @@ int tty_set_tx_buf(struct tty_serial *tty, void *buf, size_t size) tty->tx_ringbuf = buf; tty->tx_ringbuf_sz = size; + k_sem_init(&tty->tx_sem, size - 1, UINT_MAX); + /* New buffer is initially empty, no need to re-enable interrupts, * it will be done when needed (on first output char). */