Skip to content

Commit 527d8ef

Browse files
tokangasnordicjm
authored andcommitted
samples: cellular: modem_shell: Add command for changing UART baudrate
Added "uart baudrate" subcommand for changing the shell UART baudrate at runtime. Signed-off-by: Tommi Kangas <[email protected]>
1 parent 7187dd6 commit 527d8ef

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ Cellular samples
354354
* Printing of the last reset reason when the sample starts.
355355
* Support for printing the sample version information using the ``version`` command.
356356
* Support for counting pulses from a GPIO pin using the ``gpio_count`` command.
357+
* Support for changing shell UART baudrate using the ``uart baudrate`` command.
357358

358359
* Removed the nRF7002 EK devicetree overlay file :file:`nrf91xxdk_with_nrf7002ek.overlay`, because UART1 is disabled through the shield configuration.
359360

samples/cellular/modem_shell/README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ UART
788788

789789
MoSh command: ``uart``
790790

791-
Disable UARTs for power measurement purposes.
791+
Disable UARTs for power measurement purposes or change shell UART baudrate.
792792

793793
* Disable UARTs for 30 seconds:
794794

@@ -802,6 +802,12 @@ Disable UARTs for power measurement purposes.
802802
803803
uart during_sleep disable
804804
805+
* Change shell UART baudrate to 921600:
806+
807+
.. code-block:: console
808+
809+
uart baudrate 921600
810+
805811
----
806812

807813
Heap usage statistics

samples/cellular/modem_shell/src/uart/uart_shell.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <zephyr/pm/device.h>
1313
#include <zephyr/shell/shell.h>
1414
#include <zephyr/shell/shell_uart.h>
15+
#include <zephyr/drivers/uart.h>
1516
#include <modem/lte_lc.h>
1617
#include <modem/nrf_modem_lib_trace.h>
1718

@@ -155,6 +156,38 @@ static int cmd_uart_enable_when_sleep(void)
155156
return 0;
156157
}
157158

159+
static int cmd_uart_baudrate(const struct shell *shell, size_t argc, char **argv)
160+
{
161+
int err;
162+
int baudrate;
163+
struct uart_config cfg;
164+
165+
baudrate = atoi(argv[1]);
166+
if (baudrate < 0 || baudrate > 1000000) {
167+
mosh_error("Invalid baudrate: %d", baudrate);
168+
169+
return -EINVAL;
170+
}
171+
172+
err = uart_config_get(shell_uart_dev, &cfg);
173+
if (err) {
174+
mosh_error("Failed to read UART configuration, error: %d", err);
175+
176+
return -ENOEXEC;
177+
}
178+
179+
cfg.baudrate = baudrate;
180+
181+
err = uart_configure(shell_uart_dev, &cfg);
182+
if (err) {
183+
mosh_error("Failed to set UART configuration, error: %d", err);
184+
185+
return -ENOEXEC;
186+
}
187+
188+
return 0;
189+
}
190+
158191
SHELL_STATIC_SUBCMD_SET_CREATE(
159192
sub_uart_during_sleep,
160193
SHELL_CMD_ARG(enable, NULL, "Enable UARTs during sleep mode.",
@@ -179,11 +212,18 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_uart,
179212
"Disable UARTs during the modem sleep mode. UARTs are re-enabled once the modem "
180213
"exits sleep mode.",
181214
mosh_print_help_shell),
215+
SHELL_CMD_ARG(
216+
baudrate,
217+
NULL,
218+
"<baudrate>\nSet shell UART baudrate.",
219+
cmd_uart_baudrate,
220+
2,
221+
0),
182222
SHELL_SUBCMD_SET_END
183223
);
184224

185225
SHELL_CMD_REGISTER(
186226
uart,
187227
&sub_uart,
188-
"Commands for disabling UARTs for power measurement.",
228+
"Commands for disabling UARTs for power measurement and setting shell UART baudrate.",
189229
mosh_print_help_shell);

0 commit comments

Comments
 (0)