Rasmus Larsen
posted this on July 20, 2011 11:46
Q:
How can I use the SWO pin to output printf() statements to Keil µVision?
A:
1) Add #include <stdio.h> to the beginning of the file where you want to write your printf statement.
2) Enable SWO Output.
The simplest way of enabling the SWO line in the EFM32 is by using the setupSWO function. You can find this function in the energyAware Profiler, in the left pane, when you open up the program. Once this is run, the SWO output will be enabled to the correct pin on the Development Kit or the Starter Kit.
3) In the project options in Keil, go to the Debug tab and Press the Settings button next to the debugger selection (Should say Cortex-M/R J-LINK/J-Trace). Go to the Trace tab and check the Enable box as well as setting Core Clock to 14 MHz and the Prescaler under SWO Settings to Core Clock / 16.

4) Write printf("Hello world"); in your code after you have enabled the SWO output.
5) To enable the ARM compiler to send printf commands via the SWO interface you need to add the following lines to your code:
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
ITM_SendChar(ch);
return(ch);
}
6) Compile the code and download it to the Starter Kit/Development Kit. Enter a debug session.
7) Open the printf-viewer by going to View->Serial Windows->Debug (printf) Viewer
8) When you hit Run you should see the printf statement show up as below:
