J-Link SWO Viewer

From SEGGER Wiki
Jump to: navigation, search
J-Link SWO Viewer GUI version

The J-Link SWO Viewer is displays the terminal output of the target using the SWO pin. It is available as GUI and command line version as part of the J-Link Software and Documentation Pack, which is available for download on the SEGGER webpage. The J-Link SWO Viewer can be used in parallel with a debugger or stand-alone. This is especially useful when using debuggers which do not come with built-in support for SWO such as most GDB / GDB+Eclipse based debug environments.


J-Link SWO Viewer CL (command line version)

J-Link SWO Viewer GUI version

J-Link SWO Viewer CL is the command line-only version of the J-Link SWO Viewer. All commands available for J-Link SWO Viewer (GUI) can be used with J-Link SWO Viewer CL. Similar to the GUI Version, J-Link SWO Viewer CL asks for a device name or CPU clock speed at startup to be able to calculate the correct SWO speed or to connect to a running J-Link GDB Server. Using the syntax given in the List below), J-Link SWO Viewer CL can be directly started with preset parameters.

Usage

J-Link SWO Viewer GUI version

The J-Link SWO Viewer is available via the start menu. It asks for a device name or CPU clock speed at startup to be able to calculate the correct SWO speed or to connect to a running J-Link GDB Server.
Optionally you can select the preferred SWO clock speed from a drop down list. If nothing is selected for SWO clock speed then the debug probe will calculate the optimal value. To populate the drop down list the device needs to be selected or CPU clock speed must be measured once per session.

When running in normal mode J-Link SWO Viewer automatically performs the necessary initialization to enable SWO output on the target, in GDB Server mode the initialization has to be done by the debugger. Should you have a target connection already open e.g. a debug session in your IDE we recommend defining the parameters device name, CPU clock frequency and SWO clock frequency via command line to avoid connection errors. The stimulus port(s) from which SWO data is received can be chosen by using the port checkboxes 0 to 31 of the main window.

Command line options

J-Link SWO Viewer can also be controlled from the command line if used in a automated test environment etc. Minimum information needed by J-Link SWO Viewer is the device name (to enable CPU frequency auto detection) or the CPU clock speed. The following table lists all command line options available for the J-Link SWO Viewer:

Command Description
-cpufreq Select the CPU frequency.
-device Select the target device.
-ip Configure connection settings to IP IPAddress.
-itmmask Selects a set of itm stimulus ports which should be used to listen to.
-itmport Selects a itm stimulus port which should be used to listen to.
-outputfile Print the output of SWO Viewer to the selected file.
-settingsfile Specify a J-Link settings file.
-swoattach Starts the J-Link SWO Viewer in SWO attach mode.
-swofreq Select the SWO frequency.
-usb Configure connection settings to USB S/N.

-cpufreq

Defines the current CPU speed in Hz that the CPU is running at. If the CPU is for example running at 96 MHz, the command line should look as below.

Syntax

-cpufreq <CPUFreq>

Example

-cpufreq 96000000

-device

Select the target device to enable the CPU frequency auto detection of the J-Link DLL. To select a ST STM32F207IG as target device, the command line should look as below. For a list of all supported device names, please refer to the list of supported devices.

Syntax

-device <DeviceID>

Example

-device STM32F207IG

-ip

Selects IP as host interface to connect to J-Link. Default host interface is USB.

Syntax

-ip <IPAddr>

Example

-ip 192.168.1.17

-itmmask

Defines a set of stimulus ports from which SWO data is received and displayed by SWO Viewer. If itmmask is given, itmport will be ignored.

Syntax

-itmmask <Mask>

Example

Listen on ports 0 and 2:

-itmmask 0x5

-itmport

Defines the stimulus port from which SWO data is received and displayed by the SWO Viewer. Default is stimulus port 0. The command line should look as below.

Syntax

-itmport <ITMPortIndex>

Example

-itmport 0

-outputfile

Define a file to which the output of SWO Viewer is printed.

Syntax

-outputfile <PathToFile>

Example

-outputfile "C:\Temp\Output.log"

-settingsfile

Select a J-Link settings file to use for the target device.

Syntax

-settingsfile <PathToFile>

Example

-settingsfile "C:\Temp\Settings.jlink"

-swoattach

Starts the J-Link SWO Viewer in SWO attach mode which means that it will not perform any SWO init on the target but relies on the target application to perform the SWO init instead.
Note: When using this option, also the option -swofreq must be passed.

Syntax

-swoattach <OnOff>

Example

-swoattach 1

-swofreq

Defines the SWO frequency that shall be used by J-Link SWO Viewer for sampling SWO data. Usually not necessary to define since optimal SWO speed is calculated automatically based on the CPU frequency and the capabilities of the connected J-Link. If the targeted SWO speed is 6 MHz the command line should look as follows.

Syntax

-swofreq <SWOFreq>

Example

-swofreq 6000000

-usb

Configures the connection settings according to defined USB S/N. Usually not necessary to define if only one debug device is connected to the PC.

Syntax

-usb <S/N>

Example

-usb 01234567

Configure SWO output after device reset

In some situations it might happen that the target application is reset and it is desired to log the SWO output of the target after reset during the booting process. For such situations, the target application itself needs to initialize the CPU for SWO output, since the SWO Viewer is not restarted but continuously running.

Example code for enabling SWO via target application

