subsys: console: Add pull-style console API support.
This change introduces console_getchar() and console_getline() API
calls which can be used to get pending console input (either one
char or whole line), or block waiting for one. In this regard, they
are similar to well-known ANSI C function getchar/gets/fgets, and
are intended to ease porting of existing applications to Zephyr, and
indeed, these functions (shaped as an external module) are already
used by few applications.
The implementation of the functions is structured as a new "console"
subsystem. The intention is that further generic console code may be
pulled there instead of being in drivers/console/. Besides the
functions themselves, initialization code and sample applications
are included.
At this time, there're may limitations of how these functions can
be used. For example, console_getchar() and console_getline() are
mutually exclusive, and both are incompatible with callback
(push-style) console API (and e.g. with console shell subsystem
which uses this API). Again, the intention is to make a first step
towards refactoring console subsystem to allow more flexible
real-world usage, better reusability and composability.
Change-Id: I3f4015bb5b26e0656f82f428b11ba30e980d25a0
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-03-24 18:50:16 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zephyr.h>
|
2018-09-27 02:09:28 +08:00
|
|
|
#include <device.h>
|
2019-06-26 00:08:16 +08:00
|
|
|
#include <console/console.h>
|
2019-06-26 00:08:50 +08:00
|
|
|
#include <console/tty.h>
|
subsys: console: Add pull-style console API support.
This change introduces console_getchar() and console_getline() API
calls which can be used to get pending console input (either one
char or whole line), or block waiting for one. In this regard, they
are similar to well-known ANSI C function getchar/gets/fgets, and
are intended to ease porting of existing applications to Zephyr, and
indeed, these functions (shaped as an external module) are already
used by few applications.
The implementation of the functions is structured as a new "console"
subsystem. The intention is that further generic console code may be
pulled there instead of being in drivers/console/. Besides the
functions themselves, initialization code and sample applications
are included.
At this time, there're may limitations of how these functions can
be used. For example, console_getchar() and console_getline() are
mutually exclusive, and both are incompatible with callback
(push-style) console API (and e.g. with console shell subsystem
which uses this API). Again, the intention is to make a first step
towards refactoring console subsystem to allow more flexible
real-world usage, better reusability and composability.
Change-Id: I3f4015bb5b26e0656f82f428b11ba30e980d25a0
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-03-24 18:50:16 +08:00
|
|
|
|
2018-09-27 02:09:28 +08:00
|
|
|
static struct tty_serial console_serial;
|
subsys: console: Add pull-style console API support.
This change introduces console_getchar() and console_getline() API
calls which can be used to get pending console input (either one
char or whole line), or block waiting for one. In this regard, they
are similar to well-known ANSI C function getchar/gets/fgets, and
are intended to ease porting of existing applications to Zephyr, and
indeed, these functions (shaped as an external module) are already
used by few applications.
The implementation of the functions is structured as a new "console"
subsystem. The intention is that further generic console code may be
pulled there instead of being in drivers/console/. Besides the
functions themselves, initialization code and sample applications
are included.
At this time, there're may limitations of how these functions can
be used. For example, console_getchar() and console_getline() are
mutually exclusive, and both are incompatible with callback
(push-style) console API (and e.g. with console shell subsystem
which uses this API). Again, the intention is to make a first step
towards refactoring console subsystem to allow more flexible
real-world usage, better reusability and composability.
Change-Id: I3f4015bb5b26e0656f82f428b11ba30e980d25a0
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-03-24 18:50:16 +08:00
|
|
|
|
2018-07-17 02:57:07 +08:00
|
|
|
static u8_t console_rxbuf[CONFIG_CONSOLE_GETCHAR_BUFSIZE];
|
|
|
|
static u8_t console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE];
|
2017-04-10 15:47:28 +08:00
|
|
|
|
2018-10-24 02:15:46 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-07-17 02:57:07 +08:00
|
|
|
int console_putchar(char c)
|
|
|
|
{
|
2018-10-24 02:15:46 +08:00
|
|
|
return tty_write(&console_serial, &c, 1);
|
2018-07-17 02:57:07 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:15:46 +08:00
|
|
|
int console_getchar(void)
|
2018-07-17 02:57:07 +08:00
|
|
|
{
|
2018-10-24 02:15:46 +08:00
|
|
|
u8_t c;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = tty_read(&console_serial, &c, 1);
|
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
2018-07-17 02:57:07 +08:00
|
|
|
}
|
|
|
|
|
2017-04-10 15:47:28 +08:00
|
|
|
void console_init(void)
|
subsys: console: Add pull-style console API support.
This change introduces console_getchar() and console_getline() API
calls which can be used to get pending console input (either one
char or whole line), or block waiting for one. In this regard, they
are similar to well-known ANSI C function getchar/gets/fgets, and
are intended to ease porting of existing applications to Zephyr, and
indeed, these functions (shaped as an external module) are already
used by few applications.
The implementation of the functions is structured as a new "console"
subsystem. The intention is that further generic console code may be
pulled there instead of being in drivers/console/. Besides the
functions themselves, initialization code and sample applications
are included.
At this time, there're may limitations of how these functions can
be used. For example, console_getchar() and console_getline() are
mutually exclusive, and both are incompatible with callback
(push-style) console API (and e.g. with console shell subsystem
which uses this API). Again, the intention is to make a first step
towards refactoring console subsystem to allow more flexible
real-world usage, better reusability and composability.
Change-Id: I3f4015bb5b26e0656f82f428b11ba30e980d25a0
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-03-24 18:50:16 +08:00
|
|
|
{
|
2018-07-17 02:57:07 +08:00
|
|
|
struct device *uart_dev;
|
|
|
|
|
2017-04-10 15:47:28 +08:00
|
|
|
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
2018-12-05 17:07:38 +08:00
|
|
|
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));
|
subsys: console: Add pull-style console API support.
This change introduces console_getchar() and console_getline() API
calls which can be used to get pending console input (either one
char or whole line), or block waiting for one. In this regard, they
are similar to well-known ANSI C function getchar/gets/fgets, and
are intended to ease porting of existing applications to Zephyr, and
indeed, these functions (shaped as an external module) are already
used by few applications.
The implementation of the functions is structured as a new "console"
subsystem. The intention is that further generic console code may be
pulled there instead of being in drivers/console/. Besides the
functions themselves, initialization code and sample applications
are included.
At this time, there're may limitations of how these functions can
be used. For example, console_getchar() and console_getline() are
mutually exclusive, and both are incompatible with callback
(push-style) console API (and e.g. with console shell subsystem
which uses this API). Again, the intention is to make a first step
towards refactoring console subsystem to allow more flexible
real-world usage, better reusability and composability.
Change-Id: I3f4015bb5b26e0656f82f428b11ba30e980d25a0
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
2017-03-24 18:50:16 +08:00
|
|
|
}
|