tty: Remove buffer params from tty_init()
Let's have more orthogonal and cleaner API, where buffers are configured by tty_set_rx_buf/tty_set_tx_buf, and only them. It means that newly initialized tty starts in unbuffered mode, which is somewhat a sidestep from a main usecase behind tty, which is buffered operation, but again, having a cleaner API (and good docs, explaining users how it should be and what they should do) prevails. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
a7df3a1e09
commit
0925411356
3 changed files with 23 additions and 22 deletions
|
@ -37,21 +37,19 @@ struct tty_serial {
|
||||||
* "tty" device provides support for buffered, interrupt-driven,
|
* "tty" device provides support for buffered, interrupt-driven,
|
||||||
* timeout-controlled access to an underlying UART device. For
|
* timeout-controlled access to an underlying UART device. For
|
||||||
* completeness, it also support non-interrupt-driven, busy-polling
|
* 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 tty tty device structure to initialize
|
||||||
* @param uart_dev underlying UART device to use (should support
|
* @param uart_dev underlying UART device to use (should support
|
||||||
* interrupt-driven operation)
|
* 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,
|
int tty_init(struct tty_serial *tty, struct device *uart_dev);
|
||||||
u8_t *rxbuf, u16_t rxbuf_sz,
|
|
||||||
u8_t *txbuf, u16_t txbuf_sz);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set receive timeout for tty device.
|
* @brief Set receive timeout for tty device.
|
||||||
|
|
|
@ -51,7 +51,7 @@ void console_init(void)
|
||||||
struct device *uart_dev;
|
struct device *uart_dev;
|
||||||
|
|
||||||
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
||||||
tty_init(&console_serial, uart_dev,
|
tty_init(&console_serial, uart_dev);
|
||||||
console_rxbuf, sizeof(console_rxbuf),
|
tty_set_tx_buf(&console_serial, console_txbuf, sizeof(console_txbuf));
|
||||||
console_txbuf, sizeof(console_txbuf));
|
tty_set_rx_buf(&console_serial, console_rxbuf, sizeof(console_rxbuf));
|
||||||
}
|
}
|
||||||
|
|
|
@ -238,24 +238,24 @@ ssize_t tty_read(struct tty_serial *tty, void *buf, size_t size)
|
||||||
return out_size;
|
return out_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tty_init(struct tty_serial *tty, struct device *uart_dev,
|
int tty_init(struct tty_serial *tty, struct device *uart_dev)
|
||||||
u8_t *rxbuf, u16_t rxbuf_sz,
|
|
||||||
u8_t *txbuf, u16_t txbuf_sz)
|
|
||||||
{
|
{
|
||||||
tty->uart_dev = uart_dev;
|
tty->uart_dev = uart_dev;
|
||||||
tty->rx_ringbuf = rxbuf;
|
|
||||||
tty->rx_ringbuf_sz = rxbuf_sz;
|
/* We start in unbuffer mode. */
|
||||||
tty->tx_ringbuf = txbuf;
|
tty->rx_ringbuf = NULL;
|
||||||
tty->tx_ringbuf_sz = txbuf_sz;
|
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;
|
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->rx_timeout = K_FOREVER;
|
||||||
tty->tx_timeout = K_FOREVER;
|
tty->tx_timeout = K_FOREVER;
|
||||||
|
|
||||||
uart_irq_callback_user_data_set(uart_dev, tty_uart_isr, tty);
|
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)
|
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;
|
tty->rx_ringbuf_sz = size;
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
|
k_sem_init(&tty->rx_sem, 0, UINT_MAX);
|
||||||
uart_irq_rx_enable(tty->uart_dev);
|
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 = buf;
|
||||||
tty->tx_ringbuf_sz = size;
|
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,
|
/* New buffer is initially empty, no need to re-enable interrupts,
|
||||||
* it will be done when needed (on first output char).
|
* it will be done when needed (on first output char).
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue