Skip to content

Commit e1db87e

Browse files
dnltzkartben
authored andcommitted
driver: serial: uart_shell: Add read command
This command waits for a given time (in seconds) and will continuously poll from the UART device and print on the Shell console. This command can be used to also test the RX line. Signed-off-by: Daniel Schultz <d.schultz@phytec.de>
1 parent 46ba523 commit e1db87e

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

drivers/serial/uart_shell.c

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,68 @@ static int cmd_uart_write(const struct shell *sh, size_t argc, char **argv)
3636
return 0;
3737
}
3838

39+
40+
static int cmd_uart_read(const struct shell *sh, size_t argc, char **argv)
41+
{
42+
char *s_dev_name = argv[1];
43+
const struct device *dev = shell_device_get_binding(s_dev_name);
44+
int ret = 0;
45+
char chr;
46+
k_timepoint_t end;
47+
uint64_t seconds;
48+
49+
if (!dev || !device_is_uart(dev)) {
50+
shell_error(sh, "UART: Device driver %s not found.", s_dev_name);
51+
return -ENODEV;
52+
}
53+
54+
seconds = shell_strtoul(argv[2], 10, &ret);
55+
if (ret != 0) {
56+
shell_help(sh);
57+
return SHELL_CMD_HELP_PRINTED;
58+
}
59+
if (seconds < 1) {
60+
return -EINVAL;
61+
}
62+
shell_info(sh, "UART: Read for %lli seconds from %s.", seconds, s_dev_name);
63+
64+
end = sys_timepoint_calc(K_SECONDS(seconds));
65+
while (!sys_timepoint_expired(end)) {
66+
ret = uart_poll_in(dev, &chr);
67+
if (ret == 0) {
68+
shell_fprintf_normal(sh, "%c", chr);
69+
}
70+
if (ret != 0 && ret != -1) {
71+
shell_error(sh, "Failed to read from UART (%d)", ret);
72+
break;
73+
}
74+
}
75+
76+
shell_fprintf_normal(sh, "\n");
77+
78+
return ret;
79+
}
80+
81+
3982
static int cmd_uart_baudrate(const struct shell *sh, size_t argc, char **argv)
4083
{
4184
char *s_dev_name = argv[1];
4285
const struct device *dev;
4386
struct uart_config cfg;
4487
uint32_t baudrate;
45-
int ret;
88+
int ret = 0;
4689

4790
dev = shell_device_get_binding(s_dev_name);
4891
if (!dev || !device_is_uart(dev)) {
4992
shell_error(sh, "UART: Device driver %s not found.", s_dev_name);
5093
return -ENODEV;
5194
}
5295

53-
baudrate = strtol(argv[2], NULL, 10);
96+
baudrate = shell_strtoul(argv[2], 10, &ret);
97+
if (ret != 0) {
98+
shell_help(sh);
99+
return SHELL_CMD_HELP_PRINTED;
100+
}
54101
ret = uart_config_get(dev, &cfg);
55102
if (ret < 0) {
56103
shell_error(sh, "UART: Failed to get current configuration: %d", ret);
@@ -126,6 +173,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_uart_cmds,
126173
"Write data to the UART device\n"
127174
"Usage: write <device> <data>",
128175
cmd_uart_write, 3, 0),
176+
SHELL_CMD_ARG(read, &dsub_device_name,
177+
"read data from the UART device\n"
178+
"Usage: read <device> <duration in secs>",
179+
cmd_uart_read, 3, 0),
129180
SHELL_CMD_ARG(baudrate, &dsub_device_name,
130181
"Configure UART device baudrate\n"
131182
"Usage: baudrate <device> <baudrate>",

0 commit comments

Comments
 (0)