Redirecting printf to STlink Debugger on STM32F4-Discovery using Keil

Keywords: Embedded systems, ARM, STM32F4, UART, ITM, printf

Code Link: Source Code Github Link

In the previous tutorial we described how to redirect printf messages to UART. The problem with redirecting printf messages to UART is that it requires a dedicated UART apart from extra CPU cycles required to shift character to UART buffer. Fortunately ARM debug technology (Coresight) provides awesome tracing facility that provides debugging and tracing of embedded software with almost least interaction of CPU core. Apart from tracing facility, printf like messages can be rerouted to Coresight tracer.

This tutorial will be relatively short one and we will mostly focus on how to configure Keil to reroute printf messages to Stlink debugger on STM32F4-Discovery board.

The main routing entity behind Stlink debugger is ARM Instrumentation Trace Macrocell (ITM)Instrumentation Trace Macrocell (ITM) is a part of ARM Coresight debug technology. Coresight is optional for silicon manufacturer. STM32F407VG (Microcontroller on STM32F4-Discovery) implements part of Coreseight (ITM and few other) on its SoC. We will not go into more details of how ITM works as this is not in scope of this tutorial. For more details refer to ARM debug tutorial. We have dedicated tutorial on ARM Cortex-M debug technology.

Following are the steps to redirect printf messages to Stlink in Keil.

Steps:

1. Create a black project in for STM32F407VG (Microcontroller on STM32F4-Discovery board). Create a new main.c file with the simple main function as shown below.

#include <stdint.h>

extern void SystemCoreClockUpdate(void);

extern uint32_t SystemCoreClock;

// main function main.c
int main () {
	
   /*
      once the SystemCoreClockUpdate gets executed. 
      Examine SystemCoreClock in watch window.
   */  
   
   SystemCoreClockUpdate();
   
   while (1) {}
}

2. Build the project, connect STM32F4-Discovery board, and start debugger. Add SystemCoreClock to watch window (right click –> add SystemCoreClock to –> watch1). Put a break point atSS while (1) and run the project in debug mode. The debugger will stop the processor at break point. Examine and copy SystemCoreClock variable value from watch window (previously added to the watch window). We will need this value later in tracer configuration.

Note: The number shown in watch window for SystemCoreClock variable is system core frequency in Hertz (Hz), make sure to convert it to Mega-Hertz(Mhz) i.e. dividing it by 1000000.

3. Add tracer code from Keil run-time Environment manager. This can be done by clicking Manage Run-Time Environment () in Keil.
Click Compiler –> I/O –> STDIO –> check the enable check box and select “ITM” from drop down menu – Figure-1.

Figure-1: Keil run-time Environment Manager

4. Click on Options for target… (), Select Debug tab, from drop down menu on right select ST-Link Debugger Figure-2.

Figure-2: Project Build options…

5. Click on Settings button (Figure-2). The Cortex-M Target Driver Setup Dialog will appear. Click on Trace tab. Check Trace Enable and set Core Clock in Mhz to one we checked in Step-2. Enable Port-0 of ITM Stimulus Ports and un-check all privilege check boxes- Figure-3. Finally click OK.

Figure-3: Cortex-M Target Driver Setup

6. Add some printf messages to main function like shown in bellow code snippet.

#include <stdio.h>
#include <stdint.h>

extern void SystemCoreClockUpdate(void);

extern uint32_t SystemCoreClock;

int main () {

	volatile int i = 0;

  /*
    once the SystemCoreClockUpdate gets executed. 
    Examine SystemCoreClock in watch window.
  */  
  SystemCoreClockUpdate();

	while (1) {
		/*
			This string will be redirected to STLINK on STM32f4-Discovery
		*/
		printf("String redirection to STlink Debugger :-)\n");
		
		for (i = 0; i < 5000; i++);
	}
}

7. Build the Project, connect STM32F4-Discovery board to computer and start Debugger. Click View –> Serial Windows –> Debug (printf) Viewer.

8. Run the Program in debug mode. You will see printf output in Debug (printf) Viewer. See the bellow video demonstration.

Video demonstration:

Click the full screen button for more clear view.

References:

[1] – Reference manual-RM0090



26 thoughts on “Redirecting printf to STlink Debugger on STM32F4-Discovery using Keil”

Leave a Reply

Your email address will not be published. Required fields are marked *