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

En el artículo pasado Visual Studio Code for ARM with CMake #7 Cortex Debug realizamos la configuración para depurar en placa NUCLEO-G071RB. En este artículo vamos a agregar la librería SEGGER RTT para utilizar en nuestra depuración.

Con la conversión en el firmware de nuestro depurador ST-Link podemos utilizar funciones exclusivas de un depurador J-Link una de ellas es RTT.

Transferencia en tiempo real

J-Link SEGGER RTT utiliza la función de acceso a la memoria de fondo en el puerto de acceso de depuración (DAP) en MCU Cortex-M y RX para comunicarse entre la MCU y la aplicación host de la PC, a través de sondas J-Link.

RTT puede utilizarse en cualquier MCU ARM desde la interfaz SWO, la desventaja es que solo es compatible con depuradores J-Link.

No entraremos en detalles sobre cómo funciona RTT para eso hay muy buenos artículos como este.

RTT Library

Agregaremos una serie de archivos fuente necesarios para utilizar RTT no es necesario hacer ningún cambio en los archivos fuente estos archivos están en Github.

Los archivos necesarios los agregaremos al proyecto en la carpeta Lib.

Archivos SEGGER RTT SDK

Agregar a la Compilación

Es necesario agregar estos archivos a la compilación para esto vamos a nuestro archivo CMakeLists.txt.

Crearemos una variable llamada SEGGER_RTT_SRCS donde se configura la ruta del directorio donde están los archivos fuente.

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

Ahora definamos una variable donde definamos los archivos fuente que necesitamos agregar a la compilación.

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 )

Agregamos estos archivos a la lista de archivos ejecutables.

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

Finalmente agregamos la ruta de los archivos de inclusión.

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

Veamos el archivo CMakeLists.txt completo.

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)

Configurar IntelliSense

Agreguemos la ruta de los archivos a nuestra configuración de la exención IntellinSense.

${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
}

Configurar RTT

Para utilizar RTT necesitamos llamar a un par de funciones al inicio de nuestro programa.

Agregamos la cabecera para la inicialización en main.h #include SEGGER_RTT.h

Para inicializar SEGGER_RTT_Init( );

Para enviar un mensaje 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

En el articulo Visual Studio Code for ARM with CMake #7 Cortex Debug agregamos el archivo de configuración para la depuración, también se agregaron instrucciones para habilitar la salida de RTT mediante la consola.

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

Compilamos nuevamente el proyecto.

Archivos SEGGER RTT SDK

Iniciamos la depuración para ver los mensajes RTT.

Archivos 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