ARM SWO Performance Counters

In “Cycle Counting on ARM Cortex-M with DWT” I have used the ARM DWT register to count the executed cycles. With the MCUXpresso IDE comes with a very useful feature: it can capture the ARM SWO (Single Wire Output) trace data. One special kind of trace data is the ‘cycle counter’ information which is sent through SWO.

SWO Counters

SWO Counters

Preconditions

To use SWO trace and performance counters in MCUXpresso IDE, I need the following:

  • An ARM core capable to produce SWO trace data, e.g. ARM Cortex-M3 or M4, as on FRDM-K64F:

    Freescale FRDM-K64F Board

    Freescale FRDM-K64F Board

  • SWO trace pin present on the debug connector

    SWO Trace Connected to Debug Header

    SWO Trace Connected to Debug Header

  • The SWO pin on the processor is not used/muxed for anything else.
  • The trace clock should be enabled, either through the debugger or by the application code. See “Tutorial: Getting ETM Instruction Trace with NXP Kinetis ARM Cortex-M4F” for more details.
  • A debug probe capable to capture SWO data, e.g. the LinkServer debug connection in MCUXpresso IDE, e.g. with a LPC-Link2:
    LPC-Link2 debugging FRDM-K64F

    LPC-Link2 debugging FRDM-K64F

    💡 In the current v10.0.0 of the IDE, a LinkServer probe is required, e.g. a P&E or Segger won’t support the SWO trace in the MCUXpresso IDE. The NXP OpenSDA circuit on the Freedom and Tower boards do not support SWO.

Enable SWO Performance Counters

The SWO trace pin is sending the data based on the clock speed. Make sure that the speed is matching the core clock speed. To change the speed, I have to be connected with the target:

SWO Clock Speed

SWO Clock Speed

💡 In case the debug probe is not able to catch up with the clock speed, try to run the core with a lower clock speed.

I can add a new view (or open the existing one) with the ‘+’ icon for the performance view:

New Performance Counter View

New Performance Counter View

Performance Counters and more

To start collecting messages, use the ‘play’ button:

Start Performance Couter Processing

Start Performance Counter Processing

This will configure the ARM core to send out the SWO messages.

The most useful for me is the cycle counter: I can collect it while running, or measure the number of cycles, e.g. between break points:

Cycle Counter

Cycle Counter

The cycle counter gets incremented by every (core) clock cycle. This does not directly give the number of executed instructions, as cycles are spent during sleep or for load/store overhead. These overhead cycles are counted with the other SWO cycle counters shown in this view. To the number of instructions, see http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka16403.html

Still, the cycle counter alone is useful to measure time between two execution points for a first estimation, without the need for a full trace solution.

Summary

The ARM Cortex M3/M4 features SWO counters. The core can send the counter information out to the debugger. The MCUXpresso IDE has a built-in and easy to use way to collect these counters and show them in the SWO Counters view.

SWO does not mean counters only. There is more:

SWO Trace Views

SWO Trace Views

But I reserve these for one or more follow-up articles.

Happy Cycling 🙂

Links

8 thoughts on “ARM SWO Performance Counters

  1. Pingback: New NXP MCUXpresseo IDE V10.3.0 Release | MCU on Eclipse

  2. Pingback: SWO with NXP i.MX RT1064-EVK Board | MCU on Eclipse

  3. Dear Erich,

    Thank you for your tutorial.
    I want a counter that can count microsecond time (e.g. from 0.06us to 1sec with steps 0.09), I’m using k22fn512 development board. I tried to use the free counter components and assign the steps to the microsecond, but the issue that the counter reset in the middle of the process. If I increase the to seconds period the steps will be millisecond. Do you have any advice?

    Regards
    Banu

    Like

    • Hi Banu,
      are you using a Processor Expert component? Other than that, I would simply use a free-running timer with a prescaler/clock set to 1 us.
      If your counter resets: have you checked the number of available bits?

      Like

      • Hi Erich,
        Yes, I’m using processor expert component called “FreeCntr_LDD” and I set the counter frequency into 10.4MHz.
        Sorry, but what do you mean by “number of bits”?

        Like

        • I mean that the counter is 16bits only (the usual FTM timers on Kinetis). So it means that if you run the counter with around 10 MHz, it will overflow after about 4 ms.
          Have you set the counter to ‘Free-running’ mode without interrupts already?

          Liked by 1 person

  4. Pingback: SWO with ARM Cortex-M33 | MCU on Eclipse

  5. Pingback: Standalone SWO | MCU on Eclipse

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.