From 4f3ba980e38f1384de76e549e0cff3ccff0b92e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20R=C3=B6=C3=B6s?= Date: Mon, 18 Aug 2025 14:01:48 +0200 Subject: [PATCH 1/2] hisory: don't store history if previous command was the same --- src/shell/history.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shell/history.c b/src/shell/history.c index 30b56990..438ce1e4 100644 --- a/src/shell/history.c +++ b/src/shell/history.c @@ -22,7 +22,7 @@ history_add(const char *line) if (history.latest == NULL) { history.lines[0] = strdup(line); history.latest = history.lines; - } else { + } else if (strcmp(line, *history.latest)) { history.latest++; if (history.latest >= &history.lines[MAX_LINES]) history.latest = history.lines; From e345b9e86dafb0a3dd7e5e6499f3775458443208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20R=C3=B6=C3=B6s?= Date: Fri, 22 Aug 2025 11:49:54 +0200 Subject: [PATCH 2/2] add a watch monitor --- include/mios/cli.h | 2 ++ src/drivers/pl011.c | 23 ++++++++++++++++++++++- src/shell/cli.c | 15 +++++++++++---- src/shell/shell.mk | 6 ++++-- src/shell/watch.c | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/shell/watch.c diff --git a/include/mios/cli.h b/include/mios/cli.h index f8fb2ef0..f2cf8037 100644 --- a/include/mios/cli.h +++ b/include/mios/cli.h @@ -40,6 +40,8 @@ int cli_getc(cli_t *cli, int wait); int cli_on_stream(struct stream *s, char promptchar); +error_t cli_dispatch_command(cli_t *c, int argc, char *argv[]); + void cli_console(char promptchar); #define cli_flush(cli) stream_write((cli)->cl_stream, NULL, 0, 0) diff --git a/src/drivers/pl011.c b/src/drivers/pl011.c index 5c40c117..f4fdf317 100644 --- a/src/drivers/pl011.c +++ b/src/drivers/pl011.c @@ -192,10 +192,31 @@ pl011_uart_read(struct stream *s, void *buf, size_t size, size_t minbytes) return size; } +static task_waitable_t * +pl011_uart_poll(stream_t *s, poll_type_t type) +{ + pl011_t *u = (pl011_t *)s; + + irq_forbid(IRQ_LEVEL_CONSOLE); + + if(type == POLL_STREAM_WRITE) { + + if(TX_FIFO_SIZE - (u->tx_fifo_wrptr - u->tx_fifo_rdptr)) + return NULL; + return &u->uart_tx; + + } else { + + if(u->rx_fifo_wrptr != u->rx_fifo_rdptr) + return NULL; + return &u->uart_rx; + } +} static const stream_vtable_t pl011_uart_vtable = { .read = pl011_uart_read, - .write = pl011_uart_write + .write = pl011_uart_write, + .poll = pl011_uart_poll }; struct stream * diff --git a/src/shell/cli.c b/src/shell/cli.c index 2b5ca4c0..3c4afe87 100644 --- a/src/shell/cli.c +++ b/src/shell/cli.c @@ -103,31 +103,38 @@ dispatch_command(cli_t *c, char *line) if(argc == 0) return; + cli_dispatch_command(c, argc, c->cl_argv); +} + +error_t +cli_dispatch_command(cli_t *c, int argc, char *argv[]) +{ extern unsigned long _clicmd_array_begin; extern unsigned long _clicmd_array_end; cli_cmd_t *clicmd = (void *)&_clicmd_array_begin; cli_cmd_t *clicmd_array_end = (void *)&_clicmd_array_end; - if(!strcmp(c->cl_argv[0], "help")) { + if(!strcmp(argv[0], "help")) { cli_printf(c, "Available commands:\n"); for(; clicmd != clicmd_array_end; clicmd++) { cli_printf(c, "\t%s\n", clicmd->cmd); } - return; + return ERR_OK; } for(; clicmd != clicmd_array_end; clicmd++) { - if(!strcmp(c->cl_argv[0], clicmd->cmd)) { + if(!strcmp(argv[0], clicmd->cmd)) { error_t err = clicmd->dispatch(c, argc, c->cl_argv); if(err) { cli_printf(c, "! Error: %s\n", error_to_string(err)); } - return; + return err; } } cli_printf(c, "No such command\n"); + return ERR_NOT_FOUND; } #define ESCAPE 27 diff --git a/src/shell/shell.mk b/src/shell/shell.mk index 50b3c07e..3144304a 100644 --- a/src/shell/shell.mk +++ b/src/shell/shell.mk @@ -1,11 +1,13 @@ GLOBALDEPS += ${SRC}/shell/shell.mk -SRCS += ${SRC}/shell/cli.c \ - ${SRC}/shell/monitor.c \ +SRCS += \ + ${SRC}/shell/cli.c \ ${SRC}/shell/cmd_gpio.c \ ${SRC}/shell/cmd_i2c.c \ ${SRC}/shell/history.c \ + ${SRC}/shell/monitor.c \ ${SRC}/shell/perf.c \ + ${SRC}/shell/watch.c \ ${MOS}/shell/cli.o : CFLAGS += ${NOFPU} ${MOS}/shell/monitor.o : CFLAGS += ${NOFPU} diff --git a/src/shell/watch.c b/src/shell/watch.c new file mode 100644 index 00000000..c7648a4b --- /dev/null +++ b/src/shell/watch.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#include +#include + + +static error_t +cmd_watch(cli_t *cli, int argc, char **argv) +{ + const uint64_t watchtime = 1000000; + uint64_t deadline = clock_get() + watchtime; + if (argc == 1) { + cli_printf(cli, "watch \n"); + return ERR_INVALID_ARGS; + } + uint8_t c = 0; + const pollset_t ps = { + .obj = cli->cl_stream, + .type = POLL_STREAM_READ, + }; + while (c != 0x03) { + error_t err = cli_dispatch_command(cli, argc - 1, argv + 1); + if (err) + return err; + + if (poll(&ps, 1, NULL, deadline) < 0) { + deadline += watchtime; + continue; + } + stream_read(cli->cl_stream, &c, 1, 1); + } + return ERR_OK; +} + +CLI_CMD_DEF("watch", cmd_watch);