2018-05-16 14:50:33 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-09-14 20:48:54 +08:00
|
|
|
|
|
|
|
#include <logging/log_backend.h>
|
2018-05-16 14:50:33 +08:00
|
|
|
#include <logging/log_core.h>
|
|
|
|
#include <logging/log_msg.h>
|
|
|
|
#include <logging/log_output.h>
|
2020-09-21 21:29:02 +08:00
|
|
|
#include <logging/log_backend_std.h>
|
2018-05-16 14:50:33 +08:00
|
|
|
#include <device.h>
|
2019-06-26 03:54:01 +08:00
|
|
|
#include <drivers/uart.h>
|
2020-09-22 20:42:43 +08:00
|
|
|
#include <sys/__assert.h>
|
2018-05-16 14:50:33 +08:00
|
|
|
|
2020-07-09 20:54:24 +08:00
|
|
|
static const struct device *uart_dev;
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int char_out(uint8_t *data, size_t length, void *ctx)
|
2018-05-16 14:50:33 +08:00
|
|
|
{
|
2020-07-09 20:54:24 +08:00
|
|
|
ARG_UNUSED(ctx);
|
2018-05-16 14:50:33 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
2020-07-09 20:54:24 +08:00
|
|
|
uart_poll_out(uart_dev, data[i]);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
static uint8_t uart_output_buf;
|
2018-05-16 14:50:33 +08:00
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
LOG_OUTPUT_DEFINE(log_output_uart, char_out, &uart_output_buf, 1);
|
2018-05-16 14:50:33 +08:00
|
|
|
|
|
|
|
static void put(const struct log_backend *const backend,
|
|
|
|
struct log_msg *msg)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_UART_SYST_ENABLE) ?
|
2019-10-29 05:51:27 +08:00
|
|
|
LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
|
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
log_backend_std_put(&log_output_uart, flag, msg);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:38:23 +08:00
|
|
|
static void log_backend_uart_init(void)
|
2018-05-16 14:50:33 +08:00
|
|
|
{
|
2020-07-09 20:54:24 +08:00
|
|
|
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
2020-09-22 20:42:43 +08:00
|
|
|
__ASSERT_NO_MSG((void *)uart_dev);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void panic(struct log_backend const *const backend)
|
|
|
|
{
|
2020-07-24 05:39:34 +08:00
|
|
|
log_backend_std_panic(&log_output_uart);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static void dropped(const struct log_backend *const backend, uint32_t cnt)
|
2018-12-07 17:35:02 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(backend);
|
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
log_backend_std_dropped(&log_output_uart, cnt);
|
2018-12-07 17:35:02 +08:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:38:33 +08:00
|
|
|
static void sync_string(const struct log_backend *const backend,
|
2020-05-28 00:26:57 +08:00
|
|
|
struct log_msg_ids src_level, uint32_t timestamp,
|
2019-01-10 15:38:33 +08:00
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_UART_SYST_ENABLE) ?
|
2019-10-29 05:51:27 +08:00
|
|
|
LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
|
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
log_backend_std_sync_string(&log_output_uart, flag, src_level,
|
2019-07-17 16:29:52 +08:00
|
|
|
timestamp, fmt, ap);
|
2019-01-10 15:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_hexdump(const struct log_backend *const backend,
|
2020-05-28 00:26:57 +08:00
|
|
|
struct log_msg_ids src_level, uint32_t timestamp,
|
|
|
|
const char *metadata, const uint8_t *data, uint32_t length)
|
2019-01-10 15:38:33 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t flag = IS_ENABLED(CONFIG_LOG_BACKEND_UART_SYST_ENABLE) ?
|
2019-10-29 05:51:27 +08:00
|
|
|
LOG_OUTPUT_FLAG_FORMAT_SYST : 0;
|
|
|
|
|
2020-07-24 05:39:34 +08:00
|
|
|
log_backend_std_sync_hexdump(&log_output_uart, flag, src_level,
|
2019-07-17 16:29:52 +08:00
|
|
|
timestamp, metadata, data, length);
|
2019-01-10 15:38:33 +08:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:50:33 +08:00
|
|
|
const struct log_backend_api log_backend_uart_api = {
|
2019-01-10 15:38:33 +08:00
|
|
|
.put = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : put,
|
|
|
|
.put_sync_string = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ?
|
|
|
|
sync_string : NULL,
|
|
|
|
.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ?
|
|
|
|
sync_hexdump : NULL,
|
2018-05-16 14:50:33 +08:00
|
|
|
.panic = panic,
|
2018-09-14 20:31:26 +08:00
|
|
|
.init = log_backend_uart_init,
|
2019-01-10 15:38:33 +08:00
|
|
|
.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
|
2018-05-16 14:50:33 +08:00
|
|
|
};
|
2018-09-14 20:48:54 +08:00
|
|
|
|
2018-10-11 12:28:18 +08:00
|
|
|
LOG_BACKEND_DEFINE(log_backend_uart, log_backend_uart_api, true);
|