From b03022b2afc52597f030379093a723fbf5f850c3 Mon Sep 17 00:00:00 2001 From: Lorenzo Rai Date: Mon, 14 Aug 2023 18:05:15 +0200 Subject: [PATCH] [cli] support RTT as cli interface (#9148) This commit adds an RTT implementation for the cli uart api. This can be enabled using the `OT_RTT_UART` flag for cmake or `OPENTHREAD_ENABLE_RTT_UART` for make. --- examples/platforms/utils/CMakeLists.txt | 7 ++ examples/platforms/utils/uart_rtt.c | 136 ++++++++++++++++++++++++ examples/platforms/utils/uart_rtt.h | 118 ++++++++++++++++++++ 3 files changed, 261 insertions(+) create mode 100644 examples/platforms/utils/uart_rtt.c create mode 100644 examples/platforms/utils/uart_rtt.h diff --git a/examples/platforms/utils/CMakeLists.txt b/examples/platforms/utils/CMakeLists.txt index 3dcc67e46..3e4a680d0 100644 --- a/examples/platforms/utils/CMakeLists.txt +++ b/examples/platforms/utils/CMakeLists.txt @@ -34,12 +34,19 @@ add_library(openthread-platform-utils OBJECT otns_utils.cpp settings_ram.c soft_source_match_table.c + uart_rtt.c ) target_compile_definitions(openthread-platform-utils PRIVATE $ ) +if(OT_RTT_UART) + target_compile_definitions(openthread-platform-utils PRIVATE + OPENTHREAD_UART_RTT_ENABLE=1 + ) +endif() + target_include_directories(openthread-platform-utils PRIVATE ${OT_PUBLIC_INCLUDES} $ diff --git a/examples/platforms/utils/uart_rtt.c b/examples/platforms/utils/uart_rtt.c new file mode 100644 index 000000000..9b682aa71 --- /dev/null +++ b/examples/platforms/utils/uart_rtt.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2023, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file implements the RTT implementation of the uart API. + * + */ + +#include + +#include +#include + +#include +#include + +#include "SEGGER_RTT.h" +#include "uart.h" +#include "uart_rtt.h" + +#if OPENTHREAD_UART_RTT_ENABLE + +static bool sUartInitialized = false; +static bool sUartPendingUp = false; + +#if UART_RTT_BUFFER_INDEX != 0 +static uint8_t sUartUpBuffer[UART_RTT_UP_BUFFER_SIZE]; +static uint8_t sUartDownBuffer[UART_RTT_DOWN_BUFFER_SIZE]; +#endif + +otError otPlatUartEnable(void) +{ + otError error = OT_ERROR_FAILED; + +#if UART_RTT_BUFFER_INDEX != 0 + int resUp = SEGGER_RTT_ConfigUpBuffer(UART_RTT_BUFFER_INDEX, UART_RTT_BUFFER_NAME, sUartUpBuffer, + UART_RTT_UP_BUFFER_SIZE, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL); + int resDown = SEGGER_RTT_ConfigDownBuffer(UART_RTT_BUFFER_INDEX, UART_RTT_BUFFER_NAME, sUartDownBuffer, + UART_RTT_DOWN_BUFFER_SIZE, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL); +#else + int resUp = SEGGER_RTT_SetFlagsUpBuffer(UART_RTT_BUFFER_INDEX, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL); + int resDown = SEGGER_RTT_SetFlagsDownBuffer(UART_RTT_BUFFER_INDEX, SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL); +#endif + + otEXPECT(resUp >= 0 && resDown >= 0); + + sUartInitialized = true; + sUartPendingUp = false; + error = OT_ERROR_NONE; + +exit: + return error; +} + +otError otPlatUartDisable(void) +{ + sUartInitialized = false; + + return OT_ERROR_NONE; +} + +otError otPlatUartSend(const uint8_t *aBuf, uint16_t aBufLength) +{ + otError error = OT_ERROR_NONE; + + otEXPECT_ACTION(SEGGER_RTT_Write(UART_RTT_BUFFER_INDEX, aBuf, aBufLength) != 0, error = OT_ERROR_FAILED); + sUartPendingUp = true; + +exit: + return error; +} + +otError otPlatUartFlush(void) +{ + otError error = OT_ERROR_NONE; + + otEXPECT_ACTION(sUartPendingUp, error = OT_ERROR_INVALID_STATE); + + while (SEGGER_RTT_HasDataUp(UART_RTT_BUFFER_INDEX) != 0) + { + } + +exit: + return error; +} + +void utilsUartRttProcess(void) +{ + uint8_t buf[UART_RTT_READ_BUFFER_SIZE]; + unsigned count; + + otEXPECT(sUartInitialized); + + if (sUartPendingUp && SEGGER_RTT_HasDataUp(UART_RTT_BUFFER_INDEX) == 0) + { + sUartPendingUp = false; + otPlatUartSendDone(); + } + + count = SEGGER_RTT_Read(UART_RTT_BUFFER_INDEX, &buf, sizeof(buf)); + if (count > 0) + { + otPlatUartReceived((const uint8_t *)&buf, count); + } + +exit: + return; +} + +#endif // OPENTHREAD_UART_RTT_ENABLE diff --git a/examples/platforms/utils/uart_rtt.h b/examples/platforms/utils/uart_rtt.h new file mode 100644 index 000000000..b1529f59f --- /dev/null +++ b/examples/platforms/utils/uart_rtt.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2023, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file defines the RTT implementation of the uart API and default constants used by uart_rtt.c. + * + */ + +#ifndef UTILS_UART_RTT_H +#define UTILS_UART_RTT_H + +#include "openthread-core-config.h" +#include + +#include "logging_rtt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @def UART_RTT_BUFFER_INDEX + * + * RTT buffer index used for the uart. + * + */ +#ifndef UART_RTT_BUFFER_INDEX +#define UART_RTT_BUFFER_INDEX 1 +#endif + +#if OPENTHREAD_UART_RTT_ENABLE && (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) && \ + (LOG_RTT_BUFFER_INDEX == UART_RTT_BUFFER_INDEX) +#error "Log buffer index matches uart buffer index" +#endif + +/** + * @def UART_RTT_BUFFER_NAME + * + * RTT name used for the uart. Only used if UART_RTT_BUFFER_INDEX is not 0. + * Otherwise, the buffer name is fixed to "Terminal". + * + */ +#ifndef UART_RTT_BUFFER_NAME +#define UART_RTT_BUFFER_NAME "Terminal" +#endif + +/** + * @def UART_RTT_UP_BUFFER_SIZE + * + * RTT up buffer size used for the uart. Only used if UART_RTT_BUFFER_INDEX + * is not 0. To configure buffer #0 size, check the BUFFER_SIZE_UP definition + * in SEGGER_RTT_Conf.h + * + */ +#ifndef UART_RTT_UP_BUFFER_SIZE +#define UART_RTT_UP_BUFFER_SIZE 256 +#endif + +/** + * @def UART_RTT_DOWN_BUFFER_SIZE + * + * RTT down buffer size used for the uart. Only used if UART_RTT_BUFFER_INDEX + * is not 0. To configure buffer #0 size, check the BUFFER_SIZE_DOWN definition + * in SEGGER_RTT_Conf.h + * + */ +#ifndef UART_RTT_DOWN_BUFFER_SIZE +#define UART_RTT_DOWN_BUFFER_SIZE 16 +#endif + +/** + * @def UART_RTT_READ_BUFFER_SIZE + * + * Size of the temporary buffer used when reading from the RTT channel. It will be + * locally allocated on the stack. + * + */ +#ifndef UART_RTT_READ_BUFFER_SIZE +#define UART_RTT_READ_BUFFER_SIZE 16 +#endif + +/** + * Updates the rtt uart. Must be called frequently to process receive and send done. + * + */ +void utilsUartRttProcess(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // UTILS_UART_RTT_H