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