2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc.
|
|
|
|
*
|
2015-10-06 18:00:37 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-10-06 18:00:37 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-10-06 18:00:37 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-04 16:09:39 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief UART-driven console
|
|
|
|
*
|
2015-10-20 18:42:33 +02:00
|
|
|
*
|
|
|
|
* Serial console driver.
|
|
|
|
* Hooks into the printk and fputc (for printf) modules. Poll driven.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2015-04-20 10:04:22 +02:00
|
|
|
#include <nanokernel.h>
|
2015-05-28 19:56:47 +02:00
|
|
|
#include <arch/cpu.h>
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2015-04-20 10:04:22 +02:00
|
|
|
#include <errno.h>
|
2015-12-08 09:35:04 +01:00
|
|
|
#include <ctype.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2015-08-12 19:17:35 +02:00
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#include <board.h>
|
2015-10-15 08:48:03 +02:00
|
|
|
#include <uart.h>
|
2015-04-23 12:12:51 +02:00
|
|
|
#include <console/uart_console.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
#include <toolchain.h>
|
|
|
|
#include <sections.h>
|
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
static struct device *uart_console_dev;
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#if 0 /* NOTUSED */
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Get a character from UART
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return the character or EOF if nothing present
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
static int console_in(void)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
|
|
|
unsigned char c;
|
2015-10-20 18:42:33 +02:00
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
if (uart_poll_in(uart_console_dev, &c) < 0)
|
2015-04-11 01:44:37 +02:00
|
|
|
return EOF;
|
|
|
|
else
|
|
|
|
return (int)c;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Output one character to UART
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
|
|
|
* Outputs both line feed and carriage return in the case of a '\n'.
|
|
|
|
*
|
2015-12-01 17:42:20 +01:00
|
|
|
* @param c Character to output
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return The character passed as input.
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
static int console_out(int c)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2015-12-01 17:42:20 +01:00
|
|
|
uart_poll_out(uart_console_dev, (unsigned char)c);
|
2015-04-11 01:44:37 +02:00
|
|
|
if ('\n' == c) {
|
2015-12-01 17:42:20 +01:00
|
|
|
uart_poll_out(uart_console_dev, (unsigned char)'\r');
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2015-12-01 17:42:20 +01:00
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_STDOUT_CONSOLE)
|
|
|
|
extern void __stdout_hook_install(int (*hook)(int));
|
|
|
|
#else
|
|
|
|
#define __stdout_hook_install(x) \
|
|
|
|
do {/* nothing */ \
|
|
|
|
} while ((0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_PRINTK)
|
|
|
|
extern void __printk_hook_install(int (*fn)(int));
|
|
|
|
#else
|
|
|
|
#define __printk_hook_install(x) \
|
|
|
|
do {/* nothing */ \
|
|
|
|
} while ((0))
|
|
|
|
#endif
|
|
|
|
|
2015-04-20 10:04:22 +02:00
|
|
|
#if defined(CONFIG_CONSOLE_HANDLER)
|
2015-05-04 13:43:36 +02:00
|
|
|
static struct nano_fifo *avail_queue;
|
|
|
|
static struct nano_fifo *lines_queue;
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
/* Control characters */
|
|
|
|
#define ESC 0x1b
|
|
|
|
#define DEL 0x7f
|
|
|
|
|
|
|
|
/* ANSI escape sequences */
|
|
|
|
#define ANSI_ESC '['
|
|
|
|
#define ANSI_UP 'A'
|
|
|
|
#define ANSI_DOWN 'B'
|
|
|
|
#define ANSI_FORWARD 'C'
|
|
|
|
#define ANSI_BACKWARD 'D'
|
|
|
|
#define ANSI_UP_STR "\x1b[A"
|
|
|
|
#define ANSI_DOWN_STR "\x1b[B"
|
|
|
|
#define ANSI_FORWARD_STR "\x1b[C"
|
|
|
|
#define ANSI_BACKWARD_STR "\x1b[D"
|
|
|
|
|
2015-08-05 21:13:36 +02:00
|
|
|
static int read_uart(struct device *uart, uint8_t *buf, unsigned int size)
|
2015-04-20 10:04:22 +02:00
|
|
|
{
|
|
|
|
int rx;
|
|
|
|
|
|
|
|
rx = uart_fifo_read(uart, buf, size);
|
|
|
|
if (rx < 0) {
|
|
|
|
/* Overrun issue. Stop the UART */
|
|
|
|
uart_irq_rx_disable(uart);
|
|
|
|
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rx;
|
|
|
|
}
|
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
static void write_uart(struct device *uart, const char *str)
|
|
|
|
{
|
|
|
|
while (*str) {
|
|
|
|
uart_poll_out(uart, *str);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 09:20:32 +02:00
|
|
|
void uart_console_isr(void *unused)
|
2015-04-20 10:04:22 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
2015-12-07 14:03:20 +01:00
|
|
|
while (uart_irq_update(uart_console_dev) &&
|
|
|
|
uart_irq_is_pending(uart_console_dev)) {
|
|
|
|
static struct uart_console_input *cmd;
|
2015-12-08 09:35:04 +01:00
|
|
|
static bool esc, ansi_esc;
|
2015-12-07 14:05:45 +01:00
|
|
|
static size_t pos;
|
2015-12-07 14:03:20 +01:00
|
|
|
uint8_t byte;
|
|
|
|
int rx;
|
|
|
|
|
|
|
|
if (!uart_irq_rx_ready(uart_console_dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-04 13:43:36 +02:00
|
|
|
|
2015-12-07 14:03:20 +01:00
|
|
|
/* Character(s) have been received */
|
2015-09-17 19:22:00 +02:00
|
|
|
|
2015-12-07 14:03:20 +01:00
|
|
|
rx = read_uart(uart_console_dev, &byte, 1);
|
|
|
|
if (rx < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-04 13:43:36 +02:00
|
|
|
|
2015-12-07 14:03:20 +01:00
|
|
|
if (uart_irq_input_hook(uart_console_dev, byte) != 0) {
|
|
|
|
/*
|
|
|
|
* The input hook indicates that no further processing
|
|
|
|
* should be done by this handler.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-07 14:03:20 +01:00
|
|
|
if (!cmd) {
|
|
|
|
cmd = nano_isr_fifo_get(avail_queue);
|
|
|
|
if (!cmd)
|
|
|
|
return;
|
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
/* Handle ANSI escape mode */
|
|
|
|
if (ansi_esc) {
|
|
|
|
ansi_esc = false;
|
|
|
|
/* Ignore all ANSI escape sequences for now */
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
/* Handle escape mode */
|
|
|
|
if (esc) {
|
|
|
|
esc = false;
|
|
|
|
switch (byte) {
|
|
|
|
case ANSI_ESC:
|
|
|
|
ansi_esc = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-08 08:26:31 +01:00
|
|
|
continue;
|
2015-04-20 10:04:22 +02:00
|
|
|
}
|
2015-12-08 08:26:31 +01:00
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
/* Handle special control characters */
|
|
|
|
if (!isprint(byte)) {
|
|
|
|
switch (byte) {
|
|
|
|
case DEL:
|
|
|
|
if (pos > 0) {
|
|
|
|
write_uart(uart_console_dev, "\b \b");
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ESC:
|
|
|
|
esc = true;
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
cmd->line[pos] = '\0';
|
|
|
|
uart_poll_out(uart_console_dev, '\n');
|
|
|
|
pos = 0;
|
|
|
|
nano_isr_fifo_put(lines_queue, cmd);
|
|
|
|
cmd = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-12-08 08:26:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-08 09:35:04 +01:00
|
|
|
/* Echo back to console */
|
|
|
|
uart_poll_out(uart_console_dev, byte);
|
|
|
|
|
|
|
|
/* Ignore characters if there's no more buffer space */
|
|
|
|
if (pos < sizeof(cmd->line) - 1) {
|
|
|
|
cmd->line[pos++] = byte;
|
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 17:42:35 +02:00
|
|
|
IRQ_CONNECT_STATIC(console, CONFIG_UART_CONSOLE_IRQ,
|
2015-12-01 17:42:20 +01:00
|
|
|
CONFIG_UART_CONSOLE_IRQ_PRI, uart_console_isr, 0,
|
2015-11-03 00:06:08 +01:00
|
|
|
UART_IRQ_FLAGS);
|
2015-06-04 17:42:35 +02:00
|
|
|
|
2015-05-04 13:43:36 +02:00
|
|
|
static void console_input_init(void)
|
2015-04-20 10:04:22 +02:00
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
uart_irq_rx_disable(uart_console_dev);
|
|
|
|
uart_irq_tx_disable(uart_console_dev);
|
2015-11-30 18:21:13 +01:00
|
|
|
IRQ_CONFIG(console, uart_irq_get(uart_console_dev));
|
2015-12-01 17:42:20 +01:00
|
|
|
irq_enable(uart_irq_get(uart_console_dev));
|
2015-04-20 10:04:22 +02:00
|
|
|
|
|
|
|
/* Drain the fifo */
|
2015-12-01 17:42:20 +01:00
|
|
|
while (uart_irq_rx_ready(uart_console_dev)) {
|
|
|
|
uart_fifo_read(uart_console_dev, &c, 1);
|
2015-08-05 21:13:36 +02:00
|
|
|
}
|
2015-04-20 10:04:22 +02:00
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
uart_irq_rx_enable(uart_console_dev);
|
2015-04-20 10:04:22 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 13:43:36 +02:00
|
|
|
void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines)
|
2015-04-20 10:04:22 +02:00
|
|
|
{
|
2015-05-04 13:43:36 +02:00
|
|
|
avail_queue = avail;
|
|
|
|
lines_queue = lines;
|
|
|
|
|
|
|
|
console_input_init();
|
2015-04-20 10:04:22 +02:00
|
|
|
}
|
|
|
|
#else
|
2015-05-04 13:43:36 +02:00
|
|
|
#define console_input_init(x) \
|
2015-04-20 10:04:22 +02:00
|
|
|
do {/* nothing */ \
|
|
|
|
} while ((0))
|
2015-05-04 13:43:36 +02:00
|
|
|
#define uart_register_input(x) \
|
2015-04-20 10:04:22 +02:00
|
|
|
do {/* nothing */ \
|
|
|
|
} while ((0))
|
|
|
|
#endif
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-08-12 19:17:35 +02:00
|
|
|
* @brief Install printk/stdout hook for UART console output
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-08-12 19:17:35 +02:00
|
|
|
void uart_console_hook_install(void)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2015-12-01 17:42:20 +01:00
|
|
|
__stdout_hook_install(console_out);
|
|
|
|
__printk_hook_install(console_out);
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
2015-08-12 19:17:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Initialize one UART as the console/debug port
|
|
|
|
*
|
|
|
|
* @return DEV_OK if successful, otherwise failed.
|
|
|
|
*/
|
|
|
|
static int uart_console_init(struct device *arg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
2015-12-01 17:42:20 +01:00
|
|
|
uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
|
|
|
|
2015-08-12 19:17:35 +02:00
|
|
|
uart_console_hook_install();
|
|
|
|
|
|
|
|
return DEV_OK;
|
|
|
|
}
|
|
|
|
DECLARE_DEVICE_INIT_CONFIG(uart_console, "", uart_console_init, NULL);
|
2015-10-26 20:56:02 +01:00
|
|
|
|
|
|
|
/* UART consloe initializes after the UART device itself */
|
|
|
|
#if defined(CONFIG_EARLY_CONSOLE)
|
|
|
|
SYS_DEFINE_DEVICE(uart_console, NULL, PRIMARY, CONFIG_UART_CONSOLE_PRIORITY);
|
|
|
|
#else
|
|
|
|
SYS_DEFINE_DEVICE(uart_console, NULL, SECONDARY, CONFIG_UART_CONSOLE_PRIORITY);
|
|
|
|
#endif
|