From b3555efa723d0b7101ea6989dc04492eca6b8745 Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Wed, 4 Oct 2023 00:36:34 -0600 Subject: demo: implement cli input --- demo/console.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 4 deletions(-) (limited to 'demo/console.c') 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 +#include #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); +} -- cgit v1.2.3