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> |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 32 | #include <ctype.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 33 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 34 | #include <device.h> |
| 35 | #include <init.h> |
| 36 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 37 | #include <board.h> |
Tomasz Bursztyka | 17e06fb | 2015-10-15 09:48:03 +0300 | [diff] [blame] | 38 | #include <uart.h> |
Tomasz Bursztyka | ae09a48 | 2015-04-23 13:12:51 +0300 | [diff] [blame] | 39 | #include <console/uart_console.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 40 | #include <toolchain.h> |
| 41 | #include <sections.h> |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 42 | #include <atomic.h> |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 43 | #include <misc/printk.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 44 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 45 | static struct device *uart_console_dev; |
| 46 | |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 47 | #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame^] | 48 | |
| 49 | static uart_console_in_debug_hook_t debug_hook_in; |
| 50 | void uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook) |
| 51 | { |
| 52 | debug_hook_in = hook; |
| 53 | } |
| 54 | |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 55 | static UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop) |
| 56 | { |
| 57 | ARG_UNUSED(c); |
| 58 | return !UART_CONSOLE_DEBUG_HOOK_HANDLED; |
| 59 | } |
| 60 | |
| 61 | static uart_console_out_debug_hook_t *debug_hook_out = debug_hook_out_nop; |
| 62 | void uart_console_out_debug_hook_install(uart_console_out_debug_hook_t *hook) |
| 63 | { |
| 64 | debug_hook_out = hook; |
| 65 | } |
| 66 | #define HANDLE_DEBUG_HOOK_OUT(c) \ |
| 67 | (debug_hook_out(c) == UART_CONSOLE_DEBUG_HOOK_HANDLED) |
| 68 | #else |
| 69 | #define HANDLE_DEBUG_HOOK_OUT(c) 0 |
| 70 | #endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */ |
| 71 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 72 | #if 0 /* NOTUSED */ |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 73 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 74 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 75 | * @brief Get a character from UART |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 76 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 77 | * @return the character or EOF if nothing present |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 78 | */ |
| 79 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 80 | static int console_in(void) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 81 | { |
| 82 | unsigned char c; |
Dan Kalowsky | da67b29 | 2015-10-20 09:42:33 -0700 | [diff] [blame] | 83 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 84 | if (uart_poll_in(uart_console_dev, &c) < 0) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 85 | return EOF; |
| 86 | else |
| 87 | return (int)c; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | #if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE) |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 92 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 93 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 94 | * @brief Output one character to UART |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 95 | * |
| 96 | * Outputs both line feed and carriage return in the case of a '\n'. |
| 97 | * |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 98 | * @param c Character to output |
| 99 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 100 | * @return The character passed as input. |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 101 | */ |
| 102 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 103 | static int console_out(int c) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 104 | { |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 105 | int handled_by_debug_server = HANDLE_DEBUG_HOOK_OUT(c); |
| 106 | |
| 107 | if (handled_by_debug_server) { |
| 108 | return c; |
| 109 | } |
| 110 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 111 | if ('\n' == c) { |
Andy Ross | 425145d | 2016-09-01 08:49:10 -0700 | [diff] [blame] | 112 | uart_poll_out(uart_console_dev, '\r'); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 113 | } |
Andy Ross | 425145d | 2016-09-01 08:49:10 -0700 | [diff] [blame] | 114 | uart_poll_out(uart_console_dev, c); |
| 115 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 116 | return c; |
| 117 | } |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 118 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 119 | #endif |
| 120 | |
| 121 | #if defined(CONFIG_STDOUT_CONSOLE) |
| 122 | extern void __stdout_hook_install(int (*hook)(int)); |
| 123 | #else |
| 124 | #define __stdout_hook_install(x) \ |
| 125 | do {/* nothing */ \ |
| 126 | } while ((0)) |
| 127 | #endif |
| 128 | |
| 129 | #if defined(CONFIG_PRINTK) |
| 130 | extern void __printk_hook_install(int (*fn)(int)); |
| 131 | #else |
| 132 | #define __printk_hook_install(x) \ |
| 133 | do {/* nothing */ \ |
| 134 | } while ((0)) |
| 135 | #endif |
| 136 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 137 | #if defined(CONFIG_CONSOLE_HANDLER) |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 138 | static struct nano_fifo *avail_queue; |
| 139 | static struct nano_fifo *lines_queue; |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 140 | static uint8_t (*completion_cb)(char *line, uint8_t len); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 141 | |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 142 | /* Control characters */ |
| 143 | #define ESC 0x1b |
| 144 | #define DEL 0x7f |
| 145 | |
| 146 | /* ANSI escape sequences */ |
| 147 | #define ANSI_ESC '[' |
| 148 | #define ANSI_UP 'A' |
| 149 | #define ANSI_DOWN 'B' |
| 150 | #define ANSI_FORWARD 'C' |
| 151 | #define ANSI_BACKWARD 'D' |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 152 | |
Daniel Leung | 1ad2a56 | 2015-08-05 12:13:36 -0700 | [diff] [blame] | 153 | 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] | 154 | { |
| 155 | int rx; |
| 156 | |
| 157 | rx = uart_fifo_read(uart, buf, size); |
| 158 | if (rx < 0) { |
| 159 | /* Overrun issue. Stop the UART */ |
| 160 | uart_irq_rx_disable(uart); |
| 161 | |
| 162 | return -EIO; |
| 163 | } |
| 164 | |
| 165 | return rx; |
| 166 | } |
| 167 | |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 168 | static inline void cursor_forward(unsigned int count) |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 169 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 170 | printk("\x1b[%uC", count); |
| 171 | } |
| 172 | |
| 173 | static inline void cursor_backward(unsigned int count) |
| 174 | { |
| 175 | printk("\x1b[%uD", count); |
| 176 | } |
| 177 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 178 | static inline void cursor_save(void) |
| 179 | { |
| 180 | printk("\x1b[s"); |
| 181 | } |
| 182 | |
| 183 | static inline void cursor_restore(void) |
| 184 | { |
| 185 | printk("\x1b[u"); |
| 186 | } |
| 187 | |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 188 | static void insert_char(char *pos, char c, uint8_t end) |
| 189 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 190 | char tmp; |
| 191 | |
| 192 | /* Echo back to console */ |
| 193 | uart_poll_out(uart_console_dev, c); |
| 194 | |
| 195 | if (end == 0) { |
| 196 | *pos = c; |
| 197 | return; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 198 | } |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 199 | |
| 200 | tmp = *pos; |
| 201 | *(pos++) = c; |
| 202 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 203 | cursor_save(); |
| 204 | |
| 205 | while (end-- > 0) { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 206 | uart_poll_out(uart_console_dev, tmp); |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 207 | c = *pos; |
| 208 | *(pos++) = tmp; |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 209 | tmp = c; |
| 210 | } |
| 211 | |
| 212 | /* Move cursor back to right place */ |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 213 | cursor_restore(); |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void del_char(char *pos, uint8_t end) |
| 217 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 218 | uart_poll_out(uart_console_dev, '\b'); |
| 219 | |
| 220 | if (end == 0) { |
| 221 | uart_poll_out(uart_console_dev, ' '); |
| 222 | uart_poll_out(uart_console_dev, '\b'); |
| 223 | return; |
| 224 | } |
| 225 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 226 | cursor_save(); |
| 227 | |
| 228 | while (end-- > 0) { |
| 229 | *pos = *(pos + 1); |
| 230 | uart_poll_out(uart_console_dev, *(pos++)); |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | uart_poll_out(uart_console_dev, ' '); |
| 234 | |
| 235 | /* Move cursor back to right place */ |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 236 | cursor_restore(); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 239 | enum { |
| 240 | ESC_ESC, |
| 241 | ESC_ANSI, |
| 242 | ESC_ANSI_FIRST, |
| 243 | ESC_ANSI_VAL, |
| 244 | ESC_ANSI_VAL_2 |
| 245 | }; |
| 246 | |
| 247 | static atomic_t esc_state; |
| 248 | static unsigned int ansi_val, ansi_val_2; |
| 249 | static uint8_t cur, end; |
| 250 | |
| 251 | static void handle_ansi(uint8_t byte) |
| 252 | { |
| 253 | if (atomic_test_and_clear_bit(&esc_state, ESC_ANSI_FIRST)) { |
| 254 | if (!isdigit(byte)) { |
| 255 | ansi_val = 1; |
| 256 | goto ansi_cmd; |
| 257 | } |
| 258 | |
| 259 | atomic_set_bit(&esc_state, ESC_ANSI_VAL); |
| 260 | ansi_val = byte - '0'; |
| 261 | ansi_val_2 = 0; |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (atomic_test_bit(&esc_state, ESC_ANSI_VAL)) { |
| 266 | if (isdigit(byte)) { |
| 267 | if (atomic_test_bit(&esc_state, ESC_ANSI_VAL_2)) { |
| 268 | ansi_val_2 *= 10; |
| 269 | ansi_val_2 += byte - '0'; |
| 270 | } else { |
| 271 | ansi_val *= 10; |
| 272 | ansi_val += byte - '0'; |
| 273 | } |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | /* Multi value sequence, e.g. Esc[Line;ColumnH */ |
| 278 | if (byte == ';' && |
| 279 | !atomic_test_and_set_bit(&esc_state, ESC_ANSI_VAL_2)) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | atomic_clear_bit(&esc_state, ESC_ANSI_VAL); |
| 284 | atomic_clear_bit(&esc_state, ESC_ANSI_VAL_2); |
| 285 | } |
| 286 | |
| 287 | ansi_cmd: |
| 288 | switch (byte) { |
| 289 | case ANSI_BACKWARD: |
| 290 | if (ansi_val > cur) { |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | end += ansi_val; |
| 295 | cur -= ansi_val; |
| 296 | cursor_backward(ansi_val); |
| 297 | break; |
| 298 | case ANSI_FORWARD: |
| 299 | if (ansi_val > end) { |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | end -= ansi_val; |
| 304 | cur += ansi_val; |
| 305 | cursor_forward(ansi_val); |
| 306 | break; |
| 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | atomic_clear_bit(&esc_state, ESC_ANSI); |
| 312 | } |
| 313 | |
Daniel Leung | e643ced | 2016-03-03 10:14:50 -0800 | [diff] [blame] | 314 | void uart_console_isr(struct device *unused) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 315 | { |
| 316 | ARG_UNUSED(unused); |
| 317 | |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 318 | while (uart_irq_update(uart_console_dev) && |
| 319 | uart_irq_is_pending(uart_console_dev)) { |
| 320 | static struct uart_console_input *cmd; |
| 321 | uint8_t byte; |
| 322 | int rx; |
| 323 | |
| 324 | if (!uart_irq_rx_ready(uart_console_dev)) { |
| 325 | continue; |
| 326 | } |
| 327 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 328 | /* Character(s) have been received */ |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 329 | |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 330 | rx = read_uart(uart_console_dev, &byte, 1); |
| 331 | if (rx < 0) { |
| 332 | return; |
| 333 | } |
| 334 | |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame^] | 335 | #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS |
| 336 | if (debug_hook_in != NULL && debug_hook_in(byte) != 0) { |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 337 | /* |
| 338 | * The input hook indicates that no further processing |
| 339 | * should be done by this handler. |
| 340 | */ |
| 341 | return; |
| 342 | } |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame^] | 343 | #endif |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 344 | |
| 345 | if (!cmd) { |
Peter Mitsis | cd6db37 | 2015-12-11 11:46:40 -0500 | [diff] [blame] | 346 | cmd = nano_isr_fifo_get(avail_queue, TICKS_NONE); |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 347 | if (!cmd) |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 348 | return; |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 349 | } |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 350 | |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 351 | /* Handle ANSI escape mode */ |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 352 | if (atomic_test_bit(&esc_state, ESC_ANSI)) { |
| 353 | handle_ansi(byte); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 354 | continue; |
| 355 | } |
| 356 | |
| 357 | /* Handle escape mode */ |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 358 | if (atomic_test_and_clear_bit(&esc_state, ESC_ESC)) { |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 359 | switch (byte) { |
| 360 | case ANSI_ESC: |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 361 | atomic_set_bit(&esc_state, ESC_ANSI); |
| 362 | atomic_set_bit(&esc_state, ESC_ANSI_FIRST); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 363 | break; |
| 364 | default: |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | /* Handle special control characters */ |
| 372 | if (!isprint(byte)) { |
| 373 | switch (byte) { |
| 374 | case DEL: |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 375 | if (cur > 0) { |
| 376 | del_char(&cmd->line[--cur], end); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 377 | } |
| 378 | break; |
| 379 | case ESC: |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 380 | atomic_set_bit(&esc_state, ESC_ESC); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 381 | break; |
| 382 | case '\r': |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 383 | cmd->line[cur + end] = '\0'; |
Johan Hedberg | 6543318 | 2016-02-05 13:45:48 +0200 | [diff] [blame] | 384 | uart_poll_out(uart_console_dev, '\r'); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 385 | uart_poll_out(uart_console_dev, '\n'); |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 386 | cur = 0; |
| 387 | end = 0; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 388 | nano_isr_fifo_put(lines_queue, cmd); |
| 389 | cmd = NULL; |
| 390 | break; |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 391 | case '\t': |
| 392 | if (completion_cb && !end) { |
| 393 | cur += completion_cb(cmd->line, cur); |
| 394 | } |
| 395 | break; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | continue; |
| 401 | } |
| 402 | |
Johan Hedberg | 6147fc6 | 2015-12-08 09:26:31 +0200 | [diff] [blame] | 403 | /* Ignore characters if there's no more buffer space */ |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 404 | if (cur + end < sizeof(cmd->line) - 1) { |
| 405 | insert_char(&cmd->line[cur++], byte, end); |
Johan Hedberg | 6147fc6 | 2015-12-08 09:26:31 +0200 | [diff] [blame] | 406 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 410 | static void console_input_init(void) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 411 | { |
| 412 | uint8_t c; |
| 413 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 414 | uart_irq_rx_disable(uart_console_dev); |
| 415 | uart_irq_tx_disable(uart_console_dev); |
Daniel Leung | e643ced | 2016-03-03 10:14:50 -0800 | [diff] [blame] | 416 | |
| 417 | uart_irq_callback_set(uart_console_dev, uart_console_isr); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 418 | |
| 419 | /* Drain the fifo */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 420 | while (uart_irq_rx_ready(uart_console_dev)) { |
| 421 | uart_fifo_read(uart_console_dev, &c, 1); |
Daniel Leung | 1ad2a56 | 2015-08-05 12:13:36 -0700 | [diff] [blame] | 422 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 423 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 424 | uart_irq_rx_enable(uart_console_dev); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 425 | } |
| 426 | |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 427 | void uart_register_input(struct nano_fifo *avail, struct nano_fifo *lines, |
| 428 | uint8_t (*completion)(char *str, uint8_t len)) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 429 | { |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 430 | avail_queue = avail; |
| 431 | lines_queue = lines; |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 432 | completion_cb = completion; |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 433 | |
| 434 | console_input_init(); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 435 | } |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 436 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 437 | #else |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 438 | #define console_input_init(x) \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 439 | do {/* nothing */ \ |
| 440 | } while ((0)) |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 441 | #define uart_register_input(x) \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 442 | do {/* nothing */ \ |
| 443 | } while ((0)) |
| 444 | #endif |
| 445 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 446 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 447 | * |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 448 | * @brief Install printk/stdout hook for UART console output |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 449 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 450 | * @return N/A |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 451 | */ |
| 452 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 453 | void uart_console_hook_install(void) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 454 | { |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 455 | __stdout_hook_install(console_out); |
| 456 | __printk_hook_install(console_out); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 457 | } |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * |
| 461 | * @brief Initialize one UART as the console/debug port |
| 462 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 463 | * @return 0 if successful, otherwise failed. |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 464 | */ |
| 465 | static int uart_console_init(struct device *arg) |
| 466 | { |
| 467 | ARG_UNUSED(arg); |
| 468 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 469 | uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); |
| 470 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 471 | uart_console_hook_install(); |
| 472 | |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 473 | return 0; |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 474 | } |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 475 | |
Chuck Jordan | 12e29fe | 2016-05-06 12:43:56 -0700 | [diff] [blame] | 476 | /* UART console initializes after the UART device itself */ |
Benjamin Walsh | a4ec963 | 2016-01-28 15:16:31 -0500 | [diff] [blame] | 477 | SYS_INIT(uart_console_init, |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 478 | #if defined(CONFIG_EARLY_CONSOLE) |
Benjamin Walsh | d340d4c | 2016-01-28 14:48:47 -0500 | [diff] [blame] | 479 | PRIMARY, |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 480 | #else |
Benjamin Walsh | d340d4c | 2016-01-28 14:48:47 -0500 | [diff] [blame] | 481 | SECONDARY, |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 482 | #endif |
Daniel Leung | 546b8ad | 2016-03-28 14:05:33 -0700 | [diff] [blame] | 483 | CONFIG_UART_CONSOLE_INIT_PRIORITY); |