How to display printf output over SWO in Keil µVision
05/140/2014 | 11:22 AM
This article explains how to redirect printf() to the Serial Wire Output (SWO) pin and how to display the output in Keil µVision.
1) Add
#include <stdio.h>
to the beginning of the file where you want to write your printf statement. 2) Enable SWO Output on the EFM32
The simplest way of enabling the SWO line in the EFM32 is by using the setupSWOForPrint() function. You can find this function under SWO Terminal in Simplicity Studio. The function was attached to this article. If you want to find the latest version open the SWO Terminal in Studio and check the Console viewer. 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 J-LINK/J-Trace Cortex). 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) Insert
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:
How to display printf output over SWO in Keil µVision
Chinese version
http://community.silabs.com/t5/Silicon-Labs%E4%B8%AD%E6%96%87%E6%8A%80%E6%9C%AF%E8%AE%BA%E5%9D%9B/%E5%A6%82%E4%BD%95%E5%9C%A8Keil-%C2%B5Vision%E4%B8%AD%E6%98%BE%E7%A4%BAMCU%E7%94%B1SWO%E8%BE%93%E5%87%BA%E7%9A%84%E4%BF%A1%E6%81%AF/ta-p/159282