Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc. |
| 3 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 7 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 9 | * |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Anas Nashif | 275ca60 | 2015-12-04 10:09:39 -0500 | [diff] [blame^] | 17 | /** |
| 18 | * @file |
| 19 | * @brief UART-driven console |
| 20 | * |
Dan Kalowsky | da67b29 | 2015-10-20 09:42:33 -0700 | [diff] [blame] | 21 | * |
| 22 | * Serial console driver. |
| 23 | * Hooks into the printk and fputc (for printf) modules. Poll driven. |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 24 | */ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 25 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 26 | #include <nanokernel.h> |
Dan Kalowsky | c02dd34 | 2015-05-28 10:56:47 -0700 | [diff] [blame] | 27 | #include <arch/cpu.h> |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 28 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdint.h> |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 31 | #include <errno.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 32 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 33 | #include <device.h> |
| 34 | #include <init.h> |
| 35 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 36 | #include <board.h> |
Tomasz Bursztyka | 17e06fb | 2015-10-15 09:48:03 +0300 | [diff] [blame] | 37 | #include <uart.h> |
Tomasz Bursztyka | ae09a48 | 2015-04-23 13:12:51 +0300 | [diff] [blame] | 38 | #include <console/uart_console.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 39 | #include <toolchain.h> |
| 40 | #include <sections.h> |
| 41 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 42 | static struct device *uart_console_dev; |
| 43 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 44 | #if 0 /* NOTUSED */ |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 45 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 46 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 47 | * @brief Get a character from UART |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 48 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 49 | * @return the character or EOF if nothing present |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 50 | */ |
| 51 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 52 | static int console_in(void) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 53 | { |
| 54 | unsigned char c; |
Dan Kalowsky | da67b29 | 2015-10-20 09:42:33 -0700 | [diff] [blame] | 55 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 56 | if (uart_poll_in(uart_console_dev, &c) < 0) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 57 | return EOF; |
| 58 | else |
| 59 | return (int)c; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | #if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE) |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 64 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 65 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 66 | * @brief Output one character to UART |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 67 | * |
| 68 | * Outputs both line feed and carriage return in the case of a '\n'. |
| 69 | * |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 70 | * @param c Character to output |
| 71 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 72 | * @return The character passed as input. |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 73 | */ |
| 74 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 75 | static int console_out(int c) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 76 | { |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 77 | uart_poll_out(uart_console_dev, (unsigned char)c); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 78 | if ('\n' == c) { |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 79 | uart_poll_out(uart_console_dev, (unsigned char)'\r'); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 80 | } |
| 81 | return c; |
| 82 | } |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 83 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | #if defined(CONFIG_STDOUT_CONSOLE) |
| 87 | extern void __stdout_hook_install(int (*hook)(int)); |
| 88 | #else |
| 89 | #define __stdout_hook_install(x) \ |
| 90 | do {/* nothing */ \ |
| 91 | } while ((0)) |
| 92 | #endif |
| 93 | |
| 94 | #if defined(CONFIG_PRINTK) |
| 95 | extern void __printk_hook_install(int (*fn)(int)); |
| 96 | #else |
| 97 | #define __printk_hook_install(x) \ |
| 98 | do {/* nothing */ \ |
| 99 | } while ((0)) |
| 100 | #endif |
| 101 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 102 | #if defined(CONFIG_CONSOLE_HANDLER) |
Dan Kalowsky | 2e93848 | 2015-10-14 16:12:09 -0700 | [diff] [blame] | 103 | static size_t pos; |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 104 | |
| 105 | static struct nano_fifo *avail_queue; |
| 106 | static struct nano_fifo *lines_queue; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 107 | |
Daniel Leung | 1ad2a56 | 2015-08-05 12:13:36 -0700 | [diff] [blame] | 108 | static int read_uart(struct device *uart, uint8_t *buf, unsigned int size) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 109 | { |
| 110 | int rx; |
| 111 | |
| 112 | rx = uart_fifo_read(uart, buf, size); |
| 113 | if (rx < 0) { |
| 114 | /* Overrun issue. Stop the UART */ |
| 115 | uart_irq_rx_disable(uart); |
| 116 | |
| 117 | return -EIO; |
| 118 | } |
| 119 | |
| 120 | return rx; |
| 121 | } |
| 122 | |
Andrei Emeltchenko | dfa5f23 | 2015-04-23 10:20:32 +0300 | [diff] [blame] | 123 | void uart_console_isr(void *unused) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 124 | { |
| 125 | ARG_UNUSED(unused); |
| 126 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 127 | while (uart_irq_update(uart_console_dev) |
| 128 | && uart_irq_is_pending(uart_console_dev)) { |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 129 | /* Character(s) have been received */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 130 | if (uart_irq_rx_ready(uart_console_dev)) { |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 131 | static struct uart_console_input *cmd; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 132 | uint8_t byte; |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 133 | int rx; |
| 134 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 135 | rx = read_uart(uart_console_dev, &byte, 1); |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 136 | if (rx < 0) { |
| 137 | return; |
| 138 | } |
| 139 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 140 | if (uart_irq_input_hook(uart_console_dev, byte) != 0) { |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 141 | /* |
| 142 | * The input hook indicates that no further processing |
| 143 | * should be done by this handler. |
| 144 | */ |
| 145 | return; |
| 146 | } |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 147 | |
| 148 | if (!cmd) { |
| 149 | cmd = nano_isr_fifo_get(avail_queue); |
| 150 | if (!cmd) |
| 151 | return; |
| 152 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 153 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 154 | /* Echo back to console */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 155 | uart_poll_out(uart_console_dev, byte); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 156 | |
| 157 | if (byte == '\r' || byte == '\n' || |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 158 | pos == sizeof(cmd->line) - 1) { |
| 159 | cmd->line[pos] = '\0'; |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 160 | uart_poll_out(uart_console_dev, '\n'); |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 161 | pos = 0; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 162 | |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 163 | nano_isr_fifo_put(lines_queue, cmd); |
| 164 | cmd = NULL; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 165 | } else { |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 166 | cmd->line[pos++] = byte; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
Dmitriy Korovkin | 8d06534 | 2015-06-04 11:42:35 -0400 | [diff] [blame] | 173 | IRQ_CONNECT_STATIC(console, CONFIG_UART_CONSOLE_IRQ, |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 174 | CONFIG_UART_CONSOLE_IRQ_PRI, uart_console_isr, 0, |
Dmitriy Korovkin | f142051 | 2015-11-02 18:06:08 -0500 | [diff] [blame] | 175 | UART_IRQ_FLAGS); |
Dmitriy Korovkin | 8d06534 | 2015-06-04 11:42:35 -0400 | [diff] [blame] | 176 | |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 177 | static void console_input_init(void) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 178 | { |
| 179 | uint8_t c; |
| 180 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 181 | uart_irq_rx_disable(uart_console_dev); |
| 182 | uart_irq_tx_disable(uart_console_dev); |
Juan Manuel Cruz | bc1a79c | 2015-11-30 11:21:13 -0600 | [diff] [blame] | 183 | IRQ_CONFIG(console, uart_irq_get(uart_console_dev)); |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 184 | irq_enable(uart_irq_get(uart_console_dev)); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 185 | |
| 186 | /* Drain the fifo */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 187 | while (uart_irq_rx_ready(uart_console_dev)) { |
| 188 | uart_fifo_read(uart_console_dev, &c, 1); |
Daniel Leung | 1ad2a56 | 2015-08-05 12:13:36 -0700 | [diff] [blame] | 189 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 190 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 191 | uart_irq_rx_enable(uart_console_dev); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 192 | } |
| 193 | |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 194 | void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 195 | { |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 196 | avail_queue = avail; |
| 197 | lines_queue = lines; |
| 198 | |
| 199 | console_input_init(); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 200 | } |
| 201 | #else |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 202 | #define console_input_init(x) \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 203 | do {/* nothing */ \ |
| 204 | } while ((0)) |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 205 | #define uart_register_input(x) \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 206 | do {/* nothing */ \ |
| 207 | } while ((0)) |
| 208 | #endif |
| 209 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 210 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 211 | * |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 212 | * @brief Install printk/stdout hook for UART console output |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 213 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 214 | * @return N/A |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 215 | */ |
| 216 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 217 | void uart_console_hook_install(void) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 218 | { |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 219 | __stdout_hook_install(console_out); |
| 220 | __printk_hook_install(console_out); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 221 | } |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * |
| 225 | * @brief Initialize one UART as the console/debug port |
| 226 | * |
| 227 | * @return DEV_OK if successful, otherwise failed. |
| 228 | */ |
| 229 | static int uart_console_init(struct device *arg) |
| 230 | { |
| 231 | ARG_UNUSED(arg); |
| 232 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 233 | uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); |
| 234 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 235 | uart_console_hook_install(); |
| 236 | |
| 237 | return DEV_OK; |
| 238 | } |
| 239 | DECLARE_DEVICE_INIT_CONFIG(uart_console, "", uart_console_init, NULL); |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 240 | |
| 241 | /* UART consloe initializes after the UART device itself */ |
| 242 | #if defined(CONFIG_EARLY_CONSOLE) |
| 243 | SYS_DEFINE_DEVICE(uart_console, NULL, PRIMARY, CONFIG_UART_CONSOLE_PRIORITY); |
| 244 | #else |
| 245 | SYS_DEFINE_DEVICE(uart_console, NULL, SECONDARY, CONFIG_UART_CONSOLE_PRIORITY); |
| 246 | #endif |