nrf52_bsim: drivers/console: Refactor printk backend

Fix a very old cmake warning for the nrf52_bsim due to the
console library being empty.
The printk backend was originally provided by the board
code for simplicity, but this left the console library
empty when building for this board.
Instead refactor that code to where all printk backends are.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2023-04-21 16:34:19 +02:00 committed by Carles Cufí
commit 157596acf4
7 changed files with 105 additions and 69 deletions

View file

@ -8,14 +8,6 @@ if BOARD_NRF52_BSIM
# must be read also from here.
source "soc/arm/nordic_nrf/Kconfig.peripherals"
comment "NRF52_BSIM options"
config PRINTK_HOOK_INIT_PRIORITY
int
default 50
help
Just the driver init priority
endif # BOARD_NRF52_BSIM

View file

@ -7,7 +7,6 @@ config BOARD_NRF52_BSIM
select SOC_COMPATIBLE_NRF
select SOC_COMPATIBLE_NRF52X
select SOC_COMPATIBLE_NRF52833
select CONSOLE_HAS_DRIVER
select NRF_RTC_TIMER
select CLOCK_CONTROL
select HAS_NRFX

View file

@ -41,4 +41,14 @@ endchoice
endif # LOG
if CONSOLE
config BSIM_CONSOLE
default y if !SERIAL
config UART_CONSOLE
default y if SERIAL
endif # CONSOLE
endif # BOARD_NRF52_BSIM

View file

@ -3,72 +3,17 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <zephyr/init.h>
#include "bs_tracing.h"
#include "posix_board_if.h"
#define _STDOUT_BUF_SIZE 256
static char stdout_buff[_STDOUT_BUF_SIZE];
static int n_pend; /* Number of pending characters in buffer */
int print_char(int c)
{
int printnow = 0;
if ((c != '\n') && (c != '\r')) {
stdout_buff[n_pend++] = c;
stdout_buff[n_pend] = 0;
} else {
printnow = 1;
}
if (n_pend >= _STDOUT_BUF_SIZE - 1) {
printnow = 1;
}
if (printnow) {
bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0,
"%s\n", stdout_buff);
n_pend = 0;
stdout_buff[0] = 0;
}
return c;
}
/**
* Flush to the terminal any possible pending printk
*/
void posix_flush_stdout(void)
{
if (n_pend) {
stdout_buff[n_pend] = 0;
bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0,
"%s", stdout_buff);
n_pend = 0;
stdout_buff[0] = 0;
fflush(stdout);
}
}
/*
* @brief Initialize the driver that provides the printk output
*
* @return 0 if successful, otherwise failed.
* Provide the posix_print_* functions required from all POSIX arch boards
* (This functions provide a lower level, more direct, print mechanism than
* Zephyr's printk or logger and therefore can be relied on even if the kernel
* is down.
*/
static int printk_init(void)
{
extern void __printk_hook_install(int (*fn)(int));
__printk_hook_install(print_char);
return 0;
}
SYS_INIT(printk_init, PRE_KERNEL_1, CONFIG_PRINTK_HOOK_INIT_PRIORITY);
void posix_print_error_and_exit(const char *format, ...)
{
@ -101,7 +46,6 @@ void posix_print_trace(const char *format, ...)
va_end(variable_argsp);
}
int posix_trace_over_tty(int file_number)
{
return bs_trace_is_tty(file_number);

View file

@ -1,6 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
if (${CONFIG_BSIM_CONSOLE})
zephyr_library_sources(bsim_console.c)
zephyr_library_include_directories($ENV{BSIM_COMPONENTS_PATH}/libUtilv1/src/)
endif()
zephyr_library_sources_ifdef(CONFIG_GSM_MUX gsm_mux.c)
zephyr_library_sources_ifdef(CONFIG_IPM_CONSOLE_RECEIVER ipm_console_receiver.c)
zephyr_library_sources_ifdef(CONFIG_IPM_CONSOLE_SENDER ipm_console_sender.c)

View file

@ -227,6 +227,20 @@ config XTENSA_SIM_CONSOLE
help
Use simulator console to print messages.
config BSIM_CONSOLE
bool "Use the BabbleSim tracing system for console output"
depends on ARCH_POSIX
select CONSOLE_HAS_DRIVER
help
Use the BabbleSim tracing system for the Zephyr console.
config BSIM_CONSOLE_INIT_PRIORITY
int "Init priority"
default 99
depends on BSIM_CONSOLE
help
Device driver initialization priority.
config NATIVE_POSIX_CONSOLE
bool "Use the host terminal for console"
depends on ARCH_POSIX

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2018 Oticon A/S
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/init.h>
#include "bs_tracing.h"
#include "posix_board_if.h"
#define _STDOUT_BUF_SIZE 256
static char stdout_buff[_STDOUT_BUF_SIZE];
static int n_pend; /* Number of pending characters in buffer */
int bsim_print_char(int c)
{
int printnow = 0;
if ((c != '\n') && (c != '\r')) {
stdout_buff[n_pend++] = c;
stdout_buff[n_pend] = 0;
} else {
printnow = 1;
}
if (n_pend >= _STDOUT_BUF_SIZE - 1) {
printnow = 1;
}
if (printnow) {
bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0,
"%s\n", stdout_buff);
n_pend = 0;
stdout_buff[0] = 0;
}
return c;
}
/**
* Ensure that whatever was written thru printk is displayed now
*/
void posix_flush_stdout(void)
{
if (n_pend) {
stdout_buff[n_pend] = 0;
bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0,
"%s", stdout_buff);
n_pend = 0;
stdout_buff[0] = 0;
fflush(stdout);
}
}
/*
* @brief Initialize the driver that provides the printk output
*
* @return 0 if successful, otherwise failed.
*/
static int bsim_console_init(void)
{
#ifdef CONFIG_PRINTK
extern void __printk_hook_install(int (*fn)(int));
__printk_hook_install(bsim_print_char);
#endif
return 0;
}
SYS_INIT(bsim_console_init, PRE_KERNEL_1, CONFIG_BSIM_CONSOLE_INIT_PRIORITY);