Measuring code run time with SWD Debug Interface (SWO)

Source: Internet
Author: User

Using SWD Debug Interface to measure code run time problems About time measurement

In embedded, we often need to measure the execution time of a piece of code or measure the time of event triggering, the general idea is:

1: Reverse level at start point of measurement
2: Reverse the level again at the measurement end point

The reversal interval is then measured by an oscilloscope or a logic analyzer, which is the code time

This method is useful when measuring two or more time signals in sync, which is, in fact, the only way.

But if the other code controls the pin level or the periodic action in the measurement, it needs to be before the < action 1>
The preamble is added to facilitate the identification of a specific waveform in a complex waveform

Also, if the measured time value is very small, the oscilloscope is not easy to capture, even if it can be identified

SWD Solutions

In the arm cortex-m chip, the SWD debug interface replaces the traditional JTAG debug interface, which consumes less MCU pins while improving debug performance.

SWD consists of four lines, Swo,swdio,swclk,gnd;swdio and SWCLK constitute the basic serial debugging interface, SWO provides an information output channel,
Can output a lot of information, such as the execution time of the instruction or the ISR trigger event, so we can use the SWD interface with the IDE to get the code execution time, to some extent, replace the oscilloscope

Using SWD to measure time in IAR

Note: The following assumes that the reader is already familiar with the use of IAR, if you are not familiar with, please refer to the online corresponding getting Started tutorial

1: Configure SWD Clock

Here, we need to do this in the debugger options

1: Select SWD Debug Interface

2: Set the clock frequency to the MCU clock frequency, while the SWO clock is selected as the automatic recognition mode

After the configuration is complete, enter debug mode, select the J-LINK->SWD configuration page and focus on the actual value of the SWO clock, as shown below

If the display is incorrect, the SWO configuration is incorrect

2: Set the time-scale variable

In an oscilloscope scenario, we need a pin to measure the actual, and SWD is similar, except that the physical pin is replaced with a variable.

Here, we set the timeline variable as the time-scale variable

volatile 0;

volatileis to prevent TimeLine the compiler from being optimized, resulting in inaccurate measurement values.

3: Set Data Log

The data log is a debug breakpoint on the IAR, similar to a breakpoint, which records the corresponding changes in real time and is therefore called log.

In debug mode, right-click on the TimeLine variable and select "Set Data Log breakpoint for ' TimeLine" option

After setting, it will not be marked in red like a regular breakpoint, and under the IAR we need to enter Jlink's breakpoint usage to view, as shown

Here, we can see clearly that TimeLine has been set to read and write breakpoints

4: Enable data log and data log Summary

Under the Jlink menu, enter Data Log and Data Log Summary window, then right-click to selectEnable

Now that we've set it up, we can place the timeline at the beginning of the measurement and the end position.

5: Start Time measurement

Our test code is as follows

 while (1) {    0;    Index++    ; 1 ;    Index+ +;}

After completing the configuration as described above, click on Run full run for a few seconds, then pause, you can Data Log Data Log Summary see the corresponding information in the window

Why, why are all overflow??? What's wrong?

Remember when we first set the clock speed of SWO? When the time measurement point is too small and the sending rate is too fast, it will exceed the SWO transmission capacity, resulting in overflow problems

To verify that this is really the case, we will insert some software delay in the code, as shown below

void delay (volatile  uint32_t tick) {        while (tick--);} ....  while (1) {    0;    Index+ +;    Delay (0xFFFF);     1 ;    Index+ +;    Delay (0xFFFF);}

Recompile, enter debug mode, all is running, pause, OK, this time you can see everything is normal

/******************** Data Log window ************************/Time approx PC Description Type Value Address17040.43US X0x8000a6cTimeLine W0@0x20000390+?25232.68US X0x8000a7cTimeLine W1@0x20000390+?33424.99US X0x8000a6cTimeLine W0@0x20000390+?41617.24US X0x8000a7cTimeLine W1@0x20000390+?49809.54US X0x8000a6cTimeLine W0@0x20000390+?58001.79US X0x8000a7cTimeLine W1@0x20000390+?66194.10US X0x8000a6cTimeLine W0@0x20000390+?74386.35US X0x8000a7cTimeLine W1@0x20000390+?82578.65US X0x8000a6cTimeLine W0@0x20000390+?90770.90US X0x8000a7cTimeLine W1@0x20000390+?98963.21US X0x8000a6cTimeLine W0@0x20000390+?/********************* Data Log Summary window *****************/TimeLine Total accesses:335Read accesses:0Write accesses:335approximative time Count:335Overflow Count:0

Alternatively, we can open the Timeline option under the J-link menu (don't confuse our timeline variable)

As shown below

6: Precautions

When the SWO data volume is too large, prone to overshoot, the solution is in debug mode, single step, so as to avoid sending big data

Complete the full text, hope this article will help you ^_^

Measuring code run time with SWD Debug Interface (SWO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.