1 /*
2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "console.h"
16 #include "command.h"
17 #include "uart.h"
18
19 volatile bool cmd_exe_in_irq = false;
20 #ifdef CFG_RTOS
21 static rtos_notify_cb_t console_notify_cb = NULL;
22 #endif
23
console_irq_handler(void)24 static void console_irq_handler(void)
25 {
26 bool cmd_valid = false;
27 while (stdio_uart_get_rx_count()) {
28 unsigned char c = (unsigned char)stdio_uart_getc();
29 if (command_handle_char(c) > 0) {
30 cmd_valid = true;
31 }
32 }
33
34 if (cmd_valid) {
35 /* if cmd_exe_in_irq was true, we check and execute commands here */
36 if (cmd_exe_in_irq) {
37 console_schedule();
38 }
39 #ifdef CFG_RTOS
40 else if (console_notify_cb) {
41 console_notify_cb(true);
42 }
43 #endif /* CFG_RTOS */
44 }
45 }
46
console_putc(char c)47 void console_putc(char c)
48 {
49 stdio_uart_putc(c);
50 }
51
console_puts(const char * s)52 void console_puts(const char *s)
53 {
54 while (*s) {
55 stdio_uart_putc(*s++);
56 }
57 }
58
console_init(void)59 void console_init(void)
60 {
61 if (!stdio_uart_inited) {
62 stdio_uart_init();
63 }
64
65 command_init();
66
67 register_stdio_uart_rx_function(console_irq_handler);
68 }
69
console_schedule(void)70 void console_schedule(void)
71 {
72 command_parser();
73 }
74
console_cmd_add(const char * name,const char * usage,int maxargs,int (* func)(int,char * []))75 int console_cmd_add(const char *name, const char *usage, int maxargs, int (*func)(int, char *[]))
76 {
77 return command_add(name, usage, maxargs, (cmd_func_t)func);
78 }
79
console_cmd_strtoul(const char * cp,char ** endp,unsigned int base)80 unsigned int console_cmd_strtoul(const char *cp, char **endp, unsigned int base)
81 {
82 return command_strtoul(cp, endp, base);
83 }
84
console_buf_empty(void)85 unsigned int console_buf_empty(void)
86 {
87 return command_pend_list_empty();
88 }
89
90 #ifdef CFG_RTOS
console_ntf_register(rtos_notify_cb_t notify_cb)91 void console_ntf_register(rtos_notify_cb_t notify_cb)
92 {
93 if (!console_notify_cb) {
94 console_notify_cb = notify_cb;
95 }
96 }
97 #endif /* CFG_RTOS */
98
99 #if CONSOLE_GLOBAL_DEBUG_MODE
100 #include "reg_stdio_uart.h"
101
console_global_dbgmode_enable(void)102 void console_global_dbgmode_enable(void)
103 {
104 NVIC_SetPriority(UART_IRQn, 0x07UL);
105 cmd_exe_in_irq = true;
106 }
107 #endif /* CONSOLE_GLOBAL_DEBUG_MODE */
108