[efr32] add support for RTT log (#3285)

This commit is contained in:
Zhanglong Xia
2018-12-14 09:25:41 -08:00
committed by Jonathan Hui
parent ff0b30db55
commit 4279dbc14c
30 changed files with 2982 additions and 640 deletions
+7 -4
View File
@@ -30,20 +30,23 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
lib_LIBRARIES = libopenthread-platform-utils.a
libopenthread_platform_utils_a_CPPFLAGS = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/examples/platforms \
-I$(top_srcdir)/src/core \
libopenthread_platform_utils_a_CPPFLAGS = \
-I$(top_srcdir)/include \
-I$(top_srcdir)/examples/platforms \
-I$(top_srcdir)/src/core \
-I$(top_srcdir)/third_party/jlink/SEGGER_RTT_V640/RTT \
$(NULL)
libopenthread_platform_utils_a_SOURCES = \
debug_uart.c \
logging_rtt.c \
settings_flash.c \
$(NULL)
noinst_HEADERS = \
code_utils.h \
flash.h \
logging_rtt.h \
$(NULL)
include $(abs_top_nlbuild_autotools_dir)/automake/post.am
+172
View File
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2018, 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 OpenThread platform abstraction for logging.
*
*/
#include <openthread-core-config.h>
#include <openthread/config.h>
#include <utils/code_utils.h>
#include <openthread/platform/alarm-milli.h>
#include <openthread/platform/logging.h>
#include "SEGGER_RTT.h"
#include "logging_rtt.h"
#if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) || \
(OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL)
#if (LOG_RTT_COLOR_ENABLE == 1)
#define RTT_COLOR_CODE_DEFAULT "\x1B[0m"
#define RTT_COLOR_CODE_RED "\x1B[1;31m"
#define RTT_COLOR_CODE_GREEN "\x1B[1;32m"
#define RTT_COLOR_CODE_YELLOW "\x1B[1;33m"
#define RTT_COLOR_CODE_CYAN "\x1B[1;36m"
#else // LOG_RTT_COLOR_ENABLE == 1
#define RTT_COLOR_CODE_DEFAULT ""
#define RTT_COLOR_CODE_RED ""
#define RTT_COLOR_CODE_GREEN ""
#define RTT_COLOR_CODE_YELLOW ""
#define RTT_COLOR_CODE_CYAN ""
#endif // LOG_RTT_COLOR_ENABLE == 1
static bool sLogInitialized = false;
static uint8_t sLogBuffer[LOG_RTT_BUFFER_SIZE];
/**
* Function for getting color of a given level log.
*
* @param[in] aLogLevel The log level.
*
* @returns String with a log level color value.
*/
static inline const char *levelToString(otLogLevel aLogLevel)
{
switch (aLogLevel)
{
case OT_LOG_LEVEL_CRIT:
return RTT_COLOR_CODE_RED;
case OT_LOG_LEVEL_WARN:
return RTT_COLOR_CODE_YELLOW;
case OT_LOG_LEVEL_INFO:
return RTT_COLOR_CODE_GREEN;
case OT_LOG_LEVEL_DEBG:
default:
return RTT_COLOR_CODE_DEFAULT;
}
}
#if (LOG_TIMESTAMP_ENABLE == 1)
/**
* Function for printing actual timestamp.
*
* @param[inout] aLogString Pointer to the log buffer.
* @param[in] aMaxSize Maximum size of the log buffer.
*
* @returns Number of bytes successfully written to the log buffer.
*/
static inline int logTimestamp(char *aLogString, uint16_t aMaxSize)
{
long unsigned int now = otPlatAlarmMilliGetNow();
return snprintf(aLogString, (size_t)aMaxSize, "%s[%010lu]", RTT_COLOR_CODE_CYAN, now);
}
#endif
/**
* Function for printing log level.
*
* @param[inout] aLogString Pointer to log buffer.
* @param[in] aMaxSize Maximum size of log buffer.
* @param[in] aLogLevel Log level.
*
* @returns Number of bytes successfully written to the log buffer.
*/
static inline int logLevel(char *aLogString, uint16_t aMaxSize, otLogLevel aLogLevel)
{
return snprintf(aLogString, (size_t)aMaxSize, "%s ", levelToString(aLogLevel));
}
void utilsLogRttInit(void)
{
int res = SEGGER_RTT_ConfigUpBuffer(LOG_RTT_BUFFER_INDEX, LOG_RTT_BUFFER_NAME, sLogBuffer, LOG_RTT_BUFFER_SIZE,
SEGGER_RTT_MODE_NO_BLOCK_TRIM);
otEXPECT(res >= 0);
sLogInitialized = true;
exit:
return;
}
void utilsLogRttDeinit(void)
{
sLogInitialized = false;
}
void utilsLogRttOutput(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list ap)
{
(void)aLogRegion;
uint16_t length = 0;
int charsWritten;
char logString[LOG_PARSE_BUFFER_SIZE + 1];
otEXPECT(sLogInitialized == true);
#if (LOG_TIMESTAMP_ENABLE == 1)
length += logTimestamp(logString, LOG_PARSE_BUFFER_SIZE);
#endif
// Add level information.
length += logLevel(&logString[length], (LOG_PARSE_BUFFER_SIZE - length), aLogLevel);
charsWritten = vsnprintf(&logString[length], (size_t)(LOG_PARSE_BUFFER_SIZE - length), aFormat, ap);
otEXPECT(charsWritten >= 0);
length += charsWritten;
if (length > LOG_PARSE_BUFFER_SIZE)
{
length = LOG_PARSE_BUFFER_SIZE;
}
logString[length++] = '\n';
// Write user log to the RTT memory block.
SEGGER_RTT_WriteNoLock(0, logString, length);
exit:
return;
}
#endif // (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED) ||
// (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_NCP_SPINEL)
+137
View File
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2018, 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
* @brief
* This file defines the logging rtt interfaces and default constants used by logging_rtt.c.
*/
#ifndef UTILS_LOGGING_RTT_H
#define UTILS_LOGGING_RTT_H
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "openthread-core-config.h"
#include <openthread/config.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @def LOG_RTT_BUFFER_INDEX
*
* RTT's buffer index.
*
*/
#ifndef LOG_RTT_BUFFER_INDEX
#define LOG_RTT_BUFFER_INDEX 0
#endif
/**
* @def LOG_RTT_BUFFER_NAME
*
* RTT's name.
*
*/
#ifndef LOG_RTT_BUFFER_NAME
#define LOG_RTT_BUFFER_NAME "Terminal"
#endif
/**
* @def LOG_RTT_BUFFER_SIZE
*
* LOG RTT's buffer size.
*
*/
#ifndef LOG_RTT_BUFFER_SIZE
#define LOG_RTT_BUFFER_SIZE 256
#endif
/**
* @def LOG_RTT_COLOR_ENABLE
*
* Enable colors on RTT Viewer.
*
*/
#ifndef LOG_RTT_COLOR_ENABLE
#define LOG_RTT_COLOR_ENABLE 1
#endif
/**
* @def LOG_PARSE_BUFFER_SIZE
*
* LOG buffer used to parse print format. It will be locally allocated on the
* stack.
*
*/
#ifndef LOG_PARSE_BUFFER_SIZE
#define LOG_PARSE_BUFFER_SIZE 128
#endif
/**
* @def LOG_TIMESTAMP_ENABLE
*
* Enable timestamp in the logs.
*
*/
#ifndef LOG_TIMESTAMP_ENABLE
#define LOG_TIMESTAMP_ENABLE 1
#endif
/**
* Initialization of Logger driver.
*
*/
void utilsLogRttInit(void);
/**
* Deinitialization of Logger driver.
*
*/
void utilsLogRttDeinit(void);
/**
* This function outputs logs to SEGGER RTT.
*
* @param[in] aLogLevel The log level.
* @param[in] aLogRegion The log region.
* @param[in] aFormat A pointer to the format string.
* @param[in] ap va_list matching information for aFormat
*
*/
void utilsLogRttOutput(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list ap);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // UTILS_LOGGING_RTT_H