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.

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

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.

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
Pretty portion of content. I just stumbled upon your blog and
in accession capital to claim that I get in fact enjoyed account your weblog posts.
Any way I’ll be subscribing to your feeds or even I fulfillment you access
persistently fast.
Hi there just wanted to give you a quick heads up. The words
in your article seem to be running off the screen in Firefox.
I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d
post to let you know. The layout look great though! Hope you get the
problem fixed soon. Many thanks
Hi there to every one, as I am really eager of reading
this website’s post to be updated daily. It
contains pleasant data.
hi!,I really like your writing so a lot! proportion we keep up a correspondence extra approximately your article
on AOL? I require an expert on this area to solve my problem.
Maybe that is you! Taking a look ahead to look you.
What’s up, the whole thing is going fine here and ofcourse every one is sharing facts, that’s really excellent, keep up writing.
This web site really has all of the information and facts I wanted concerning this subject
and didn’t know who to ask.
Hey there, I think your site might be having browser
compatibility issues. When I look at your website in Ie, it
looks fine but when opening in Internet Explorer, it
has some overlapping. I just wanted to give you a quick heads up!
Other then that, fantastic blog!
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using Movable-type on a number of websites for about a year and am
concerned about switching to another platform.
I have heard good things about blogengine.net. Is there a way I can transfer all my wordpress content
into it? Any help would be greatly appreciated!
Unquestionably imagine that that you said. Your favorite reason seemed to be at the
internet the simplest thing to remember of. I say to
you, I certainly get annoyed even as other people think about worries
that they plainly don’t know about. You controlled to hit the nail upon the
highest and outlined out the whole thing with no need side-effects ,
people could take a signal. Will probably be back
to get more. Thank you
Thanks designed for sharing such a pleasant thinking,
post is good, thats why i have read it entirely quest bars http://bit.ly/3jZgEA2 quest bars
excellent submit, very informative. I ponder why the opposite
specialists of this sector do not notice this. You must continue your writing.
I am confident, you have a huge readers’ base already!
asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
Hi there, just wanted to tell you, I liked this article.
It was inspiring. Keep on posting! cheap flights http://1704milesapart.tumblr.com/ cheap flights
Thanks for your marvelous posting! I certainly enjoyed reading it, you are a great author.
I will always bookmark your blog and definitely will come back very soon. I want
to encourage one to continue your great work, have a nice
morning! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
I couldn’t resist commenting
It’s impressive that you are getting thoughts from this piece of
writing as well as from our dialogue made at this time.
scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
Have you ever thought about creating an ebook or guest authoring
on other websites? I have a blog centered on the same ideas you discuss and would really like to
have you share some stories/information. I know my readers
would appreciate your work. If you’re even remotely interested,
feel free to send me an e-mail. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
I used to be recommended this web site by my cousin. I’m not positive whether or not
this publish is written via him as nobody else recognize such precise approximately my problem.
You are amazing! Thanks! ps4 https://bitly.com/3z5HwTp ps4 games
Very nice post. I just stumbled upon your weblog and wanted
to say that I’ve truly enjoyed surfing around your blog posts.
In any case I’ll be subscribing to your rss feed and I hope
you write again very soon!
If users plan on printing their digital photos, they should adapt the white balance before they start shooting.
Making adjustments in the middle of a shoot can be frustrating with the camera’s odd menu layout.Get Camera at Amazon
Acne skincare also recommends removing your make up using a
constitute remover, and this should happen before
you go to bed (not each day).Shop now Skin Care at Amazon
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how could we communicate?
I¦ll immediately clutch your rss feed as I can not in finding your email subscription link or e-newsletter service. Do you’ve any? Kindly allow me realize in order that I may just subscribe. Thanks.
Heya i am for the first time here. I came across
this board and I find It really useful & it helped
me out much. I hope to give something back and aid others like you aided me.
https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes
Hello my family member! I want to say that this post is amazing, nice written and come with approximately all vital infos. I would like to look more posts like this.
As I website possessor I think the subject material here is very fantastic, thanks for your efforts.
Excellent post. Keep posting such kind of info on your
site. Im really impressed by your blog.
Hello there, You have done a great job.
I’ll definitely digg it and personally recommend to my
friends. I am sure they’ll be benefited from this web site.
I love the efforts you have put in this, appreciate it for all the great content.
Thanks for sharing your thoughts on on. Regards
Excellent post. I used to be checking continuously this weblog
and I’m inspired! Very useful information specially the final section 🙂 I deal with such info much.
I used to be seeking this particular information for a long time.
Thanks and good luck.
Its like you read my thoughts! You seem to understand
a lot about this, such as you wrote the book in it or something.
I feel that you just could do with a few % to power the message house a bit, however instead of that, this is magnificent blog.
A fantastic read. I will definitely be back.
These are genuinely wonderful ideas in on the topic of blogging.
You have touched some nice points here. Any way keep up wrinting.
It’s perfect time to make a few plans for the long run and
it is time to be happy. I have read this submit and
if I may just I want to counsel you some fascinating
things or advice. Maybe you could write next articles regarding this article.
I wish to learn even more issues approximately it!
Look at my web page … tracfone 2022