summaryrefslogtreecommitdiff
path: root/demo/console.c
diff options
context:
space:
mode:
authorAlejandro Soto <alejandro@34project.org>2023-10-04 00:36:34 -0600
committerAlejandro Soto <alejandro@34project.org>2023-10-04 00:36:38 -0600
commitb3555efa723d0b7101ea6989dc04492eca6b8745 (patch)
tree65217cf306fe30225cbd88653bdec96ef9275513 /demo/console.c
parent3de42a19661a3d6d24de46ca2920b06e1dc88fe0 (diff)
demo: implement cli input
Diffstat (limited to '')
-rw-r--r--demo/console.c99
1 files changed, 95 insertions, 4 deletions
diff --git a/demo/console.c b/demo/console.c
index e531b2e..b35ace7 100644
--- a/demo/console.c
+++ b/demo/console.c
@@ -1,11 +1,12 @@
#include <stdarg.h>
+#include <stddef.h>
#include "demo.h"
-struct lock console_lock;
-
#define JTAG_UART_BASE 0x30000000
#define JTAG_UART_DATA (*(volatile unsigned *)(JTAG_UART_BASE + 0))
+#define JTAG_UART_DATA_RVALID (1 << 15)
+#define JTAG_UART_DATA_RAVAIL 0xffff0000
#define JTAG_UART_CONTROL (*(volatile unsigned *)(JTAG_UART_BASE + 4))
#define JTAG_UART_CONTROL_RE (1 << 0)
#define JTAG_UART_CONTROL_AC (1 << 10)
@@ -16,6 +17,13 @@ struct lock console_lock;
#define INTC_MASK_TIMER (1 << 0)
#define INTC_MASK_UART (1 << 1)
+static struct lock console_lock;
+
+static int input_run = 0;
+static unsigned input_len = 0;
+static unsigned input_max;
+static char *input_buf;
+
static void print_char(char c)
{
while (!(JTAG_UART_CONTROL & JTAG_UART_CONTROL_WSPACE));
@@ -65,6 +73,9 @@ void console_init(void)
{
spin_init(&console_lock);
+ input_buf = NULL;
+ input_max = 0;
+
// Habilitar irqs de entrada uart
JTAG_UART_CONTROL = JTAG_UART_CONTROL_RE;
@@ -85,7 +96,11 @@ void print(const char *fmt, ...)
while (!(JTAG_UART_CONTROL & JTAG_UART_CONTROL_AC));
- print_str("cpu");
+ print_str("\r ");
+ for (unsigned i = 0; i < input_len; ++i)
+ print_char(' ');
+
+ print_str("\rcpu");
print_dec(this_cpu->num);
print_str(": ");
@@ -137,9 +152,85 @@ void print(const char *fmt, ...)
}
}
- print_str("\r\n");
+ print_str("\r\n> ");
+ if (!input_run && input_buf)
+ print_str(input_buf);
+
JTAG_UART_CONTROL = JTAG_UART_CONTROL_RE | JTAG_UART_CONTROL_AC;
spin_unlock(&console_lock, irq_save);
va_end(args);
}
+
+void read_line(char *buf, unsigned size)
+{
+ if (!size)
+ return;
+
+ buf[0] = '\0';
+
+ unsigned irq_save;
+ spin_lock(&console_lock, &irq_save);
+ input_buf = buf;
+ input_max = size;
+ input_len = 0;
+ spin_unlock(&console_lock, irq_save);
+
+ while (1) {
+ spin_lock(&console_lock, &irq_save);
+ if (input_run)
+ break;
+
+ spin_unlock(&console_lock, irq_save);
+ }
+
+ input_buf = NULL;
+ input_len = 0;
+ input_max = 0;
+ input_run = 0;
+ spin_unlock(&console_lock, irq_save);
+}
+
+void irq(void)
+{
+ unsigned irq_save;
+ spin_lock(&console_lock, &irq_save);
+
+ unsigned data;
+ do {
+ data = JTAG_UART_DATA;
+ if (!(data & JTAG_UART_DATA_RVALID))
+ break;
+ else if (input_run || !input_buf)
+ continue;
+
+ char c = (char)data;
+
+ switch (c) {
+ case 0x7f: // DEL
+ if (input_len > 0) {
+ --input_len;
+ print_str("\b \b");
+ }
+
+ break;
+
+ case '\n':
+ input_run = 1;
+ input_len = 0;
+ print_str("\r\n> ");
+ break;
+
+ default:
+ if (input_len < input_max - 1 && c >= ' ' && c <= '~') {
+ print_char(c);
+ input_buf[input_len++] = c;
+ input_buf[input_len] = '\0';
+ }
+
+ break;
+ }
+ } while (data & JTAG_UART_DATA_RAVAIL);
+
+ spin_unlock(&console_lock, irq_save);
+}