Skip to content

Commit

Permalink
Allow using Segger RTT as debug output (EdgeTX#3052)
Browse files Browse the repository at this point in the history
Firmware needs to be built with -DDEBUG=YES and -DDEBUG_SEGGER_RTT=YES to use this feature.
  • Loading branch information
dlktdr authored and mha1 committed Jan 27, 2023
1 parent 45de3e7 commit 369bfd2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
path = radio/src/thirdparty/FreeRTOS
url = https://github.com/FreeRTOS/FreeRTOS-Kernel.git
shallow = true
[submodule "radio/src/thirdparty/Segger_RTT"]
path = radio/src/thirdparty/Segger_RTT
url = https://github.com/adfernandes/segger-rtt.git
8 changes: 7 additions & 1 deletion radio/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ option(RAS "RAS (SWR) enabled" ON)
option(TEMPLATES "Model templates menu" OFF)
option(TRACE_SIMPGMSPACE "Turn on traces in simpgmspace.cpp" ON)
option(TRACE_LUA_INTERNALS "Turn on traces for Lua internals" OFF)
option(DEBUG_SEGGER_RTT "Debug output to Segger RTT" OFF)
option(DEBUG_WINDOWS "Turn on windows traces" OFF)
option(DEBUG_YAML "Turn on YAML traces" OFF)
option(DEBUG_LABELS "Turn on Labels traces" OFF)
Expand Down Expand Up @@ -365,6 +366,11 @@ if(TRACE_LUA_INTERNALS)
add_definitions(-DTRACE_LUA_INTERNALS_ENABLED)
endif()

if(DEBUG_SEGGER_RTT)
add_definitions(-DDEBUG_SEGGER_RTT)
set(SRC ${SRC} ${THIRDPARTY_DIR}/Segger_RTT/RTT/SEGGER_RTT.c)
endif()

if(DEBUG_WINDOWS)
add_definitions(-DDEBUG_WINDOWS)
endif()
Expand Down Expand Up @@ -520,7 +526,7 @@ if(NATIVE_BUILD)
target_compile_options(radiolib_native PUBLIC -DSIMU)
add_dependencies(radiolib_native ${RADIO_DEPENDENCIES})
set_property(TARGET radiolib_native PROPERTY POSITION_INDEPENDENT_CODE ON)

add_subdirectory(targets/simu)
add_subdirectory(tests)
endif()
Expand Down
4 changes: 2 additions & 2 deletions radio/src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void dumpTraceBuffer()
" notd", // INT_OTG_FS_RX_NOT_DEVICE,
#endif // #if defined(DEBUG_USB_INTERRUPTS)
};
#elif defined(PCBTARANIS)
#elif defined(PCBTARANIS)
const char * const interruptNames[INT_LAST] = {
"Tick ", // INT_TICK,
"5ms ", // INT_5MS,
Expand Down Expand Up @@ -201,7 +201,7 @@ void DebugTimer::stop()
else {
last *= 10000ul; //adjust unit to 1us
}
evalStats();
evalStats();
}

DebugTimer debugTimers[DEBUG_TIMERS_COUNT];
Expand Down
40 changes: 36 additions & 4 deletions radio/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
#include "extmodule_serial_driver.h"
#endif

#if defined(DEBUG_SEGGER_RTT)
#include "thirdparty/Segger_RTT/RTT/SEGGER_RTT.h"
#endif

#define PRINTF_BUFFER_SIZE 128

static void (*dbg_serial_putc)(void*, uint8_t) = nullptr;
Expand All @@ -67,6 +71,33 @@ void dbgSerialSetSendCb(void* ctx, void (*cb)(void*, uint8_t))
dbg_serial_putc = cb;
}

#if defined(DEBUG_SEGGER_RTT)

extern "C" void dbgSerialPutc(char c)
{
SEGGER_RTT_Write(0, (const void *)&c, 1);
}

extern "C" void dbgSerialPrintf(const char * format, ...)
{
va_list arglist;
char tmp[PRINTF_BUFFER_SIZE+1];

// no need to do anything if we don't have an output
if (!dbg_serial_putc) return;

va_start(arglist, format);
vsnprintf(tmp, PRINTF_BUFFER_SIZE, format, arglist);
tmp[PRINTF_BUFFER_SIZE] = '\0';
va_end(arglist);

const char *t = tmp;
while (*t && dbg_serial_putc) {
SEGGER_RTT_Write(0, (const void *)t++, 1);
}
}
#else

extern "C" void dbgSerialPutc(char c)
{
auto _putc = dbg_serial_putc;
Expand All @@ -81,7 +112,7 @@ extern "C" void dbgSerialPrintf(const char * format, ...)

// no need to do anything if we don't have an output
if (!dbg_serial_putc) return;

va_start(arglist, format);
vsnprintf(tmp, PRINTF_BUFFER_SIZE, format, arglist);
tmp[PRINTF_BUFFER_SIZE] = '\0';
Expand All @@ -92,6 +123,7 @@ extern "C" void dbgSerialPrintf(const char * format, ...)
dbg_serial_putc(dbg_serial_ctx, *t++);
}
}
#endif

extern "C" void dbgSerialCrlf()
{
Expand Down Expand Up @@ -157,7 +189,7 @@ static void serialSetCallBacks(int mode, void* ctx, const etx_serial_port_t* por
getByte = drv->getByte;
setRxCb = drv->setReceiveCb;
}
}
}

// prevent compiler warnings
(void)sendByte;
Expand Down Expand Up @@ -228,7 +260,7 @@ static void serialSetCallBacks(int mode, void* ctx, const etx_serial_port_t* por
extmoduleSetSerialPort(drv);
break;
#endif

#endif
}
}
Expand Down Expand Up @@ -392,7 +424,7 @@ void serialInit(uint8_t port_nr, int mode)
void initSerialPorts()
{
memset(serialPortStates, 0, sizeof(serialPortStates));

for (uint8_t port_nr = 0; port_nr < MAX_AUX_SERIAL; port_nr++) {
auto mode = getSerialPortMode(port_nr);
serialInit(port_nr, mode);
Expand Down
13 changes: 10 additions & 3 deletions radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ if(BLUETOOTH)
)
endif()

if(DEBUG_SEGGER_RTT)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../thirdparty/Segger_RTT/RTT/SEGGER_RTT.c
)
endif()


if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
set(BOOTLOADER_SRC
Expand All @@ -73,7 +80,7 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
foreach(LVGL_FILE ${LVGL_SRC_FILES_MINIMAL})
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} ../../../../../${LVGL_FILE})
endforeach()

if(USB_CHARGER)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
Expand All @@ -88,11 +95,11 @@ if(PCB STREQUAL X10 OR PCB STREQUAL X12S OR PCB STREQUAL NV14)
../../../../../targets/${TARGET_DIR}/extmodule_helper.cpp
)
endif()

if(DEBUG)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_usart.c
# ../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_usart.c
../../../../../serial.cpp
../../../../../targets/common/arm/stm32/aux_serial_driver.cpp
../../../../../targets/common/arm/stm32/stm32_usart_driver.cpp
Expand Down
12 changes: 10 additions & 2 deletions radio/src/targets/common/arm/stm32/bootloader/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include "keys.h"
#include "debug.h"

#if defined(DEBUG_SEGGER_RTT)
#include "thirdparty/Segger_RTT/RTT/SEGGER_RTT.h"
#endif

#if defined(PCBXLITE)
#define BOOTLOADER_KEYS 0x0F
#else
Expand Down Expand Up @@ -83,7 +87,7 @@ void interrupt10ms()
#if defined(DEBUG)
g_tmr10ms++;
#endif

uint8_t index = 0;
uint32_t in = readKeys();

Expand Down Expand Up @@ -228,6 +232,10 @@ void bootloaderInitApp()
boardBootloaderInit();
#endif

#if defined(DEBUG_SEGGER_RTT)
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
#endif

pwrInit();
keysInit();

Expand Down Expand Up @@ -267,7 +275,7 @@ void bootloaderInitApp()
#endif

delaysInit(); // needed for lcdInit()

#if defined(DEBUG)
initSerialPorts();
#endif
Expand Down
1 change: 1 addition & 0 deletions radio/src/thirdparty/Segger_RTT
Submodule Segger_RTT added at fdac9c

0 comments on commit 369bfd2

Please sign in to comment.