Skip to content

Commit 3eb0d31

Browse files
author
Magnus Röös
committed
add a watch monitor
1 parent 4f3ba98 commit 3eb0d31

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

include/mios/cli.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ int cli_getc(cli_t *cli, int wait);
4040

4141
int cli_on_stream(struct stream *s, char promptchar);
4242

43+
error_t cli_dispatch_command(cli_t *c, int argc, char *argv[]);
44+
4345
void cli_console(char promptchar);
4446

4547
#define cli_flush(cli) stream_write((cli)->cl_stream, NULL, 0, 0)

src/drivers/pl011.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,31 @@ pl011_uart_read(struct stream *s, void *buf, size_t size, size_t minbytes)
192192
return size;
193193
}
194194

195+
static task_waitable_t *
196+
pl011_uart_poll(stream_t *s, poll_type_t type)
197+
{
198+
pl011_t *u = (pl011_t *)s;
199+
200+
irq_forbid(IRQ_LEVEL_CONSOLE);
201+
202+
if(type == POLL_STREAM_WRITE) {
203+
204+
if(TX_FIFO_SIZE - (u->tx_fifo_wrptr - u->tx_fifo_rdptr))
205+
return NULL;
206+
return &u->uart_tx;
207+
208+
} else {
209+
210+
if(u->rx_fifo_wrptr != u->rx_fifo_rdptr)
211+
return NULL;
212+
return &u->uart_rx;
213+
}
214+
}
195215

196216
static const stream_vtable_t pl011_uart_vtable = {
197217
.read = pl011_uart_read,
198-
.write = pl011_uart_write
218+
.write = pl011_uart_write,
219+
.poll = pl011_uart_poll
199220
};
200221

201222
struct stream *

src/shell/cli.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,38 @@ dispatch_command(cli_t *c, char *line)
103103
if(argc == 0)
104104
return;
105105

106+
cli_dispatch_command(c, argc, c->cl_argv);
107+
}
108+
109+
error_t
110+
cli_dispatch_command(cli_t *c, int argc, char *argv[])
111+
{
106112
extern unsigned long _clicmd_array_begin;
107113
extern unsigned long _clicmd_array_end;
108114

109115
cli_cmd_t *clicmd = (void *)&_clicmd_array_begin;
110116
cli_cmd_t *clicmd_array_end = (void *)&_clicmd_array_end;
111117

112-
if(!strcmp(c->cl_argv[0], "help")) {
118+
if(!strcmp(argv[0], "help")) {
113119
cli_printf(c, "Available commands:\n");
114120
for(; clicmd != clicmd_array_end; clicmd++) {
115121
cli_printf(c, "\t%s\n", clicmd->cmd);
116122
}
117-
return;
123+
return ERR_OK;
118124
}
119125

120126
for(; clicmd != clicmd_array_end; clicmd++) {
121-
if(!strcmp(c->cl_argv[0], clicmd->cmd)) {
127+
if(!strcmp(argv[0], clicmd->cmd)) {
122128
error_t err = clicmd->dispatch(c, argc, c->cl_argv);
123129

124130
if(err) {
125131
cli_printf(c, "! Error: %s\n", error_to_string(err));
126132
}
127-
return;
133+
return err;
128134
}
129135
}
130136
cli_printf(c, "No such command\n");
137+
return ERR_NOT_FOUND;
131138
}
132139

133140
#define ESCAPE 27

src/shell/shell.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
GLOBALDEPS += ${SRC}/shell/shell.mk
22

3-
SRCS += ${SRC}/shell/cli.c \
4-
${SRC}/shell/monitor.c \
3+
SRCS += \
4+
${SRC}/shell/cli.c \
55
${SRC}/shell/cmd_gpio.c \
66
${SRC}/shell/cmd_i2c.c \
77
${SRC}/shell/history.c \
8+
${SRC}/shell/monitor.c \
89
${SRC}/shell/perf.c \
10+
${SRC}/shell/watch.c \
911

1012
${MOS}/shell/cli.o : CFLAGS += ${NOFPU}
1113
${MOS}/shell/monitor.o : CFLAGS += ${NOFPU}

src/shell/watch.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdlib.h>
2+
#include <unistd.h>
3+
#include <string.h>
4+
#include <stdbool.h>
5+
6+
#include <mios/cli.h>
7+
#include <mios/mios.h>
8+
9+
10+
static error_t
11+
cmd_watch(cli_t *cli, int argc, char **argv)
12+
{
13+
const uint64_t watchtime = 1000000;
14+
uint64_t deadline = clock_get() + watchtime;
15+
if (argc == 1) {
16+
cli_printf(cli, "watch <command> <args>\n");
17+
return ERR_INVALID_ARGS;
18+
}
19+
while (true) {
20+
error_t err = cli_dispatch_command(cli, argc - 1, argv + 1);
21+
if (err) {
22+
return err;
23+
}
24+
pollset_t ps = {
25+
.obj = cli->cl_stream,
26+
.type = POLL_STREAM_READ,
27+
};
28+
if (poll(&ps, 1, NULL, deadline) < 0) {
29+
deadline += watchtime;
30+
continue;
31+
}
32+
uint8_t c;
33+
if (stream_read(cli->cl_stream, &c, 1, 1) <= 0) {
34+
return ERR_INTERRUPTED;
35+
}
36+
if (c == 0x03) {
37+
cli_printf(cli, "Interrupted Ctrl-C\n");
38+
return ERR_OK;
39+
}
40+
}
41+
}
42+
43+
CLI_CMD_DEF("watch", cmd_watch);

0 commit comments

Comments
 (0)