2018-09-26 20:09:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Linaro Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/drivers/uart.h>
|
|
|
|
#include <zephyr/sys/printk.h>
|
|
|
|
#include <zephyr/console/tty.h>
|
2018-09-26 20:09:28 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static int tty_irq_input_hook(struct tty_serial *tty, uint8_t c);
|
|
|
|
static int tty_putchar(struct tty_serial *tty, uint8_t c);
|
2018-09-26 20:09:28 +02:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void tty_uart_isr(const struct device *dev, void *user_data)
|
2018-09-26 20:09:28 +02:00
|
|
|
{
|
|
|
|
struct tty_serial *tty = user_data;
|
|
|
|
|
|
|
|
uart_irq_update(dev);
|
|
|
|
|
|
|
|
if (uart_irq_rx_ready(dev)) {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t c;
|
2018-09-26 20:09:28 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (uart_fifo_read(dev, &c, 1) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tty_irq_input_hook(tty, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uart_irq_tx_ready(dev)) {
|
|
|
|
if (tty->tx_get == tty->tx_put) {
|
|
|
|
/* Output buffer empty, don't bother
|
|
|
|
* us with tx interrupts
|
|
|
|
*/
|
|
|
|
uart_irq_tx_disable(dev);
|
|
|
|
} else {
|
|
|
|
uart_fifo_fill(dev, &tty->tx_ringbuf[tty->tx_get++], 1);
|
|
|
|
if (tty->tx_get >= tty->tx_ringbuf_sz) {
|
2019-03-27 02:57:45 +01:00
|
|
|
tty->tx_get = 0U;
|
2018-09-26 20:09:28 +02:00
|
|
|
}
|
2018-10-23 19:02:25 +02:00
|
|
|
k_sem_give(&tty->tx_sem);
|
2018-09-26 20:09:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static int tty_irq_input_hook(struct tty_serial *tty, uint8_t c)
|
2018-09-26 20:09:28 +02:00
|
|
|
{
|
|
|
|
int rx_next = tty->rx_put + 1;
|
|
|
|
|
|
|
|
if (rx_next >= tty->rx_ringbuf_sz) {
|
|
|
|
rx_next = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rx_next == tty->rx_get) {
|
|
|
|
/* Try to give a clue to user that some input was lost */
|
2018-11-12 16:17:42 +01:00
|
|
|
tty_putchar(tty, '~');
|
2018-09-26 20:09:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty->rx_ringbuf[tty->rx_put] = c;
|
|
|
|
tty->rx_put = rx_next;
|
|
|
|
k_sem_give(&tty->rx_sem);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
static int tty_putchar(struct tty_serial *tty, uint8_t c)
|
2018-09-26 20:09:28 +02:00
|
|
|
{
|
|
|
|
unsigned int key;
|
|
|
|
int tx_next;
|
2018-10-23 19:02:25 +02:00
|
|
|
int res;
|
|
|
|
|
2020-06-16 14:48:47 +02:00
|
|
|
res = k_sem_take(&tty->tx_sem,
|
|
|
|
k_is_in_isr() ? K_NO_WAIT :
|
|
|
|
SYS_TIMEOUT_MS(tty->tx_timeout));
|
2018-10-23 19:02:25 +02:00
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-26 20:09:28 +02:00
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
tx_next = tty->tx_put + 1;
|
|
|
|
if (tx_next >= tty->tx_ringbuf_sz) {
|
|
|
|
tx_next = 0;
|
|
|
|
}
|
|
|
|
if (tx_next == tty->tx_get) {
|
|
|
|
irq_unlock(key);
|
2018-10-23 20:15:46 +02:00
|
|
|
return -ENOSPC;
|
2018-09-26 20:09:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tty->tx_ringbuf[tty->tx_put] = c;
|
|
|
|
tty->tx_put = tx_next;
|
|
|
|
|
|
|
|
irq_unlock(key);
|
|
|
|
uart_irq_tx_enable(tty->uart_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:15:46 +02:00
|
|
|
ssize_t tty_write(struct tty_serial *tty, const void *buf, size_t size)
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
const uint8_t *p = buf;
|
2018-10-23 20:15:46 +02:00
|
|
|
size_t out_size = 0;
|
|
|
|
int res = 0;
|
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
if (tty->tx_ringbuf_sz == 0U) {
|
2018-12-05 09:03:12 +01:00
|
|
|
/* Unbuffered operation, implicitly blocking. */
|
|
|
|
out_size = size;
|
|
|
|
|
|
|
|
while (size--) {
|
|
|
|
uart_poll_out(tty->uart_dev, *p++);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_size;
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:15:46 +02:00
|
|
|
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)
|
2018-09-26 20:09:28 +02:00
|
|
|
{
|
|
|
|
unsigned int key;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t c;
|
2018-10-22 20:01:22 +02:00
|
|
|
int res;
|
2018-09-26 20:09:28 +02:00
|
|
|
|
2020-05-04 12:58:46 +02:00
|
|
|
res = k_sem_take(&tty->rx_sem, SYS_TIMEOUT_MS(tty->rx_timeout));
|
2018-10-22 20:01:22 +02:00
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-26 20:09:28 +02:00
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
c = tty->rx_ringbuf[tty->rx_get++];
|
|
|
|
if (tty->rx_get >= tty->rx_ringbuf_sz) {
|
2019-03-27 02:57:45 +01:00
|
|
|
tty->rx_get = 0U;
|
2018-09-26 20:09:28 +02:00
|
|
|
}
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-12-05 09:03:12 +01:00
|
|
|
static ssize_t tty_read_unbuf(struct tty_serial *tty, void *buf, size_t size)
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t *p = buf;
|
2018-12-05 09:03:12 +01:00
|
|
|
size_t out_size = 0;
|
|
|
|
int res = 0;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t timeout = tty->rx_timeout;
|
2018-12-05 09:03:12 +01:00
|
|
|
|
|
|
|
while (size) {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t c;
|
2018-12-05 09:03:12 +01:00
|
|
|
res = uart_poll_in(tty->uart_dev, &c);
|
|
|
|
if (res <= -2) {
|
2019-06-18 20:45:40 +02:00
|
|
|
/* Error occurred, best we can do is to return
|
2018-12-05 09:03:12 +01:00
|
|
|
* accumulated data w/o error, or return error
|
|
|
|
* directly if none.
|
|
|
|
*/
|
|
|
|
if (out_size == 0) {
|
|
|
|
errno = res;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == 0) {
|
|
|
|
*p++ = c;
|
|
|
|
out_size++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:54:28 +01:00
|
|
|
if (size == 0 ||
|
2020-05-04 12:58:46 +02:00
|
|
|
((timeout != SYS_FOREVER_MS) && timeout-- == 0U)) {
|
2018-12-05 09:03:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid 100% busy-polling, and yet try to process bursts
|
|
|
|
* of data without extra delays.
|
|
|
|
*/
|
|
|
|
if (res == -1) {
|
2019-10-06 21:02:31 +02:00
|
|
|
k_sleep(K_MSEC(1));
|
2018-12-05 09:03:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_size;
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:15:46 +02:00
|
|
|
ssize_t tty_read(struct tty_serial *tty, void *buf, size_t size)
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t *p = buf;
|
2018-10-23 20:15:46 +02:00
|
|
|
size_t out_size = 0;
|
|
|
|
int res = 0;
|
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
if (tty->rx_ringbuf_sz == 0U) {
|
2018-12-05 09:03:12 +01:00
|
|
|
return tty_read_unbuf(tty, buf, size);
|
|
|
|
}
|
|
|
|
|
2018-10-23 20:15:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
*p++ = (uint8_t)res;
|
2018-10-23 20:15:46 +02:00
|
|
|
out_size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_size;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int tty_init(struct tty_serial *tty, const struct device *uart_dev)
|
2018-09-26 20:09:28 +02:00
|
|
|
{
|
2019-07-15 19:02:29 +02:00
|
|
|
if (!uart_dev) {
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:09:28 +02:00
|
|
|
tty->uart_dev = uart_dev;
|
2018-12-05 10:07:38 +01:00
|
|
|
|
|
|
|
/* We start in unbuffer mode. */
|
|
|
|
tty->rx_ringbuf = NULL;
|
2019-03-27 02:57:45 +01:00
|
|
|
tty->rx_ringbuf_sz = 0U;
|
2018-12-05 10:07:38 +01:00
|
|
|
tty->tx_ringbuf = NULL;
|
2019-03-27 02:57:45 +01:00
|
|
|
tty->tx_ringbuf_sz = 0U;
|
2018-12-05 10:07:38 +01:00
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
tty->rx_get = tty->rx_put = tty->tx_get = tty->tx_put = 0U;
|
2018-09-26 20:09:28 +02:00
|
|
|
|
2020-05-04 12:58:46 +02:00
|
|
|
tty->rx_timeout = SYS_FOREVER_MS;
|
|
|
|
tty->tx_timeout = SYS_FOREVER_MS;
|
2018-10-22 20:01:22 +02:00
|
|
|
|
2018-09-26 20:09:28 +02:00
|
|
|
uart_irq_callback_user_data_set(uart_dev, tty_uart_isr, tty);
|
2018-12-05 10:07:38 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-09-26 20:09:28 +02:00
|
|
|
}
|
2018-12-05 09:03:12 +01:00
|
|
|
|
|
|
|
int tty_set_rx_buf(struct tty_serial *tty, void *buf, size_t size)
|
|
|
|
{
|
|
|
|
uart_irq_rx_disable(tty->uart_dev);
|
|
|
|
|
|
|
|
tty->rx_ringbuf = buf;
|
|
|
|
tty->rx_ringbuf_sz = size;
|
|
|
|
|
|
|
|
if (size > 0) {
|
2021-03-03 21:02:05 +01:00
|
|
|
k_sem_init(&tty->rx_sem, 0, K_SEM_MAX_LIMIT);
|
2018-12-05 09:03:12 +01:00
|
|
|
uart_irq_rx_enable(tty->uart_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tty_set_tx_buf(struct tty_serial *tty, void *buf, size_t size)
|
|
|
|
{
|
|
|
|
uart_irq_tx_disable(tty->uart_dev);
|
|
|
|
|
|
|
|
tty->tx_ringbuf = buf;
|
|
|
|
tty->tx_ringbuf_sz = size;
|
|
|
|
|
2021-03-03 21:02:05 +01:00
|
|
|
k_sem_init(&tty->tx_sem, size - 1, K_SEM_MAX_LIMIT);
|
2018-12-05 10:07:38 +01:00
|
|
|
|
2018-12-05 09:03:12 +01:00
|
|
|
/* New buffer is initially empty, no need to re-enable interrupts,
|
|
|
|
* it will be done when needed (on first output char).
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|