Embedded Development Board Learning


Visual Studio Code for ARM with CMake #8 SEGGER RTT

Fecha: marzo 13, 2023

Autor: Guillermo Garcia

Categorías: Visual Studio Code Etiquetas:

Visual Studio Code como entorno de desarrollo para proyecto Embedded Systems

In the previous article, Visual Studio Code for ARM with CMake #7 Cortex Debug, we configured the debugging setup for the NUCLEO-G071RB board. In this article, we will add the SEGGER RTT library to use in our debugging process.

With the firmware conversion of our ST-Link debugger, we can now use exclusive features of the J-Link debugger, one of which is RTT.

Real-time transfer

J-Link SEGGER RTT uses the background memory access feature on the Debug Access Port (DAP) on Cortex-M and RX MCUs to communicate between the MCU and the host PC application, via J-Link probes.

RTT can be used on any ARM MCU from the SWO interface, the downside is that it is only compatible with J-Link debuggers.

We won’t go into details about how RTT works for that there are very good articles like this one.

RTT Library

We will add a series of source files necessary to use RTT, no changes need to be made to the source files, these files are on Github.

We add the necessary files to the project in the Lib folder.

SEGGER RTT SDK Files

Add to Compilation

It is necessary to add these files to the compilation, for this we go to our CMakeLists.txt file.

We will create a variable called SEGGER_RTT_SRCS where the path of the directory where the source files are is configured.

set( SEGGER_RTT_SRCS ${CMAKE_SOURCE_DIR}/Lib/SEGGER_RTT )

Now we define a variable where we define the source files that we need to add to the build.

set( SRC_RTT_FILES
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_ASM_ARMv7M.S
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_printf.c 
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_Syscalls_GCC.c 
     ${SEGGER_RTT_SRCS}/SEGGER_RTT.c )

Add these files to the list of executable files.

add_executable(${EXECUTABLE} ${SRC_FILES} ${SRC_RTT_FILES} )

Finally we add the path to the include files.

target_include_directories(${EXECUTABLE} PRIVATE
        main
        CMSIS/Include
        CMSIS/Device/ST/STM32G0xx
        Device
        Drivers/Inc/
        Drivers/Inc/Legacy
        ${SEGGER_RTT_SRCS}
        )

Let’s look at the complete CMakeLists.txt file.

cmake_minimum_required(VERSION 3.23.1)

# Optional: print out extra messages to see what is going on. Comment it to have less verbose messages
set(CMAKE_VERBOSE_MAKEFILE ON)

# Path to toolchain file. This one has to be before 'project()' below
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/Buildtools.cmake)

# Setup project, output and linker file
project(VSC_G071RB)
set(EXECUTABLE ${PROJECT_NAME}.elf)
set(LINKER_FILE ${CMAKE_SOURCE_DIR}/Device/STM32G071RBTX_FLASH.ld)

enable_language(C ASM)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# Optional: issue a message to be sure it uses the correct toolchain file.
message(STATUS "CMAKE_TOOLCHAIN_FILE is: ${CMAKE_TOOLCHAIN_FILE}")

set( SEGGER_RTT_SRCS ${CMAKE_SOURCE_DIR}/Lib/SEGGER_RTT )

# List of source files
set(SRC_FILES
        main/main.c

        Device/stm32g0xx_it.c
        Device/system_stm32g0xx.c
        Device/startup_stm32g071xx.s
        Device/stm32g0xx_hal_msp.c
        
        Drivers/Src/stm32g0xx_hal.c 
        Drivers/Src/stm32g0xx_hal_rcc.c 
        Drivers/Src/stm32g0xx_hal_rcc_ex.c 
        Drivers/Src/stm32g0xx_hal_flash.c
        Drivers/Src/stm32g0xx_hal_flash_ex.c
        Drivers/Src/stm32g0xx_hal_dma.c
        Drivers/Src/stm32g0xx_hal_dma_ex.c
        Drivers/Src/stm32g0xx_hal_gpio.c 
        Drivers/Src/stm32g0xx_hal_cortex.c 
        Drivers/Src/stm32g0xx_hal_exti.c 
        Drivers/Src/stm32g0xx_hal_tim.c 
        Drivers/Src/stm32g0xx_hal_tim_ex.c 
        Drivers/Src/stm32g0xx_hal_pwr.c
        Drivers/Src/stm32g0xx_hal_pwr_ex.c
        )

set( SRC_RTT_FILES
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_ASM_ARMv7M.S
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_printf.c 
     ${SEGGER_RTT_SRCS}/SEGGER_RTT_Syscalls_GCC.c 
     ${SEGGER_RTT_SRCS}/SEGGER_RTT.c )

