console: Refactor serial console interface
Make serial console interface more robust. Enables interrupts only when registering input, all memory is managed inside application. Interface to application is changed to have two fifo queues. One queue is a free line slots and another queue is keeping entered lines. This way memory for lines is managed inside application which provides free lines queue. It is also simpler to manage entered lines by sleeping on fifo_get on app layer. Change-Id: I4776c03eddd1e7d880df3b902bd48f5f2c901cad Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
This commit is contained in:
parent
37d3fae356
commit
879541a918
|
@ -111,10 +111,10 @@ extern void __printk_hook_install(int (*fn)(int));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_CONSOLE_HANDLER)
|
#if defined(CONFIG_CONSOLE_HANDLER)
|
||||||
#define MAX_LINE_LENGTH 1024
|
static size_t pos = 0;
|
||||||
static char rcv_data[MAX_LINE_LENGTH];
|
|
||||||
static uint8_t rcv_pos = 0;
|
static struct nano_fifo *avail_queue;
|
||||||
static void (*handler) (const char *string);
|
static struct nano_fifo *lines_queue;
|
||||||
|
|
||||||
static int read_uart(int uart, uint8_t *buf, unsigned int size)
|
static int read_uart(int uart, uint8_t *buf, unsigned int size)
|
||||||
{
|
{
|
||||||
|
@ -138,33 +138,43 @@ void uart_console_isr(void *unused)
|
||||||
while (uart_irq_update(UART) && uart_irq_is_pending(UART)) {
|
while (uart_irq_update(UART) && uart_irq_is_pending(UART)) {
|
||||||
/* Character(s) have been received */
|
/* Character(s) have been received */
|
||||||
if (uart_irq_rx_ready(UART)) {
|
if (uart_irq_rx_ready(UART)) {
|
||||||
int rx;
|
static struct uart_console_input *cmd;
|
||||||
uint8_t byte;
|
uint8_t byte;
|
||||||
|
int rx;
|
||||||
|
|
||||||
|
|
||||||
|
if (!cmd) {
|
||||||
|
cmd = nano_isr_fifo_get(avail_queue);
|
||||||
|
if (!cmd)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
rx = read_uart(UART, &byte, 1);
|
rx = read_uart(UART, &byte, 1);
|
||||||
if (rx < 0)
|
if (rx < 0) {
|
||||||
|
nano_isr_fifo_put(avail_queue, cmd);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Echo back to console */
|
/* Echo back to console */
|
||||||
uart_poll_out(UART, byte);
|
uart_poll_out(UART, byte);
|
||||||
|
|
||||||
if (byte == '\r' || byte == '\n' ||
|
if (byte == '\r' || byte == '\n' ||
|
||||||
rcv_pos == sizeof(rcv_data) - 1) {
|
pos == sizeof(cmd->line) - 1) {
|
||||||
rcv_data[rcv_pos] = '\0';
|
cmd->line[pos] = '\0';
|
||||||
uart_poll_out(UART, '\n');
|
uart_poll_out(UART, '\n');
|
||||||
rcv_pos = 0;
|
pos = 0;
|
||||||
|
|
||||||
if (handler)
|
nano_isr_fifo_put(lines_queue, cmd);
|
||||||
handler(rcv_data);
|
cmd = NULL;
|
||||||
} else {
|
} else {
|
||||||
rcv_data[rcv_pos++] = byte;
|
cmd->line[pos++] = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void console_handler_init(void)
|
static void console_input_init(void)
|
||||||
{
|
{
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
|
|
||||||
|
@ -179,15 +189,18 @@ static void console_handler_init(void)
|
||||||
uart_irq_rx_enable(UART);
|
uart_irq_rx_enable(UART);
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_register_handler(void (*cb) (const char *string))
|
void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines)
|
||||||
{
|
{
|
||||||
handler = cb;
|
avail_queue = avail;
|
||||||
|
lines_queue = lines;
|
||||||
|
|
||||||
|
console_input_init();
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#define console_handler_init(x) \
|
#define console_input_init(x) \
|
||||||
do {/* nothing */ \
|
do {/* nothing */ \
|
||||||
} while ((0))
|
} while ((0))
|
||||||
#define uart_register_handler(x) \
|
#define uart_register_input(x) \
|
||||||
do {/* nothing */ \
|
do {/* nothing */ \
|
||||||
} while ((0))
|
} while ((0))
|
||||||
#endif
|
#endif
|
||||||
|
@ -203,5 +216,4 @@ void uart_console_init(void)
|
||||||
{
|
{
|
||||||
__stdout_hook_install(consoleOut);
|
__stdout_hook_install(consoleOut);
|
||||||
__printk_hook_install(consoleOut);
|
__printk_hook_install(consoleOut);
|
||||||
console_handler_init();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,15 +38,30 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <cputype.h>
|
#include <cputype.h>
|
||||||
|
#include <nanokernel.h>
|
||||||
|
|
||||||
extern void uart_console_init(void);
|
extern void uart_console_init(void);
|
||||||
|
|
||||||
/*
|
#define MAX_LINE_LEN 1024
|
||||||
* Register callback function which gets called when string typed in
|
struct uart_console_input {
|
||||||
* the serial console. Carriage return is translated to NULL making
|
int __unused;
|
||||||
* string always NULL terminated.
|
char line[MAX_LINE_LEN];
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @brief Register uart input processing
|
||||||
|
*
|
||||||
|
* Input processing is started when string is typed in the console.
|
||||||
|
* Carriage return is translated to NULL making string always NULL
|
||||||
|
* terminated. Application before calling register function need to
|
||||||
|
* initialize two fifo queues mentioned below.
|
||||||
|
*
|
||||||
|
* @param avail nano_fifo queue keeping available input slots
|
||||||
|
* @param lines nano_fifo queue of entered lines which to be processed
|
||||||
|
* in the application code.
|
||||||
|
*
|
||||||
|
* @return None
|
||||||
*/
|
*/
|
||||||
void uart_register_handler(void (*cb) (const char *string));
|
void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines);
|
||||||
|
|
||||||
void uart_console_isr(void *unused);
|
void uart_console_isr(void *unused);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue