diff --git a/CODEOWNERS b/CODEOWNERS index 0da97fc1665..456cbf32bd4 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -139,6 +139,7 @@ /drivers/can/*mcp2515* @karstenkoenig /drivers/clock_control/*nrf* @nordic-krch /drivers/counter/ @nordic-krch +/drivers/console/semihost_console.c @luozhongyao /drivers/counter/counter_cmos.c @andrewboie /drivers/counter/maxim_ds3231.c @pabigot /drivers/crypto/*nrf_ecb* @maciekfabia @anangl diff --git a/drivers/console/CMakeLists.txt b/drivers/console/CMakeLists.txt index dfd83311338..ddb56527c12 100644 --- a/drivers/console/CMakeLists.txt +++ b/drivers/console/CMakeLists.txt @@ -11,3 +11,4 @@ zephyr_sources_if_kconfig(xtensa_sim_console.c) zephyr_sources_if_kconfig(native_posix_console.c) zephyr_sources_if_kconfig(uart_mux.c) zephyr_sources_if_kconfig(gsm_mux.c) +zephyr_sources_if_kconfig(semihost_console.c) diff --git a/drivers/console/Kconfig b/drivers/console/Kconfig index 33a907f21c5..06032547662 100644 --- a/drivers/console/Kconfig +++ b/drivers/console/Kconfig @@ -291,6 +291,20 @@ config NATIVE_POSIX_CONSOLE_INIT_PRIORITY help Device driver initialization priority. +config SEMIHOST_CONSOLE + bool "Use semihosting for console" + select CONSOLE_HAS_DRIVER + depends on CPU_CORTEX_M + help + Enable this option to use semihosting for console. + Semihosting is a mechanism that enables code running on an ARM target + to communicate and use the Input/Output facilities on a host computer + that is running a debugger. + Additional information can be found in: + https://developer.arm.com/docs/dui0471/k/what-is-semihosting/what-is-semihosting + This option is compatible with hardware and with QEMU, through the + (automatic) use of the -semihosting-config switch when invoking it. + module = UART_CONSOLE module-str = UART console source "subsys/logging/Kconfig.template.log_config" diff --git a/drivers/console/semihost_console.c b/drivers/console/semihost_console.c new file mode 100644 index 00000000000..fbe276cdbfa --- /dev/null +++ b/drivers/console/semihost_console.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 LuoZhongYao + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +extern void __printk_hook_install(int (*fn)(int)); +extern void __stdout_hook_install(int (*fn)(int)); + +static int semihost_console_out(int ch) +{ + static unsigned char c; + + c = ch; + __asm__ __volatile__ ( + "movs r1, %0\n" + "movs r0, #3\n" + "bkpt 0xab\n" + : + : "r" (&c) + : "r0", "r1"); + return ch; +} + +static int semihost_console_init(struct device *dev) +{ + ARG_UNUSED(dev); + + __printk_hook_install(semihost_console_out); + __stdout_hook_install(semihost_console_out); + + return 0; +} + +SYS_INIT(semihost_console_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);