# Build the executable based on the source files
add_executable(${EXECUTABLE} ${SRC_FILES} ${SRC_RTT_FILES} )

# List of compiler defines, prefix with -D compiler option
target_compile_definitions(${EXECUTABLE} PRIVATE
        -DSTM32G071xx
        -DUSE_HAL_DRIVER
        )

# List of includ directories
target_include_directories(${EXECUTABLE} PRIVATE
        main
        CMSIS/Include
        CMSIS/Device/ST/STM32G0xx
        Device
        Drivers/Inc/
        Drivers/Inc/Legacy
        ${SEGGER_RTT_SRCS}
        )

# Compiler options
target_compile_options(${EXECUTABLE} PRIVATE
        -mcpu=cortex-m0
        -mthumb
        -mfloat-abi=soft
        -fdata-sections
        -ffunction-sections
        
        -Wall
        -O0
        -Og
        -gdwarf-2
        )

# Linker options
target_link_options(${EXECUTABLE} PRIVATE
        -T${LINKER_FILE}
        -mcpu=cortex-m0
        -mthumb
        -mfloat-abi=soft
        --specs=rdimon.specs
        --specs=nano.specs
        -lc
        -lm
        -lnosys
        -Wl,-Map=${PROJECT_NAME}.map,--cref
        -Wl,--gc-sections
        -Xlinker -print-memory-usage -Xlinker
        )

# Optional: Print executable size as part of the post build process
add_custom_command(TARGET ${EXECUTABLE}
        POST_BUILD
        COMMAND ${CMAKE_SIZE_UTIL} ${EXECUTABLE})

# Optional: Create hex, bin and S-Record files after the build
add_custom_command(TARGET ${EXECUTABLE}
        POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O srec --srec-len=64 ${EXECUTABLE} ${PROJECT_NAME}.s19
        COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE} ${PROJECT_NAME}.hex
        COMMAND ${CMAKE_OBJCOPY} -O binary ${EXECUTABLE} ${PROJECT_NAME}.bin)

Setting up IntelliSense

Let’s add the file path to our IntellinSense exemption configuration.

${workspaceFolder}/Lib/SEGGER_RTT

{
    "configurations": [
        {
            "name": "ARM none eabi GCC",
            "includePath": [
                "${workspaceFolder}/main",
                "${workspaceFolder}/CMSIS/Include",
                "${workspaceFolder}/CMSIS/Device/ST/STM32G0xx",
                "${workspaceFolder}/Device",
                "${workspaceFolder}/Drivers/Inc",
                "${workspaceFolder}/Drivers/Inc/Legacy",
                "${workspaceFolder}/Lib/SEGGER_RTT"
            ],
            "defines": [
                "STM32G071xx",
                "USE_HAL_DRIVER"
            ],
            "compilerPath": "${config:SetPathToolchainARM}/arm-none-eabi-gcc.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-arm"
        }
    ],
    "version": 4
}

Configure RTT

To use RTT, we need to call a couple of functions at the beginning of our program.

We add the header for initialization in main.h:
#include "SEGGER_RTT.h"

To initialize:
SEGGER_RTT_Init();

To send a message:
SEGGER_RTT_printf(0, "Send data RTT\r\n");

#include "main.h"

void SystemClock_Config(void);
static GPIO_InitTypeDef  GPIO_InitStruct = {0};

int main(void)
{  

  HAL_Init();

  SystemClock_Config();

  SEGGER_RTT_Init();

  __HAL_RCC_GPIOA_CLK_ENABLE();

  GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull  = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Pin = GPIO_PIN_5;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  while(1)
  {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    SEGGER_RTT_printf( 0, "Running App\r\n" );
    
    HAL_Delay(100);
  }

  return 0;
}

Debuggin RTT

In the article Visual Studio Code for ARM with CMake #7 Cortex Debug, we added the configuration file for debugging. Instructions were also included to enable the RTT output through the console.

          "rttConfig": {
            "enabled": true,
            "address": "auto",
            "decoders": [
                {
                    "port": 0,
                    "type": "console"
                }
            ]
        }

We compile the project again.

Archivos SEGGER RTT SDK

We start debugging to see the RTT messages.

Files SEGGER RTT SDK


Card image cap
Guillermo Garcia I am an embedded systems software engineer. I like constant learning, IoT systems and sharing knowledge


Comentarios... no existen comentarios.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Subscribe


Subscribe to receive the latest content.
Loading