2018-10-19 15:37:47 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Makaio GmbH
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <shell/shell_rtt.h>
|
|
|
|
#include <init.h>
|
2018-12-05 17:28:48 +08:00
|
|
|
#include <SEGGER_RTT.h>
|
2018-11-28 21:06:40 +08:00
|
|
|
#include <logging/log.h>
|
2018-10-19 15:37:47 +08:00
|
|
|
|
2019-02-11 22:25:53 +08:00
|
|
|
BUILD_ASSERT_MSG(!(IS_ENABLED(CONFIG_LOG_BACKEND_RTT) &&
|
|
|
|
COND_CODE_0(CONFIG_LOG_BACKEND_RTT_BUFFER, (1), (0))),
|
|
|
|
"Conflicting log RTT backend enabled on the same channel");
|
|
|
|
|
2018-10-19 15:37:47 +08:00
|
|
|
SHELL_RTT_DEFINE(shell_transport_rtt);
|
2019-03-22 23:22:24 +08:00
|
|
|
SHELL_DEFINE(shell_rtt, CONFIG_SHELL_PROMPT_RTT, &shell_transport_rtt,
|
2018-12-17 17:43:58 +08:00
|
|
|
CONFIG_SHELL_BACKEND_RTT_LOG_MESSAGE_QUEUE_SIZE,
|
|
|
|
CONFIG_SHELL_BACKEND_RTT_LOG_MESSAGE_QUEUE_TIMEOUT,
|
2018-10-19 15:37:47 +08:00
|
|
|
SHELL_FLAG_OLF_CRLF);
|
|
|
|
|
2018-11-28 21:06:40 +08:00
|
|
|
LOG_MODULE_REGISTER(shell_rtt, CONFIG_SHELL_RTT_LOG_LEVEL);
|
2018-10-19 15:37:47 +08:00
|
|
|
|
2019-02-11 22:25:53 +08:00
|
|
|
static bool rtt_blocking;
|
|
|
|
|
2018-11-28 21:06:40 +08:00
|
|
|
static void timer_handler(struct k_timer *timer)
|
2018-10-19 15:37:47 +08:00
|
|
|
{
|
2018-11-28 21:06:40 +08:00
|
|
|
const struct shell_rtt *sh_rtt = k_timer_user_data_get(timer);
|
2018-10-19 15:37:47 +08:00
|
|
|
|
2018-11-28 21:06:40 +08:00
|
|
|
if (SEGGER_RTT_HasData(0)) {
|
|
|
|
sh_rtt->handler(SHELL_TRANSPORT_EVT_RX_RDY, sh_rtt->context);
|
2018-10-19 15:37:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init(const struct shell_transport *transport,
|
|
|
|
const void *config,
|
|
|
|
shell_transport_handler_t evt_handler,
|
|
|
|
void *context)
|
|
|
|
{
|
|
|
|
struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
|
|
|
|
|
|
|
|
sh_rtt->handler = evt_handler;
|
|
|
|
sh_rtt->context = context;
|
|
|
|
|
2018-11-28 21:06:40 +08:00
|
|
|
k_timer_init(&sh_rtt->timer, timer_handler, NULL);
|
|
|
|
k_timer_user_data_set(&sh_rtt->timer, (void *)sh_rtt);
|
|
|
|
k_timer_start(&sh_rtt->timer, CONFIG_SHELL_RTT_RX_POLL_PERIOD,
|
|
|
|
CONFIG_SHELL_RTT_RX_POLL_PERIOD);
|
2018-10-19 15:37:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int uninit(const struct shell_transport *transport)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int enable(const struct shell_transport *transport, bool blocking)
|
|
|
|
{
|
2018-11-28 21:06:40 +08:00
|
|
|
struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
|
|
|
|
|
|
|
|
if (blocking) {
|
2019-02-11 22:25:53 +08:00
|
|
|
rtt_blocking = true;
|
2018-11-28 21:06:40 +08:00
|
|
|
k_timer_stop(&sh_rtt->timer);
|
|
|
|
}
|
|
|
|
|
2018-10-19 15:37:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write(const struct shell_transport *transport,
|
|
|
|
const void *data, size_t length, size_t *cnt)
|
|
|
|
{
|
|
|
|
struct shell_rtt *sh_rtt = (struct shell_rtt *)transport->ctx;
|
|
|
|
const u8_t *data8 = (const u8_t *)data;
|
|
|
|
|
2019-02-11 22:25:53 +08:00
|
|
|
if (rtt_blocking) {
|
|
|
|
*cnt = SEGGER_RTT_WriteNoLock(0, data8, length);
|
|
|
|
while (SEGGER_RTT_HasDataUp(0)) {
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*cnt = SEGGER_RTT_Write(0, data8, length);
|
|
|
|
}
|
2018-10-19 15:37:47 +08:00
|
|
|
|
|
|
|
sh_rtt->handler(SHELL_TRANSPORT_EVT_TX_RDY, sh_rtt->context);
|
2018-11-28 21:06:40 +08:00
|
|
|
|
2018-10-19 15:37:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read(const struct shell_transport *transport,
|
|
|
|
void *data, size_t length, size_t *cnt)
|
|
|
|
{
|
2018-11-28 21:06:40 +08:00
|
|
|
*cnt = SEGGER_RTT_Read(0, data, length);
|
2018-10-19 15:37:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct shell_transport_api shell_rtt_transport_api = {
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.enable = enable,
|
|
|
|
.write = write,
|
|
|
|
.read = read
|
|
|
|
};
|
|
|
|
|
|
|
|
static int enable_shell_rtt(struct device *arg)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
2018-11-28 21:06:40 +08:00
|
|
|
bool log_backend = CONFIG_SHELL_RTT_INIT_LOG_LEVEL > 0;
|
|
|
|
u32_t level = (CONFIG_SHELL_RTT_INIT_LOG_LEVEL > LOG_LEVEL_DBG) ?
|
|
|
|
CONFIG_LOG_MAX_LEVEL : CONFIG_SHELL_RTT_INIT_LOG_LEVEL;
|
2018-10-19 15:37:47 +08:00
|
|
|
|
2018-11-19 20:24:25 +08:00
|
|
|
shell_init(&shell_rtt, NULL, true, log_backend, level);
|
2018-10-19 15:37:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Function is used for testing purposes */
|
|
|
|
const struct shell *shell_backend_rtt_get_ptr(void)
|
|
|
|
{
|
2018-11-19 20:24:25 +08:00
|
|
|
return &shell_rtt;
|
2018-10-19 15:37:47 +08:00
|
|
|
}
|
|
|
|
SYS_INIT(enable_shell_rtt, POST_KERNEL, 0);
|