blob: 74d1013d26c769ca372659595f239002f46b7fd2 [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001/*
2 * Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005 */
6
Anas Nashif275ca602015-12-04 10:09:39 -05007/**
8 * @file
9 * @brief UART-driven console
10 *
Dan Kalowskyda67b292015-10-20 09:42:33 -070011 *
12 * Serial console driver.
13 * Hooks into the printk and fputc (for printf) modules. Poll driven.
Anas Nashifea0d0b22015-07-01 17:22:39 -040014 */
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070015
Baohong Liufa2d38e2016-11-30 14:58:45 -080016#include <kernel.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030017
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070018#include <stdio.h>
Kumar Gala78908162017-04-19 10:32:08 -050019#include <zephyr/types.h>
Erwan Gouriou2716cbc2019-11-27 17:31:57 +010020#include <sys/__assert.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030021#include <errno.h>
Johan Hedbergf88cccd2015-12-08 10:35:04 +020022#include <ctype.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070023
Daniel Leungad2d2962015-08-12 10:17:35 -070024#include <device.h>
25#include <init.h>
26
Anas Nashifd1b27182019-06-25 15:54:01 -040027#include <drivers/uart.h>
Anas Nashife8a182c2019-06-21 13:13:19 -040028#include <drivers/console/console.h>
29#include <drivers/console/uart_console.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070030#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040031#include <linker/sections.h>
Anas Nashife1e05a22019-06-25 12:25:32 -040032#include <sys/atomic.h>
Anas Nashif9ab2a562019-06-26 10:33:49 -040033#include <sys/printk.h>
Christopher Collins14735112018-01-17 18:01:52 -080034#ifdef CONFIG_UART_CONSOLE_MCUMGR
35#include "mgmt/serial.h"
36#endif
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070037
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020038static const struct device *uart_console_dev;
Daniel Leung08b4fd42015-12-01 08:42:20 -080039
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040040#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +010041
42static uart_console_in_debug_hook_t debug_hook_in;
43void uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook)
44{
45 debug_hook_in = hook;
46}
47
Christopher Collins14735112018-01-17 18:01:52 -080048static UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop) {
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040049 ARG_UNUSED(c);
50 return !UART_CONSOLE_DEBUG_HOOK_HANDLED;
51}
52
53static uart_console_out_debug_hook_t *debug_hook_out = debug_hook_out_nop;
54void 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 Frascinof3a92412016-11-16 12:01:48 +000060
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040061#endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
62
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070063
64#if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
Anas Nashifea0d0b22015-07-01 17:22:39 -040065/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070066 *
Anas Nashiff367f072015-07-01 17:51:40 -040067 * @brief Output one character to UART
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070068 *
69 * Outputs both line feed and carriage return in the case of a '\n'.
70 *
Daniel Leung08b4fd42015-12-01 08:42:20 -080071 * @param c Character to output
72 *
Anas Nashif1362e3c2015-07-01 17:29:04 -040073 * @return The character passed as input.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070074 */
75
Daniel Leung08b4fd42015-12-01 08:42:20 -080076static int console_out(int c)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070077{
Vincenzo Frascinof3a92412016-11-16 12:01:48 +000078#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
79
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040080 int handled_by_debug_server = HANDLE_DEBUG_HOOK_OUT(c);
81
82 if (handled_by_debug_server) {
83 return c;
84 }
85
Christopher Collins14735112018-01-17 18:01:52 -080086#endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
Vincenzo Frascinof3a92412016-11-16 12:01:48 +000087
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070088 if ('\n' == c) {
Andy Ross425145d2016-09-01 08:49:10 -070089 uart_poll_out(uart_console_dev, '\r');
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070090 }
Andy Ross425145d2016-09-01 08:49:10 -070091 uart_poll_out(uart_console_dev, c);
92
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070093 return c;
94}
Daniel Leung08b4fd42015-12-01 08:42:20 -080095
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070096#endif
97
98#if defined(CONFIG_STDOUT_CONSOLE)
99extern void __stdout_hook_install(int (*hook)(int));
100#else
Christopher Collins14735112018-01-17 18:01:52 -0800101#define __stdout_hook_install(x) \
102 do { /* nothing */ \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700103 } while ((0))
104#endif
105
106#if defined(CONFIG_PRINTK)
107extern void __printk_hook_install(int (*fn)(int));
108#else
Christopher Collins14735112018-01-17 18:01:52 -0800109#define __printk_hook_install(x) \
110 do { /* nothing */ \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700111 } while ((0))
112#endif
113
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300114#if defined(CONFIG_CONSOLE_HANDLER)
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200115static struct k_fifo *avail_queue;
116static struct k_fifo *lines_queue;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500117static uint8_t (*completion_cb)(char *line, uint8_t len);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300118
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200119/* Control characters */
qianfan Zhao13e1c3c2018-05-29 12:43:16 +0800120#define BS 0x08
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200121#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 Janc6b0cf542016-10-26 18:10:50 +0200130#define ANSI_END 'F'
131#define ANSI_HOME 'H'
Szymon Janca0373162016-10-26 18:15:02 +0200132#define ANSI_DEL '~'
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200133
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200134static int read_uart(const struct device *uart, uint8_t *buf,
135 unsigned int size)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300136{
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 Hedberga9f6f892015-12-08 22:45:12 +0200150static inline void cursor_forward(unsigned int count)
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200151{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200152 printk("\x1b[%uC", count);
153}
154
155static inline void cursor_backward(unsigned int count)
156{
157 printk("\x1b[%uD", count);
158}
159
Johan Hedbergceba31a2015-12-09 12:40:29 +0200160static inline void cursor_save(void)
161{
162 printk("\x1b[s");
163}
164
165static inline void cursor_restore(void)
166{
167 printk("\x1b[u");
168}
169
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500170static void insert_char(char *pos, char c, uint8_t end)
Johan Hedberga9f6f892015-12-08 22:45:12 +0200171{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200172 char tmp;
173
174 /* Echo back to console */
175 uart_poll_out(uart_console_dev, c);
176
Patrik Flykt24d71432019-03-26 19:57:45 -0600177 if (end == 0U) {
Johan Hedberga9f6f892015-12-08 22:45:12 +0200178 *pos = c;
179 return;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200180 }
Johan Hedberga9f6f892015-12-08 22:45:12 +0200181
182 tmp = *pos;
183 *(pos++) = c;
184
Johan Hedbergceba31a2015-12-09 12:40:29 +0200185 cursor_save();
186
187 while (end-- > 0) {
Johan Hedberga9f6f892015-12-08 22:45:12 +0200188 uart_poll_out(uart_console_dev, tmp);
Johan Hedbergceba31a2015-12-09 12:40:29 +0200189 c = *pos;
190 *(pos++) = tmp;
Johan Hedberga9f6f892015-12-08 22:45:12 +0200191 tmp = c;
192 }
193
194 /* Move cursor back to right place */
Johan Hedbergceba31a2015-12-09 12:40:29 +0200195 cursor_restore();
Johan Hedberga9f6f892015-12-08 22:45:12 +0200196}
197
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500198static void del_char(char *pos, uint8_t end)
Johan Hedberga9f6f892015-12-08 22:45:12 +0200199{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200200 uart_poll_out(uart_console_dev, '\b');
201
Patrik Flykt24d71432019-03-26 19:57:45 -0600202 if (end == 0U) {
Johan Hedberga9f6f892015-12-08 22:45:12 +0200203 uart_poll_out(uart_console_dev, ' ');
204 uart_poll_out(uart_console_dev, '\b');
205 return;
206 }
207
Johan Hedbergceba31a2015-12-09 12:40:29 +0200208 cursor_save();
209
210 while (end-- > 0) {
211 *pos = *(pos + 1);
212 uart_poll_out(uart_console_dev, *(pos++));
Johan Hedberga9f6f892015-12-08 22:45:12 +0200213 }
214
215 uart_poll_out(uart_console_dev, ' ');
216
217 /* Move cursor back to right place */
Johan Hedbergceba31a2015-12-09 12:40:29 +0200218 cursor_restore();
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200219}
220
Johan Hedberg8683dc42015-12-09 11:36:25 +0200221enum {
222 ESC_ESC,
223 ESC_ANSI,
224 ESC_ANSI_FIRST,
225 ESC_ANSI_VAL,
Christopher Collins14735112018-01-17 18:01:52 -0800226 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 Hedberg8683dc42015-12-09 11:36:25 +0200233};
234
235static atomic_t esc_state;
236static unsigned int ansi_val, ansi_val_2;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500237static uint8_t cur, end;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200238
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500239static void handle_ansi(uint8_t byte, char *line)
Johan Hedberg8683dc42015-12-09 11:36:25 +0200240{
241 if (atomic_test_and_clear_bit(&esc_state, ESC_ANSI_FIRST)) {
242 if (!isdigit(byte)) {
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800243 ansi_val = 1U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200244 goto ansi_cmd;
245 }
246
247 atomic_set_bit(&esc_state, ESC_ANSI_VAL);
248 ansi_val = byte - '0';
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800249 ansi_val_2 = 0U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200250 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 Flykt24d71432019-03-26 19:57:45 -0600256 ansi_val_2 *= 10U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200257 ansi_val_2 += byte - '0';
258 } else {
Patrik Flykt24d71432019-03-26 19:57:45 -0600259 ansi_val *= 10U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200260 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
275ansi_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 Janc6b0cf542016-10-26 18:10:50 +0200295 case ANSI_HOME:
296 if (!cur) {
297 break;
298 }
299
300 cursor_backward(cur);
301 end += cur;
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800302 cur = 0U;
Szymon Janc6b0cf542016-10-26 18:10:50 +0200303 break;
304 case ANSI_END:
305 if (!end) {
306 break;
307 }
308
309 cursor_forward(end);
310 cur += end;
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800311 end = 0U;
Szymon Janc6b0cf542016-10-26 18:10:50 +0200312 break;
Szymon Janca0373162016-10-26 18:15:02 +0200313 case ANSI_DEL:
314 if (!end) {
315 break;
316 }
317
318 cursor_forward(1);
319 del_char(&line[cur], --end);
320 break;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200321 default:
322 break;
323 }
324
325 atomic_clear_bit(&esc_state, ESC_ANSI);
326}
327
Christopher Collins14735112018-01-17 18:01:52 -0800328#ifdef CONFIG_UART_CONSOLE_MCUMGR
329
330static 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
345static 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 */
399static 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 Flykt8ff96b52018-11-29 11:12:22 -0800426 cur = 0U;
427 end = 0U;
Christopher Collins14735112018-01-17 18:01:52 -0800428 }
429
430 return true;
431}
432
433#endif /* CONFIG_UART_CONSOLE_MCUMGR */
434
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200435static void uart_console_isr(const struct device *unused, void *user_data)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300436{
437 ARG_UNUSED(unused);
Tomasz Bursztyka7d1af022020-06-24 19:06:44 +0200438 ARG_UNUSED(user_data);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300439
Johan Hedberg5ad78032015-12-07 15:03:20 +0200440 while (uart_irq_update(uart_console_dev) &&
441 uart_irq_is_pending(uart_console_dev)) {
Tomasz Bursztyka2f1af492017-01-24 10:08:08 +0100442 static struct console_input *cmd;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500443 uint8_t byte;
Johan Hedberg5ad78032015-12-07 15:03:20 +0200444 int rx;
445
446 if (!uart_irq_rx_ready(uart_console_dev)) {
447 continue;
448 }
449
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300450 /* Character(s) have been received */
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300451
Johan Hedberg5ad78032015-12-07 15:03:20 +0200452 rx = read_uart(uart_console_dev, &byte, 1);
453 if (rx < 0) {
454 return;
455 }
456
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +0100457#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
458 if (debug_hook_in != NULL && debug_hook_in(byte) != 0) {
Johan Hedberg5ad78032015-12-07 15:03:20 +0200459 /*
460 * The input hook indicates that no further processing
461 * should be done by this handler.
462 */
463 return;
464 }
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +0100465#endif
Johan Hedberg5ad78032015-12-07 15:03:20 +0200466
467 if (!cmd) {
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200468 cmd = k_fifo_get(avail_queue, K_NO_WAIT);
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100469 if (!cmd) {
Peter Mitsisb1c10202015-09-17 13:22:00 -0400470 return;
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100471 }
Johan Hedberg5ad78032015-12-07 15:03:20 +0200472 }
Peter Mitsisb1c10202015-09-17 13:22:00 -0400473
Christopher Collins14735112018-01-17 18:01:52 -0800474#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 Hedbergf88cccd2015-12-08 10:35:04 +0200483 /* Handle ANSI escape mode */
Johan Hedberg8683dc42015-12-09 11:36:25 +0200484 if (atomic_test_bit(&esc_state, ESC_ANSI)) {
Szymon Janca0373162016-10-26 18:15:02 +0200485 handle_ansi(byte, cmd->line);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200486 continue;
487 }
488
489 /* Handle escape mode */
Johan Hedberg8683dc42015-12-09 11:36:25 +0200490 if (atomic_test_and_clear_bit(&esc_state, ESC_ESC)) {
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100491 if (byte == ANSI_ESC) {
Johan Hedberg8683dc42015-12-09 11:36:25 +0200492 atomic_set_bit(&esc_state, ESC_ANSI);
493 atomic_set_bit(&esc_state, ESC_ANSI_FIRST);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200494 }
495
496 continue;
497 }
498
Ruslan Mstoica94b862018-02-22 18:33:01 +0200499 /* Handle special control characters */
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200500 if (!isprint(byte)) {
501 switch (byte) {
qianfan Zhao13e1c3c2018-05-29 12:43:16 +0800502 case BS:
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200503 case DEL:
Johan Hedberga9f6f892015-12-08 22:45:12 +0200504 if (cur > 0) {
505 del_char(&cmd->line[--cur], end);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200506 }
507 break;
508 case ESC:
Johan Hedberg8683dc42015-12-09 11:36:25 +0200509 atomic_set_bit(&esc_state, ESC_ESC);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200510 break;
511 case '\r':
Johan Hedberga9f6f892015-12-08 22:45:12 +0200512 cmd->line[cur + end] = '\0';
Johan Hedberg65433182016-02-05 13:45:48 +0200513 uart_poll_out(uart_console_dev, '\r');
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200514 uart_poll_out(uart_console_dev, '\n');
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800515 cur = 0U;
516 end = 0U;
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200517 k_fifo_put(lines_queue, cmd);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200518 cmd = NULL;
519 break;
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200520 case '\t':
521 if (completion_cb && !end) {
522 cur += completion_cb(cmd->line, cur);
523 }
524 break;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200525 default:
526 break;
527 }
Ruslan Mstoica94b862018-02-22 18:33:01 +0200528
529 continue;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200530 }
531
Johan Hedberg6147fc62015-12-08 09:26:31 +0200532 /* Ignore characters if there's no more buffer space */
Johan Hedberga9f6f892015-12-08 22:45:12 +0200533 if (cur + end < sizeof(cmd->line) - 1) {
534 insert_char(&cmd->line[cur++], byte, end);
Johan Hedberg6147fc62015-12-08 09:26:31 +0200535 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300536 }
537}
538
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300539static void console_input_init(void)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300540{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500541 uint8_t c;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300542
Daniel Leung08b4fd42015-12-01 08:42:20 -0800543 uart_irq_rx_disable(uart_console_dev);
544 uart_irq_tx_disable(uart_console_dev);
Daniel Leunge643ced2016-03-03 10:14:50 -0800545
546 uart_irq_callback_set(uart_console_dev, uart_console_isr);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300547
548 /* Drain the fifo */
Daniel Leung08b4fd42015-12-01 08:42:20 -0800549 while (uart_irq_rx_ready(uart_console_dev)) {
550 uart_fifo_read(uart_console_dev, &c, 1);
Daniel Leung1ad2a562015-08-05 12:13:36 -0700551 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300552
Daniel Leung08b4fd42015-12-01 08:42:20 -0800553 uart_irq_rx_enable(uart_console_dev);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300554}
555
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200556void uart_register_input(struct k_fifo *avail, struct k_fifo *lines,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500557 uint8_t (*completion)(char *str, uint8_t len))
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300558{
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300559 avail_queue = avail;
560 lines_queue = lines;
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200561 completion_cb = completion;
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300562
563 console_input_init();
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300564}
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200565
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300566#else
Christopher Collins14735112018-01-17 18:01:52 -0800567#define console_input_init(x) \
568 do { /* nothing */ \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300569 } while ((0))
Christopher Collins14735112018-01-17 18:01:52 -0800570#define uart_register_input(x) \
571 do { /* nothing */ \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300572 } while ((0))
573#endif
574
Anas Nashifea0d0b22015-07-01 17:22:39 -0400575/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700576 *
Daniel Leungad2d2962015-08-12 10:17:35 -0700577 * @brief Install printk/stdout hook for UART console output
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700578 *
Anas Nashif1362e3c2015-07-01 17:29:04 -0400579 * @return N/A
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700580 */
581
Andrei Emeltchenkocaa95fc2019-12-09 11:28:17 +0200582static void uart_console_hook_install(void)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700583{
Daniel Leung08b4fd42015-12-01 08:42:20 -0800584 __stdout_hook_install(console_out);
585 __printk_hook_install(console_out);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700586}
Daniel Leungad2d2962015-08-12 10:17:35 -0700587
588/**
589 *
590 * @brief Initialize one UART as the console/debug port
591 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300592 * @return 0 if successful, otherwise failed.
Daniel Leungad2d2962015-08-12 10:17:35 -0700593 */
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200594static int uart_console_init(const struct device *arg)
Daniel Leungad2d2962015-08-12 10:17:35 -0700595{
Jithu Josephfca0add2016-11-06 16:58:14 -0800596
Daniel Leungad2d2962015-08-12 10:17:35 -0700597 ARG_UNUSED(arg);
598
Emil Obalskib552e602020-07-30 14:52:15 +0200599 /* Claim console device */
Daniel Leung08b4fd42015-12-01 08:42:20 -0800600 uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
601
Daniel Leungad2d2962015-08-12 10:17:35 -0700602 uart_console_hook_install();
603
Andre Guedes024cfe72016-03-09 14:01:20 -0300604 return 0;
Daniel Leungad2d2962015-08-12 10:17:35 -0700605}
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400606
Chuck Jordan12e29fe2016-05-06 12:43:56 -0700607/* UART console initializes after the UART device itself */
Benjamin Walsha4ec9632016-01-28 15:16:31 -0500608SYS_INIT(uart_console_init,
Jithu Josephfca0add2016-11-06 16:58:14 -0800609#if defined(CONFIG_USB_UART_CONSOLE)
Christopher Collins14735112018-01-17 18:01:52 -0800610 APPLICATION,
Jithu Josephfca0add2016-11-06 16:58:14 -0800611#elif defined(CONFIG_EARLY_CONSOLE)
Christopher Collins14735112018-01-17 18:01:52 -0800612 PRE_KERNEL_1,
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400613#else
Christopher Collins14735112018-01-17 18:01:52 -0800614 POST_KERNEL,
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400615#endif
Christopher Collins14735112018-01-17 18:01:52 -0800616 CONFIG_UART_CONSOLE_INIT_PRIORITY);