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> |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 17 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 19 | #include <zephyr/types.h> |
Erwan Gouriou | 2716cbc | 2019-11-27 17:31:57 +0100 | [diff] [blame] | 20 | #include <sys/__assert.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 | |
Anas Nashif | d1b2718 | 2019-06-25 15:54:01 -0400 | [diff] [blame] | 27 | #include <drivers/uart.h> |
Anas Nashif | e8a182c | 2019-06-21 13:13:19 -0400 | [diff] [blame] | 28 | #include <drivers/console/console.h> |
| 29 | #include <drivers/console/uart_console.h> |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 30 | #include <toolchain.h> |
Anas Nashif | 397d29d | 2017-06-17 11:30:47 -0400 | [diff] [blame] | 31 | #include <linker/sections.h> |
Anas Nashif | e1e05a2 | 2019-06-25 12:25:32 -0400 | [diff] [blame] | 32 | #include <sys/atomic.h> |
Anas Nashif | 9ab2a56 | 2019-06-26 10:33:49 -0400 | [diff] [blame] | 33 | #include <sys/printk.h> |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 34 | #ifdef CONFIG_UART_CONSOLE_MCUMGR |
| 35 | #include "mgmt/serial.h" |
| 36 | #endif |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 37 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 38 | static const struct device *uart_console_dev; |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 39 | |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 40 | #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame] | 41 | |
| 42 | static uart_console_in_debug_hook_t debug_hook_in; |
| 43 | void uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook) |
| 44 | { |
| 45 | debug_hook_in = hook; |
| 46 | } |
| 47 | |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 48 | static UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop) { |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 49 | ARG_UNUSED(c); |
| 50 | return !UART_CONSOLE_DEBUG_HOOK_HANDLED; |
| 51 | } |
| 52 | |
| 53 | static uart_console_out_debug_hook_t *debug_hook_out = debug_hook_out_nop; |
| 54 | void uart_console_out_debug_hook_install(uart_console_out_debug_hook_t *hook) |
| 55 | { |
| 56 | debug_hook_out = hook; |
| 57 | } |
| 58 | #define HANDLE_DEBUG_HOOK_OUT(c) \ |
| 59 | (debug_hook_out(c) == UART_CONSOLE_DEBUG_HOOK_HANDLED) |
Vincenzo Frascino | f3a9241 | 2016-11-16 12:01:48 +0000 | [diff] [blame] | 60 | |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 61 | #endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */ |
| 62 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 63 | |
| 64 | #if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE) |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 65 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 66 | * |
Anas Nashif | f367f07 | 2015-07-01 17:51:40 -0400 | [diff] [blame] | 67 | * @brief Output one character to UART |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 68 | * |
| 69 | * Outputs both line feed and carriage return in the case of a '\n'. |
| 70 | * |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 71 | * @param c Character to output |
| 72 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 73 | * @return The character passed as input. |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 74 | */ |
| 75 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 76 | static int console_out(int c) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 77 | { |
Vincenzo Frascino | f3a9241 | 2016-11-16 12:01:48 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS |
| 79 | |
Benjamin Walsh | 0ff5d37 | 2016-04-11 17:11:28 -0400 | [diff] [blame] | 80 | int handled_by_debug_server = HANDLE_DEBUG_HOOK_OUT(c); |
| 81 | |
| 82 | if (handled_by_debug_server) { |
| 83 | return c; |
| 84 | } |
| 85 | |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 86 | #endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */ |
Vincenzo Frascino | f3a9241 | 2016-11-16 12:01:48 +0000 | [diff] [blame] | 87 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 88 | if ('\n' == c) { |
Andy Ross | 425145d | 2016-09-01 08:49:10 -0700 | [diff] [blame] | 89 | uart_poll_out(uart_console_dev, '\r'); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 90 | } |
Andy Ross | 425145d | 2016-09-01 08:49:10 -0700 | [diff] [blame] | 91 | uart_poll_out(uart_console_dev, c); |
| 92 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 93 | return c; |
| 94 | } |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 95 | |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 96 | #endif |
| 97 | |
| 98 | #if defined(CONFIG_STDOUT_CONSOLE) |
| 99 | extern void __stdout_hook_install(int (*hook)(int)); |
| 100 | #else |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 101 | #define __stdout_hook_install(x) \ |
| 102 | do { /* nothing */ \ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 103 | } while ((0)) |
| 104 | #endif |
| 105 | |
| 106 | #if defined(CONFIG_PRINTK) |
| 107 | extern void __printk_hook_install(int (*fn)(int)); |
| 108 | #else |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 109 | #define __printk_hook_install(x) \ |
| 110 | do { /* nothing */ \ |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 111 | } while ((0)) |
| 112 | #endif |
| 113 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 114 | #if defined(CONFIG_CONSOLE_HANDLER) |
Luiz Augusto von Dentz | 6b2443e | 2016-11-11 14:11:44 +0200 | [diff] [blame] | 115 | static struct k_fifo *avail_queue; |
| 116 | static struct k_fifo *lines_queue; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 117 | static uint8_t (*completion_cb)(char *line, uint8_t len); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 118 | |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 119 | /* Control characters */ |
qianfan Zhao | 13e1c3c | 2018-05-29 12:43:16 +0800 | [diff] [blame] | 120 | #define BS 0x08 |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 121 | #define ESC 0x1b |
| 122 | #define DEL 0x7f |
| 123 | |
| 124 | /* ANSI escape sequences */ |
| 125 | #define ANSI_ESC '[' |
| 126 | #define ANSI_UP 'A' |
| 127 | #define ANSI_DOWN 'B' |
| 128 | #define ANSI_FORWARD 'C' |
| 129 | #define ANSI_BACKWARD 'D' |
Szymon Janc | 6b0cf54 | 2016-10-26 18:10:50 +0200 | [diff] [blame] | 130 | #define ANSI_END 'F' |
| 131 | #define ANSI_HOME 'H' |
Szymon Janc | a037316 | 2016-10-26 18:15:02 +0200 | [diff] [blame] | 132 | #define ANSI_DEL '~' |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 133 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 134 | static int read_uart(const struct device *uart, uint8_t *buf, |
| 135 | unsigned int size) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 136 | { |
| 137 | int rx; |
| 138 | |
| 139 | rx = uart_fifo_read(uart, buf, size); |
| 140 | if (rx < 0) { |
| 141 | /* Overrun issue. Stop the UART */ |
| 142 | uart_irq_rx_disable(uart); |
| 143 | |
| 144 | return -EIO; |
| 145 | } |
| 146 | |
| 147 | return rx; |
| 148 | } |
| 149 | |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 150 | static inline void cursor_forward(unsigned int count) |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 151 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 152 | printk("\x1b[%uC", count); |
| 153 | } |
| 154 | |
| 155 | static inline void cursor_backward(unsigned int count) |
| 156 | { |
| 157 | printk("\x1b[%uD", count); |
| 158 | } |
| 159 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 160 | static inline void cursor_save(void) |
| 161 | { |
| 162 | printk("\x1b[s"); |
| 163 | } |
| 164 | |
| 165 | static inline void cursor_restore(void) |
| 166 | { |
| 167 | printk("\x1b[u"); |
| 168 | } |
| 169 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 170 | static void insert_char(char *pos, char c, uint8_t end) |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 171 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 172 | char tmp; |
| 173 | |
| 174 | /* Echo back to console */ |
| 175 | uart_poll_out(uart_console_dev, c); |
| 176 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 177 | if (end == 0U) { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 178 | *pos = c; |
| 179 | return; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 180 | } |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 181 | |
| 182 | tmp = *pos; |
| 183 | *(pos++) = c; |
| 184 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 185 | cursor_save(); |
| 186 | |
| 187 | while (end-- > 0) { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 188 | uart_poll_out(uart_console_dev, tmp); |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 189 | c = *pos; |
| 190 | *(pos++) = tmp; |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 191 | tmp = c; |
| 192 | } |
| 193 | |
| 194 | /* Move cursor back to right place */ |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 195 | cursor_restore(); |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 198 | static void del_char(char *pos, uint8_t end) |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 199 | { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 200 | uart_poll_out(uart_console_dev, '\b'); |
| 201 | |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 202 | if (end == 0U) { |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 203 | uart_poll_out(uart_console_dev, ' '); |
| 204 | uart_poll_out(uart_console_dev, '\b'); |
| 205 | return; |
| 206 | } |
| 207 | |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 208 | cursor_save(); |
| 209 | |
| 210 | while (end-- > 0) { |
| 211 | *pos = *(pos + 1); |
| 212 | uart_poll_out(uart_console_dev, *(pos++)); |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | uart_poll_out(uart_console_dev, ' '); |
| 216 | |
| 217 | /* Move cursor back to right place */ |
Johan Hedberg | ceba31a | 2015-12-09 12:40:29 +0200 | [diff] [blame] | 218 | cursor_restore(); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 221 | enum { |
| 222 | ESC_ESC, |
| 223 | ESC_ANSI, |
| 224 | ESC_ANSI_FIRST, |
| 225 | ESC_ANSI_VAL, |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 226 | ESC_ANSI_VAL_2, |
| 227 | #ifdef CONFIG_UART_CONSOLE_MCUMGR |
| 228 | ESC_MCUMGR_PKT_1, |
| 229 | ESC_MCUMGR_PKT_2, |
| 230 | ESC_MCUMGR_FRAG_1, |
| 231 | ESC_MCUMGR_FRAG_2, |
| 232 | #endif |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static atomic_t esc_state; |
| 236 | static unsigned int ansi_val, ansi_val_2; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 237 | static uint8_t cur, end; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 238 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 239 | static void handle_ansi(uint8_t byte, char *line) |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 240 | { |
| 241 | if (atomic_test_and_clear_bit(&esc_state, ESC_ANSI_FIRST)) { |
| 242 | if (!isdigit(byte)) { |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 243 | ansi_val = 1U; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 244 | goto ansi_cmd; |
| 245 | } |
| 246 | |
| 247 | atomic_set_bit(&esc_state, ESC_ANSI_VAL); |
| 248 | ansi_val = byte - '0'; |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 249 | ansi_val_2 = 0U; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
| 253 | if (atomic_test_bit(&esc_state, ESC_ANSI_VAL)) { |
| 254 | if (isdigit(byte)) { |
| 255 | if (atomic_test_bit(&esc_state, ESC_ANSI_VAL_2)) { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 256 | ansi_val_2 *= 10U; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 257 | ansi_val_2 += byte - '0'; |
| 258 | } else { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 259 | ansi_val *= 10U; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 260 | ansi_val += byte - '0'; |
| 261 | } |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | /* Multi value sequence, e.g. Esc[Line;ColumnH */ |
| 266 | if (byte == ';' && |
| 267 | !atomic_test_and_set_bit(&esc_state, ESC_ANSI_VAL_2)) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | atomic_clear_bit(&esc_state, ESC_ANSI_VAL); |
| 272 | atomic_clear_bit(&esc_state, ESC_ANSI_VAL_2); |
| 273 | } |
| 274 | |
| 275 | ansi_cmd: |
| 276 | switch (byte) { |
| 277 | case ANSI_BACKWARD: |
| 278 | if (ansi_val > cur) { |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | end += ansi_val; |
| 283 | cur -= ansi_val; |
| 284 | cursor_backward(ansi_val); |
| 285 | break; |
| 286 | case ANSI_FORWARD: |
| 287 | if (ansi_val > end) { |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | end -= ansi_val; |
| 292 | cur += ansi_val; |
| 293 | cursor_forward(ansi_val); |
| 294 | break; |
Szymon Janc | 6b0cf54 | 2016-10-26 18:10:50 +0200 | [diff] [blame] | 295 | case ANSI_HOME: |
| 296 | if (!cur) { |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | cursor_backward(cur); |
| 301 | end += cur; |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 302 | cur = 0U; |
Szymon Janc | 6b0cf54 | 2016-10-26 18:10:50 +0200 | [diff] [blame] | 303 | break; |
| 304 | case ANSI_END: |
| 305 | if (!end) { |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | cursor_forward(end); |
| 310 | cur += end; |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 311 | end = 0U; |
Szymon Janc | 6b0cf54 | 2016-10-26 18:10:50 +0200 | [diff] [blame] | 312 | break; |
Szymon Janc | a037316 | 2016-10-26 18:15:02 +0200 | [diff] [blame] | 313 | case ANSI_DEL: |
| 314 | if (!end) { |
| 315 | break; |
| 316 | } |
| 317 | |
| 318 | cursor_forward(1); |
| 319 | del_char(&line[cur], --end); |
| 320 | break; |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 321 | default: |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | atomic_clear_bit(&esc_state, ESC_ANSI); |
| 326 | } |
| 327 | |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 328 | #ifdef CONFIG_UART_CONSOLE_MCUMGR |
| 329 | |
| 330 | static void clear_mcumgr(void) |
| 331 | { |
| 332 | atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_1); |
| 333 | atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_2); |
| 334 | atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_1); |
| 335 | atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_2); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * These states indicate whether an mcumgr frame is being received. |
| 340 | */ |
| 341 | #define CONSOLE_MCUMGR_STATE_NONE 1 |
| 342 | #define CONSOLE_MCUMGR_STATE_HEADER 2 |
| 343 | #define CONSOLE_MCUMGR_STATE_PAYLOAD 3 |
| 344 | |
| 345 | static int read_mcumgr_byte(uint8_t byte) |
| 346 | { |
| 347 | bool frag_1; |
| 348 | bool frag_2; |
| 349 | bool pkt_1; |
| 350 | bool pkt_2; |
| 351 | |
| 352 | pkt_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_1); |
| 353 | pkt_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_2); |
| 354 | frag_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_1); |
| 355 | frag_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_2); |
| 356 | |
| 357 | if (pkt_2 || frag_2) { |
| 358 | /* Already fully framed. */ |
| 359 | return CONSOLE_MCUMGR_STATE_PAYLOAD; |
| 360 | } |
| 361 | |
| 362 | if (pkt_1) { |
| 363 | if (byte == MCUMGR_SERIAL_HDR_PKT_2) { |
| 364 | /* Final framing byte received. */ |
| 365 | atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_2); |
| 366 | return CONSOLE_MCUMGR_STATE_PAYLOAD; |
| 367 | } |
| 368 | } else if (frag_1) { |
| 369 | if (byte == MCUMGR_SERIAL_HDR_FRAG_2) { |
| 370 | /* Final framing byte received. */ |
| 371 | atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_2); |
| 372 | return CONSOLE_MCUMGR_STATE_PAYLOAD; |
| 373 | } |
| 374 | } else { |
| 375 | if (byte == MCUMGR_SERIAL_HDR_PKT_1) { |
| 376 | /* First framing byte received. */ |
| 377 | atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_1); |
| 378 | return CONSOLE_MCUMGR_STATE_HEADER; |
| 379 | } else if (byte == MCUMGR_SERIAL_HDR_FRAG_1) { |
| 380 | /* First framing byte received. */ |
| 381 | atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_1); |
| 382 | return CONSOLE_MCUMGR_STATE_HEADER; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* Non-mcumgr byte received. */ |
| 387 | return CONSOLE_MCUMGR_STATE_NONE; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * @brief Attempts to process a received byte as part of an mcumgr frame. |
| 392 | * |
| 393 | * @param cmd The console command currently being received. |
| 394 | * @param byte The byte just received. |
| 395 | * |
| 396 | * @return true if the command being received is an mcumgr frame; false if it |
| 397 | * is a plain console command. |
| 398 | */ |
| 399 | static bool handle_mcumgr(struct console_input *cmd, uint8_t byte) |
| 400 | { |
| 401 | int mcumgr_state; |
| 402 | |
| 403 | mcumgr_state = read_mcumgr_byte(byte); |
| 404 | if (mcumgr_state == CONSOLE_MCUMGR_STATE_NONE) { |
| 405 | /* Not an mcumgr command; let the normal console handling |
| 406 | * process the byte. |
| 407 | */ |
| 408 | cmd->is_mcumgr = 0; |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | /* The received byte is part of an mcumgr command. Process the byte |
| 413 | * and return true to indicate that normal console handling should |
| 414 | * ignore it. |
| 415 | */ |
| 416 | if (cur + end < sizeof(cmd->line) - 1) { |
| 417 | cmd->line[cur++] = byte; |
| 418 | } |
| 419 | if (mcumgr_state == CONSOLE_MCUMGR_STATE_PAYLOAD && byte == '\n') { |
| 420 | cmd->line[cur + end] = '\0'; |
| 421 | cmd->is_mcumgr = 1; |
| 422 | k_fifo_put(lines_queue, cmd); |
| 423 | |
| 424 | clear_mcumgr(); |
| 425 | cmd = NULL; |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 426 | cur = 0U; |
| 427 | end = 0U; |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | #endif /* CONFIG_UART_CONSOLE_MCUMGR */ |
| 434 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 435 | static void uart_console_isr(const struct device *unused, void *user_data) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 436 | { |
| 437 | ARG_UNUSED(unused); |
Tomasz Bursztyka | 7d1af02 | 2020-06-24 19:06:44 +0200 | [diff] [blame] | 438 | ARG_UNUSED(user_data); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 439 | |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 440 | while (uart_irq_update(uart_console_dev) && |
| 441 | uart_irq_is_pending(uart_console_dev)) { |
Tomasz Bursztyka | 2f1af49 | 2017-01-24 10:08:08 +0100 | [diff] [blame] | 442 | static struct console_input *cmd; |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 443 | uint8_t byte; |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 444 | int rx; |
| 445 | |
| 446 | if (!uart_irq_rx_ready(uart_console_dev)) { |
| 447 | continue; |
| 448 | } |
| 449 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 450 | /* Character(s) have been received */ |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 451 | |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 452 | rx = read_uart(uart_console_dev, &byte, 1); |
| 453 | if (rx < 0) { |
| 454 | return; |
| 455 | } |
| 456 | |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame] | 457 | #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS |
| 458 | if (debug_hook_in != NULL && debug_hook_in(byte) != 0) { |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 459 | /* |
| 460 | * The input hook indicates that no further processing |
| 461 | * should be done by this handler. |
| 462 | */ |
| 463 | return; |
| 464 | } |
Marcus Shawcroft | 1bc999c | 2016-10-23 08:53:21 +0100 | [diff] [blame] | 465 | #endif |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 466 | |
| 467 | if (!cmd) { |
Luiz Augusto von Dentz | 6b2443e | 2016-11-11 14:11:44 +0200 | [diff] [blame] | 468 | cmd = k_fifo_get(avail_queue, K_NO_WAIT); |
Tomasz Bursztyka | fb98109 | 2017-01-24 09:06:08 +0100 | [diff] [blame] | 469 | if (!cmd) { |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 470 | return; |
Tomasz Bursztyka | fb98109 | 2017-01-24 09:06:08 +0100 | [diff] [blame] | 471 | } |
Johan Hedberg | 5ad7803 | 2015-12-07 15:03:20 +0200 | [diff] [blame] | 472 | } |
Peter Mitsis | b1c1020 | 2015-09-17 13:22:00 -0400 | [diff] [blame] | 473 | |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 474 | #ifdef CONFIG_UART_CONSOLE_MCUMGR |
| 475 | /* Divert this byte from normal console handling if it is part |
| 476 | * of an mcumgr frame. |
| 477 | */ |
| 478 | if (handle_mcumgr(cmd, byte)) { |
| 479 | continue; |
| 480 | } |
| 481 | #endif /* CONFIG_UART_CONSOLE_MCUMGR */ |
| 482 | |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 483 | /* Handle ANSI escape mode */ |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 484 | if (atomic_test_bit(&esc_state, ESC_ANSI)) { |
Szymon Janc | a037316 | 2016-10-26 18:15:02 +0200 | [diff] [blame] | 485 | handle_ansi(byte, cmd->line); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 486 | continue; |
| 487 | } |
| 488 | |
| 489 | /* Handle escape mode */ |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 490 | if (atomic_test_and_clear_bit(&esc_state, ESC_ESC)) { |
Tomasz Bursztyka | fb98109 | 2017-01-24 09:06:08 +0100 | [diff] [blame] | 491 | if (byte == ANSI_ESC) { |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 492 | atomic_set_bit(&esc_state, ESC_ANSI); |
| 493 | atomic_set_bit(&esc_state, ESC_ANSI_FIRST); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | continue; |
| 497 | } |
| 498 | |
Ruslan Mstoi | ca94b86 | 2018-02-22 18:33:01 +0200 | [diff] [blame] | 499 | /* Handle special control characters */ |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 500 | if (!isprint(byte)) { |
| 501 | switch (byte) { |
qianfan Zhao | 13e1c3c | 2018-05-29 12:43:16 +0800 | [diff] [blame] | 502 | case BS: |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 503 | case DEL: |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 504 | if (cur > 0) { |
| 505 | del_char(&cmd->line[--cur], end); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 506 | } |
| 507 | break; |
| 508 | case ESC: |
Johan Hedberg | 8683dc4 | 2015-12-09 11:36:25 +0200 | [diff] [blame] | 509 | atomic_set_bit(&esc_state, ESC_ESC); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 510 | break; |
| 511 | case '\r': |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 512 | cmd->line[cur + end] = '\0'; |
Johan Hedberg | 6543318 | 2016-02-05 13:45:48 +0200 | [diff] [blame] | 513 | uart_poll_out(uart_console_dev, '\r'); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 514 | uart_poll_out(uart_console_dev, '\n'); |
Patrik Flykt | 8ff96b5 | 2018-11-29 11:12:22 -0800 | [diff] [blame] | 515 | cur = 0U; |
| 516 | end = 0U; |
Luiz Augusto von Dentz | 6b2443e | 2016-11-11 14:11:44 +0200 | [diff] [blame] | 517 | k_fifo_put(lines_queue, cmd); |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 518 | cmd = NULL; |
| 519 | break; |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 520 | case '\t': |
| 521 | if (completion_cb && !end) { |
| 522 | cur += completion_cb(cmd->line, cur); |
| 523 | } |
| 524 | break; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 525 | default: |
| 526 | break; |
| 527 | } |
Ruslan Mstoi | ca94b86 | 2018-02-22 18:33:01 +0200 | [diff] [blame] | 528 | |
| 529 | continue; |
Johan Hedberg | f88cccd | 2015-12-08 10:35:04 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Johan Hedberg | 6147fc6 | 2015-12-08 09:26:31 +0200 | [diff] [blame] | 532 | /* Ignore characters if there's no more buffer space */ |
Johan Hedberg | a9f6f89 | 2015-12-08 22:45:12 +0200 | [diff] [blame] | 533 | if (cur + end < sizeof(cmd->line) - 1) { |
| 534 | insert_char(&cmd->line[cur++], byte, end); |
Johan Hedberg | 6147fc6 | 2015-12-08 09:26:31 +0200 | [diff] [blame] | 535 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 539 | static void console_input_init(void) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 540 | { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 541 | uint8_t c; |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 542 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 543 | uart_irq_rx_disable(uart_console_dev); |
| 544 | uart_irq_tx_disable(uart_console_dev); |
Daniel Leung | e643ced | 2016-03-03 10:14:50 -0800 | [diff] [blame] | 545 | |
| 546 | uart_irq_callback_set(uart_console_dev, uart_console_isr); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 547 | |
| 548 | /* Drain the fifo */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 549 | while (uart_irq_rx_ready(uart_console_dev)) { |
| 550 | uart_fifo_read(uart_console_dev, &c, 1); |
Daniel Leung | 1ad2a56 | 2015-08-05 12:13:36 -0700 | [diff] [blame] | 551 | } |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 552 | |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 553 | uart_irq_rx_enable(uart_console_dev); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 554 | } |
| 555 | |
Luiz Augusto von Dentz | 6b2443e | 2016-11-11 14:11:44 +0200 | [diff] [blame] | 556 | void uart_register_input(struct k_fifo *avail, struct k_fifo *lines, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 557 | uint8_t (*completion)(char *str, uint8_t len)) |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 558 | { |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 559 | avail_queue = avail; |
| 560 | lines_queue = lines; |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 561 | completion_cb = completion; |
Andrei Emeltchenko | 879541a | 2015-05-04 14:43:36 +0300 | [diff] [blame] | 562 | |
| 563 | console_input_init(); |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 564 | } |
Szymon Janc | d7e8fd0 | 2016-05-25 16:23:42 +0200 | [diff] [blame] | 565 | |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 566 | #else |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 567 | #define console_input_init(x) \ |
| 568 | do { /* nothing */ \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 569 | } while ((0)) |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 570 | #define uart_register_input(x) \ |
| 571 | do { /* nothing */ \ |
Andrei Emeltchenko | 139c856 | 2015-04-20 11:04:22 +0300 | [diff] [blame] | 572 | } while ((0)) |
| 573 | #endif |
| 574 | |
Anas Nashif | ea0d0b2 | 2015-07-01 17:22:39 -0400 | [diff] [blame] | 575 | /** |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 576 | * |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 577 | * @brief Install printk/stdout hook for UART console output |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 578 | * |
Anas Nashif | 1362e3c | 2015-07-01 17:29:04 -0400 | [diff] [blame] | 579 | * @return N/A |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 580 | */ |
| 581 | |
Andrei Emeltchenko | caa95fc | 2019-12-09 11:28:17 +0200 | [diff] [blame] | 582 | static void uart_console_hook_install(void) |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 583 | { |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 584 | __stdout_hook_install(console_out); |
| 585 | __printk_hook_install(console_out); |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 586 | } |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 587 | |
| 588 | /** |
| 589 | * |
| 590 | * @brief Initialize one UART as the console/debug port |
| 591 | * |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 592 | * @return 0 if successful, otherwise failed. |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 593 | */ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 594 | static int uart_console_init(const struct device *arg) |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 595 | { |
Jithu Joseph | fca0add | 2016-11-06 16:58:14 -0800 | [diff] [blame] | 596 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 597 | ARG_UNUSED(arg); |
| 598 | |
Emil Obalski | b552e60 | 2020-07-30 14:52:15 +0200 | [diff] [blame] | 599 | /* Claim console device */ |
Daniel Leung | 08b4fd4 | 2015-12-01 08:42:20 -0800 | [diff] [blame] | 600 | uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); |
| 601 | |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 602 | uart_console_hook_install(); |
| 603 | |
Andre Guedes | 024cfe7 | 2016-03-09 14:01:20 -0300 | [diff] [blame] | 604 | return 0; |
Daniel Leung | ad2d296 | 2015-08-12 10:17:35 -0700 | [diff] [blame] | 605 | } |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 606 | |
Chuck Jordan | 12e29fe | 2016-05-06 12:43:56 -0700 | [diff] [blame] | 607 | /* UART console initializes after the UART device itself */ |
Benjamin Walsh | a4ec963 | 2016-01-28 15:16:31 -0500 | [diff] [blame] | 608 | SYS_INIT(uart_console_init, |
Jithu Joseph | fca0add | 2016-11-06 16:58:14 -0800 | [diff] [blame] | 609 | #if defined(CONFIG_USB_UART_CONSOLE) |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 610 | APPLICATION, |
Jithu Joseph | fca0add | 2016-11-06 16:58:14 -0800 | [diff] [blame] | 611 | #elif defined(CONFIG_EARLY_CONSOLE) |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 612 | PRE_KERNEL_1, |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 613 | #else |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 614 | POST_KERNEL, |
Dmitriy Korovkin | 57f2741 | 2015-10-26 15:56:02 -0400 | [diff] [blame] | 615 | #endif |
Christopher Collins | 1473511 | 2018-01-17 18:01:52 -0800 | [diff] [blame] | 616 | CONFIG_UART_CONSOLE_INIT_PRIORITY); |