2023-04-21 22:34:19 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Oticon A/S
|
|
|
|
* Copyright (c) 2023 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <zephyr/init.h>
|
2023-05-23 23:04:00 +08:00
|
|
|
#include <zephyr/arch/posix/posix_trace.h>
|
2024-07-23 19:01:03 +08:00
|
|
|
#include <zephyr/sys/printk-hooks.h>
|
2023-04-21 22:34:19 +08:00
|
|
|
|
|
|
|
#define _STDOUT_BUF_SIZE 256
|
|
|
|
static char stdout_buff[_STDOUT_BUF_SIZE];
|
|
|
|
static int n_pend; /* Number of pending characters in buffer */
|
|
|
|
|
2023-05-23 23:04:00 +08:00
|
|
|
static int print_char(int c)
|
2023-04-21 22:34:19 +08:00
|
|
|
{
|
|
|
|
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) {
|
2023-05-23 23:04:00 +08:00
|
|
|
posix_print_trace("%s\n", stdout_buff);
|
2023-04-21 22:34:19 +08:00
|
|
|
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;
|
2023-05-23 23:04:00 +08:00
|
|
|
posix_print_trace("%s", stdout_buff);
|
2023-04-21 22:34:19 +08:00
|
|
|
n_pend = 0;
|
|
|
|
stdout_buff[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-23 23:04:00 +08:00
|
|
|
static int posix_arch_console_init(void)
|
2023-04-21 22:34:19 +08:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_PRINTK
|
2023-05-23 23:04:00 +08:00
|
|
|
__printk_hook_install(print_char);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_STDOUT_CONSOLE
|
|
|
|
extern void __stdout_hook_install(int (*fn)(int));
|
|
|
|
__stdout_hook_install(print_char);
|
2023-04-21 22:34:19 +08:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-23 23:04:00 +08:00
|
|
|
SYS_INIT(posix_arch_console_init, PRE_KERNEL_1,
|
|
|
|
CONFIG_POSIX_ARCH_CONSOLE_INIT_PRIORITY);
|