/*********************************************************************
*                   (c) SEGGER Microcontroller GmbH                  *
*                        The Embedded Experts                        *
*                           www.segger.com                           *
**********************************************************************
-------------------------- END-OF-HEADER -----------------------------
File    : SWO.c
Purpose : Sample to initialize SWO via target application.
Literature:
  [1]  UM08001 J-Link / J-Trace User Guide
*/

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ITM_ENA   (*(volatile unsigned int*)0xE0000E00) // ITM Enable
#define ITM_TPR   (*(volatile unsigned int*)0xE0000E40) // Trace Privilege
                                                        // Register
#define ITM_TCR   (*(volatile unsigned int*)0xE0000E80) // ITM Trace Control Reg.
#define ITM_LSR   (*(volatile unsigned int*)0xE0000FB0) // ITM Lock Status
                                                        // Register
#define DHCSR     (*(volatile unsigned int*)0xE000EDF0) // Debug register
#define DEMCR     (*(volatile unsigned int*)0xE000EDFC) // Debug register
#define TPIU_ACPR (*(volatile unsigned int*)0xE0040010) // Async Clock
                                                        // prescaler register
#define TPIU_SPPR (*(volatile unsigned int*)0xE00400F0) // Selected Pin Protocol
                                                        // Register
#define DWT_CTRL  (*(volatile unsigned int*)0xE0001000) // DWT Control Register
#define FFCR      (*(volatile unsigned int*)0xE0040304) // Formatter and flush
                                                        // Control Register

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
U32 _ITMPort  = 0; // The stimulus port from which SWO data is received
                   // and displayed.
U32 TargetDiv = 1; // Has to be calculated according to
                   // the CPU speed and the output baud rate

/*********************************************************************
*
*       Static functions
*
**********************************************************************
*/

/*********************************************************************
*
*       _EnableSWO()
*
* Function description
*   Enables SWO from target side.
*
*/
static void _EnableSWO() {
  U32 StimulusRegs;
  //
  // Enable access to SWO registers
  //
  DEMCR |= (1 << 24);
  ITM_LSR = 0xC5ACCE55;
  //
  // Initially disable ITM and stimulus port
  // To make sure that nothing is transferred via SWO
  // when changing the SWO prescaler etc.
  //
  StimulusRegs = ITM_ENA;
  StimulusRegs &= ~(1 << _ITMPort);
  ITM_ENA = StimulusRegs; // Disable ITM stimulus port
  ITM_TCR = 0; // Disable ITM
  //
  // Initialize SWO (prescaler, etc.)
  //
  TPIU_SPPR = 0x00000002; // Select NRZ mode
  TPIU_ACPR = TargetDiv - 1; // Example: 72/48 = 1,5 MHz
  ITM_TPR = 0x00000000;
  DWT_CTRL = 0x400003FE;
  FFCR = 0x00000100;
  //
  // Enable ITM and stimulus port
  //
  ITM_TCR = 0x1000D; // Enable ITM
  ITM_ENA = StimulusRegs | (1 << _ITMPort); // Enable ITM stimulus port
}

/*************************** End of file ****************************/

Example code for terminal output

This section provides a simple implementation for output via SWO for Cortex-M processors. It can be used with any IDE. This sample implementation ensures that output via SWO is enabled in order to guarantee that the application does not hang.

/*********************************************************************
*                   (c) SEGGER Microcontroller GmbH                  *
*                        The Embedded Experts                        *
*                           www.segger.com                           *
**********************************************************************
-------------------------- END-OF-HEADER -----------------------------
File    : SWO.c
Purpose : Simple implementation for output via SWO for Cortex-M processors.
          It can be used with any IDE. This sample implementation ensures
          that output via SWO is enabled in order to guarantee that the
          application does not hang.
Literature:
  [1]  UM08001 J-Link / J-Trace User Guide
*/

/*********************************************************************
*
*       Prototypes (to be placed in a header file such as SWO.h)
*
**********************************************************************
*/
void SWO_PrintChar  (char c);
void SWO_PrintString(const char *s);

/*********************************************************************
*
*       Defines for Cortex-M debug unit
*
**********************************************************************
*/
#define ITM_STIM_U32 (*(volatile unsigned int*)0xE0000000) // STIM word access
#define ITM_STIM_U8  (*(volatile         char*)0xE0000000) // STIM Byte access
#define ITM_ENA      (*(volatile unsigned int*)0xE0000E00) // ITM Enable Register
#define ITM_TCR      (*(volatile unsigned int*)0xE0000E80) // ITM Trace Control
                                                           // Register

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       SWO_PrintChar()
*
* Function description
*   Checks if SWO is set up. If it is not, return,
*   to avoid program hangs if no debugger is connected.
*   If it is set up, print a character to the ITM_STIM register
*   in order to provide data for SWO.
* Parameters
*   c:    The character to be printed.
* Notes
*   Additional checks for device specific registers can be added.
*/
void SWO_PrintChar(char c) {
  //
  // Check if ITM_TCR.ITMENA is set
  //
  if ((ITM_TCR & 1) == 0) {
    return;
  }
  //
  // Check if stimulus port is enabled
  //
  if ((ITM_ENA & 1) == 0) {
    return;
  }
  //
  // Wait until STIMx is ready,
  // then send data
  //
  while ((ITM_STIM_U8 & 1) == 0);
  ITM_STIM_U8 = c;
}

/*********************************************************************
*
*       SWO_PrintString()
*
* Function description
*   Print a string via SWO.
*
*/
void SWO_PrintString(const char *s) {
  //
  // Print out character per character
  //
  while (*s) {
    SWO_PrintChar(*s++);
  }
}

/*************************** End of file ****************************/