Bare Metal

Setup STM32 Serial Output redirection on Atollic TrueSTUDIO

by | Aug 28, 2020 | Bare Metal

1 Introduction

Atollic TrueSTUDIO is one if the most common IDE’s used for STM32 Development. The Application, just like other IDEs, offer the capability to edit, compile and debug. But Atollic TrueSTUDIO with its many Analysis capabilities for debugging, it stands out as one of the best development environments for STM32 Boards.

This blog describes how to use Atollic TrueSTUDIO to redirect the printf() output into its console in the debugger perspective. This is one of the most basic, yet the one of the most popular and important tools you can use in a debug process.

 

2 Prerequisites

These instructions assume that you are using:

Atollic TrueSTUDIO V9.3.0
ST-Link JTAG

 

3 Configuring the code for serial Output re-direction

Atollic TrueSTUDIO, just like other GNU GCC compilers use the _write() function, which gets called by the printf() function, to redirect the output to the desired channel (LCD, UART, CAN etc..).

In the project waiting to be configured, open up the syscalls.c file. The _write() function that needs to get configured is generated here by the IDE.
Redeclare the function as shown below and the code will redirect the output to the debugger using the ITM (Instrumentation Trace Macrocell) port, using the ITM_SendChar().

Note : The header file for the chip family needs to be included in the syscalls.c file to import the ITM_SendChar(). eg. The STM32F4 series would need the header file stm32f4xx.h included.

 

int _write(int32_t file, uint8_t *ptr, int32_t len)
{
    int i=0;
    for(i=0 ; i<len ; i++)
            ITM_SendChar((*ptr++));

        errno = ENOSYS;
        return len;
}

4 Configuring the Debugger

The Serial Wire Viewer (SWV) is one of the most important debugger functionalities in the Atollic TrueSTUDIO IDE. The following options must be
enabled to configure the debugger for the SWV option :

Run > Debug Configurations > Debugger

Debug probe > ST-LINK

Select ‘Autostart local GDB Server’

Interface > SWD (Serial Wire Viewer) > Enable

Core Clock : 168 MHz – SWO Clock : 2000 kHz

Untick “Wait for Sync packet” if selected

Note : Make sure the core clock has the same frequency as the frequency showed in CubeMx clock configurations.

 

DebuggerConfig.jpg

 

5 Configure the Serial Wire Viewer (SWV)

Once the debugger settings have been set, launch a debug session and the IDE shall go into the debugger perspective and wait at the main.c breakpoint. At this point the Serial Wire Viewer Console need to be configured as follows:

The SWV Console Window (usually located bottom right) need to be configured to view serial data.

If the SWV console window is not displayed, go to Views menu SWV > SWV Console

Select Configure trace in the SWV console Window

Tick : Timestamps – Enable, Comparator 0 – Enable , ITM Stimulus Ports – Enable port 0, EXETRX – Enable.

Select ‘OK’ and then select ‘Start Trace’

Now that the configuration is done, select Run / Resume in the Run tab to execute the loaded image.

Note : All printf statements need a \n at the end of the string. If \n is not present, the string will not be printed.

 

SWVConfigWindow.jpg

 

 

Got A Project In Mind?

Our levels of professionalism and commitment to our clients results in a long waiting list for projects. Let us know in advance if you might need us.