Skip to content

Commit

Permalink
shell: uart: added shell command
Browse files Browse the repository at this point in the history
Added shell command for UART. Currently only able to set baudrate and
flow control.

Signed-off-by: Yishai Jaffe <[email protected]>
  • Loading branch information
yishai1999 authored and henrikbrixandersen committed Jan 6, 2025
1 parent c7da46a commit 6ba0563
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_SERIAL_TEST serial_test.c)
zephyr_library_sources_ifdef(CONFIG_UART_ASYNC_RX_HELPER uart_async_rx.c)
zephyr_library_sources_ifdef(CONFIG_UART_ASYNC_TO_INT_DRIVEN_API uart_async_to_irq.c)
zephyr_library_sources_ifdef(CONFIG_UART_SHELL uart_shell.c)
zephyr_library_sources_ifdef(CONFIG_USB_CDC_ACM ${ZEPHYR_BASE}/misc/empty_file.c)
# zephyr-keep-sorted-stop

Expand Down
6 changes: 6 additions & 0 deletions drivers/serial/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ config UART_ASYNC_TO_INT_DRIVEN_RX_TIMEOUT
help
Receiver inactivity timeout. It is used to calculate timeout in microseconds.

config UART_SHELL
bool "UART Shell commands"
depends on SHELL
help
UART Shell commands

comment "Serial Drivers"

# zephyr-keep-sorted-start
Expand Down
116 changes: 116 additions & 0 deletions drivers/serial/uart_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2024 Yishai Jaffe <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/drivers/uart.h>
#include <zephyr/shell/shell.h>
#include <stdlib.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(uart_shell, CONFIG_LOG_DEFAULT_LEVEL);

static bool device_is_uart(const struct device *dev)
{
return DEVICE_API_IS(uart, dev);
}

static int cmd_uart_baudrate(const struct shell *sh, size_t argc, char **argv)
{
char *s_dev_name = argv[1];
const struct device *dev;
struct uart_config cfg;
uint32_t baudrate;
int ret;

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

baudrate = strtol(argv[2], NULL, 10);
ret = uart_config_get(dev, &cfg);
if (ret < 0) {
shell_error(sh, "UART: Failed to get current configuration: %d", ret);
return ret;
}
cfg.baudrate = baudrate;

ret = uart_configure(dev, &cfg);
if (ret < 0) {
shell_error(sh, "UART: Failed to configure device: %d", ret);
return ret;
}
return 0;
}

static int cmd_uart_flow_control(const struct shell *sh, size_t argc, char **argv)
{
char *s_dev_name = argv[1];
const struct device *dev;
struct uart_config cfg;
uint8_t flow_control;
int ret;

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

if (!strcmp(argv[2], "none")) {
flow_control = UART_CFG_FLOW_CTRL_NONE;
} else if (!strcmp(argv[2], "rtscts")) {
flow_control = UART_CFG_FLOW_CTRL_RTS_CTS;
} else if (!strcmp(argv[2], "dtrdsr")) {
flow_control = UART_CFG_FLOW_CTRL_DTR_DSR;
} else if (!strcmp(argv[2], "rs485")) {
flow_control = UART_CFG_FLOW_CTRL_RS485;
} else {
shell_error(sh, "Unknown: '%s'", argv[2]);
shell_help(sh);
return SHELL_CMD_HELP_PRINTED;
}

ret = uart_config_get(dev, &cfg);
if (ret < 0) {
shell_error(sh, "UART: Failed to get current configuration: %d", ret);
return ret;
}
cfg.flow_ctrl = flow_control;

ret = uart_configure(dev, &cfg);
if (ret < 0) {
shell_error(sh, "UART: Failed to configure device: %d", ret);
return ret;
}
return 0;
}

static void device_name_get(size_t idx, struct shell_static_entry *entry)
{
const struct device *dev = shell_device_filter(idx, device_is_uart);

entry->syntax = (dev != NULL) ? dev->name : NULL;
entry->handler = NULL;
entry->help = NULL;
entry->subcmd = NULL;
}

SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);

SHELL_STATIC_SUBCMD_SET_CREATE(sub_uart_cmds,
SHELL_CMD_ARG(baudrate, &dsub_device_name,
"Configure UART device baudrate\n"
"Usage: baudrate <device> <baudrate>",
cmd_uart_baudrate, 3, 0),
SHELL_CMD_ARG(fc, &dsub_device_name,
"Configure UART device flow control\n"
"Usage: fc <device> <none|rtscts|dtrdsr|rs485>",
cmd_uart_flow_control, 3, 0),
SHELL_SUBCMD_SET_END /* Array terminated. */
);

SHELL_CMD_REGISTER(uart, &sub_uart_cmds, "UART commands", NULL);

0 comments on commit 6ba0563

Please sign in to comment.