blob: 91c5325a7d28c0a7b0820410bd6ac4d6e294c5cb [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001/* uart_console.c - UART-driven console */
2
3/*
4 * Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc.
5 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -05006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07009 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050010 * http://www.apache.org/licenses/LICENSE-2.0
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070011 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070017 */
18
19/*
Dan Kalowskyda67b292015-10-20 09:42:33 -070020 * DESCRIPTION
21 *
22 * Serial console driver.
23 * Hooks into the printk and fputc (for printf) modules. Poll driven.
Anas Nashifea0d0b22015-07-01 17:22:39 -040024 */
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070025
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030026#include <nanokernel.h>
Dan Kalowskyc02dd342015-05-28 10:56:47 -070027#include <arch/cpu.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030028
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070029#include <stdio.h>
30#include <stdint.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030031#include <errno.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070032
Daniel Leungad2d2962015-08-12 10:17:35 -070033#include <device.h>
34#include <init.h>
35
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070036#include <board.h>
Tomasz Bursztyka17e06fb2015-10-15 09:48:03 +030037#include <uart.h>
Tomasz Bursztykaae09a482015-04-23 13:12:51 +030038#include <console/uart_console.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070039#include <toolchain.h>
40#include <sections.h>
41
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070042#if 0 /* NOTUSED */
Anas Nashifea0d0b22015-07-01 17:22:39 -040043/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070044 *
Anas Nashiff367f072015-07-01 17:51:40 -040045 * @brief Get a character from UART
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070046 *
Anas Nashif1362e3c2015-07-01 17:29:04 -040047 * @return the character or EOF if nothing present
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070048 */
49
50static int consoleIn(void)
51{
Daniel Leung1ad2a562015-08-05 12:13:36 -070052#ifdef UART_CONSOLE_DEV
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070053 unsigned char c;
Dan Kalowskyda67b292015-10-20 09:42:33 -070054
Daniel Leung1ad2a562015-08-05 12:13:36 -070055 if (uart_poll_in(UART_CONSOLE_DEV, &c) < 0)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070056 return EOF;
57 else
58 return (int)c;
Daniel Leung1ad2a562015-08-05 12:13:36 -070059#else
60 return 0;
61#endif
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070062}
63#endif
64
65#if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
Anas Nashifea0d0b22015-07-01 17:22:39 -040066/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070067 *
Anas Nashiff367f072015-07-01 17:51:40 -040068 * @brief Output one character to UART
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070069 *
70 * Outputs both line feed and carriage return in the case of a '\n'.
71 *
Anas Nashif1362e3c2015-07-01 17:29:04 -040072 * @return The character passed as input.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070073 */
74
75static int consoleOut(int c /* character to output */
76 )
77{
Daniel Leung1ad2a562015-08-05 12:13:36 -070078#ifdef UART_CONSOLE_DEV
79 uart_poll_out(UART_CONSOLE_DEV, (unsigned char)c);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070080 if ('\n' == c) {
Daniel Leung1ad2a562015-08-05 12:13:36 -070081 uart_poll_out(UART_CONSOLE_DEV, (unsigned char)'\r');
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070082 }
83 return c;
Daniel Leung1ad2a562015-08-05 12:13:36 -070084#else
85 return 0;
86#endif
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070087}
88#endif
89
90#if defined(CONFIG_STDOUT_CONSOLE)
91extern void __stdout_hook_install(int (*hook)(int));
92#else
93#define __stdout_hook_install(x) \
94 do {/* nothing */ \
95 } while ((0))
96#endif
97
98#if defined(CONFIG_PRINTK)
99extern void __printk_hook_install(int (*fn)(int));
100#else
101#define __printk_hook_install(x) \
102 do {/* nothing */ \
103 } while ((0))
104#endif
105
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300106#if defined(CONFIG_CONSOLE_HANDLER)
Dan Kalowsky2e938482015-10-14 16:12:09 -0700107static size_t pos;
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300108
109static struct nano_fifo *avail_queue;
110static struct nano_fifo *lines_queue;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300111
Daniel Leung1ad2a562015-08-05 12:13:36 -0700112static int read_uart(struct device *uart, uint8_t *buf, unsigned int size)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300113{
114 int rx;
115
116 rx = uart_fifo_read(uart, buf, size);
117 if (rx < 0) {
118 /* Overrun issue. Stop the UART */
119 uart_irq_rx_disable(uart);
120
121 return -EIO;
122 }
123
124 return rx;
125}
126
Andrei Emeltchenkodfa5f232015-04-23 10:20:32 +0300127void uart_console_isr(void *unused)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300128{
129 ARG_UNUSED(unused);
130
Daniel Leung1ad2a562015-08-05 12:13:36 -0700131 while (uart_irq_update(UART_CONSOLE_DEV)
132 && uart_irq_is_pending(UART_CONSOLE_DEV)) {
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300133 /* Character(s) have been received */
Daniel Leung1ad2a562015-08-05 12:13:36 -0700134 if (uart_irq_rx_ready(UART_CONSOLE_DEV)) {
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300135 static struct uart_console_input *cmd;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300136 uint8_t byte;
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300137 int rx;
138
Peter Mitsisb1c10202015-09-17 13:22:00 -0400139 rx = read_uart(UART_CONSOLE_DEV, &byte, 1);
140 if (rx < 0) {
141 return;
142 }
143
144 if (uart_irq_input_hook(UART_CONSOLE_DEV, byte) != 0) {
145 /*
146 * The input hook indicates that no further processing
147 * should be done by this handler.
148 */
149 return;
150 }
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300151
152 if (!cmd) {
153 cmd = nano_isr_fifo_get(avail_queue);
154 if (!cmd)
155 return;
156 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300157
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300158 /* Echo back to console */
Daniel Leung1ad2a562015-08-05 12:13:36 -0700159 uart_poll_out(UART_CONSOLE_DEV, byte);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300160
161 if (byte == '\r' || byte == '\n' ||
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300162 pos == sizeof(cmd->line) - 1) {
163 cmd->line[pos] = '\0';
Daniel Leung1ad2a562015-08-05 12:13:36 -0700164 uart_poll_out(UART_CONSOLE_DEV, '\n');
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300165 pos = 0;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300166
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300167 nano_isr_fifo_put(lines_queue, cmd);
168 cmd = NULL;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300169 } else {
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300170 cmd->line[pos++] = byte;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300171 }
172
173 }
174 }
175}
176
Dmitriy Korovkin8d065342015-06-04 11:42:35 -0400177IRQ_CONNECT_STATIC(console, CONFIG_UART_CONSOLE_IRQ,
Dmitriy Korovkinf1420512015-11-02 18:06:08 -0500178 CONFIG_UART_CONSOLE_INT_PRI, uart_console_isr, 0,
179 UART_IRQ_FLAGS);
Dmitriy Korovkin8d065342015-06-04 11:42:35 -0400180
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300181static void console_input_init(void)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300182{
183 uint8_t c;
184
Daniel Leung1ad2a562015-08-05 12:13:36 -0700185 uart_irq_rx_disable(UART_CONSOLE_DEV);
186 uart_irq_tx_disable(UART_CONSOLE_DEV);
Dirk Brandewie147d6582015-10-15 08:13:50 -0700187 IRQ_CONFIG(console, uart_irq_get(UART_CONSOLE_DEV), 0);
Daniel Leung1ad2a562015-08-05 12:13:36 -0700188 irq_enable(uart_irq_get(UART_CONSOLE_DEV));
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300189
190 /* Drain the fifo */
Daniel Leung1ad2a562015-08-05 12:13:36 -0700191 while (uart_irq_rx_ready(UART_CONSOLE_DEV)) {
192 uart_fifo_read(UART_CONSOLE_DEV, &c, 1);
193 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300194
Daniel Leung1ad2a562015-08-05 12:13:36 -0700195 uart_irq_rx_enable(UART_CONSOLE_DEV);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300196}
197
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300198void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300199{
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300200 avail_queue = avail;
201 lines_queue = lines;
202
203 console_input_init();
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300204}
205#else
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300206#define console_input_init(x) \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300207 do {/* nothing */ \
208 } while ((0))
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300209#define uart_register_input(x) \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300210 do {/* nothing */ \
211 } while ((0))
212#endif
213
Anas Nashifea0d0b22015-07-01 17:22:39 -0400214/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700215 *
Daniel Leungad2d2962015-08-12 10:17:35 -0700216 * @brief Install printk/stdout hook for UART console output
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700217 *
Anas Nashif1362e3c2015-07-01 17:29:04 -0400218 * @return N/A
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700219 */
220
Daniel Leungad2d2962015-08-12 10:17:35 -0700221void uart_console_hook_install(void)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700222{
223 __stdout_hook_install(consoleOut);
224 __printk_hook_install(consoleOut);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700225}
Daniel Leungad2d2962015-08-12 10:17:35 -0700226
227/**
228 *
229 * @brief Initialize one UART as the console/debug port
230 *
231 * @return DEV_OK if successful, otherwise failed.
232 */
233static int uart_console_init(struct device *arg)
234{
235 ARG_UNUSED(arg);
236
237 uart_console_hook_install();
238
239 return DEV_OK;
240}
241DECLARE_DEVICE_INIT_CONFIG(uart_console, "", uart_console_init, NULL);
Dirk Brandewie4365f022015-09-25 13:02:12 -0700242pre_kernel_late_init(uart_console, NULL);