Console echo

Browse source code on GitHub

Overview

This example shows how the console_getchar() and console_putchar() functions can be used to echo an input character back to the console.

Requirements

UART console is required to run this sample.

Building and Running

Build and flash the sample as follows, changing nucleo_f401re for your board. Alternatively you can run this using QEMU, as described in console_getchar().

west build -b nucleo_f401re samples/subsys/console/echo
west flash

Following the initial prompt given by the firmware, start pressing keys on a keyboard, and they will be echoed back to the terminal program you are using.

Running on QEMU

On QEMU you can use the VIRTIO console device. It is already configured in Zephyr. To use it, run:

west build -b qemu_x86_64 samples/subsys/console/echo -- -DDTC_OVERLAY_FILE=virtconsole.overlay -DEXTRA_CONF_FILE=overlay-virtconsole.conf
west build -t run

It is possible to use multiple inputs/outputs at once by configuring them in the overlay-virtconsole.conf file.

  • Add CONFIG_UART_VIRTIO_CONSOLE_F_MULTIPORT=y to enable the multiport feature.

  • Configure more ports using the CONFIG_QEMU_EXTRA_FLAGS option. Refer to the QEMU manual for details.

Sample Output

You should see another line with instructions below. If not,
the (interrupt-driven) console device doesn't work as expected:
Start typing characters to see them echoed back

See also

Console API