Skip to content

Commit

Permalink
[cli] support RTT as cli interface (openthread#9148)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CodingRays authored Aug 14, 2023
1 parent 68c0565 commit b03022b
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/platforms/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<TARGET_PROPERTY:ot-config,INTERFACE_COMPILE_DEFINITIONS>
)

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}
$<TARGET_PROPERTY:ot-config,INTERFACE_INCLUDE_DIRECTORIES>
Expand Down
136 changes: 136 additions & 0 deletions examples/platforms/utils/uart_rtt.c
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

#include <openthread-core-config.h>
#include <openthread/config.h>

#include <utils/code_utils.h>
#include <openthread/error.h>

#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
118 changes: 118 additions & 0 deletions examples/platforms/utils/uart_rtt.h
Original file line number Diff line number Diff line change
@@ -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 <openthread/config.h>

#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

0 comments on commit b03022b

Please sign in to comment.