-
Notifications
You must be signed in to change notification settings - Fork 8.2k
subsys: console/tty: Refactor to introduce POSIX-like read/write API #10765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,36 @@ static struct tty_serial console_serial; | |
| static u8_t console_rxbuf[CONFIG_CONSOLE_GETCHAR_BUFSIZE]; | ||
| static u8_t console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE]; | ||
|
|
||
| ssize_t console_write(void *dummy, const void *buf, size_t size) | ||
| { | ||
| ARG_UNUSED(dummy); | ||
|
|
||
| return tty_write(&console_serial, buf, size); | ||
| } | ||
|
|
||
| ssize_t console_read(void *dummy, void *buf, size_t size) | ||
| { | ||
| ARG_UNUSED(dummy); | ||
|
|
||
| return tty_read(&console_serial, buf, size); | ||
| } | ||
|
|
||
| int console_putchar(char c) | ||
| { | ||
| return tty_putchar(&console_serial, (u8_t)c); | ||
| return tty_write(&console_serial, &c, 1); | ||
| } | ||
|
|
||
| u8_t console_getchar(void) | ||
| int console_getchar(void) | ||
|
||
| { | ||
| return tty_getchar(&console_serial); | ||
| u8_t c; | ||
| int res; | ||
|
|
||
| res = tty_read(&console_serial, &c, 1); | ||
| if (res < 0) { | ||
| return res; | ||
| } | ||
|
|
||
| return c; | ||
| } | ||
|
|
||
| void console_init(void) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ static void tty_uart_isr(void *user_data) | |
| if (tty->tx_get >= tty->tx_ringbuf_sz) { | ||
| tty->tx_get = 0; | ||
| } | ||
| k_sem_give(&tty->tx_sem); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -68,10 +69,16 @@ static int tty_irq_input_hook(struct tty_serial *tty, u8_t c) | |
| return 1; | ||
| } | ||
|
|
||
| int tty_putchar(struct tty_serial *tty, u8_t c) | ||
| static int tty_putchar(struct tty_serial *tty, u8_t c) | ||
| { | ||
| unsigned int key; | ||
| int tx_next; | ||
| int res; | ||
|
|
||
| res = k_sem_take(&tty->tx_sem, tty->tx_timeout); | ||
|
||
| if (res < 0) { | ||
| return res; | ||
| } | ||
|
|
||
| key = irq_lock(); | ||
| tx_next = tty->tx_put + 1; | ||
|
|
@@ -80,7 +87,7 @@ int tty_putchar(struct tty_serial *tty, u8_t c) | |
| } | ||
| if (tx_next == tty->tx_get) { | ||
|
||
| irq_unlock(key); | ||
| return -1; | ||
| return -ENOSPC; | ||
| } | ||
|
|
||
| tty->tx_ringbuf[tty->tx_put] = c; | ||
|
|
@@ -91,12 +98,46 @@ int tty_putchar(struct tty_serial *tty, u8_t c) | |
| return 0; | ||
| } | ||
|
|
||
| u8_t tty_getchar(struct tty_serial *tty) | ||
| ssize_t tty_write(struct tty_serial *tty, const void *buf, size_t size) | ||
| { | ||
| const u8_t *p = buf; | ||
| size_t out_size = 0; | ||
| int res = 0; | ||
|
|
||
| while (size--) { | ||
| res = tty_putchar(tty, *p++); | ||
| if (res < 0) { | ||
| /* If we didn't transmit anything, return the error. */ | ||
| if (out_size == 0) { | ||
| errno = -res; | ||
| return res; | ||
| } | ||
|
|
||
| /* | ||
| * Otherwise, return how much we transmitted. If error | ||
| * was transient (like EAGAIN), on next call user might | ||
| * not even get it. And if it's non-transient, they'll | ||
| * get it on the next call. | ||
| */ | ||
| return out_size; | ||
| } | ||
|
|
||
| out_size++; | ||
| } | ||
|
|
||
| return out_size; | ||
| } | ||
|
|
||
| static int tty_getchar(struct tty_serial *tty) | ||
| { | ||
|
||
| unsigned int key; | ||
| u8_t c; | ||
| int res; | ||
|
|
||
| k_sem_take(&tty->rx_sem, K_FOREVER); | ||
| res = k_sem_take(&tty->rx_sem, tty->rx_timeout); | ||
| if (res < 0) { | ||
| return res; | ||
| } | ||
|
|
||
| key = irq_lock(); | ||
| c = tty->rx_ringbuf[tty->rx_get++]; | ||
|
|
@@ -108,6 +149,37 @@ u8_t tty_getchar(struct tty_serial *tty) | |
| return c; | ||
| } | ||
|
|
||
| ssize_t tty_read(struct tty_serial *tty, void *buf, size_t size) | ||
| { | ||
| u8_t *p = buf; | ||
| size_t out_size = 0; | ||
| int res = 0; | ||
|
|
||
| while (size--) { | ||
| res = tty_getchar(tty); | ||
| if (res < 0) { | ||
| /* If we didn't transmit anything, return the error. */ | ||
| if (out_size == 0) { | ||
| errno = -res; | ||
| return res; | ||
| } | ||
|
|
||
| /* | ||
| * Otherwise, return how much we transmitted. If error | ||
| * was transient (like EAGAIN), on next call user might | ||
| * not even get it. And if it's non-transient, they'll | ||
| * get it on the next call. | ||
| */ | ||
| return out_size; | ||
| } | ||
|
|
||
| *p++ = (u8_t)res; | ||
| out_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) | ||
|
|
@@ -119,6 +191,10 @@ void tty_init(struct tty_serial *tty, struct device *uart_dev, | |
| tty->tx_ringbuf_sz = txbuf_sz; | ||
| 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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to update the description of a function return values in a header file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done