Skip to content

Commit 8fd2ecb

Browse files
samples: shell: introduce threadless shell sample
Introduce threadless shell sample which shows how to build and use shells in threadless (single threaded) applications. Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
1 parent 2e32820 commit 8fd2ecb

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(shell_threadless)
8+
9+
target_sources(app PRIVATE src/main.c)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.. zephyr:code-sample:: threadless_shell
2+
:name: Threadless shell
3+
4+
Run shell in single threaded build
5+
6+
Overview
7+
********
8+
9+
Demonstrate how to configure and use a shell in a single threaded build
10+
(:kconfig:option:`CONFIG_MULTITHREADING` disabled). The sample uses the
11+
UART shell backend using either the async
12+
(:kconfig:option:`CONFIG_UART_ASYNC_API`) or interrupt driven
13+
(:kconfig:option:`CONFIG_UART_INTERRUPT_DRIVEN`) APIs. The sample
14+
implements a single command, which simply echoes back the single
15+
argument passed to it, along with the built-in "help" command.
16+
17+
.. code-block:: console
18+
19+
uart:~$ sample foobar
20+
foobar
21+
uart:~$
22+
23+
Building and Running
24+
********************
25+
26+
This application can be built and executed as follows:
27+
28+
.. zephyr-app-commands::
29+
:zephyr-app: samples/subsys/shell/threadless
30+
:host-os: unix
31+
:board: nrf54l15dk/nrf54l15/cpuapp
32+
:goals: run
33+
:compact:
34+
35+
To build for another board, change "nrf54l15dk/nrf54l15/cpuapp" above
36+
to that board's name.
37+
38+
Sample Output
39+
*************
40+
41+
.. code-block:: console
42+
43+
uart:~$
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_MULTITHREADING=n
2+
CONFIG_SHELL=y
3+
CONFIG_SHELL_MINIMAL=y
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sample:
2+
description: Threadless shell using uart backend
3+
name: Threadless shell
4+
tests:
5+
sample.subsys.shell.threadless:
6+
tags: threadless
7+
min_ram: 2
8+
min_flash: 32
9+
filter: CONFIG_ARCH_HAS_SINGLE_THREAD_SUPPORT and
10+
dt_chosen_enabled("zephyr,shell-uart")
11+
harness: shell
12+
harness_config:
13+
shell_commands:
14+
- command: "sample foobar"
15+
expected: "foobar"
16+
platform_exclude:
17+
- qemu_cortex_a53
18+
- qemu_cortex_a53/qemu_cortex_a53/smp
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/shell/shell.h>
8+
#include <zephyr/shell/shell_uart.h>
9+
10+
static int sample_sh_echo(const struct shell *sh, size_t argc, char **argv)
11+
{
12+
shell_print(sh, "%s", argv[1]);
13+
return 0;
14+
}
15+
16+
SHELL_CMD_ARG_REGISTER(sample, NULL, "Echo", sample_sh_echo, 2, 2);
17+
18+
int main(void)
19+
{
20+
/*
21+
* Start the shell. This prints the shell prompt and enables
22+
* input reception.
23+
*/
24+
shell_start(shell_backend_uart_get_ptr());
25+
26+
while (1) {
27+
/* Check for new input and process it if present */
28+
shell_process(shell_backend_uart_get_ptr());
29+
}
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)